Skip to main content

DescriptionNode

Overview​

DescriptionNode is 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 instance of a language model client, configured for generating answers.
  • verbose (bool): A flag indicating whether to show print statements during execution.

Args​

  • input (str): Boolean expression defining the input keys needed from the state.
  • output (List[str]): List of output keys to be updated in the state.
  • node_config (dict): Additional configuration for the node.
  • node_name (str): The unique identifier name for the node, defaulting to "DESCRIPTION".

Example​

description_node = DescriptionNode(
input="docs",
output=["docs"],
node_config={
"llm_model": llm_model_instance,
"verbose": True,
"cache_path": "/path/to/cache"
}
)

Methods​

init(self, input: str, output: List[str], node_config: Optional[dict] = None, node_name: str = "DESCRIPTION")

Initializes the DescriptionNode with the given input, output, node configuration, and node name.

execute(self, state: dict) -> dict

Executes the node's logic to compress the input tokens and store the document in a vector database for retrieval.

Args

state (dict): The current state of the graph. The input keys will be used to fetch the correct data types from the state. Returns

dict: The updated state with a new output key containing the compressed documents. Detailed Implementation

execute(self, state: dict) -> dict

Logging: Logs the start of the node execution. Fetch Documents: Retrieves the documents from the state. Process Chunks: Iterates over each document chunk and creates a prompt for each chunk using PromptTemplate. Run Chains: Uses RunnableParallel to run the prompts in parallel and generate summaries. Update State: Updates the state with the summarized documents.

Example Usage​

description_node = DescriptionNode(
input="docs",
output=["docs"],
node_config={
"llm_model": llm_model_instance,
"verbose": True,
"cache_path": "/path/to/cache"
}
)

state = {
"docs": [
{"document": "Document content 1"},
{"document": "Document content 2"},
]
}

updated_state = description_node.execute(state)