actualbrain's picture
score-45, gpt-4.1
3c0a133
raw
history blame contribute delete
3.12 kB
"""State definitions.
State is the interface between the graph and end user as well as the
data model used internally by the graph.
"""
import operator
from dataclasses import dataclass, field
from typing import Annotated, Any, List, Optional
from langchain_core.messages import BaseMessage
from langgraph.graph import add_messages
@dataclass(kw_only=True)
class InputState:
"""Input state defines the interface between the graph and the user (external API)."""
question: str
"The question for which the agent is tasked to gather information."
task_id: str
"The ID of the task being processed"
info: Optional[dict[str, Any]] = field(default=None)
"The info state tracks the current extracted data for the given question, conforming to the provided schema. This is primarily populated by the agent."
@dataclass(kw_only=True)
class State(InputState):
"""A graph's State defines three main things.
1. The structure of the data to be passed between nodes (which "channels" to read from/write to and their types)
2. Default values for each field
3. Reducers for the state's fields. Reducers are functions that determine how to apply updates to the state.
See [Reducers](https://langchain-ai.github.io/langgraph/concepts/low_level/#reducers) for more information.
"""
messages: Annotated[List[BaseMessage], add_messages] = field(default_factory=list)
"""
Messages track the primary execution state of the agent.
Typically accumulates a pattern of:
1. HumanMessage - user input
2. AIMessage with .tool_calls - agent picking tool(s) to use to collect
information
3. ToolMessage(s) - the responses (or errors) from the executed tools
(... repeat steps 2 and 3 as needed ...)
4. AIMessage without .tool_calls - agent responding in unstructured
format to the user.
5. HumanMessage - user responds with the next conversational turn.
(... repeat steps 2-5 as needed ... )
Merges two lists of messages, updating existing messages by ID.
By default, this ensures the state is "append-only", unless the
new message has the same ID as an existing message.
Returns:
A new list of messages with the messages from `right` merged into `left`.
If a message in `right` has the same ID as a message in `left`, the
message from `right` will replace the message from `left`.
"""
loop_step: Annotated[int, operator.add] = field(default=0)
# Feel free to add additional attributes to your state as needed.
# Common examples include retrieved documents, extracted entities, API connections, etc.
@dataclass(kw_only=True)
class OutputState:
"""The response object for the end user.
This class defines the structure of the output that will be provided
to the user after the graph's execution is complete.
"""
info: dict[str, Any]
"""
A dictionary containing the extracted and processed information
based on the user's query and the graph's execution.
This is the primary output of the enrichment process.
"""