跳转到内容

执行结果

当你运行你的智能体时,你将会收到:

  • 如果调用 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?'));
}

lastAgent 属性包含最后一个运行的智能体。根据你的应用,这通常对用户下次输入时很有用。例如,如果你有一个一线分诊智能体会交接到特定语言的智能体,你可以存储最后一个智能体,并在用户下次向智能体发消息时复用它。

在流式模式下,访问映射到当前正在运行智能体的 currentAgent 属性也会很有用。

newItems 属性包含运行期间生成的新项。各项为 RunItem。运行项包装了由 LLM 生成的原始项。除了访问 LLM 的输出外,你还可以用它们来了解这些事件关联的是哪个智能体。

  • RunMessageOutputItem 表示来自 LLM 的消息。原始项为生成的消息。
  • RunHandoffCallItem 表示 LLM 调用了交接工具。原始项为来自 LLM 的工具调用项。
  • RunHandoffOutputItem 表示发生了交接。原始项为对交接工具调用的工具响应。你也可以从该项访问源/目标智能体。
  • RunToolCallItem 表示 LLM 调用了某个工具。
  • RunToolCallOutputItem 表示某个工具被调用。原始项为工具响应。你也可以从该项访问工具输出。
  • RunReasoningItem 表示来自 LLM 的推理项。原始项为生成的推理。
  • RunToolApprovalItem 表示 LLM 请求对工具调用进行批准。原始项为来自 LLM 的工具调用项。

state 属性包含本次运行的状态。附加到 result 的大多数内容都派生自 state,但 state 可序列化/反序列化,并且在你需要从错误中恢复或处理中断时,也可作为后续调用 run 的输入。

如果你在智能体中使用了 needsApproval,你的 run 可能会触发一些你需要在继续之前处理的 interruptions。在这种情况下,interruptions 将是导致中断的 ToolApprovalItem 数组。有关如何处理中断的更多信息,请查看人机协作

rawResponses 属性包含智能体运行期间由模型生成的原始 LLM 响应。

lastResponseId 属性包含智能体运行期间由模型生成的最后一次响应的 ID。

inputGuardrailResultsoutputGuardrailResults 属性包含护栏的结果(如果有)。护栏结果有时包含你可能想记录或存储的有用信息,因此我们将它们提供给你。

input 属性包含你提供给 run 方法的原始输入。大多数情况下你不需要它,但在需要时可以使用。