主题
Chat Completions
OpenAI Chat Completions 兼容接口。适合多数聊天客户端与 OpenAI SDK。
请求
http
POST /v1/chat/completions
Authorization: Bearer <API Key>
Content-Type: application/json1
2
3
2
3
请求体
| 字段 | 类型 | 必填 | 说明 |
|---|---|---|---|
model | string | 是 | 模型 ID |
messages | array | 是 | 对话消息数组 |
stream | boolean | 否 | 是否使用流式响应,默认 false |
temperature | number | 否 | 采样温度 |
max_tokens | integer | 否 | 最大生成 token 数 |
tools | array | 否 | 可用工具列表 |
tool_choice | string / object | 否 | 工具选择策略 |
消息格式:
json
{
"model": "<模型 ID>",
"messages": [
{ "role": "system", "content": "你是一个简洁的助手。" },
{ "role": "user", "content": "你好" }
]
}1
2
3
4
5
6
7
2
3
4
5
6
7
不同模型支持的参数可能不同。如果不确定,先使用最小请求验证。
非流式响应
json
{
"id": "chatcmpl-...",
"object": "chat.completion",
"created": 1730000000,
"model": "<模型 ID>",
"choices": [
{
"index": 0,
"message": {
"role": "assistant",
"content": "你好!有什么可以帮你?"
},
"finish_reason": "stop"
}
],
"usage": {
"prompt_tokens": 12,
"completion_tokens": 9,
"total_tokens": 21
}
}1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
流式响应
设置 "stream": true 后,返回 SSE 事件:
text
data: {"choices":[{"delta":{"content":"你好"}}]}
data: {"choices":[{"delta":{"content":"!"}}]}
data: [DONE]1
2
3
4
5
2
3
4
5
示例
bash
curl https://api.monoai.today/v1/chat/completions \
-H "Authorization: Bearer $MONOIZE_API_KEY" \
-H "Content-Type: application/json" \
-d '{
"model": "<模型 ID>",
"messages": [{ "role": "user", "content": "用一句话解释什么是 API。" }]
}'1
2
3
4
5
6
7
2
3
4
5
6
7