17.5 CAMEL:探索智能体缩放法则的开源框架

📖 本节概览
CAMEL(Communicative Agents for "Mind" Exploration of Large Language Model Society)是一个开源的多智能体框架,由CAMEL-AI.org社区开发和维护。它的核心使命是探索智能体的缩放法则(Scaling Laws of Agents)。
与CrewAI、LangGraph等框架不同,CAMEL更加注重学术研究和大规模智能体系统的模拟与分析。它支持多达百万级别智能体的模拟,是研究智能体群体行为、能力边界和潜在风险的重要工具。
🎯 核心定位
| 特性 | 描述 |
|---|---|
| 定位 | 学术研究驱动的多智能体框架 |
| 论文 | arXiv:2303.17760 |
| GitHub Stars | 11.7k+ |
| 核心团队 | 100+ 研究人员 |
| 支持规模 | 百万级智能体模拟 |
🧬 框架设计原则
CAMEL框架遵循四个核心设计原则,这些原则使其在学术研究和大规模智能体系统方面具有独特优势:
1. Evolvability(可进化性)
┌─────────────────────────────────────────────────────────────┐
│ 可进化性架构 │
│ │
│ ┌──────────┐ ┌──────────┐ ┌──────────┐ │
│ │ Agent │────▶│Environment│────▶│ Data │ │
│ │ System │ │ Interaction│ │Generation│ │
│ └──────────┘ └──────────┘ └──────────┘ │
│ │ │ │ │
│ ▼ ▼ ▼ │
│ ┌──────────────────────────────────────────────┐ │
│ │ 强化学习 / 监督学习 驱动的持续进化 │ │
│ └──────────────────────────────────────────────┘ │
└─────────────────────────────────────────────────────────────┘- 多智能体系统通过生成数据和与环境交互来持续进化
- 进化可以通过强化学习(带可验证奖励)或监督学习驱动
- 支持智能体在运行过程中不断优化策略
2. Scalability(可扩展性)
框架设计支持百万级智能体的协调、通信和资源管理:
# CAMEL 可以模拟大规模社会系统
from camel.societies import RolePlaying
# 支持复杂的多智能体交互
# - 动态通信
# - 分布式协调
# - 资源优化管理3. Statefulness(状态保持)
┌─────────────────────────────────────────────────────────────┐
│ 状态记忆系统 │
│ │
│ Step 1 Step 2 Step 3 Step 4 Step 5 │
│ │ │ │ │ │ │
│ ▼ ▼ ▼ ▼ ▼ │
│ ┌───────────────────────────────────────────┐ │
│ │ Stateful Memory Store │ │
│ │ ┌─────┐ ┌─────┐ ┌─────┐ ┌─────┐ ┌─────┐ │ │
│ │ │Mem 1│ │Mem 2│ │Mem 3│ │Mem 4│ │Mem 5│ │ │
│ │ └─────┘ └─────┘ └─────┘ └─────┘ └─────┘ │ │
│ └───────────────────────────────────────────┘ │
│ │ │
│ ▼ │
│ Multi-step Task Execution │
└─────────────────────────────────────────────────────────────┘- 智能体维护有状态的记忆
- 支持与环境的多步交互
- 能够高效处理复杂的长期任务
4. Code-as-Prompt(代码即提示)
这是CAMEL最独特的设计理念:
"Every line of code and comment serves as a prompt for agents."
# 代码和注释都是智能体的提示
# 好的代码既要让人类理解,也要让AI理解
class ChatAgent:
"""
ChatAgent是CAMEL的核心智能体类。
它可以进行对话、使用工具、保持记忆。
这段注释本身就是Agent理解自身能力的提示。
"""
pass🏗️ 技术架构

核心模块结构
camel/
├── agents/ # 智能体模块
│ ├── base.py # 基类 BaseAgent
│ ├── chat_agent.py # 核心对话智能体
│ ├── critic_agent.py # 批评智能体
│ ├── task_agent.py # 任务规划智能体
│ ├── search_agent.py # 搜索智能体
│ ├── embodied_agent.py # 具身智能体
│ ├── mcp_agent.py # MCP协议智能体
│ ├── knowledge_graph_agent.py # 知识图谱智能体
│ └── repo_agent.py # 代码仓库智能体
│
├── societies/ # 社会模拟模块
│ ├── role_playing.py # 角色扮演(核心)
│ ├── babyagi_playing.py # BabyAGI集成
│ └── workforce/ # 工作力系统
│ ├── workforce.py # 工作力管理
│ ├── worker.py # 工作者基类
│ ├── task_channel.py # 任务通道
│ └── prompts.py # 提示词模板
│
├── models/ # LLM模型后端
│ ├── openai_model.py # OpenAI
│ ├── anthropic_model.py # Anthropic Claude
│ ├── gemini_model.py # Google Gemini
│ ├── deepseek_model.py # DeepSeek
│ ├── ollama_model.py # Ollama (本地)
│ └── ... (30+ 模型支持)
│
├── toolkits/ # 工具集
│ ├── search_toolkit.py # 搜索工具
│ ├── browser_toolkit.py # 浏览器工具
│ ├── code_execution.py # 代码执行
│ ├── github_toolkit.py # GitHub工具
│ ├── gmail_toolkit.py # Gmail工具
│ └── ... (60+ 工具)
│
├── memories/ # 记忆系统
├── storages/ # 存储后端
├── retrievers/ # 检索器
├── datagen/ # 数据生成
└── benchmarks/ # 基准测试智能体类型一览
| 智能体类型 | 用途 | 核心能力 |
|---|---|---|
ChatAgent | 核心对话智能体 | 对话、工具调用、记忆 |
CriticAgent | 批评评估 | 评估输出质量、提供反馈 |
TaskSpecifyAgent | 任务细化 | 将模糊任务转化为具体指令 |
TaskPlannerAgent | 任务规划 | 分解复杂任务为子任务 |
SearchAgent | 信息检索 | 多源搜索、信息整合 |
EmbodiedAgent | 具身交互 | 与物理/虚拟环境交互 |
MCPAgent | MCP协议 | 模型上下文协议集成 |
KnowledgeGraphAgent | 知识图谱 | 构建和查询知识图谱 |
RepoAgent | 代码分析 | 理解和操作代码仓库 |
🎭 角色扮演系统 (Role-Playing)

CAMEL的角色扮演系统是其核心特性之一,允许两个AI智能体进行角色扮演对话:
基本概念
from camel.societies import RolePlaying
# 创建角色扮演会话
role_play = RolePlaying(
assistant_role_name="Python程序员", # 助手角色
user_role_name="股票交易员", # 用户角色
task_prompt="开发一个量化交易策略", # 任务描述
with_task_specify=True, # 启用任务细化
with_task_planner=False, # 禁用任务规划
with_critic_in_the_loop=False, # 禁用批评者
)角色扮演流程
┌─────────────────────────────────────────────────────────────┐
│ Role-Playing 流程 │
│ │
│ ┌────────────────┐ ┌────────────────┐ │
│ │ User Agent │◄──────▶│Assistant Agent │ │
│ │ (股票交易员) │ │ (Python程序员) │ │
│ └────────────────┘ └────────────────┘ │
│ │ │ │
│ │ Task: 开发量化交易策略 │ │
│ │ │ │
│ ▼ ▼ │
│ ┌──────────────────────────────────────────┐ │
│ │ 可选组件 │ │
│ │ ┌──────────┐ ┌──────────┐ ┌──────────┐│ │
│ │ │TaskSpec │ │TaskPlan │ │ Critic ││ │
│ │ │ Agent │ │ Agent │ │ Agent ││ │
│ │ └──────────┘ └──────────┘ └──────────┘│ │
│ └──────────────────────────────────────────┘ │
└─────────────────────────────────────────────────────────────┘代码示例
from camel.societies import RolePlaying
from camel.types import TaskType
# 初始化角色扮演
role_play = RolePlaying(
assistant_role_name="AI研究员",
user_role_name="产品经理",
task_prompt="设计一个多智能体客服系统",
task_type=TaskType.AI_SOCIETY,
with_task_specify=True,
)
# 获取初始消息
input_msg = role_play.init_chat()
# 执行多轮对话
for round_idx in range(10):
assistant_response, user_response = role_play.step(input_msg)
# 检查是否完成
if "CAMEL_TASK_DONE" in assistant_response.msg.content:
break
input_msg = assistant_response.msg
print(f"Round {round_idx}: {input_msg.content[:100]}...")👥 Workforce 系统

Workforce是CAMEL中更高级的多智能体协作系统,支持:
- 自动任务分解:将复杂任务分解为可管理的子任务
- 动态工作者分配:根据任务需求分配合适的智能体
- Pipeline模式:预定义的任务处理流程
- 错误恢复:自动处理失败并重试
Workforce 架构
┌─────────────────────────────────────────────────────────────┐
│ Workforce 系统架构 │
│ │
│ ┌─────────────────────────────────────────┐ │
│ │ Task Manager │ │
│ │ ┌─────────────────────────────────┐ │ │
│ │ │ Task Decomposition │ │ │
│ │ │ Task Assignment │ │ │
│ │ │ Quality Evaluation │ │ │
│ │ └─────────────────────────────────┘ │ │
│ └─────────────────────────────────────────┘ │
│ │ │
│ ▼ │
│ ┌─────────────────────────────────────────┐ │
│ │ Task Channel │ │
│ └─────────────────────────────────────────┘ │
│ │ │ │ │ │
│ ▼ ▼ ▼ ▼ │
│ ┌─────────┐ ┌─────────┐ ┌─────────┐ ┌─────────┐ │
│ │ Worker 1│ │ Worker 2│ │ Worker 3│ │ Worker N│ │
│ │(Single) │ │(Single) │ │(RolePlay)│ │(Custom)│ │
│ └─────────┘ └─────────┘ └─────────┘ └─────────┘ │
└─────────────────────────────────────────────────────────────┘Worker 类型
| Worker类型 | 描述 | 适用场景 |
|---|---|---|
SingleAgentWorker | 单智能体工作者 | 简单独立任务 |
RolePlayingWorker | 角色扮演工作者 | 需要多视角协作的任务 |
CustomWorker | 自定义工作者 | 特殊需求 |
代码示例
from camel.societies.workforce import Workforce
from camel.agents import ChatAgent
from camel.tasks import Task
# 创建工作力系统
workforce = Workforce(
description="数据分析团队",
)
# 添加工作者
analyst = ChatAgent(
system_message="你是数据分析专家",
tools=[search_tool, code_tool],
)
workforce.add_single_agent_worker(
description="数据分析师",
worker=analyst,
)
# 创建并执行任务
task = Task(
content="分析2024年科技股走势",
additional_info="关注FAANG股票",
)
# 执行任务
result = workforce.process_task(task)🛠️ 工具系统
CAMEL提供了**60+**预构建工具,覆盖各种常见场景:
工具分类
| 类别 | 工具 | 描述 |
|---|---|---|
| 搜索 | SearchToolkit | Google、DuckDuckGo、Bing搜索 |
| 浏览器 | BrowserToolkit | 网页浏览、截图、交互 |
| 代码 | CodeExecutionToolkit | Python代码执行 |
| 文件 | FileToolkit | 文件读写操作 |
| GitHub | GithubToolkit | 仓库操作、PR管理 |
| 邮件 | GmailToolkit | Gmail收发邮件 |
| 日历 | GoogleCalendarToolkit | 日程管理 |
| 地图 | GoogleMapsToolkit | 地理信息查询 |
| 学术 | ArxivToolkit, PubmedToolkit | 论文检索 |
| 数学 | MathToolkit, SymPyToolkit | 数学计算 |
| 社交 | TwitterToolkit, RedditToolkit | 社交媒体 |
工具使用示例
from camel.agents import ChatAgent
from camel.toolkits import SearchToolkit, CodeExecutionToolkit
# 创建工具实例
search_tool = SearchToolkit()
code_tool = CodeExecutionToolkit()
# 创建带工具的智能体
agent = ChatAgent(
system_message="你是一个研究助手",
tools=[
*search_tool.get_tools(),
*code_tool.get_tools(),
],
)
# 智能体会自动选择合适的工具
response = agent.step("搜索最新的GPT-5信息并用Python可视化")📊 数据生成系统

CAMEL的数据生成系统是其重要特性,支持多种数据生成模式:
1. Chain-of-Thought (CoT) 数据生成

from camel.datagen import CoTDataGenerator
# 创建CoT数据生成器
generator = CoTDataGenerator(
model=model,
task_type="reasoning",
)
# 生成训练数据
data = generator.generate(
num_samples=1000,
complexity="medium",
)2. Self-Instruct 数据生成
from camel.datagen.self_instruct import SelfInstructGenerator
# 自动生成指令-响应对
generator = SelfInstructGenerator()
dataset = generator.generate(
seed_tasks=["写一首诗", "解释量子力学"],
num_instructions=500,
)3. Source2Synth 数据生成
从源文档生成合成训练数据:
from camel.datagen.source2synth import Source2SynthGenerator
# 从PDF/网页生成QA数据
generator = Source2SynthGenerator()
qa_pairs = generator.from_documents(
sources=["paper.pdf", "https://example.com"],
output_format="qa_pairs",
)🔬 研究项目
CAMEL支撑了多个重要的研究项目:
OWL:浏览器自动化智能体

OWL(Optimized Web Learning)是CAMEL团队开发的先进浏览器自动化智能体,在GAIA基准测试中表现优异。
OASIS:大规模社会模拟

OASIS(Open Agent Society for Intelligent Simulation)支持模拟具有复杂行为和社会结构的大规模智能体社会。
CRAB:跨环境智能体基准

CRAB(Cross-environment Agent Benchmark)是一个跨平台的智能体评估框架。
Loong:长文本理解

Loong专注于解决长文本理解和处理问题。
Agent Trust:智能体信任研究

研究多智能体系统中的信任机制和行为。
Emos:情感智能体

Emos探索智能体的情感理解和表达能力。
🔗 RAG 集成

CAMEL提供完整的RAG(Retrieval-Augmented Generation)支持:
RAG 组件
┌─────────────────────────────────────────────────────────────┐
│ CAMEL RAG Pipeline │
│ │
│ ┌──────────┐ ┌──────────┐ ┌──────────┐ │
│ │ Loaders │───▶│Chunking │───▶│Embedding │ │
│ │ PDF/Web │ │Strategies│ │ Models │ │
│ └──────────┘ └──────────┘ └──────────┘ │
│ │ │
│ ▼ │
│ ┌──────────────────────────────────────────┐ │
│ │ Vector Stores │ │
│ │ Qdrant / Milvus / Chroma / Pinecone │ │
│ └──────────────────────────────────────────┘ │
│ │ │
│ ▼ │
│ ┌──────────────────────────────────────────┐ │
│ │ Retrievers │ │
│ │ Semantic / Hybrid / Re-ranking │ │
│ └──────────────────────────────────────────┘ │
│ │ │
│ ▼ │
│ ┌──────────────────────────────────────────┐ │
│ │ ChatAgent + RAG │ │
│ └──────────────────────────────────────────┘ │
└─────────────────────────────────────────────────────────────┘代码示例
from camel.retrievers import AutoRetriever
from camel.embeddings import OpenAIEmbedding
# 创建检索器
retriever = AutoRetriever(
embedding_model=OpenAIEmbedding(),
vector_store="qdrant",
)
# 索引文档
retriever.ingest(documents=["doc1.pdf", "doc2.pdf"])
# 检索相关内容
results = retriever.query("量化交易策略", top_k=5)📈 LLM 模型支持
CAMEL支持**30+**主流LLM平台:
| 平台 | 模型 | 特点 |
|---|---|---|
| OpenAI | GPT-4, GPT-4o, o1 | 最强综合能力 |
| Anthropic | Claude 3.5 Sonnet, Opus | 长上下文、安全 |
| Gemini 1.5 Pro/Flash | 多模态 | |
| DeepSeek | DeepSeek V3, R1 | 高性价比 |
| Ollama | Llama, Qwen等 | 本地部署 |
| Azure OpenAI | 同OpenAI | 企业级 |
| AWS Bedrock | 多模型 | 云原生 |
| Groq | 超快推理 | 低延迟 |
| Together AI | 多模型 | 开放平台 |
模型切换示例
from camel.models import ModelFactory
from camel.types import ModelPlatformType, ModelType
# OpenAI
openai_model = ModelFactory.create(
model_platform=ModelPlatformType.OPENAI,
model_type=ModelType.GPT_4O,
)
# Anthropic Claude
claude_model = ModelFactory.create(
model_platform=ModelPlatformType.ANTHROPIC,
model_type="claude-3-5-sonnet-20241022",
)
# DeepSeek
deepseek_model = ModelFactory.create(
model_platform=ModelPlatformType.DEEPSEEK,
model_type="deepseek-chat",
)
# 本地 Ollama
ollama_model = ModelFactory.create(
model_platform=ModelPlatformType.OLLAMA,
model_type="qwen2.5:32b",
)🔄 与其他框架对比
| 特性 | CAMEL | CrewAI | LangGraph | AutoGen |
|---|---|---|---|---|
| 核心定位 | 学术研究 | 生产应用 | 工作流编排 | 多智能体对话 |
| 智能体规模 | 百万级 | 小团队 | 中等 | 中等 |
| 设计理念 | Code-as-Prompt | 角色分工 | 状态图 | 对话驱动 |
| 状态管理 | 有状态记忆 | 简单状态 | 强状态图 | 会话状态 |
| 数据生成 | ✅ 强支持 | ❌ | ❌ | ❌ |
| 学术论文 | ✅ 多篇 | ❌ | ❌ | ✅ |
| 社会模拟 | ✅ OASIS | ❌ | ❌ | ❌ |
| 工具数量 | 60+ | 20+ | 自定义 | 20+ |
| 模型支持 | 30+ | 10+ | 自定义 | 10+ |
何时选择 CAMEL?
✅ 选择CAMEL当你需要:
- 进行学术研究,需要可复现的实验
- 模拟大规模智能体社会
- 生成高质量的训练数据
- 探索智能体的缩放法则
- 研究智能体的信任和社会行为
❌ 可能不适合CAMEL的场景:
- 需要简单的工作流编排(考虑LangGraph)
- 需要快速上手的生产应用(考虑CrewAI)
- 只需要简单的对话系统
📚 快速开始
安装
# 基础安装
pip install camel-ai
# 完整安装(包含所有依赖)
pip install 'camel-ai[all]'第一个 ChatAgent
from camel.agents import ChatAgent
# 创建智能体
agent = ChatAgent(
system_message="你是一个友好的助手。",
)
# 对话
response = agent.step("你好,请自我介绍一下。")
print(response.msg.content)第一个 RolePlaying
from camel.societies import RolePlaying
# 创建角色扮演
role_play = RolePlaying(
assistant_role_name="Python专家",
user_role_name="初学者",
task_prompt="教授Python基础知识",
)
# 初始化并执行
input_msg = role_play.init_chat()
for _ in range(5):
assistant_response, user_response = role_play.step(input_msg)
print(f"专家: {assistant_response.msg.content[:200]}...")
input_msg = assistant_response.msg🎓 学习资源
官方资源
| 资源 | 链接 | 描述 |
|---|---|---|
| GitHub | camel-ai/camel | 源代码仓库 |
| 文档 | docs.camel-ai.org | 官方文档 |
| 论文 | arXiv:2303.17760 | 原始论文 |
| Discord | discord.camel-ai.org | 社区讨论 |
Cookbooks 推荐
基础概念
- ChatAgent使用
- 工具调用
- 记忆系统
高级特性
- RolePlaying深入
- Workforce系统
- RAG集成
数据生成
- CoT数据生成
- Self-Instruct
- Source2Synth
多智能体应用
- 社会模拟
- 协作任务
- 竞争博弈
📖 本章小结
CAMEL是一个独特的多智能体框架,其核心价值在于:
- 学术研究导向:支持可复现的研究实验
- 大规模模拟:支持百万级智能体社会模拟
- 数据生成能力:强大的合成数据生成工具
- Code-as-Prompt:独特的设计理念
- 丰富的工具生态:60+预构建工具
CAMEL特别适合需要深入研究智能体行为、探索缩放法则、进行社会模拟的研究者和开发者。
📑 参考资料
- CAMEL论文: Li, G., et al. (2023). "CAMEL: Communicative Agents for 'Mind' Exploration of Large Language Model Society". arXiv:2303.17760
- GitHub仓库: https://github.com/camel-ai/camel
- 官方文档: https://docs.camel-ai.org
- OWL项目: 浏览器自动化智能体
- OASIS项目: 大规模社会模拟
LearnGraph.online | Module 17 - CAMEL多智能体框架
撰写日期: 2024年12月