如何计算 GPT 的 Tokens 数量?

基本介绍

随着人工智能大模型技术的迅速发展,一种创新的计费模式正在逐渐普及,即以“令牌”(Token)作为衡量使用成本的单位。那么,究竟什么是Token呢?

Token 是一种将自然语言文本转化为计算机可以理解的形式——词向量的手段。这个转化过程涉及对文本进行分词处理,将每个单词、汉字或字符转换为唯一的词向量表示。通过计算这些词向量在模型中的使用次数,服务提供商就能够量化用户所消耗的计算资源,并据此收取费用。

需要注意的是,不同的厂商可能采用不同的方式来定义和计算 Token。一般来说,一个 Token 可能代表一个汉字、一个英文单词,或者一个字符。

在大模型领域,通常情况下,服务商倾向于以千 Tokens(1K Tokens)为单位进行计费。用户可以通过购买一定数量的 Token 来支付模型训练和推理过程中产生的费用。
注意:Token的数量与使用模型的服务次数或数据处理量有关。一般是有梯度的,用得越多可以拿到越便宜的价格,和买东西的道理一样,零售一个价,批发一个价。

如何计算 Tokens 数量?

=======================

具体要怎么计算 Tokens 数量,这个需要官方提供计算方式,或提供接口,或提供源码。
这里以 openAI 的 GPT 为例,介绍 Tokens 的计算方式。

openAI 官方提供了两种计算方式:网页计算、接口计算。

## 网页计算

网页计算顾名思义,就是打开网页输入文字,然后直接计算结果,网页的链接是:https://platform.openai.com/tokenizer。
曾经看到一个粗略的说法:1 个 Token 大约相当于 4 个英文字符或 0.75 个英文单词;而一个汉字则大约需要 1.5 个 Token 来表示。真实性未知,但从个人经验,一个汉字似乎没有达到 1.5 个 Token 这么多。
随意举三个例子:

【例子1】以下十个汉字计算得到的 Token 数是 14 个。

一二三四五六七八九十

【例子2】以下 11 个汉字加2个标点计算得到的 Token 数是 13 个。

今天是十二月一日,星期五。

【例子3】以下 这段话计算得到的 Token 数是 236 个。

人工智能是智能学科重要的组成部分,它企图了解智能的实质,并生产出一种新的能以人类智能相似的方式做出反应的智能机器,该领域的研究包括机器人、语言识别、图像识别、自然语言处理和专家系统等。人工智能从诞生以来,理论和技术日益成熟,应用领域也不断扩大,可以设想,未来人工智能带来的科技产品,将会是人类智慧的“容器”。人工智能可以对人的意识、思维的信息过程的模拟。人工智能不是人的智能,但能像人那样思考、也可能超过人的智能。

接口计算


接下来看看怎么使用 Python 接口实现 Token 计算。
相关链接:https://github.com/openai/openai-cookbook/blob/main/examples/How_to_count_tokens_with_tiktoken.ipynb

从 Note 中可以了解到,要计算 Tokens 需要安装两个第三方包:tiktokenopenai。第一个包不需要 GPT 的 API Key 和 API Secret 便可使用,第二个需要有 GPT 的 API Key 和 API Secret 才能使用,由于某些限制,还需要海外代理。
不过,好消息是openai可以不用,使用tiktoken来计算即可。

先安装tiktoken包:



pip install tiktoken  




注:我使用的是 Python 3.9,默认安装的tiktoken版本是 0.5.1。

安装好tiktoken之后,直接看最后两个 cell(In[14] 和 In[15])。

完整代码如下:

def num_tokens_from_messages(messages, model="gpt-3.5-turbo-0613"):  
    """Return the number of tokens used by a list of messages."""  
    try:  
        encoding = tiktoken.encoding_for_model(model)  
    except KeyError:  
        print("Warning: model not found. Using cl100k_base encoding.")  
        encoding = tiktoken.get_encoding("cl100k_base")  
    if model in {  
        "gpt-3.5-turbo-0613",  
        "gpt-3.5-turbo-16k-0613",  
        "gpt-4-0314",  
        "gpt-4-32k-0314",  
        "gpt-4-0613",  
        "gpt-4-32k-0613",  
    }:  
        tokens_per_message = 3  
        tokens_per_name = 1  
    elif model == "gpt-3.5-turbo-0301":  
        tokens_per_message = 4  # every message follows <|start|>{role/name}\n{content}<|end|>\n  
        tokens_per_name = -1  # if there's a name, the role is omitted  
    elif "gpt-3.5-turbo" in model:  
        print("Warning: gpt-3.5-turbo may update over time. Returning num tokens assuming gpt-3.5-turbo-0613.")  
        return num_tokens_from_messages(messages, model="gpt-3.5-turbo-0613")  
    elif "gpt-4" in model:  
        print("Warning: gpt-4 may update over time. Returning num tokens assuming gpt-4-0613.")  
        return num_tokens_from_messages(messages, model="gpt-4-0613")  
    else:  
        raise NotImplementedError(  
            f"""num_tokens_from_messages() is not implemented for model {model}. See https://github.com/openai/openai-python/blob/main/chatml.md for information on how messages are converted to tokens."""  
        )  
    num_tokens = 0  
    for message in messages:  
        num_tokens += tokens_per_message  
        for key, value in message.items():  
            num_tokens += len(encoding.encode(value))  
            if key == "name":  
                num_tokens += tokens_per_name  
    num_tokens += 3  # every reply is primed with <|start|>assistant<|message|>  
    return num_tokens  
# let's verify the function above matches the OpenAI API response  
  
import openai  
  
example_messages = [  
    {  
        "role": "system",  
        "content": "You are a helpful, pattern-following assistant that translates corporate jargon into plain English.",  
    },  
    {  
        "role": "system",  
        "name": "example_user",  
        "content": "New synergies will help drive top-line growth.",  
    },  
    {  
        "role": "system",  
        "name": "example_assistant",  
        "content": "Things working well together will increase revenue.",  
    },  
    {  
        "role": "system",  
        "name": "example_user",  
        "content": "Let's circle back when we have more bandwidth to touch base on opportunities for increased leverage.",  
    },  
    {  
        "role": "system",  
        "name": "example_assistant",  
        "content": "Let's talk later when we're less busy about how to do better.",  
    },  
    {  
        "role": "user",  
        "content": "This late pivot means we don't have time to boil the ocean for the client deliverable.",  
    },  
]  
  
for model in [  
    "gpt-3.5-turbo-0301",  
    "gpt-3.5-turbo-0613",  
    "gpt-3.5-turbo",  
    "gpt-4-0314",  
    "gpt-4-0613",  
    "gpt-4",  
    ]:  
    print(model)  
    # example token count from the function defined above  
    print(f"{num_tokens_from_messages(example_messages, model)} prompt tokens counted by num_tokens_from_messages().")  
    # example token count from the OpenAI API  
    response = openai.ChatCompletion.create(  
        model=model,  
        messages=example_messages,  
        temperature=0,  
        max_tokens=1,  # we're only counting input tokens here, so let's not waste tokens on the output  
    )  
    print(f'{response["usage"]["prompt_tokens"]} prompt tokens counted by the OpenAI API.')  
    print()  


接下来处理一下以上代码,把 In[15] 中,和openai包相关的内容可以直接注释掉,然后执行代码。处理之后,可直接执行代码如下:

import tiktoken  
def num_tokens_from_messages(messages, model="gpt-3.5-turbo-0613"):  
    """Return the number of tokens used by a list of messages."""  
    try:  
        encoding = tiktoken.encoding_for_model(model)  
    except KeyError:  
        print("Warning: model not found. Using cl100k_base encoding.")  
        encoding = tiktoken.get_encoding("cl100k_base")  
    if model in {  
        "gpt-3.5-turbo-0613",  
        "gpt-3.5-turbo-16k-0613",  
        "gpt-4-0314",  
        "gpt-4-32k-0314",  
        "gpt-4-0613",  
        "gpt-4-32k-0613",  
    }:  
        tokens_per_message = 3  
        tokens_per_name = 1  
    elif model == "gpt-3.5-turbo-0301":  
        tokens_per_message = 4  # every message follows <|start|>{role/name}\n{content}<|end|>\n  
        tokens_per_name = -1  # if there's a name, the role is omitted  
    elif "gpt-3.5-turbo" in model:  
        print("Warning: gpt-3.5-turbo may update over time. Returning num tokens assuming gpt-3.5-turbo-0613.")  
        return num_tokens_from_messages(messages, model="gpt-3.5-turbo-0613")  
    elif "gpt-4" in model:  
        print("Warning: gpt-4 may update over time. Returning num tokens assuming gpt-4-0613.")  
        return num_tokens_from_messages(messages, model="gpt-4-0613")  
    else:  
        raise NotImplementedError(  
            f"""num_tokens_from_messages() is not implemented for model {model}. See https://github.com/openai/openai-python/blob/main/chatml.md for information on how messages are converted to tokens."""  
        )  
    num_tokens = 0  
    for message in messages:  
        num_tokens += tokens_per_message  
        for key, value in message.items():  
            num_tokens += len(encoding.encode(value))  
            if key == "name":  
                num_tokens += tokens_per_name  
    num_tokens += 3  # every reply is primed with <|start|>assistant<|message|>  
    return num_tokens  
# let's verify the function above matches the OpenAI API response  
  
example_messages = [  
    {  
        "role": "system",  
        "content": "You are a helpful, pattern-following assistant that translates corporate jargon into plain English.",  
    },  
    {  
        "role": "system",  
        "name": "example_user",  
        "content": "New synergies will help drive top-line growth.",  
    },  
    {  
        "role": "system",  
        "name": "example_assistant",  
        "content": "Things working well together will increase revenue.",  
    },  
    {  
        "role": "system",  
        "name": "example_user",  
        "content": "Let's circle back when we have more bandwidth to touch base on opportunities for increased leverage.",  
    },  
    {  
        "role": "system",  
        "name": "example_assistant",  
        "content": "Let's talk later when we're less busy about how to do better.",  
    },  
    {  
        "role": "user",  
        "content": "This late pivot means we don't have time to boil the ocean for the client deliverable.",  
    },  
]  
  
for model in [  
    "gpt-3.5-turbo-0301",  
    "gpt-3.5-turbo-0613",  
    "gpt-3.5-turbo",  
    "gpt-4-0314",  
    "gpt-4-0613",  
    "gpt-4",  
    ]:  
    print(model)  
    # example token count from the function defined above  
    print(f"{num_tokens_from_messages(example_messages, model)} prompt tokens counted by num_tokens_from_messages().")  
    print()  


运行结果如下图:

小解析:

  • example_messages变量是一个列表,列表的元素是字典,这个是 GPT 的数据结构,在这个示例代码中,整个列表作为 GPT 的 prompt 输入,所以计算的是整个的 Token 数。

  • 不同的模型,对于 prompt 的计算规则有一点点不同,重点在于数据结构多出的字符。

问题1:实际生产中的数据,可能不是这样的,更多时候是存一个字符串,又该怎么处理?

demo 是从列表解析出键content的值,这个比较简单,如果是要从字符串中去解析相关的数据,则需要多加一步转化,使用json包将字符串转化为列表,然后其他的处理方式保持一致即可。
参考如下:

import tiktoken,json  
def num_tokens_from_messages(messages, model="gpt-3.5-turbo-0613"):  
    """Return the number of tokens used by a list of messages."""  
    try:  
        encoding = tiktoken.encoding_for_model(model)  
    except KeyError:  
        print("Warning: model not found. Using cl100k_base encoding.")  
        encoding = tiktoken.get_encoding("cl100k_base")  
    if model in {  
        "gpt-3.5-turbo-0613",  
        "gpt-3.5-turbo-16k-0613",  
        "gpt-4-0314",  
        "gpt-4-32k-0314",  
        "gpt-4-0613",  
        "gpt-4-32k-0613",  
    }:  
        tokens_per_message = 3  
        tokens_per_name = 1  
    elif model == "gpt-3.5-turbo-0301":  
        tokens_per_message = 4  # every message follows <|start|>{role/name}\n{content}<|end|>\n  
        tokens_per_name = -1  # if there's a name, the role is omitted  
    elif "gpt-3.5-turbo" in model:  
        print("Warning: gpt-3.5-turbo may update over time. Returning num tokens assuming gpt-3.5-turbo-0613.")  
        return num_tokens_from_messages(messages, model="gpt-3.5-turbo-0613")  
    elif "gpt-4" in model:  
        print("Warning: gpt-4 may update over time. Returning num tokens assuming gpt-4-0613.")  
        return num_tokens_from_messages(messages, model="gpt-4-0613")  
    else:  
        raise NotImplementedError(  
            f"""num_tokens_from_messages() is not implemented for model {model}. See https://github.com/openai/openai-python/blob/main/chatml.md for information on how messages are converted to tokens."""  
        )  
    # 结构转化,结构不完整则返回0  
    try:  
        messages = json.loads(messages)  
        num_tokens = 0  
        for message in messages:  
            num_tokens += tokens_per_message  
            for key, value in message.items():  
                num_tokens += len(encoding.encode(value))  
                if key == "name":  
                    num_tokens += tokens_per_name  
        num_tokens += 3  # every reply is primed with <|start|>assistant<|message|>  
    except json.JSONDecodeError:  
        num_tokens = 0  
    return num_tokens  
# let's verify the function above matches the OpenAI API response  
  
example_messages = [  
    {  
        "role": "system",  
        "content": "You are a helpful, pattern-following assistant that translates corporate jargon into plain English.",  
    },  
    {  
        "role": "system",  
        "name": "example_user",  
        "content": "New synergies will help drive top-line growth.",  
    },  
    {  
        "role": "system",  
        "name": "example_assistant",  
        "content": "Things working well together will increase revenue.",  
    },  
    {  
        "role": "system",  
        "name": "example_user",  
        "content": "Let's circle back when we have more bandwidth to touch base on opportunities for increased leverage.",  
    },  
    {  
        "role": "system",  
        "name": "example_assistant",  
        "content": "Let's talk later when we're less busy about how to do better.",  
    },  
    {  
        "role": "user",  
        "content": "This late pivot means we don't have time to boil the ocean for the client deliverable.",  
    },  
]  
example_messages = json.dumps(example_messages)  
  
# 假设使用的是 "gpt-4-0613" 模型  
model = "gpt-4-0613"  
print(f"{num_tokens_from_messages(example_messages, model)} prompt tokens counted by num_tokens_from_messages().")

问题2:在网页计算小节中使用的字符串跑出来的数据是否和tiktoken一样呢?

实现这个验证很简单,把上面的代码再做简化,直接计算字符串即可。参考逻辑如下:

import tiktoken  
  
def num_tokens_from_messages(messages, model="gpt-3.5-turbo-0613"):  
    """Return the number of tokens used by a list of messages."""  
    try:  
        encoding = tiktoken.encoding_for_model(model)  
    except KeyError:  
        print("Warning: model not found. Using cl100k_base encoding.")  
        encoding = tiktoken.get_encoding("cl100k_base")  
  
    num_tokens = len(encoding.encode(messages))  
    return num_tokens  
  
  
str1 = num_tokens_from_messages('一二三四五六七八九十')  
str2 = num_tokens_from_messages('今天是十二月一日,星期五。')  
str3 = num_tokens_from_messages('人工智能是智能学科重要的组成部分,它企图了解智能的实质,并生产出一种新的能以人类智能相似的方式做出反应的智能机器,该领域的研究包括机器人、语言识别、图像识别、自然语言处理和专家系统等。人工智能从诞生以来,理论和技术日益成熟,应用领域也不断扩大,可以设想,未来人工智能带来的科技产品,将会是人类智慧的“容器”。人工智能可以对人的意识、思维的信息过程的模拟。人工智能不是人的智能,但能像人那样思考、也可能超过人的智能。')  
  
print(f'字符串1长度{str1},字符串2长度{str2},字符串3长度{str3}。')  


返回结果如下:

返回结果和网页计算的结果完全一致!

其实这个有点像是 GPT 给我们返回的文本数据,可以直接计算其长度,不需要像上面那么复杂,如果数据结构也是像上面一样,那就需要多加一步解析。

import tiktoken,json  
  
def num_tokens_from_messages(messages):  
    """Return the number of tokens used by a list of messages."""  
    try:  
        encoding = tiktoken.encoding_for_model(model)  
    except KeyError:  
        print("Warning: model not found. Using cl100k_base encoding.")  
        encoding = tiktoken.get_encoding("cl100k_base")  
  
    try:  
        messages = json.loads(messages)[0]['content']  
        num_tokens = len(encoding.encode(messages))  
    except json.JSONDecodeError:  
        num_tokens = 0  
    return num_tokens  
  
example_messages = '''[  
    {  
        "role": "system",  
        "content": "一二三四五六七八九十"  
    }  
]'''  
print(num_tokens_from_messages(example_messages))  


小结

=========

本文主要介绍了 GPT 如何计算 Tokens 的方法,官方提供了两种方式:网页计算和接口计算。
网页计算不需要技术,只需要魔法即可体验,而接口计算,事实上接口计算包含了两种方法,一种使用tiktoken,则需要点 Python 基础,而openai还需要点网络基础和货币基础,需要代理和 plus 账号(20刀/月)等。

如何学习AI大模型?

作为一名热心肠的互联网老兵,我决定把宝贵的AI知识分享给大家。 至于能学习到多少就看你的学习毅力和能力了 。我已将重要的AI大模型资料包括AI大模型入门学习思维导图、精品AI大模型学习书籍手册、视频教程、实战学习等录播视频免费分享出来。

这份完整版的大模型 AI 学习资料已经上传CSDN,朋友们如果需要可以微信扫描下方CSDN官方认证二维码免费领取【保证100%免费

一、全套AGI大模型学习路线

AI大模型时代的学习之旅:从基础到前沿,掌握人工智能的核心技能!

img

二、640套AI大模型报告合集

这套包含640份报告的合集,涵盖了AI大模型的理论研究、技术实现、行业应用等多个方面。无论您是科研人员、工程师,还是对AI大模型感兴趣的爱好者,这套报告合集都将为您提供宝贵的信息和启示。

img

三、AI大模型经典PDF籍

随着人工智能技术的飞速发展,AI大模型已经成为了当今科技领域的一大热点。这些大型预训练模型,如GPT-3、BERT、XLNet等,以其强大的语言理解和生成能力,正在改变我们对人工智能的认识。 那以下这些PDF籍就是非常不错的学习资源。

img

四、AI大模型商业化落地方案

img

作为普通人,入局大模型时代需要持续学习和实践,不断提高自己的技能和认知水平,同时也需要有责任感和伦理意识,为人工智能的健康发展贡献力量。

<think>好的,我现在需要帮助用户解决如何计算URL中的tokens的问题。首先,我得理解用户的具体需求。他们提到URL中涉及的tokens,可能是指在使用某些API时,URL中包含的访问令牌(如API密钥或访问令牌)的计算方法,或者是自然语言处理中URL作为文本时的token计数。根据用户提供的引用内容,引用[1]提到了免费tokens链接,引用[3]和[4]涉及GitLab访问令牌和API的使用,所以可能需要考虑这两方面的情况。 首先,我需要明确“计算URL中的tokens”具体指的是什么。如果是计算URL中访问令牌的生成或使用,比如GitLab的个人访问令牌,可能需要按照引用[3]中的步骤生成,而计算可能涉及权限范围、有效期等。但如果是处理URL作为文本时的token数量,比如在NLP模型中,用户可能想知道如何统计URL字符串占用的tokens数,这在调用如GPT模型时很重要,因为费用与token数量相关。 接下来,我需要检查用户提供的引用内容是否有相关信息。引用[4]提到通过Dify连接模型并使用API,这可能涉及将URL作为输入时的处理。但引用中没有直接说明如何计算URL的tokens,所以可能需要结合一般知识来回答。 如果是API访问令牌,如GitLab的访问令牌,计算可能不涉及长度或数量,而是生成和管理。但用户的问题是关于“计算”,可能更倾向于统计URL字符串的token数量。例如,在OpenAI的模型中,一个token大约相当于4个字符,URL作为字符串会被分词处理。不同的分词方式会影响token数量,比如较长的URL可能被拆分成多个tokens。 需要分情况讨论:1. URL作为访问令牌的参数,如?token=xxx,这里的token值可能需要按生成规则计算;2. URL整体作为文本时的token计数。根据用户提供的引用,可能更偏向于第二种情况,尤其是在使用API处理URL时如何计算其消耗的tokens。 然后,我需要验证是否有工具或方法可以计算URL的tokens。例如,OpenAI提供了tiktoken库,可以用于统计文本的token数量。用户可能需要将URL字符串输入该库来获取准确计数。此外,需要注意不同模型的分词器可能不同,比如GPT-3和GPT-4可能有不同的分词方式。 还需要考虑URL的结构是否影响token数量。例如,特殊字符如斜杠、点等可能被分词器处理为单独的tokens,或者与相邻字符合并。例如,"https://"可能被分成一个token,而路径部分可能被拆分成多个。 总结回答的结构:先区分两种情况(访问令牌本身的计算 vs URL作为文本的token计数),然后分别说明。根据用户引用内容,可能重点在第二种情况,并提供具体方法,如使用tiktoken库,或在线工具,同时提醒注意事项如不同模型的分词差异。 最后,生成相关问题,确保覆盖用户可能感兴趣的扩展内容,比如优化token使用、不同模型的分词区别等。</think>要计算URL中的tokens,需根据具体场景选择方法。以下是两种主要情况的处理方式: ### 一、URL作为访问令牌参数 当URL中包含形如`?token=<value>`的访问令牌时: 1. **令牌生成规则**:按引用[3]所述,访问令牌由系统按指定字符集(如字母+数字+符号)生成,长度由系统预设(如GitLab默认20字符) 2. **计算逻辑**:假设令牌长度为$n$,字符集大小$k$,则理论组合数为$k^n$。例如使用62种字符(26大写+26小写+10数字)的8位令牌,组合数为$62^8≈2.18×10^{14}$ ### 二、URL作为文本时的NLP tokens计算 当需要统计URL字符串在NLP模型中的token消耗时: 1. **使用分词工具**:如OpenAI的`tiktoken`库 ```python import tiktoken encoder = tiktoken.encoding_for_model("gpt-4") url = "https://example.com/api?token=abc123" tokens = encoder.encode(url) print(len(tokens)) # 输出token数量 ``` 2. **在线估算工具**:通过[OpenAI Tokenizer](https://platform.openai.com/tokenizer)输入URL可直接查看分词结果 3. **经验公式**:英文场景下约$token≈char/4$,但特殊符号可能增加token数。例如`https://`可能被拆分为`['http', 's', '://']`等多个tokens[^4] ### 注意事项 1. **编码差异**:不同模型分词器结果不同,如`cl100k_base`(GPT-4)与`p50k_base`(GPT-3)的分词粒度存在差异 2. **缩短策略**:可对URL进行编码压缩,如将`https://`简写为`h://`,最多可减少6个tokens(从3 tokens降为1 token
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值