File size: 462 Bytes
f8bf7df
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
import tiktoken


def count_num_tokens(text: str, model: str) -> int:
    """
    Returns the number of tokens in the given text.
    Args:
        text (str): The text to count tokens in.
        model (str, optional): The name of the GPT model to use. Defaults to the model specified in the app config.

    Returns:
        int: The number of tokens in the text.
    """
    encoding = tiktoken.encoding_for_model(model)
    return len(encoding.encode(text))