By Xavier Collantes
1import tiktoken
2
3def count_tokens(text, model="gpt-4"):
4 """Count the number of tokens in a text string."""
5 encoder = tiktoken.encoding_for_model(model)
6 tokens = encoder.encode(text)
7 return len(tokens)
8
9# Example usage.
10sample_text = "The dominant sequence transduction models are based on complex recurrent or convolutional neural networks in an encoder-decoder configuration."
11
12print(f"GPT-4: {count_tokens(sample_text, 'gpt-4')} tokens")
13print(f"GPT-3.5: {count_tokens(sample_text, 'gpt-3.5-turbo')} tokens")
14print(f"davinci: {count_tokens(sample_text, 'text-davinci-003')} tokens")
15
Related by topics: