离线资料 · 01-ai-agent-handbook/chapters/13-collaboration-patterns.html
深入理解六大协作模式,掌握复杂任务的分治策略。
Agent A → Agent B → Agent C → 结果
适用场景: 固定流程、每个步骤职责明确 代表框架: CrewAI Sequential
┌→ Agent A ─┐
用户输入 ──────┼→ Agent B ─┼→ 汇总
└→ Agent C ─┘
适用场景: 多维分析、并行研究 代表框架: LangGraph Parallel
┌──────────────┐
│ Supervisor │
└──────┬───────┘
│
┌─────────┼─────────┐
↓ ↓ ↓
Agent A Agent B Agent C
适用场景: 工单分流、客服系统 代表框架: LangGraph Supervisor
Agent A ↔ Agent B ↔ Agent C
↕ ↕ ↕
(动态路由)
适用场景: 灵活路由、多域客服 代表框架: OpenAI Swarm
Manager
│
┌────┴────┐
Team A Team B
│ │
Worker Worker
适用场景: 大型项目管理 代表框架: LangGraph Subgraph
┌─────────────┐
│ Shared │
│ Memory │
└──────┬──────┘
│
┌──────┼──────┐
↓ ↓ ↓
Agent A Agent B Agent C
(主动读写)
适用场景: 异步长任务、复杂依赖 代表框架: 自定义实现
| 模式 | 复杂度 | 灵活性 | 调试难度 | 适用规模 |
|---|---|---|---|---|
| 顺序流水线 | 低 | 低 | 低 | 简单任务 |
| 并行扇出 | 中 | 中 | 中 | 中等任务 |
| Supervisor | 中 | 高 | 中 | 复杂任务 |
| Swarm | 高 | 高 | 高 | 大型系统 |
| 层级嵌套 | 高 | 高 | 高 | 超大型项目 |
| 黑板架构 | 极高 | 极高 | 极高 | 研究场景 |
# supervisor_pattern.py
from langgraph.graph import StateGraph, END
from typing import TypedDict
from langchain_openai import ChatOpenAI
# 初始化 LLM(示例用 OpenAI,可替换为其他提供商)
llm = ChatOpenAI(model="gpt-4o-mini")
class SupervisorState(TypedDict):
task: str
chosen_agent: str
result: str
def supervisor(state: SupervisorState) -> dict:
"""决策者:选择最合适的 Agent"""
# 使用 LLM 判断任务类型
prompt = f"""根据任务类型选择合适的专家:
任务: {state['task']}
可选专家:
- researcher: 负责信息检索
- analyst: 负责数据分析
- writer: 负责内容创作
请输出 JSON: {{"chosen_agent": "专家名称"}}"""
response = llm.invoke(prompt)
import json
choice = json.loads(response.content)
return {"chosen_agent": choice["chosen_agent"]}
def researcher(state):
return {"result": "执行研究..."}
def analyst(state):
return {"result": "执行分析..."}
def writer(state):
return {"result": "撰写报告..."}
def route_to_agent(state):
if state["chosen_agent"] == "researcher":
return "researcher"
elif state["chosen_agent"] == "analyst":
return "analyst"
else:
return "writer"
# 构建图
workflow = StateGraph(SupervisorState)
workflow.add_node("supervisor", supervisor)
workflow.add_node("researcher", researcher)
workflow.add_node("analyst", analyst)
workflow.add_node("writer", writer)
workflow.set_entry_point("supervisor")
workflow.add_conditional_edges("supervisor", route_to_agent)
workflow.add_edge("researcher", END)
workflow.add_edge("analyst", END)
workflow.add_edge("writer", END)
graph = workflow.compile()
任务复杂度?
/ \
简单 复杂
| |
顺序流水线 Agent 数量?
/ \
2-3 4+
| |
并行扇出 Supervisor?
/ \
是 否
| |
层级嵌套 Swarm/黑板
✅ 理解了六大协作模式的原理和适用场景
✅ 掌握了模式的对比和选择方法
✅ 学会了 Supervisor 模式的实现