2026-02-01 22:47:44 +01:00
|
|
|
|
---
|
|
|
|
|
|
read_when:
|
|
|
|
|
|
- 集成需要 OpenAI Chat Completions 的工具
|
2026-02-03 13:23:00 -08:00
|
|
|
|
summary: 从 Gateway 网关暴露 OpenAI 兼容的 /v1/chat/completions HTTP 端点
|
2026-02-01 22:47:44 +01:00
|
|
|
|
title: OpenAI Chat Completions
|
|
|
|
|
|
x-i18n:
|
2026-02-03 13:23:00 -08:00
|
|
|
|
generated_at: "2026-02-03T07:48:15Z"
|
2026-02-01 22:47:44 +01:00
|
|
|
|
model: claude-opus-4-5
|
|
|
|
|
|
provider: pi
|
|
|
|
|
|
source_hash: 6f935777f489bff925a3bf18b1e4b7493f83ae7b1e581890092e5779af59b732
|
|
|
|
|
|
source_path: gateway/openai-http-api.md
|
2026-02-03 13:23:00 -08:00
|
|
|
|
workflow: 15
|
2026-02-01 22:47:44 +01:00
|
|
|
|
---
|
|
|
|
|
|
|
2026-02-03 13:23:00 -08:00
|
|
|
|
# OpenAI Chat Completions(HTTP)
|
2026-02-01 22:47:44 +01:00
|
|
|
|
|
2026-02-03 13:23:00 -08:00
|
|
|
|
OpenClaw 的 Gateway 网关可以提供一个小型的 OpenAI 兼容 Chat Completions 端点。
|
2026-02-01 22:47:44 +01:00
|
|
|
|
|
|
|
|
|
|
此端点**默认禁用**。请先在配置中启用它。
|
|
|
|
|
|
|
|
|
|
|
|
- `POST /v1/chat/completions`
|
2026-02-03 13:23:00 -08:00
|
|
|
|
- 与 Gateway 网关相同的端口(WS + HTTP 多路复用):`http://<gateway-host>:<port>/v1/chat/completions`
|
2026-02-01 22:47:44 +01:00
|
|
|
|
|
2026-02-03 13:23:00 -08:00
|
|
|
|
底层实现中,请求作为普通的 Gateway 网关智能体运行执行(与 `openclaw agent` 相同的代码路径),因此路由/权限/配置与你的 Gateway 网关一致。
|
2026-02-01 22:47:44 +01:00
|
|
|
|
|
|
|
|
|
|
## 认证
|
|
|
|
|
|
|
2026-02-03 13:23:00 -08:00
|
|
|
|
使用 Gateway 网关认证配置。发送 bearer 令牌:
|
2026-02-01 22:47:44 +01:00
|
|
|
|
|
|
|
|
|
|
- `Authorization: Bearer <token>`
|
|
|
|
|
|
|
2026-02-03 13:23:00 -08:00
|
|
|
|
注意事项:
|
2026-02-01 22:47:44 +01:00
|
|
|
|
|
|
|
|
|
|
- 当 `gateway.auth.mode="token"` 时,使用 `gateway.auth.token`(或 `OPENCLAW_GATEWAY_TOKEN`)。
|
|
|
|
|
|
- 当 `gateway.auth.mode="password"` 时,使用 `gateway.auth.password`(或 `OPENCLAW_GATEWAY_PASSWORD`)。
|
|
|
|
|
|
|
|
|
|
|
|
## 选择智能体
|
|
|
|
|
|
|
2026-02-03 13:23:00 -08:00
|
|
|
|
无需自定义头:在 OpenAI `model` 字段中编码智能体 ID:
|
2026-02-01 22:47:44 +01:00
|
|
|
|
|
2026-02-03 13:23:00 -08:00
|
|
|
|
- `model: "openclaw:<agentId>"`(例如:`"openclaw:main"`、`"openclaw:beta"`)
|
2026-02-01 22:47:44 +01:00
|
|
|
|
- `model: "agent:<agentId>"`(别名)
|
|
|
|
|
|
|
2026-02-03 13:23:00 -08:00
|
|
|
|
或通过头指定特定的 OpenClaw 智能体:
|
2026-02-01 22:47:44 +01:00
|
|
|
|
|
2026-02-03 13:23:00 -08:00
|
|
|
|
- `x-openclaw-agent-id: <agentId>`(默认:`main`)
|
2026-02-01 22:47:44 +01:00
|
|
|
|
|
2026-02-03 13:23:00 -08:00
|
|
|
|
高级选项:
|
2026-02-01 22:47:44 +01:00
|
|
|
|
|
2026-02-03 13:23:00 -08:00
|
|
|
|
- `x-openclaw-session-key: <sessionKey>` 完全控制会话路由。
|
2026-02-01 22:47:44 +01:00
|
|
|
|
|
|
|
|
|
|
## 启用端点
|
|
|
|
|
|
|
|
|
|
|
|
将 `gateway.http.endpoints.chatCompletions.enabled` 设置为 `true`:
|
|
|
|
|
|
|
|
|
|
|
|
```json5
|
|
|
|
|
|
{
|
|
|
|
|
|
gateway: {
|
|
|
|
|
|
http: {
|
|
|
|
|
|
endpoints: {
|
|
|
|
|
|
chatCompletions: { enabled: true },
|
|
|
|
|
|
},
|
|
|
|
|
|
},
|
|
|
|
|
|
},
|
|
|
|
|
|
}
|
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
|
|
## 禁用端点
|
|
|
|
|
|
|
|
|
|
|
|
将 `gateway.http.endpoints.chatCompletions.enabled` 设置为 `false`:
|
|
|
|
|
|
|
|
|
|
|
|
```json5
|
|
|
|
|
|
{
|
|
|
|
|
|
gateway: {
|
|
|
|
|
|
http: {
|
|
|
|
|
|
endpoints: {
|
|
|
|
|
|
chatCompletions: { enabled: false },
|
|
|
|
|
|
},
|
|
|
|
|
|
},
|
|
|
|
|
|
},
|
|
|
|
|
|
}
|
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
|
|
## 会话行为
|
|
|
|
|
|
|
2026-02-03 13:23:00 -08:00
|
|
|
|
默认情况下,端点是**每请求无状态**的(每次调用生成新的会话键)。
|
2026-02-01 22:47:44 +01:00
|
|
|
|
|
2026-02-03 13:23:00 -08:00
|
|
|
|
如果请求包含 OpenAI `user` 字符串,Gateway 网关会从中派生一个稳定的会话键,因此重复调用可以共享智能体会话。
|
2026-02-01 22:47:44 +01:00
|
|
|
|
|
2026-02-03 13:23:00 -08:00
|
|
|
|
## 流式传输(SSE)
|
2026-02-01 22:47:44 +01:00
|
|
|
|
|
2026-02-03 13:23:00 -08:00
|
|
|
|
设置 `stream: true` 以接收 Server-Sent Events(SSE):
|
2026-02-01 22:47:44 +01:00
|
|
|
|
|
|
|
|
|
|
- `Content-Type: text/event-stream`
|
2026-02-03 13:23:00 -08:00
|
|
|
|
- 每个事件行是 `data: <json>`
|
2026-02-01 22:47:44 +01:00
|
|
|
|
- 流以 `data: [DONE]` 结束
|
|
|
|
|
|
|
|
|
|
|
|
## 示例
|
|
|
|
|
|
|
|
|
|
|
|
非流式:
|
|
|
|
|
|
|
|
|
|
|
|
```bash
|
|
|
|
|
|
curl -sS http://127.0.0.1:18789/v1/chat/completions \
|
|
|
|
|
|
-H 'Authorization: Bearer YOUR_TOKEN' \
|
|
|
|
|
|
-H 'Content-Type: application/json' \
|
|
|
|
|
|
-H 'x-openclaw-agent-id: main' \
|
|
|
|
|
|
-d '{
|
|
|
|
|
|
"model": "openclaw",
|
|
|
|
|
|
"messages": [{"role":"user","content":"hi"}]
|
|
|
|
|
|
}'
|
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
|
|
流式:
|
|
|
|
|
|
|
|
|
|
|
|
```bash
|
|
|
|
|
|
curl -N http://127.0.0.1:18789/v1/chat/completions \
|
|
|
|
|
|
-H 'Authorization: Bearer YOUR_TOKEN' \
|
|
|
|
|
|
-H 'Content-Type: application/json' \
|
|
|
|
|
|
-H 'x-openclaw-agent-id: main' \
|
|
|
|
|
|
-d '{
|
|
|
|
|
|
"model": "openclaw",
|
|
|
|
|
|
"stream": true,
|
|
|
|
|
|
"messages": [{"role":"user","content":"hi"}]
|
|
|
|
|
|
}'
|
|
|
|
|
|
```
|