续写对话
更新时间:2024-11-15
千帆ModelBuilder支持HuggingFace Transformer架构的自定义大模型导入。本文介绍了自定义导入模型发布为服务后,使用续写模式调用相关API说明。如何导入并部署第三方模型,请查看快速导入并部署第三方模型。
续写模式
使用续写模式,创建completion,发起一次文本续写请求。
使用说明
本文API支持通过Python SDK、Go SDK、Java SDK 和 Node.js SDK调用,调用流程请参考SDK安装及使用流程。
SDK调用
调用示例(单轮)
对于用户快速部署自行发布的模型服务,通过使用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"
comp = qianfan.Completion()
# 使用自行发布的模型
resp = comp.do(endpoint="your_custom_endpoint", prompt="Introduce the city Beijing")
# 可以通过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.NewCompletion(
qianfan.WithEndpoint("your_endpoint"),
)
resp, _ := chat.Do(
context.TODO(),
&qianfan.CompletionRequest{
Prompt: "Introduce the city Beijing",
},
)
fmt.Println(resp.Result)
}
import com.baidubce.qianfan.Qianfan;
import com.baidubce.qianfan.model.completion.CompletionResponse;
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");
// 使用自行发布的模型
CompletionResponse resp = qianfan.completion()
.endpoint("your_custom_endpoint")
.prompt("hello")
.execute();
System.out.print(resp.getResult());
}
}
import {Completions, setEnvVariable} from "@baiducloud/qianfan";
const client = new Completions({ QIANFAN_ACCESS_KEY: 'your_iam_ak', QIANFAN_SECRET_KEY: 'your_iam_sk', Endpoint: 'your_endpoint' });
async function main() {
const resp = await client.completions({
prompt: 'Introduce the city Beijing',
});
console.log(resp);
}
main();
返回示例(单轮)
{
"id": "as-rq3wwusja8",
"object": "completion",
"created": 1693811110,
"result": " China.\nBeijing is the capital city of China and is located in the northern part of the country. It is the largest city in China and is known for its modern architecture, cultural heritage, and historical significance. The city is home to many famous landmarks, such as the Forbidden City, Tiananmen Square, and the Great Wall of China. It is also known for its vibrant nightlife, delicious food, and stunning scenery.",
"is_safe": 1,
"usage": {
"prompt_tokens": 5,
"completion_tokens": 92,
"total_tokens": 97
}
}
China.Beijing is the capital city of China and is located in the northern part of the country. It is the largest city in China and is known for its modern architecture, cultural heritage, and historical significance. The city is home to many famous landmarks, such as the Forbidden City, Tiananmen Square, and the Great Wall of China. It is also known for its vibrant nightlife, delicious food, and stunning scenery.
Hello! It's nice to meet you. Is there something I can help you with or would you like to chat?
{
id: 'as-hfmv5mvdim',
object: 'chat.completion',
created: 1709779789,
result: 'China.\nBeijing is the capital city of China and is located in the northern part of the country. It is the largest city in China and is known for its modern architecture, cultural heritage, and historical significance. The city is home to many famous landmarks, such as the Forbidden City, Tiananmen Square, and the Great Wall of China. It is also known for its vibrant nightlife, delicious food, and stunning scenery.',
is_safe: 1,
usage: { prompt_tokens: 5, completion_tokens: 92, total_tokens: 97 }
}
调用示例(流式)
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"
comp = qianfan.Completion()
# 续写功能同样支持流式调用
resp = comp.do(endpoint="your_custom_endpoint", prompt="Introduce the city Beijing", 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.NewCompletion(
qianfan.WithEndpoint("your_endpoint"),
)
resp, _ := chat.Stream( // Stream 启用流式返回,参数与 Do 相同
context.TODO(),
&qianfan.CompletionRequest{
Prompt: "hello",
},
)
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.completion()
.endpoint("your_custom_endpoint")
.prompt("hello")
.executeStream()
.forEachRemaining(chunk -> System.out.print(gson.toJson(chunk)));
}
}
import {Completions, setEnvVariable} from "@baiducloud/qianfan";
const client = new Completions({ QIANFAN_ACCESS_KEY: 'your_iam_ak', QIANFAN_SECRET_KEY: 'your_iam_sk', Endpoint: 'your_endpoint' });
async function main() {
const stream = await client.completions({
prompt: 'Introduce the city Beijing',
stream: true, //启用流式返回
});
for await (const chunk of stream as AsyncIterableIterator<any>) {
console.log(chunk);
}
}
main();
返回示例(流式)
{'id': 'as-9hyt4r1mp9', 'object': 'completion', 'created': 1718781893, 'sentence_id': 0, 'is_end': False, 'is_truncated': False, 'result': 'Beijing', 'need_clear_history': False, 'usage': {'prompt_tokens': 6, 'completion_tokens': 0, 'total_tokens': 6}}
{'id': 'as-9hyt4r1mp9', 'object': 'completion', 'created': 1718781893, 'sentence_id': 1, 'is_end': False, 'is_truncated': False, 'result': ' is the capital city of China and the center of the administrative, cultural, scientific, and technological district of the country. It is also the main gateway', 'need_clear_history': False, 'usage': {'prompt_tokens': 6, 'completion_tokens': 0, 'total_tokens': 6}}
{'id': 'as-9hyt4r1mp9', 'object': 'completion', 'created': 1718781894, 'sentence_id': 2, 'is_end': False, 'is_truncated': False, 'result': " of China's economic, cultural, and political exchanges with the outside world. Beijing is located in the northern part of the Chinese mainland and is", 'need_clear_history': False, 'usage': {'prompt_tokens': 6, 'completion_tokens': 0, 'total_tokens': 6}}
{'id': 'as-9hyt4r1mp9', 'object': 'completion', 'created': 1718781894, 'sentence_id': 3, 'is_end': False, 'is_truncated': False, 'result': ' bordered by Hebei Province to the east and the Bohai Sea to the west. It has a total area of 16,41', 'need_clear_history': False, 'usage': {'prompt_tokens': 6, 'completion_tokens': 0, 'total_tokens': 6}}
{'id': 'as-9hyt4r1mp9', 'object': 'completion', 'created': 1718781895, 'sentence_id': 4, 'is_end': False, 'is_truncated': False, 'result': '0 square kilometers, including 10,807 square kilometers of urban area. Beijing is a city with a long history and splendid culture', 'need_clear_history': False, 'usage': {'prompt_tokens': 6, 'completion_tokens': 0, 'total_tokens': 6}}
{'id': 'as-9hyt4r1mp9', 'object': 'completion', 'created': 1718781895, 'sentence_id': 5, 'is_end': False, 'is_truncated': False, 'result': '. It has a history of more than 3,000 years and is one of the most ancient and important cities in China. Beijing is', 'need_clear_history': False, 'usage': {'prompt_tokens': 6, 'completion_tokens': 0, 'total_tokens': 6}}
{'id': 'as-9hyt4r1mp9', 'object': 'completion', 'created': 1718781895, 'sentence_id': 6, 'is_end': False, 'is_truncated': False, 'result': ' also a city with a unique modern history. It was founded by the Manchu people in 1421 and became the capital of China in', 'need_clear_history': False, 'usage': {'prompt_tokens': 6, 'completion_tokens': 0, 'total_tokens': 6}}
{'id': 'as-9hyt4r1mp9', 'object': 'completion', 'created': 1718781896, 'sentence_id': 7, 'is_end': False, 'is_truncated': False, 'result': " 1421. After the founding of the People's Republic of China, Beijing has become the center of China's scientific, technological", 'need_clear_history': False, 'usage': {'prompt_tokens': 6, 'completion_tokens': 0, 'total_tokens': 6}}
{'id': 'as-9hyt4r1mp9', 'object': 'completion', 'created': 1718781896, 'sentence_id': 8, 'is_end': False, 'is_truncated': False, 'result': ', cultural, educational and other fields. Beijing is also a city with a unique geographical environment. It is surrounded by mountains and rivers and has rich natural', 'need_clear_history': False, 'usage': {'prompt_tokens': 6, 'completion_tokens': 0, 'total_tokens': 6}}
{'id': 'as-9hyt4r1mp9', 'object': 'completion', 'created': 1718781897, 'sentence_id': 9, 'is_end': False, 'is_truncated': False, 'result': ' resources and beautiful scenery. Beijing is a city with a unique urban layout and architectural style. It has many historical buildings and cultural relics', 'need_clear_history': False, 'usage': {'prompt_tokens': 6, 'completion_tokens': 272, 'total_tokens': 278}}
{'id': 'as-9hyt4r1mp9', 'object': 'completion', 'created': 1718781897, 'sentence_id': 10, 'is_end': False, 'is_truncated': False, 'result': ', including the Forbidden City, the Temple of Heaven, the Summer Palace, etc., which are all very precious cultural heritage of China. In short', 'need_clear_history': False, 'usage': {'prompt_tokens': 6, 'completion_tokens': 272, 'total_tokens': 278}}
{'id': 'as-9hyt4r1mp9', 'object': 'completion', 'created': 1718781897, 'sentence_id': 11, 'is_end': False, 'is_truncated': False, 'result': ', Beijing is a city with a long history, splendid culture, unique modern history, rich natural resources, beautiful scenery, unique urban layout', 'need_clear_history': False, 'usage': {'prompt_tokens': 6, 'completion_tokens': 272, 'total_tokens': 278}}
{'id': 'as-9hyt4r1mp9', 'object': 'completion', 'created': 1718781898, 'sentence_id': 12, 'is_end': False, 'is_truncated': False, 'result': ' and architectural style, as well as many historical and cultural relics. It is a city that combines ancient and modern, traditional and modern, and', 'need_clear_history': False, 'usage': {'prompt_tokens': 6, 'completion_tokens': 272, 'total_tokens': 278}}
{'id': 'as-9hyt4r1mp9', 'object': 'completion', 'created': 1718781898, 'sentence_id': 13, 'is_end': True, 'is_truncated': False, 'result': ' has become an international metropolis with great influence.', 'need_clear_history': False, 'usage': {'prompt_tokens': 6, 'completion_tokens': 372, 'total_tokens': 378}}
Hello! It's nice to meet you. Is there something I can help you with
or would you like to chat for a bit
{"id":"as-d73nus31se","object":"chat.completion","created":1710471174,"sentenceId":0,"isEnd":false,"isSafe":1,"result":"Hello! It\u0027","usage":{"promptTokens":1,"completionTokens":2,"totalTokens":3}}
{"id":"as-d73nus31se","object":"chat.completion","created":1710471174,"sentenceId":1,"isEnd":true,"isSafe":1,"result":"s nice to meet you. Is there something I can help you with or would you like to chat?","usage":{"promptTokens":1,"completionTokens":24,"totalTokens":27}}
{
id: 'as-wnck26hc3y',
object: 'chat.completion',
created: 1718782021,
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,
finish_reason: 'normal',
usage: { prompt_tokens: 1, completion_tokens: 0, total_tokens: 1 }
}
{
id: 'as-wnck26hc3y',
object: 'chat.completion',
created: 1718782022,
sentence_id: 1,
is_end: false,
is_truncated: false,
result: 'or would you like to chat for a bit?',
need_clear_history: false,
finish_reason: 'normal',
usage: { prompt_tokens: 1, completion_tokens: 29, total_tokens: 30 }
}
}
请求参数
注意:以下为Python SDK参数说明,其他SDK参数相关说明请参考Go SDK-续写Completions参数相关说明、Java SDK参数相关说明、Node.js SDK参数相关说明。
名称 | 类型 | 必填 | 描述 |
---|---|---|---|
prompt | string | 是 | 请求信息 |
endpoint | string | 是 | 用于指定用户自行发布的模型服务,如用户自定义导入模型。说明: (1)该字段值可以通过查看服务地址获取:打开模型服务-模型推理-我的服务页面,选择创建的服务-点击详情页查看服务地址,endpoint值为 https://aip.baidubce.com/rpc/2.0/ai_custom/v1/wenxinworkshop/completions/ 后面的地址,如下图所示(2)注意:在创建服务页面,选择模型后,API地址会自动新增个后缀。例如选择某模型,输入API地址为“test1”,endpoint的取值即为“lbwxxxd4_test1”,如下图所示,如何发布服务请参考快速部署自定义导入模型 |
stream | bool | 否 | 是否以流式接口的形式返回数据,默认false |
temperature | float | 否 | 说明: (1)较高的数值会使输出更加随机,而较低的数值会使其更加集中和确定 (2)范围 (0, 1.0],不能为0 (3)建议该参数和top_p只设置1个 (4)默认值以generation_config.json配置为准,如果未配置默认值为1.0 |
top_k | int | 否 | Top-K 采样参数,在每轮token生成时,保留k个概率最高的token作为候选。说明: (1)影响输出文本的多样性,取值越大,生成文本的多样性越强 (2)取值范围:正整数 (3)默认值以generation_config.json配置为准,如果未配置默认值为50 |
top_p | float | 否 | 说明: (1)影响输出文本的多样性,取值越大,生成文本的多样性越强 (2)取值范围 [0, 1.0] (3)建议该参数和temperature只设置1个 (4)默认值以generation_config.json配置为准,如果未配置默认值为1.0 |
penalty_score | float | 否 | 通过对已生成的token增加惩罚,减少重复生成的现象。说明: (1)值越大表示惩罚越大 (2)取值范围:[1.0, 2.0] (3)默认值以generation_config.json配置为准,如果未配置默认值为1.0 |
stop | List(String) | 否 | 生成停止标识。当模型生成结果以stop中某个元素结尾时,停止文本生成。说明: (1)每个元素长度不超过20字符。 (2)最多4个元素 |
user_id | 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总数 |