需要用到官方计算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
 
        
       
     
      
123
非常实用,感谢博主分享!