Update get_memory.py

pull/992/head
2829798842 2025-05-27 20:28:19 +08:00 committed by GitHub
parent a99eaf9491
commit 09eaac41ca
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
1 changed files with 22 additions and 45 deletions

View File

@ -1,64 +1,41 @@
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")
class GetMemoryTool(BaseTool):
"""从记忆系统中获取相关记忆的工具"""
logger = get_logger("relationship_tool")
name = "get_memory"
description = "Use tool to retrieve relevant memories from the memory system"
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"
parameters = {
"type": "object",
"properties": {
"topic": {"type": "string", "description": "Related topics to query, separated by commas"},
"max_memory_num": {"type": "integer", "description": "Maximum number of memories to return"},
"text": {"type": "string", "description": "Received text"},
"changed_value": {"type": "number", "description": "Change value"},
"reason": {"type": "string", "description": "Reason for change"},
},
"required": ["topic"],
"required": ["text", "changed_value", "reason"],
}
async def execute(self, function_args: Dict[str, Any]) -> Dict[str, Any]:
"""执行记忆获取
async def execute(self, function_args: dict[str, Any], message_txt: str = "") -> dict:
"""执行工具功能
Args:
function_args: 工具参数
function_args: 包含工具参数的字典
message_txt: 原始消息文本
Returns:
Dict: 工具执行结果
dict: 包含执行结果的字典
"""
try:
topic = function_args.get("topic")
max_memory_num = function_args.get("max_memory_num", 2)
text = function_args.get("text")
changed_value = function_args.get("changed_value")
reason = function_args.get("reason")
# 将主题字符串转换为列表
topic_list = topic.split(",")
return {"content": f"Because you just {reason}, your relationship value with the person who sent [{text}] has changed by {changed_value}"}
# 调用记忆系统
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"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)
logger.error(f"Error occurred while modifying relationship value: {str(e)}")
return {"content": f"Failed to modify relationship value: {str(e)}"}