gpt4 book ai didi

openai-api - OpenAI ChatGPT (GPT-3.5) API : Why does the API report more prompt_tokens used for the messages parameter than I thought it would?

转载 作者:行者123 更新时间:2023-12-02 05:49:22 24 4
gpt4 key购买 nike

我使用 gpt-3.5-turbo 模型并向 OpenAI API 发送以下消息:哪个国家最美丽?

我将其作为 JSON 对象发送:{"role":"user","content":"哪个是最美丽的国家?"}

我以为它会返回 prompt_tokens7 个 token ,但事实并非如此。它返回 prompt_tokens15 个 token 。即使仅发送 .,它也会返回 prompt_tokens9 token 。

正确吗?

最佳答案

如果您发送{"role": "user", "content": "What is the most beautiful country?"}作为messages参数,不仅仅是What is the most beautiful country?发送到 OpenAI API 端点,但看起来像是整个 "role": "user", "content": "What is the most beautiful country?" .

我能够使用 tiktoken 确认这一点.

如果你运行get_tokens_long_example.py您将得到以下输出:

14

get_tokens_long_example.py

import tiktoken

def num_tokens_from_string(string: str, encoding_name: str) -> int:
"""Returns the number of tokens in a text string."""
encoding = tiktoken.get_encoding(encoding_name)
num_tokens = len(encoding.encode(string))
return num_tokens

print(num_tokens_from_string("'role':'user','content':'What is the most beautiful country?'", "cl100k_base"))

如果你运行get_tokens_short_example.py您将得到以下输出:

8

get_tokens_short_example.py

import tiktoken

def num_tokens_from_string(string: str, encoding_name: str) -> int:
"""Returns the number of tokens in a text string."""
encoding = tiktoken.get_encoding(encoding_name)
num_tokens = len(encoding.encode(string))
return num_tokens

print(num_tokens_from_string("'role':'user','content':'.'", "cl100k_base"))

您说 OpenAI API 报告 15第一个示例中使用的 token 和 9第二个示例中使用的标记。您可能注意到我得到了 148使用 tiktoken 的代币(即两个示例中都少了 1 个代币)。这似乎是一个known tiktoken problem那应该是solved .

无论如何,我并没有深入研究为什么我仍然少了 1 个代币,但我能够证明这不仅仅是 What is the most beautiful country?发送到 OpenAI API 端点。

更多关于tiktoken的信息,请参见 this answer .

关于openai-api - OpenAI ChatGPT (GPT-3.5) API : Why does the API report more prompt_tokens used for the messages parameter than I thought it would?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/76347639/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com