openai-cookbook
openai-cookbook copied to clipboard
> 是的,会的。用作`cl100k_base`编码。
trafficstars
> 是的,会的。用作`cl100k_base`编码。
如果您用于
tiktoken计算 ChatGPT API 调用的令牌,现在您可以将每条消息的内容和名称字段的长度加 4。
Is there an example? What I am using here is wrong
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": "system", "content": "You are a helpful assistant."},
{"role": "user", "content": "Who won the world series in 2020?"},
{"role": "assistant", "content": "The Los Angeles Dodgers won the World Series in 2020."},
{"role": "user", "content": "Where was it played?"}""", "cl100k_base"))
Originally posted by @xujimu in https://github.com/openai/openai-cookbook/issues/165#issuecomment-1451311083