POST/v1/chat/completions
OpenAI 兼容Chat Completions
创建对话补全请求,支持流式响应和函数调用
请求参数
| 参数 | 类型 | 必填 | 说明 |
|---|---|---|---|
| model | string | 是 | 模型 ID,如 openai/gpt-4o |
| messages | array | 是 | 消息数组 |
| temperature | number | 否 | 采样温度,范围 0-2默认: 0.7 |
| max_tokens | integer | 否 | 最大响应 token 数 |
| stream | boolean | 否 | 是否使用流式响应默认: false |
| tools | array | 否 | 函数工具定义数组 |
| tool_choice | string/object | 否 | 工具选择策略可选值: auto, none, required |
| response_format | object | 否 | 输出格式规范 |
Message 格式说明
messages 数组中的每个消息对象包含以下字段:
system系统提示,用于设置助手行为
user用户消息
assistant助手消息
content 字段支持两种格式:
- 字符串:纯文本消息
- 内容数组:支持文本和图片混合(多模态模型)
请求示例
cURL
curl https://api.mintcloud.ai/v1/chat/completions \
-H "Authorization: Bearer $MINTCLOUD_API_KEY" \
-H "Content-Type: application/json" \
-d '{
"model": "openai/gpt-4o",
"messages": [{"role": "user", "content": "Hello!"}]
}'from openai import OpenAI
client = OpenAI(
base_url="https://api.mintcloud.ai/v1",
api_key="<your API key>"
)
response = client.chat.completions.create(
model="openai/gpt-4o",
messages=[{"role": "user", "content": "Hello!"}]
)
print(response.choices[0].message.content)响应格式
标准 OpenAI Chat Completions 响应格式:
{
"id": "chatcmpl-xxxxxxxxxxxx",
"object": "chat.completion",
"created": 1677652288,
"model": "openai/gpt-4o",
"choices": [
{
"index": 0,
"message": {
"role": "assistant",
"content": "Hello! How can I help you today?"
},
"finish_reason": "stop"
}
],
"usage": {
"prompt_tokens": 10,
"completion_tokens": 12,
"total_tokens": 22
}
}流式响应
设置 stream: true 启用 SSE 流式响应,每个 Chunk 格式如下:
data: {"id":"chatcmpl-xxx","object":"chat.completion.chunk","created":1234567890,"model":"openai/gpt-4o","choices":[{"index":0,"delta":{"content":"Hello"},"finish_reason":null}]}
data: {"id":"chatcmpl-xxx","object":"chat.completion.chunk","created":1234567890,"model":"openai/gpt-4o","choices":[{"index":0,"delta":{"content":"!"},"finish_reason":null}]}
data: {"id":"chatcmpl-xxx","object":"chat.completion.chunk","created":1234567890,"model":"openai/gpt-4o","choices":[{"index":0,"delta":{},"finish_reason":"stop"}]}
data: [DONE]流式响应特点
- 每个数据块以 data: 开头
- 最后一块为 data: [DONE]
- delta.content 包含增量文本
- finish_reason 出现时表示流结束