持久性¶
许多AI应用程序需要记忆来在单个对话“线程”中的多次交互中共享上下文。在LangGraph中,这种对话级别的记忆可以通过使用检查点(Checkpointers)添加到任何图上。
只需使用兼容的检查点编译图即可。下面是一个使用简单的内存中“MemorySaver”的示例
import { MemorySaver } from "@langchain/langgraph";
const checkpointer = new MemorySaver();
const graph = workflow.compile({ checkpointer });
本指南将展示如何为您的图添加线程级别的持久化。
注意:多对话记忆
如果您需要跨多个对话或用户共享的记忆(跨线程持久化),请查看此操作指南)。
注意
在本操作指南中,我们将从头开始创建我们的代理以使其透明(但冗长)。您可以使用createReactAgent(model, tools=tool, checkpointer=checkpointer)
(API 文档)构造函数实现类似的功能。如果您习惯使用LangChain的AgentExecutor类,这可能更合适。
设置¶
本指南将使用 OpenAI 的 GPT-4o 模型。我们可以选择设置用于 LangSmith 追踪 的 API 密钥,这将为我们提供一流的可观察性。
// process.env.OPENAI_API_KEY = "sk_...";
// Optional, add tracing in LangSmith
// process.env.LANGCHAIN_API_KEY = "ls__...";
process.env.LANGCHAIN_CALLBACKS_BACKGROUND = "true";
process.env.LANGCHAIN_TRACING_V2 = "true";
process.env.LANGCHAIN_PROJECT = "Persistence: LangGraphJS";
定义状态¶
状态是我们图中所有节点的接口。
import { Annotation } from "@langchain/langgraph";
import { BaseMessage } from "@langchain/core/messages";
const GraphState = Annotation.Root({
messages: Annotation<BaseMessage[]>({
reducer: (x, y) => x.concat(y),
}),
});
设置工具¶
我们首先定义要使用的工具。对于这个简单的示例,我们将创建一个占位符搜索引擎。但是,创建自己的工具非常容易——请参阅此处关于如何操作的文档。
import { tool } from "@langchain/core/tools";
import { z } from "zod";
const searchTool = tool(async ({}: { query: string }) => {
// This is a placeholder for the actual implementation
return "Cold, with a low of 13 ℃";
}, {
name: "search",
description:
"Use to surf the web, fetch current information, check the weather, and retrieve other information.",
schema: z.object({
query: z.string().describe("The query to use in your search."),
}),
});
await searchTool.invoke({ query: "What's the weather like?" });
const tools = [searchTool];
我们现在可以将这些工具包装在一个简单的 ToolNode 中。每当我们的 LLM 调用这些工具(函数)时,此对象将实际运行它们。
设置模型¶
现在我们将加载聊天模型。
- 它应该能与消息配合使用。我们将所有代理状态都以消息的形式表示,因此它需要能够很好地与消息配合使用。
- 它应该能与工具调用配合使用,这意味着它可以在响应中返回函数参数。
注意
这些模型要求并非 LangGraph 的通用要求——它们仅是此示例的要求。
完成此操作后,我们应确保模型知道它可以使用这些工具。我们可以通过调用bindTools来完成此操作。
定义图¶
我们现在可以把它们放在一起。我们将首先在没有检查点的情况下运行它
import { END, START, StateGraph } from "@langchain/langgraph";
import { AIMessage } from "@langchain/core/messages";
import { RunnableConfig } from "@langchain/core/runnables";
const routeMessage = (state: typeof GraphState.State) => {
const { messages } = state;
const lastMessage = messages[messages.length - 1] as AIMessage;
// If no tools are called, we can finish (respond to the user)
if (!lastMessage.tool_calls?.length) {
return END;
}
// Otherwise if there is, we continue and call the tools
return "tools";
};
const callModel = async (
state: typeof GraphState.State,
config?: RunnableConfig,
) => {
const { messages } = state;
const response = await boundModel.invoke(messages, config);
return { messages: [response] };
};
const workflow = new StateGraph(GraphState)
.addNode("agent", callModel)
.addNode("tools", toolNode)
.addEdge(START, "agent")
.addConditionalEdges("agent", routeMessage)
.addEdge("tools", "agent");
const graph = workflow.compile();
let inputs = { messages: [{ role: "user", content: "Hi I'm Yu, nice to meet you." }] };
for await (
const { messages } of await graph.stream(inputs, {
streamMode: "values",
})
) {
let msg = messages[messages?.length - 1];
if (msg?.content) {
console.log(msg.content);
} else if (msg?.tool_calls?.length > 0) {
console.log(msg.tool_calls);
} else {
console.log(msg);
}
console.log("-----\n");
}
inputs = { messages: [{ role: "user", content: "Remember my name?" }] };
for await (
const { messages } of await graph.stream(inputs, {
streamMode: "values",
})
) {
let msg = messages[messages?.length - 1];
if (msg?.content) {
console.log(msg.content);
} else if (msg?.tool_calls?.length > 0) {
console.log(msg.tool_calls);
} else {
console.log(msg);
}
console.log("-----\n");
}
添加记忆¶
让我们再次尝试使用检查点。我们将使用MemorySaver,它会将检查点“保存”到内存中。
import { MemorySaver } from "@langchain/langgraph";
// Here we only save in-memory
const memory = new MemorySaver();
const persistentGraph = workflow.compile({ checkpointer: memory });
let config = { configurable: { thread_id: "conversation-num-1" } };
inputs = { messages: [{ role: "user", content: "Hi I'm Jo, nice to meet you." }] };
for await (
const { messages } of await persistentGraph.stream(inputs, {
...config,
streamMode: "values",
})
) {
let msg = messages[messages?.length - 1];
if (msg?.content) {
console.log(msg.content);
} else if (msg?.tool_calls?.length > 0) {
console.log(msg.tool_calls);
} else {
console.log(msg);
}
console.log("-----\n");
}
Hi I'm Jo, nice to meet you.
-----
Hello Jo, nice to meet you too! How can I assist you today?
-----
inputs = { messages: [{ role: "user", content: "Remember my name?"}] };
for await (
const { messages } of await persistentGraph.stream(inputs, {
...config,
streamMode: "values",
})
) {
let msg = messages[messages?.length - 1];
if (msg?.content) {
console.log(msg.content);
} else if (msg?.tool_calls?.length > 0) {
console.log(msg.tool_calls);
} else {
console.log(msg);
}
console.log("-----\n");
}
新建对话线程¶
如果我们要开始新的对话,可以传入不同的thread_id
。咻!所有记忆都消失了(开玩笑,它们会永远存在于那个线程中)!
inputs = { messages: [{ role: "user", content: "you forgot?" }] };
for await (
const { messages } of await persistentGraph.stream(inputs, {
...config,
streamMode: "values",
})
) {
let msg = messages[messages?.length - 1];
if (msg?.content) {
console.log(msg.content);
} else if (msg?.tool_calls?.length > 0) {
console.log(msg.tool_calls);
} else {
console.log(msg);
}
console.log("-----\n");
}