Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
raphaelmansuy committed Jun 28, 2024
1 parent b1b83e5 commit db87d78
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 19 deletions.
21 changes: 21 additions & 0 deletions code2prompt/count_tokens_1.py
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
20 changes: 1 addition & 19 deletions code2prompt/main.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import click
from pathlib import Path
import tiktoken
from code2prompt.count_tokens_1 import count_tokens
from code2prompt.utils.is_binary import is_binary
from code2prompt.utils.generate_markdown_content import generate_markdown_content
from code2prompt.utils.is_filtered import is_filtered
Expand Down Expand Up @@ -144,24 +144,6 @@ def generate_content(files_data, options):
return process_template(template_content, files_data, user_inputs)
return generate_markdown_content(files_data, options['no_codeblock'])

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

if __name__ == "__main__":
# pylint: disable=no-value-for-parameter
create_markdown_file()

0 comments on commit db87d78

Please sign in to comment.