🤖 自动格式化代码 [skip ci]

This commit is contained in:
github-actions[bot]
2025-04-29 07:20:35 +00:00
parent ac40490bf1
commit 76922c3c28
4 changed files with 30 additions and 23 deletions

View File

@@ -234,7 +234,7 @@ class BotConfig:
forget_memory_interval: int = 600 # 记忆遗忘间隔(秒) forget_memory_interval: int = 600 # 记忆遗忘间隔(秒)
memory_forget_time: int = 24 # 记忆遗忘时间(小时) memory_forget_time: int = 24 # 记忆遗忘时间(小时)
memory_forget_percentage: float = 0.01 # 记忆遗忘比例 memory_forget_percentage: float = 0.01 # 记忆遗忘比例
consolidate_memory_interval: int = 1000 # 记忆整合间隔(秒) consolidate_memory_interval: int = 1000 # 记忆整合间隔(秒)
consolidation_similarity_threshold: float = 0.7 # 相似度阈值 consolidation_similarity_threshold: float = 0.7 # 相似度阈值
consolidate_memory_percentage: float = 0.01 # 检查节点比例 consolidate_memory_percentage: float = 0.01 # 检查节点比例

View File

@@ -146,7 +146,7 @@ class MainSystem:
print("\033[1;32m[记忆遗忘]\033[0m 开始遗忘记忆...") print("\033[1;32m[记忆遗忘]\033[0m 开始遗忘记忆...")
await HippocampusManager.get_instance().forget_memory(percentage=global_config.memory_forget_percentage) await HippocampusManager.get_instance().forget_memory(percentage=global_config.memory_forget_percentage)
print("\033[1;32m[记忆遗忘]\033[0m 记忆遗忘完成") print("\033[1;32m[记忆遗忘]\033[0m 记忆遗忘完成")
@staticmethod @staticmethod
async def consolidate_memory_task(): async def consolidate_memory_task():
"""记忆整合任务""" """记忆整合任务"""

View File

@@ -1353,11 +1353,11 @@ class ParahippocampalGyrus:
if not memory_items: if not memory_items:
try: try:
self.memory_graph.G.remove_node(node) self.memory_graph.G.remove_node(node)
node_changes["removed"].append(f"{node}(空节点)") # 标记为空节点移除 node_changes["removed"].append(f"{node}(空节点)") # 标记为空节点移除
logger.debug(f"[遗忘] 移除了空的节点: {node}") logger.debug(f"[遗忘] 移除了空的节点: {node}")
except nx.NetworkXError as e: except nx.NetworkXError as e:
logger.warning(f"[遗忘] 移除空节点 {node} 时发生错误(可能已被移除): {e}") logger.warning(f"[遗忘] 移除空节点 {node} 时发生错误(可能已被移除): {e}")
continue # 处理下一个节点 continue # 处理下一个节点
# --- 如果节点不为空,则执行原来的不活跃检查和随机移除逻辑 --- # --- 如果节点不为空,则执行原来的不活跃检查和随机移除逻辑 ---
last_modified = node_data.get("last_modified", current_time) last_modified = node_data.get("last_modified", current_time)
@@ -1373,15 +1373,15 @@ class ParahippocampalGyrus:
memory_items.remove(removed_item) memory_items.remove(removed_item)
# 条件3检查移除后 memory_items 是否变空 # 条件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]["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)})") node_changes["reduced"].append(f"{node} (数量: {current_count} -> {len(memory_items)})")
else: # 如果移除后列表为空 else: # 如果移除后列表为空
# 尝试移除节点,处理可能的错误 # 尝试移除节点,处理可能的错误
try: try:
self.memory_graph.G.remove_node(node) self.memory_graph.G.remove_node(node)
node_changes["removed"].append(f"{node}(遗忘清空)") # 标记为遗忘清空 node_changes["removed"].append(f"{node}(遗忘清空)") # 标记为遗忘清空
logger.debug(f"[遗忘] 节点 {node} 因移除最后一项而被清空。") logger.debug(f"[遗忘] 节点 {node} 因移除最后一项而被清空。")
except nx.NetworkXError as e: except nx.NetworkXError as e:
logger.warning(f"[遗忘] 尝试移除节点 {node} 时发生错误(可能已被移除):{e}") logger.warning(f"[遗忘] 尝试移除节点 {node} 时发生错误(可能已被移除):{e}")
@@ -1464,9 +1464,9 @@ class ParahippocampalGyrus:
node_data = self.memory_graph.G.nodes[node] node_data = self.memory_graph.G.nodes[node]
memory_items = node_data.get("memory_items", []) memory_items = node_data.get("memory_items", [])
if not isinstance(memory_items, list) or len(memory_items) < 2: 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): for item1, item2 in combinations(items_copy, 2):
@@ -1495,21 +1495,24 @@ class ParahippocampalGyrus:
# 从原始列表中移除信息量较低的项 # 从原始列表中移除信息量较低的项
try: try:
memory_items.remove(item_to_remove) 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 merged_count += 1
nodes_modified.add(node) nodes_modified.add(node)
node_data['last_modified'] = current_timestamp # 更新修改时间 node_data["last_modified"] = current_timestamp # 更新修改时间
_merged_in_this_node = True _merged_in_this_node = True
break # 每个节点每次检查只合并一对 break # 每个节点每次检查只合并一对
except ValueError: except ValueError:
# 如果项已经被移除(例如,在之前的迭代中作为 item_to_keep则跳过 # 如果项已经被移除(例如,在之前的迭代中作为 item_to_keep则跳过
logger.warning(f"[整合] 尝试移除节点 '{node}' 中不存在的项 '{item_to_remove[:30]}...',可能已被合并。") logger.warning(
f"[整合] 尝试移除节点 '{node}' 中不存在的项 '{item_to_remove[:30]}...',可能已被合并。"
)
continue continue
# # 如果节点内发生了合并,更新节点数据 (这种方式不安全,会丢失其他属性) # # 如果节点内发生了合并,更新节点数据 (这种方式不安全,会丢失其他属性)
# if merged_in_this_node: # if merged_in_this_node:
# self.memory_graph.G.nodes[node]["memory_items"] = memory_items # self.memory_graph.G.nodes[node]["memory_items"] = memory_items
if merged_count > 0: if merged_count > 0:
logger.info(f"[整合] 共合并了 {merged_count} 对相似记忆项,分布在 {len(nodes_modified)} 个节点中。") logger.info(f"[整合] 共合并了 {merged_count} 对相似记忆项,分布在 {len(nodes_modified)} 个节点中。")
sync_start = time.time() sync_start = time.time()
@@ -1594,7 +1597,7 @@ class HippocampusManager:
if not self._initialized: if not self._initialized:
raise RuntimeError("HippocampusManager 尚未初始化,请先调用 initialize 方法") raise RuntimeError("HippocampusManager 尚未初始化,请先调用 initialize 方法")
return await self._hippocampus.parahippocampal_gyrus.operation_forget_topic(percentage) return await self._hippocampus.parahippocampal_gyrus.operation_forget_topic(percentage)
async def consolidate_memory(self): async def consolidate_memory(self):
"""整合记忆的公共接口""" """整合记忆的公共接口"""
if not self._initialized: if not self._initialized:

View File

@@ -19,9 +19,9 @@ class MemoryConfig:
memory_ban_words: List[str] # 记忆过滤词列表 memory_ban_words: List[str] # 记忆过滤词列表
# 新增:记忆整合相关配置 # 新增:记忆整合相关配置
consolidation_similarity_threshold: float # 相似度阈值 consolidation_similarity_threshold: float # 相似度阈值
consolidate_memory_percentage: float # 检查节点比例 consolidate_memory_percentage: float # 检查节点比例
consolidate_memory_interval: int # 记忆整合间隔 consolidate_memory_interval: int # 记忆整合间隔
llm_topic_judge: str # 话题判断模型 llm_topic_judge: str # 话题判断模型
llm_summary_by_topic: str # 话题总结模型 llm_summary_by_topic: str # 话题总结模型
@@ -31,7 +31,9 @@ class MemoryConfig:
"""从全局配置创建记忆系统配置""" """从全局配置创建记忆系统配置"""
# 使用 getattr 提供默认值,防止全局配置缺少这些项 # 使用 getattr 提供默认值,防止全局配置缺少这些项
return cls( 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_num=getattr(global_config, "build_memory_sample_num", 5),
build_memory_sample_length=getattr(global_config, "build_memory_sample_length", 30), build_memory_sample_length=getattr(global_config, "build_memory_sample_length", 30),
memory_compress_rate=getattr(global_config, "memory_compress_rate", 0.1), 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), consolidation_similarity_threshold=getattr(global_config, "consolidation_similarity_threshold", 0.7),
consolidate_memory_percentage=getattr(global_config, "consolidate_memory_percentage", 0.01), consolidate_memory_percentage=getattr(global_config, "consolidate_memory_percentage", 0.01),
consolidate_memory_interval=getattr(global_config, "consolidate_memory_interval", 1000), consolidate_memory_interval=getattr(global_config, "consolidate_memory_interval", 1000),
llm_topic_judge=getattr(global_config, "llm_topic_judge", "default_judge_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"), # 添加默认模型名 llm_summary_by_topic=getattr(
global_config, "llm_summary_by_topic", "default_summary_model"
), # 添加默认模型名
) )