跳过内容

流式处理

流式处理是构建响应式应用的关键。您需要流式处理几种类型的数据

  1. 智能体进度 — 在智能体图中每个节点执行后获取更新。
  2. LLM 令牌 — 流式处理语言模型生成的令牌。
  3. 自定义更新 — 在执行期间从工具中发出自定义数据(例如,“已抓取 10/100 条记录”)

您可以一次流式处理多种类型的数据

image

等待是给鸽子准备的。

智能体进度

要流式处理智能体进度,请使用 stream() 方法,并设置 streamMode: "updates"。这会在智能体每一步执行后发出一个事件。

例如,如果您有一个智能体调用工具一次,您应该看到以下更新

  • LLM 节点: 包含工具调用请求的 AI 消息
  • 工具节点: 包含执行结果的工具消息
  • LLM 节点: 最终 AI 响应
import { createReactAgent } from "@langchain/langgraph/prebuilt";
import { initChatModel } from "langchain/chat_models/universal";

const llm = await initChatModel("anthropic:claude-3-7-sonnet-latest");
const agent = createReactAgent({
  llm,
  tools: [getWeather],
});
for await (const chunk of await agent.stream(
  { messages: "what is the weather in sf" },
  { streamMode: "updates" }
)) {
  console.log(chunk);
  console.log("\n");
}

LLM 令牌

要流式处理 LLM 生成的令牌,请使用 streamMode: "messages"

import { createReactAgent } from "@langchain/langgraph/prebuilt";
import { initChatModel } from "langchain/chat_models/universal";

const llm = await initChatModel("anthropic:claude-3-7-sonnet-latest");
const agent = createReactAgent({
  llm,
  tools: [getWeather],
});
for await (const [token, metadata] of await agent.stream(
  { messages: "what is the weather in sf" },
  { streamMode: "messages" }
)) {
  console.log("Token", token);
  console.log("Metadata", metadata);
  console.log("\n");
}

工具更新

要流式处理工具执行时的更新,您可以使用通过 config.writer 可用的 writer 对象

import { LangGraphRunnableConfig } from "@langchain/langgraph";
import { createReactAgent } from "@langchain/langgraph/prebuilt";
import { initChatModel } from "langchain/chat_models/universal";

const getWeather = tool(
  async (input: { city: string }, config: LangGraphRunnableConfig) => {
    // stream any arbitrary data
    config.writer?.(`Looking up data for city: ${input.city}`);
    return `It's always sunny in ${input.city}!`;
  },
  {
    name: "getWeather",
    schema: z.object({
      city: z.string().describe("The city to get the weather for"),
    }),
    description: "Get weather for a given city.",
  }
);

const llm = await initChatModel("anthropic:claude-3-7-sonnet-latest");
const agent = createReactAgent({
  llm,
  tools: [getWeather],
});

for await (const chunk of await agent.stream(
  { messages: "what is the weather in sf" },
  { streamMode: "custom" }
)) {
  console.log(chunk);
  console.log("\n");
}

流式处理多种模式

您可以通过将流模式作为列表传递来指定多种流式处理模式:streamMode: ["updates", "messages", "custom"]

import { createReactAgent } from "@langchain/langgraph/prebuilt";
import { initChatModel } from "langchain/chat_models/universal";

const llm = await initChatModel("anthropic:claude-3-7-sonnet-latest");
const agent = createReactAgent({
  llm,
  tools: [getWeather],
});

for await (const [streamMode, chunk] of await agent.stream(
  { messages: "what is the weather in sf" },
  { streamMode: ["updates", "messages", "custom"] }
)) {
  console.log(streamMode, chunk);
  console.log("\n");
}

附加资源