跳转到内容

执行结果

当你运行你的智能体时,你将会收到以下之一:

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

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

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

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

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

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