Mangogo 营业厅Mangogo 营业厅
首页API 参考Skills插件

POST /v1/chat/completions

POST/v1/chat/completions

Chat 对话,支持非流式 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}

请求字段

字段类型必填说明
modelstring是模型 ID
messagesarray是对话消息列表
messages[].rolestring是system / user / assistant / tool
messages[].contentstring / array是文本或多模态内容
streamboolean否true 时返回 SSE 流,默认 false
temperaturenumber否采样温度,0–2
top_pnumber否核采样,0–1
max_tokensinteger否最大输出 Token 数
max_completion_tokensinteger否最大补全 Token 数
stopstring / array否停止序列
toolsarray否工具定义列表
tool_choicestring / object否工具选择策略
response_formatobject否结构化输出格式
stream_optionsobject否流式选项,如 { "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  }}

响应字段

字段类型说明
idstring响应 ID,如 chatcmpl-xxx
objectstring固定为 chat.completion
createdintegerUnix 时间戳(秒)
modelstring实际使用的模型
choicesarray生成结果列表,长度等于请求中的 n(默认 1)
choices[].indexinteger结果索引,从 0 开始
choices[].message.rolestring固定为 assistant
choices[].message.contentstring / null回复文本;工具调用时可能为 null
choices[].message.tool_callsarray工具调用列表(启用 tools 时)
choices[].message.tool_calls[].idstring工具调用 ID
choices[].message.tool_calls[].typestring固定为 function
choices[].message.tool_calls[].function.namestring函数名
choices[].message.tool_calls[].function.argumentsstring函数参数 JSON 字符串
choices[].message.reasoning_contentstring推理过程文本(推理模型)
choices[].finish_reasonstring结束原因,见下表
usageobjectToken 用量,见 公共响应对象

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 字段

字段类型说明
idstring与最终响应相同的 ID
objectstring固定为 chat.completion.chunk
createdintegerUnix 时间戳(秒)
modelstring使用的模型
system_fingerprintstring系统指纹(部分模型返回)
choices[].indexinteger结果索引
choices[].delta.rolestring首条 chunk 可能含 assistant
choices[].delta.contentstring增量文本片段
choices[].delta.reasoning_contentstring增量推理文本(推理模型)
choices[].delta.tool_callsarray增量工具调用片段
choices[].finish_reasonstring / null末条 chunk 为结束原因,其余为 null
usageobject仅末条 chunk 出现(需 stream_options.include_usage=true)

流式结束标志:最后一行 data: [DONE]。