INNER CODE UNIT · Python

num_tokens_from_messages

adamyodinsky/TerminalGPT · terminalgpt/chat.py:183

    def num_tokens_from_messages(self) -> int:
        """Returns the number of tokens used by a list of messages."""
        encoding = tiktoken.get_encoding(config.ENCODING_MODEL)
        num_tokens = 0
        for message in self.__messages:
            num_tokens += (
                4  # every message follows <im_start>{role/name}\n{content}<im_end>\n
            )
            for key, value in message.items():
                num_tokens += len(encoding.encode(value))
                if key == "name":  # if there's a name, the role is omitted
                    num_tokens -= 1  # role is always required and always 1 token
        num_tokens -= 2  # every reply is primed with <im_start>assistant
        return num_tokens

    def welcome_message(self, messages: list):
        """Prints the welcome message."""
        print()

View source record →

📰 Research Paper
Loading…
⏳ Fetching content…