forked from raphaelmansuy/code2prompt
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
b1b83e5
commit db87d78
Showing
2 changed files
with
22 additions
and
19 deletions.
There are no files selected for viewing
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
import click | ||
import tiktoken | ||
|
||
|
||
def count_tokens(text: str, encoding: str) -> int: | ||
""" | ||
Count the number of tokens in the given text using the specified encoding. | ||
Args: | ||
text (str): The text to tokenize and count. | ||
encoding (str): The encoding to use for tokenization. | ||
Returns: | ||
int: The number of tokens in the text. | ||
""" | ||
try: | ||
encoder = tiktoken.get_encoding(encoding) | ||
return len(encoder.encode(text)) | ||
except Exception as e: | ||
click.echo(f"Error counting tokens: {str(e)}", err=True) | ||
return 0 |
This file contains 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