端点
POST /v1/chat/completions
OpenAI Chat Completions 兼容端点,适用于 GPT 与 Gemini 模型。不建议通过本端点调用 Claude:会丢失 prompt cache、thinking 等能力,请改用 创建消息。
请求
请求头
| 请求头 | 取值 | 说明 |
|---|---|---|
Authorization | Bearer sk-xxxxxx | 鉴权。也可以改用 x-api-key: sk-xxxxxx |
content-type | application/json |
最小请求体:
json
{
"model": "gpt-5.6-sol",
"messages": [
{"role": "user", "content": "Hello"}
]
}参数
| 字段 | 类型 | 必填 | 说明 |
|---|---|---|---|
model | string | 是 | GPT 或 Gemini 模型 ID,完整列表见 GET /v1/models |
messages | array | 是 | 对话消息,role 为 system、user 或 assistant,content 为文本 |
max_tokens | integer | 否 | 最大输出 token 数 |
stream | boolean | 否 | 设为 true 时以 SSE 流式返回,默认 false |
其余字段按 OpenAI Chat Completions API 格式透传,用法以 OpenAI 官方文档为准。
响应
非流式返回 chat.completion 对象:
json
{
"object": "chat.completion",
"model": "gpt-5.6-sol",
"choices": [
{"index": 0, "message": {"role": "assistant", "content": "Hello!"}, "finish_reason": "stop"}
],
"usage": {"prompt_tokens": 214, "completion_tokens": 3, "total_tokens": 217}
}| 字段 | 说明 |
|---|---|
id | 响应 ID |
object | 固定为 chat.completion |
created | 创建时间(Unix 秒) |
model | 实际响应的模型 ID |
choices | 回复数组,文本在 choices[0].message.content,finish_reason 为停止原因(如 stop) |
usage | prompt_tokens、completion_tokens、total_tokens;命中缓存时另有 prompt_tokens_details.cached_tokens |
service_tier | 本次请求实际使用的服务档位 |
stream 为 true 时返回 data: {...} 分片(object 为 chat.completion.chunk),[DONE] 之前的最后一个分片带 usage,详见 流式输出。
示例
基础请求
bash
curl https://caapi.top/v1/chat/completions \
-H "Authorization: Bearer sk-xxxxxx" \
-H "content-type: application/json" \
-d '{
"model": "gpt-5.6-sol",
"max_tokens": 1024,
"messages": [{"role": "user", "content": "Hello"}]
}'流式请求
bash
curl https://caapi.top/v1/chat/completions \
-H "Authorization: Bearer sk-xxxxxx" \
-H "content-type: application/json" \
-d '{
"model": "gpt-5.6-sol",
"stream": true,
"messages": [{"role": "user", "content": "写一句话"}]
}'Python SDK
python
from openai import OpenAI
client = OpenAI(
api_key="sk-xxxxxx",
base_url="https://caapi.top/v1",
)
resp = client.chat.completions.create(
model="gpt-5.6-sol",
messages=[{"role": "user", "content": "你好"}],
)
print(resp.choices[0].message.content)错误
| HTTP 状态 | error.type | 触发条件 |
|---|---|---|
| 401 | missing_auth_credential / invalid_api_key / invalid_bearer_token | 鉴权失败,详见 鉴权 |
| 400 | model_not_available | model 不存在或暂不可用,请对照 GET /v1/models 检查 ID |
| 400 | model_not_supported | 该模型当前不支持调用,按提示改用其他模型 |