mirror of https://github.com/Mai-with-u/MaiBot.git
Update get_memory.py
parent
d1c940c782
commit
e55eb74890
|
|
@ -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.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):
|
||||||
|
"""从记忆系统中获取相关记忆的工具"""
|
||||||
|
|
||||||
|
name = "get_memory"
|
||||||
class RelationshipTool(BaseTool):
|
description = "Use tool to retrieve relevant memories from the memory system"
|
||||||
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 = {
|
parameters = {
|
||||||
"type": "object",
|
"type": "object",
|
||||||
"properties": {
|
"properties": {
|
||||||
"text": {"type": "string", "description": "Received text"},
|
"topic": {"type": "string", "description": "Related topics to query, separated by commas"},
|
||||||
"changed_value": {"type": "number", "description": "Change value"},
|
"max_memory_num": {"type": "integer", "description": "Maximum number of memories to return"},
|
||||||
"reason": {"type": "string", "description": "Reason for change"},
|
|
||||||
},
|
},
|
||||||
"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:
|
Args:
|
||||||
function_args: 包含工具参数的字典
|
function_args: 工具参数
|
||||||
message_txt: 原始消息文本
|
|
||||||
|
|
||||||
Returns:
|
Returns:
|
||||||
dict: 包含执行结果的字典
|
Dict: 工具执行结果
|
||||||
"""
|
"""
|
||||||
try:
|
try:
|
||||||
text = function_args.get("text")
|
topic = function_args.get("topic")
|
||||||
changed_value = function_args.get("changed_value")
|
max_memory_num = function_args.get("max_memory_num", 2)
|
||||||
reason = function_args.get("reason")
|
|
||||||
|
|
||||||
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:
|
except Exception as e:
|
||||||
logger.error(f"Error occurred while modifying relationship value: {str(e)}")
|
logger.error(f"Memory retrieval tool execution failed: {str(e)}")
|
||||||
return {"content": f"Failed to modify relationship value: {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)
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue