Appearance
Chat 对话补全
OpenAI 兼容的对话补全接口,根据对话历史生成模型回复。支持流式和非流式响应。
参考文档: https://platform.openai.com/docs/api-reference/chat/create
请求
POST /v1/chat/completions
认证
使用 Bearer Token 认证,格式: Authorization: Bearer sk-xxxxxx
请求体 (application/json)
| 参数 | 类型 | 必填 | 说明 |
|---|---|---|---|
| model | string | 是 | 模型 ID(如 gpt-4) |
| messages | array[Message] | 是 | 对话消息列表 |
| temperature | number | 否 | 采样温度,范围 0-2,默认 1 |
| top_p | number | 否 | 核采样参数,范围 0-1,默认 1 |
| n | integer | 否 | 生成补全数量,最小 1,默认 1 |
| stream | boolean | 否 | 是否流式响应,默认 false |
| stream_options | object | 否 | 流式选项,包含 include_usage (boolean) |
| stop | string/array | 否 | 停止序列 |
| max_tokens | integer | 否 | 最大生成 token 数 |
| max_completion_tokens | integer | 否 | 最大总 token 数 |
| presence_penalty | number | 否 | 存在惩罚,范围 -2 到 2,默认 0 |
| frequency_penalty | number | 否 | 频率惩罚,范围 -2 到 2,默认 0 |
| logit_bias | object | 否 | token ID 到偏置值的映射 |
| user | string | 否 | 用户标识 |
| tools | array[Tool] | 否 | 模型可调用的工具列表 |
| tool_choice | string/object | 否 | none、auto、required 或具体工具对象 |
| response_format | object | 否 | 格式规范(text、json_object、json_schema) |
| seed | integer | 否 | 确定性采样种子 |
| reasoning_effort | string | 否 | 推理努力程度:low、medium、high |
| modalities | array[string] | 否 | 模态:text、audio |
| audio | object | 否 | 音频配置,含 voice 和 format |
Message 对象
| 参数 | 类型 | 必填 | 说明 |
|---|---|---|---|
| role | string | 是 | system、user、assistant、tool、developer |
| content | string/array | 是 | 消息内容 |
| name | string | 否 | 参与者名称 |
| tool_calls | array | 否 | 工具调用(assistant 消息) |
| tool_call_id | string | 否 | 工具调用 ID(tool 角色消息) |
| reasoning_content | string | 否 | 推理内容 |
多模态消息内容 (MessageContent)
| 参数 | 类型 | 说明 |
|---|---|---|
| type | string | text、image_url、input_audio、file、video_url |
| text | string | 文本内容(type=text 时) |
| image_url | object | `{url: string, detail: "low" |
| input_audio | object | `{data: string(base64), format: "wav" |
| file | object | {filename, file_data, file_id} |
| video_url | object | {url: string} |
请求示例
bash
curl -X POST "https://your-api-base/v1/chat/completions" \
-H "Authorization: Bearer sk-xxxxxx" \
-H "Content-Type: application/json" \
-d '{
"model": "gpt-4",
"messages": [
{"role": "system", "content": "You are a helpful assistant."},
{"role": "user", "content": "Hello!"}
],
"temperature": 0.7,
"stream": false
}'python
from openai import OpenAI
client = OpenAI(
api_key="sk-xxxxxx",
base_url="https://your-api-base/v1"
)
response = client.chat.completions.create(
model="gpt-4",
messages=[
{"role": "system", "content": "You are a helpful assistant."},
{"role": "user", "content": "Hello!"}
],
temperature=0.7
)
print(response.choices[0].message.content)javascript
const response = await fetch("https://your-api-base/v1/chat/completions", {
method: "POST",
headers: {
"Authorization": "Bearer sk-xxxxxx",
"Content-Type": "application/json"
},
body: JSON.stringify({
model: "gpt-4",
messages: [
{ role: "system", content: "You are a helpful assistant." },
{ role: "user", content: "Hello!" }
],
temperature: 0.7
})
});
const data = await response.json();
console.log(data.choices[0].message.content);响应
200 成功响应
json
{
"id": "chatcmpl-abc123",
"object": "chat.completion",
"created": 1677858242,
"model": "gpt-4",
"choices": [
{
"index": 0,
"message": {
"role": "assistant",
"content": "Hello! How can I help you today?"
},
"finish_reason": "stop"
}
],
"usage": {
"prompt_tokens": 20,
"completion_tokens": 8,
"total_tokens": 28,
"prompt_tokens_details": {
"cached_tokens": 0,
"text_tokens": 20,
"audio_tokens": 0,
"image_tokens": 0
},
"completion_tokens_details": {
"text_tokens": 8,
"audio_tokens": 0,
"reasoning_tokens": 0
}
},
"system_fingerprint": "fp_44709d6fcb"
}流式响应 (chunk)
json
{
"id": "chatcmpl-abc123",
"object": "chat.completion.chunk",
"created": 1677858242,
"model": "gpt-4",
"choices": [
{
"index": 0,
"delta": {
"role": "assistant",
"content": "Hello"
},
"finish_reason": null
}
]
}错误响应
json
{
"error": {
"message": "Invalid API key",
"type": "invalid_request_error",
"param": null,
"code": "invalid_api_key"
}
}