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

XuanYuan-70B-Chat-4bit

XuanYuan-70B-Chat-4bit由度小满开发,基于Llama2-70B模型进行中文增强的金融行业大模型,通用能力显著提升,在CMMLU/CEVAL等各项榜单中排名前列;金融域任务超越领先通用模型,支持金融知识问答、金融计算、金融分析等各项任务。本文介绍了相关API。

接口描述

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

在线调试

平台提供了 API在线调试平台-示例代码 ,用于帮助开发者调试接口,平台集成快速检索、查看开发文档、查看在线调用的请求内容和返回结果、复制和下载示例代码等功能,简单易用,更多内容请查看API在线调试介绍

鉴权说明

本文API,支持2种鉴权方式。不同鉴权方式,调用方式不同,使用Header、Query参数不同,详见本文请求说明。开发者可以选择以下任一种方式进行鉴权。

请求结构

以访问凭证access_token鉴权方式为例,说明调用API请求结构,示例如下。

POST /rpc/2.0/ai_custom/v1/wenxinworkshop/chat/xuanyuan_70b_chat?access_token=24.4a3a19b******18992 HTTP/1.1
Host: aip.baidubce.com
Content-Type: application/json

{
   "messages": [
    {"role":"user","content":"等额本金和等额本息有什么区别?"}
   ]
}

请求头域

除公共头域外,无其它特殊头域。注意:鉴权方式不同,对应请求头域不同。

请求参数

  • 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总长度不能超过11200个字符
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 表示最终用户的唯一标识符
metadata map<string,string> 说明:
(1)元素个数最大支持16个
(2)key和value必须都是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 -X POST  'https://aip.baidubce.com/rpc/2.0/ai_custom/v1/wenxinworkshop/chat/xuanyuan_70b_chat?access_token=[步骤一调用接口获取的access_token]' -d '{
   "messages": [
    {"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/xuanyuan_70b_chat?access_token=" + get_access_token()
    
    payload = json.dumps({
        "messages": [
            {
                "role": "user",
                "content": "等额本金和等额本息有什么区别?"
            }
        ]
    })
    headers = {
        'Content-Type': 'application/json'
    }
    
    response = requests.request("POST", url, headers=headers, data=payload)
    
    print(response.text)
    

if __name__ == '__main__':
    main()

响应示例(单轮)

{
    "id": "as-gc9stq5sfg",
    "object": "chat.completion",
    "created": 1700727730,
    "result": " 等额本金和等额本息是两种常见的还款方式,它们在还款金额和还款期限上有着区别。\n\n首先,等额本金的特点是每月还款金额逐渐减少,前期还款金额较大,后期逐渐减少,最后一期还款金额最小。而等额本息的特点是每月还款金额固定不变,但是前期还款中本金所占比例较大,后期逐渐向利息所占比例增加。\n\n另外,还款期限也是两种方式的区别之一。等额本金的还款期限相对较短,因为每月还款金额逐渐减少,所以在还款期限内本金所占的比例较小,利息所占比例较大。而等额本息的还款期限相对较长,因为每月还款金额固定不变,所以在还款期限内,利息所占比例较大,本金所占比例较小。\n\n综上所述,选择等额本金还是等额本息可以根据借款人的资金状况和个人偏好来决定。需要考虑的因素包括借款人的偿还能力、借款目的和期限等。最好在借款前咨询专业人士或者金融机构,以便做出更好的决策。",
    "is_truncated": false,
    "need_clear_history": false,
    "is_safe": 0,
    "usage": {
        "prompt_tokens": 14,
        "completion_tokens": 334,
        "total_tokens": 348
    }
}

请求示例(多轮)

# 步骤一,获取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 -X POST 'https://aip.baidubce.com/rpc/2.0/ai_custom/v1/wenxinworkshop/chat/xuanyuan_70b_chat?access_token=[步骤一调用接口获取的access_token]' -d '{
  "messages": [
    {"role":"user","content":"请介绍一下等额本金的特点"},
    {"role":"assistant","content":"等额本金的特点是每月还款金额逐渐减少,前期还款金额较大,后期逐渐减少,最后一期还款金额最小。"},
    {"role":"user","content": "等额本金和等额本息有什么区别?"}
  ]
}'  | iconv -f utf-8 -t utf-8
import requests
import json


def main():
        
    url = "https://aip.baidubce.com/rpc/2.0/ai_custom/v1/wenxinworkshop/chat/xuanyuan_70b_chat?access_token=[调用鉴权接口获取的token]"
    
    payload = json.dumps({
        "messages": [
            {
                "role": "user",
                "content": "请介绍一下等额本金的特点"
            },
            {
                "role": "assistant",
                "content": "等额本金的特点是每月还款金额逐渐减少,前期还款金额较大,后期逐渐减少,最后一期还款金额最小。"
            },
            {
                "role": "user",
                "content": "等额本金和等额本息有什么区别?"
            }
        ]
    })
    headers = {
        'Content-Type': 'application/json'
    }
    
    response = requests.request("POST", url, headers=headers, data=payload)
    
    print(response.text)
    

if __name__ == '__main__':
    main()

响应示例(多轮)

{
    "id": "as-gc9stq5sfg",
    "object": "chat.completion",
    "created": 1700727730,
    "result": " 等额本金和等额本息是两种常见的还款方式,它们在还款金额和还款期限上有着区别。\n\n首先,等额本金的特点是每月还款金额逐渐减少,前期还款金额较大,后期逐渐减少,最后一期还款金额最小。而等额本息的特点是每月还款金额固定不变,但是前期还款中本金所占比例较大,后期逐渐向利息所占比例增加。\n\n另外,还款期限也是两种方式的区别之一。等额本金的还款期限相对较短,因为每月还款金额逐渐减少,所以在还款期限内本金所占的比例较小,利息所占比例较大。而等额本息的还款期限相对较长,因为每月还款金额固定不变,所以在还款期限内,利息所占比例较大,本金所占比例较小。\n\n综上所述,选择等额本金还是等额本息可以根据借款人的资金状况和个人偏好来决定。需要考虑的因素包括借款人的偿还能力、借款目的和期限等。最好在借款前咨询专业人士或者金融机构,以便做出更好的决策。",
    "is_truncated": false,
    "need_clear_history": false,
    "is_safe": 0,
    "usage": {
        "prompt_tokens": 14,
        "completion_tokens": 334,
        "total_tokens": 348
    }
}

请求示例(流式)

# 步骤一,获取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 -X POST 'https://aip.baidubce.com/rpc/2.0/ai_custom/v1/wenxinworkshop/chat/xuanyuan_70b_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/xuanyuan_70b_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()

响应示例(流式)

data: {"id":"as-uaehp44xhk","object":"chat.completion","created":1700735265,"sentence_id":0,"is_end":false,"is_truncated":false,"result":" 等额本金和等额本息是两种常见的还款方式,它们在还款过程中有着明显","need_clear_history":false,"is_safe":0,"usage":{"prompt_tokens":14,"completion_tokens":31,"total_tokens":45}}

data: {"id":"as-uaehp44xhk","object":"chat.completion","created":1700735266,"sentence_id":1,"is_end":false,"is_truncated":false,"result":"的区别。\n\n首先,等额本金是在还款期内每个月还款的本金金额保持一","need_clear_history":false,"is_safe":0,"usage":{"prompt_tokens":14,"completion_tokens":31,"total_tokens":45}}

data: {"id":"as-uaehp44xhk","object":"chat.completion","created":1700735270,"sentence_id":2,"is_end":false,"is_truncated":false,"result":"致的方式。借款人每个月需要偿还固定的本金金额,而利息会随着本金的","need_clear_history":false,"is_safe":0,"usage":{"prompt_tokens":14,"completion_tokens":31,"total_tokens":45}}

data: {"id":"as-uaehp44xhk","object":"chat.completion","created":1700735275,"sentence_id":3,"is_end":false,"is_truncated":false,"result":"逐步偿还而逐渐减少。在还款初期,利息占还款总额的比例较大,随着时","need_clear_history":false,"is_safe":0,"usage":{"prompt_tokens":14,"completion_tokens":31,"total_tokens":45}}

data: {"id":"as-uaehp44xhk","object":"chat.completion","created":1700735279,"sentence_id":4,"is_end":false,"is_truncated":false,"result":"间推移,本金逐渐增多,利息逐渐减少,还款总额逐渐减少。等额本金的","need_clear_history":false,"is_safe":0,"usage":{"prompt_tokens":14,"completion_tokens":31,"total_tokens":45}}

data: {"id":"as-uaehp44xhk","object":"chat.completion","created":1700735283,"sentence_id":5,"is_end":false,"is_truncated":false,"result":"特点是总的利息支出较低,前期还款额较大,后期逐渐减少,适合具备较","need_clear_history":false,"is_safe":0,"usage":{"prompt_tokens":14,"completion_tokens":31,"total_tokens":45}}

data: {"id":"as-uaehp44xhk","object":"chat.completion","created":1700735287,"sentence_id":6,"is_end":false,"is_truncated":false,"result":"强还款能力的借款人。\n\n而等额本息则是在整个还款期间,每月还款总","need_clear_history":false,"is_safe":0,"usage":{"prompt_tokens":14,"completion_tokens":31,"total_tokens":45}}

data: {"id":"as-uaehp44xhk","object":"chat.completion","created":1700735291,"sentence_id":7,"is_end":false,"is_truncated":false,"result":"额(本金+利息)保持一致。每个月还款的本金和利息的比例会有所变化","need_clear_history":false,"is_safe":0,"usage":{"prompt_tokens":14,"completion_tokens":31,"total_tokens":45}}

data: {"id":"as-uaehp44xhk","object":"chat.completion","created":1700735295,"sentence_id":8,"is_end":false,"is_truncated":false,"result":",一开始利息较多,本金较少,随着时间推移,本金逐渐增多,利息逐渐","need_clear_history":false,"is_safe":0,"usage":{"prompt_tokens":14,"completion_tokens":31,"total_tokens":45}}

data: {"id":"as-uaehp44xhk","object":"chat.completion","created":1700735299,"sentence_id":9,"is_end":false,"is_truncated":false,"result":"减少。等额本息的特点是每月还款总额固定,方便管理财务,适合对预算","need_clear_history":false,"is_safe":0,"usage":{"prompt_tokens":14,"completion_tokens":31,"total_tokens":45}}

data: {"id":"as-uaehp44xhk","object":"chat.completion","created":1700735306,"sentence_id":10,"is_end":false,"is_truncated":false,"result":"有一定要求的借款人。\n\n因此,选择等额本金还是等额本息应根据借款","need_clear_history":false,"is_safe":0,"usage":{"prompt_tokens":14,"completion_tokens":316,"total_tokens":330}}

data: {"id":"as-uaehp44xhk","object":"chat.completion","created":1700735308,"sentence_id":11,"is_end":false,"is_truncated":false,"result":"人的财务状况和还款需求来决定。等额本金可以减少总的利息支出,但需","need_clear_history":false,"is_safe":0,"usage":{"prompt_tokens":14,"completion_tokens":316,"total_tokens":330}}

data: {"id":"as-uaehp44xhk","object":"chat.completion","created":1700735312,"sentence_id":12,"is_end":false,"is_truncated":false,"result":"要在前期还款额较大;而等额本息每月还款总额固定,适合做预算规划。","need_clear_history":false,"is_safe":0,"usage":{"prompt_tokens":14,"completion_tokens":316,"total_tokens":330}}

data: {"id":"as-uaehp44xhk","object":"chat.completion","created":1700735316,"sentence_id":13,"is_end":false,"is_truncated":false,"result":"无论选择哪种方式,重要的是要考虑借款人的财务状况和个人偏好,并在","need_clear_history":false,"is_safe":0,"usage":{"prompt_tokens":14,"completion_tokens":316,"total_tokens":330}}

错误码

如果请求错误,服务器返回的JSON文本包含以下参数。

名称 描述
error_code 错误码
error_msg 错误描述信息,帮助理解和解决发生的错误

例如Access Token失效返回以下内容,需要重新获取新的Access Token再次请求。

{
  "error_code": 110,
  "error_msg": "Access token invalid or no longer valid"
}

更多相关错误码,请查看错误码说明

上一篇
XVERSE-13B-Chat
下一篇
DISC-MedLLM