Skip to content

Commit

Permalink
função para contagem de tokens
Browse files Browse the repository at this point in the history
  • Loading branch information
pmarkun committed Feb 19, 2024
1 parent 460a200 commit f58c5bc
Showing 1 changed file with 8 additions and 1 deletion.
9 changes: 8 additions & 1 deletion app/utils.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import os
from huggingface_hub import hf_hub_download
import tiktoken

MODEL_DIRECTORY = os.path.join(os.path.dirname(os.path.abspath(__file__)), "models")

Expand Down Expand Up @@ -53,4 +54,10 @@ def split_message_by_line(message, max_length=1600):
if current_message:
messages.append(current_message)

return messages
return messages

def count_tokens(string: str, model_name: str) -> int:
"""Returns the number of tokens in a text string using a specified encoding."""
encoding = tiktoken.encoding_for_model(model_name)
num_tokens = len(encoding.encode(string))
return num_tokens

0 comments on commit f58c5bc

Please sign in to comment.