Initializes the GenerateAnswerNode class. Args: input (str): The input data type for the node. output (List[str]): The output data type(s) for the node. node_config (Optional[dict]): Configuration dictionary for the node, which includes the LLM model, verbos
| 28 | |
| 29 | |
| 30 | class GenerateAnswerNode(BaseNode): |
| 31 | """ |
| 32 | Initializes the GenerateAnswerNode class. |
| 33 | |
| 34 | Args: |
| 35 | input (str): The input data type for the node. |
| 36 | output (List[str]): The output data type(s) for the node. |
| 37 | node_config (Optional[dict]): Configuration dictionary for the node, |
| 38 | which includes the LLM model, verbosity, schema, and other settings. |
| 39 | Defaults to None. |
| 40 | node_name (str): The name of the node. Defaults to "GenerateAnswer". |
| 41 | |
| 42 | Attributes: |
| 43 | llm_model: The language model specified in the node configuration. |
| 44 | verbose (bool): Whether verbose mode is enabled. |
| 45 | force (bool): Whether to force certain behaviors, overriding defaults. |
| 46 | script_creator (bool): Whether the node is in script creation mode. |
| 47 | is_md_scraper (bool): Whether the node is scraping markdown data. |
| 48 | additional_info (Optional[str]): Any additional information to be |
| 49 | included in the prompt templates. |
| 50 | """ |
| 51 | |
| 52 | def __init__( |
| 53 | self, |
| 54 | input: str, |
| 55 | output: List[str], |
| 56 | node_config: Optional[dict] = None, |
| 57 | node_name: str = "GenerateAnswer", |
| 58 | ): |
| 59 | super().__init__(node_name, "node", input, output, 2, node_config) |
| 60 | self.llm_model = node_config["llm_model"] |
| 61 | |
| 62 | if isinstance(node_config["llm_model"], ChatOllama): |
| 63 | if node_config.get("schema", None) is None: |
| 64 | self.llm_model.format = "json" |
| 65 | else: |
| 66 | self.llm_model.format = self.node_config["schema"].model_json_schema() |
| 67 | |
| 68 | self.verbose = node_config.get("verbose", False) |
| 69 | self.force = node_config.get("force", False) |
| 70 | self.script_creator = node_config.get("script_creator", False) |
| 71 | self.is_md_scraper = node_config.get("is_md_scraper", False) |
| 72 | self.additional_info = node_config.get("additional_info") |
| 73 | self.timeout = node_config.get("timeout", 480) |
| 74 | |
| 75 | def invoke_with_timeout(self, chain, inputs, timeout): |
| 76 | """Helper method to invoke chain with timeout""" |
| 77 | try: |
| 78 | start_time = time.time() |
| 79 | response = chain.invoke(inputs) |
| 80 | if time.time() - start_time > timeout: |
| 81 | raise Timeout(f"Response took longer than {timeout} seconds") |
| 82 | return response |
| 83 | except Timeout as e: |
| 84 | self.logger.error(f"Timeout error: {str(e)}") |
| 85 | raise |
| 86 | except Exception as e: |
| 87 | self.logger.error(f"Error during chain execution: {str(e)}") |
no outgoing calls