camel-ai/camel · 上手攻略

  • 仓库:camel-ai/camel
  • 链接:https://github.com/camel-ai/camel
  • 分类:agent · ai(ai)
  • 作者:Jay
  • 更新:2026-07-07

这是什么

CAMEL(camel-ai/camel)是首个专注多 Agent 系统研究的开源框架,自称"Finding the Scaling Law of Agents"。它不是单 Agent 工具,而是一个用于构建、运行和分析大规模多 Agent 系统的平台级框架,官方定位是研究框架,支持模拟最多 100 万个 Agent 同时运行。CAMEL 背后是一个 100+ 研究员组成的国际社区,专注于多 Agent 的缩放定律(Scaling Law)研究。

解决的问题: 单 Agent 框架(如 LangChain Agent)只解决"一个 Agent 怎么用工具",而 CAMEL 解决的是"多个 Agent 如何协作、如何分工、如何涌现能力"——这是多 Agent 时代最核心的研究问题。


快速安装

# 标准安装(PyPI)
pip install camel-ai

# 完整安装(含所有可选依赖)
pip install "camel-ai[all]"

# 安装特定工具包
pip install "camel-ai[web_tools]"    # 含 DuckDuckGo 搜索
pip install "camel-ai[rag]"           # RAG 相关依赖
pip install "camel-ai[agentscope]"    # AgentScope 集成

环境变量配置:

# 创建环境变量文件
cp .env.example .env
# 编辑 .env,填入 API Key
export OPENAI_API_KEY='your-api-key'

# 可选:开启日志记录
export CAMEL_MODEL_LOG_ENABLED=true
export CAMEL_LOG_DIR=camel_logs

核心用法

基本单 Agent 使用

from camel.models import ModelFactory
from camel.types import ModelPlatformType, ModelType
from camel.agents import ChatAgent
from camel.toolkits import SearchToolkit

# 创建模型(支持 OpenAI/Anthropic 等多种平台)
model = ModelFactory.create(
    model_platform=ModelPlatformType.OPENAI,
    model_type=ModelType.GPT_4O,
    model_config_dict={"temperature": 0.0},
)

# 创建 Agent
search_tool = SearchToolkit().search_duckduckgo
agent = ChatAgent(model=model, tools=[search_tool])

# 对话
response = agent.step("What is CAMEL-AI?")
print(response.msgs[0].content)

多 Agent 角色扮演(Role Playing)

CAMEL 的核心功能——两个 Agent 分别扮演不同角色协作完成任务:

from camel.societies import RolePlaying
from camel.models import ModelFactory
from camel.types import ModelPlatformType, ModelType

model = ModelFactory.create(
    model_platform=ModelPlatformType.OPENAI,
    model_type=ModelType.GPT_4O,
)

# 定义角色:程序员 + 股票交易员协作写量化交易 Bot
role_play = RolePlaying(
    assistant_role_name="Python Programmer",
    assistant_model=model,
    user_role_name="Stock Trader",
    user_model=model,
)

# 启动对话
chat_result = role_play.chat(
    task_prompt="Develop a trading bot for the stock market.",
    round_limit=10,
)

print(chat_result.msgs[-1].content)

RAG(检索增强生成)

from camel.embeddings import OpenAIEmbedding
from camel.types import EmbeddingModelType
from camel.storages import QdrantStorage
from camel.retrievers import VectorRetriever

# 1. 初始化 embedding
embedding = OpenAIEmbedding(
    model_type=EmbeddingModelType.TEXT_EMBEDDING_3_LARGE
)

# 2. 向量存储(Qdrant)
storage = QdrantStorage(
    vector_dim=embedding.get_output_dim(),
    path="local_data",
    collection_name="my_collection",
)

# 3. 构建检索器
retriever = VectorRetriever(
    embedding_model=embedding,
    storage=storage,
)

# 4. 处理文档
retriever.process(content="path/to/document.pdf")

# 5. 查询
results = retriever.query(
    query="your question here",
    top_k=3,
    similarity_threshold=0.75,
)

MCP(Model Context Protocol)集成

from camel.agents import ChatAgent
from camel.mcp import MCPToolkit

# 连接 MCP 服务器
mcp_toolkit = MCPToolkit(server_config="path/to/mcp_config.json")
agent = ChatAgent(model=model, tools=mcp_toolkit.get_tools())

工具集成(ToolKit)

CAMEL 内置多种工具包:

from camel.toolkits import (
    SearchToolkit,      # DuckDuckGo 搜索
    BrowserToolkit,     # 网页浏览
    FunctionToolkit,    # 自定义函数工具
    WebSearchToolkit,   # 通用网页搜索
)

典型适用场景

  • 多 Agent 协作研究:模拟两个 Agent(如"产品经理"+"工程师")对话,研究协作涌现行为
  • 大规模 Agent 仿真:学术研究需要模拟千/百万级 Agent(如社交网络模拟、市场博弈)
  • Synthetic Data 生成:用多 Agent 对话自动生成高质量训练数据
  • Chain-of-Thought 数据构建:通过 CAMEL 的 CoT 数据生成器构建推理轨迹数据
  • RAG 系统构建:用 CAMEL 的 Retriever 模块快速搭检索增强系统
  • 工具调用研究:集成各种外部工具(MCP/Composio)研究 Agent 工具使用能力

坑与注意

  1. 主要面向研究和高级用户:API 文档相对完善,但上手需要一定 Python 基础和 LLM Agent 概念
  2. 默认依赖 OpenAI:代码示例默认用 OpenAI,若用其他模型需查文档确认兼容性
  3. 大规模 Agent 模拟硬件要求高:100 万 Agent 的说法是理论上限,实际需要大量内存和计算资源
  4. 版本迭代快:CAMEL 仍在活跃开发中,API 可能随版本变化,生产环境使用前建议锁版本
  5. Cohort 类 Agent 概念多:Agent/Society/RolePlaying/Workforce 等概念层级多,初学者容易混淆
  6. Colab 依赖外部资源:部分 cookbook 在 Google Colab 中运行,涉及下载 arXiv 论文等大文件,可能较慢
  7. 中文资料有限:官方文档英文为主,社区中文资源较少

与同类对比

框架 Stars 定位 规模支持
CAMEL 17,334 ⭐ 多 Agent 研究 + 框架 100 万+ Agent
LangChain/LangGraph 50k+ ⭐ 通用 LLM 应用框架 单/小规模多 Agent
CrewAI 较高 ⭐ 多 Agent 编排,偏应用 中等规模
AutoGen (Microsoft) 高 ⭐ 多 Agent 对话框架 中等规模
AgentScope 较低 ⭐ 国产多 Agent 框架 大规模仿真

CAMEL 的核心差异: 不是"帮你搭应用"的框架,而是"研究多 Agent 行为和缩放定律"的平台——如果你想研究"1000 个 Agent 会发生什么",CAMEL 是最直接的选择;如果你想快速搭一个多 Agent 应用,CrewAI 或 LangGraph 可能更合适。


一句话结论

CAMEL 是目前最纯粹的多 Agent 研究框架,适合想深入研究 Agent 协作、生成大规模合成数据、或探索 Agent 缩放定律的开发者——但如果你只是想快速搭一个多 Agent 应用,LangGraph 或 CrewAI 的门槛更低。