Skip to content

Simple tokenizer

SimpleTokenizer

Bases: BaseTokenizer

Source code in griptape/griptape/tokenizers/simple_tokenizer.py
@define(frozen=True)
class SimpleTokenizer(BaseTokenizer):
    characters_per_token: int = field(kw_only=True)

    def count_tokens(self, text: str | list) -> int:
        if isinstance(text, str):
            num_tokens = (len(text) + self.characters_per_token - 1) // self.characters_per_token

            return num_tokens
        else:
            raise ValueError("Text must be a string.")

characters_per_token: int = field(kw_only=True) class-attribute instance-attribute

count_tokens(text)

Source code in griptape/griptape/tokenizers/simple_tokenizer.py
def count_tokens(self, text: str | list) -> int:
    if isinstance(text, str):
        num_tokens = (len(text) + self.characters_per_token - 1) // self.characters_per_token

        return num_tokens
    else:
        raise ValueError("Text must be a string.")