Agents
运行智能体
运行智能体
智能体支持使用 .invoke() 获取完整响应,或使用 .stream() 进行输出的增量流式传输。本节解释如何提供输入、解释输出、启用流式传输以及控制执行限制。
基本用法
智能体可以使用 .invoke() 执行:
import { createReactAgent } from "@langchain/langgraph/prebuilt";
const agent = createReactAgent(...);
// highlight-next-line
const response = await agent.invoke(
{ messages: [ { role: "user", content: "what is the weather in sf" } ] }
);
输入和输出
智能体使用期望 messages 列表作为输入的语言模型。因此,智能体输入和输出存储在智能体状态中 messages 键下的 messages 列表中。
输入格式
智能体输入必须是具有 messages 键的对象。支持的格式有:
| 格式 | 示例 |
|---|---|
| 字符串 | { messages: "Hello" } — 被解释为 HumanMessage |
| 消息对象 | { messages: { "role": "user", "content": "Hello" } } |
| 消息列表 | { messages: [ {"role": "user", "content": "Hello" } ] } |
| 带自定义状态 | { messages: [ {"role": "user", "content": "Hello"} ], "user_name": "Alice" } — 如果使用自定义 stateSchema |
消息会自动转换为 LangChain 的内部消息格式。您可以阅读 LangChain 消息 文档了解更多信息。
:::tip[使用自定义智能体状态]
您可以在输入字典中直接提供在智能体状态模式中定义的附加字段。这允许基于运行时数据或先前工具输出的动态行为。 详细信息请参见 context guide。
:::note
messages 的字符串输入被转换为 HumanMessage。此行为与 createReactAgent 中的 prompt 参数不同,后者作为字符串传递时被解释为 SystemMessage。
输出格式
智能体输出是一个包含以下内容的字典:
messages:执行期间交换的所有消息列表(用户输入、助手回复、工具调用)。- 如果配置了结构化输出,则可选的
structuredResponse。 - 如果使用自定义
stateSchema,输出中还可能存在与定义字段对应的其他键。这些可以保存来自工具执行或提示词逻辑的更新状态值。
有关使用自定义状态模式和访问上下文的更多详细信息,请参见 context guide。
流式输出
智能体支持流式响应以实现更具响应性的应用程序。这包括:
- 每一步后的进度更新
- 生成时的 LLM token
- 执行期间的自定义工具消息
流式传输在同步和异步模式下都可用:
for await (
const chunk of await agent.stream(
{ messages: [ { role: "user", content: "what is the weather in sf" } ] },
{ streamMode: "updates" },
)
) {
console.log(chunk);
}
:::tip
有关完整详细信息,请参见 streaming guide。
最大迭代次数
要控制智能体执行并避免无限循环,请设置递归限制。这定义了智能体在引发 GraphRecursionError 之前可以采取的最大步骤数。您可以在运行时或通过 .withConfig() 定义智能体时配置 recursionLimit:
=== 运行时
import { GraphRecursionError } from "@langchain/langgraph";
import { createReactAgent } from "@langchain/langgraph/prebuilt";
import { initChatModel } from "langchain/chat_models/universal";
const maxIterations = 3;
// highlight-next-line
const recursionLimit = 2 * maxIterations + 1;
const llm = await initChatModel("anthropic:claude-3-5-haiku-latest");
const agent = createReactAgent({
llm,
tools: [getWeather]
});
try {
const response = await agent.invoke(
{ messages: [ { role: "user", content: "what's the weather in sf" } ] },
// highlight-next-line
{ recursionLimit }
);
} catch (error) {
if (error instanceof GraphRecursionError) {
console.log("Agent stopped due to max iterations.");
} else {
throw error;
}
}
=== .withConfig()
import { GraphRecursionError } from "@langchain/langgraph";
import { createReactAgent } from "@langchain/langgraph/prebuilt";
import { initChatModel } from "langchain/chat_models/universal";
const maxIterations = 3;
// highlight-next-line
const recursionLimit = 2 * maxIterations + 1;
const llm = await initChatModel("anthropic:claude-3-5-haiku-latest");
const agent = createReactAgent({
llm,
tools: [getWeather]
});
// highlight-next-line
const agentWithRecursionLimit = agent.withConfig({ recursionLimit });
try {
const response = await agentWithRecursionLimit.invoke(
{ messages: [ { role: "user", content: "what's the weather in sf" } ] }
);
} catch (error) {
if (error instanceof GraphRecursionError) {
console.log("Agent stopped due to max iterations.");
} else {
throw error;
}
}