跳转到内容

执行结果

当您在运行智能体时,您将会收到:

  • 如果调用 run 且未设置 stream: true,则为 RunResult
  • 如果调用 run 且设置了 stream: true,则为 StreamedRunResult。有关流式传输的详细信息,请参阅流式传输

finalOutput 属性包含最后一个运行的智能体的最终输出。该结果可能是:

  • string — 对于未定义 outputType 的任意智能体的默认类型
  • unknown — 如果智能体将 JSON 模式定义为输出类型。在这种情况下 JSON 已解析,但仍需您手动验证其类型。
  • z.infer<outputType> — 如果智能体将 Zod 模式定义为输出类型。输出将自动按此模式进行解析。
  • undefined — 如果智能体未产生输出(例如在产生输出之前被停止)

如果您使用具有不同输出类型的交接,建议使用 Agent.create() 方法而不是 new Agent() 构造函数来创建智能体。

这将使 SDK 能够在所有可能的交接中推断输出类型,并为 finalOutput 属性提供联合类型。

例如:

Handoff final output types
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 是维护完整历史的便捷方式:

History loop
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 可序列化/反序列化,并且在您需要从错误恢复或处理一次interruption时,也可作为后续调用 run 的输入。

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

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

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

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

input 属性包含您提供给 run 方法的原始输入。大多数情况下您不需要它,但如果需要,它也可用。