流式传输¶
流式传输是构建响应式应用程序的关键。您需要流式传输以下几种类型的数据:
您可以同时流式传输不止一种类型的数据。
代理进度¶
要流式传输代理进度,请使用 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");
}