续写Completions
更新时间:2024-11-15
SDK 支持调用续写Completions相关API,支持非流式、流式调用。
注意事项
(1)本文内容适用Python SDK、Go SDK、Java SDK和Node.js SDK调用,调用流程请参考SDK使用说明。
(2)模型不同,调用SDK时,使用参数不同。请结合具体模型,查看参数使用说明进行调用。
- 指定支持预置服务的模型,使用model参数指定支持预置服务的模型,详情见参数说明。
- 用户快速部署自行发布的模型服务,需使用参数endpoint,详见请求参数说明。
支持模型列表
支持的模型列表及各模型支持的调用方法说明如下,更多详情见请求参数说明。
模型 | 支持预置服务的模型 (使用model参数) |
用户自行发布的模型服务 (使用endpoint参数) |
---|---|---|
SQLCoder-7B | ✓ | ✓ |
CodeLlama-7b-Instruct | ✓ | ✓ |
Yi-34B | ✕ | ✓ |
AquilaCode-multi | ✕ | ✓ |
Cerebras-GPT-13B | ✕ | ✓ |
Pythia-12B | ✕ | ✓ |
GPT-J-6B | ✕ | ✓ |
GPT-NeoX-20B | ✕ | ✓ |
GPT4All-J | ✕ | ✓ |
StarCoder | ✕ | ✓ |
StableLM-Alpha-7B | ✕ | ✓ |
SDK调用
调用示例(非流式)
- 指定支持预置服务的模型
使用model
字段,指定平台支持预置服务的模型,调用示例如下。
import os
import qianfan
# 使用安全认证AK/SK鉴权,通过环境变量方式初始化;替换下列示例中参数,安全认证Access Key替换your_iam_ak,Secret Key替换your_iam_sk,如何获取请查看https://cloud.baidu.com/doc/Reference/s/9jwvz2egb
os.environ["QIANFAN_ACCESS_KEY"] = "your_iam_ak"
os.environ["QIANFAN_SECRET_KEY"] = "your_iam_sk"
comp = qianfan.Completion()
resp = comp.do(model="CodeLlama-7b-Instruct", prompt="In Bash, how do I list all text files in the current directory (excluding subdirectories) that have been modified in the last month")
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,如何获取请查看https://cloud.baidu.com/doc/Reference/s/9jwvz2egb
os.Setenv("QIANFAN_ACCESS_KEY", "your_iam_ak")
os.Setenv("QIANFAN_SECRET_KEY", "your_iam_sk")
chat := qianfan.NewCompletion(
qianfan.WithModel("CodeLlama-7b-Instruct"),
)
resp, _ := chat.Do( // Stream 启用流式返回,参数与 Do 相同
context.TODO(),
&qianfan.CompletionRequest{
Prompt: "你好,你是谁",
},
)
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,如何获取请查看https://cloud.baidu.com/doc/Reference/s/9jwvz2egb
Qianfan qianfan = new Qianfan("your_iam_ak", "your_iam_sk");
// 指定模型
CompletionResponse resp = qianfan.completion()
.model("CodeLlama-7b-Instruct")
.prompt("hello")
.execute();
System.out.print(resp.getResult());
}
}
import {Completions, setEnvVariable} from "@baiducloud/qianfan";
// 使用安全认证AK/SK鉴权,替换下列示例中参数,安全认证Access Key替换your_iam_ak,Secret Key替换your_iam_sk,如何获取请查看https://cloud.baidu.com/doc/Reference/s/9jwvz2egb
const client = new Completions({ QIANFAN_ACCESS_KEY: 'your_iam_ak', QIANFAN_SECRET_KEY: 'your_iam_sk' });
async function main() {
const resp = await client.completions({
prompt: '你好',
}, 'CodeLlama-7b-Instruct');
console.log(resp);
}
main();
- 用户自行发布的模型服务
对于用户快速部署自行发布的模型服务,通过使用endpoint
字段进行调用,示例如下。
import os
import qianfan
# 使用安全认证AK/SK鉴权,通过环境变量方式初始化;替换下列示例中参数,安全认证Access Key替换your_iam_ak,Secret Key替换your_iam_sk,如何获取请查看https://cloud.baidu.com/doc/Reference/s/9jwvz2egb
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="你好")
# 可以通过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( // Stream 启用流式返回,参数与 Do 相同
context.TODO(),
&qianfan.CompletionRequest{
Prompt: "你好,你是谁",
},
)
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: '***' });
async function main() {
const resp = await client.completions({
prompt: '你好,你是谁',
});
console.log(resp);
}
main();
返回示例(非流式)
{
'id': 'as-jy35txjsru',
'object': 'completion',
'created': 1718782039,
'result': 'You can use the `find` command in Bash to list all text files in the current directory (excluding subdirectories) that have been modified in the last month. Here\'s an example command:\n```\nfind . -type f -name "*.txt" -mtime -1\n```\nHere\'s how the command works:\n\n* `.` represents the current directory.\n* `-type f` specifies that we are looking for files (as opposed to directories).\n* `-name "*.txt"` specifies that we are looking for files with the `.txt` extension.\n* `-mtime -1` specifies that we are looking for files that have been modified in the last month.\n\nThe `-mtime` option takes a numerical argument, which represents the number of days since the file was last modified. In this case, `-1` means "modified in the last day".\n\nNote that this command will only list files that have been modified in the current directory and its subdirectories. If you want to search for files in a specific directory and its subdirectories, you can replace `.` with the name of the directory you want to search. For example:\n```\nfind /path/to/directory -type f -name "*.txt" -mtime -1\n```\nThis will search for text files in the specified directory and its subdirectories that have been modified in the last month.',
'is_safe': 1,
'usage': {
'prompt_tokens': 29,
'completion_tokens': 279,
'total_tokens': 308
}
}
你好,我是文心一言,英文名是ERNIE Bot,可以协助你完成范围广泛的任务并提供有关各种主题的信息,比如回答问题,提供定义和解释及建议。如果你有任何问题,请随时向我提问。
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: '你好!请问有什么我可以帮助你的吗?无论你有什么问题或需要帮助,我都会尽力回答和提供支持。请随时告诉我你的需求,我会尽快回复你。',
is_truncated: false,
need_clear_history: false,
finish_reason: 'normal',
usage: { prompt_tokens: 1, completion_tokens: 34, total_tokens: 35 }
}
调用示例(流式)
import os
import qianfan
# 使用安全认证AK/SK鉴权,通过环境变量方式初始化;替换下列示例中参数,安全认证Access Key替换your_iam_ak,Secret Key替换your_iam_sk,如何获取请查看https://cloud.baidu.com/doc/Reference/s/9jwvz2egb
os.environ["QIANFAN_ACCESS_KEY"] = "your_iam_ak"
os.environ["QIANFAN_SECRET_KEY"] = "your_iam_sk"
comp = qianfan.Completion()
# 续写功能同样支持流式调用
resp = comp.do(model="CodeLlama-7b-Instruct", prompt="In Bash, how do I list all text files in the current directory (excluding subdirectories) that have been modified in the last month", 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.WithModel("CodeLlama-7b-Instruct"),
)
resp, _ := chat.Stream( // Stream 启用流式返回,参数与 Do 相同
context.TODO(),
&qianfan.CompletionRequest{
Prompt: "你好,你是谁",
},
)
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()
.model("CodeLlama-7b-Instruct")
.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' });
async function main() {
const stream = await client.completions({
prompt: '你好,你是谁',
stream: true, //启用流式返回
model: CodeLlama-7b-Instruct,
});
for await (const chunk of stream as AsyncIterableIterator<any>) {
console.log(chunk);
}
}
main();
返回示例(流式)
{'id': 'as-wjs702ndui', 'object': 'completion', 'created': 1718782182, 'sentence_id': 0, 'is_end': False, 'result': 'To list all text files in the current directory (excluding subdirectories) that have been modified in the last month in Bash, you can use the', 'is_safe': 1, 'usage': {'prompt_tokens': 29, 'completion_tokens': 32, 'total_tokens': 61}}
{'id': 'as-wjs702ndui', 'object': 'completion', 'created': 1718782183, 'sentence_id': 1, 'is_end': False, 'result': ' following command:\n```\nfind . -type f -name "*.txt" -mtime -1\n```\nHere\'s how the command works', 'is_safe': 1, 'usage': {'prompt_tokens': 29, 'completion_tokens': 22, 'total_tokens': 83}}
{'id': 'as-wjs702ndui', 'object': 'completion', 'created': 1718782184, 'sentence_id': 2, 'is_end': False, 'result': ':\n\n* `find`: The find command is used to search for files based on various criteria.\n* `.`: The current directory is specified using the', 'is_safe': 1, 'usage': {'prompt_tokens': 29, 'completion_tokens': 29, 'total_tokens': 112}}
{'id': 'as-wjs702ndui', 'object': 'completion', 'created': 1718782185, 'sentence_id': 3, 'is_end': False, 'result': ' `.` notation.\n* `-type f`: This option tells find to search for files (not directories).\n* `-name "*.txt"`: This', 'is_safe': 1, 'usage': {'prompt_tokens': 29, 'completion_tokens': 27, 'total_tokens': 139}}
{'id': 'as-wjs702ndui', 'object': 'completion', 'created': 1718782186, 'sentence_id': 4, 'is_end': False, 'result': ' option tells find to search for files with the .txt extension.\n* `-mtime -1`: This option tells find to search for files that have been', 'is_safe': 1, 'usage': {'prompt_tokens': 29, 'completion_tokens': 32, 'total_tokens': 171}}
{'id': 'as-wjs702ndui', 'object': 'completion', 'created': 1718782187, 'sentence_id': 5, 'is_end': False, 'result': ' modified in the last month. The `-mtime` option is used to specify the modification time, and the `-1` option tells find to search for files', 'is_safe': 1, 'usage': {'prompt_tokens': 29, 'completion_tokens': 35, 'total_tokens': 206}}
{'id': 'as-wjs702ndui', 'object': 'completion', 'created': 1718782188, 'sentence_id': 6, 'is_end': False, 'result': ' that have been modified in the last day.\n\nYou can also use the `-mtime +1` option to search for files that have been modified in', 'is_safe': 1, 'usage': {'prompt_tokens': 29, 'completion_tokens': 33, 'total_tokens': 239}}
{'id': 'as-wjs702ndui', 'object': 'completion', 'created': 1718782189, 'sentence_id': 7, 'is_end': False, 'result': ' the last 2 days, or the `-mtime +2` option to search for files that have been modified in the last 3 days, and so', 'is_safe': 1, 'usage': {'prompt_tokens': 29, 'completion_tokens': 32, 'total_tokens': 271}}
{'id': 'as-wjs702ndui', 'object': 'completion', 'created': 1718782190, 'sentence_id': 8, 'is_end': False, 'result': ' on.\n\nNote that the `-mtime` option only works for files that have a modification time that is less than or equal to the current time.', 'is_safe': 1, 'usage': {'prompt_tokens': 29, 'completion_tokens': 33, 'total_tokens': 304}}
{'id': 'as-wjs702ndui', 'object': 'completion', 'created': 1718782191, 'sentence_id': 9, 'is_end': False, 'result': ' If you want to search for files that have been modified in the last month, you can use the `-mtime -1` option. If you want to', 'is_safe': 1, 'usage': {'prompt_tokens': 29, 'completion_tokens': 35, 'total_tokens': 339}}
{'id': 'as-wjs702ndui', 'object': 'completion', 'created': 1718782192, 'sentence_id': 10, 'is_end': True, 'result': ' search for files that have been modified in the last 2 months, you can use the `-mtime -2` option, and so on.', 'is_safe': 1, 'usage': {'prompt_tokens': 29, 'completion_tokens': 29, 'total_tokens': 368}}
你好,
我是百度研发的知识增强大语言模型,中文名是文心一言,英文名是ERNIE Bot。
我能够与人对话互动,回答问题,协助创作,高效便捷地帮助人们获取信息、知识和灵感。
请问你有需要我帮助你的问题吗?
{"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-cck51r1rfw',
object: 'chat.completion',
created: 1709779938,
sentence_id: 0,
is_end: false,
is_truncated: false,
result: '你好!',
need_clear_history: false,
finish_reason: 'normal',
usage: { prompt_tokens: 1, completion_tokens: 2, total_tokens: 3 }
}
{
id: 'as-cck51r1rfw',
object: 'chat.completion',
created: 1709779938,
sentence_id: 1,
is_end: false,
is_truncated: false,
result: '请问有什么可以帮助你的吗?',
need_clear_history: false,
finish_reason: 'normal',
usage: { prompt_tokens: 1, completion_tokens: 2, total_tokens: 3 }
}
{
id: 'as-cck51r1rfw',
object: 'chat.completion',
created: 1709779938,
sentence_id: 2,
is_end: true,
is_truncated: false,
result: '',
need_clear_history: false,
finish_reason: 'normal',
usage: { prompt_tokens: 1, completion_tokens: 8, total_tokens: 9 }
}
请求参数
注意:以下为Python SDK参数说明,其他SDK参数相关说明请参考Go SDK-续写Completions参数相关说明、Java SDK参数相关说明、Node.js SDK参数相关说明。
名称 | 类型 | 必填 | 描述 |
---|---|---|---|
prompt | string | 是 | 请求信息 |
model | string | 是 | 模型名称,用于指定平台支持的预置服务模型,说明: (1)如果需指定平台支持的预置服务模型,此字段必填 (2)支持模型如下: · SQLCoder-7B · CodeLlama-7b-Instruct |
endpoint | string | 否 | 用于指定用户自行发布的模型服务。说明: (1)如果需指定用户自行发布的模型服务,endpoint字段为必填,目前平台支持自行发布的模型服务,请参考本文支持的模型列表-用户自行发布的模型服务列表,示例:GPT-J-6B (2)该字段值可以通过查看服务地址获取:打开模型服务-模型推理-我的服务页面,选择创建的服务-点击详情页查看服务地址,endpoint值为 https://aip.baidubce.com/rpc/2.0/ai_custom/v1/wenxinworkshop/completions/ 后面的地址,如下图所示注意: 在创建服务页面,选择模型后,API地址会自动新增个后缀。例如选择模型Pythia-12B,输入API地址为“test1”,endpoint的取值即为“lbwxxxd4_test1”,如下图所示,如何发布服务请参考发布平台预置的模型服务 |
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] |
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 | 否 | 表示最终用户的唯一标识符 |
返回参数
名称 | 类型 | 描述 |
---|---|---|
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总数 |