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

Lan
Lan
2023-04-19 / 2 评论 / 365 阅读 / 正在检测是否收录...
温馨提示:
本文最后更新于2023年04月19日,已超过385天没有更新,若内容或图片失效,请留言反馈。

需要用到官方计算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

1

评论 (2)

取消
  1. 头像
    12342141212
    Windows 10 · Google Chrome

    123

    回复
  2. 头像
    Bruce
    Windows 10 · Google Chrome

    非常实用,感谢博主分享!表情

    回复