跳过内容

上下文

代理通常需要除消息列表之外的更多信息才能有效运行。它们需要**上下文**。

上下文包括消息列表之外的*任何*数据,这些数据可以影响代理行为或工具执行。这可以是:

  • 在运行时传递的信息,例如 user_id 或 API 凭据。
  • 在多步推理过程中更新的内部状态。
  • 来自先前交互的持久记忆或事实。

LangGraph 提供**三种**主要方式来提供上下文:

类型 描述 可变? 生命周期
配置 在运行开始时传递的数据 每次运行
状态 执行期间可更改的动态数据 每次运行或会话
长期记忆(存储) 可在会话之间共享的数据 跨会话

您可以使用上下文来:

  • 调整模型看到的系统提示
  • 向工具提供必要的输入
  • 在进行中的会话中跟踪事实

提供运行时上下文

当您需要在运行时将数据注入代理时,请使用此项。

配置(静态上下文)

配置适用于不可变数据,例如用户元数据或 API 密钥。当您的值在运行过程中不会更改时使用。

使用名为 **"configurable"** 的键指定配置,该键专用于此目的

await agent.invoke(
  { messages: "hi!" },
  { configurable: { userId: "user_123" } }
)

状态(可变上下文)

状态在运行期间充当短期记忆。它保存执行期间可以演变的动态数据,例如来自工具或 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" } }
);

有关更多详细信息,请参阅如何从工具更新状态