跳到主要内容

Chat Completions API

POST /v1/chat/completions 是最常用的端点,用于与语言模型进行多轮对话。

请求

POST https://real200.com/v1/chat/completions

Headers

Header类型必填说明
AuthorizationstringBearer sk-real200-xxx
Content-Typestringapplication/json

Body 参数

参数类型必填默认值说明
modelstring-模型名称,如 gpt-4oclaude-sonnet-4
messagesarray-对话消息列表
temperaturenumber1.0随机性,范围 0–2
top_pnumber1.0核采样概率阈值
max_tokensinteger模型默认最大生成 Token 数
streambooleanfalse是否启用流式输出
stopstring/arraynull停止序列
presence_penaltynumber0存在惩罚,范围 -2–2
frequency_penaltynumber0频率惩罚,范围 -2–2
response_formatobject-指定 JSON 响应格式
toolsarray-工具(函数调用)定义
tool_choicestring/object"auto"工具选择策略
seedinteger-随机种子(可复现)

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 状态码错误类型说明
400invalid_request_error请求参数错误
401invalid_api_keyAPI Key 无效
402insufficient_quota余额或额度不足
429rate_limit_exceeded请求频率超限
500api_error服务端错误

详见错误码字典