70-多Agent协作-CrewAI-AutoGen-角色分工与信息传递协议

文章目录

  • 【70.Python+AI】多Agent协作实战:CrewAI与AutoGen的角色分工与信息传递
    • 导入语
    • [1 ~> 单Agent vs 多Agent](#1 ~> 单Agent vs 多Agent)
      • [1.1 单Agent的瓶颈](#1.1 单Agent的瓶颈)
      • [1.2 多Agent协作流程](#1.2 多Agent协作流程)
    • [2 ~> CrewAI:团队角色卡式框架](#2 ~> CrewAI:团队角色卡式框架)
      • [2.1 核心三件套](#2.1 核心三件套)
      • [2.2 定义任务与协作](#2.2 定义任务与协作)
      • [2.3 信息传递的本质](#2.3 信息传递的本质)
    • [3 ~> AutoGen:对话驱动式框架](#3 ~> AutoGen:对话驱动式框架)
      • [3.1 不同的协作哲学](#3.1 不同的协作哲学)
      • [3.2 CrewAI vs AutoGen 选型](#3.2 CrewAI vs AutoGen 选型)
    • [4 ~> 多Agent设计的四条军规](#4 ~> 多Agent设计的四条军规)
    • [思考 && 总结](#思考 && 总结)
    • 结尾

【70.Python+AI】多Agent协作实战:CrewAI与AutoGen的角色分工与信息传递

📖 文章简介: 本文系统讲解多Agent协作(Multi-Agent Collaboration)这一2025年Agent开发的热门方向。文章从"单Agent为什么搞不定复杂任务"切入,详解多Agent系统的三大设计核心:角色分工(Researcher/Writer/Reviewer的专业化拆分)、信息传递协议(Agent之间如何交接任务与结果)、以及两大主流框架的对比------CrewAI的"团队角色卡"式定义与AutoGen的"对话驱动"式协作。通过一个"调研→撰写→审核"三Agent协作完成研究报告的完整CrewAI代码案例,配上Mermaid协作流程图,适合已经掌握单Agent开发、想向多Agent系统进阶的开发者。


🎬 个人主页: 源码骑士

专栏传送门: 《Android开发基础》《python基础课程》

⭐️热衷从源码视角拆解技术底层原理,将复杂架构讲得通俗易懂


🎬 源码骑士的简介:

5年Android Framework系统开发经验,曾主导多项系统级性能优化专项

技术栈覆盖Android系统全链路(Binder/Handler/AMS/WMS/启动流程)及Java后端全家桶(Spring + MyBatis + Redis + Oracle)

累计产出原创技术文章100+篇,文章以流程图为特色,被读者评价为"看一篇胜过啃一周源码"


导入语

你写了一个Agent,给它塞了5个工具,让它"调研Python异步框架的市场情况并写一份报告"。结果它一会儿搜索、一会儿尝试总结、一会儿又回头搜索------上下文越滚越长,最后输出的报告前言不搭后语。

问题不在LLM的能力,在于你让一个Agent同时扮演了"调研员"和"撰稿人"两个角色。现实中一个研究员和一个编辑是两个人,AI世界里同样应该如此------这就是多Agent协作的思想:一个Agent查资料,一个Agent写报告,再加一个Agent审核把关。


1 ~> 单Agent vs 多Agent

1.1 单Agent的瓶颈

瓶颈 表现 原因
上下文膨胀 执行后期输出质量明显下降 搜索原文、中间结论、工具调用全挤在一个context里
角色混淆 既要客观调研又要主观写作 一个System Prompt很难同时定义两种人格
无质量关卡 写完直接输出,没人校对 自己写的自己看不出问题

1.2 多Agent协作流程

#mermaid-svg-KqJVhUi1SMm3Dc5Z{font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;fill:#333;}@keyframes edge-animation-frame{from{stroke-dashoffset:0;}}@keyframes dash{to{stroke-dashoffset:0;}}#mermaid-svg-KqJVhUi1SMm3Dc5Z .edge-animation-slow{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 50s linear infinite;stroke-linecap:round;}#mermaid-svg-KqJVhUi1SMm3Dc5Z .edge-animation-fast{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 20s linear infinite;stroke-linecap:round;}#mermaid-svg-KqJVhUi1SMm3Dc5Z .error-icon{fill:#552222;}#mermaid-svg-KqJVhUi1SMm3Dc5Z .error-text{fill:#552222;stroke:#552222;}#mermaid-svg-KqJVhUi1SMm3Dc5Z .edge-thickness-normal{stroke-width:1px;}#mermaid-svg-KqJVhUi1SMm3Dc5Z .edge-thickness-thick{stroke-width:3.5px;}#mermaid-svg-KqJVhUi1SMm3Dc5Z .edge-pattern-solid{stroke-dasharray:0;}#mermaid-svg-KqJVhUi1SMm3Dc5Z .edge-thickness-invisible{stroke-width:0;fill:none;}#mermaid-svg-KqJVhUi1SMm3Dc5Z .edge-pattern-dashed{stroke-dasharray:3;}#mermaid-svg-KqJVhUi1SMm3Dc5Z .edge-pattern-dotted{stroke-dasharray:2;}#mermaid-svg-KqJVhUi1SMm3Dc5Z .marker{fill:#333333;stroke:#333333;}#mermaid-svg-KqJVhUi1SMm3Dc5Z .marker.cross{stroke:#333333;}#mermaid-svg-KqJVhUi1SMm3Dc5Z svg{font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;}#mermaid-svg-KqJVhUi1SMm3Dc5Z p{margin:0;}#mermaid-svg-KqJVhUi1SMm3Dc5Z .label{font-family:"trebuchet ms",verdana,arial,sans-serif;color:#333;}#mermaid-svg-KqJVhUi1SMm3Dc5Z .cluster-label text{fill:#333;}#mermaid-svg-KqJVhUi1SMm3Dc5Z .cluster-label span{color:#333;}#mermaid-svg-KqJVhUi1SMm3Dc5Z .cluster-label span p{background-color:transparent;}#mermaid-svg-KqJVhUi1SMm3Dc5Z .label text,#mermaid-svg-KqJVhUi1SMm3Dc5Z span{fill:#333;color:#333;}#mermaid-svg-KqJVhUi1SMm3Dc5Z .node rect,#mermaid-svg-KqJVhUi1SMm3Dc5Z .node circle,#mermaid-svg-KqJVhUi1SMm3Dc5Z .node ellipse,#mermaid-svg-KqJVhUi1SMm3Dc5Z .node polygon,#mermaid-svg-KqJVhUi1SMm3Dc5Z .node path{fill:#ECECFF;stroke:#9370DB;stroke-width:1px;}#mermaid-svg-KqJVhUi1SMm3Dc5Z .rough-node .label text,#mermaid-svg-KqJVhUi1SMm3Dc5Z .node .label text,#mermaid-svg-KqJVhUi1SMm3Dc5Z .image-shape .label,#mermaid-svg-KqJVhUi1SMm3Dc5Z .icon-shape .label{text-anchor:middle;}#mermaid-svg-KqJVhUi1SMm3Dc5Z .node .katex path{fill:#000;stroke:#000;stroke-width:1px;}#mermaid-svg-KqJVhUi1SMm3Dc5Z .rough-node .label,#mermaid-svg-KqJVhUi1SMm3Dc5Z .node .label,#mermaid-svg-KqJVhUi1SMm3Dc5Z .image-shape .label,#mermaid-svg-KqJVhUi1SMm3Dc5Z .icon-shape .label{text-align:center;}#mermaid-svg-KqJVhUi1SMm3Dc5Z .node.clickable{cursor:pointer;}#mermaid-svg-KqJVhUi1SMm3Dc5Z .root .anchor path{fill:#333333!important;stroke-width:0;stroke:#333333;}#mermaid-svg-KqJVhUi1SMm3Dc5Z .arrowheadPath{fill:#333333;}#mermaid-svg-KqJVhUi1SMm3Dc5Z .edgePath .path{stroke:#333333;stroke-width:2.0px;}#mermaid-svg-KqJVhUi1SMm3Dc5Z .flowchart-link{stroke:#333333;fill:none;}#mermaid-svg-KqJVhUi1SMm3Dc5Z .edgeLabel{background-color:rgba(232,232,232, 0.8);text-align:center;}#mermaid-svg-KqJVhUi1SMm3Dc5Z .edgeLabel p{background-color:rgba(232,232,232, 0.8);}#mermaid-svg-KqJVhUi1SMm3Dc5Z .edgeLabel rect{opacity:0.5;background-color:rgba(232,232,232, 0.8);fill:rgba(232,232,232, 0.8);}#mermaid-svg-KqJVhUi1SMm3Dc5Z .labelBkg{background-color:rgba(232, 232, 232, 0.5);}#mermaid-svg-KqJVhUi1SMm3Dc5Z .cluster rect{fill:#ffffde;stroke:#aaaa33;stroke-width:1px;}#mermaid-svg-KqJVhUi1SMm3Dc5Z .cluster text{fill:#333;}#mermaid-svg-KqJVhUi1SMm3Dc5Z .cluster span{color:#333;}#mermaid-svg-KqJVhUi1SMm3Dc5Z div.mermaidTooltip{position:absolute;text-align:center;max-width:200px;padding:2px;font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:12px;background:hsl(80, 100%, 96.2745098039%);border:1px solid #aaaa33;border-radius:2px;pointer-events:none;z-index:100;}#mermaid-svg-KqJVhUi1SMm3Dc5Z .flowchartTitleText{text-anchor:middle;font-size:18px;fill:#333;}#mermaid-svg-KqJVhUi1SMm3Dc5Z rect.text{fill:none;stroke-width:0;}#mermaid-svg-KqJVhUi1SMm3Dc5Z .icon-shape,#mermaid-svg-KqJVhUi1SMm3Dc5Z .image-shape{background-color:rgba(232,232,232, 0.8);text-align:center;}#mermaid-svg-KqJVhUi1SMm3Dc5Z .icon-shape p,#mermaid-svg-KqJVhUi1SMm3Dc5Z .image-shape p{background-color:rgba(232,232,232, 0.8);padding:2px;}#mermaid-svg-KqJVhUi1SMm3Dc5Z .icon-shape .label rect,#mermaid-svg-KqJVhUi1SMm3Dc5Z .image-shape .label rect{opacity:0.5;background-color:rgba(232,232,232, 0.8);fill:rgba(232,232,232, 0.8);}#mermaid-svg-KqJVhUi1SMm3Dc5Z .label-icon{display:inline-block;height:1em;overflow:visible;vertical-align:-0.125em;}#mermaid-svg-KqJVhUi1SMm3Dc5Z .node .label-icon path{fill:currentColor;stroke:revert;stroke-width:revert;}#mermaid-svg-KqJVhUi1SMm3Dc5Z :root{--mermaid-font-family:"trebuchet ms",verdana,arial,sans-serif;} 通过
打回
用户任务
Researcher Agent

调研资料
信息传递协议

结构化交接
Writer Agent

撰写报告
Reviewer Agent

审核反馈
最终交付

三个Agent各司其职,上下文互相隔离,输出通过结构化的"交接单"传递。


2 ~> CrewAI:团队角色卡式框架

2.1 核心三件套

CrewAI 的建模方式非常直观------像组建一个真实团队:

python 复制代码
from crewai import Agent, Task, Crew

# 1. 定义角色(Agent = 团队成员 + 岗位说明书)
researcher = Agent(
    role="资深行业研究员",
    goal="收集全面、准确、有数据支撑的调研信息",
    backstory="你有10年市场调研经验,擅长从海量信息中提炼关键事实",
    tools=[search_tool],
    verbose=True
)

writer = Agent(
    role="技术报告撰稿人",
    goal="把调研素材组织成结构清晰、易读性强的报告",
    backstory="你曾就职于知名咨询公司的报告团队",
)

reviewer = Agent(
    role="质量审核专家",
    goal="检查报告的事实准确性、逻辑完整性和可读性",
    backstory="你是出了名的'挑刺王',不放过任何一处表述不清",
)

2.2 定义任务与协作

python 复制代码
# 2. 定义任务(Task = 工单)
research_task = Task(
    description="调研Python异步框架(asyncio/Trio/anyio)的市场应用现状",
    expected_output="一份包含框架对比、应用案例、趋势的调研纪要",
    agent=researcher
)

write_task = Task(
    description="基于调研纪要撰写2000字技术报告",
    expected_output="结构完整的Markdown报告",
    agent=writer,
    context=[research_task]   # 依赖research_task的输出
)

review_task = Task(
    description="审核报告,输出修改意见或'通过'结论",
    expected_output="审核意见列表或PASS",
    agent=reviewer,
    context=[write_task]
)

# 3. 组队并执行
crew = Crew(
    agents=[researcher, writer, reviewer],
    tasks=[research_task, write_task, review_task],
    verbose=True
)

result = crew.kickoff()

2.3 信息传递的本质

context=[research_task] 这一行就是信息传递协议------Writer Agent 执行时,会自动收到 Researcher 任务的输出作为上下文。CrewAI 的交接是"任务产物"级别的,不是"聊天记录"级别的,这保证了传递的是干净的结果而非杂乱的中间过程。


3 ~> AutoGen:对话驱动式框架

3.1 不同的协作哲学

AutoGen(微软开源)把多Agent协作建模为群聊------Agent之间通过多轮对话推进任务:

python 复制代码
from autogen import AssistantAgent, UserProxyAgent, GroupChat, GroupChatManager

analyst = AssistantAgent(
    name="analyst",
    system_message="你是数据分析师,负责解读数据并给出结论",
    llm_config={"model": "gpt-4"}
)

critic = AssistantAgent(
    name="critic",
    system_message="你是评审专家,负责质疑analyst的结论,要求证据支撑",
    llm_config={"model": "gpt-4"}
)

# 群聊管理器自动决定谁该发言
groupchat = GroupChat(
    agents=[analyst, critic],
    messages=[],
    max_round=10
)
manager = GroupChatManager(groupchat=groupchat, llm_config={"model": "gpt-4"})

3.2 CrewAI vs AutoGen 选型

对比维度 CrewAI AutoGen
协作模型 流水线+角色分工 多轮对话+自由讨论
控制精度 ------流程可预测 中------对话走向不完全可控
适合任务 流程明确的生产任务 需要思辨碰撞的开放任务
上手难度 ------概念贴近真实团队 中------GroupChat机制稍抽象
Token消耗 较低 较高(多轮对话滚雪球)

4 ~> 多Agent设计的四条军规

bash 复制代码
1. 角色要少而精:3~5个Agent是上限,再多信息传递成本指数上升
2. 交接要结构化:传JSON/Markdown文档,不传聊天记录
3. 每个Agent要有明确的"退出条件":什么时候算做完,写进goal里
4. 审核Agent必须独立:不能让Writer自己审自己

多Agent不是Agent越多越智能。两个配合默契的Agent,胜过五个职责模糊的Agent。


思考 && 总结

  1. 多Agent解决的是单Agent的"角色过载"问题: 调研、写作、审核是三种不同的人格和上下文需求,硬塞给一个Agent只会让输出质量全面下滑。
  2. CrewAI和AutoGen代表两种协作范式: CrewAI是"流水线分工",适合流程确定的生产场景;AutoGen是"圆桌讨论",适合需要观点碰撞的开放场景。
  3. 信息传递协议比Agent数量更重要: 结构化的任务产物传递(而非聊天记录共享)是多Agent系统不崩溃的关键。
  4. 控制Agent数量和退出条件: 超过5个Agent的系统,调试成本会超过它带来的收益。

多Agent协作的本质不是"多个AI一起干活",而是"用组织架构的思路设计AI系统"。你设计的是团队,不是代码。


结尾

各位小伙伴,本文的内容到这里就全部结束了,源码骑士在这里再次感谢您的阅读!

源码骑士 --- Android Framework & 全栈开发

👀 关注:跟博主一起从源码视角深耕底层原理,见证每一次成长

❤️ 点赞:让优质内容被更多人看见,让知识传递更有力量

收藏:把核心知识点存好,在需要时随时查、随时用

💬 评论:分享你的经验或疑问,评论区一起交流避坑

🔄 一键四连:不要忘记给博主"一键四连"哦!

🗡️ 寄语:技术之路难免有困惑,但同行的人会让前进更有方向

结语:当你的单Agent开始"精神错乱"时,别急着调Prompt------也许你该给它招几个同事了。不要忘记给博主"一键四连"哦!

相关推荐
Hachi被抢先注册了8 小时前
Skills总结
python
用户0332126663679 小时前
使用 Python 在 PowerPoint 中创建折线图和条形图
python
benchmark_cc9 小时前
如何用 Python 进行多周期 K 线合成与时区对齐?基于 QuantDash 与 Pandas 的量化数据清洗实战(附 GitHub 源码)
开发语言·python·github·盯盘·pandas·quantdash·量化数据
不如语冰9 小时前
AI大模型入门-参数的传递
数据结构·人工智能·pytorch·python
用户6760840656179 小时前
GraphBLAS_01_图的稀疏表示
python
小陈工9 小时前
第7篇:Django框架核心原理与实战深度解析(下)
后端·python·面试
用户298698530149 小时前
Python 数据处理:XML 与 Excel 互转的实用指南
后端·python·excel
m0_617493949 小时前
PyCharm 新手避坑指南:一文解决“项目列表消失”与“模块导入报错”两大玄学问题
ide·python·pycharm
中微极客9 小时前
从Prompt到RAG:LLM工程实战全链路解析
人工智能·python·prompt