跳转到内容

执行结果

当你运行智能体时,你会得到以下两种之一的结果:

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

  • string —— 对于未定义 outputType 的任何智能体的默认值
  • unknown —— 如果智能体的输出类型定义为 JSON 模式。在这种情况下 JSON 已被解析,但你仍需要手动验证其类型。
  • z.infer<outputType> —— 如果智能体的输出类型定义为 Zod 模式。输出将自动按该模式解析。
  • 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 属性包含护栏的结果(如果有)。护栏结果有时包含你希望记录或存储的有用信息,因此我们将其提供给你。

工具护栏结果通过 toolInputGuardrailResultstoolOutputGuardrailResults 分别暴露。

Token 用量汇总在 result.state.usage 中,其中跟踪了本次运行的请求次数和 token 总数。对于流式运行,这些数据会随着响应到达而更新。

从 RunState 读取用量
import { Agent, run } from '@openai/agents';
const agent = new Agent({
name: 'Usage Tracker',
instructions: 'Summarize the latest project update in one sentence.',
});
const result = await run(
agent,
'Summarize this: key customer feedback themes and the next product iteration.',
);
const usage = result.state.usage;
console.log({
requests: usage.requests,
inputTokens: usage.inputTokens,
outputTokens: usage.outputTokens,
totalTokens: usage.totalTokens,
});
if (usage.requestUsageEntries) {
for (const entry of usage.requestUsageEntries) {
console.log('request', {
endpoint: entry.endpoint,
inputTokens: entry.inputTokens,
outputTokens: entry.outputTokens,
totalTokens: entry.totalTokens,
});
}
}

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