多供应商路由

niuma_code 同时支持多个 API 供应商。在 factories 中配置不同端点后,请求会根据所选模型自动路由到正确的供应商。


配置

供应商配置位于 settings.json 中:

{
    "factories": [
        {
            "base_url": "https://api.anthropic.com",
            "api_key": "sk-ant-key1",
            "options": ["claude-sonnet-4-6", "claude-opus-4-8"]
        },
        {
            "base_url": "https://openrouter.ai/api",
            "api_key": "sk-or-key2",
            "options": ["gpt-4o", "gemini-2.5-pro"]
        }
    ],
    "llm": {
        "default_model": "claude-sonnet-4-6",
        "default_effort": "medium",
        "thinking_budget_low": "0",
        "thinking_budget_medium": "0",
        "thinking_budget_high": "10000",
        "thinking_budget_max": "20000"
    }
}

Factory 字段

字段 必填 说明
base_url API 端点 URL(支持 Anthropic 兼容代理)
api_key 供应商认证密钥
options 此供应商可用的模型名称列表

LLM 块

字段 必填 默认值 说明
default_model claude-sonnet-4-6 会话启动时使用的模型
default_effort medium 思考深度:lowmediumhighmax
thinking_budget_low "0" effort=low 时的 token 预算
thinking_budget_medium "0" effort=medium 时的 token 预算
thinking_budget_high "10000" effort=high 时的 token 预算
thinking_budget_max "20000" effort=max 时的 token 预算

路由机制

你输入:/model
  → TUI:弹出模型设置浮层,显示所有供应商及其模型
  → CLI:列出所有供应商及模型名称
  → 你从列表中选择模型
  → Config.get_current_model() 切换会话模型
  → get_factory_for_model() 路由到匹配供应商的 base_url + api_key
  → 立即切换,无需重启

列表中的第一个供应商为默认供应商。切换瞬时生效,会触发 prompt cache 失效警告。


切换模型

通过 /model 命令

/model    → 打开模型选择浮层(TUI)或列出所有供应商(CLI)

/model 命令在 TUI 模式下打开可视化的模型选择浮层。在 CLI 模式下显示当前模型和所有供应商的编号模型列表。

选择新模型会更新会话级 current_model——后续所有 LLM 请求使用新模型及其对应的供应商端点。

Effort 控制

Effort 控制思考深度,不切换模型。会话中可随时切换:

Effort 思考预算 适用场景
low 0 tokens 快速响应、简单问答
medium 0 tokens 一般编程任务(默认)
high 10,000 tokens 复杂重构、架构设计
max 20,000 tokens 深度分析、多步骤推理

可在 settings.jsonllm 块中配置各 effort 级别的 token 预算。


配置文件选择(单一来源)

factories/llm/env/compact/memory_quality 等主配置采用单一来源(不叠加):

模式 配置路径 触发条件
开发模式 <项目>/.niuma/settings.json CWD 包含 "niuma" 且有 app/ + app/core/ 子目录
部署模式 ~/.niuma/settings.json 其他情况

没有 local.json 层,没有三层合并。每个模式只读取一个文件——"在哪跑就用哪份配置"。

例外——memory_palace:此部分使用两层覆盖(项目级优先 → 用户级回退),独立于主配置。若项目级 settings.json 中存在 memory_palace 块则使用,否则回退到 ~/.niuma/settings.json

// 开发模式:.niuma/settings.json
// (CWD 有 app/ + app/core/ → 仅项目级)
{
    "factories": [
        { "base_url": "https://api.anthropic.com", "api_key": "...", "options": ["claude-sonnet-4-6"] }
    ]
}
// 部署模式:~/.niuma/settings.json
// (其他 CWD → 仅用户级)
{
    "factories": [
        { "base_url": "https://custom-proxy.com", "api_key": "...", "options": ["claude-sonnet-4-6"] }
    ]
}

添加新供应商

  1. 从供应商获取 API 密钥
  2. settings.json 添加新条目:
{
    "factories": [
        { "base_url": "https://api.anthropic.com", "api_key": "sk-ant-...", "options": ["claude-sonnet-4-6", "claude-opus-4-8"] },
        { "base_url": "https://openrouter.ai/api", "api_key": "sk-or-...", "options": ["gpt-4o", "gemini-2.5-pro"] },
        { "base_url": "https://api.deepseek.com", "api_key": "sk-ds-...", "options": ["deepseek-chat", "deepseek-coder"] }
    ]
}
  1. 使用 /model 打开选择浮层并切换到新模型

常见供应商配置

Anthropic(直连或代理)

{
    "base_url": "https://api.anthropic.com",
    "api_key": "sk-ant-...",
    "options": ["claude-sonnet-4-6", "claude-opus-4-8"]
}

OpenRouter(多模型)

{
    "base_url": "https://openrouter.ai/api",
    "api_key": "sk-or-...",
    "options": ["gpt-4o", "gemini-2.5-pro", "llama-3.1-405b"]
}

本地自托管

{
    "base_url": "http://localhost:11434/v1",
    "api_key": "not-needed",
    "options": ["llama3.1", "codellama"]
}

故障排除

问题 解决方案
模型未在选择列表中找到 检查匹配供应商 options 列表中的模型名是否精确匹配
401 未授权 确认该供应商的 api_key 正确
连接被拒 检查 base_url 和网络访问
响应缓慢 尝试其他供应商或区域端点
模型切换未生效 确保模型名精确匹配(区分大小写)
切换后 prompt cache 警告 正常现象——切换模型会使 prompt cache 失效