
获得徽章 0
赞了这篇沸点
赞了这篇沸点
#豆包MarsCode AI练中学# 补充一下10_记忆/05_ConversationSummaryBufferMemory 由于统计token的方法失败所以得自己重载一下汇总token的方法 ```get_num_tokens_from_messages```。粗略的实现,本质是避免这个脚本报错。
```
class CustomChatOpenAI(ChatOpenAI):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
def get_num_tokens_from_messages(self, messages):
"""Returns the number of tokens used by a list of messages."""
print(">>> messages: ", messages)
num_tokens = 0
for message in messages:
num_tokens += 4 # every message follows <im_start>{role/name}\n{content}<im_end>\n
num_tokens += len(message.content)
num_tokens += 2 # every reply is primed with <im_start>assistant
return num_tokens
```
```
class CustomChatOpenAI(ChatOpenAI):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
def get_num_tokens_from_messages(self, messages):
"""Returns the number of tokens used by a list of messages."""
print(">>> messages: ", messages)
num_tokens = 0
for message in messages:
num_tokens += 4 # every message follows <im_start>{role/name}\n{content}<im_end>\n
num_tokens += len(message.content)
num_tokens += 2 # every reply is primed with <im_start>assistant
return num_tokens
```
展开
评论
点赞