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

千帆prompt_token用量计费逻辑

本文介绍了token计算规则,并以 ERNIE-3.5-8K 为例说明计算过程。
说明:token计算规则,后续会进一步优化完善,请您保持关注。

ERNIE3.5、ERNIE4.0,ERNIE4.0 turbo 系列模型接口(包含Latest版本、preivew版本、带日期退役版本),触发检索的需要单独计费0.008元/次,并且会触发prompt_tokens增加,在大模型推理接口返回的usage.prompt_tokens_details.search_tokens当中体现。详细说明可查看参考内容

名词解释

字符

  • 1个字母=1个字符,举例,hello=5字符
  • 1个汉字=1个字符,举例,你好=2字符

token

大模型中,token是指语言模型中用来表示中文汉字、英文单词、或中英文短语的符号。token可以是单个字符,也可以是多个字符组成的序列。您可以使用token计算器来在线计算部分文心大模型的字符转token数。

输入长度限制逻辑

  • 字符长度限制:使用 输入token长度*4 进行第一道拦截,比如ernie-3.5-8k接口,输入token长度限制为5k,字符长度限制为20000,超过返回错误码 336007-the max length of current question is 20000
  • token长度限制:使用 token长度进行第二道拦截,超过返回错误码 336103-Prompt tokens too long
  • 多轮对话场景,输入token长度不断变长,可能导致输入长度超限,此时用户可以使用千帆ModelBuilder SDK提供的自动遗忘历史对话功能,避免输入长度过长。

prompt_tokens计算规则

以下信息参与prompt_tokens计算:

  • message中的content
  • system
  • functions
  • 触发搜索增强以后是search_tokens

示例

预期效果

用户调用ERNIE-3.5-8K API能力,请求参数使用了message.content、functions、system等,各请求参数内容如下。
调用接口后,返回prompt_tokens字段值为564,即问题tokens数为564,包括请求参数message.content、functions、system对应的内容。

各请求参数内容

(1)message中content字段内容

"messages": [{
        "role": "user",
        "content": "当前日期2023-09-30, 查询李四最近五个月旅游过的国内城市"
    }]

(2)system字段内容

"system": "你是一个AI智能助手",

(3)functions字段内容

"functions": [
        {
            "name": "get_domestic_tourist_place",
            "description": "当前日期为 2023-09-30,查询用户近期旅游过的三个国内城市",
            "parameters": {
                "type": "object",
                "properties": {
                    "name": {
                        "type": "string",
                        "description": "用户姓名"
                    },
                    "start": {
                        "type": "string",
                        "description": "查询的起始时间,默认最近一年"
                    },
                    "end": {
                        "type": "string",
                        "description": "查询的结束时间,默认当前日期"
                    }
                },
                "required": ["name"]
            },
            "responses": {
                "type": "object",
                "properties": {
                    "result": {
                        "type": "string",
                        "description": "目的地列表"
                    }
                }
            },
            "examples": [
                {
                    "role": "user",
                    "content": "查询张三最近三个月旅游过的国内城市"
                },
                {
                    "role": "assistant",
                    "content": "",
                    "function_call": {
                        "name": "get_domestic_tourist_place",
                        "arguments": "{\"name\": \"张三\", \"start\": \"2023-05-01-01\", \"end\": \"2023-08-30-30\"}"
                    }
                },
                {
                    "role": "function",
                    "name": "get_domestic_tourist_place",
                    "content": "{\"result\": \"苏州;上海;北京\"}"
                },
                {
                    "role": "assistant",
                    "content": "张三最近三个月旅游过的国内城市为北京,广州,呼和浩特。"
                }
            ]
        },
        {
            "name": "get_foreign_tourist_place",
            "description": "当前日期为 2023-10,查询用户近期旅游过的三个国外城市",
            "parameters": {
                "type": "object",
                "properties": {
                    "name": {
                        "type": "string",
                        "description": "用户姓名"
                    },
                    "start": {
                        "type": "string",
                        "description": "查询的起始时间,默认最近一年"
                    },
                    "end": {
                        "type": "string",
                        "description": "查询的结束时间,默认当前日期"
                    }
                },
                "required": ["name"]
            },
            "responses": {
                "type": "object",
                "properties": {
                    "result": {
                        "type": "string",
                        "description": "目的地列表"
                    }
                }
            },
            "examples": [
                {
                    "role": "user",
                    "content": "查询张三最近三个月旅游过的国外城市"
                },
                {
                    "role": "assistant",
                    "content": "",
                    "function_call": {
                        "name": "get_foreign_tourist_place",
                        "arguments": "{\"name\": \"张三\", \"start\": \"2023-05-01\", \"end\": \"2023-08-30\"}"
                    }
                },
                {
                    "role": "function",
                    "name": "get_foreign_tourist_place",
                    "content": "{\"result\": \"仰光;巴瓦提\"}"
                },
                {
                    "role": "assistant",
                    "content": "张三最近三个月旅游过的国外城市为仰光,巴瓦提。"
                }
            ]
        }
    ],

调用ERNIE-3.5-8K API 查看prompt_token

步骤一:使用以下方法调用ERNIE-3.5-8K大模型。

curl --location 'https://aip.baidubce.com/rpc/2.0/ai_custom/v1/wenxinworkshop/chat/completions?access_token={access_token}' \
--header 'Content-Type: application/json' \
--data '{
    "messages":[
        {
            "role":"user",
            "content":"当前日期2023-09-30, 查询李四最近五个月旅游过的国内城市"
        }
    ],
    "functions":[
        {
            "name":"get_domestic_tourist_place",
            "description":"当前日期为 2023-09-30,查询用户近期旅游过的三个国内城市",
            "parameters":{
                "type":"object",
                "properties":{
                    "name":{
                        "type":"string",
                        "description":"用户姓名"
                    },
                    "start":{
                        "type":"string",
                        "description":"查询的起始时间,默认最近一年"
                    },
                    "end":{
                        "type":"string",
                        "description":"查询的结束时间,默认当前日期"
                    }
                },
                "required":[
                    "name"
                ]
            },
            "responses":{
                "type":"object",
                "properties":{
                    "result":{
                        "type":"string",
                        "description":"目的地列表"
                    }
                }
            },
            "examples":[
                {
                    "role":"user",
                    "content":"查询张三最近三个月旅游过的国内城市"
                },
                {
                    "role":"assistant",
                    "content":"",
                    "function_call":{
                        "name":"get_domestic_tourist_place",
                        "arguments":"{\"name\": \"张三\", \"start\": \"2023-05-01-01\", \"end\": \"2023-08-30-30\"}"
                    }
                },
                {
                    "role":"function",
                    "name":"get_domestic_tourist_place",
                    "content":"{\"result\": \"苏州;上海;北京\"}"
                },
                {
                    "role":"assistant",
                    "content":"张三最近三个月旅游过的国内城市为北京,广州,呼和浩特。"
                }
            ]
        },
        {
            "name":"get_foreign_tourist_place",
            "description":"当前日期为 2023-10,查询用户近期旅游过的三个国外城市",
            "parameters":{
                "type":"object",
                "properties":{
                    "name":{
                        "type":"string",
                        "description":"用户姓名"
                    },
                    "start":{
                        "type":"string",
                        "description":"查询的起始时间,默认最近一年"
                    },
                    "end":{
                        "type":"string",
                        "description":"查询的结束时间,默认当前日期"
                    }
                },
                "required":[
                    "name"
                ]
            },
            "responses":{
                "type":"object",
                "properties":{
                    "result":{
                        "type":"string",
                        "description":"目的地列表"
                    }
                }
            },
            "examples":[
                {
                    "role":"user",
                    "content":"查询张三最近三个月旅游过的国外城市"
                },
                {
                    "role":"assistant",
                    "content":"",
                    "function_call":{
                        "name":"get_foreign_tourist_place",
                        "arguments":"{\"name\": \"张三\", \"start\": \"2023-05-01\", \"end\": \"2023-08-30\"}"
                    }
                },
                {
                    "role":"function",
                    "name":"get_foreign_tourist_place",
                    "content":"{\"result\": \"仰光;巴瓦提\"}"
                },
                {
                    "role":"assistant",
                    "content":"张三最近三个月旅游过的国外城市为仰光,巴瓦提。"
                }
            ]
        }
    ],
    "system":"你是一个AI智能助手"
}'

步骤二:成功调用接口,在响应字段中查看usage中prompt_tokens值为564。

{
  "id": "as-2kx0t93hip",
  "object": "chat.completion",
  "created": 1703060563,
  "result": "",
  "is_truncated": false,
  "need_clear_history": false,
  "function_call": {
    "name": "get_domestic_tourist_place",
    "thoughts": "根据用户的请求,我需要调用“get_domestic_tourist_place”工具来查询李四最近五个月旅游过的国内城市。我将设置参数“name”为“李四”,开始和结束日期分别为“2023-04-01”和“2023-09-30”。",
    "arguments": "{\"name\":\"李四\",\"start\":\"2023-04-01\",\"end\":\"2023-09-30\"}"
  },
  "finish_reason": "function_call",
  "usage": {
    "prompt_tokens": 564,
    "completion_tokens": 112,
    "total_tokens": 676
  }
}

如何计算prompt_token

在使用大模型服务时,可以通过以下方式计算prompt_token对应的值。
以示例中message.content、functions、system等内容为例进行说明计算过程。

计算流程简介

计算prompt_token步骤如下:

步骤一:计算脚本

import json
import time
import requests

# 获取传递给大模型计算token长度的入参
def get_prompt(request):

    
    prompt = ""
    for message in request["messages"]:
        prompt = prompt + message["content"]
    if "system" in request:
        prompt = prompt + request["system"]
    if "functions" in request:
        prompt = prompt + json.dumps(request["functions"], separators=(',', ':'), ensure_ascii=False)

    print(prompt)


# 接口完整body入参
call_function = {
    "messages": [{
        "role": "user",
        "content": "当前日期2023-09-30, 查询李四最近五个月旅游过的国内城市"
    }],
    "functions": [
        {
            "name": "get_domestic_tourist_place",
            "description": "当前日期为 2023-09-30,查询用户近期旅游过的三个国内城市",
            "parameters": {
                "type": "object",
                "properties": {
                    "name": {
                        "type": "string",
                        "description": "用户姓名"
                    },
                    "start": {
                        "type": "string",
                        "description": "查询的起始时间,默认最近一年"
                    },
                    "end": {
                        "type": "string",
                        "description": "查询的结束时间,默认当前日期"
                    }
                },
                "required": ["name"]
            },
            "responses": {
                "type": "object",
                "properties": {
                    "result": {
                        "type": "string",
                        "description": "目的地列表"
                    }
                }
            },
            "examples": [
                {
                    "role": "user",
                    "content": "查询张三最近三个月旅游过的国内城市"
                },
                {
                    "role": "assistant",
                    "content": "",
                    "function_call": {
                        "name": "get_domestic_tourist_place",
                        "arguments": "{\"name\": \"张三\", \"start\": \"2023-05-01-01\", \"end\": \"2023-08-30-30\"}"
                    }
                },
                {
                    "role": "function",
                    "name": "get_domestic_tourist_place",
                    "content": "{\"result\": \"苏州;上海;北京\"}"
                },
                {
                    "role": "assistant",
                    "content": "张三最近三个月旅游过的国内城市为北京,广州,呼和浩特。"
                }
            ]
        },
        {
            "name": "get_foreign_tourist_place",
            "description": "当前日期为 2023-10,查询用户近期旅游过的三个国外城市",
            "parameters": {
                "type": "object",
                "properties": {
                    "name": {
                        "type": "string",
                        "description": "用户姓名"
                    },
                    "start": {
                        "type": "string",
                        "description": "查询的起始时间,默认最近一年"
                    },
                    "end": {
                        "type": "string",
                        "description": "查询的结束时间,默认当前日期"
                    }
                },
                "required": ["name"]
            },
            "responses": {
                "type": "object",
                "properties": {
                    "result": {
                        "type": "string",
                        "description": "目的地列表"
                    }
                }
            },
            "examples": [
                {
                    "role": "user",
                    "content": "查询张三最近三个月旅游过的国外城市"
                },
                {
                    "role": "assistant",
                    "content": "",
                    "function_call": {
                        "name": "get_foreign_tourist_place",
                        "arguments": "{\"name\": \"张三\", \"start\": \"2023-05-01\", \"end\": \"2023-08-30\"}"
                    }
                },
                {
                    "role": "function",
                    "name": "get_foreign_tourist_place",
                    "content": "{\"result\": \"仰光;巴瓦提\"}"
                },
                {
                    "role": "assistant",
                    "content": "张三最近三个月旅游过的国外城市为仰光,巴瓦提。"
                }
            ]
        }
    ],
    "system": "你是一个AI智能助手"
}
call_function_payload = json.dumps(call_function, indent=4, ensure_ascii=False)

get_prompt(call_function)

步骤二:查看输出结果

当前日期2023-09-30, 查询李四最近五个月旅游过的国内城市你是一个AI智能助手[{"name":"get_domestic_tourist_place","description":"当前日期为 2023-09-30,查询用户近期旅游过的三个国内城市","parameters":{"type":"object","properties":{"name":{"type":"string","description":"用户姓名"},"start":{"type":"string","description":"查询的起始时间,默认最近一年"},"end":{"type":"string","description":"查询的结束时间,默认当前日期"}},"required":["name"]},"responses":{"type":"object","properties":{"result":{"type":"string","description":"目的地列表"}}},"examples":[{"role":"user","content":"查询张三最近三个月旅游过的国内城市"},{"role":"assistant","content":"","function_call":{"name":"get_domestic_tourist_place","arguments":"{\"name\": \"张三\", \"start\": \"2023-05-01-01\", \"end\": \"2023-08-30-30\"}"}},{"role":"function","name":"get_domestic_tourist_place","content":"{\"result\": \"苏州;上海;北京\"}"},{"role":"assistant","content":"张三最近三个月旅游过的国内城市为北京,广州,呼和浩特。"}]},{"name":"get_foreign_tourist_place","description":"当前日期为 2023-10,查询用户近期旅游过的三个国外城市","parameters":{"type":"object","properties":{"name":{"type":"string","description":"用户姓名"},"start":{"type":"string","description":"查询的起始时间,默认最近一年"},"end":{"type":"string","description":"查询的结束时间,默认当前日期"}},"required":["name"]},"responses":{"type":"object","properties":{"result":{"type":"string","description":"目的地列表"}}},"examples":[{"role":"user","content":"查询张三最近三个月旅游过的国外城市"},{"role":"assistant","content":"","function_call":{"name":"get_foreign_tourist_place","arguments":"{\"name\": \"张三\", \"start\": \"2023-05-01\", \"end\": \"2023-08-30\"}"}},{"role":"function","name":"get_foreign_tourist_place","content":"{\"result\": \"仰光;巴瓦提\"}"},{"role":"assistant","content":"张三最近三个月旅游过的国外城市为仰光,巴瓦提。"}]}]

步骤三:使用token计算器计算prompt_token长度

打开token计算器功能,并将步骤二的结果复制到token计算的文本输入框,点击开始计算prompt_token的长度。计算结果Tokens数量为564,如下图所示。

image.png

触发搜索prompt_tokens扩大及独立按次计费说明

接口范围

  1. ERNIE-3.5、ERNIE-4.0、ERNIE-4.0-turbo系列接口(包含Latest版本、preivew版本、带日期退役版本),已与2024年9月27日正式商业化收费。

收费方案

触发搜索以后:

  1. prompt_tokens会扩大,可以在大模型推理接口返回的usage.prompt_tokens_details.search_tokens当中体现。与2024年9月27日正式生效。
  2. 每一次触发搜索,都会收取一次搜索费用,在大模型推理接口返回的usage.search_count当中体现。搜索增强按次收费当前限时免费,将于2024年10月31号正式商用。

关于按次收费与按tokens收费的具体介绍,请见常见问题文档中的“搜索增强会额外收费吗?”部分。

返回usage说明

    "usage": {
        "prompt_tokens": 3997,  # 总输入token
        "prompt_tokens_details": { # 输入token明细说明
            "search_tokens": 3990  # 输入token当中触发搜索增强以后增加的token。prompt_tokens-search_tokens为用户输入的可见token数量,不触发搜索不返回。
        },
        "completion_tokens": 264, # 大模型返回的可见token数量
        "total_tokens": 4261, # total_tokens=prompt_tokens+completion_tokens
        "search_count": 1  # 触发搜索增强次数,固定为1,不触发搜索不返回。
     }

示例

请求

curl --location 'https://aip.baidubce.com/rpc/2.0/ai_custom/v1/wenxinworkshop/chat/completions?access_token=-42601389' \
--header 'Content-Type: application/json' \
--data '{
   "messages": [
    {"role":"user","content":"24年中国奥运会奖牌数是多少"}
   ],
    "enable_trace":true
}'

返回

{
    "id": "as-qudm54qfnj",
    "object": "chat.completion",
    "created": 1725251975,
    "result": "关于2024年中国在奥运会上的奖牌数,目前还无法给出确切的数字,因为2024年巴黎奥运会尚未举行,奖牌的具体数量需要在奥运会结束后才能统计得出。\n\n然而,我们可以根据一些预测和分析来推测中国代表团在巴黎奥运会上的表现。根据Gracenote的预测,中国运动员在巴黎奥运会上预计将夺得37枚金牌,与美国并列金牌榜首位。这一预测是基于对历届奥运会、世锦赛和世界杯的个人及团体成绩的综合统计模型分析得出的。\n\n中国代表团在多个项目上都具备强劲的实力,如跳水、射击、游泳、举重、乒乓球和羽毛球等。这些项目在历届奥运会上都为中国代表团贡献了大量的奖牌,尤其是金牌。因此,中国代表团在巴黎奥运会上的奖牌数有望继续保持较高水平。\n\n需要注意的是,预测结果仅供参考,并不能完全代表实际比赛的结果。在奥运会这样的高水平赛事中,运动员的临场发挥、对手的表现以及比赛环境等多种因素都可能影响最终的奖牌数量。因此,我们还需要耐心等待巴黎奥运会的举行,并为中国代表团加油助威。",
    "is_truncated": false,
    "need_clear_history": false,
    "search_info": {
        "search_results": [
            {
                "index": 1,
                "url": "http://localsite.baidu.com/site/wjzsorv8/8cd47d9a-7797-42f3-9306-b902ded71161?qaId=970837&categoryLv1=教育培训&efs=1&ch=54&srcid=10014&source=natural&category=其他&eduFrom=136&botSourceType=46",
                "title": "24届奥运会奖牌榜排名表-百度知了爱学",
                "datasource_id": "aurora-1"
            },
            {
                "index": 2,
                "url": "https://baijiahao.baidu.com/s?id=1805890606295601739&wfr=spider&for=pc",
                "title": "中国队在2024年巴黎奥运会上的奖牌预测:多元优势项目助力奖牌榜",
                "datasource_id": "aurora-1"
            },
            {
                "index": 3,
                "url": "https://baijiahao.baidu.com/s?id=1805105440305684091&wfr=spider&for=pc",
                "title": "263金!中国体育代表团征战夏季奥运会奖牌数据全集",
                "datasource_id": "aurora-1"
            },
            {
                "index": 4,
                "url": "https://www.mayiwenku.com/p-45649341.html",
                "title": "2023年奥运会",
                "datasource_id": "aurora-1"
            }
        ]
    },
    "finish_reason": "normal",
    "usage": {
        "prompt_tokens": 3997,
        "completion_tokens": 264,
        "total_tokens": 4261,
        "prompt_tokens_details": {
            "search_tokens": 3990
        },
        "search_count": 1
     }
}

触发搜索以后token膨胀数量的大致计算

触发搜索以后,在search_results当中会给出可以外部访问的索引链接(部分公网不可访问的材料,并没有给出链接)。打开这些url地址,将其中的内容粘贴到百度的tokenizer计算器当中。可以看出来tokenizer器当中的token数量与prompt token数量接近。

 "search_info": {
        "search_results": [
            {
                "index": 1,
                "url": "http://localsite.baidu.com/site/wjzsorv8/8cd47d9a-7797-42f3-9306-b902ded71161?qaId=970837&categoryLv1=教育培训&efs=1&ch=54&srcid=10014&source=natural&category=其他&eduFrom=136&botSourceType=46",
                "title": "24届奥运会奖牌榜排名表-百度知了爱学",
                "datasource_id": "aurora-1"
            },
            {
                "index": 2,
                "url": "https://baijiahao.baidu.com/s?id=1805890606295601739&wfr=spider&for=pc",
                "title": "中国队在2024年巴黎奥运会上的奖牌预测:多元优势项目助力奖牌榜",
                "datasource_id": "aurora-1"
            },
            {
                "index": 3,
                "url": "https://baijiahao.baidu.com/s?id=1805105440305684091&wfr=spider&for=pc",
                "title": "263金!中国体育代表团征战夏季奥运会奖牌数据全集",
                "datasource_id": "aurora-1"
            },
            {
                "index": 4,
                "url": "https://www.mayiwenku.com/p-45649341.html",
                "title": "2023年奥运会",
                "datasource_id": "aurora-1"
            }
        ]
    }

账单

查看计费项账单:

可以看到搜索增强服务这个计费项,单价0.008元/次

上一篇
价格详情
下一篇
续费和退款说明