Chat 流式补全
![]()
▼
Authorization
在 Header 添加参数 Authorization,其值为 Bearer 之后拼接 Token
示例:
Authorization: Bearer ********************
请求参数
Header 参数
Content-Type
string
必需
示例: application/json
Authorization
string
必需
示例: Bearer $OPENAI_API_KEY
Body 参数 application/json
model
string
必需
要使用的模型的 ID。有关哪些模型可与聊天 API 一起使用的详细信息,请参阅模型端点兼容性表。
messages
array [object]
必需
至今为止对话所包含的消息列表。
role
string
可选
content
string
可选
temperature
number
可选
使用什么采样温度,介于 0 和 2 之间。较高的值(如 0.8)将使输出更加随机,而较低的值(如 0.2)将使输出更加集中和确定。我们通常建议改变这个或 top_p 但不是两者。
top_p
number
可选
一种替代温度采样的方法,称为核采样,其中模型考虑具有 top_p 概率质量的标记的结果。所以 0.1 意味着只考虑构成前 10% 概率质量的标记。我们通常建议改变这个或 temperature 但不是两者。
n
integer
可选
默认为 1。为每个输入消息生成多少个聊天补全选择。
stream
boolean
可选
默认为 false。如果设置,则像在 ChatGPT 中一样会发送部分消息增量。标记将以仅数据的服务器发送事件的形式发送,并在 data: [DONE] 消息终止流。
stop
string
可选
默认为 null。最多 4 个序列,API 将停止进一步生成标记。
max_tokens
integer
可选
默认为 inf。在聊天补全中生成的最大标记数。输入标记和生成标记的总长度受模型的上下文长度限制。
presence_penalty
number
可选
-2.0 和 2.0 之间的数字。正值会根据到目前为止是否出现在文本中来惩罚新标记,从而增加模型谈论新主题的可能性。
frequency_penalty
number
可选
默认为 0。-2.0 到 2.0 之间的数字。正值根据文本目前的存在频率惩罚新标记,降低模型重复相同行的可能性。
logit_bias
object
可选
修改指定标记出现在补全中的可能性。接受一个 JSON 对象,该对象将标记映射到相关的偏差值(-100 到 100)。
user
string
可选
代表您的最终用户的唯一标识符,可以帮助监控和检测滥用行为。
tools
array [object]
可选
模型可以调用的一组工具列表。目前,只支持作为工具的函数。
tool_choice
object
可选
控制模型调用哪个函数(如果有的话)。none 表示不调用函数,auto 表示自动选择。
response_format
object
可选
指定模型必须输出的格式的对象。将 { "type": "json_object" } 启用 JSON 模式。
seed
integer
可选
此功能处于测试阶段。如果指定,我们的系统将尽最大努力确定性地进行采样。
stream_options
object
可选
流式响应选项,如 { "include_usage": true } 可在最后一个 chunk 中返回 usage 信息。
示例
{
"model": "gpt-5-mini",
"max_tokens": 1000,
"messages": [
{
"role": "system",
"content": "You are a helpful assistant."
},
{
"role": "user",
"content": "你好"
}
],
"temperature": 1.0,
"stream": true,
"stream_options": {
"include_usage": true
}
}
请求示例代码
curl --location --request POST 'https://api.quickrouter.ai/v1/chat/completions' \
--header 'Authorization: Bearer <token>' \
--header 'Content-Type: application/json' \
--data-raw '{
"model": "gpt-5-mini",
"max_tokens": 1000,
"messages": [
{
"role": "system",
"content": "You are a helpful assistant."
},
{
"role": "user",
"content": "你好"
}
],
"temperature": 1.0,
"stream": true,
"stream_options": {
"include_usage": true
}
}'
var myHeaders = new Headers();
myHeaders.append("Authorization", "Bearer <token>");
myHeaders.append("Content-Type", "application/json");
var raw = JSON.stringify({
"model": "gpt-5-mini",
"max_tokens": 1000,
"messages": [
{
"role": "system",
"content": "You are a helpful assistant."
},
{
"role": "user",
"content": "你好"
}
],
"temperature": 1,
"stream": true,
"stream_options": {
"include_usage": true
}
});
var requestOptions = {
method: 'POST',
headers: myHeaders,
body: raw,
redirect: 'follow'
};
fetch("https://api.quickrouter.ai/v1/chat/completions", requestOptions)
.then(response => response.text())
.then(result => console.log(result))
.catch(error => console.log('error', error));
import http.client
import json
conn = http.client.HTTPSConnection("api.quickrouter.ai")
payload = json.dumps({
"model": "gpt-5-mini",
"max_tokens": 1000,
"messages": [
{
"role": "system",
"content": "You are a helpful assistant."
},
{
"role": "user",
"content": "你好"
}
],
"temperature": 1,
"stream": True,
"stream_options": {
"include_usage": True
}
})
headers = {
'Authorization': 'Bearer <token>',
'Content-Type': 'application/json'
}
conn.request("POST", "/v1/chat/completions", payload, headers)
res = conn.getresponse()
data = res.read()
print(data.decode("utf-8"))
返回响应
响应参数 🟢 200 OK · application/json
id
string
必需
object
string
必需
created
integer
必需
model
string
必需
choices
array [object]
必需
usage
object
必需
示例
{
"id": "chatcmpl-123",
"object": "chat.completion",
"created": 1677652288,
"model": "gpt-3.5-turbo-0613",
"choices": [
{
"index": 0,
"message": {
"role": "assistant",
"content": "\n\nHello there, how may I assist you today?"
},
"finish_reason": "stop"
}
],
"usage": {
"prompt_tokens": 9,
"completion_tokens": 12,
"total_tokens": 21
}
}