Skip to content

Instantly share code, notes, and snippets.

@amrakm
Last active April 19, 2023 19:18
Show Gist options
  • Save amrakm/f48a19049db606b8b4f062b708dc7ff8 to your computer and use it in GitHub Desktop.
Save amrakm/f48a19049db606b8b4f062b708dc7ff8 to your computer and use it in GitHub Desktop.
gpt token counter
import tiktoken
tokenizer = tiktoken.get_encoding('p50k_base')
# create the length function
def tiktoken_len(text):
tokens = tokenizer.encode(
text,
disallowed_special=()
)
return len(tokens)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment