From 6e8c53ef7ccfee3f539c9a8a76929af6017771d7 Mon Sep 17 00:00:00 2001 From: Windpicker-owo <3431391539@qq.com> Date: Tue, 7 Oct 2025 11:53:51 +0800 Subject: [PATCH] =?UTF-8?q?feat(memory):=20=E5=A2=9E=E5=BC=BA=E8=AE=B0?= =?UTF-8?q?=E5=BF=86=E6=A3=80=E7=B4=A2=E6=97=A5=E5=BF=97=EF=BC=8C=E6=89=93?= =?UTF-8?q?=E5=8D=B0=E6=9C=89=E6=95=88=E8=AE=B0=E5=BF=86=E7=9A=84=E8=AF=A6?= =?UTF-8?q?=E7=BB=86=E4=BF=A1=E6=81=AF=E5=92=8C=E5=85=83=E6=95=B0=E6=8D=AE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/chat/memory_system/memory_manager.py | 25 ++++++++++ src/chat/memory_system/memory_system.py | 59 +++++++++++++++++++----- 2 files changed, 72 insertions(+), 12 deletions(-) diff --git a/src/chat/memory_system/memory_manager.py b/src/chat/memory_system/memory_manager.py index dd627c084..c4c76481b 100644 --- a/src/chat/memory_system/memory_manager.py +++ b/src/chat/memory_system/memory_manager.py @@ -140,6 +140,17 @@ class MemoryManager: results.append((topic, content)) logger.debug(f"从文本检索到 {len(results)} 条相关记忆") + + # 如果检索到有效记忆,打印详细信息 + if results: + logger.info(f"📚 从文本 '{text[:50]}...' 检索到 {len(results)} 条有效记忆:") + for i, (topic, content) in enumerate(results, 1): + # 处理长内容,如果超过150字符则截断 + display_content = content + if len(content) > 150: + display_content = content[:150] + "..." + logger.info(f" 记忆#{i} [{topic}]: {display_content}") + return results except Exception as e: @@ -183,6 +194,20 @@ class MemoryManager: results.append((topic, content)) logger.debug(f"从关键词 {valid_keywords} 检索到 {len(results)} 条相关记忆") + + # 如果检索到有效记忆,打印详细信息 + if results: + keywords_str = ", ".join(valid_keywords[:5]) # 最多显示5个关键词 + if len(valid_keywords) > 5: + keywords_str += f" ... (共{len(valid_keywords)}个关键词)" + logger.info(f"🔍 从关键词 [{keywords_str}] 检索到 {len(results)} 条有效记忆:") + for i, (topic, content) in enumerate(results, 1): + # 处理长内容,如果超过150字符则截断 + display_content = content + if len(content) > 150: + display_content = content[:150] + "..." + logger.info(f" 记忆#{i} [{topic}]: {display_content}") + return results except Exception as e: diff --git a/src/chat/memory_system/memory_system.py b/src/chat/memory_system/memory_system.py index 8f185cb82..b8d76753a 100644 --- a/src/chat/memory_system/memory_system.py +++ b/src/chat/memory_system/memory_system.py @@ -843,20 +843,55 @@ class MemorySystem: retrieval_time = time.time() - start_time - # 详细日志 + # 详细日志 - 打印检索到的有效记忆的完整内容 if scored_memories: - logger.info("[阶段三] 综合重排完成: Top 3 得分详情") - for i, (mem, score, details) in enumerate(scored_memories[:3], 1): + logger.info("🧠 检索到的有效记忆内容详情:") + for i, (mem, score, details) in enumerate(scored_memories[:effective_limit], 1): try: - summary = mem.content[:60] if hasattr(mem, "content") and mem.content else "" - except Exception: - summary = "" - logger.info( - f" #{i} | final={details['final']:.3f} " - f"(vec={details['vector']:.3f}, rec={details['recency']:.3f}, " - f"imp={details['importance']:.3f}, freq={details['frequency']:.3f}) " - f"| {summary}" - ) + # 获取记忆的完整内容 + memory_content = "" + if hasattr(mem, "text_content") and mem.text_content: + memory_content = mem.text_content + elif hasattr(mem, "display") and mem.display: + memory_content = mem.display + elif hasattr(mem, "content") and mem.content: + memory_content = str(mem.content) + + # 获取记忆的元数据信息 + memory_type = mem.memory_type.value if hasattr(mem, "memory_type") and mem.memory_type else "unknown" + importance = mem.metadata.importance.name if hasattr(mem.metadata, "importance") and mem.metadata.importance else "unknown" + confidence = mem.metadata.confidence.name if hasattr(mem.metadata, "confidence") and mem.metadata.confidence else "unknown" + created_time = mem.metadata.created_at if hasattr(mem.metadata, "created_at") else 0 + + # 格式化时间 + import datetime + created_time_str = datetime.datetime.fromtimestamp(created_time).strftime("%Y-%m-%d %H:%M:%S") if created_time else "unknown" + + # 打印记忆详细信息 + logger.info(f" 📝 记忆 #{i}") + logger.info(f" 类型: {memory_type} | 重要性: {importance} | 置信度: {confidence}") + logger.info(f" 创建时间: {created_time_str}") + logger.info(f" 综合得分: {details['final']:.3f} (向量:{details['vector']:.3f}, 时效:{details['recency']:.3f}, 重要性:{details['importance']:.3f}, 频率:{details['frequency']:.3f})") + + # 处理长内容,如果超过200字符则截断并添加省略号 + display_content = memory_content + if len(memory_content) > 200: + display_content = memory_content[:200] + "..." + + logger.info(f" 内容: {display_content}") + + # 如果有关键词,也打印出来 + if hasattr(mem, "keywords") and mem.keywords: + keywords_str = ", ".join(mem.keywords[:10]) # 最多显示10个关键词 + if len(mem.keywords) > 10: + keywords_str += f" ... (共{len(mem.keywords)}个关键词)" + logger.info(f" 关键词: {keywords_str}") + + logger.info("") # 空行分隔 + + except Exception as e: + logger.warning(f"打印记忆详情时出错: {e}") + continue logger.info( "✅ 三阶段记忆检索完成"