Skip to content

Providers 与模型策略

目标

让模型选择从"凭感觉"变成"按任务类型分层配置"。

参考:

Provider 配置

基础配置

yaml
providers:
  # OpenAI
  openai:
    api_key: ${secrets.openai_api_key}
    base_url: https://api.openai.com/v1 # 可选,用于代理
    models:
      gpt-4-turbo:
        enabled: true
        cost_limit: 100 # 美元/月
      gpt-4:
        enabled: true
        cost_limit: 50
      gpt-3.5-turbo:
        enabled: true
        cost_limit: 20

  # Anthropic
  anthropic:
    api_key: ${secrets.anthropic_api_key}
    models:
      claude-3-opus:
        enabled: true
        cost_limit: 200
      claude-3-sonnet:
        enabled: true
        cost_limit: 100

  # Google
  google:
    api_key: ${secrets.google_api_key}
    models:
      gemini-pro:
        enabled: true
        cost_limit: 50

  # 本地模型 (Ollama)
  ollama:
    base_url: http://localhost:11434
    models:
      llama2:
        enabled: true
      codellama:
        enabled: true

模型路由配置

yaml
# 按任务类型路由
model_routing:
  # 默认模型
  default: gpt-4-turbo

  # 按任务类型
  by_task:
    coding:
      primary: claude-3-opus
      fallback: gpt-4-turbo

    analysis:
      primary: gpt-4-turbo
      fallback: gpt-4

    chat:
      primary: gpt-3.5-turbo
      fallback: gpt-4-turbo

    summary:
      primary: gpt-3.5-turbo
      fallback: claude-3-sonnet

  # 按渠道路由
  by_channel:
    discord:
      model: gpt-4-turbo
    telegram:
      model: claude-3-sonnet

推荐配置思路

1. 默认模型

兼顾质量与成本,用于日常任务:

yaml
agents:
  defaults:
    model: gpt-4-turbo
    temperature: 0.7
    max_tokens: 4096

2. 高质量模型

用于复杂编码、架构设计、关键审查:

yaml
agents:
  routing:
    - name: code-review
      trigger: 'review:'
      model: claude-3-opus
      temperature: 0.3

    - name: architecture
      trigger: 'arch:'
      model: claude-3-opus

3. 低成本模型

用于分类、改写、摘要类批处理:

yaml
agents:
  routing:
    - name: classify
      trigger: 'classify:'
      model: gpt-3.5-turbo

    - name: summarize
      trigger: 'summarize:'
      model: gpt-3.5-turbo

模型能力对比

模型优势劣势推荐场景
GPT-4 Turbo综合能力强、稳定成本高复杂任务、关键决策
GPT-3.5 Turbo速度快、成本低推理能力有限简单任务、批量处理
Claude 3 Opus编码能力强、长上下文成本最高代码审查、架构设计
Claude 3 Sonnet平衡质量与成本-日常任务、对话
Gemini Pro多模态、成本低-图像理解、简单任务
Llama 2 (本地)完全私有、无成本能力有限隐私敏感、离线场景

Failover 配置

yaml
failover:
  # 主模型失败时降级
  strategy: cascade # cascade | random | round_robin

  rules:
    - from: gpt-4-turbo
      to: gpt-4
      conditions:
        - timeout
        - rate_limit
        - error

    - from: gpt-4
      to: gpt-3.5-turbo
      conditions:
        - timeout
        - rate_limit

    - from: claude-3-opus
      to: claude-3-sonnet
      conditions:
        - timeout
        - error

  # 失败记录
  log_failures: true

  # 自动恢复检查
  recovery_check:
    interval: 60 # 秒
    retries: 3

成本控制

预算配置

yaml
budget:
  daily: 50 # 美元/天
  weekly: 200 # 美元/周
  monthly: 800 # 美元/月

  # 预算告警
  alerts:
    - threshold: 50%
      action: notify
    - threshold: 80%
      action: notify + throttle
    - threshold: 100%
      action: notify + disable

  # 按模型预算
  by_model:
    gpt-4-turbo:
      daily: 30
    claude-3-opus:
      daily: 20

使用统计

bash
# 查看使用统计
openclaw stats usage

# 查看成本报告
openclaw stats cost --period day
openclaw stats cost --period week
openclaw stats cost --period month

# 按模型查看
openclaw stats cost --by model

# 按渠道查看
openclaw stats cost --by channel

# 导出报告
openclaw stats export --output usage-report.csv

最小可维护策略

  1. 先定义 agents.defaults.model.primary
  2. 再按 agent 场景做差异化配置
  3. 对高成本任务启用触发条件(避免滥用)

策略示例

txt
# 阶段 1:基础配置
agents:
  defaults:
    model: gpt-4-turbo

# 阶段 2:场景分化
agents:
  routing:
    - name: dev-agent
      trigger: "dev:"
      model: claude-3-opus
      tools: coding

    - name: ops-agent
      trigger: "ops:"
      model: gpt-4-turbo
      tools: minimal

    - name: pm-agent
      trigger: "pm:"
      model: gpt-3.5-turbo
      tools: minimal

# 阶段 3:成本控制
agents:
  routing:
    - name: expensive-task
      trigger: "analysis:"
      model: claude-3-opus
      conditions:
        - require_confirmation: true
        - daily_limit: 10

成本与质量协同

开发者负责

  • 调整技术参数与 fallback 策略
  • 监控模型响应时间和错误率
  • 优化提示词减少 token 消耗

产品经理负责

  • 定义任务 SLA 与预算上限
  • 决定"哪些任务必须高质量模型"
  • 制定成本告警和响应策略