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

计算ERNIE系列Token长度

接口描述

根据输入prompt计算token数。

在线调试

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

鉴权说明

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

请求结构

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

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

{
  "prompt":"你好啊"
}

请求头域

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

请求参数

  • Query参数

只有访问凭证access_token鉴权方式,需使用Query参数。

访问凭证access_token鉴权

名称 类型 必填 描述
access_token string 通过API Key和Secret Key获取的access_token,参考Access Token获取
  • Body参数
名称 类型 必填 描述
prompt string prompt内容,最大 512000 字符
model string (1)如果填写此参数,模型可选值如下:
· ERNIE 4.0系列:ernie-4.0-8k
· ERNIE 3.5系列:ernie-3.5-8k
· ERNIE Speed系列: ernie-speed-8kernie-speed-128k
· ERNIE Lite系列:ernie-lite-8k
· 其他,可选值为 ernie-tiny-8kernie-char-8k
(2)如果值不在上述范围,model入参可以为空

响应头域

除公共头域外,无其它特殊头域。

响应参数

名称 类型 描述
id string 本轮对话的id
object string 回包类型,“tokenizer.erniebot”
created int 时间戳
usage usage token统计信息

usage说明

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

请求示例(未设置model)

# 步骤一,获取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/tokenizer/erniebot?access_token=[步骤一调用接口获取的access_token]' -d '{
  "prompt":"你好啊"
}'
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/tokenizer/erniebot?access_token=" + get_access_token()
    
    payload = json.dumps({
        "prompt": "你好啊"
    })
    headers = {
        'Content-Type': 'application/json'
    }
    
    response = requests.request("POST", url, headers=headers, data=payload)
    
    print(response.text)
    

if __name__ == '__main__':
    main()

响应示例(未设置model)

{
  "id": "as-biv9bzt19n",
  "object": "tokenizer.erniebot",
  "created": 1698655037,
  "usage": {
    "prompt_tokens": 2,
    "total_tokens": 2
  }
}

请求示例(设置model)

# 步骤一,获取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/tokenizer/erniebot?access_token=[步骤一调用接口获取的access_token]' -d '{
  "prompt":"你好啊",
  "model":"ernie-4.0-8k"
}'
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/tokenizer/erniebot?access_token=" + get_access_token()
    
    payload = json.dumps({
        "prompt": "你好啊",
        "model":"ernie-4.0-8k"
    })
    headers = {
        'Content-Type': 'application/json'
    }
    
    response = requests.request("POST", url, headers=headers, data=payload)
    
    print(response.text)
    

if __name__ == '__main__':
    main()

响应示例(设置model)

{
  "id": "as-7s4gejc58b",
  "object": "tokenizer.erniebot",
  "created": 1698655037,
  "usage": {
    "prompt_tokens": 1,
    "total_tokens": 1
  }
}

错误码

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

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

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

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

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

上一篇
平台训练模型调用API
下一篇
快速入门