05. Add memory to Agent

Currently, chatbots cannot remember their past interactions themselves, so there are limits to conducting consistent multi-turn conversations.

To solve this in this tutorial memory Add.

Reference

This time pre-built ToolNode Wow tools_condition Use it.

  1. ToolNode : Node for tool call

  2. tools_condition : Condition branch based on whether the tool is called

Our chatbots can now use tools to answer user questions, but of previous interaction context I don't remember. This limits the ability to conduct multi-turn conversations.

LangGraph has persistent checkpointing Solve this problem.

When compiling graphs checkpointer When providing and calling the graph thread_id If you provide, LangGraph After each step Save status automatically To. same thread_id When you call the graph again using, the graph loads the saved state so that the chatbot can continue the conversation at the point where it was previously stopped.

checkpointing Is much more powerful than LangChain's memory function. (Maybe you can check this naturally by completing this tutorial.)

Copy

# Configuration file for managing API keys as environment variables
from dotenv import load_dotenv

# Load API key information
load_dotenv()

Copy

 True 

Copy

Copy

But before going too far, to enable multi-turn conversations checkpointing Let's add it.

MemorySaver Generates checkpointer.

Copy

Reference

In this tutorial in-memory checkpointer Use.

But in the production phase, this SqliteSaver or PostgresSaver You can change to and connect to your own DB.

Copy

Finally, provided checkpointer Compile graphs using

Copy

Graph connectivity LangGraph-Agent Same as

Just, what was added this time was that the graph handled each node State Just checkpointing.

Copy

RunnableConfig Settings

RunnableConfig Define recursion_limit and thread_id Set.

  • recursion_limit : Maximum number of nodes to visit. More than that, RecursionError

  • thread_id : Thread ID setting

thread_id Is used to separate conversation sessions. In other words, the storage of memory thread_id It is done individually according to.

Copy

Copy

Copy

Copy

Copy

this time RunnableConfig of thread_id After changing, I'll ask if you remember the previous conversation.

Copy

Copy

Snapshot: Check the saved State

So far, I have made several checkpoints in two different threads.

Checkpoint Is the current status value, its configuration, and to be processed next Node included.

Graph at a given setting state Anytime to check get_state(config) Call

Copy

Copy

snapshot.config You can check the config information set to output.

Copy

Copy

snapshot.value You can check the state values stored so far by outputting.

Copy

Copy

snapshot.next Output to find forward at this point Check the following nodes You can.

END Since it has been reached, the next node is output with an empty value.

Copy

Copy

Copy

Copy

To visualize metadata of complex structures display_message_tree Use functions.

Copy

Last updated