ANALYSIS
OpenClaw 模型配置完整指南
概述
OpenClaw 支持多种大语言模型,包括本地模型(Ollama)、云端模型(OpenAI、Claude、国产模型等)。本文档详细介绍如何配置各种模型,帮助你根据需求选择最适合的模型方案。
模型类型概述
| 模型类型 | 优点 | 缺点 | 适用场景 |
|---|---|---|---|
| 本地模型 | 隐私安全、无网络依赖、成本低 | 需要硬件支持、性能较弱 | 隐私敏感、离线使用、成本控制 |
| 云端模型 | 性能强大、更新及时、功能丰富 | 需要网络、产生费用、数据上传 | 复杂任务、高性能需求、最新功能 |
| 混合模式 | 兼顾性能和成本 | 配置复杂 | 生产环境、成本优化 |
本地模型配置(Ollama)
安装 Ollama
Windows
PRTCL // POWERSHELL
# 下载并安装 Ollama# 访问 https://ollama.ai/download 下载 Windows 版本# 运行安装程序
# 验证安装ollama --versionmacOS
PRTCL // BASH
# 使用 Homebrew 安装brew install ollama
# 验证安装ollama --versionLinux
PRTCL // BASH
# 安装 Ollamacurl -fsSL https://ollama.ai/install.sh | sh
# 验证安装ollama --version下载模型
PRTCL // BASH
# 下载常用模型ollama pull qwen2.5:7b # 通义千问 7Bollama pull llama3.2:3b # Llama 3.2 3Bollama pull glm4:9b # 智谱 GLM-4 9Bollama pull deepseek-coder:6.6b # DeepSeek Coder 6.6B
# 查看已下载的模型ollama list
# 删除模型ollama rm <model-name>配置 OpenClaw 使用本地模型
方法一:命令行配置
PRTCL // BASH
# 添加本地模型提供商openclaw model provider add ollama
# 设置默认模型openclaw model default ollama/qwen2.5:7b
# 测试模型连接openclaw model test ollama/qwen2.5:7b方法二:配置文件
PRTCL // YAML
providers: - name: "ollama" type: "local" baseUrl: "http://localhost:11434" api: "openai-completions" models: - id: "ollama/qwen2.5:7b" name: "通义千问 7B" capabilities: - "text" - "code" - id: "ollama/llama3.2:3b" name: "Llama 3.2 3B" capabilities: - "text" - id: "ollama/glm4:9b" name: "智谱 GLM-4 9B" capabilities: - "text" - "code" - "tool"
defaultModel: "ollama/qwen2.5:7b"硬件要求
| 模型大小 | 显存需求 | 内存需求 | 推荐配置 |
|---|---|---|---|
| 3B | 4GB | 8GB | 入门级 |
| 7B | 8GB | 16GB | 中端 |
| 14B | 16GB | 32GB | 高端 |
| 32B | 32GB | 64GB | 专业级 |
性能优化
PRTCL // BASH
# 启用 GPU 加速(如果有 NVIDIA GPU)# 设置环境变量export CUDA_VISIBLE_DEVICES=0
# 使用量化模型ollama pull qwen2.5:7b-q4_K_M
# 调整上下文长度# 在配置文件中设置context: maxTokens: 4096云端模型配置
OpenAI 模型
获取 API Key
- 访问 https://platform.openai.com/api-keys
- 登录或注册账户
- 创建新的 API Key
- 保存 API Key(只显示一次)
配置 OpenClaw
PRTCL // YAML
providers: - name: "openai" type: "openai" baseUrl: "https://api.openai.com/v1" apiKey: "${OPENAI_API_KEY}" # 使用环境变量 models: - id: "gpt-4o" name: "GPT-4o" capabilities: - "text" - "image" - "audio" - "code" - "tool" - id: "gpt-4o-mini" name: "GPT-4o Mini" capabilities: - "text" - "code" - "tool" - id: "o1-preview" name: "o1 Preview" capabilities: - "text" - "reasoning"使用环境变量
PRTCL // BASH
# 设置 API Keyexport OPENAI_API_KEY="sk-xxxxxxxxxxxxxxxxxxxxxxxx"
# 或在配置文件中引用Anthropic Claude 模型
获取 API Key
- 访问 https://console.anthropic.com/
- 登录或注册账户
- 进入 API Keys 页面
- 创建新的 API Key
- 保存 API Key
配置 OpenClaw
PRTCL // YAML
providers: - name: "anthropic" type: "anthropic" baseUrl: "https://api.anthropic.com" apiKey: "${ANTHROPIC_API_KEY}" models: - id: "claude-opus-4-20250514" name: "Claude Opus 4" capabilities: - "text" - "code" - "tool" - "vision" - id: "claude-sonnet-4-20250514" name: "Claude Sonnet 4" capabilities: - "text" - "code" - "tool" - "vision" - id: "claude-haiku-4-20250514" name: "Claude Haiku 4" capabilities: - "text" - "code"国产模型配置
阿里云百炼
PRTCL // YAML
providers: - name: "qwen" type: "openai" baseUrl: "https://dashscope.aliyuncs.com/compatible-mode/v1" apiKey: "${DASHSCOPE_API_KEY}" models: - id: "qwen-max" name: "通义千问 Max" capabilities: - "text" - "code" - "tool" - id: "qwen-plus" name: "通义千问 Plus" capabilities: - "text" - "code" - id: "qwen-turbo" name: "通义千问 Turbo" capabilities: - "text"智谱 AI
PRTCL // YAML
providers: - name: "zhipu" type: "openai" baseUrl: "https://open.bigmodel.cn/api/paas/v4" apiKey: "${ZHIPU_API_KEY}" models: - id: "glm-4" name: "GLM-4" capabilities: - "text" - "code" - "tool" - id: "glm-4-flash" name: "GLM-4 Flash" capabilities: - "text" - "code"DeepSeek
PRTCL // YAML
providers: - name: "deepseek" type: "openai" baseUrl: "https://api.deepseek.com" apiKey: "${DEEPSEEK_API_KEY}" models: - id: "deepseek-chat" name: "DeepSeek Chat" capabilities: - "text" - "code" - id: "deepseek-coder" name: "DeepSeek Coder" capabilities: - "code"月之暗面 Kimi
PRTCL // YAML
providers: - name: "kimi" type: "openai" baseUrl: "https://api.moonshot.cn/v1" apiKey: "${KIMI_API_KEY}" models: - id: "moonshot-v1-128k" name: "Kimi 128K" capabilities: - "text" - "code" - id: "moonshot-v1-32k" name: "Kimi 32K" capabilities: - "text" - "code"自定义模型提供商
通用配置模板
如果使用第三方兼容 OpenAI/Anthropic 协议的模型,可以使用以下模板:
PRTCL // YAML
providers: - name: "custom_provider" type: "openai" # 或 "anthropic" baseUrl: "https://api.custom-provider.com/v1" apiKey: "${CUSTOM_API_KEY}" models: - id: "custom-model-id" name: "Custom Model Name" capabilities: - "text" - "code"示例:硅基流动
PRTCL // YAML
providers: - name: "siliconflow" type: "openai" baseUrl: "https://api.siliconflow.cn/v1" apiKey: "${SILICONFLOW_API_KEY}" models: - id: "deepseek-ai/DeepSeek-V3" name: "DeepSeek V3" capabilities: - "text" - "code" - id: "Qwen/Qwen2.5-72B-Instruct" name: "Qwen2.5 72B" capabilities: - "text" - "code"示例:OpenRouter
PRTCL // YAML
providers: - name: "openrouter" type: "openai" baseUrl: "https://openrouter.ai/api/v1" apiKey: "${OPENROUTER_API_KEY}" models: - id: "anthropic/claude-3.5-sonnet" name: "Claude 3.5 Sonnet" capabilities: - "text" - "code" - id: "google/gemini-pro-1.5" name: "Gemini Pro 1.5" capabilities: - "text" - "code"模型路由策略
基于任务类型的路由
PRTCL // YAML
router: enabled: true rules: - condition: "task.type == 'coding'" model: "gpt-4o" priority: 10
- condition: "task.type == 'writing'" model: "claude-opus-4" priority: 9
- condition: "task.type == 'analysis'" model: "qwen-max" priority: 8
- condition: "task.complexity < 3" model: "gpt-4o-mini" priority: 7
- default: "gpt-4o-mini"基于成本的路由
PRTCL // YAML
router: enabled: true strategy: "cost" rules: - condition: "budget.available < 10" model: "ollama/qwen2.5:7b" priority: 10
- condition: "budget.available >= 10" model: "gpt-4o-mini" priority: 5
- condition: "budget.available >= 50" model: "gpt-4o" priority: 3基于性能的路由
PRTCL // YAML
router: enabled: true strategy: "performance" rules: - condition: "task.urgency == 'high'" model: "gpt-4o" priority: 10
- condition: "task.urgency == 'medium'" model: "claude-sonnet-4" priority: 7
- condition: "task.urgency == 'low'" model: "ollama/llama3.2:3b" priority: 3多模态模型配置
GPT-4o Vision
PRTCL // YAML
providers: - name: "openai" models: - id: "gpt-4o" name: "GPT-4o" capabilities: - "text" - "image" - "audio" - "video" vision: enabled: true maxImageSize: 10485760 # 10MB supportedFormats: - "png" - "jpeg" - "gif" - "webp"Claude Vision
PRTCL // YAML
providers: - name: "anthropic" models: - id: "claude-opus-4" name: "Claude Opus 4" capabilities: - "text" - "vision" vision: enabled: true maxImageSize: 20971520 # 20MB supportedFormats: - "png" - "jpeg" - "gif" - "webp"国产多模态模型
PRTCL // YAML
providers: - name: "qwen" models: - id: "qwen-vl-max" name: "通义千问 VL Max" capabilities: - "text" - "vision" vision: enabled: true maxImageSize: 10485760参数调优
常用参数
| 参数 | 说明 | 默认值 | 推荐范围 |
|---|---|---|---|
| temperature | 温度参数,控制随机性 | 0.7 | 0.0-1.0 |
| max_tokens | 最大生成 Token 数 | 2000 | 100-8000 |
| top_p | 核采样概率 | 1.0 | 0.8-1.0 |
| frequency_penalty | 频率惩罚 | 0.0 | -2.0-2.0 |
| presence_penalty | 存在惩罚 | 0.0 | -2.0-2.0 |
不同场景的参数配置
创意写作
PRTCL // YAML
creative_writing: temperature: 0.9 top_p: 0.95 max_tokens: 4000 frequency_penalty: 0.5 presence_penalty: 0.3代码生成
PRTCL // YAML
code_generation: temperature: 0.2 top_p: 0.95 max_tokens: 2000 frequency_penalty: 0.0 presence_penalty: 0.0文档摘要
PRTCL // YAML
summarization: temperature: 0.3 top_p: 0.9 max_tokens: 1000 frequency_penalty: 0.1 presence_penalty: 0.1问答系统
PRTCL // YAML
qa_system: temperature: 0.1 top_p: 0.85 max_tokens: 1500 frequency_penalty: 0.0 presence_penalty: 0.0监控与优化
Token 使用监控
PRTCL // YAML
monitoring: tokenUsage: enabled: true alertThreshold: 10000 resetPeriod: "daily" report: true costTracking: enabled: true budget: daily: 10 monthly: 300 alerts: - level: "warning" threshold: 0.8 - level: "critical" threshold: 0.95性能监控
PRTCL // YAML
performance: metrics: - "response_time" - "throughput" - "error_rate" - "success_rate" logging: enabled: true level: "info"优化建议
- 使用缓存:对重复查询启用缓存
- 批量处理:将多个小请求合并处理
- 模型选择:根据任务复杂度选择合适的模型
- 上下文管理:合理控制上下文长度
- 并发控制:避免过度并发导致限流
故障排查
连接问题
症状:无法连接到模型 API
解决方案:
PRTCL // BASH
# 测试网络连接curl -I https://api.openai.com
# 检查 API Keyopenclaw model test <model-id>
# 查看详细日志openclaw logs -f --level=debug认证问题
症状:401 Unauthorized 错误
解决方案:
PRTCL // BASH
# 验证 API Keyecho $OPENAI_API_KEY
# 重新设置 API Keyexport OPENAI_API_KEY="new-key"
# 测试连接openclaw model test <model-id>限流问题
症状:429 Too Many Requests 错误
解决方案:
PRTCL // YAML
# 降低请求频率rateLimit: enabled: true requestsPerMinute: 60
# 使用多个 API Keyproviders: - name: "openai" apiKeys: - "${OPENAI_API_KEY_1}" - "${OPENAI_API_KEY_2}"本地模型性能问题
症状:本地模型响应缓慢
解决方案:
PRTCL // BASH
# 检查硬件资源nvidia-smi # GPU 使用情况free -h # 内存使用情况
# 使用量化模型ollama pull qwen2.5:7b-q4_K_M
# 减少上下文长度context: maxTokens: 2048最佳实践
安全建议
- 保护 API Key:使用环境变量或密钥管理服务
- 限制访问:配置适当的权限和访问控制
- 审计日志:记录所有模型调用
- 定期轮换:定期更换 API Key
成本控制
- 设置预算:配置每日 / 每月预算限制
- 使用缓存:避免重复调用
- 选择合适模型:根据任务选择性价比高的模型
- 监控使用:定期检查 Token 使用情况
性能优化
- 模型路由:根据任务类型自动选择模型
- 批量处理:合并相似请求
- 本地优先:简单任务使用本地模型
- 异步处理:耗时任务异步执行
资源链接
- OpenAI 文档: https://platform.openai.com/docs
- Anthropic 文档: https://docs.anthropic.com
- Ollama 文档: https://ollama.ai/docs
- 阿里云百炼: https://help.aliyun.com/zh/dashscope/
- 智谱 AI: https://open.bigmodel.cn/dev/api
- OpenClaw 模型文档: https://docs.openclaw.ai/models
最后更新: 2026-03-12 作者: EchoHaoRan
R P
Rhine Lab Pioneer Division
Auth_Verified: 2026.04.08
Auth_Verified: 2026.04.08
