Skip to content

Latest commit

 

History

History
353 lines (275 loc) · 9.13 KB

api-reference.md

File metadata and controls

353 lines (275 loc) · 9.13 KB

🔌 API 参考

Version


📋 基础信息

项目 值
Base URL http://127.0.0.1:10000
API Version v1
认证方式 Bearer Token (当 API_KEY 配置时必需)

🔑 认证

# 带认证
curl -H "Authorization: Bearer YOUR_API_KEY" ...

# 不带认证 (未配置 API_KEY 时)
curl ...

📡 端点

⚙️ 运行时并发调节

GET /admin/concurrency
POST /admin/concurrency

该接口始终需要配置非空 API_KEY,认证可使用 Bearer Token 或 x-api-key。POST body 可包含 pool(proxy、model、image、direct,默认 proxy)、environments/concurrency(1-1024)、max_queue(0-100000)和 queue_timeout_ms(0-600000);缩容会等待正在执行的任务自然结束。pool=model 可额外传入 model:指定时只调整该模型,省略时调整所有现有模型池、overflow 和未来默认值。模型队列上限与超时不会超过当前 proxy admission 设置。

curl -X POST http://127.0.0.1:10000/admin/concurrency \
  -H "Authorization: Bearer YOUR_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{"pool":"proxy","environments":4,"max_queue":128}'
curl -X POST http://127.0.0.1:10000/admin/concurrency \
  -H "x-api-key: YOUR_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{"pool":"model","model":"opencode/big-pickle","concurrency":2,"max_queue":8}'

✅ 健康检查

GET /health

/health 是 liveness 接口,只确认代理进程可响应 HTTP,因此在后端启动或暂时不可用时仍返回 200。部署探针应使用 /ready:它会检查后端模型目录,未就绪时返回 503。

GET /ready

响应示例:

{
  "status": "ok",
  "proxy": true
}

📋 模型列表

GET /v1/models

配置了显式模型 alias 且目标模型存在时,alias 也会出现在列表中;代理不会把未知模型静默映射到其他模型。

响应示例:

{
  "object": "list",
  "data": [
    {
      "id": "opencode/big-pickle",
      "object": "model",
      "created": 1704067200,
      "owned_by": "opencode"
    }
  ]
}

💬 Chat Completions

POST /v1/chat/completions

请求体:

参数 类型 必填 说明
model string ✅ 模型 ID
messages array ✅ 消息数组
tools array - 外部工具定义数组,遵循 OpenAI-compatible function tools 结构
tool_choice string/object - 工具选择策略;会按代理桥接语义处理
stream boolean - 是否流式输出
temperature number - 温度 (0-2)
top_p number - 核采样 (0-1)
max_tokens number - 最大 token 数
reasoning_effort string - 推理强度,支持 off、low、medium、high、max
thinking object - DeepSeek 思考模式,例如 { "type": "enabled" } 或 { "type": "disabled" }

示例:

curl -X POST http://127.0.0.1:10000/v1/chat/completions \
  -H "Authorization: Bearer YOUR_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{
    "model": "opencode/big-pickle",
    "messages": [{"role": "user", "content": "你好!"}],
    "stream": false
  }'

带外部工具的示例:

curl -X POST http://127.0.0.1:10000/v1/chat/completions \
  -H "Authorization: Bearer YOUR_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{
    "model": "opencode/big-pickle",
    "messages": [
      {"role": "user", "content": "读取 https://example.com 并告诉我标题"}
    ],
    "tools": [
      {
        "type": "function",
        "function": {
          "name": "web_fetch",
          "description": "Fetch a web page and summarize it",
          "parameters": {
            "type": "object",
            "properties": {
              "url": {"type": "string"}
            },
            "required": ["url"]
          }
        }
      }
    ]
  }'

当模型决定调用工具时,非流式响应会返回标准 message.tool_calls;流式响应会返回 chat.completion.chunk 中的 delta.tool_calls。

推理内容不会混入正文或包装为 <think> 标签。非流式响应使用 message.reasoning_content,流式响应使用 delta.reasoning_content;可见答案继续使用 message.content / delta.content。


🧠 Responses API

POST /v1/responses

🧭 推荐提示模板(OpenClaw / Claude Code)

在真实运行环境中,如果你希望第一跳稳定先产出 tool call,推荐把“调用工具”和“基于工具结果继续回答”拆成两步,而不是混在同一句里。

推荐第一跳提示:

Call weather_lookup for Tokyo now. Do not answer directly.

或中文:

现在调用 weather_lookup 查询 Tokyo。不要直接回答。

收到 tool_calls / function_call 后,再把工具结果回灌,并追加第二跳提示:

Great, now answer the original request using the tool result.

或中文:

很好,现在基于工具结果回答原始问题。

为什么推荐两段式提示

  • 第一跳只负责稳定产出工具调用
  • 第二跳只负责基于工具结果生成最终回答
  • 这样比把“先调工具再回答”写在同一条用户消息里更稳定,尤其适合 OpenClaw、Claude Code 这类 agent 客户端

推荐的 agent 行为

  1. 先发送严格的 tool-only 提示
  2. 如果收到 tool_calls / function_call,执行工具
  3. 回灌工具结果
  4. 再发送第二跳提示要求模型整合工具结果回答

这个两段式模式是当前 opencode2api 外部工具桥接的推荐集成方式。

🌊 流式工具调用

curl -N -X POST http://127.0.0.1:10000/v1/responses \
  -H "Authorization: Bearer YOUR_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{
    "model": "opencode/big-pickle",
    "input": "查询东京天气",
    "stream": true,
    "tools": [
      {
        "type": "function",
        "function": {
          "name": "weather_lookup",
          "description": "Look up weather by city",
          "parameters": {
            "type": "object",
            "properties": {
              "city": {"type": "string"}
            },
            "required": ["city"]
          }
        }
      }
    ]
  }'

当启用流式模式时:

  • Chat Completions 会在 chat.completion.chunk 中返回 delta.tool_calls
  • Responses API 会返回 response.output_item.added、response.function_call_arguments.delta、response.function_call_arguments.done、response.output_item.done 等事件

注意:代理内部会使用命名空间隔离同名工具,但这些内部名称不会作为公开 API 返回给客户端。DeepSeek 返回的 DSML、<function_calls> 或整条回复为严格 JSON 工具对象时,都会转换为公开的标准调用格式;原始标记不会混入正文。


🟣 Anthropic Messages API

POST /v1/messages

支持文本、thinking、tool_use/tool_result 和 stream=true SSE。工具调用由客户端执行后,以 Anthropic tool_result 消息继续请求。

curl -N -X POST http://127.0.0.1:10000/v1/messages \
  -H "Authorization: Bearer YOUR_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{
    "model": "opencode/big-pickle",
    "max_tokens": 512,
    "system": "Answer briefly.",
    "messages": [{"role": "user", "content": "你好"}],
    "stream": true
  }'

工具定义使用 Anthropic 的 name、description、input_schema 字段,并通过 tool_choice.type 支持 auto、none、any 和指定 tool。session adapter 会把工具调用映射并应用代理策略;匹配 Anthropic direct route 时,请求与响应的原生工具块会直接透传,不执行工具,也不应用 session adapter 的 allow/deny/repair 策略。客户端收到 tool_use 后负责执行工具,再用 tool_result 继续请求。

{
  "model": "opencode/big-pickle",
  "max_tokens": 512,
  "messages": [{ "role": "user", "content": "查询东京天气" }],
  "tools": [{
    "name": "weather_lookup",
    "description": "按城市查询天气",
    "input_schema": {
      "type": "object",
      "properties": { "city": { "type": "string" } },
      "required": ["city"]
    }
  }],
  "tool_choice": { "type": "auto" }
}

🔧 推理强度

输入值 映射结果
off none
minimal none
low low
medium medium
high high
max max
xhigh high

⚠️ 错误响应

401 Unauthorized

{
  "error": {
    "message": "Invalid API key",
    "type": "invalid_request_error",
    "code": "invalid_api_key"
  }
}

404 Not Found

{
  "error": {
    "message": "Model not found",
    "type": "invalid_request_error",
    "code": "model_not_found"
  }
}

500 Internal Server Error

{
  "error": {
    "message": "Internal server error",
    "type": "server_error",
    "code": "internal_error"
  }
}