|
| 1 | +""" |
| 2 | +KnowledgeGraphNode Module |
| 3 | +""" |
| 4 | + |
| 5 | +# Imports from standard library |
| 6 | +from typing import List, Optional |
| 7 | +from tqdm import tqdm |
| 8 | + |
| 9 | +# Imports from Langchain |
| 10 | +from langchain.prompts import PromptTemplate |
| 11 | +from langchain_core.output_parsers import JsonOutputParser |
| 12 | + |
| 13 | +# Imports from the library |
| 14 | +from .base_node import BaseNode |
| 15 | + |
| 16 | + |
| 17 | +class KnowledgeGraphNode(BaseNode): |
| 18 | + """ |
| 19 | + A node responsible for generating a knowledge graph from a dictionary. |
| 20 | +
|
| 21 | + Attributes: |
| 22 | + llm_model: An instance of a language model client, configured for generating answers. |
| 23 | + verbose (bool): A flag indicating whether to show print statements during execution. |
| 24 | +
|
| 25 | + Args: |
| 26 | + input (str): Boolean expression defining the input keys needed from the state. |
| 27 | + output (List[str]): List of output keys to be updated in the state. |
| 28 | + node_config (dict): Additional configuration for the node. |
| 29 | + node_name (str): The unique identifier name for the node, defaulting to "GenerateAnswer". |
| 30 | + """ |
| 31 | + |
| 32 | + def __init__(self, input: str, output: List[str], node_config: Optional[dict] = None, |
| 33 | + node_name: str = "KnowledgeGraph"): |
| 34 | + super().__init__(node_name, "node", input, output, 2, node_config) |
| 35 | + |
| 36 | + self.llm_model = node_config["llm_model"] |
| 37 | + self.verbose = False if node_config is None else node_config.get( |
| 38 | + "verbose", False) |
| 39 | + |
| 40 | + def execute(self, state: dict) -> dict: |
| 41 | + """ |
| 42 | + Executes the node's logic to create a knowledge graph from a dictionary. |
| 43 | +
|
| 44 | + Args: |
| 45 | + state (dict): The current state of the graph. The input keys will be used |
| 46 | + to fetch the correct data from the state. |
| 47 | +
|
| 48 | + Returns: |
| 49 | + dict: The updated state with the output key containing the generated answer. |
| 50 | +
|
| 51 | + Raises: |
| 52 | + KeyError: If the input keys are not found in the state, indicating |
| 53 | + that the necessary information for generating an answer is missing. |
| 54 | + """ |
| 55 | + |
| 56 | + if self.verbose: |
| 57 | + print(f"--- Executing {self.node_name} Node ---") |
| 58 | + |
| 59 | + # Interpret input keys based on the provided input expression |
| 60 | + input_keys = self.get_input_keys(state) |
| 61 | + |
| 62 | + # Fetching data from the state based on the input keys |
| 63 | + input_data = [state[key] for key in input_keys] |
| 64 | + |
| 65 | + user_prompt = input_data[0] |
| 66 | + answer_dict = input_data[1] |
| 67 | + |
| 68 | + output_parser = JsonOutputParser() |
| 69 | + format_instructions = output_parser.get_format_instructions() |
| 70 | + |
| 71 | + template_merge = """ |
| 72 | + You are a website scraper and you have just scraped some content from multiple websites.\n |
| 73 | + You are now asked to provide an answer to a USER PROMPT based on the content you have scraped.\n |
| 74 | + You need to merge the content from the different websites into a single answer without repetitions (if there are any). \n |
| 75 | + The scraped contents are in a JSON format and you need to merge them based on the context and providing a correct JSON structure.\n |
| 76 | + OUTPUT INSTRUCTIONS: {format_instructions}\n |
| 77 | + USER PROMPT: {user_prompt}\n |
| 78 | + WEBSITE CONTENT: {website_content} |
| 79 | + """ |
| 80 | + |
| 81 | + prompt_template = PromptTemplate( |
| 82 | + template=template_merge, |
| 83 | + input_variables=["user_prompt"], |
| 84 | + partial_variables={ |
| 85 | + "format_instructions": format_instructions, |
| 86 | + "website_content": answers_str, |
| 87 | + }, |
| 88 | + ) |
| 89 | + |
| 90 | + merge_chain = prompt_template | self.llm_model | output_parser |
| 91 | + answer = merge_chain.invoke({"user_prompt": user_prompt}) |
| 92 | + |
| 93 | + # Update the state with the generated answer |
| 94 | + state.update({self.output[0]: answer}) |
| 95 | + return state |
0 commit comments