|
| 1 | +""" |
| 2 | +PromptRefinerNode Module |
| 3 | +""" |
| 4 | +from typing import List, Optional |
| 5 | +from langchain.prompts import PromptTemplate |
| 6 | +from langchain_core.output_parsers import StrOutputParser |
| 7 | +from langchain_community.chat_models import ChatOllama |
| 8 | +from .base_node import BaseNode |
| 9 | +from ..utils import transform_schema |
| 10 | +from ..prompts import ( |
| 11 | + TEMPLATE_REASONING, TEMPLATE_REASONING_WITH_CONTEXT |
| 12 | +) |
| 13 | + |
| 14 | +class ReasoningNode(BaseNode): |
| 15 | + """ |
| 16 | + A node that refine the user prompt with the use of the schema and additional context and |
| 17 | + create a precise prompt in subsequent steps that explicitly link elements in the user's |
| 18 | + original input to their corresponding representations in the JSON schema. |
| 19 | +
|
| 20 | + Attributes: |
| 21 | + llm_model: An instance of a language model client, configured for generating answers. |
| 22 | + verbose (bool): A flag indicating whether to show print statements during execution. |
| 23 | +
|
| 24 | + Args: |
| 25 | + input (str): Boolean expression defining the input keys needed from the state. |
| 26 | + output (List[str]): List of output keys to be updated in the state. |
| 27 | + node_config (dict): Additional configuration for the node. |
| 28 | + node_name (str): The unique identifier name for the node, defaulting to "GenerateAnswer". |
| 29 | + """ |
| 30 | + |
| 31 | + def __init__( |
| 32 | + self, |
| 33 | + input: str, |
| 34 | + output: List[str], |
| 35 | + node_config: Optional[dict] = None, |
| 36 | + node_name: str = "PromptRefiner", |
| 37 | + ): |
| 38 | + super().__init__(node_name, "node", input, output, 2, node_config) |
| 39 | + |
| 40 | + self.llm_model = node_config["llm_model"] |
| 41 | + |
| 42 | + if isinstance(node_config["llm_model"], ChatOllama): |
| 43 | + self.llm_model.format="json" |
| 44 | + |
| 45 | + self.verbose = ( |
| 46 | + True if node_config is None else node_config.get("verbose", False) |
| 47 | + ) |
| 48 | + self.force = ( |
| 49 | + False if node_config is None else node_config.get("force", False) |
| 50 | + ) |
| 51 | + |
| 52 | + self.additional_info = node_config.get("additional_info", None) |
| 53 | + |
| 54 | + self.output_schema = node_config.get("schema") |
| 55 | + |
| 56 | + def execute(self, state: dict) -> dict: |
| 57 | + """ |
| 58 | + Generate a refined prompt for the reasoning task based |
| 59 | + on the user's input and the JSON schema. |
| 60 | +
|
| 61 | + Args: |
| 62 | + state (dict): The current state of the graph. The input keys will be used |
| 63 | + to fetch the correct data from the state. |
| 64 | +
|
| 65 | + Returns: |
| 66 | + dict: The updated state with the output key containing the generated answer. |
| 67 | +
|
| 68 | + Raises: |
| 69 | + KeyError: If the input keys are not found in the state, indicating |
| 70 | + that the necessary information for generating an answer is missing. |
| 71 | + """ |
| 72 | + |
| 73 | + self.logger.info(f"--- Executing {self.node_name} Node ---") |
| 74 | + |
| 75 | + user_prompt = state['user_prompt'] |
| 76 | + |
| 77 | + self.simplefied_schema = transform_schema(self.output_schema.schema()) |
| 78 | + |
| 79 | + if self.additional_info is not None: |
| 80 | + prompt = PromptTemplate( |
| 81 | + template=TEMPLATE_REASONING_WITH_CONTEXT, |
| 82 | + partial_variables={"user_input": user_prompt, |
| 83 | + "json_schema": str(self.simplefied_schema), |
| 84 | + "additional_context": self.additional_info}) |
| 85 | + else: |
| 86 | + prompt = PromptTemplate( |
| 87 | + template=TEMPLATE_REASONING, |
| 88 | + partial_variables={"user_input": user_prompt, |
| 89 | + "json_schema": str(self.simplefied_schema)}) |
| 90 | + |
| 91 | + output_parser = StrOutputParser() |
| 92 | + |
| 93 | + chain = prompt | self.llm_model | output_parser |
| 94 | + refined_prompt = chain.invoke({}) |
| 95 | + |
| 96 | + state.update({self.output[0]: refined_prompt}) |
| 97 | + return state |
0 commit comments