v0.5.4.0 记忆系统更新
移除jieba
This commit is contained in:
@@ -13,6 +13,7 @@ from .willing_manager import willing_manager
|
||||
from nonebot.rule import to_me
|
||||
from .bot import chat_bot
|
||||
from .emoji_manager import emoji_manager
|
||||
import time
|
||||
|
||||
|
||||
# 获取驱动器
|
||||
@@ -86,31 +87,27 @@ async def _(bot: Bot):
|
||||
async def _(bot: Bot, event: GroupMessageEvent, state: T_State):
|
||||
await chat_bot.handle_message(event, bot)
|
||||
|
||||
'''
|
||||
@scheduler.scheduled_job("interval", seconds=300000, id="monitor_relationships")
|
||||
async def monitor_relationships():
|
||||
"""每15秒打印一次关系数据"""
|
||||
relationship_manager.print_all_relationships()
|
||||
'''
|
||||
|
||||
# 添加build_memory定时任务
|
||||
@scheduler.scheduled_job("interval", seconds=global_config.build_memory_interval, id="build_memory")
|
||||
async def build_memory_task():
|
||||
"""每30秒执行一次记忆构建"""
|
||||
print("\033[1;32m[记忆构建]\033[0m 开始构建记忆...")
|
||||
await hippocampus.operation_build_memory(chat_size=30)
|
||||
print("\033[1;32m[记忆构建]\033[0m 记忆构建完成")
|
||||
print("\033[1;32m[记忆构建]\033[0m -------------------------------------------开始构建记忆-------------------------------------------")
|
||||
start_time = time.time()
|
||||
await hippocampus.operation_build_memory(chat_size=20)
|
||||
end_time = time.time()
|
||||
print(f"\033[1;32m[记忆构建]\033[0m -------------------------------------------记忆构建完成:耗时: {end_time - start_time:.2f} 秒-------------------------------------------")
|
||||
|
||||
@scheduler.scheduled_job("interval", seconds=global_config.forget_memory_interval, id="forget_memory")
|
||||
async def forget_memory_task():
|
||||
"""每30秒执行一次记忆构建"""
|
||||
print("\033[1;32m[记忆遗忘]\033[0m 开始遗忘记忆...")
|
||||
await hippocampus.operation_forget_topic(percentage=0.1)
|
||||
print("\033[1;32m[记忆遗忘]\033[0m 记忆遗忘完成")
|
||||
# print("\033[1;32m[记忆遗忘]\033[0m 开始遗忘记忆...")
|
||||
# await hippocampus.operation_forget_topic(percentage=0.1)
|
||||
# print("\033[1;32m[记忆遗忘]\033[0m 记忆遗忘完成")
|
||||
|
||||
@scheduler.scheduled_job("interval", seconds=global_config.build_memory_interval + 10, id="build_memory")
|
||||
async def build_memory_task():
|
||||
@scheduler.scheduled_job("interval", seconds=global_config.build_memory_interval + 10, id="merge_memory")
|
||||
async def merge_memory_task():
|
||||
"""每30秒执行一次记忆构建"""
|
||||
print("\033[1;32m[记忆整合]\033[0m 开始整合")
|
||||
await hippocampus.operation_merge_memory(percentage=0.1)
|
||||
print("\033[1;32m[记忆整合]\033[0m 记忆整合完成")
|
||||
# print("\033[1;32m[记忆整合]\033[0m 开始整合")
|
||||
# await hippocampus.operation_merge_memory(percentage=0.1)
|
||||
# print("\033[1;32m[记忆整合]\033[0m 记忆整合完成")
|
||||
|
||||
|
||||
@@ -69,11 +69,9 @@ class ChatBot:
|
||||
current_time = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(message.time))
|
||||
|
||||
|
||||
identifier=topic_identifier.identify_topic()
|
||||
if global_config.topic_extract=='llm':
|
||||
topic=await identifier(message.processed_plain_text)
|
||||
else:
|
||||
topic=identifier(message.detailed_plain_text)
|
||||
|
||||
topic=await topic_identifier.identify_topic_llm(message.processed_plain_text)
|
||||
|
||||
|
||||
# topic1 = topic_identifier.identify_topic_jieba(message.processed_plain_text)
|
||||
# topic2 = await topic_identifier.identify_topic_llm(message.processed_plain_text)
|
||||
|
||||
@@ -26,7 +26,7 @@ class BotConfig:
|
||||
talk_frequency_down_groups = set()
|
||||
ban_user_id = set()
|
||||
|
||||
build_memory_interval: int = 60 # 记忆构建间隔(秒)
|
||||
build_memory_interval: int = 30 # 记忆构建间隔(秒)
|
||||
forget_memory_interval: int = 300 # 记忆遗忘间隔(秒)
|
||||
EMOJI_CHECK_INTERVAL: int = 120 # 表情包检查间隔(分钟)
|
||||
EMOJI_REGISTER_INTERVAL: int = 10 # 表情包注册间隔(分钟)
|
||||
|
||||
@@ -95,7 +95,11 @@ class ResponseGenerator:
|
||||
# return None
|
||||
|
||||
# 生成回复
|
||||
content, reasoning_content = await model.generate_response(prompt)
|
||||
try:
|
||||
content, reasoning_content = await model.generate_response(prompt)
|
||||
except Exception as e:
|
||||
print(f"生成回复时出错: {e}")
|
||||
return None
|
||||
|
||||
# 保存到数据库
|
||||
self._save_to_db(
|
||||
|
||||
@@ -36,7 +36,9 @@ class PromptBuilder:
|
||||
|
||||
memory_prompt = ''
|
||||
start_time = time.time() # 记录开始时间
|
||||
topic = topic_identifier.identify_topic_jieba(message_txt)
|
||||
# topic = await topic_identifier.identify_topic_llm(message_txt)
|
||||
topic = topic_identifier.identify_topic_snownlp(message_txt)
|
||||
|
||||
# print(f"\033[1;32m[pb主题识别]\033[0m 主题: {topic}")
|
||||
|
||||
all_first_layer_items = [] # 存储所有第一层记忆
|
||||
@@ -64,15 +66,7 @@ class PromptBuilder:
|
||||
if overlap:
|
||||
# print(f"\033[1;32m[前额叶]\033[0m 发现主题 '{current_topic}' 和 '{other_topic}' 有共同的第二层记忆: {overlap}")
|
||||
overlapping_second_layer.update(overlap)
|
||||
|
||||
# 合并所有需要的记忆
|
||||
# if all_first_layer_items:
|
||||
# print(f"\033[1;32m[前额叶]\033[0m 合并所有需要的记忆1: {all_first_layer_items}")
|
||||
# if overlapping_second_layer:
|
||||
# print(f"\033[1;32m[前额叶]\033[0m 合并所有需要的记忆2: {list(overlapping_second_layer)}")
|
||||
|
||||
# 使用集合去重
|
||||
# 从每个来源随机选择2条记忆(如果有的话)
|
||||
|
||||
selected_first_layer = random.sample(all_first_layer_items, min(2, len(all_first_layer_items))) if all_first_layer_items else []
|
||||
selected_second_layer = random.sample(list(overlapping_second_layer), min(2, len(overlapping_second_layer))) if overlapping_second_layer else []
|
||||
|
||||
|
||||
@@ -15,16 +15,6 @@ class TopicIdentifier:
|
||||
self.llm_client = LLM_request(model=global_config.llm_topic_extract)
|
||||
self.select=global_config.topic_extract
|
||||
|
||||
def identify_topic(self):
|
||||
if self.select=='jieba':
|
||||
return self.identify_topic_jieba
|
||||
elif self.select=='snownlp':
|
||||
return self.identify_topic_snownlp
|
||||
elif self.select=='llm':
|
||||
return self.identify_topic_llm
|
||||
else:
|
||||
return self.identify_topic_snownlp
|
||||
|
||||
|
||||
async def identify_topic_llm(self, text: str) -> Optional[List[str]]:
|
||||
"""识别消息主题,返回主题列表"""
|
||||
@@ -48,56 +38,10 @@ class TopicIdentifier:
|
||||
|
||||
# 解析主题字符串为列表
|
||||
topic_list = [t.strip() for t in topic.split(",") if t.strip()]
|
||||
|
||||
print(f"\033[1;32m[主题识别]\033[0m 主题: {topic_list}")
|
||||
return topic_list if topic_list else None
|
||||
|
||||
def identify_topic_jieba(self, text: str) -> Optional[str]:
|
||||
"""使用jieba识别主题"""
|
||||
words = jieba.lcut(text)
|
||||
# 去除停用词和标点符号
|
||||
stop_words = {
|
||||
'的', '了', '和', '是', '就', '都', '而', '及', '与', '这', '那', '但', '然', '却',
|
||||
'因为', '所以', '如果', '虽然', '一个', '我', '你', '他', '她', '它', '我们', '你们',
|
||||
'他们', '在', '有', '个', '把', '被', '让', '给', '从', '向', '到', '又', '也', '很',
|
||||
'啊', '吧', '呢', '吗', '呀', '哦', '哈', '么', '嘛', '啦', '哎', '唉', '哇', '嗯',
|
||||
'哼', '哪', '什么', '怎么', '为什么', '怎样', '如何', '什么样', '这样', '那样', '这么',
|
||||
'那么', '多少', '几', '谁', '哪里', '哪儿', '什么时候', '何时', '为何', '怎么办',
|
||||
'怎么样', '这些', '那些', '一些', '一点', '一下', '一直', '一定', '一般', '一样',
|
||||
'一会儿', '一边', '一起',
|
||||
# 添加更多量词
|
||||
'个', '只', '条', '张', '片', '块', '本', '册', '页', '幅', '面', '篇', '份',
|
||||
'朵', '颗', '粒', '座', '幢', '栋', '间', '层', '家', '户', '位', '名', '群',
|
||||
'双', '对', '打', '副', '套', '批', '组', '串', '包', '箱', '袋', '瓶', '罐',
|
||||
# 添加更多介词
|
||||
'按', '按照', '把', '被', '比', '比如', '除', '除了', '当', '对', '对于',
|
||||
'根据', '关于', '跟', '和', '将', '经', '经过', '靠', '连', '论', '通过',
|
||||
'同', '往', '为', '为了', '围绕', '于', '由', '由于', '与', '在', '沿', '沿着',
|
||||
'依', '依照', '以', '因', '因为', '用', '由', '与', '自', '自从'
|
||||
}
|
||||
|
||||
# 过滤掉停用词和标点符号,只保留名词和动词
|
||||
filtered_words = []
|
||||
for word in words:
|
||||
if word not in stop_words and not word.strip() in {
|
||||
'。', ',', '、', ':', ';', '!', '?', '"', '"', ''', ''',
|
||||
'(', ')', '【', '】', '《', '》', '…', '—', '·', '、', '~',
|
||||
'~', '+', '=', '-', '/', '\\', '|', '*', '#', '@', '$', '%',
|
||||
'^', '&', '[', ']', '{', '}', '<', '>', '`', '_', '.', ',',
|
||||
';', ':', '\'', '"', '(', ')', '?', '!', '±', '×', '÷', '≠',
|
||||
'≈', '∈', '∉', '⊆', '⊇', '⊂', '⊃', '∪', '∩', '∧', '∨'
|
||||
}:
|
||||
filtered_words.append(word)
|
||||
|
||||
# 统计词频
|
||||
word_freq = {}
|
||||
for word in filtered_words:
|
||||
word_freq[word] = word_freq.get(word, 0) + 1
|
||||
|
||||
# 按词频排序,取前3个
|
||||
sorted_words = sorted(word_freq.items(), key=lambda x: x[1], reverse=True)
|
||||
top_words = [word for word, freq in sorted_words[:3]]
|
||||
|
||||
return top_words if top_words else None
|
||||
|
||||
def identify_topic_snownlp(self, text: str) -> Optional[List[str]]:
|
||||
"""使用 SnowNLP 进行主题识别
|
||||
|
||||
@@ -113,7 +57,7 @@ class TopicIdentifier:
|
||||
try:
|
||||
s = SnowNLP(text)
|
||||
# 提取前3个关键词作为主题
|
||||
keywords = s.keywords(3)
|
||||
keywords = s.keywords(5)
|
||||
return keywords if keywords else None
|
||||
except Exception as e:
|
||||
print(f"\033[1;31m[错误]\033[0m SnowNLP 处理失败: {str(e)}")
|
||||
|
||||
@@ -75,13 +75,11 @@ def cosine_similarity(v1, v2):
|
||||
norm2 = np.linalg.norm(v2)
|
||||
return dot_product / (norm1 * norm2)
|
||||
|
||||
def calculate_information_content(text):
|
||||
def calculate_information_content(text):
|
||||
"""计算文本的信息量(熵)"""
|
||||
# 统计字符频率
|
||||
char_count = Counter(text)
|
||||
total_chars = len(text)
|
||||
|
||||
# 计算熵
|
||||
entropy = 0
|
||||
for count in char_count.values():
|
||||
probability = count / total_chars
|
||||
@@ -90,27 +88,37 @@ def calculate_information_content(text):
|
||||
return entropy
|
||||
|
||||
def get_cloest_chat_from_db(db, length: int, timestamp: str):
|
||||
# 从数据库中根据时间戳获取离其最近的聊天记录
|
||||
"""从数据库中获取最接近指定时间戳的聊天记录,并记录读取次数"""
|
||||
chat_text = ''
|
||||
closest_record = db.db.messages.find_one({"time": {"$lte": timestamp}}, sort=[('time', -1)]) # 调试输出
|
||||
# print(f"距离time最近的消息时间: {time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(int(closest_record['time'])))}")
|
||||
closest_record = db.db.messages.find_one({"time": {"$lte": timestamp}}, sort=[('time', -1)])
|
||||
|
||||
if closest_record:
|
||||
if closest_record and closest_record.get('memorized', 0) < 4:
|
||||
closest_time = closest_record['time']
|
||||
group_id = closest_record['group_id'] # 获取groupid
|
||||
# 获取该时间戳之后的length条消息,且groupid相同
|
||||
chat_record = list(db.db.messages.find({"time": {"$gt": closest_time}, "group_id": group_id}).sort('time', 1).limit(length))
|
||||
for record in chat_record:
|
||||
time_str = time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(int(record['time'])))
|
||||
try:
|
||||
displayname="[(%s)%s]%s" % (record["user_id"],record["user_nickname"],record["user_cardname"])
|
||||
except:
|
||||
displayname=record["user_nickname"] or "用户" + str(record["user_id"])
|
||||
chat_text += f'[{time_str}] {displayname}: {record["processed_plain_text"]}\n' # 添加发送者和时间信息
|
||||
chat_records = list(db.db.messages.find(
|
||||
{"time": {"$gt": closest_time}, "group_id": group_id}
|
||||
).sort('time', 1).limit(length))
|
||||
|
||||
# 更新每条消息的memorized属性
|
||||
for record in chat_records:
|
||||
# 检查当前记录的memorized值
|
||||
current_memorized = record.get('memorized', 0)
|
||||
if current_memorized > 3:
|
||||
# print(f"消息已读取3次,跳过")
|
||||
return ''
|
||||
|
||||
# 更新memorized值
|
||||
db.db.messages.update_one(
|
||||
{"_id": record["_id"]},
|
||||
{"$set": {"memorized": current_memorized + 1}}
|
||||
)
|
||||
|
||||
chat_text += record["detailed_plain_text"]
|
||||
|
||||
return chat_text
|
||||
|
||||
return [] # 如果没有找到记录,返回空列表
|
||||
|
||||
print(f"消息已读取3次,跳过")
|
||||
return ''
|
||||
|
||||
def get_recent_group_messages(db, group_id: int, limit: int = 12) -> list:
|
||||
"""从数据库获取群组最近的消息记录
|
||||
|
||||
@@ -52,8 +52,8 @@ class WillingManager:
|
||||
reply_probability = reply_probability / 3.5
|
||||
|
||||
reply_probability = min(reply_probability, 1)
|
||||
if reply_probability < 0.1:
|
||||
reply_probability = 0.1
|
||||
if reply_probability < 0:
|
||||
reply_probability = 0
|
||||
return reply_probability
|
||||
|
||||
def change_reply_willing_sent(self, group_id: int):
|
||||
|
||||
Reference in New Issue
Block a user