You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
21 lines
484 B
21 lines
484 B
import subprocess
|
|
|
|
|
|
def count_tokens(text: str, model: str = "gpt-4o") -> int:
|
|
"""Count the number of tokens in the given text."""
|
|
try:
|
|
from litellm import encode
|
|
except ImportError:
|
|
import sys
|
|
|
|
subprocess.run(
|
|
[sys.executable, "-m", "pip", "install", "litellm"]
|
|
)
|
|
from litellm import encode
|
|
|
|
return len(encode(model=model, text=text))
|
|
|
|
|
|
# if __name__ == "__main__":
|
|
# print(count_tokens("Hello, how are you?"))
|