openai-cookbook icon indicating copy to clipboard operation
openai-cookbook copied to clipboard

[SUPPORT]How to count tokens when I use gpt-4o

Open kusuri-h opened this issue 1 year ago • 2 comments
trafficstars

When I use a Chinese prompt to call GPT-4, the actual number of tokens used by the model is less than that of GPT-4 Turbo, which is expected. But how can I calculate the actual number of tokens consumed when calling GPT-4?

kusuri-h avatar May 23 '24 03:05 kusuri-h

Also want to know. Thanks!

no-coder-pl avatar May 23 '24 14:05 no-coder-pl

ChatGPT's answer:

import tiktoken

# Select the encoding corresponding to the model
encoding = tiktoken.encoding_for_model("gpt-4")

# Your Chinese prompt
prompt = "你好,世界!"

# Tokenize the prompt
tokens = encoding.encode(prompt)

# Count the number of tokens
num_tokens = len(tokens)

print(f"Number of tokens: {num_tokens}")

chaityacshah avatar Jun 01 '24 07:06 chaityacshah

This issue is stale because it has been open 60 days with no activity. Remove stale label or comment or this will be closed in 10 days.

github-actions[bot] avatar Aug 01 '24 01:08 github-actions[bot]

This issue was closed because it has been stalled for 10 days with no activity.

github-actions[bot] avatar Aug 11 '24 01:08 github-actions[bot]