diff --git a/src/config/config.py b/src/config/config.py index ebfc444c1..28de20538 100644 --- a/src/config/config.py +++ b/src/config/config.py @@ -234,7 +234,7 @@ class BotConfig: forget_memory_interval: int = 600 # 记忆遗忘间隔(秒) memory_forget_time: int = 24 # 记忆遗忘时间(小时) memory_forget_percentage: float = 0.01 # 记忆遗忘比例 - + consolidate_memory_interval: int = 1000 # 记忆整合间隔(秒) consolidation_similarity_threshold: float = 0.7 # 相似度阈值 consolidate_memory_percentage: float = 0.01 # 检查节点比例 diff --git a/src/main.py b/src/main.py index 047e075f4..c0e743d66 100644 --- a/src/main.py +++ b/src/main.py @@ -146,7 +146,7 @@ class MainSystem: print("\033[1;32m[记忆遗忘]\033[0m 开始遗忘记忆...") await HippocampusManager.get_instance().forget_memory(percentage=global_config.memory_forget_percentage) print("\033[1;32m[记忆遗忘]\033[0m 记忆遗忘完成") - + @staticmethod async def consolidate_memory_task(): """记忆整合任务""" diff --git a/src/plugins/memory_system/Hippocampus.py b/src/plugins/memory_system/Hippocampus.py index 5cca0d074..e5aa096fa 100644 --- a/src/plugins/memory_system/Hippocampus.py +++ b/src/plugins/memory_system/Hippocampus.py @@ -1353,11 +1353,11 @@ class ParahippocampalGyrus: if not memory_items: try: self.memory_graph.G.remove_node(node) - node_changes["removed"].append(f"{node}(空节点)") # 标记为空节点移除 + node_changes["removed"].append(f"{node}(空节点)") # 标记为空节点移除 logger.debug(f"[遗忘] 移除了空的节点: {node}") except nx.NetworkXError as e: logger.warning(f"[遗忘] 移除空节点 {node} 时发生错误(可能已被移除): {e}") - continue # 处理下一个节点 + continue # 处理下一个节点 # --- 如果节点不为空,则执行原来的不活跃检查和随机移除逻辑 --- last_modified = node_data.get("last_modified", current_time) @@ -1373,15 +1373,15 @@ class ParahippocampalGyrus: memory_items.remove(removed_item) # 条件3:检查移除后 memory_items 是否变空 - if memory_items: # 如果移除后列表不为空 + if memory_items: # 如果移除后列表不为空 # self.memory_graph.G.nodes[node]["memory_items"] = memory_items # 直接修改列表即可 - self.memory_graph.G.nodes[node]["last_modified"] = current_time # 更新修改时间 + self.memory_graph.G.nodes[node]["last_modified"] = current_time # 更新修改时间 node_changes["reduced"].append(f"{node} (数量: {current_count} -> {len(memory_items)})") - else: # 如果移除后列表为空 + else: # 如果移除后列表为空 # 尝试移除节点,处理可能的错误 try: self.memory_graph.G.remove_node(node) - node_changes["removed"].append(f"{node}(遗忘清空)") # 标记为遗忘清空 + node_changes["removed"].append(f"{node}(遗忘清空)") # 标记为遗忘清空 logger.debug(f"[遗忘] 节点 {node} 因移除最后一项而被清空。") except nx.NetworkXError as e: logger.warning(f"[遗忘] 尝试移除节点 {node} 时发生错误(可能已被移除):{e}") @@ -1464,9 +1464,9 @@ class ParahippocampalGyrus: node_data = self.memory_graph.G.nodes[node] memory_items = node_data.get("memory_items", []) if not isinstance(memory_items, list) or len(memory_items) < 2: - continue # 双重检查,理论上不会进入 + continue # 双重检查,理论上不会进入 - items_copy = list(memory_items) # 创建副本以安全迭代和修改 + items_copy = list(memory_items) # 创建副本以安全迭代和修改 # 遍历所有记忆项组合 for item1, item2 in combinations(items_copy, 2): @@ -1495,21 +1495,24 @@ class ParahippocampalGyrus: # 从原始列表中移除信息量较低的项 try: memory_items.remove(item_to_remove) - logger.info(f"[整合] 已合并节点 '{node}' 中的记忆,保留: '{item_to_keep[:60]}...', 移除: '{item_to_remove[:60]}...'" ) + logger.info( + f"[整合] 已合并节点 '{node}' 中的记忆,保留: '{item_to_keep[:60]}...', 移除: '{item_to_remove[:60]}...'" + ) merged_count += 1 nodes_modified.add(node) - node_data['last_modified'] = current_timestamp # 更新修改时间 + node_data["last_modified"] = current_timestamp # 更新修改时间 _merged_in_this_node = True - break # 每个节点每次检查只合并一对 + break # 每个节点每次检查只合并一对 except ValueError: # 如果项已经被移除(例如,在之前的迭代中作为 item_to_keep),则跳过 - logger.warning(f"[整合] 尝试移除节点 '{node}' 中不存在的项 '{item_to_remove[:30]}...',可能已被合并。") + logger.warning( + f"[整合] 尝试移除节点 '{node}' 中不存在的项 '{item_to_remove[:30]}...',可能已被合并。" + ) continue - # # 如果节点内发生了合并,更新节点数据 (这种方式不安全,会丢失其他属性) + # # 如果节点内发生了合并,更新节点数据 (这种方式不安全,会丢失其他属性) # if merged_in_this_node: # self.memory_graph.G.nodes[node]["memory_items"] = memory_items - if merged_count > 0: logger.info(f"[整合] 共合并了 {merged_count} 对相似记忆项,分布在 {len(nodes_modified)} 个节点中。") sync_start = time.time() @@ -1594,7 +1597,7 @@ class HippocampusManager: if not self._initialized: raise RuntimeError("HippocampusManager 尚未初始化,请先调用 initialize 方法") return await self._hippocampus.parahippocampal_gyrus.operation_forget_topic(percentage) - + async def consolidate_memory(self): """整合记忆的公共接口""" if not self._initialized: diff --git a/src/plugins/memory_system/memory_config.py b/src/plugins/memory_system/memory_config.py index 8f7e1ffe7..b2fb72801 100644 --- a/src/plugins/memory_system/memory_config.py +++ b/src/plugins/memory_system/memory_config.py @@ -19,9 +19,9 @@ class MemoryConfig: memory_ban_words: List[str] # 记忆过滤词列表 # 新增:记忆整合相关配置 - consolidation_similarity_threshold: float # 相似度阈值 - consolidate_memory_percentage: float # 检查节点比例 - consolidate_memory_interval: int # 记忆整合间隔 + consolidation_similarity_threshold: float # 相似度阈值 + consolidate_memory_percentage: float # 检查节点比例 + consolidate_memory_interval: int # 记忆整合间隔 llm_topic_judge: str # 话题判断模型 llm_summary_by_topic: str # 话题总结模型 @@ -31,7 +31,9 @@ class MemoryConfig: """从全局配置创建记忆系统配置""" # 使用 getattr 提供默认值,防止全局配置缺少这些项 return cls( - memory_build_distribution=getattr(global_config, "memory_build_distribution", (24, 12, 0.5, 168, 72, 0.5)), # 添加默认值 + memory_build_distribution=getattr( + global_config, "memory_build_distribution", (24, 12, 0.5, 168, 72, 0.5) + ), # 添加默认值 build_memory_sample_num=getattr(global_config, "build_memory_sample_num", 5), build_memory_sample_length=getattr(global_config, "build_memory_sample_length", 30), memory_compress_rate=getattr(global_config, "memory_compress_rate", 0.1), @@ -41,6 +43,8 @@ class MemoryConfig: consolidation_similarity_threshold=getattr(global_config, "consolidation_similarity_threshold", 0.7), consolidate_memory_percentage=getattr(global_config, "consolidate_memory_percentage", 0.01), consolidate_memory_interval=getattr(global_config, "consolidate_memory_interval", 1000), - llm_topic_judge=getattr(global_config, "llm_topic_judge", "default_judge_model"), # 添加默认模型名 - llm_summary_by_topic=getattr(global_config, "llm_summary_by_topic", "default_summary_model"), # 添加默认模型名 + llm_topic_judge=getattr(global_config, "llm_topic_judge", "default_judge_model"), # 添加默认模型名 + llm_summary_by_topic=getattr( + global_config, "llm_summary_by_topic", "default_summary_model" + ), # 添加默认模型名 )