流式接口
POST
/v1/chat/completions在使用 API 接口前,请先在百川 API 开放平台完成实名认证、充值、创建 APIkey 等流程,如下图所示
请求参数
Header 参数
Content-Type
string
响应的数据格式
示例值:
application/json
Authorization
string
必需
示例值:
Bearer $API_KEY
Body 参数application/json
model
string
必需
使用的模型 ID,模型列表: Baichuan2-Turbo Baichuan2-Turbo-192k
messages
array [object {2}]
必需
对话消息列表 (历史对话按从老到新顺序填入)
role
string
可选
消息作者的角色,为以下其中之一 1. user 2. assistant
content
string
消息内容
temperature
number
可选
取值范围: [.0f, 1.0f]。 多样性,越高,多样性越好, 缺省 0.3
stream
boolean
可选
是否使用流式接口,默认值为 false
示例
{
"model": "Baichuan2-Turbo",
"messages": [
{
"role": "user",
"content": "世界第一高峰是?"
}
],
"temperature": 0.3,
"stream": true
}
示例代码
返回响应
成功(200)
HTTP 状态码: 200
内容格式: JSONapplication/json
数据结构
id
string
必需
"Chat Complete"的唯一标识符
object
string
必需
created
integer
必需
创建聊天完成时的 Unix 时间戳(秒)
model
string
必需
choices
array [object {3}]
聊天完成选项的列表
index
integer
可选
choices 中对应的索引
delta
object
可选
增量返回,由流式模型生成的聊天完成消息
finish_reason
string
可选
模型停止生成 token 的原因,为以下值: 1. stop :模型达到自然停止点/用户 stop 指令 2. content_filter:被内容过滤器中过滤掉
示例
data:{"id":"chatcmpl-M633300APBknoaF","object":"chat.completion.chunk","created":1698205608,"model":"Baichuan2-Turbo","choices":[{"index":0,"delta":{"role":"assistant","content":"世界第一高峰是珠穆"},"finish_reason":""}]}
data:{"id":"chatcmpl-M633300APBknoaF","object":"chat.completion.chunk","created":1698205608,"model":"Baichuan2-Turbo","choices":[{"index":0,"delta":{"role":"assistant","content":"朗玛峰(Mount"},"finish_reason":""}]}
data:{"id":"chatcmpl-M633300APBknoaF","object":"chat.completion.chunk","created":1698205608,"model":"Baichuan2-Turbo","choices":[{"index":0,"delta":{"role":"assistant","content":" Everest),位于尼泊尔"},"finish_reason":""}]}
data:{"id":"chatcmpl-M633300APBknoaF","object":"chat.completion.chunk","created":1698205609,"model":"Baichuan2-Turbo","choices":[{"index":0,"delta":{"role":"assistant","content":"和中国边境,海拔高度"},"finish_reason":""}]}
data:{"id":"chatcmpl-M633300APBknoaF","object":"chat.completion.chunk","created":1698205609,"model":"Baichuan2-Turbo","choices":[{"index":0,"delta":{"role":"assistant","content":"为8,84"},"finish_reason":""}]}
data:{"id":"chatcmpl-M633300APBknoaF","object":"chat.completion.chunk","created":1698205609,"model":"Baichuan2-Turbo","choices":[{"index":0,"delta":{"role":"assistant","content":"8米。"},"finish_reason":"stop"}],"usage":{"prompt_tokens":6,"completion_tokens":29,"total_tokens":35}}
data:[DONE]
最后修改时间: 1 年前