NIUBOYI»Home › › › Chatgpt帮助文档 › 中文 | English
微信扫一扫 分享朋友圈

已有 1537 人浏览分享

[api设置]    What is OpenAI's TOKEN, and how is it calculated?

api设置 2024-6-6 15:46:521537 0
What Is a Token and How Is It Calculated?

What Is a Token?
Tokens can be thought of as parts of words. Before the API processes a request, the input is broken down into multiple tokens. These tokens are not cut precisely at the beginning or end of words; they may include trailing spaces or even subwords. Here are some useful rules of thumb for token length:

· 1 token ≈ 4 English characters
· 1 token ≈ ¾ of a word
· 100 tokens ≈ 75 words
or
· 1-2 sentences ≈ 30 tokens
· 1 paragraph ≈ 100 tokens
· 1,500 words ≈ 2048 tokens

To further understand how tokens accumulate, consider the following examples:

· Wayne Gretzky's quote "You miss 100% of the shots you don't take" contains 11 tokens.
· OpenAI's charter contains 476 tokens.
· A copy of the U.S. Declaration of Independence contains 1,695 tokens.

How words are split into tokens also depends on the language. For example, the Spanish "Cómo estás" ("How are you") contains 5 tokens (10 characters in total). A higher token-to-character ratio may make non-English API implementations more expensive.

You can use OpenAI's interactive Token tool to further explore tokenization, count tokens, and see how text is split into tokens. Note that the exact tokenization process differs across models. Newer models such as GPT-3.5 and GPT-4 use a different tokenizer from earlier models and generate different tokens for the same input text.

Alternatively, if you want to tokenize text programmatically, you can use Tiktoken, a fast BPE tokenizer designed specifically for OpenAI models.

Token Limits
Depending on the model used, a request can use up to 128,000 tokens, which are shared between the prompt and the completion. Some models (such as GPT-4 Turbo) have different limits for input and output tokens.

There are often creative ways to work within the limits, such as compressing the prompt text, splitting the text into smaller fragments, and so on.

Token Pricing
The API offers multiple model types at different prices. Pricing for requests also differs by model. For detailed token pricing information, please refer here.

Exploring Tokens
The API processes words according to context in the corpus data. The model receives a prompt, converts the input into a list of tokens, processes the prompt, and converts the predicted tokens back into the words that OpenAI sees in the response.

The same word may generate different tokens in different contexts. The following are examples of the API generating 'token values' based on context:
v_Sp27FQeadYK4lYgk8CA_l2mR70v-WSOUyXfjjpPj_Ny9Dc8RRWnxon4o_ct59dqOgPY_5D88puOFdquuKueWRsD8sM9DSB7yBntE3KqnrMPhHR91BHClTFTnab3u6i7p051PF4?expires=1717660800&signature=3e18937e682236e5b5751039ce72852ae479d111a9109892423904f24dc3ab25.jpg
_Q6XzFgIIAIDmUVdCFHF1vGyic8hG61Pc-3eOIAtOG29qBxEgXsUzCOm_k2xIn2UfrV9PPCIBuz6YOcPYru_ssvFZymE8ijKKxxHNEZd8Wx-BwimhfXXNipF6TXm2-Xy3grlXYwO?expires=1717660800&signature=d235dd097d2d149d9a6f283014b2dfb8cc45195a61068c4a6ce62f7db2a8a9f7.jpg
In the first example, the token “2266” represents “ red” (with a trailing space).
vvmePo3aWbhExL36QLsDYXe8OaeFvzeqEj4zqXqRGV-5xFA6W8zXu1QQMvsMlycoje4OJPCuA1YMLpokaAvaVH_CXzWuZInVhCE4wByMEiakQ9HObxECfbo36pKASPtKndcxgPd-?expires=1717660800&signature=bd784b2501f36c4e55d91d299ae3aa04c1609fcd7f256422f9a06827be8fb2f1.jpg
lRvNrEim9UIXmL5j6ahrf2Mcb46uTyMyoL66xygd2GYFJeP4o0DjyBg2B3lIXnZhd1GuWnqIQWJDJ7yrjSzX8JCNMnJ24BJ_Yfy5CLqbFny6Kns4kVIA2ofPhDBq_AYVyhU2ELrx?expires=1717660800&signature=f8df5f60461a7c449a4db93df7635de2155cb835a1a3dd349a4cdb9af075db6e.jpg
The token “2296” represents “ Red” (with a leading space and beginning with a capital letter), which is different from the token “2266” for lowercase “ red”.
4P-Lfk9pc5SwjUw0PqcOrCmT2Tyo1KE86v_xseULCBSbqXA7k2JVEvFMD3ZVu7PxAq0zFf1xV74r3eoou2RONtvZnxAQjHYig1iXYK98up5c-c1lJrMn-N9uxkCHl3MU3ilc8TTa?expires=1717660800&signature=7509e72fbf8f42370cde267507aee00d175761c6d3fd54d7916534ed1235733f.jpg
tWm5ZRDcO4XniG3GnopnJ1lvrKoCxU75Pea3H2Qd8MyGHr-w6Brv4ITnbqpP229XFvz_7m1zOcJnYR0ZXWXlZAJ0x-XUcWucTa3qmcAy5_heEK8NXABMIbmshRNiQjdM5qbbGNg0?expires=1717660800&signature=501183c10b2eb9c2d30f4a12dcab22e993ee197c0a831a90acc53edd64cdbe2b.jpg
When “Red” appears at the beginning of a sentence, the generated token does not contain a leading space. The token “7738” is different from the word in the previous two examples.

Observations:
The lower the token number, the more frequently that token appears in the corpus:

In all 3 sentences, the period has the same token number (“13”). This is because, in the corpus, the use of periods is very similar across contexts.

When ‘red’ appears in different positions in the sentence, the generated token is also different:

· Lowercase in the middle of a sentence: ‘ red’ · (token: “2266”)
· Uppercase in the middle of a sentence: ‘ Red’ · (token: “2297”)
· Uppercase at the beginning of a sentence: ‘Red’ · (token: “7738”)
  • WeChat Official Account

  • Scan to Visit Mobile Site

QQ|NIUBOYI ( 京ICP备17025393号 )|网站地图

GMT+8, 2026-10-31 07:17 , Processed in 0.050030 second(s), 41 queries .

POWERED BY NIUBOYIAI! 主理人:NIUBOYIAI  点亮哥

Copyright © 2001-2020, NIUBOYI AI.