From c4a9c8a44af971f7b48b670c5eae92af9f064100 Mon Sep 17 00:00:00 2001 From: Windpicker-owo <3431391539@qq.com> Date: Fri, 7 Nov 2025 16:37:00 +0800 Subject: [PATCH] =?UTF-8?q?refactor:=20=E7=A7=BB=E9=99=A4=E5=A4=9A?= =?UTF-8?q?=E6=9F=A5=E8=AF=A2=E7=94=9F=E6=88=90=E6=96=B9=E6=B3=95=EF=BC=8C?= =?UTF-8?q?=E7=AE=80=E5=8C=96=E8=AE=B0=E5=BF=86=E6=A3=80=E7=B4=A2=E9=80=BB?= =?UTF-8?q?=E8=BE=91=EF=BC=9B=E5=9C=A8=E5=B7=A5=E5=85=B7=E6=8E=A5=E5=8F=A3?= =?UTF-8?q?=E4=B8=AD=E6=B7=BB=E5=8A=A0=E5=BD=93=E5=89=8D=E6=97=B6=E9=97=B4?= =?UTF-8?q?=E4=BF=A1=E6=81=AF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/memory_graph/manager.py | 92 -------------------------- src/memory_graph/tools/memory_tools.py | 2 + 2 files changed, 2 insertions(+), 92 deletions(-) diff --git a/src/memory_graph/manager.py b/src/memory_graph/manager.py index 738f8ec7a..5becfe0b1 100644 --- a/src/memory_graph/manager.py +++ b/src/memory_graph/manager.py @@ -345,98 +345,6 @@ class MemoryManager: return False # ==================== 记忆检索操作 ==================== - - async def generate_multi_queries( - self, - query: str, - context: dict[str, Any] | None = None, - ) -> list[tuple[str, float]]: - """ - 使用小模型生成多个查询语句(用于多路召回) - - 简化版多查询策略:直接让小模型生成3-5个不同角度的查询, - 避免复杂的查询分解和组合逻辑。 - - Args: - query: 原始查询 - context: 上下文信息(聊天历史、发言人、参与者等) - - Returns: - List of (query_string, weight) - 查询语句和权重 - """ - try: - from src.config.config import model_config - from src.llm_models.utils_model import LLMRequest - - llm = LLMRequest( - model_set=model_config.model_task_config.utils_small, - request_type="memory.multi_query_generator" - ) - - # 构建上下文信息 - chat_history = context.get("chat_history", "") if context else "" - - prompt = f"""你是记忆检索助手。为提高检索准确率,请为查询生成3-5个不同角度的搜索语句。 - -**核心原则(重要!):** -对于包含多个概念的复杂查询(如"小明如何评价小王"),应该生成: -1. 完整查询(包含所有要素)- 权重1.0 -2. 每个关键概念的独立查询(如"小明"、"小王")- 权重0.8,避免被主体淹没! -3. 主体+动作组合(如"小明 评价")- 权重0.6 -4. 泛化查询(如"评价")- 权重0.7 - -**要求:** -- 第一个必须是原始查询或同义改写 -- 识别查询中的所有重要概念,为每个概念生成独立查询 -- 查询简洁(5-20字) -- 直接输出JSON,不要添加说明 - -**对话上下文:** {chat_history[-300:] if chat_history else "无"} - -**输出JSON格式:** -```json -{{ - "queries": [ - {{"text": "完整查询", "weight": 1.0}}, - {{"text": "关键概念1", "weight": 0.8}}, - {{"text": "关键概念2", "weight": 0.8}}, - {{"text": "组合查询", "weight": 0.6}} - ] -}} -```""" - - response, _ = await llm.generate_response_async(prompt, temperature=0.3, max_tokens=300) - - # 解析JSON - import json - import re - response = re.sub(r"```json\s*", "", response) - response = re.sub(r"```\s*$", "", response).strip() - - try: - data = json.loads(response) - queries = data.get("queries", []) - - result = [] - for item in queries: - text = item.get("text", "").strip() - weight = float(item.get("weight", 0.5)) - if text: - result.append((text, weight)) - - if result: - logger.info(f"生成 {len(result)} 个查询: {[q for q, _ in result]}") - return result - - except json.JSONDecodeError as e: - logger.warning(f"解析失败: {e}, response={response[:100]}") - - except Exception as e: - logger.warning(f"多查询生成失败: {e}") - - # 回退到原始查询 - return [(query, 1.0)] - async def search_memories( self, query: str, diff --git a/src/memory_graph/tools/memory_tools.py b/src/memory_graph/tools/memory_tools.py index 2b869f0be..325cd0af4 100644 --- a/src/memory_graph/tools/memory_tools.py +++ b/src/memory_graph/tools/memory_tools.py @@ -649,6 +649,7 @@ class MemoryTools: **当前查询:** {query} **发送者:** {sender if sender else '未知'} **参与者:** {', '.join(participants) if participants else '无'} +**当前时间:** {__import__('datetime').datetime.now().__str__()} **最近聊天记录(最近5条):** {recent_chat if recent_chat else '无聊天历史'} @@ -664,6 +665,7 @@ class MemoryTools: 2. **关键概念查询**(权重0.8):查询中的核心概念,特别是聊天中提到的实体 3. **话题扩展查询**(权重0.7):基于最近聊天话题的相关查询 4. **动作/情感查询**(权重0.6):如果涉及情感或动作,生成相关查询 +5. **精准时间查询**(权重0.5):针对时间相关的查询,生成更具体的时间范围,如2023年5月1日 12:00 **输出JSON格式:** ```json