资讯动态

CAMEL 多智能体框架环境配置实战:API 密钥、模型提供商与本地模型接入

发布时间:2026/9/13 17:42:04 来源:尧图企业网站定制
CAMEL 多智能体框架环境配置实战API 密钥、模型提供商与本地模型接入【免费下载链接】camel CAMEL: The first and the best multi-agent framework. Finding the Scaling Law of Agents. https://www.camel-ai.org项目地址: https://gitcode.com/GitHub_Trending/ca/camelCAMEL-AIcamel-ai是一个以多智能体协作为核心的开源框架运行任何 Agent 之前你都需要先完成最关键的一步配置 API 凭据并选择模型提供商。本篇指南以 docs/get_started/setup.md 为骨架结合仓库源码如 camel/models/openai_model.py、camel/types/enums.py、camel/models/model_factory.py展开讲解。读完本文你将掌握OpenAI 等托管 API 的三种密钥配置方式、API_BASE_URL代理/中转配置、DEFAULT_MODEL_PLATFORM_TYPE与DEFAULT_MODEL_TYPE默认模型机制、OpenAI 兼容接口的接入方法以及如何在本机用 Ollama、vLLM、SGLang 跑开源模型。1. 环境配置概览CAMEL 支持哪些模型后端CAMEL 采用多模型后端 统一接口的设计所有模型提供商在 camel/types/enums.py 中被统一抽象为ModelPlatformType枚举每个平台对应一个独立的模型实现类最终由ModelFactory统一创建。从源码camel/types/enums.py#L1972-L2022可以看到当前框架支持的后端包括直接集成OpenAI、Azure OpenAI、AnthropicClaude、Gemini、Mistral、DeepSeek、Qwen、ZhipuAIGLM、MoonshotKimi、xAIGrok、Cohere、InternLM、Reka、MiniMax 等API/连接器平台GROQ、OpenRouter、Together AI、SambaNova、Ollama、LiteLLM、LMStudio、vLLM、SGLang、NVIDIA、ModelScope、SiliconFlow、AWS Bedrock、IBM WatsonX、Qianfan、Volcano、AIHubMix 等兼容通道OPENAI_COMPATIBLE_MODEL可接入任意暴露 OpenAI 兼容 API 的服务。配置这些后端的方式高度统一设置环境变量API Key、Base URL 通过ModelFactory.create在代码中指定平台与模型。下文以最常见的 OpenAI 为例完整演示。2. 前置准备Python 版本与安装配置密钥前请确认 Python 版本。CAMEL-AI 要求Python 3.10 且 3.14可先检查python3 --version随后安装 CAMEL 核心库更完整的安装方式、extras 可选依赖与源码安装请参考 docs/get_started/installation.mdpip install camel-ai # 或安装全部功能依赖推荐含 RAG、存储、Web 工具等 pip install camel-ai[all]3. 配置 OpenAI API 密钥三种主流方式setup.md给出了三种配置OPENAI_API_KEY的方式适用于不同开发场景。密钥本身可在 OpenAI 控制台的 API Keys 页面生成。3.1 UnixmacOS/Linux写入 shell 配置文件echo export OPENAI_API_KEYYOUR_KEY ~/.zshrc source ~/.zshrc如果你使用 bash请将~/.zshrc替换为~/.bashrc。这种方式把密钥固化进 shell 启动脚本每次打开新终端自动生效适合个人开发机。3.2 Windows永久环境变量setx OPENAI_API_KEY YOUR_KEY /Msetx写入的是系统级永久环境变量修改后需要重启终端部分情况需重启资源管理器或系统才会生效。3.3 项目级.env文件推荐用于多项目隔离在项目根目录创建.env文件OPENAI_API_KEYYOUR_KEY然后在 Python 中加载from dotenv import load_dotenv load_dotenv() import os print(os.getenv(OPENAI_API_KEY))python-dotenv是社区标准方案CAMEL 的 pyproject.toml 中owl等 extras 也显式声明了python-dotenv依赖。它避免把密钥写进 shell 配置也便于把.env加入.gitignore防止误提交。如果希望.env覆盖已存在的同名环境变量可调用load_dotenv(overrideTrue)。源码证据无论采用哪种方式最终生效的都是进程环境变量。在 camel/models/openai_model.py#L125-L149 中OpenAIModel.__init__通过api_key api_key or os.environ.get(OPENAI_API_KEY)读取环境变量也就是说显式传入的api_key参数优先未传入时才回退到环境变量。4. 配置 API Base URL代理、中转与 Azure如果你的服务不直连 OpenAI 官方端点例如使用 Azure OpenAI、企业代理或国内中转服务需要额外设置OPENAI_API_BASE_URLexport OPENAI_API_BASE_URLYOUR_BASE_URL在源码层面该变量在 camel/models/openai_model.py#L149 被读取url url or os.environ.get(OPENAI_API_BASE_URL)并最终作为 OpenAI 客户端的base_url传入。因此显式传url参数 环境变量 官方默认端点优先级清晰可预测。除了OPENAI_API_KEY与OPENAI_API_BASE_URLCAMEL 还支持MODEL_TIMEOUT环境变量控制请求超时默认 180 秒见 camel/models/openai_model.py#L150在弱网或长推理场景下可适当调大。5. 默认模型机制DEFAULT_MODEL_PLATFORM_TYPE 与 DEFAULT_MODEL_TYPECAMEL 提供两个全局默认值环境变量让你不必在每次创建 Agent 时都手动指定模型export DEFAULT_MODEL_PLATFORM_TYPEopenai # 例如 openai、anthropic、gemini ... export DEFAULT_MODEL_TYPEgpt-4o-mini # 例如 gpt-3.5-turbo、gpt-4o-mini ...在源码中camel/types/enums.py#L34 与 camel/types/enums.py#L1973ModelType.DEFAULT os.getenv(DEFAULT_MODEL_TYPE, gpt-4.1-mini-2025-04-14)ModelPlatformType.DEFAULT os.getenv(DEFAULT_MODEL_PLATFORM_TYPE, openai)。即未设置时平台默认openai模型默认gpt-4.1-mini-2025-04-14不同版本仓库的默认值可能略有差异请以当前仓库源码为准。这在多 Agent 场景中非常有用——例如 examples/ai_society/role_playing.py 中的RolePlaying会把同一个model传给 assistant、user 和 task_specify 三个 Agent配合默认模型机制可大幅减少重复配置。6. 其他托管 API用 ModelFactory 统一接入对于非 OpenAI 提供商Anthropic、Gemini、Mistral、DeepSeek、Qwen、Moonshot 等setup.md指引查看 docs/key_modules/models.md该文档给出了完整的平台与模型清单。这里以代码方式说明通用接入模式——所有平台都走同一个ModelFactory.createfrom camel.models import ModelFactory from camel.types import ModelPlatformType, ModelType from camel.configs import ChatGPTConfig from camel.agents import ChatAgent model ModelFactory.create( model_platformModelPlatformType.OPENAI, model_typeModelType.GPT_4O_MINI, model_config_dictChatGPTConfig(temperature0.2).as_dict(), ) agent ChatAgent( system_messageYou are a helpful assistant., modelmodel, ) response agent.step(Say hi to CAMEL AI community.) print(response.msg.content)把model_platform换成ModelPlatformType.GEMINI、ModelPlatformType.ANTHROPIC、ModelPlatformType.DEEPSEEK等并配套对应平台的 Config 类与 API Key 环境变量即可无缝切换后端。从 camel/models/model_factory.py#L78-L128 的映射表可以看到ModelFactory._MODEL_PLATFORM_TO_CLASS_MAP把每个ModelPlatformType映射到独立的模型实现类如OpenAIModel、AnthropicModel、GeminiModel、DeepSeekModel等创建时还会把字符串形式的平台/模型名自动转换为枚举camel/models/model_factory.py#L193-L209因此你也可以直接传字符串模型名。OpenAI 兼容通道如果你的服务暴露 OpenAI 兼容 API包括 OpenRouter 等聚合平台无需单独实现直接用OPENAI_COMPATIBLE_MODEL指定自定义端点与模型名即可import os from camel.agents import ChatAgent from camel.models import ModelFactory from camel.types import ModelPlatformType model ModelFactory.create( model_platformModelPlatformType.OPENAI_COMPATIBLE_MODEL, model_typeyour-model-name, # 例如 gpt-4o urlhttps://your-openai-compatible-endpoint/v1, api_keyos.getenv(OPENAI_COMPATIBLE_API_KEY), model_config_dict{temperature: 0.2}, ) agent ChatAgent( system_messageYou are a helpful assistant., modelmodel, ) response agent.step(Explain quantum computing in simple terms.) print(response.msg.content)url、api_key与model_type均按你的服务商提供的值替换即可请求模式与 OpenAI 完全一致。6.1 常用配置参数速查以 OpenAI 为例配置字典来自各平台的 Config 类。以ChatGPTConfigcamel/configs/openai_config.py#L23-L120为例常用参数包括参数说明典型取值temperature采样温度0~2越低越确定0.2、0.0top_p核采样仅考虑累积概率为 top_p 的 token0.1~1.0max_tokens单次生成的最大 token 数视模型上下文而定stream是否流式返回True/Falsestop停止序列最多 4 个字符串或列表presence_penalty/frequency_penalty话题新鲜度/重复惩罚-2.0~2.00~1response_format结构化输出如{type: json_object}JSON 模式tools/tool_choice函数调用工具及选择策略auto、required等reasoning_effort推理强度o1/o3 等模型low/medium/highmedium这些参数通过.as_dict()传入ModelFactory.create最终透传给底层模型客户端。7. 本地开源模型完全离线运行setup.md指出希望在设备上完全离线运行开源模型时参考 docs/key_modules/models.md 中的本地模型指南。核心思路与托管 API 完全一致——用ModelFactory.create指向本机推理服务区别仅在于url指向localhost。7.1 通过 Ollama 运行 Llama 3# 安装 Ollama 后拉取模型 ollama pull llama3接入 CAMELfrom camel.agents import ChatAgent from camel.models import ModelFactory from camel.types import ModelPlatformType ollama_model ModelFactory.create( model_platformModelPlatformType.OLLAMA, model_typellama3, urlhttp://localhost:11434/v1, model_config_dict{temperature: 0.4}, ) agent ChatAgent(You are a helpful assistant., modelollama_model) response agent.step(Say hi to CAMEL) print(response.msg.content)7.2 通过 vLLM 运行 Phi-3 等模型python -m vllm.entrypoints.openai.api_server \ --model microsoft/Phi-3-mini-4k-instruct \ --api-key vllm --dtype bfloat16from camel.agents import ChatAgent from camel.models import ModelFactory from camel.types import ModelPlatformType vllm_model ModelFactory.create( model_platformModelPlatformType.VLLM, model_typemicrosoft/Phi-3-mini-4k-instruct, urlhttp://localhost:8000/v1, model_config_dict{temperature: 0.0}, ) agent ChatAgent(You are a helpful assistant., modelvllm_model) response agent.step(Say hi to CAMEL AI) print(response.msg.content)7.3 通过 SGLang 运行 Meta-Llamafrom camel.agents import ChatAgent from camel.models import ModelFactory from camel.types import ModelPlatformType sglang_model ModelFactory.create( model_platformModelPlatformType.SGLANG, model_typemeta-llama/Llama-3.2-1B-Instruct, model_config_dict{temperature: 0.0}, api_keysglang, ) agent ChatAgent(You are a helpful assistant., modelsglang_model) response agent.step(Say hi to CAMEL AI) print(response.msg.content)三条路径的共同点model_platform换成OLLAMA/VLLM/SGLANGurl指向本地推理服务地址本地模型同样支持工具调用、多 Agent 协作等全部上层能力适合原型验证与数据隐私敏感场景。8. 验证配置从安装检查到跑通第一个 Agent配置完成后按以下顺序验证环境是否就绪第一步确认安装pip show camel-ai第二步用最简代码验证密钥读取import os print(os.getenv(OPENAI_API_KEY)) # 应能打印出你配置的密钥 print(os.getenv(OPENAI_API_BASE_URL)) # 如配置了代理端点这里应可见第三步运行仓库自带的官方示例示例目录见 examples/# 双 Agent 角色扮演协作 python examples/ai_society/role_playing.py # Agent 使用代码执行工具 python examples/toolkits/code_execution_toolkit.py # 生成知识图谱 python examples/knowledge_graph/knowledge_graph_agent_example.py # 多 Agent 协作处理复杂任务 python examples/workforce/multiple_single_agents.py # 创意图像生成 python examples/vision/image_crafting.py例如 examples/ai_society/role_playing.py 会构建一个 Python Programmer 与 Stock Trader 的角色扮演会话任务为 Develop a trading bot for the stock market跑通即代表环境配置成功。第四步可选运行测试套件pytest --fast-test-mode test/ # 运行全部测试 pytest -v apps/ # 指定类别9. 常见问题与排查建议密钥未生效 / 401 认证失败优先检查环境变量是否在当前终端进程可见echo $OPENAI_API_KEY使用setx后必须重启终端使用.env时确认load_dotenv()已执行且路径正确必要时改用load_dotenv(overrideTrue)。需要代理端点却直连官方 API确认设置了OPENAI_API_BASE_URL并注意它需要包含/v1等路径前缀取决于服务商。Python 3.13 兼容性unstructured、pyobvector等包在 Python 3.13 上不可用它们依赖 NumPy 2.0与 3.13 不兼容。需要这些功能的场景建议使用 Python 3.10~3.12其余功能在 3.13 上可正常工作。该约束同时体现在 pyproject.toml 的依赖标记中例如unstructured0.16.20; python_version 3.13。请求超时长任务场景通过MODEL_TIMEOUT环境变量调大超时阈值或在ModelFactory.create中显式传timeout与max_retries默认重试 3 次。想快速切换多家模型善用DEFAULT_MODEL_PLATFORM_TYPE/DEFAULT_MODEL_TYPE两个默认变量或直接在ModelFactory.create中传字符串模型名源码会自动完成字符串到枚举的转换无需改动 Agent 代码。完成以上配置后即可进入 docs/key_modules/models.md 探索各平台的完整模型清单或参考 docs/cookbooks/ 中的实战案例开始搭建你的第一个多智能体系统。【免费下载链接】camel CAMEL: The first and the best multi-agent framework. Finding the Scaling Law of Agents. https://www.camel-ai.org项目地址: https://gitcode.com/GitHub_Trending/ca/camel创作声明:本文部分内容由AI辅助生成(AIGC),仅供参考

读完文章,也想定制专属网站?

尧图设计师 24 小时内与您沟通定制方案

免费获取报价