Yi-34B-Chat
Yi-34B是由零一万物开发并开源的双语大语言模型,使用4K序列长度进行训练,在推理期间可扩展到32K;模型在多项评测中全球领跑,取得了多项 SOTA 国际最佳性能指标表现,该版本为支持对话的chat版本。本文介绍了相关API。
功能介绍
调用本接口,发起一次对话请求。
在线调试
百度智能云千帆提供了 API在线调试平台-示例代码 ,用于帮助开发者调试接口,平台集成快速检索、查看开发文档、查看在线调用的请求内容和返回结果、复制和下载示例代码等功能,简单易用,更多内容请查看API在线调试介绍。
SDK调用
使用说明
- 本文内容适用千帆Python、Go、Java和Node.js SDK,SDK调用流程请参考SDK安装及使用流程。
-
本文模型支持不同调用方式,请结合具体使用的模型服务,查看参数使用说明进行调用。
- 指定支持预置服务的模型,使用model参数指定支持预置服务的模型,详见请求参数说明。
- 用户快速部署自行发布的模型服务,需使用参数endpoint,详见请求参数说明。
调用示例(单轮)
- 指定支持预置服务的模型
使用model
字段,指定千帆平台支持预置服务的模型,调用示例如下。
import os
import qianfan
#【推荐】使用安全认证AK/SK鉴权,通过环境变量初始化认证信息
# 替换下列示例中参数,安全认证Access Key替换your_iam_ak,Secret Key替换your_iam_sk
os.environ["QIANFAN_ACCESS_KEY"] = "your_iam_ak"
os.environ["QIANFAN_SECRET_KEY"] = "your_iam_sk"
chat_comp = qianfan.ChatCompletion()
# 指定特定模型
resp = chat_comp.do(model="Yi-34B-Chat", messages=[{
"role": "user",
"content": "hi"
}])
print(resp["body"])
package main
import (
"context"
"fmt"
"os"
"github.com/baidubce/bce-qianfan-sdk/go/qianfan"
)
func main() {
// 使用安全认证AK/SK鉴权,通过环境变量初始化;替换下列示例中参数,安全认证Access Key替换your_iam_ak,Secret Key替换your_iam_sk
os.Setenv("QIANFAN_ACCESS_KEY", "your_iam_ak")
os.Setenv("QIANFAN_SECRET_KEY", "your_iam_sk")
// 指定特定模型
chat := qianfan.NewChatCompletion(
qianfan.WithModel("Yi-34B-Chat"),
)
resp, _ := chat.Do(
context.TODO(),
&qianfan.ChatCompletionRequest{
Messages: []qianfan.ChatCompletionMessage{
qianfan.ChatCompletionUserMessage("hi"),
},
},
)
fmt.Println(resp.Result)
}
import com.baidubce.qianfan.Qianfan;
import com.baidubce.qianfan.model.chat.ChatResponse;
public class Demo {
public static void main(String[] args) {
// 使用安全认证AK/SK鉴权,替换下列示例中参数,安全认证Access Key替换your_iam_ak,Secret Key替换your_iam_sk
Qianfan qianfan = new Qianfan("your_iam_ak", "your_iam_sk");
// 指定模型
ChatResponse resp = qianfan.chatCompletion()
.model("Yi-34B-Chat")
.addMessage("user", "hi")
.execute();
System.out.println(resp.getResult());
}
}
import {ChatCompletion, setEnvVariable} from "@baiducloud/qianfan";
// 使用安全认证AK/SK鉴权,通过环境变量初始化;替换下列示例中参数,安全认证Access Key替换your_iam_ak,Secret Key替换your_iam_sk
setEnvVariable('QIANFAN_ACCESS_KEY','your_iam_ak');
setEnvVariable('QIANFAN_SECRET_KEY','your_iam_sk');
const client = new ChatCompletion();
async function main() {
const resp = await client.chat({
messages: [
{
role: 'user',
content: 'hi',
},
],
}, 'Yi-34B-Chat');
console.log(resp);
}
main();
- 用户自行发布的模型服务
对于用户快速部署自行发布的模型服务,通过使用endpoint
字段进行调用,示例如下。
import os
import qianfan
#【推荐】使用安全认证AK/SK鉴权,通过环境变量初始化认证信息
# 替换下列示例中参数,安全认证Access Key替换your_iam_ak,Secret Key替换your_iam_sk
os.environ["QIANFAN_ACCESS_KEY"] = "your_iam_ak"
os.environ["QIANFAN_SECRET_KEY"] = "your_iam_sk"
chat_comp = qianfan.ChatCompletion()
# 使用自行发布的模型
resp = chat_comp.do(endpoint="your_custom_endpoint", messages=[{
"role": "user",
"content": "hi"
}])
# 可以通过resp["body"]获取接口返回的内容
print(resp["body"])
package main
import (
"context"
"fmt"
"os"
"github.com/baidubce/bce-qianfan-sdk/go/qianfan"
)
func main() {
// 使用安全认证AK/SK鉴权,通过环境变量初始化;替换下列示例中参数,安全认证Access Key替换your_iam_ak,Secret Key替换your_iam_sk
os.Setenv("QIANFAN_ACCESS_KEY", "your_iam_ak")
os.Setenv("QIANFAN_SECRET_KEY", "your_iam_sk")
// 使用自行发布的模型
chat := qianfan.NewChatCompletion(
qianfan.WithEndpoint("your_endpoint"),
)
resp, _ := chat.Do(
context.TODO(),
&qianfan.ChatCompletionRequest{
Messages: []qianfan.ChatCompletionMessage{
qianfan.ChatCompletionUserMessage("hi"),
},
},
)
fmt.Println(resp.Result)
}
import com.baidubce.qianfan.Qianfan;
import com.baidubce.qianfan.model.chat.ChatResponse;
public class Demo {
public static void main(String[] args) {
// 使用安全认证AK/SK鉴权,替换下列示例中参数,安全认证Access Key替换your_iam_ak,Secret Key替换your_iam_sk
Qianfan qianfan = new Qianfan("your_iam_ak", "your_iam_sk");
// 使用自行发布的模型,替换your_custom_endpoint
ChatResponse resp = qianfan.chatCompletion()
.endpoint("your_custom_endpoint")
.addMessage("user", "hi")
.execute();
System.out.println(resp.getResult());
}
}
import {ChatCompletion, setEnvVariable} from "@baiducloud/qianfan";
// 使用安全认证AK/SK鉴权,通过环境变量初始化;替换下列示例中参数,安全认证Access Key替换your_iam_ak,Secret Key替换your_iam_sk
setEnvVariable('QIANFAN_ACCESS_KEY','your_iam_ak');
setEnvVariable('QIANFAN_SECRET_KEY','your_iam_sk');
const client = new ChatCompletion({Endpoint: 'your_endpoint'});
async function main() {
const resp = await client.chat({
messages: [
{
role: 'user',
content: 'hi',
},
],
});
console.log(resp);
}
main();
返回示例(单轮)
{
'id': 'as-tc4zh3aqnj',
'object': 'chat.completion',
'created': 1719048276,
'result': " Hello! It's nice to meet you. Is there something I can help you with or would you like to chat for a bit?",
'is_truncated': False,
'need_clear_history': False,
'usage': {'prompt_tokens': 1, 'completion_tokens': 29, 'total_tokens': 30}
}
Hello! It's nice to meet you. Is there something I can help you with or would you like to chat for a bit?
Hello! It's nice to meet you. Is there something I can help you with or would you like to chat for a bit?
{
headers: {...},
id: 'as-pu7511p9e3',
object: 'chat.completion',
created: 1719049368,
result: " Hello! It's nice to meet you. Is there something I can help you with or would you like to chat for a bit?",
is_truncated: false,
need_clear_history: false,
usage: { prompt_tokens: 1, completion_tokens: 29, total_tokens: 30 }
}
调用示例(多轮)
import os
import qianfan
#通过环境变量初始化认证信息
# 【推荐】使用安全认证AK/SK鉴权
# 替换下列示例中参数,安全认证Access Key替换your_iam_ak,Secret Key替换your_iam_sk
os.environ["QIANFAN_ACCESS_KEY"] = "your_iam_ak"
os.environ["QIANFAN_SECRET_KEY"] = "your_iam_sk"
chat_comp = qianfan.ChatCompletion()
# 多轮对话
resp = chat_comp.do(model="Yi-34B-Chat", messages=[{
"role": "user",
"content": "hi"
},
{
"role": "assistant",
"content": "Hello! It's nice to meet you. Is there something I can help you with or would you like to chat for a bit?"
},
{
"role": "user",
"content": "Please introduce The Great Wall"
},
])
print(resp["body"])
package main
import (
"context"
"fmt"
"os"
"github.com/baidubce/bce-qianfan-sdk/go/qianfan"
)
func main() {
// 使用安全认证AK/SK鉴权,通过环境变量初始化;替换下列示例中参数,安全认证Access Key替换your_iam_ak,Secret Key替换your_iam_sk
os.Setenv("QIANFAN_ACCESS_KEY", "your_iam_ak")
os.Setenv("QIANFAN_SECRET_KEY", "your_iam_sk")
// 多轮对话
chat := qianfan.NewChatCompletion(
qianfan.WithModel("Yi-34B-Chat"),
)
resp, _ := chat.Do(
context.TODO(),
&qianfan.ChatCompletionRequest{
Messages: []qianfan.ChatCompletionMessage{
qianfan.ChatCompletionUserMessage("hi"),
qianfan.ChatCompletionAssistantMessage("Hello! It's nice to meet you. Is there something I can help you with or would you like to chat for a bit?"),
qianfan.ChatCompletionUserMessage("Please introduce The Great Wall"),
},
},
)
fmt.Println(resp.Result)
}
import com.baidubce.qianfan.Qianfan;
import com.baidubce.qianfan.model.chat.ChatResponse;
public class Demo {
public static void main(String[] args) {
// 使用安全认证AK/SK鉴权,替换下列示例中参数,安全认证Access Key替换your_iam_ak,Secret Key替换your_iam_sk
Qianfan qianfan = new Qianfan("your_iam_ak", "your_iam_sk");
// 多轮对话
ChatResponse resp = qianfan.chatCompletion()
.model("Yi-34B-Chat")
.addMessage("user", "hi")
.addMessage("assistant", "Hello! It's nice to meet you. Is there something I can help you with or would you like to chat for a bit?")
.addMessage("user", "Please introduce The Great Wall")
.execute();
System.out.println(resp.getResult());
}
}
import {ChatCompletion, setEnvVariable} from "@baiducloud/qianfan";
// 使用安全认证AK/SK鉴权,通过环境变量初始化;替换下列示例中参数,安全认证Access Key替换your_iam_ak,Secret Key替换your_iam_sk
setEnvVariable('QIANFAN_ACCESS_KEY','your_iam_ak');
setEnvVariable('QIANFAN_SECRET_KEY','your_iam_sk');
const client = new ChatCompletion();
async function main() {
const resp = await client.chat({
messages: [
{
role: 'user',
content: 'hi',
},
{
role: "assistant",
content: "Hello! It's nice to meet you. Is there something I can help you with or would you like to chat for a bit?"
},
{
role: "user",
"content": "Please introduce The Great Wall"
},
],
},'Yi-34B-Chat');
console.log(resp);
}
main();
返回示例(多轮)
{
'id': 'as-i0cxzhk8qa',
'object': 'chat.completion',
'created': 1719048428,
'result': " The Great Wall of China is a series of fortifications made of stone, brick, tamped earth, and other materials, built across the northern borders of China to protect the Chinese Empire from invasions by nomadic tribes. It was constructed over the course of many centuries, with the first versions of the wall being built as early as the 7th century BC. The wall was continuously expanded and fortified over time, with the most famous and impressive sections being built during the Ming Dynasty (1368-1644).\n\nThe Great Wall stretches for over 4,000 miles, making it one of the longest structures ever built. It was originally built to be up to 30 feet high and up to 25 feet wide, with watchtowers and beacon towers spaced at regular intervals. The wall was designed to be impenetrable, with its sheer size and complexity making it nearly impossible for invading armies to breach.\n\nThe Great Wall is not only an engineering marvel but also a testament to the ingenuity and determination of the Chinese people. It is estimated that over a million workers were involved in its construction, and it is believed that thousands of workers died during its construction.\n\nThe Great Wall is now a UNESCO World Heritage Site and is considered one of the most famous landmarks in the world. It is a popular tourist destination, with visitors from all over the world coming to marvel at its grandeur and historical significance.\n\nIn addition to its military and historical significance, the Great Wall is also a symbol of Chinese culture and civilization. It is a reminder of the country's rich history and its ability to endure and adapt over time.\n\nOverall, the Great Wall of China is an incredible feat of engineering, a testament to the ingenuity and determination of the Chinese people, and a symbol of Chinese culture and civilization.",
'is_truncated': False,
'need_clear_history': False,
'usage': {'prompt_tokens': 34, 'completion_tokens': 396, 'total_tokens': 430}
}
The Great Wall of China is a series of fortifications made of stone, brick, tamped earth, and other materials, built across the northern borders of China to protect the Chinese Empire from invasions by nomadic tribes. It was constructed over the course of many centuries, with the first versions of the wall being built as early as the 7th century BC. The wall was continuously expanded and fortified over time, with the most famous and impressive sections being built during the Ming Dynasty (1368-1644).
The Great Wall stretches for over 4,000 miles, making it one of the longest structures ever built. It passes through mountains, deserts, and plains, and was originally built to be up to 30 feet high and up to 25 feet wide. The wall was equipped with watchtowers, barracks, and beacon towers, which allowed soldiers to communicate and respond quickly to threats.
The Great Wall is not only an impressive feat of engineering and construction, but it is also a testament to the ingenuity and determination of the Chinese people. It has become a symbol of China's rich history and culture, and is considered one of the greatest architectural achievements in human history.
The Great Wall has been designated as a UNESCO World Heritage Site, and is one of the most popular tourist attractions in China. Visitors can walk or hike along certain sections of the wall, and many people attempt to walk the entire length of the wall, which is considered a challenging but rewarding experience.
Overall, the Great Wall of China is a remarkable structure that has stood the test of time, and continues to inspire awe and wonder in people from all over the world.
The Great Wall of China is a series of fortifications made of stone, brick, tamped earth, and other materials, built across the northern borders of China to protect the Chinese Empire from invasions by nomadic tribes. It was constructed over the course of many centuries, with the first versions of the wall being built as early as the 7th century BC. The wall was continuously expanded and fortified over time, with the most famous and impressive sections being built during the Ming Dynasty (1368-1644).
The Great Wall stretches for over 4,000 miles, making it one of the longest structures ever built. It passes through mountains, deserts, and plains, and was originally built to be up to 30 feet high and up to 25 feet wide. The wall was equipped with watchtowers, barracks, and signal towers, which allowed soldiers to communicate and respond quickly to any threats.
The Great Wall is not only an engineering marvel but also a testament to the ingenuity and determination of the Chinese people. It has become a symbol of China's rich history and culture, and is considered one of the greatest architectural achievements in history.
The Great Wall has been designated as a UNESCO World Heritage Site, and is one of the most popular tourist attractions in China. Visitors can walk or hike along the wall, and many sections have been restored and renovated to their original glory. The wall's historical significance and breathtaking scenery make it a must-see destination for anyone interested in history, culture, and natural beauty.
{
headers: {...},
id: 'as-9u4yb2fh3f',
object: 'chat.completion',
created: 1719049493,
result: ' The Great Wall of China is a series of fortifications made of stone, brick, tamped earth, and other materials, built across the northern borders of China to protect the Chinese Empire from invasions by nomadic tribes. It was constructed over the course of many centuries, with the first versions of the wall being built as early as the 7th century BC. The wall was continuously expanded and fortified over time, with the most famous and impressive sections being built during the Ming Dynasty (1368-1644).\n' +
'\n' +
'The Great Wall stretches for over 4,000 miles, making it one of the longest structures ever built. It spans across 15 provinces and regions in China, winding its way through mountains, deserts, and plains. The wall was built to be impenetrable, with watchtowers and beacon towers spaced at regular intervals to alert soldiers of any impending invasions.\n' +
'\n' +
"The Great Wall is not only an engineering marvel but also a testament to the ingenuity and determination of the Chinese people. It has become a symbol of China's rich history and culture, and is considered one of the Seven Wonders of the Medieval World.\n" +
'\n' +
'The wall was built to serve several purposes, including:\n' +
'\n' +
'1. Military defense: The wall was built to protect China from invasions by nomadic tribes from the north, such as the Xiongnu, Mongols, and Manchu.\n' +
'2. Border control: The wall helped to regulate trade and immigration, and prevented smuggling and other illegal activities.\n' +
'3. Communication: The wall facilitated communication between different regions of China, allowing messages to be transmitted quickly and efficiently.\n' +
"4. Symbol of power: The wall was a symbol of the Chinese Empire's power and prestige, showcasing the strength and grandeur of the Chinese state.\n" +
'\n' +
'The Great Wall has been the subject of numerous legends, stories, and poems throughout Chinese history. It has also been the focus of many restoration and preservation efforts, with parts of the wall being designated as UNESCO World Heritage Sites. Today, the Great Wall remains a popular tourist destination, attracting millions of visitors each year who come to marvel at its grandeur and historical significance.',
is_truncated: false,
need_clear_history: false,
usage: { prompt_tokens: 34, completion_tokens: 474, total_tokens: 508 }
}
调用示例(流式)
import os
import qianfan
#【推荐】使用安全认证AK/SK鉴权,通过环境变量初始化认证信息
# 替换下列示例中参数,安全认证Access Key替换your_iam_ak,Secret Key替换your_iam_sk
os.environ["QIANFAN_ACCESS_KEY"] = "your_iam_ak"
os.environ["QIANFAN_SECRET_KEY"] = "your_iam_sk"
chat_comp = qianfan.ChatCompletion()
resp = chat_comp.do(model="Yi-34B-Chat", messages=[{
"role": "user",
"content": "hi"
}], stream=True)
for r in resp:
print(r["body"])
package main
import (
"context"
"fmt"
"os"
"github.com/baidubce/bce-qianfan-sdk/go/qianfan"
)
func main() {
// 使用安全认证AK/SK鉴权,通过环境变量初始化;替换下列示例中参数,安全认证Access Key替换your_iam_ak,Secret Key替换your_iam_sk
os.Setenv("QIANFAN_ACCESS_KEY", "your_iam_ak")
os.Setenv("QIANFAN_SECRET_KEY", "your_iam_sk")
chat := qianfan.NewChatCompletion(
qianfan.WithModel("Yi-34B-Chat"),
)
resp, _ := chat.Stream( // Stream 启用流式返回,参数与 Do 相同
context.TODO(),
&qianfan.ChatCompletionRequest{
Messages: []qianfan.ChatCompletionMessage{
qianfan.ChatCompletionUserMessage("hi"),
},
},
)
for {
r, err := resp.Recv()
if err != nil {
panic(err)
}
if resp.IsEnd { // 判断是否结束
break
}
fmt.Println(r.Result)
}
}
import com.baidubce.qianfan.Qianfan;
import com.google.gson.Gson;
public class Demo {
public static void main(String[] args) {
Gson gson = new Gson();
// 使用安全认证AK/SK鉴权,替换下列示例中参数,安全认证Access Key替换your_iam_ak,Secret Key替换your_iam_sk
Qianfan qianfan = new Qianfan("your_iam_ak", "your_iam_sk");
qianfan.chatCompletion()
.model("Yi-34B-Chat")
.addMessage("user", "hi")
// 启用流式返回
.executeStream()
.forEachRemaining(chunk -> System.out.print(gson.toJson(chunk)));
}
}
import {ChatCompletion, setEnvVariable} from "@baiducloud/qianfan";
// 使用安全认证AK/SK鉴权,通过环境变量初始化;替换下列示例中参数,安全认证Access Key替换your_iam_ak,Secret Key替换your_iam_sk
setEnvVariable('QIANFAN_ACCESS_KEY','your_iam_ak');
setEnvVariable('QIANFAN_SECRET_KEY','your_iam_sk');
const client = new ChatCompletion();
async function main() {
const resp = await client.chat({
messages: [
{
role: 'user',
content: 'hi',
},
],
stream: true, //启用流式返回
}, 'Yi-34B-Chat');
for await (const chunk of resp) {
console.log(chunk);
}
}
main();
返回示例(流式)
{'id': 'as-xs2v0tmhbj', 'object': 'chat.completion', 'created': 1719048136, 'sentence_id': 0, 'is_end': False, 'is_truncated': False, 'result': " Hello! It's nice to meet you. Is there something I can help you with ", 'need_clear_history': False, 'usage': {'prompt_tokens': 1, 'completion_tokens': 0, 'total_tokens': 1}}
{'id': 'as-xs2v0tmhbj', 'object': 'chat.completion', 'created': 1719048136, 'sentence_id': 1, 'is_end': True, 'is_truncated': False, 'result': 'or would you like to chat for a bit?', 'need_clear_history': False, 'usage': {'prompt_tokens': 1, 'completion_tokens': 29, 'total_tokens': 30}}
Hello! It's nice to meet you. Is there something I can help you with
or would you like to chat for a bit?
{"sentenceId":0,"isEnd":false,"isTruncated":false,"result":" Hello! It\u0027s nice to meet you. Is there something I can help you with ","needClearHistory":false,"usage":{"promptTokens":1,"completionTokens":0,"totalTokens":1},"id":"as-mp0kmpjtpg","object":"chat.completion","created":1719050100,"headers":{...}}
{"sentenceId":1,"isEnd":true,"isTruncated":false,"result":"or would you like to chat for a bit?","needClearHistory":false,"usage":{"promptTokens":1,"completionTokens":29,"totalTokens":30},"id":"as-mp0kmpjtpg","object":"chat.completion","created":1719050101,"headers":{...}}
{
headers: {...},
id: 'as-tq2bg16sg2',
object: 'chat.completion',
created: 1719050184,
sentence_id: 0,
is_end: false,
is_truncated: false,
result: " Hello! It's nice to meet you. Is there something I can help you with ",
need_clear_history: false,
usage: { prompt_tokens: 1, completion_tokens: 0, total_tokens: 1 }
}
{
headers: {...},
id: 'as-tq2bg16sg2',
object: 'chat.completion',
created: 1719050185,
sentence_id: 1,
is_end: true,
is_truncated: false,
result: 'or would you like to chat for a bit?',
need_clear_history: false,
usage: { prompt_tokens: 1, completion_tokens: 29, total_tokens: 30 }
}
请求参数
注意:以下为Python SDK参数说明,其他SDK参数相关说明请参考Go SDK-对话Chat参数相关说明、Java SDK参数相关说明、Node.js SDK参数相关说明。
名称 | 类型 | 必填 | 描述 |
---|---|---|---|
messages | List[dict] | 是 | 聊天上下文信息。说明: (1)messages成员不能为空,1个成员表示单轮对话,多个成员表示多轮对话 (2)最后一个message为当前请求的信息,前面的message为历史对话信息 (3)必须为奇数个成员,成员中message的role必须依次为user、assistant (4)message中的content总长度不能超过8000 个字符 |
model | string | 否 | 模型名称,用于指定平台支持预置服务的模型,说明: (1)如果调用预置服务,即调用本文API,该字段必填,且为固定值Yi-34B-Chat (2)如果指定用户自行发布的模型服务,该字段不填写,需填写endpoint字段,详见参数endpoint说明 |
endpoint | string | 否 | 用于指定用户自行发布的模型服务,说明: (1)如果指定用户自行发布的模型服务,endpoint字段为必填 (2)该字段值可以通过查看服务地址获取:打开模型服务-模型推理-我的服务页面,选择创建的服务-点击详情页查看服务地址,endpoint值为 https://aip.baidubce.com/rpc/2.0/ai_custom/v1/wenxinworkshop/chat/ 后面的地址,如下图所示(3)注意:在创建服务页面,选择模型后,API地址会自动新增个后缀。例如选择某模型,输入API地址为“test1”,endpoint的取值即为“ngxxxol8_test1”,如下图所示,如何发布服务请参考发布平台预置的模型服务 |
temperature | float | 否 | 说明: (1)较高的数值会使输出更加随机,而较低的数值会使其更加集中和确定 (2) 范围(0, 1.0],不能为0 |
top_k | float | 否 | Top-K 采样参数,在每轮token生成时,保留k个概率最高的token作为候选。说明: (1)影响输出文本的多样性,取值越大,生成文本的多样性越强 (2)取值范围:正整数 |
top_p | float | 否 | 说明: (1)影响输出文本的多样性,取值越大,生成文本的多样性越强 (2)取值范围 [0, 1.0] |
penalty_score | float | 否 | 通过对已生成的token增加惩罚,减少重复生成的现象。说明: (1)值越大表示惩罚越大 (2)取值范围:[1.0, 2.0] |
stream | bool | 否 | 是否以流式接口的形式返回数据,默认False · True:是,以流式接口的形式返回数据 · False:否 |
retry_count | int | 否 | 重试次数,默认1次 |
request_timeout | float | 否 | 请求超时时间,默认60秒 |
backoff_factor | float | 否 | 请求重试参数,用于指定重试的策略,默认为0 |
stop | List[string] | 否 | 生成停止标识。当模型生成结果以stop中某个元素结尾时,停止文本生成。说明: (1)每个元素长度不超过20字符 (2)最多4个元素 |
user_id | string | 否 | 表示最终用户的唯一标识符 |
message说明
名称 | 类型 | 描述 |
---|---|---|
role | string | 当前支持以下: user: 表示用户 assistant: 表示对话助手 |
content | string | 对话内容,不能为空 |
返回参数
名称 | 类型 | 描述 |
---|---|---|
id | string | 本轮对话的id |
object | string | 回包类型。 chat.completion:多轮对话返回 |
created | int | 时间戳 |
sentence_id | int | 表示当前子句的序号。只有在流式接口模式下会返回该字段 |
is_end | bool | 表示当前子句是否是最后一句。只有在流式接口模式下会返回该字段 |
is_truncated | bool | 当前生成的结果是否被截断 |
result | string | 对话返回结果 |
need_clear_history | bool | 表示用户输入是否存在安全,是否关闭当前会话,清理历史会话信息 True:是,表示用户输入存在安全风险,建议关闭当前会话,清理历史会话信息 False:否,表示用户输入无安全风险 |
ban_round | int | 当need_clear_history为True时,此字段会告知第几轮对话有敏感信息,如果是当前问题,ban_round=-1 |
usage | usage | token统计信息 |
usage说明
名称 | 类型 | 描述 |
---|---|---|
prompt_tokens | int | 问题tokens数 |
completion_tokens | int | 回答tokens数 |
total_tokens | int | tokens总数 |
HTTP调用
鉴权说明
本文API,支持2种鉴权方式。不同鉴权方式,调用方式不同,使用Header、Query参数不同,详见本文请求说明。开发者可以选择以下任一种方式进行鉴权。
请求说明
- 基本信息
请求地址: https://aip.baidubce.com/rpc/2.0/ai_custom/v1/wenxinworkshop/chat/yi_34b_chat
请求方式: POST
- Header参数
根据不同鉴权方式,查看对应Header参数。
访问凭证access_token鉴权
名称 | 类型 | 必填 | 描述 |
---|---|---|---|
Content-Type | string | 是 | 固定值application/json |
基于安全认证AK/SK进行签名计算鉴权
名称 | 类型 | 必填 | 描述 |
---|---|---|---|
Content-Type | string | 是 | 固定值application/json |
x-bce-date | string | 否 | 当前时间,遵循ISO8601规范,格式如2016-04-06T08:23:49Z |
Authorization | string | 是 | 用于验证请求合法性的认证信息,更多内容请参考鉴权认证机制,签名工具可参考IAM签名工具 |
- Query参数
只有访问凭证access_token鉴权方式,需使用Query参数。
访问凭证access_token鉴权
名称 | 类型 | 必填 | 描述 |
---|---|---|---|
access_token | string | 是 | 通过API Key和Secret Key获取的access_token,参考Access Token获取 |
- Body参数
名称 | 类型 | 必填 | 描述 |
---|---|---|---|
messages | List(message) | 是 | 聊天上下文信息。说明: (1)messages成员不能为空,1个成员表示单轮对话,多个成员表示多轮对话 (2)最后一个message为当前请求的信息,前面的message为历史对话信息 (3)必须为奇数个成员,成员中message的role必须依次为user、assistant (4)message中的content总长度不能超过8000 个字符 |
stream | bool | 否 | 是否以流式接口的形式返回数据,默认false |
temperature | float | 否 | 说明: (1)较高的数值会使输出更加随机,而较低的数值会使其更加集中和确定 (2)范围 (0, 1.0],不能为0 |
top_k | int | 否 | Top-K 采样参数,在每轮token生成时,保留k个概率最高的token作为候选。说明: (1)影响输出文本的多样性,取值越大,生成文本的多样性越强 (2)取值范围:正整数 |
top_p | float | 否 | 说明: (1)影响输出文本的多样性,取值越大,生成文本的多样性越强 (2)取值范围 [0, 1.0] |
penalty_score | float | 否 | 通过对已生成的token增加惩罚,减少重复生成的现象。说明: (1)值越大表示惩罚越大 (2)取值范围:[1.0, 2.0] |
stop | List(String) | 否 | 生成停止标识。当模型生成结果以stop中某个元素结尾时,停止文本生成。说明: (1)每个元素长度不超过20字符。 (2)最多4个元素 |
user_id | string | 否 | 表示最终用户的唯一标识符 |
message说明
名称 | 类型 | 描述 |
---|---|---|
role | string | 当前支持以下: user: 表示用户 assistant: 表示对话助手 |
content | string | 对话内容,不能为空 |
响应说明
名称 | 类型 | 描述 |
---|---|---|
id | string | 本轮对话的id |
object | string | 回包类型。 chat.completion:多轮对话返回 |
created | int | 时间戳 |
sentence_id | int | 表示当前子句的序号。只有在流式接口模式下会返回该字段 |
is_end | bool | 表示当前子句是否是最后一句。只有在流式接口模式下会返回该字段 |
is_truncated | bool | 当前生成的结果是否被截断 |
result | string | 对话返回结果 |
need_clear_history | bool | 表示用户输入是否存在安全,是否关闭当前会话,清理历史会话信息 true:是,表示用户输入存在安全风险,建议关闭当前会话,清理历史会话信息 false:否,表示用户输入无安全风险 |
ban_round | int | 当need_clear_history为true时,此字段会告知第几轮对话有敏感信息,如果是当前问题,ban_round=-1 |
usage | usage | token统计信息 |
usage说明
名称 | 类型 | 描述 |
---|---|---|
prompt_tokens | int | 问题tokens数 |
completion_tokens | int | 回答tokens数 |
total_tokens | int | tokens总数 |
注意 :同步模式和流式模式,响应参数返回不同,详细内容参考示例描述。
- 同步模式下,响应参数为以上字段的完整json包。
- 流式模式下,各字段的响应参数为 data: {响应参数}。
请求示例(单轮)
以访问凭证access_token鉴权方式为例,说明如何调用API,示例如下。
# 步骤一,获取access_token,替换下列示例中的应用API Key与应用Secret Key
curl 'https://aip.baidubce.com/oauth/2.0/token?grant_type=client_credentials&client_id=[应用API Key]&client_secret=[应用Secret Key]'
# 步骤二,调用本文API,使用步骤一获取的access_token,替换下列示例中的“调用接口获取的access_token”
curl -XPOST 'https://aip.baidubce.com/rpc/2.0/ai_custom/v1/wenxinworkshop/chat/yi_34b_chat?access_token=[步骤一调用接口获取的access_token]' -d '{
"messages": [
{"role":"user","content":"hi"}
]
}' | iconv -f utf-8 -t utf-8
import requests
import json
def get_access_token():
"""
使用 API Key,Secret Key 获取access_token,替换下列示例中的应用API Key、应用Secret Key
"""
url = "https://aip.baidubce.com/oauth/2.0/token?grant_type=client_credentials&client_id=[应用API Key]&client_secret=[应用Secret Key]"
payload = json.dumps("")
headers = {
'Content-Type': 'application/json',
'Accept': 'application/json'
}
response = requests.request("POST", url, headers=headers, data=payload)
return response.json().get("access_token")
def main():
url = "https://aip.baidubce.com/rpc/2.0/ai_custom/v1/wenxinworkshop/chat/yi_34b_chat?access_token=" + get_access_token()
payload = json.dumps({
"messages": [
{
"role": "user",
"content": "hi"
}
]
})
headers = {
'Content-Type': 'application/json'
}
response = requests.request("POST", url, headers=headers, data=payload)
print(response.text)
if __name__ == '__main__':
main()
响应示例(单轮)
{
"id": "as-91sb51gy1f",
"object": "chat.completion",
"created": 1703835221,
"result": "Hello! How can I assist you today? If you have any questions or need information on a specific topic, feel free to ask.",
"is_truncated": false,
"need_clear_history": false,
"usage": {
"prompt_tokens": 1,
"completion_tokens": 27,
"total_tokens": 28
}
}
请求示例(多轮)
# 步骤一,获取access_token,替换下列示例中的应用API Key与应用Secret Key
curl 'https://aip.baidubce.com/oauth/2.0/token?grant_type=client_credentials&client_id=[应用API Key]&client_secret=[应用Secret Key]'
# 步骤二,调用本文API,使用步骤一获取的access_token,替换下列示例中的“调用接口获取的access_token”
curl -XPOST 'https://aip.baidubce.com/rpc/2.0/ai_custom/v1/wenxinworkshop/chat/yi_34b_chat?access_token=[步骤一调用接口获取的access_token]' -d '{
"messages": [
{"role":"user","content":"hi"},
{"role":"assistant","content":"Hello! How can I assist you today? If you have any questions or need information on a specific topic, feel free to ask."},
{"role":"user","content": "介绍下长城"}
]
}' | iconv -f utf-8 -t utf-8
import requests
import json
def get_access_token():
"""
使用 API Key,Secret Key 获取access_token,替换下列示例中的应用API Key、应用Secret Key
"""
url = "https://aip.baidubce.com/oauth/2.0/token?grant_type=client_credentials&client_id=[应用API Key]&client_secret=[应用Secret Key]"
payload = json.dumps("")
headers = {
'Content-Type': 'application/json',
'Accept': 'application/json'
}
response = requests.request("POST", url, headers=headers, data=payload)
return response.json().get("access_token")
def main():
url = "https://aip.baidubce.com/rpc/2.0/ai_custom/v1/wenxinworkshop/chat/yi_34b_chat?access_token=" + get_access_token()
payload = json.dumps({
"messages": [
{
"role": "user",
"content": "hi"
},
{
"role": "assistant",
"content": "Hello! How can I assist you today? If you have any questions or need information on a specific topic, feel free to ask."
},
{
"role": "user",
"content": "Please introduce The Great Wall"
}
]
})
headers = {
'Content-Type': 'application/json'
}
response = requests.request("POST", url, headers=headers, data=payload)
print(response.text)
if __name__ == '__main__':
main()
响应示例(多轮)
{
"id": "as-ek0685jkga",
"object": "chat.completion",
"created": 1703844394,
"result": "The Great Wall of China is one of the most iconic and impressive structures in the world, renowned for its length, historical significance, and architectural achievements. It is a series of fortifications built along the northern border of China to protect the country from invasions by nomadic tribes. The construction of the Great Wall began in the 7th century BC during the Zhou Dynasty and continued into the Qin Dynasty (221–207 BC), when the first Emperor of Qin, Qin Shi Huang, connected and expanded existing walls to unify the defenses of the newly formed Chinese state.\n\nThe most famous sections of the Great Wall were built during the Ming Dynasty (1368–1644), as the Ming emperors sought to defend their northern border from the Mongols. The Ming construction is the one that is best preserved and most often visited by tourists today.\n\nThe Great Wall stretches from the Jiayuguan Pass in the Gansu Province in the west to the Hushan Mountain in Liaoning Province in the east, passing through several provinces and municipalities. The total length of the wall is estimated to be over 21,000 kilometers (13,000 miles), although the length of the sections built during the Ming Dynasty is generally given as approximately 8,850 kilometers (5,500 miles).\n\nThe wall is not a single, continuous structure but a series of walls, fortifications, and watchtowers that served multiple purposes, including military defense, border control, and trade regulation. The walls were built using local materials, such as brick, stone, and rammed earth, and were often built on steep and difficult terrain to enhance their defensive capabilities.\n\nThe Great Wall of China is not only a symbol of China's ancient civilization and military prowess but also a testament to the engineering and construction capabilities of the time. It was added to the UNESCO World Heritage List in 1987 and remains one of the most popular tourist attractions in the world.",
"is_truncated": false,
"need_clear_history": false,
"usage": {
"prompt_tokens": 33,
"completion_tokens": 433,
"total_tokens": 466
}
}
请求示例(流式)
# 步骤一,获取access_token,替换下列示例中的应用API Key与应用Secret Key
curl 'https://aip.baidubce.com/oauth/2.0/token?grant_type=client_credentials&client_id=[应用API Key]&client_secret=[应用Secret Key]'
# 步骤二,调用本文API,使用步骤一获取的access_token,替换下列示例中的“调用接口获取的access_token”
curl -XPOST 'https://aip.baidubce.com/rpc/2.0/ai_custom/v1/wenxinworkshop/chat/yi_34b_chat?access_token=[步骤一调用接口获取的access_token]' -d '{
"messages": [
{"role":"user", "content": "推荐一些中国自驾游路线"}
],
"stream": true
}'
import requests
import json
def get_access_token():
"""
使用 API Key,Secret Key 获取access_token,替换下列示例中的应用API Key、应用Secret Key
"""
url = "https://aip.baidubce.com/oauth/2.0/token?grant_type=client_credentials&client_id=[应用API Key]&client_secret=[应用Secret Key]"
payload = json.dumps("")
headers = {
'Content-Type': 'application/json',
'Accept': 'application/json'
}
response = requests.request("POST", url, headers=headers, data=payload)
return response.json().get("access_token")
def main():
url = "https://aip.baidubce.com/rpc/2.0/ai_custom/v1/wenxinworkshop/chat/yi_34b_chat?access_token=" + get_access_token()
payload = json.dumps({
"messages": [
{
"role": "user",
"content": "推荐一些中国自驾游路线"
}
],
"stream": True
})
headers = {
'Content-Type': 'application/json'
}
response = requests.request("POST", url, headers=headers, data=payload, stream=True)
for line in response.iter_lines():
print(line.decode("UTF-8"))
if __name__ == '__main__':
main()
响应示例(流式)
HTTP/1.1 200 OK
Date: Mon, 12 Apr 2021 06:27:55 GMT
Content-Type: text/event-stream;charset=utf-8
Cache-Control: no-cache
Statement: AI-generated
data: {"id":"as-tr5ugangar","object":"chat.completion","created":1703835658,"sentence_id":0,"is_end":false,"is_truncated":false,"result":"中国幅员辽阔,拥有多样的自然风光和丰富的文化遗产,非常适合自驾游。以下是一些推荐的中国自驾游路线:\n\n1. 丝绸之路:从西安出发,沿着古代丝绸之路的","need_clear_history":false,"usage":{"prompt_tokens":6,"completion_tokens":0,"total_tokens":6}}
data: {"id":"as-tr5ugangar","object":"chat.completion","created":1703835660,"sentence_id":1,"is_end":false,"is_truncated":false,"result":"路线,经过敦煌、嘉峪关、张掖、武威等地,最终到达新疆的乌鲁木齐。这条路线可以让你领略到中国的历史和文化。\n\n2. 川藏线:从","need_clear_history":false,"usage":{"prompt_tokens":6,"completion_tokens":0,"total_tokens":6}}
data: {"id":"as-tr5ugangar","object":"chat.completion","created":1703835663,"sentence_id":2,"is_end":false,"is_truncated":false,"result":"成都出发,沿着318国道前往西藏拉萨,途中经过康定、理塘、芒康、波密等地。这条路线被誉为中国的景观大道,沿途的自然风光非常壮观。\n\n3. 云南大理","need_clear_history":false,"usage":{"prompt_tokens":6,"completion_tokens":0,"total_tokens":6}}
data: {"id":"as-tr5ugangar","object":"chat.completion","created":1703835665,"sentence_id":3,"is_end":false,"is_truncated":false,"result":"-丽江-香格里拉:从大理出发,经过丽江古城,最终到达香格里拉。这条路线可以让你感受到云南的民族风情和美丽的自然风光。\n\n4. 海南环岛","need_clear_history":false,"usage":{"prompt_tokens":6,"completion_tokens":0,"total_tokens":6}}
data: {"id":"as-tr5ugangar","object":"chat.completion","created":1703835667,"sentence_id":4,"is_end":false,"is_truncated":false,"result":":从海口出发,沿着海南岛的沿海公路环岛一周,途中经过三亚、万宁、琼海等地。这条路线适合喜欢海滩和热带风情的人。\n\n5. 东北","need_clear_history":false,"usage":{"prompt_tokens":6,"completion_tokens":0,"total_tokens":6}}
data: {"id":"as-tr5ugangar","object":"chat.completion","created":1703835669,"sentence_id":5,"is_end":false,"is_truncated":false,"result":"边境线:从大连出发,沿着中国东北的边境线,经过丹东、长白山、延吉、珲春等地。这条路线可以让你感受到东北的森林、湖泊和","need_clear_history":false,"usage":{"prompt_tokens":6,"completion_tokens":0,"total_tokens":6}}
data: {"id":"as-tr5ugangar","object":"chat.completion","created":1703835671,"sentence_id":6,"is_end":false,"is_truncated":false,"result":"边境风情。\n\n6. 新疆喀纳斯:从乌鲁木齐出发,前往喀纳斯自然保护区,途中经过天山天池、五彩滩等地。这条路线可以让你领略到新疆的壮","need_clear_history":false,"usage":{"prompt_tokens":6,"completion_tokens":0,"total_tokens":6}}
data: {"id":"as-tr5ugangar","object":"chat.completion","created":1703835673,"sentence_id":7,"is_end":false,"is_truncated":false,"result":"丽风光和独特的民族文化。\n\n7. 内蒙古呼伦贝尔:从海拉尔出发,前往呼伦贝尔草原,途中经过额尔古纳、根河、满洲里","need_clear_history":false,"usage":{"prompt_tokens":6,"completion_tokens":0,"total_tokens":6}}
data: {"id":"as-tr5ugangar","object":"chat.completion","created":1703835675,"sentence_id":8,"is_end":false,"is_truncated":false,"result":"等地。这条路线适合喜欢草原和边境风光的人。\n\n8. 福建土楼:从厦门出发,前往南靖土楼和永定土楼,这些是中国的传统客","need_clear_history":false,"usage":{"prompt_tokens":6,"completion_tokens":0,"total_tokens":6}}
data: {"id":"as-tr5ugangar","object":"chat.completion","created":1703835677,"sentence_id":9,"is_end":false,"is_truncated":false,"result":"家建筑,非常具有特色。\n\n9. 湖北武当山:从十堰出发,前往武当山,这是中国的道教名山,山上的建筑群非常壮观。\n\n10. 安徽黄山","need_clear_history":false,"usage":{"prompt_tokens":6,"completion_tokens":0,"total_tokens":6}}
data: {"id":"as-tr5ugangar","object":"chat.completion","created":1703835679,"sentence_id":10,"is_end":false,"is_truncated":false,"result":":从黄山市出发,前往黄山风景区,这是中国的四大名山之一,以其奇特的松树、怪石、云海和温泉闻名。\n\n在规划自驾游路线时,需要","need_clear_history":false,"usage":{"prompt_tokens":6,"completion_tokens":0,"total_tokens":6}}
data: {"id":"as-tr5ugangar","object":"chat.completion","created":1703835681,"sentence_id":11,"is_end":true,"is_truncated":false,"result":"考虑路程距离、路况、天气、个人兴趣等因素,确保行程安全和愉快。同时,也要注意遵守交通规则,确保车辆和人员的安全。","need_clear_history":false,"usage":{"prompt_tokens":6,"completion_tokens":511,"total_tokens":517}}
错误码
如果请求错误,服务器返回的JSON文本包含以下参数。
名称 | 描述 |
---|---|
error_code | 错误码 |
error_msg | 错误描述信息,帮助理解和解决发生的错误 |
例如Access Token失效返回以下内容,需要重新获取新的Access Token再次请求。
{
"error_code": 110,
"error_msg": "Access token invalid or no longer valid"
}
千帆大模型平台相关错误码,请查看错误码说明。