Let's build a simple graph with 3 nodes and one conditional edge.
Let's build up to a simple chain that combines 4 concepts:
- Using chat messages as our graph state
- Using chat models in graph nodes
- Binding tools to our chat model
- Executing tool calls in graph nodes
Let's build a router, where the chat model routes between a direct response or a tool call based upon the user input. This is an simple example of an agent, where the LLM is directing the control flow either by calling a tool or just responding directly.
Let's build an agent using a general ReAct architecture
- Act - let the model call specific tools
- Observe - pass the tool output back to the model
- Reason - let the model reason about the tool output to decide what to do next (e.g., call another tool or just respond directly)
Let's build an agent using a general ReAct architecture with Memory
- Act - let the model call specific tools
- Observe - pass the tool output back to the model
- Reason - let the model reason about the tool output to decide what to do next (e.g., call another tool or just respond directly)
We will extend our agent by introducing memory.
The state schema represents the structure and types of data that our graph will use. All nodes are expected to communicate with that schema.
We will use
TypeDict
class from python'styping
module.DataClasses
from pythonPydantic
- data validation and settings management library using Python type annotations.
The reducers, which specify how state updates are performed on specific keys / channels in the state schema.
We will use
Annotated
type with reducer function likeoperator.add
.Annotated
type with custom reducer function likereduce_list
.MessagesState
Re-writing
andRemoval
of messages.
The multiple schemas are needed when,
- Internal nodes may pass information that is not required in the graph's input / output.
- We may also want to use different input / output schemas for the graph.
Filtering and Trimming messages using,
- Remove Message with MessageState
- Filtering Messages
- Trimming Messages
Let's create a simple Chatbot with conversation summary. We'll equip that Chatbot with memory, supporting long-running conversations.
Let's upgrade our Chatbot with conversation summary and external memory (SqliteSaver checkpointer), supporting long-running conversations and chat presistence.
.stream
and.astream
are sync and async methods for streaming back results- Streaming graph state using streaming modes - 'updates' and 'values'
.astream
events and each event is a dict with a few keys:
event
: This is the type of event that is being emitted.name
: This is the name of event.data
: This is the data associated with the event.metadata
: Containslanggraph_node
, the node emitting the event.
For breakpoint, we need to simply compile the graph with interrupt_before=["tools"]
where tools
is our tools node.
This means that the execution will be interrupted before the node tools
, which executes the tool call.
Using breakpoints to modify the graph state.