跳转到内容

交接

交接可让一个智能体将对话的一部分委派给另一个智能体。这在不同智能体专注于特定领域时非常有用。例如在客服应用中,你可能会有分别处理预订、退款或常见问题的智能体。

在 LLM 看来,交接会以工具形式表示。如果你交接给名为 Refund Agent 的智能体,工具名会是 transfer_to_refund_agent

在你了解应由专家接管对话后,请在阅读智能体后再阅读本页。如果专家应在原始智能体之后运行,请改用Agents as tools

每个智能体都接受一个 handoffs 选项。它可以包含其他 Agent 实例,或由 handoff() 辅助函数返回的 Handoff 对象。

如果你传入普通的 Agent 实例,其 handoffDescription(如有提供)会追加到默认工具描述中。可用它来明确模型应在何时选择该交接。

基础交接
import { Agent, handoff } from '@openai/agents';
const billingAgent = new Agent({ name: 'Billing agent' });
const refundAgent = new Agent({ name: 'Refund agent' });
// Use Agent.create method to ensure the finalOutput type considers handoffs
const triageAgent = Agent.create({
name: 'Triage agent',
handoffs: [billingAgent, handoff(refundAgent)],
});

handoff() 函数允许你调整生成的工具。

  • agent – 要交接到的智能体。
  • toolNameOverride – 覆盖默认的 transfer_to_<agent_name> 工具名。
  • toolDescriptionOverride – 覆盖默认工具描述。
  • onHandoff – 交接发生时的回调。接收一个 RunContext,并且当配置了 inputType 时,还会接收已解析的交接负载。
  • inputType – 交接工具调用参数的 schema。
  • inputFilter – 过滤传递给下一个智能体的历史记录。
  • isEnabled – 布尔值或谓词,仅在匹配的运行中暴露该交接。

handoff() 辅助函数始终会将控制权交给你传入的特定 agent。如果有多个可能目标,请为每个目标分别注册一个交接,并让模型在其中选择。当你自己的交接代码需要在调用时决定返回哪个智能体时,请使用自定义 Handoff

自定义交接
import { z } from 'zod';
import { Agent, handoff, RunContext } from '@openai/agents';
const FooSchema = z.object({ foo: z.string() });
function onHandoff(ctx: RunContext, input?: { foo: string }) {
console.log('Handoff called with:', input?.foo);
}
const agent = new Agent({ name: 'My agent' });
const handoffObj = handoff(agent, {
onHandoff,
inputType: FooSchema,
toolNameOverride: 'custom_handoff_tool',
toolDescriptionOverride: 'Custom description',
});

有时你希望模型在选择交接时附带一个小型结构化负载。此时请同时定义 inputTypeonHandoff

交接输入
import { z } from 'zod';
import { Agent, handoff, RunContext } from '@openai/agents';
const EscalationData = z.object({ reason: z.string() });
type EscalationData = z.infer<typeof EscalationData>;
async function onHandoff(
ctx: RunContext<EscalationData>,
input: EscalationData | undefined,
) {
console.log(`Escalation agent called with reason: ${input?.reason}`);
}
const agent = new Agent<EscalationData>({ name: 'Escalation agent' });
const handoffObj = handoff(agent, {
onHandoff,
inputType: EscalationData,
});

inputType 描述的是交接工具调用本身的参数。SDK 会将该 schema 作为交接工具的 parameters 暴露给模型,在本地解析返回参数,并将解析后的值传给 onHandoff

它不会替代下一个智能体的主输入,也不会选择不同的目标。handoff() 辅助函数仍会交接到你包装的特定智能体,接收方智能体仍会看到对话历史,除非你通过 inputFilter 修改它。

inputType 也独立于 RunContext。它用于模型在交接时决定的元数据,而不是你在本地已有的应用状态或依赖。

当交接需要一小段由模型生成的路由元数据(如 reasonlanguageprioritysummary)时,使用 inputType。例如,分诊智能体可通过 { reason: 'duplicate_charge', priority: 'high' } 交接给退款智能体,而 onHandoff 可在退款智能体接管前记录或持久化这些元数据。

当目标不同时,请选择其他机制:

  • 将现有应用状态放入 RunContext
  • 如果你想改变接收方智能体看到的历史,请使用 inputFilter
  • 如果有多个可能的专家目标,请为每个目标注册一个交接。inputType 可以为已选交接附加元数据,但不会在目标之间分发。
  • 当你希望 SDK 在运行 onHandoff 前校验解析后的负载时,优先使用 Zod schema;原始 JSON Schema 仅定义发送给模型的工具契约。

默认情况下,交接会接收完整的对话历史。若要修改传给下一个智能体的内容,请提供 inputFilter。常用辅助函数位于 @openai/agents-core/extensions

输入过滤器
import { Agent, handoff } from '@openai/agents';
import { removeAllTools } from '@openai/agents-core/extensions';
const agent = new Agent({ name: 'FAQ agent' });
const handoffObj = handoff(agent, {
inputFilter: removeAllTools,
});

inputFilter 会接收并返回一个 HandoffInputData 对象:

  • inputHistory – 运行开始前的输入历史。
  • preHandoffItems – 在发生交接的那一轮之前生成的条目。
  • newItems – 当前轮次中生成的条目,包括交接调用/输出条目。
  • runContext – 当前活跃的运行上下文。

如果你还在 Runner 上配置了 handoffInputFilter,则针对该特定交接,其每个交接上的 inputFilter 具有更高优先级。

当提示词中提到交接时,LLM 的响应通常更稳定。SDK 通过 RECOMMENDED_PROMPT_PREFIX 提供了一个推荐前缀。

推荐提示词
import { Agent } from '@openai/agents';
import { RECOMMENDED_PROMPT_PREFIX } from '@openai/agents-core/extensions';
const billingAgent = new Agent({
name: 'Billing agent',
instructions: `${RECOMMENDED_PROMPT_PREFIX}
Fill in the rest of your prompt here.`,
});
  • 智能体:用于在管理者模式与交接之间进行选择。
  • 智能体编排:了解更广泛的工作流权衡。
  • 工具:使用 agent.asTool() 的管理者模式替代方案。
  • 运行智能体:了解交接在运行时的行为。
  • 执行结果:了解交接图中的类型化 finalOutput