上下文¶
代理通常需要除消息列表之外的更多信息才能有效运行。它们需要上下文。
上下文包括消息列表之外的任何可以影响代理行为或工具执行的数据。这可以是:
- 运行时传递的信息,例如
user_id
或API凭据。 - 多步骤推理过程中更新的内部状态。
- 来自先前交互的持久记忆或事实。
LangGraph提供三种主要方式来提供上下文:
类型 | 描述 | 可变? | 生命周期 |
---|---|---|---|
配置 | 在运行开始时传递的数据 | ❌ | 每次运行 |
状态 | 在执行期间可以更改的动态数据 | ✅ | 每次运行或对话 |
长期记忆(存储) | 可以在对话之间共享的数据 | ✅ | 跨对话 |
您可以使用上下文来:
- 调整模型看到的系统提示
- 为工具提供必要的输入
- 在正在进行的对话中跟踪事实
提供运行时上下文¶
当您需要在运行时向代理注入数据时使用此功能。
配置 (静态上下文)¶
配置用于不可变数据,例如用户元数据或API密钥。当您有在运行期间不更改的值时使用。
使用一个名为"configurable"的键来指定配置,该键为此目的保留
状态 (可变上下文)¶
状态在运行期间充当短期记忆。它保存可以在执行期间演变的动态数据,例如来自工具或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
访问配置 - 使用
getCurrentTaskInput()
访问代理状态
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" } }
);
更多详细信息,请参阅如何从工具更新状态。