通用开放接口文本 API
POST /v1/chat/completions
POST
/v1/chat/completionsChat 对话,支持非流式 JSON 与 SSE 流式。
鉴权:Bearer Token
Content-Type:application/json
请求体
{ "model": "gpt-4o", "messages": [ { "role": "system", "content": "You are a helpful assistant." }, { "role": "user", "content": "Hello, who are you?" } ], "temperature": 0.7, "max_tokens": 1024, "stream": false}请求字段
| 字段 | 类型 | 必填 | 说明 |
|---|---|---|---|
model | string | 是 | 模型 ID |
messages | array | 是 | 对话消息列表 |
messages[].role | string | 是 | system / user / assistant / tool |
messages[].content | string / array | 是 | 文本或多模态内容 |
stream | boolean | 否 | true 时返回 SSE 流,默认 false |
temperature | number | 否 | 采样温度,0–2 |
top_p | number | 否 | 核采样,0–1 |
max_tokens | integer | 否 | 最大输出 Token 数 |
max_completion_tokens | integer | 否 | 最大补全 Token 数 |
stop | string / array | 否 | 停止序列 |
tools | array | 否 | 工具定义列表 |
tool_choice | string / object | 否 | 工具选择策略 |
response_format | object | 否 | 结构化输出格式 |
stream_options | object | 否 | 流式选项,如 { "include_usage": true } |
多模态消息示例(图片输入):
{ "model": "gpt-4o", "messages": [ { "role": "user", "content": [ { "type": "text", "text": "What's in this image?" }, { "type": "image_url", "image_url": { "url": "https://example.com/image.jpg" } } ] } ]}请求示例(curl)
非流式:
curl https://www.yunsell.com/v1/chat/completions \ -H "Authorization: Bearer sk-xxx" \ -H "Content-Type: application/json" \ -d '{ "model": "gpt-4o", "messages": [ {"role": "system", "content": "You are a helpful assistant."}, {"role": "user", "content": "Hello, who are you?"} ], "temperature": 0.7, "max_tokens": 1024, "stream": false }'流式(SSE):
curl -N https://www.yunsell.com/v1/chat/completions \ -H "Authorization: Bearer sk-xxx" \ -H "Content-Type: application/json" \ -d '{ "model": "gpt-4o", "messages": [{"role": "user", "content": "Hello"}], "stream": true }'响应体(非流式)
HTTP 200,Content-Type: application/json
{ "id": "chatcmpl-abc123", "object": "chat.completion", "created": 1712697600, "model": "gpt-4o", "choices": [ { "index": 0, "message": { "role": "assistant", "content": "Hello! I'm an AI assistant. How can I help you today?" }, "finish_reason": "stop" } ], "usage": { "prompt_tokens": 28, "completion_tokens": 15, "total_tokens": 43 }}响应字段
| 字段 | 类型 | 说明 |
|---|---|---|
id | string | 响应 ID,如 chatcmpl-xxx |
object | string | 固定为 chat.completion |
created | integer | Unix 时间戳(秒) |
model | string | 实际使用的模型 |
choices | array | 生成结果列表,长度等于请求中的 n(默认 1) |
choices[].index | integer | 结果索引,从 0 开始 |
choices[].message.role | string | 固定为 assistant |
choices[].message.content | string / null | 回复文本;工具调用时可能为 null |
choices[].message.tool_calls | array | 工具调用列表(启用 tools 时) |
choices[].message.tool_calls[].id | string | 工具调用 ID |
choices[].message.tool_calls[].type | string | 固定为 function |
choices[].message.tool_calls[].function.name | string | 函数名 |
choices[].message.tool_calls[].function.arguments | string | 函数参数 JSON 字符串 |
choices[].message.reasoning_content | string | 推理过程文本(推理模型) |
choices[].finish_reason | string | 结束原因,见下表 |
usage | object | Token 用量,见 公共响应对象 |
finish_reason 取值:
| 值 | 说明 |
|---|---|
stop | 自然结束或命中 stop 序列 |
length | 达到 max_tokens 上限 |
tool_calls | 模型发起工具调用 |
content_filter | 内容被安全策略拦截 |
工具调用响应示例:
{ "id": "chatcmpl-abc123", "object": "chat.completion", "created": 1712697600, "model": "gpt-4o", "choices": [ { "index": 0, "message": { "role": "assistant", "content": null, "tool_calls": [ { "id": "call_abc123", "type": "function", "function": { "name": "get_weather", "arguments": "{\"location\":\"Beijing\"}" } } ] }, "finish_reason": "tool_calls" } ], "usage": { "prompt_tokens": 50, "completion_tokens": 20, "total_tokens": 70 }}响应体(流式)
HTTP 200,Content-Type: text/event-stream
请求体设置 "stream": true 后,服务端以 SSE 推送,每条消息格式:
data: {"id":"chatcmpl-abc123","object":"chat.completion.chunk","created":1712697600,"model":"gpt-4o","choices":[{"index":0,"delta":{"role":"assistant","content":"Hello"},"finish_reason":null}]}data: {"id":"chatcmpl-abc123","object":"chat.completion.chunk","created":1712697600,"model":"gpt-4o","choices":[{"index":0,"delta":{"content":"!"},"finish_reason":null}]}data: {"id":"chatcmpl-abc123","object":"chat.completion.chunk","created":1712697600,"model":"gpt-4o","choices":[{"index":0,"delta":{},"finish_reason":"stop"}],"usage":{"prompt_tokens":28,"completion_tokens":15,"total_tokens":43}}data: [DONE]流式 chunk 字段
| 字段 | 类型 | 说明 |
|---|---|---|
id | string | 与最终响应相同的 ID |
object | string | 固定为 chat.completion.chunk |
created | integer | Unix 时间戳(秒) |
model | string | 使用的模型 |
system_fingerprint | string | 系统指纹(部分模型返回) |
choices[].index | integer | 结果索引 |
choices[].delta.role | string | 首条 chunk 可能含 assistant |
choices[].delta.content | string | 增量文本片段 |
choices[].delta.reasoning_content | string | 增量推理文本(推理模型) |
choices[].delta.tool_calls | array | 增量工具调用片段 |
choices[].finish_reason | string / null | 末条 chunk 为结束原因,其余为 null |
usage | object | 仅末条 chunk 出现(需 stream_options.include_usage=true) |
流式结束标志:最后一行 data: [DONE]。