Appearance
Agent 编排模式
本节介绍 Agent 的编排模式,包括单 Agent 系统、多 Agent 系统,以及具体的管理模式和去中心化模式。
基本概念
编排(Orchestration)使 Agent 能够有效地执行工作流。虽然立即构建具有复杂架构的完全自主 Agent 很诱人,但客户通常通过增量方法取得更大的成功。
两种编排类别
| 类别 | 说明 |
|---|---|
| 单 Agent 系统 | 单个模型配备适当的工具和指令,在循环中执行工作流 |
| 多 Agent 系统 | 工作流执行分布在多个协调的 Agent 之间 |
单 Agent 系统
单个 Agent 可以通过增量添加工具来处理许多任务,保持复杂性可控,简化评估和维护。每个新工具都扩展了其能力,而不会过早地迫使您编排多个 Agent。
循环机制
每个编排方法都需要一个"运行"的概念,通常实现为一个循环,让 Agent 运行直到达到退出条件(详见第 2 篇:Agent 核心循环)。
常见的退出条件:
- 工具调用完成
- 生成特定结构化输出
- 达到最大轮数
- Agent 主动放弃任务
python
Agents.run(
agent,
[UserMessage("What's the capital of the USA")]
)何时考虑创建多个 Agent
一般建议:首先最大化单个 Agent 的能力。更多 Agent 可以提供直观的概念分离,但可能引入额外的复杂性和开销。
需要拆分 Agent 的信号:
| 信号 | 说明 |
|---|---|
| 复杂逻辑 | 提示包含许多条件语句(多个 if-then-else 分支),提示模板难以扩展 |
| 工具过载 | 工具数量多、相似性或重叠度高,即使提供描述性名称和详细描述仍不能改善性能 |
多 Agent 系统
虽然多 Agent 系统可以为特定工作流和需求设计多种方式,但有以下常见类别:
| 模式 | 说明 |
|---|---|
| 管理模式 | 中央"管理器" Agent 协调多个专业 Agent,每个处理特定任务或领域 |
| 去中心化模式 | 多个 Agent 作为对等体运行,根据各自的专长相互交接任务 |
| 混合模式 | 多层管理 + 局部去中心化的组合,实际生产中最常见 |
多 Agent 系统可以建模为图,Agent 表示为节点:
- 管理模式:边表示工具调用
- 去中心化模式:边表示在 Agent 之间转移执行的交接
管理模式
管理模式授权一个中央 LLM——"管理器"——通过工具调用无缝协调专业 Agent 网络。
核心特点
- 管理器不会丢失上下文或控制权
- 智能地将任务委托给正确的 Agent 在正确的时间
- 轻松综合结果成有凝聚力的交互
- 确保平滑、统一的用户体验
架构图
用户输入: "Translate 'hello' to Spanish, French and Italian"
│
▼
┌───────────────┐
│ Manager │
└───────┬───────┘
│
┌───────────┼───────────┐
▼ ▼ ▼
┌───────┐ ┌───────┐ ┌───────┐
│Spanish│ │French │ │Italian│
│ Agent │ │ Agent │ │ Agent │
└───────┘ └───────┘ └───────┘实现示例
python
manager_agent = Agent(
name="manager_agent",
instructions=(
"You are a translation agent. You use tools given to you to translate. "
"If asked for multiple translations, you call the relevant tools."
),
tools=[
spanish_agent.as_tool(
tool_name="translate_to_spanish",
tool_description="Translate the user's message to Spanish",
),
french_agent.as_tool(
tool_name="translate_to_french",
tool_description="Translate the user's message to French",
),
italian_agent.as_tool(
tool_name="translate_to_italian",
tool_description="Translate the user's message to Italian",
),
],
)
async def main():
orchestrator_output = await Runner.run(
manager_agent,
"Translate 'hello' to Spanish, French and Italian",
)适用场景
这种模式非常适合您只希望一个 Agent 控制工作流执行并访问用户的场景。
去中心化模式
在去中心化模式中,Agent 可以相互"交接"工作流执行。交接是一种单向转移,允许一个 Agent 委托给另一个 Agent。
核心特点
- 交接是一种工具或函数
- 如果 Agent 调用交接函数,立即开始在交接的新 Agent 上执行
- 同时转移最新的对话状态
- 每个 Agent 都可以将工作流控制权移交给另一个 Agent
架构图
用户: "Where is my order?"
│
▼
┌───────────────┐
│ Triage │
└───────┬───────┘
│
┌───────────┼───────────┐
▼ ▼ ▼
┌────────┐ ┌───────┐ ┌───────┐
│ Issues │ │ Sales │ │Orders │
└───┬────┘ └───┬───┘ └───┬───┘
│ │ │
▼ ▼ ▼
"Let me help "Here are "On its
you with..." the plans" way!"实现示例
python
technical_support_agent = Agent(
name="Technical Support Agent",
instructions="You provide expert assistance with resolving technical issues.",
tools=[search_knowledge_base],
)
sales_assistant_agent = Agent(
name="Sales Assistant Agent",
instructions="You help enterprise clients browse the product catalog.",
tools=[initiate_purchase_order],
)
order_management_agent = Agent(
name="Order Management Agent",
instructions="You assist clients with inquiries regarding order tracking.",
tools=[search_order_status],
)适用场景
当您不需要单个 Agent 保持中央控制或综合时,这是最佳的——允许每个 Agent 接管执行并根据需要与用户交互。
声明式 vs 代码优先
| 方法 | 说明 | 优点 | 缺点 |
|---|---|---|---|
| 声明式 | 预先明确定义每个分支、循环和条件 | 可视化清晰 | 随着工作流变得更动态和复杂,变得繁琐 |
| 代码优先 | 直接使用熟悉的编程构造表达工作流逻辑 | 更灵活、更动态、更易调试 | 需要编程知识 |
建议:使用代码优先方法,无需预先定义整个图,实现更动态、更灵活的 Agent 编排。
总结
编排 Agent 的核心原则:
| 原则 | 说明 |
|---|---|
| 保持灵活性 | 保持组件灵活、可组合 |
| 匹配复杂性 | 使用与你的复杂性级别匹配的编排模式 |
| 增量演进 | 从单个 Agent 开始,仅在需要时演变为多 Agent 系统 |
| 避免过度设计 | 只有当复杂性能够切实改善结果时才增加复杂性 |
一句话总结:先从简单的单 Agent 开始,当单个 Agent 无法满足需求时,再考虑引入多 Agent 编排。
