资讯 社区 文档
技术能力
语音技术
文字识别
人脸与人体
图像技术
语言与知识
视频技术

对话Chat V2

本文介绍了如何通过SDK调用对话Chat V2版本相关API。

功能介绍

调用本接口,发起一次对话请求。

支持模型列表

模型名称 模型版本 model 参数值
ERNIE 4.0 ERNIE-4.0-8K-Latest ernie-4.0-8k-latest
ERNIE 4.0 ERNIE-4.0-8K-Preview ernie-4.0-8k-preview
ERNIE 4.0 ERNIE-4.0-8K ernie-4.0-8k
ERNIE 4.0 Turbo ERNIE-4.0-Turbo-8K-Latest ernie-4.0-turbo-8k-latest
ERNIE 4.0 Turbo ERNIE-4.0-Turbo-8K-Preview ernie-4.0-turbo-8k-preview
ERNIE 4.0 Turbo ERNIE-4.0-Turbo-8K ernie-4.0-turbo-8k
ERNIE 4.0 Turbo ERNIE-4.0-Turbo-128K ernie-4.0-turbo-128k
ERNIE 3.5 ERNIE-3.5-8K-Preview ernie-3.5-8k-preview
ERNIE 3.5 ERNIE-3.5-8K ernie-3.5-8k
ERNIE 3.5 ERNIE-3.5-128K ernie-3.5-128k
ERNIE Speed ERNIE-Speed-8K ernie-speed-8k
ERNIE Speed ERNIE-Speed-128K ernie-speed-128k
ERNIE Speed ERNIE-Speed-Pro-128K ernie-speed-pro-128k
ERNIE Lite ERNIE-Lite-8K ernie-lite-8k
ERNIE Lite ERNIE-Lite-Pro-128K ernie-lite-pro-128k
ERNIE Tiny ERNIE-Tiny-8K ernie-tiny-8k
ERNIE Character ERNIE-Character-8K ernie-char-8k
ERNIE Character ERNIE-Character-Fiction-8K ernie-char-fiction-8k
ERNIE-Novel-8K ERNIE-Novel-8K ernie-novel-8k



使用说明

本文API支持通过Python SDK、Go SDK和Node.js SDK调用,调用流程请参考SDK安装及使用流程

  • Python SDK,请确保使用最新版本,版本需≥0.4.11。
  • Go SDK,请确保使用最新版本,版本需≥0.0.13。
  • Node.js SDK,请确保使用最新版本,版本需≥0.2.2。

SDK调用

调用示例(非流式)

使用model字段,指定平台支持预置服务的模型,调用示例如下。

from qianfan import Qianfan

client = Qianfan(
 
    # 方式一:使用安全认证AK/SK鉴权
    # 替换下列示例中参数,安全认证Access Key替换your_iam_ak,Secret Key替换your_iam_sk,如何获取请查看https://cloud.baidu.com/doc/Reference/s/9jwvz2egb
    access_key="your_iam_ak",
    secret_key="your_iam_sk",
    #app_id="", # 选填,不填写则使用默认appid

    # 方式二:使用应用BearerToken鉴权
    # 替换下列示例中参数,将your_BearerToken替换为真实值,如何获取请查看https://cloud.baidu.com/doc/IAM/s/Mm2x80phi
    #api_key="your_BearerToken" 
    #app_id="", # 选填,不填写则使用默认appid
)

completion = client.chat.completions.create(
    model="ernie-3.5-8k", # 指定特定模型
    messages=[ 
        # 也可以不设置system字段
        {'role': 'system', 'content': '平台助手'},
        {'role': 'user', 'content': '你好'}
    ]
)

print(completion.choices[0])
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")

	cc := qianfan.NewChatCompletionV2()
	resp, err := cc.Do(context.TODO(), &qianfan.ChatCompletionV2Request{
		Model: "ernie-lite-8k",
		Messages: []qianfan.ChatCompletionV2Message{
			{
				Role:    "user",
				Content: "你好",
			},
		},
	})

	if err != nil {
		panic(err)
	}

	fmt.Println(resp.Choices[0].Message)
}
import com.baidubce.qianfan.Qianfan;
import com.baidubce.qianfan.QianfanV2;
import com.baidubce.qianfan.model.chat.v2.request.RequestV2;
import com.baidubce.qianfan.model.chat.v2.response.ResponseV2;

public class Demo {
    public static void main(String args[]){
        // 使用安全认证AK/SK鉴权,通过环境变量初始化;替换下列示例中参数,安全认证Access Key替换your_iam_ak,Secret Key替换your_iam_sk
        QianfanV2 client = new Qianfan("your_iam_ak", "your_iam_sk").v2();
        RequestV2 request = client.chatCompletion()
                .model("ernie-3.5-8k")
                .addMessage("user", "你好") // 添加用户消息 (此方法可以调用多次,以实现多轮对话的消息传递)
                .temperature(0.7) // 自定义超参数
                .build(); // 发起请求

        ResponseV2 response = client.chatCompletion(request);
        System.out.println(response.getChoices().get(0).getMessage().getContent());
    }
}
import {ChatCompletion} from "@baiducloud/qianfan";
import {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({
       version: 'v2',
});
async function main() {

    const resp = await client.chat({
        messages: [
            {
                role: 'user',
                content: '今天深圳天气',
            },
        ],
     }, "ernie-4.0-8k");
    console.log(resp?.choices);
}

main();

返回示例(非流式)

finish_reason='normal' index=0 message=ChatCompletionMessage(content='您好!请问您是想了解关于“平台助手”的信息吗?如果是的话,能否具体说明一下您想了解的是哪个平台或者哪种类型的助手呢?这样我可以为您提供更详细和准确的信息。', role='assistant', name=None, content_type=None, function_call=None) need_clear_history=None ban_round=None function_call=None search_info=None flag=0 tools_info=None
{assistant 你好!有什么需要我帮助的吗?}
您好!我是文心一言,很高兴与您交流。请问有什么我可以帮助您的吗?无论是知识问答、文本创作还是其他任何问题,我都会尽力为您提供帮助。
[
  {
    index: 0,
    message: {
      role: 'assistant',
      content: '很抱歉,我无法直接获取当前深圳的天气信息。但是,您可以通过以下方式查询深圳的天气:\n' +
        '\n' +
        '1. 使用搜索引擎:在搜索引擎中输入“深圳天气”或“今天深圳天气”,通常可以获得最新的天气预报信息。\n' +
        '\n' +
        '2. 使用天气预报应用:在手机应用商店中搜索“天气预报”或“深圳天气预报”,下载并安装相关应用,即可随时查看深圳的天气情况。\n' +
        '\n' +
        '3. 关注天气预报公众号:在微信等社交媒体平台上,关注天气预报相关的公众号,它们通常会定期发布天气预报信息。\n' +
        '\n' +
        '请注意,天气情况会随时变化,建议您在查询天气时选择多个来源进行对比,以获得更准确的信息。同时,如果您需要了解更详细的天气情况,如温度、湿度、风速等,也可以在查询时选择相应的选项。'
    },
    finish_reason: 'normal',
    flag: 0
  }
]

调用示例(流式)

from qianfan import Qianfan

client = Qianfan(
 
    # 方式一:使用安全认证AK/SK鉴权
    # 替换下列示例中参数,安全认证Access Key替换your_iam_ak,Secret Key替换your_iam_sk,如何获取请查看https://cloud.baidu.com/doc/Reference/s/9jwvz2egb
    access_key="your_iam_ak",
    secret_key="your_iam_sk",
    #app_id="", # 选填,不填写则使用默认appid

    # 方式二:使用应用BearerToken鉴权
    # 替换下列示例中参数,将your_BearerToken替换为真实值,如何获取请查看https://cloud.baidu.com/doc/IAM/s/Mm2x80phi
    #api_key="your_BearerToken" 
    #app_id="", # 选填,不填写则使用默认appid
)

completion = client.chat.completions.create(
    model="ernie-3.5-8k", # 指定特定模型
    messages=[ 
        {'role': 'system', 'content': '平台助手'},
        {'role': 'user', 'content': '你好'}
    ],
    stream=True
)

for r in completion:
    print(r)
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.NewChatCompletionV2()
	resp, err := chat.Stream(context.TODO(), &qianfan.ChatCompletionV2Request{
		Model: "ernie-3.5-8k",
		Messages: []qianfan.ChatCompletionV2Message{
			{
				Role:    "user",
				Content: "你好",
			},
		},
	})

	if err != nil {
		panic(err)
	}

	for !resp.IsEnd {
		r := qianfan.ChatCompletionV2Response{}
		err := resp.Recv(&r)
		if err != nil {
			panic(err)
		}

		if len(r.Choices) != 0 {
			fmt.Println(r.Choices[0].Delta.Content)
		}
	}
}
import com.baidubce.qianfan.Qianfan;
import com.baidubce.qianfan.QianfanV2;
import com.baidubce.qianfan.core.StreamIterator;
import com.baidubce.qianfan.model.chat.v2.request.RequestV2;
import com.baidubce.qianfan.model.chat.v2.response.StreamResponseV2;
import java.util.function.Consumer;

public class Demo {
    public static void main(String args[]){
        // 使用安全认证AK/SK鉴权,通过环境变量初始化;替换下列示例中参数,安全认证Access Key替换your_iam_ak,Secret Key替换your_iam_sk
        QianfanV2 client = new Qianfan("your_iam_ak", "your_iam_sk").v2();
        RequestV2 request = client.chatCompletion()
                .model("ernie-3.5-8k")
                .addMessage("user", "你好") // 添加用户消息 (此方法可以调用多次,以实现多轮对话的消息传递)
                .temperature(0.7) // 自定义超参数
                .build(); // 发起请求

        StreamIterator<StreamResponseV2> resps = client.chatCompletionStream(request);
        Consumer<StreamResponseV2> consumer = s -> System.out.println(s.getChoices().get(0).getDelta().getContent());
        resps.forEachRemaining(consumer);
    }
}
import {ChatCompletion,setEnvVariable} from "@baiducloud/qianfan";

// 使用安全认证AK/SK鉴权
// 通过环境变量初始化;替换下列示例中参数,安全认证Access Key替换your_iam_ak,Secret Key替换your_iam_sk,如何获取请查看https://cloud.baidu.com/doc/Reference/s/9jwvz2egb
setEnvVariable('QIANFAN_ACCESS_KEY','your_iam_ak');
setEnvVariable('QIANFAN_SECRET_KEY','your_iam_sk');

const client = new  ChatCompletion({
       version: 'v2',
        // appid: 'XXX'
});
async function main() {
    const resp = await client.chat({
        messages: [
            {
                role: 'user',
                content: '你好',
            },
        ],
        stream: true,
     }, "ernie-3.5-8k");
  
    for await (const chunk of resp) {
        console.log(chunk);
    }
}

main();

返回示例(流式)

id='as-gue7zc41p4' choices=[CompletionChunkChoice(delta=ChoiceDelta(content='您好!'), finish_reason=None, index=0)] created=1733465174 model='ernie-3.5-8k' object='chat.completion.chunk' usage=None statistic=CompletionStatistic(first_token_latency=0.49492, request_latency=0.0, total_latency=0.0, start_timestamp=1733465172918.0, avg_output_tokens_per_second=0.0)
id='as-gue7zc41p4' choices=[CompletionChunkChoice(delta=ChoiceDelta(content='很高兴与您'), finish_reason=None, index=0)] created=1733465174 model='ernie-3.5-8k' object='chat.completion.chunk' usage=None statistic=CompletionStatistic(first_token_latency=0.49492, request_latency=0.0, total_latency=0.0, start_timestamp=1733465172918.0, avg_output_tokens_per_second=0.0)
id='as-gue7zc41p4' choices=[CompletionChunkChoice(delta=ChoiceDelta(content='交流。'), finish_reason=None, index=0)] created=1733465174 model='ernie-3.5-8k' object='chat.completion.chunk' usage=None statistic=CompletionStatistic(first_token_latency=0.49492, request_latency=0.0, total_latency=0.0, start_timestamp=1733465172918.0, avg_output_tokens_per_second=0.0)
id='as-gue7zc41p4' choices=[CompletionChunkChoice(delta=ChoiceDelta(content='您提到的'), finish_reason=None, index=0)] created=1733465174 model='ernie-3.5-8k' object='chat.completion.chunk' usage=None statistic=CompletionStatistic(first_token_latency=0.49492, request_latency=0.0, total_latency=0.0, start_timestamp=1733465172918.0, avg_output_tokens_per_second=0.0)
id='as-gue7zc41p4' choices=[CompletionChunkChoice(delta=ChoiceDelta(content='“平台'), finish_reason=None, index=0)] created=1733465174 model='ernie-3.5-8k' object='chat.completion.chunk' usage=None statistic=CompletionStatistic(first_token_latency=0.49492, request_latency=0.0, total_latency=0.0, start_timestamp=1733465172918.0, avg_output_tokens_per_second=0.0)
...
你好!
有什么需要我帮助的吗?
您好!
我是文心
一言,
很高兴与您
交流。
请问有什么
我可以帮助
您的吗
?
无论是知识
问答、
文本创作
还是其他
任务,
我都会尽力
为您提供帮助
。
{
  id: 'as-aqifpbf1d0',
  object: 'chat.completion',
  created: 1733467504,
  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-aqifpbf1d0',
  object: 'chat.completion',
  created: 1733467504,
  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-aqifpbf1d0',
  object: 'chat.completion',
  created: 1733467504,
  sentence_id: 2,
  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-aqifpbf1d0',
  object: 'chat.completion',
  created: 1733467504,
  sentence_id: 3,
  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-aqifpbf1d0',
  object: 'chat.completion',
  created: 1733467504,
  sentence_id: 4,
  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-aqifpbf1d0',
  object: 'chat.completion',
  created: 1733467504,
  sentence_id: 5,
  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-aqifpbf1d0',
  object: 'chat.completion',
  created: 1733467505,
  sentence_id: 6,
  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-aqifpbf1d0',
  object: 'chat.completion',
  created: 1733467505,
  sentence_id: 7,
  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-aqifpbf1d0',
  object: 'chat.completion',
  created: 1733467505,
  sentence_id: 8,
  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-aqifpbf1d0',
  object: 'chat.completion',
  created: 1733467505,
  sentence_id: 9,
  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-aqifpbf1d0',
  object: 'chat.completion',
  created: 1733467506,
  sentence_id: 10,
  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-aqifpbf1d0',
  object: 'chat.completion',
  created: 1733467506,
  sentence_id: 11,
  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-aqifpbf1d0',
  object: 'chat.completion',
  created: 1733467506,
  sentence_id: 12,
  is_end: true,
  is_truncated: false,
  result: '',
  need_clear_history: false,
  finish_reason: 'normal',
  usage: { prompt_tokens: 1, completion_tokens: 23, total_tokens: 24 }
}

function call调用示例

  • 第一次请求
from qianfan import Qianfan
import os

#通过环境变量初始化认证信息
# 使用安全认证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"

client = Qianfan()

completion = client.chat.completions.create(
    model="ernie-3.5-8k",
    messages=[{"role": "user", "content": "你好,我想知道明天北京的天气怎么样"}],
    tools=[{
        "type": "function",
        "function": {
            "name": "get_current_weather",
            "description": "天气查询工具",
            "parameters": {
                "properties": {
                    "location": {
                        "description": "地理位置,精确到区县级别",
                        "type": "string"
                    },
                    "time": {
                        "description": "时间,格式为YYYY-MM-DD",
                        "type": "string"
                    }
                },
                "required": ["location", "time"],
                "type": "object"
            }
        }
    }],
)

print(completion.json(ensure_ascii=False))
  • 第二次请求
from qianfan import Qianfan
import os

#通过环境变量初始化认证信息
# 使用安全认证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"

client = Qianfan()

# 模拟函数调用并给出结果

completion = client.chat.completions.create(
    model="ernie-3.5-8k",
    messages=[
        {"role": "user", "content": "你好,我想知道明天北京的天气怎么样"},
        {'content': '', 'role': 'assistant', 'name': None, 'tool_calls': [{'id': '19eaa3faef0ca000', 'type': 'function', 'function': {'name': 'get_current_weather', 'arguments': '{"location": "北京", "time": "2024-12-14"}'}}], 'tool_call_id': None},
        {"role": "tool", "tool_call_id": "19exxxxx00", "name": "get_current_weather", "content": "{\"temperature\": \"20\", \"unit\": \"摄氏度\", \"description\": \"北京\"}"},
    ],
    tools=[{
        "type": "function",
        "function": {
            "name": "get_current_weather",
            "description": "天气查询工具",
            "parameters": {
                "properties": {
                    "location": {
                        "description": "地理位置,精确到区县级别",
                        "type": "string"
                    },
                    "time": {
                        "description": "时间,格式为YYYY-MM-DD",
                        "type": "string"
                    }
                },
                "required": ["location", "time"],
                "type": "object"
            }
        }
    }],
)

print(completion.json(ensure_ascii=False))

function call响应示例

  • 第一次响应
{
	"id": "as-0bd3fqniat",
	"choices": [{
		"finish_reason": "tool_calls",
		"index": 0,
		"message": {
			"content": "",
			"role": "assistant",
			"name": null,
			"tool_calls": [{
				"id": "19eaa550a7344000",
				"type": "function",
				"function": {
					"name": "get_current_weather",
					"arguments": "{\"location\": \"北京\", \"time\": \"2024-12-14\"}"
				}
			}],
			"tool_call_id": null
		},
		"ban_round": null,
		"flag": 0
	}],
	"created": 1734078514,
	"model": "ernie-3.5-8k",
	"object": "chat.completion",
	"usage": {
		"completion_tokens": 26,
		"prompt_tokens": 7,
		"total_tokens": 33
	},
	"statistic": {
		"first_token_latency": 0,
		"request_latency": 2.197312,
		"total_latency": 2.557672722,
		"start_timestamp": 1734078512335.0,
		"avg_output_tokens_per_second": 10.165491376734478
	}
}
  • 第二次响应
{
	"id": "as-1yunj9bnbx",
	"choices": [{
		"finish_reason": "normal",
		"index": 0,
		"message": {
			"content": "明天北京的天气温度是20摄氏度。请问您还有其他需要了解的吗?",
			"role": "assistant",
			"name": null,
			"tool_calls": null,
			"tool_call_id": null
		},
		"ban_round": null,
		"flag": 0
	}],
	"created": 1734078216,
	"model": "ernie-3.5-8k",
	"object": "chat.completion",
	"usage": {
		"completion_tokens": 15,
		"prompt_tokens": 26,
		"total_tokens": 41
	},
	"statistic": {
		"first_token_latency": 0,
		"request_latency": 2.704354,
		"total_latency": 2.936778522,
		"start_timestamp": 1734078212974.0,
		"avg_output_tokens_per_second": 5.107637463169924
	}
}

请求参数

Python SDK请求参数说明如下,Go SDK参数请参考Go SDK-对话Chat V2参数相关说明

名称 类型 必填 描述
model string 模型ID,可选值参考本文支持模型列表
messages List 聊天上下文信息。说明:
(1)messages成员不能为空,1个成员表示单轮对话,多个成员表示多轮对话,例如:
· 1个成员示例,"messages": [ {"role": "user","content": "你好"}]
· 3个成员示例,"messages": [ {"role": "user","content": "你好"},{"role":"assistant","content":"需要什么帮助"},{"role":"user","content":"自我介绍下"}]
(2) 最后一个message为当前请求的信息,前面的message为历史对话信息
(3)messages的role说明:
· 第一条message的role必须是user或system
· 最后一条message的role必须是user
· 当第一条message的role为user,role值需要依次为user/function -> assistant -> user/function ...,即奇数位message的role值必须为user或function,偶数位message的role值为assistant,例如:
示例中message中的role值分别为user、assistant、user、assistant、user;奇数位(红框)message中的role值为user,即第1、3、5个message中的role值为user;偶数位(蓝框)值为assistant,即第2、4个message中的role值为assistant
image.png
· 当第一条message的role为system,role值需要依次为system -> user/function -> assistant -> user/function ...
(4)message中的content总长度不能超过对应model的输入字符限制和输入tokens限制,请查看各模型上下文长度说明
stream bool 是否以流式接口的形式返回数据,说明:
(1)beam search模型只能为false
(2)默认false
stream_options object 流式响应的选项,当字段stream为true时,该字段生效
temperature float 说明:
(1)较高的数值会使输出更加随机,而较低的数值会使其更加集中和确定
(2)默认0.95,范围 (0, 1.0],不能为0
top_p float 说明:
(1)影响输出文本的多样性,取值越大,生成文本的多样性越强
(2)默认0.7,取值范围 [0, 1.0]
penalty_score float 通过对已生成的token增加惩罚,减少重复生成的现象。说明:
(1)值越大表示惩罚越大
(2)默认1.0,取值范围:[1.0, 2.0]
max_completion_tokens int 指定模型最大输出token数,说明:
(1)取值范围[2, 2048]
seed int 说明:
(1)取值范围: (0,2147483647‌),会由模型随机生成,默认值为空
(2)如果指定,系统将尽最大努力进行确定性采样,以便使用相同seed和参数的重复请求返回相同的结果
stop List 生成停止标识,当模型生成结果以stop中某个元素结尾时,停止文本生成。说明:
(1)每个元素长度不超过20字符
(2)最多4个元素
user string 表示最终用户的唯一标识符
frequency_penalty float 说明:
(1)正值根据迄今为止文本中的现有频率对新token进行惩罚,从而降低模型逐字重复同一行的可能性
(2)取值范围:[-2.0, 2.0]
(3)支持以下模型:
· ernie-speed-8k
· ernie-speed-128k
· ernie-speed-pro-128k
· ernie-lite-8k
· ernie-lite-pro-128k
· ernie-tiny-8k
· ernie-char-8k
· ernie-char-fiction-8k
presence_penalty float 说明:
(1)正值根据token记目前是否出现在文本中来对其进行惩罚,从而增加模型谈论新主题的可能性
(2)取值范围:[-2.0, 2.0]
(3)支持以下模型:
· ernie-speed-8k
· ernie-speed-128k
· ernie-speed-pro-128k
· ernie-lite-8k
· ernie-lite-pro-128k
· ernie-tiny-8k
· ernie-char-8k
· ernie-char-fiction-8k
tools List(Tool) 一个可触发函数的描述列表,支持模型请参考本文支持模型列表-是否支持function call功能
tool_choice string / tool_choice 说明:
(1)支持模型请参考本文支持模型列表-是否支持function call功能
(2)string类型,可选值如下:
· none:不希望模型调用任何function,只生成面向用户的文本消息
· auto:模型会根据输入内容自动决定是否调用函数以及调用哪些function
· required:希望模型总是调用一个或多个function
(3)当为tool_choice类型,指在函数调用场景下,提示大模型选择指定的函数,指定的函数名必须在tools中存在
parallel_tool_calls bool 说明:
(1)支持模型请参考本文支持模型列表-是否支持function call功能
(2)可选值:
· true:表示开启函数并行调用,默认开启
· false:表示关闭函数并行调用
response_format response_format 指定响应内容的格式
retry_count int 重试次数,默认1次
request_timeout float 请求超时时间,默认60秒
backoff_factor float 请求重试参数,用于指定重试的策略,默认为0
web_search web_search 搜索增强的选项,说明:
(1)默认不传关闭
(2)支持以下模型:
· ernie-4.0-8k-latest
· ernie-4.0-8k-preview
· ernie-4.0-8k
· ernie-4.0-turbo-8k-latest
· ernie-4.0-turbo-8k-preview
· ernie-4.0-turbo-8k
· ernie-4.0-turbo-128k
· ernie-3.5-8k-preview
· ernie-3.5-8k
· ernie-3.5-128k

message说明

名称 类型 必填 描述
role string 当前支持以下:
· user: 表示用户
· assistant: 表示对话助手
· system:表示人设
name string message名
content string 对话内容,说明:
(1)不能为空
(2)最后一个message对应的content不能为blank字符,如空格、"\n"、“\r”、“\f”等
tool_calls List[ToolCall] 函数调用,function call场景下第一轮对话的返回,第二轮对话作为历史信息在message中传入
tool_call_id string 说明:
(1)当role=tool时,该字段必填
(2)模型生成的function call id,对应tool_calls中的tool_calls[].id
(3)调用方应该传递真实的、由模型生成id,否则效果有损

stream_options说明

 名称 类型 必填
include_usage bool 流式响应是否输出usage,说明:
· ture:是,设置为true时,在最后一个chunk会输出一个字段,这个chunk上的usage字段显示整个请求的token统计信息
· false:否,流式响应默认不输出usage

Tool 说明

名称 类型 必填 描述
type string 工具类型,取值function
function function 函数说明

function说明

Tool中function说明如下

名称 类型 必填 描述
name string 函数名
description string 函数描述
parameters object 函数请求参数,JSON Schema 格式,参考JSON Schema描述

tool_choice说明

名称 类型 必填 描述
type string 指定工具类型,固定值function
function function 指定要使用的函数

function说明

tool_choice中function说明如下

名称 类型 必填 描述
name string 指定要使用的函数名

response_format说明

名称 类型 描述
type string 指定响应内容的格式,可选值:
· json_object:以json格式返回,可能出现不满足效果情况
· text:以文本格式返回,默认为text
· json_schema:以json_scheam规定的格式返回
json_schema object json_schema格式,请参考JSON Schema描述;当type为json_schema时,该参数必填

响应参数

名称 类型 描述
id string 本次请求的唯一标识,可用于排查问题
object string 回包类型 chat.completion:多轮对话返回
created int 时间戳
model string 模型ID
choices object stream=false时,返回内容
choices sse_choices stream=true时,返回内容
usage usage token统计信息,说明:
(1)同步请求默认返回
(2)流式请求默认不返回,当开启stream_options.include_usage=True时,会在最后一个chunk返回实际内容,其他chunk返回null
search_results search_results 搜索结果列表

choices说明

当stream=false时,返回内容如下:

名称 类型 描述
index int choice列表中的序号
message message 响应信息,当stream=false时返回
finish_reason string 输出内容标识,说明:
· normal:输出内容完全由大模型生成,未触发截断、替换
· stop:输出结果命中入参stop中指定的字段后被截断
· length:达到了最大的token数
· content_filter:输出内容被截断、兜底、替换为**等
· tool_calls:函数调用
flag int 安全细分类型,说明:
当stream=false,flag值含义如下:
· 0或不返回:安全
· 1:低危不安全场景,可以继续对话
· 2:禁聊:不允许继续对话,但是可以展示内容
· 3:禁止上屏:不允许继续对话且不能上屏展示
· 4:撤屏
ban_round int 当flag 不为 0 时,该字段会告知第几轮对话有敏感信息;如果是当前问题,ban_round = -1

sse_choices说明

当stream=true时,返回内容如下:

名称 类型 描述
index int choice列表中的序号
delta delta 响应信息,当stream=true时返回
finish_reason string 输出内容标识,说明:
· normal:输出内容完全由大模型生成,未触发截断、替换
· stop:输出结果命中入参stop中指定的字段后被截断· length:达到了最大的token数
· content_filter:输出内容被截断、兜底、替换为**等
· tool_calls:函数调用
flag int 安全细分类型,说明:当stream=true时,返回flag表示触发安全
ban_round int 当flag 不为 0 时,该字段会告知第几轮对话有敏感信息;如果是当前问题,ban_round = -1

delta说明

名称 类型 描述
content string 流式响应内容
tool_calls List[ToolCall] 由模型生成的函数调用,包含函数名称,和调用参数

ToolCall说明

名称 类型 描述
id string function call的唯一标识,由模型生成
type string 固定值function
function function function call的具体内容

function说明

名称 类型 描述
name string 函数名称
arguments string 函数参数

usage说明

名称 类型 描述
prompt_tokens int 问题tokens数(包含历史QA)
prompt_tokens_details int 问题token详情
completion_tokens int 回答tokens数
total_tokens int 总tokens数

prompt_tokens_details说明

名称 类型 描述
search_tokens int 触发检索增强以后膨胀的token;用户可以通过usage.prompt_tokens_details.search_tokens>0判断是否出发了检索增强,并且计算出发检索增强的次数

message说明

名称 类型 必填 描述
role string 当前支持以下:
· user: 表示用户
· assistant: 表示对话助手
· system:表示人设
name string message名
content string 对话内容,说明:
(1)不能为空
(2)最后一个message对应的content不能为blank字符,如空格、"\n"、“\r”、“\f”等
tool_calls List[ToolCall] 函数调用,function call场景下第一轮对话的返回,第二轮对话作为历史信息在message中传入
tool_call_id string 说明:
(1)当role=tool时,该字段必填
(2)模型生成的function call id,对应tool_calls中的tool_calls[].id
(3)调用方应该传递真实的、由模型生成id,否则效果有损
  • search_results说明
名称 类型 描述
index int 序号
url string 搜索结果URL
title string 搜索结果标题
上一篇
SDK安装及使用流程
下一篇
OpenAI接口兼容