执行结果
当你运行智能体时,你将会得到:
- 如果调用
run且未设置stream: true,则返回RunResult - 如果调用
run且设置了stream: true,则返回StreamedRunResult。关于流式传输的细节,参见流式传输。
finalOutput 属性包含最后一个运行的智能体的最终输出。该结果可能是:
string——默认值,适用于未定义outputType的任何智能体unknown——当智能体的输出类型定义为 JSON schema 时。在这种情况下,JSON 已被解析,但你仍需手动验证其类型。z.infer<outputType>——当智能体的输出类型定义为 Zod schema 时。输出会自动按该 schema 进行解析。undefined——当智能体未产生输出(例如在产生输出前已停止)
如果你使用了具有不同输出类型的交接,应使用 Agent.create() 方法而非 new Agent() 构造函数来创建智能体。
这将使 SDK 能够在所有可能的交接中推断输出类型,并为 finalOutput 属性提供联合类型。
例如:
import { Agent, run } from '@openai/agents';import { z } from 'zod';
const refundAgent = new Agent({ name: 'Refund Agent', instructions: 'You are a refund agent. You are responsible for refunding customers.', outputType: z.object({ refundApproved: z.boolean(), }),});
const orderAgent = new Agent({ name: 'Order Agent', instructions: 'You are an order agent. You are responsible for processing orders.', outputType: z.object({ orderId: z.string(), }),});
const triageAgent = Agent.create({ name: 'Triage Agent', instructions: 'You are a triage agent. You are responsible for triaging customer issues.', handoffs: [refundAgent, orderAgent],});
const result = await run(triageAgent, 'I need to a refund for my order');
const output = result.finalOutput;// ^? { refundApproved: boolean } | { orderId: string } | string | undefined有两种方式获取下一轮的输入:
result.history——包含你的输入和智能体输出的副本。result.output——包含整个智能体运行的输出。
在类聊天场景中,history 是维护完整历史的便捷方式:
import { Agent, user, run } from '@openai/agents';import type { AgentInputItem } from '@openai/agents';
const agent = new Agent({ name: 'Assistant', instructions: 'You are a helpful assistant knowledgeable about recent AGI research.',});
let history: AgentInputItem[] = [ // initial message user('Are we there yet?'),];
for (let i = 0; i < 10; i++) { // run 10 times const result = await run(agent, history);
// update the history to the new output history = result.history;
history.push(user('How about now?'));}最后一个智能体
Section titled “最后一个智能体”lastAgent 属性包含最后一个运行的智能体。根据你的应用,这通常对用户下一次输入很有用。例如,如果你有一个前线分诊智能体会交接至特定语言的智能体,你可以存储最后一个智能体,并在用户下一次与智能体对话时复用它。
在流式传输模式下,访问映射到当前正在运行的智能体的 currentAgent 属性也很有用。
newItems 属性包含在运行期间生成的新条目。条目是 RunItem。运行条目包装了 LLM 生成的原始条目。除了访问 LLM 的输出外,它们还能用于了解这些事件关联的是哪个智能体。
RunMessageOutputItem表示来自 LLM 的消息。原始条目为生成的消息。RunHandoffCallItem表示 LLM 调用了交接工具。原始条目为 LLM 的工具调用条目。RunHandoffOutputItem表示发生了交接。原始条目为对交接工具调用的工具响应。你也可以从该条目访问源/目标智能体。RunToolCallItem表示 LLM 调用了某个工具。RunToolCallOutputItem表示某个工具被调用。原始条目为工具响应。你也可以从该条目访问工具输出。RunReasoningItem表示来自 LLM 的推理条目。原始条目为生成的推理内容。RunToolApprovalItem表示 LLM 请求对工具调用进行批准。原始条目为 LLM 的工具调用条目。
state 属性包含此次运行的状态。附加到 result 上的大多数信息均源自 state,但 state 可序列化/反序列化,并且在你需要从错误中恢复或处理interruption时,也可作为后续调用 run 的输入。
如果你在智能体中使用了 needsApproval,你的 run 可能会触发一些需要处理后才能继续的 interruptions。在这种情况下,interruptions 将是导致中断的 ToolApprovalItem 数组。有关如何处理中断的更多信息,请参阅人机协作。
rawResponses 属性包含智能体运行期间由模型生成的原始 LLM 响应。
最后响应 ID
Section titled “最后响应 ID”lastResponseId 属性包含智能体运行期间由模型生成的最后一个响应的 ID。
inputGuardrailResults 和 outputGuardrailResults 属性包含护栏的运行结果(如果有)。护栏结果有时包含你希望记录或存储的有用信息,因此我们将其提供给你。
input 属性包含你传递给 run 方法的原始输入。在大多数情况下你不需要它,但在需要时它可用。