6. AutoGen:微软的多智能体框架
这是什么:AutoGen = 微软开源的多智能体协作框架,主张「复杂任务让多个角色不同的 Agent 互相对话来解决」。
为什么用:第 5 章的 LangGraph 是「把一个 Agent 画成图」;当任务复杂到需要多个专精角色互相审核、接力时,就得有个框架帮你管「谁跟谁说话、什么时候收尾」。
如何用:本章先看清 AutoGen 的生态(三条线并存),再跑通一个「写稿 + 审稿」的双 Agent 小组,最后掌握终止条件这个必备件。
基线:AutoGen(0.4+ 重写版)
autogen-agentchat0.7.5 ·autogen-core0.7.5 ·autogen-ext0.7.5 · Python ≥ 3.10 ⚠️ 本章第一节请先读完:AutoGen 生态在 2025–2026 年经历了一次重要交接,用错包会很困惑。
6.1 先看清生态:AutoGen、AG2、Agent Framework
这是很多人踩坑的地方。2026 年的现状是三条线并存:
| 项目 | 包名 | 现状 | 适合谁 |
|---|---|---|---|
| AutoGen(微软原版) | autogen-agentchat / autogen-core / autogen-ext |
2025-10 起进入维护模式,不再加新功能 | 已经在用、想保持稳定的老项目 |
| AG2(社区分叉) | ag2 |
活跃维护,2026-10 仍在发版 | 想要 AutoGen 风格 + 持续更新 |
| Microsoft Agent Framework(MAF) | agent-framework |
微软的正式接班人,2026-04 已 GA,1.x 生产可用 | 新项目、要做企业级 |
pip install autogen-agentchat autogen-ext # AutoGen 原版
pip install ag2 # AG2 社区分叉
pip install agent-framework # 微软接班人
怎么选:本教程讲 AutoGen 原版(它的 API 最经典、资料最多)。但如果你现在从零起新项目,微软官方的建议是直接上 Agent Framework;如果你想要「AutoGen 那套体验但要持续更新」,看 AG2。三者的设计思想是相通的——多 Agent 靠消息互相协作,靠终止条件收尾。
6.2 安装与第一个 Agent
pip install autogen-agentchat autogen-ext
AutoGen 的模型客户端放在 autogen-ext 里,OpenAI 兼容的写法照旧:
import os
import asyncio
from autogen_agentchat.agents import AssistantAgent
from autogen_agentchat.conditions import MaxMessageTermination
from autogen_agentchat.teams import RoundRobinGroupChat
from autogen_ext.models.openai import OpenAIChatCompletionClient
model = OpenAIChatCompletionClient(
model=os.environ.get("CHAT_MODEL", "gpt-4o-mini"),
api_key=os.environ["LLM_API_KEY"],
base_url=os.environ.get("LLM_BASE_URL", "https://api.openai.com/v1"),
)
async def main():
writer = AssistantAgent("writer", model_client=model,
description="负责把研究结果写成一段通顺的文字")
reviewer = AssistantAgent("reviewer", model_client=model,
description="负责挑毛病,指出写得不好的地方")
team = RoundRobinGroupChat(
participants=[writer, reviewer],
termination_condition=MaxMessageTermination(max_messages=6),
)
result = await team.run(task="写一段话介绍什么是 RAG,交给审稿人把关。")
print(result.messages[-1].content)
asyncio.run(main())
你会看到(示意):
写一段话介绍什么是 RAG,交给审稿人把关。
写一段话介绍什么是 RAG,交给审稿人把关。
RAG 是检索增强生成:先从资料库检索相关片段,再让模型基于这些片段作答……
description 字段是给其他 Agent 看的——组长靠它决定把活派给谁,所以要写清楚「我是干什么的」。
6.3 核心概念:Agent 与 Team
| 概念 | 是什么 | 对应第 4 章的 |
|---|---|---|
| Agent | 一个有角色、有工具、会说话的角色 | 「思考—行动—观察」里的「思考+行动」 |
| Team | 多个 Agent 的协作方式 | 整个循环 |
| Message | Agent 之间(和人)传的话 | 上下文里的消息 |
| Termination | 什么时候停下来 | 循环的停止条件 |
三种常用 Team:
| Team | 谁来发言 | 适合 |
|---|---|---|
RoundRobinGroupChat |
轮流发言,顺序固定 | 写稿→审稿这种流水线 |
SelectorGroupChat |
由一个组长挑下一个发言者 | 需要动态分工的活 |
Swarm |
Agent 自己决定交给谁 | 客服转接、专家接力 |
from autogen_agentchat.teams import SelectorGroupChat
team = SelectorGroupChat(
participants=[writer, reviewer],
group_chat_manager_name="组长", # 负责挑人
model_client=model,
termination_condition=MaxMessageTermination(max_messages=6),
)
6.4 终止条件:让对话停下来
没有终止条件,两个 Agent 能聊到天荒地老、烧光额度。AutoGen 把停止条件做成了可组合的部件:
| 条件 | 何时停 |
|---|---|
MaxMessageTermination(max_messages=N) |
消息数到 N 就停 |
TextMentionTermination("TERMINATE") |
有人说出指定关键词就停 |
SourceMatchTermination(source="user") |
轮到「用户」说话时停 |
HandoffTermination(target="agent_x") |
交接给某个 Agent 时停 |
from autogen_agentchat.conditions import TextMentionTermination
# 两种条件组合:谁先满足听谁的
termination = MaxMessageTermination(max_messages=10) | TextMentionTermination("完成")
第 3 章说的「步数上限」,在 AutoGen 里就是 MaxMessageTermination。
6.5 给 Agent 装工具
工具用法和第 8 章讲的一样,只是换了个声明方式:
from autogen_core.tools import FunctionTool
def get_weather(city: str) -> str:
"""查询指定城市的当前天气。"""
fake = {"北京": "晴,26℃", "上海": "多云,29℃"}
return fake.get(city, f"没查到 {city} 的天气")
weather_tool = FunctionTool(get_weather, description="查询城市天气")
agent = AssistantAgent("助手", model_client=model, tools=[weather_tool])
6.6 状态与人工介入
对话的完整记录在 result.messages 里。想让它停下来问人,用 UserProxyAgent(人扮的 Agent)或团队层的 HandoffTermination。
from autogen_agentchat.agents import UserProxyAgent
human = UserProxyAgent("human", input_func=input) # 轮到它时,等你敲键盘
team = RoundRobinGroupChat(participants=[writer, reviewer, human],
termination_condition=MaxMessageTermination(max_messages=6))
这也是第 5 章 interrupt() 解决的那类问题——AutoGen 提供了另一套答案。
6.7 常见翻车现场
| 报错 / 现象 | 原因 | 修法 |
|---|---|---|
ModuleNotFoundError: autogen_agentchat |
只装了 pyautogen(老包) |
装 autogen-agentchat + autogen-ext |
| 装了却用不了新 API | 装的是 AG2 或老的 0.2 版 | 确认 pip show autogen-agentchat 的版本 |
| Agent 说话没干货 | description 没写或太模糊 |
写清「我是谁、我负责什么」 |
| 对话停不下来 | 没设终止条件 | 加 MaxMessageTermination |
| 模型调用报错 | base_url 没传或 key 错 |
检查 OpenAIChatCompletionClient 的参数 |
| 新项目该不该用 | AutoGen 在维护模式 | 新项目看 Microsoft Agent Framework |
6.8 动手改
- 把
RoundRobinGroupChat换成SelectorGroupChat,看「谁来发言」是怎么被组长决定的 - 给
writer装上get_weather工具,让它写之前先查数据 - 把终止条件换成
TextMentionTermination("完成"),让 Agent 自己喊停
6.9 小结
- AutoGen 的主张:复杂任务让多个角色不同的 Agent 互相对话
- 三个核心:Agent(角色 + 工具)、Team(协作方式)、Termination(何时停)
- Team 常用三种:轮流(RoundRobin)、组长挑人(Selector)、自主交接(Swarm)
- 生态有三条线:AutoGen(维护模式)、AG2(社区分叉)、Microsoft Agent Framework(微软接班人)
- 不设终止条件 = 烧钱,务必配一个
下一章:CrewAI,换一种思路:给 Agent 分配「角色」和「任务」 🐸