Created
September 29, 2023 06:25
-
-
Save MilesCranmer/cd8f64b20b4bd9ed6eb0eba93bd8a204 to your computer and use it in GitHub Desktop.
Easily count the number of tokens in text from the command line
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function num_tokens { | |
prun python -c 'import sys; import tiktoken; s = "\n".join([line for line in sys.stdin]); encoding = tiktoken.get_encoding("cl100k_base"); print(len(encoding.encode(s)))' | |
} | |
# e.g., `echo "Hello World!" | num_tokens` | |
# this should give 3 tokens. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment