File size: 953 Bytes
6f8def7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
import tiktoken
from aworld.logs.util import logger

# TODO: merge to `models` package

MODEL_TO_ENCODING = {
    "gpt-3.5-turbo": "cl100k_base",
    "gpt-4": "cl100k_base",
    "text-davinci-003": "p50k_base",
    "text-embedding-ada-002": "cl100k_base",
    "text-curie-001": "r50k_base",
    "text-babbage-001": "r50k_base",
    "text-ada-001": "r50k_base",
}

def get_encoding_for_model(model_name: str) -> tiktoken.Encoding:
    """
    Automatically select the corresponding encoder based on the model name.
    """
    encoding_name = MODEL_TO_ENCODING.get(model_name)
    if encoding_name is None:
        logger.warning(f"model '{model_name}' not found in mapping table.")
        return "cl100k_base"
    return encoding_name

def count_tokens(model_name: str, content: str):
    encoding = tiktoken.get_encoding(get_encoding_for_model(model_name))

    tokens = encoding.encode(content)

    token_count = len(tokens)

    return token_count