Chat Completions API
POST /v1/chat/completions 是最常用的端点,用于与语言模型进行多轮对话。
请求
POST https://real200.com/v1/chat/completions
Headers
| Header | 类型 | 必填 | 说明 |
|---|---|---|---|
Authorization | string | 是 | Bearer sk-real200-xxx |
Content-Type | string | 是 | application/json |
Body 参数
| 参数 | 类型 | 必填 | 默认值 | 说明 |
|---|---|---|---|---|
model | string | 是 | - | 模型名称,如 gpt-4o、claude-sonnet-4 |
messages | array | 是 | - | 对话消息列表 |
temperature | number | 否 | 1.0 | 随机性,范围 0–2 |
top_p | number | 否 | 1.0 | 核采样概率阈值 |
max_tokens | integer | 否 | 模型默认 | 最大生成 Token 数 |
stream | boolean | 否 | false | 是否启用流式输出 |
stop | string/array | 否 | null | 停止序列 |
presence_penalty | number | 否 | 0 | 存在惩罚,范围 -2–2 |
frequency_penalty | number | 否 | 0 | 频率惩罚,范围 -2–2 |
response_format | object | 否 | - | 指定 JSON 响应格式 |
tools | array | 否 | - | 工具(函数调用)定义 |
tool_choice | string/object | 否 | "auto" | 工具选择策略 |
seed | integer | 否 | - | 随机种子(可复现) |
messages 格式
{
"messages": [
{"role": "system", "content": "你是一个有用的助手。"},
{"role": "user", "content": "今天天气怎么样?"},
{"role": "assistant", "content": "我无法获取实时天气信息..."},
{"role": "user", "content": "那北京的呢?"}
]
}
示例请求
curl https://real200.com/v1/chat/completions \
-H "Content-Type: application/json" \
-H "Authorization: Bearer $REAL200_API_KEY" \
-d '{
"model": "gpt-4o",
"messages": [
{"role": "user", "content": "请用三句话介绍你自己。"}
],
"temperature": 0.7,
"max_tokens": 200
}'
响应
非流式响应
{
"id": "chatcmpl-abc123",
"object": "chat.completion",
"created": 1716300000,
"model": "gpt-4o",
"choices": [
{
"index": 0,
"message": {
"role": "assistant",
"content": "我是一个 AI 助手..."
},
"finish_reason": "stop",
"logprobs": null
}
],
"usage": {
"prompt_tokens": 12,
"completion_tokens": 45,
"total_tokens": 57
},
"system_fingerprint": "fp_abc123"
}
流式响应
启用 stream: true 后,响应以 text/event-stream 格式返回:
data: {"id":"chatcmpl-abc123","object":"chat.completion.chunk","choices":[{"index":0,"delta":{"role":"assistant"},"finish_reason":null}]}
data: {"id":"chatcmpl-abc123","object":"chat.completion.chunk","choices":[{"index":0,"delta":{"content":"我是"},"finish_reason":null}]}
data: {"id":"chatcmpl-abc123","object":"chat.completion.chunk","choices":[{"index":0,"delta":{"content":"一个"},"finish_reason":null}]}
data: {"id":"chatcmpl-abc123","object":"chat.completion.chunk","choices":[{"index":0,"delta":{},"finish_reason":"stop"}]}
data: [DONE]
finish_reason 说明
| 值 | 说明 |
|---|---|
stop | 模型自然结束输出 |
length | 达到 max_tokens 限制 |
content_filter | 内容被安全过滤 |
tool_calls | 模型调用了工具 |
错误码
| HTTP 状态码 | 错误类型 | 说明 |
|---|---|---|
| 400 | invalid_request_error | 请求参数错误 |
| 401 | invalid_api_key | API Key 无效 |
| 402 | insufficient_quota | 余额或额度不足 |
| 429 | rate_limit_exceeded | 请求频率超限 |
| 500 | api_error | 服务端错误 |
详见错误码字典。