如何为您的图添加跨线程持久性¶
在上一篇指南中,您学习了如何在单个线程上的多个交互之间保持图状态。LangGraph 还允许您在**多个线程**之间持久化数据。例如,您可以将用户(他们的姓名或偏好)信息存储在共享内存中,并在新的对话线程中重复使用这些信息。
在本指南中,我们将展示如何构建和使用一个实现了使用Store接口的共享内存的图。
注意
本指南中使用的Store API 支持已在 LangGraph v0.2.32中添加。
本指南中使用的Store API 的index和query参数支持已在 LangGraph v0.2.54中添加。
设置¶
首先,让我们安装所需的包并设置我们的 API 密钥。
import getpass
import os
def _set_env(var: str):
if not os.environ.get(var):
os.environ[var] = getpass.getpass(f"{var}: ")
_set_env("ANTHROPIC_API_KEY")
为LangGraph开发设置LangSmith
注册LangSmith以快速发现问题并提高您的LangGraph项目的性能。LangSmith允许您使用跟踪数据来调试、测试和监控使用LangGraph构建的LLM应用程序 — 有关如何开始的更多信息,请阅读此处
定义存储¶
在这个例子中,我们将创建一个图形,可以检索用户的偏好信息。我们将通过定义一个 InMemoryStore 来实现 - 这是一个可以在内存中存储数据并查询数据的对象。然后,我们将在编译图形时传递存储对象。这允许图形中的每个节点访问存储:当你定义节点函数时,可以定义 store 关键字参数,LangGraph 将自动传递你编译图形时使用的存储对象。
在使用 Store 接口存储对象时,你需要定义两个东西:
- 对象的命名空间, 一个元组(类似于目录)
- 对象键(类似于文件名)
在我们的例子中,我们将使用 ("memories", <user_id>) 作为命名空间,并为每个新记忆使用随机 UUID 作为键。
重要的是,为了确定用户,我们将通过节点函数的 config 关键字参数传递 user_id。
让我们首先定义一个已经填充了一些关于用户记忆的 InMemoryStore。
from langgraph.store.memory import InMemoryStore
from langchain_openai import OpenAIEmbeddings
in_memory_store = InMemoryStore(
index={
"embed": OpenAIEmbeddings(model="text-embedding-3-small"),
"dims": 1536,
}
)
创建图形¶
import uuid
from typing import Annotated
from typing_extensions import TypedDict
from langchain_anthropic import ChatAnthropic
from langchain_core.runnables import RunnableConfig
from langgraph.graph import StateGraph, MessagesState, START
from langgraph.checkpoint.memory import MemorySaver
from langgraph.store.base import BaseStore
model = ChatAnthropic(model="claude-3-5-sonnet-20240620")
# NOTE: we're passing the Store param to the node --
# this is the Store we compile the graph with
def call_model(state: MessagesState, config: RunnableConfig, *, store: BaseStore):
user_id = config["configurable"]["user_id"]
namespace = ("memories", user_id)
memories = store.search(namespace, query=str(state["messages"][-1].content))
info = "\n".join([d.value["data"] for d in memories])
system_msg = f"You are a helpful assistant talking to the user. User info: {info}"
# Store new memories if the user asks the model to remember
last_message = state["messages"][-1]
if "remember" in last_message.content.lower():
memory = "User name is Bob"
store.put(namespace, str(uuid.uuid4()), {"data": memory})
response = model.invoke(
[{"type": "system", "content": system_msg}] + state["messages"]
)
return {"messages": response}
builder = StateGraph(MessagesState)
builder.add_node("call_model", call_model)
builder.add_edge(START, "call_model")
# NOTE: we're passing the store object here when compiling the graph
graph = builder.compile(checkpointer=MemorySaver(), store=in_memory_store)
# If you're using LangGraph Cloud or LangGraph Studio, you don't need to pass the store or checkpointer when compiling the graph, since it's done automatically.
注意
如果您使用的是 LangGraph Cloud 或 LangGraph Studio,您不需要在编译图形时传递存储,因为这会自动完成。
运行图形!¶
现在让我们在配置中指定一个用户ID,并告诉模型我们的名字:
config = {"configurable": {"thread_id": "1", "user_id": "1"}}
input_message = {"type": "user", "content": "Hi! Remember: my name is Bob"}
for chunk in graph.stream({"messages": [input_message]}, config, stream_mode="values"):
chunk["messages"][-1].pretty_print()
================================[1m Human Message [0m=================================
Hi! Remember: my name is Bob
==================================[1m Ai Message [0m==================================
Hello Bob! It's nice to meet you. I'll remember that your name is Bob. How can I assist you today?
config = {"configurable": {"thread_id": "2", "user_id": "1"}}
input_message = {"type": "user", "content": "what is my name?"}
for chunk in graph.stream({"messages": [input_message]}, config, stream_mode="values"):
chunk["messages"][-1].pretty_print()
================================[1m Human Message [0m=================================
what is my name?
==================================[1m Ai Message [0m==================================
Your name is Bob.
config = {"configurable": {"thread_id": "3", "user_id": "2"}}
input_message = {"type": "user", "content": "what is my name?"}
for chunk in graph.stream({"messages": [input_message]}, config, stream_mode="values"):
chunk["messages"][-1].pretty_print()
================================[1m Human Message [0m=================================
what is my name?
==================================[1m Ai Message [0m==================================
I apologize, but I don't have any information about your name. As an AI assistant, I don't have access to personal information about users unless it has been specifically shared in our conversation. If you'd like, you can tell me your name and I'll be happy to use it in our discussion.