Skip to content

Instantly share code, notes, and snippets.

@EnkrateiaLucca
Last active September 13, 2023 08:59
Show Gist options
  • Save EnkrateiaLucca/e1d501424d1a1e350762ec5eedc275ca to your computer and use it in GitHub Desktop.
Save EnkrateiaLucca/e1d501424d1a1e350762ec5eedc275ca to your computer and use it in GitHub Desktop.
Calculates number of tokens
import tiktoken
def get_num_tokens(prompt, model="gpt-3.5-turbo"):
"""Calculates the number of tokens in a text prompt"""
enc = tiktoken.encoding_for_model("gpt-3.5-turbo")
return len(enc.encode(prompt))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment