跳转到内容

OpenAI Chat Completions

CoderClaw 的 Gateway 网关可以提供一个小型的 OpenAI 兼容 Chat Completions 端点。

此端点默认禁用。请先在配置中启用它。

  • POST /v1/chat/completions
  • 与 Gateway 网关相同的端口(WS + HTTP 多路复用):http://<gateway-host>:<port>/v1/chat/completions

底层实现中,请求作为普通的 Gateway 网关智能体运行执行(与 coderclaw agent 相同的代码路径),因此路由/权限/配置与你的 Gateway 网关一致。

使用 Gateway 网关认证配置。发送 bearer 令牌:

  • Authorization: Bearer <token>

注意事项:

  • gateway.auth.mode="token" 时,使用 gateway.auth.token(或 CODERCLAW_GATEWAY_TOKEN)。
  • gateway.auth.mode="password" 时,使用 gateway.auth.password(或 CODERCLAW_GATEWAY_PASSWORD)。

无需自定义头:在 OpenAI model 字段中编码智能体 ID:

  • model: "coderclaw:<agentId>"(例如:"coderclaw:main""coderclaw:beta"
  • model: "agent:<agentId>"(别名)

或通过头指定特定的 CoderClaw 智能体:

  • x-coderclaw-agent-id: <agentId>(默认:main

高级选项:

  • x-coderclaw-session-key: <sessionKey> 完全控制会话路由。

gateway.http.endpoints.chatCompletions.enabled 设置为 true

{
gateway: {
http: {
endpoints: {
chatCompletions: { enabled: true },
},
},
},
}

gateway.http.endpoints.chatCompletions.enabled 设置为 false

{
gateway: {
http: {
endpoints: {
chatCompletions: { enabled: false },
},
},
},
}

默认情况下,端点是每请求无状态的(每次调用生成新的会话键)。

如果请求包含 OpenAI user 字符串,Gateway 网关会从中派生一个稳定的会话键,因此重复调用可以共享智能体会话。

设置 stream: true 以接收 Server-Sent Events(SSE):

  • Content-Type: text/event-stream
  • 每个事件行是 data: <json>
  • 流以 data: [DONE] 结束

非流式:

Terminal window
curl -sS http://127.0.0.1:18789/v1/chat/completions \
-H 'Authorization: Bearer YOUR_TOKEN' \
-H 'Content-Type: application/json' \
-H 'x-coderclaw-agent-id: main' \
-d '{
"model": "coderclaw",
"messages": [{"role":"user","content":"hi"}]
}'

流式:

Terminal window
curl -N http://127.0.0.1:18789/v1/chat/completions \
-H 'Authorization: Bearer YOUR_TOKEN' \
-H 'Content-Type: application/json' \
-H 'x-coderclaw-agent-id: main' \
-d '{
"model": "coderclaw",
"stream": true,
"messages": [{"role":"user","content":"hi"}]
}'