MetaGPT 教程
概述
MetaGPT 是一个多 Agent 协作框架,核心理念是 Code = SOP(Team) —— 将人类软件工程的 SOP(标准作业程序)编码为 LLM Agent 的协作流程。
核心特性
- 软件公司隐喻:为 LLM 分配产品经理、架构师、工程师等不同角色,通过标准化 SOP 协同完成从需求到代码的全流程
- 三种反应模式:支持 react(标准 ReAct 循环)、by_order(顺序执行 action)、plan_and_act(先计划后执行)
- 消息路由系统:基于 cause_by 标签和 addresses 地址的消息发布/订阅机制
- Data Interpreter:可自主编写和执行 Python 代码的数据分析 Agent,内置 Jupyter 执行器
- RoleZero:新一代通用 Agent 基类,支持动态思考、工具推荐、经验检索和意图路由
- 多模型支持:OpenAI、Azure、Anthropic、Gemini、Ollama、智谱、千帆、Spark 等 10+ 模型提供商
快速开始
安装
pip install --upgrade metagptCLI 使用
metagpt "Create a 2048 game"作为库使用
from metagpt.software_company import generate_repo
repo = generate_repo("Create a 2048 game")print(repo)Data Interpreter
import asynciofrom metagpt.roles.di.data_interpreter import DataInterpreter
async def main(): di = DataInterpreter() await di.run("Run data analysis on sklearn Iris dataset, include a plot")
asyncio.run(main())教程目录
开发架构
| 章节 | 标题 | 核心内容 |
|---|---|---|
| 01 | 软件公司隐喻与多 Agent 系统 | SOP 编码为 Agent 协作、Team 组装、MGX 商业化 |
| 02 | Role 核心循环与三种反应模式 | _observe → _think → _act 循环、react/by_order/plan_and_act |
| 03 | Action 系统与 ActionNode 结构化输出 | Action 原子能力、ActionNode 约束 LLM 输出格式 |
| 04 | Environment 消息路由与多角色协同 | 消息发布/订阅、异步并行执行、ExtEnv 游戏环境 |
| 05 | Memory 系统与消息过滤 | 三级记忆架构、向量长短期记忆、任务级隔离 |
| 06 | 软件公司工作流:从需求到代码 | PRD → Design → Task → Code → Review 流水线 |
| 07 | Data Interpreter:数据分析与代码执行 Agent | Jupyter 代码执行、Plan-and-Execute 架构 |
| 08 | RoleZero:动态思考与工具执行 | 三层意图路由、工具推荐、经验检索、快速思考 |
| 09 | LLM Provider 多模型适配层 | 10+ 模型提供商、注册表模式、重试与成本追踪 |
| 10 | Tool 注册表与推荐系统 | 工具注册、BM25 推荐、工具执行映射 |
| 11 | 扩展环境:Android/Minecraft/Werewolf | ExtEnv 游戏环境集成、Gymnasium 接口 |
| 12 | 序列化、状态恢复与成本追踪 | JSON 序列化、中断恢复、Token 成本控制 |