From e55eb748905b493943e2425ea4b89c8f1b580455 Mon Sep 17 00:00:00 2001 From: 2829798842 <2829798842@qq.com> Date: Tue, 27 May 2025 22:19:43 +0800 Subject: [PATCH] Update get_memory.py --- src/tools/not_used/get_memory.py | 67 +++++++++++++++++++++----------- 1 file changed, 45 insertions(+), 22 deletions(-) diff --git a/src/tools/not_used/get_memory.py b/src/tools/not_used/get_memory.py index e543f3c5..c2ec550c 100644 --- a/src/tools/not_used/get_memory.py +++ b/src/tools/not_used/get_memory.py @@ -1,41 +1,64 @@ -from typing import Any -from src.common.logger_manager import get_logger from src.tools.tool_can_use.base_tool import BaseTool +from src.chat.memory_system.Hippocampus import HippocampusManager +from src.common.logger import get_module_logger +from typing import Dict, Any + +logger = get_module_logger("mid_chat_mem_tool") -logger = get_logger("relationship_tool") +class GetMemoryTool(BaseTool): + """从记忆系统中获取相关记忆的工具""" - -class RelationshipTool(BaseTool): - name = "change_relationship" - description = "Modify relationship value with specific user based on received text and reply content, you can use this tool when you reply to someone's message" + name = "get_memory" + description = "Use tool to retrieve relevant memories from the memory system" parameters = { "type": "object", "properties": { - "text": {"type": "string", "description": "Received text"}, - "changed_value": {"type": "number", "description": "Change value"}, - "reason": {"type": "string", "description": "Reason for change"}, + "topic": {"type": "string", "description": "Related topics to query, separated by commas"}, + "max_memory_num": {"type": "integer", "description": "Maximum number of memories to return"}, }, - "required": ["text", "changed_value", "reason"], + "required": ["topic"], } - async def execute(self, function_args: dict[str, Any], message_txt: str = "") -> dict: - """执行工具功能 + async def execute(self, function_args: Dict[str, Any]) -> Dict[str, Any]: + """执行记忆获取 Args: - function_args: 包含工具参数的字典 - message_txt: 原始消息文本 + function_args: 工具参数 Returns: - dict: 包含执行结果的字典 + Dict: 工具执行结果 """ try: - text = function_args.get("text") - changed_value = function_args.get("changed_value") - reason = function_args.get("reason") + topic = function_args.get("topic") + max_memory_num = function_args.get("max_memory_num", 2) - return {"content": f"Because you just {reason}, your relationship value with the person who sent [{text}] has changed by {changed_value}"} + # 将主题字符串转换为列表 + topic_list = topic.split(",") + # 调用记忆系统 + related_memory = await HippocampusManager.get_instance().get_memory_from_topic( + valid_keywords=topic_list, max_memory_num=max_memory_num, max_memory_length=2, max_depth=3 + ) + + memory_info = "" + if related_memory: + for memory in related_memory: + memory_info += memory[1] + "\n" + + if memory_info: + content = f"You remember these things: {memory_info}\n" + content += "The above are your memories, not necessarily what people in the current chat said, nor necessarily what is happening now, please remember.\n" + + else: + content = f"Memories about {topic}, you don't remember clearly" + + return {"type": "memory", "id": topic_list, "content": content} except Exception as e: - logger.error(f"Error occurred while modifying relationship value: {str(e)}") - return {"content": f"Failed to modify relationship value: {str(e)}"} + logger.error(f"Memory retrieval tool execution failed: {str(e)}") + # Keep format consistent on failure, but id may not apply or be set to None/Error + return {"type": "memory_error", "id": topic_list, "content": f"Memory retrieval failed: {str(e)}"} + + +# 注册工具 +# register_tool(GetMemoryTool)