上下文¶
代理通常需要不止一条消息列表才能有效运行。它们需要上下文。
上下文包含消息列表之外的任何可以影响代理行为或工具执行的数据。这可以是
- 运行时传递的信息,例如
user_id
或 API 凭据。 - 多步推理过程中更新的内部状态。
- 来自之前交互的持久记忆或事实。
LangGraph 提供三种主要方式来提供上下文
类型 | 描述 | 可变性? | 生命周期 |
---|---|---|---|
Config | 在运行开始时传递的数据 | ❌ | 每个运行 |
State | 执行过程中可以动态变化的数据 | ✅ | 每个运行或对话 |
长期记忆(Store) | 可以在对话之间共享的数据 | ✅ | 跨对话 |
您可以使用上下文来
- 调整模型看到的系统提示
- 为工具提供必要的输入
- 跟踪正在进行的对话中的事实
提供运行时上下文¶
当您需要在运行时将数据注入到代理中时,请使用此功能。
Config(静态上下文)¶
Config 用于不可变数据,例如用户元数据或 API 密钥。当您有在运行过程中不改变的值时使用。
使用一个名为 "configurable" 的键来指定配置,此键是为此目的保留的
State(可变上下文)¶
State 在运行期间充当短期记忆。它保存可以在执行期间演变的动态数据,例如从工具或 LLM 输出派生的值。
const CustomState = Annotation.Root({
...MessagesAnnotation.spec,
userName: Annotation<string>,
});
const agent = createReactAgent({
// Other agent parameters...
stateSchema: CustomState,
})
await agent.invoke(
{ messages: "hi!", userName: "Jane" }
)
开启记忆
请参阅记忆指南以了解如何启用记忆的更多详细信息。这是一项强大的功能,允许您在多次调用中持久化代理的状态。否则,状态仅限于单个代理运行。
长期记忆(跨对话上下文)¶
对于跨对话或会话的上下文,LangGraph 允许通过 store
访问长期记忆。这可用于读取或更新持久性事实(例如,用户资料、偏好、先前交互)。欲了解更多,请参阅记忆指南。
使用上下文自定义提示¶
提示定义了代理的行为方式。为了整合运行时上下文,您可以根据代理的状态或配置动态生成提示。
常见用例
- 个性化
- 角色或目标自定义
- 条件行为(例如,用户是管理员)
import { BaseMessageLike } from "@langchain/core/messages";
import { RunnableConfig } from "@langchain/core/runnables";
import { initChatModel } from "langchain/chat_models/universal";
import { MessagesAnnotation } from "@langchain/langgraph";
import { createReactAgent } from "@langchain/langgraph/prebuilt";
const prompt = (
state: typeof MessagesAnnotation.State,
config: RunnableConfig
): BaseMessageLike[] => {
const userName = config.configurable?.userName;
const systemMsg = `You are a helpful assistant. Address the user as ${userName}.`;
return [{ role: "system", content: systemMsg }, ...state.messages];
};
const llm = await initChatModel("anthropic:claude-3-7-sonnet-latest");
const agent = createReactAgent({
llm,
tools: [getWeather],
prompt
});
await agent.invoke(
{ messages: "hi!" },
{ configurable: { userName: "John Smith" } }
);
import { BaseMessageLike } from "@langchain/core/messages";
import { RunnableConfig } from "@langchain/core/runnables";
import { initChatModel } from "langchain/chat_models/universal";
import { Annotation, MessagesAnnotation } from "@langchain/langgraph";
import { createReactAgent } from "@langchain/langgraph/prebuilt";
const CustomState = Annotation.Root({
...MessagesAnnotation.spec,
userName: Annotation<string>,
});
const prompt = (
state: typeof CustomState.State,
): BaseMessageLike[] => {
const userName = state.userName;
const systemMsg = `You are a helpful assistant. Address the user as ${userName}.`;
return [{ role: "system", content: systemMsg }, ...state.messages];
};
const llm = await initChatModel("anthropic:claude-3-7-sonnet-latest");
const agent = createReactAgent({
llm,
tools: [getWeather],
prompt,
stateSchema: CustomState,
});
await agent.invoke(
{ messages: "hi!", userName: "John Smith" },
);
工具¶
工具可以通过以下方式访问上下文
- 使用
RunnableConfig
进行 Config 访问 - 使用
getCurrentTaskInput()
获取代理 State
import { RunnableConfig } from "@langchain/core/runnables";
import { initChatModel } from "langchain/chat_models/universal";
import { createReactAgent } from "@langchain/langgraph/prebuilt";
import { tool } from "@langchain/core/tools";
import { z } from "zod";
const getUserInfo = tool(
async (input: Record<string, any>, config: RunnableConfig) => {
const userId = config.configurable?.userId;
return userId === "user_123" ? "User is John Smith" : "Unknown user";
},
{
name: "get_user_info",
description: "Look up user info.",
schema: z.object({}),
}
);
const llm = await initChatModel("anthropic:claude-3-7-sonnet-latest");
const agent = createReactAgent({
llm,
tools: [getUserInfo],
});
await agent.invoke(
{ messages: "look up user information" },
{ configurable: { userId: "user_123" } }
);
import { initChatModel } from "langchain/chat_models/universal";
import { createReactAgent } from "@langchain/langgraph/prebuilt";
import { Annotation, MessagesAnnotation, getCurrentTaskInput } from "@langchain/langgraph";
import { tool } from "@langchain/core/tools";
import { z } from "zod";
const CustomState = Annotation.Root({
...MessagesAnnotation.spec,
userId: Annotation<string>(),
});
const getUserInfo = tool(
async (
input: Record<string, any>,
) => {
const state = getCurrentTaskInput() as typeof CustomState.State;
const userId = state.userId;
return userId === "user_123" ? "User is John Smith" : "Unknown user";
},
{
name: "get_user_info",
description: "Look up user info.",
schema: z.object({})
}
);
const llm = await initChatModel("anthropic:claude-3-7-sonnet-latest");
const agent = createReactAgent({
llm,
tools: [getUserInfo],
stateSchema: CustomState,
});
await agent.invoke(
{ messages: "look up user information", userId: "user_123" }
);
从工具更新上下文¶
工具可以在执行期间修改代理的状态。这对于持久化中间结果或使信息可供后续工具或提示访问非常有用。
import { Annotation, MessagesAnnotation, LangGraphRunnableConfig, Command } from "@langchain/langgraph";
import { tool } from "@langchain/core/tools";
import { z } from "zod";
import { ToolMessage } from "@langchain/core/messages";
import { initChatModel } from "langchain/chat_models/universal";
import { createReactAgent } from "@langchain/langgraph/prebuilt";
const CustomState = Annotation.Root({
...MessagesAnnotation.spec,
userName: Annotation<string>(), // Will be updated by the tool
});
const getUserInfo = tool(
async (
_input: Record<string, never>,
config: LangGraphRunnableConfig
): Promise<Command> => {
const userId = config.configurable?.userId;
if (!userId) {
throw new Error("Please provide a user id in config.configurable");
}
const toolCallId = config.toolCall?.id;
const name = userId === "user_123" ? "John Smith" : "Unknown user";
// Return command to update state
return new Command({
update: {
userName: name,
// Update the message history
messages: [
new ToolMessage({
content: "Successfully looked up user information",
tool_call_id: toolCallId,
}),
],
},
});
},
{
name: "get_user_info",
description: "Look up user information.",
schema: z.object({}),
}
);
const llm = await initChatModel("anthropic:claude-3-7-sonnet-latest");
const agent = createReactAgent({
llm,
tools: [getUserInfo],
stateSchema: CustomState,
});
await agent.invoke(
{ messages: "look up user information" },
{ configurable: { userId: "user_123" } }
);
更多详细信息,请参阅如何从工具更新 State。