A node responsible for compressing the input tokens and storing the document in a vector database for retrieval. Relevant chunks are stored in the state. It allows scraping of big documents without exceeding the token limit of the language model. Attributes: llm_model: An
| 13 | |
| 14 | |
| 15 | class DescriptionNode(BaseNode): |
| 16 | """ |
| 17 | A node responsible for compressing the input tokens and storing the document |
| 18 | in a vector database for retrieval. Relevant chunks are stored in the state. |
| 19 | |
| 20 | It allows scraping of big documents without exceeding the token limit of the language model. |
| 21 | |
| 22 | Attributes: |
| 23 | llm_model: An instance of a language model client, configured for generating answers. |
| 24 | verbose (bool): A flag indicating whether to show print statements during execution. |
| 25 | |
| 26 | Args: |
| 27 | input (str): Boolean expression defining the input keys needed from the state. |
| 28 | output (List[str]): List of output keys to be updated in the state. |
| 29 | node_config (dict): Additional configuration for the node. |
| 30 | node_name (str): The unique identifier name for the node, defaulting to "Parse". |
| 31 | """ |
| 32 | |
| 33 | def __init__( |
| 34 | self, |
| 35 | input: str, |
| 36 | output: List[str], |
| 37 | node_config: Optional[dict] = None, |
| 38 | node_name: str = "DESCRIPTION", |
| 39 | ): |
| 40 | super().__init__(node_name, "node", input, output, 2, node_config) |
| 41 | self.llm_model = node_config["llm_model"] |
| 42 | self.verbose = ( |
| 43 | False if node_config is None else node_config.get("verbose", False) |
| 44 | ) |
| 45 | self.cache_path = node_config.get("cache_path", False) |
| 46 | |
| 47 | def execute(self, state: dict) -> dict: |
| 48 | self.logger.info(f"--- Executing {self.node_name} Node ---") |
| 49 | |
| 50 | docs = list(state.get("docs")) |
| 51 | |
| 52 | chains_dict = {} |
| 53 | |
| 54 | for i, chunk in enumerate( |
| 55 | tqdm(docs, desc="Processing chunks", disable=not self.verbose) |
| 56 | ): |
| 57 | prompt = PromptTemplate( |
| 58 | template=DESCRIPTION_NODE_PROMPT, |
| 59 | partial_variables={"content": chunk.get("document")}, |
| 60 | ) |
| 61 | chain_name = f"chunk{i + 1}" |
| 62 | chains_dict[chain_name] = prompt | self.llm_model |
| 63 | |
| 64 | async_runner = RunnableParallel(**chains_dict) |
| 65 | batch_results = async_runner.invoke({}) |
| 66 | |
| 67 | for i in range(1, len(docs) + 1): |
| 68 | docs[i - 1]["summary"] = batch_results.get(f"chunk{i}").content |
| 69 | |
| 70 | state.update({self.output[0]: docs}) |
| 71 | |
| 72 | return state |