2023.04.19
调用ChatGPT超过4096Token后自动截取保留指定长度的Token
Python

调用ChatGPT超过4096Token后自动截取保留指定长度的Token

年份
2023
分类
Python
阅读

需要用到官方计算token的包:tiktoken

pip install tiktoken

截取并返回


def num_tokens_from_string(string: str) -> int:
    # www.lanol.cn
    encoding = tiktoken.get_encoding('cl100k_base')
    num_tokens = len(encoding.encode(string))
    return num_tokens


def truncate_messages(messages, max_chars):
    # By Lan www.lanol.cn
    total_chars = sum(num_tokens_from_string(message['content']) for message in messages)
    while total_chars > max_chars:
        removed_message = messages.pop(0)
        total_chars -= num_tokens_from_string(removed_message['content'])
    return messages

www.lanol.cn