feat(memory): 移除传统内存系统并优化内存图谱
- 移除整个传统内存系统,包括内存系统模块及所有相关组件 - 删除弃用的内存组件:增强型内存激活器、海马体采样器、内存构建器、内存块、内存遗忘引擎、内存格式器、内存融合器、内存管理器、内存元数据索引、内存查询规划器、内存系统、消息集合处理器、消息集合存储、向量内存存储_v2 - 更新内存图配置,采用增强型检索设置 - 优化内存管理器查询功能,以分析完整对话上下文 - 更新机器人配置模板版本至7.6.1,新增内存图表检索参数 重大变更:旧版内存系统已被完全移除。所有内存功能现依赖于内存图系统。请更新配置以包含新的内存图检索参数。
This commit is contained in:
@@ -637,150 +637,6 @@ class Prompt:
|
||||
logger.error(f"构建表达习惯失败: {e}")
|
||||
return {"expression_habits_block": ""}
|
||||
|
||||
# _build_memory_block 和 _build_memory_block_fast 已移除
|
||||
# 记忆构建现在完全由 default_generator.py 的 build_memory_block 方法处理
|
||||
# 使用新的记忆图系统,通过 pre_built_params 传入
|
||||
|
||||
async def _REMOVED_build_memory_block(self) -> dict[str, Any]:
|
||||
"""已废弃:构建与当前对话相关的记忆上下文块(完整版)."""
|
||||
if not global_config.memory.enable_memory:
|
||||
return {"memory_block": ""}
|
||||
|
||||
try:
|
||||
from src.chat.memory_system.enhanced_memory_activator import enhanced_memory_activator
|
||||
|
||||
# 准备用于记忆激活的聊天历史
|
||||
chat_history = ""
|
||||
if self.parameters.message_list_before_now_long:
|
||||
recent_messages = self.parameters.message_list_before_now_long[-20:]
|
||||
chat_history = await build_readable_messages(
|
||||
recent_messages, replace_bot_name=True, timestamp_mode="normal", truncate=True
|
||||
)
|
||||
|
||||
# 并行查询长期记忆和即时记忆以提高性能
|
||||
import asyncio
|
||||
|
||||
memory_tasks = [
|
||||
enhanced_memory_activator.activate_memory_with_chat_history(
|
||||
target_message=self.parameters.target, chat_history_prompt=chat_history
|
||||
),
|
||||
enhanced_memory_activator.get_instant_memory(
|
||||
target_message=self.parameters.target, chat_id=self.parameters.chat_id
|
||||
),
|
||||
]
|
||||
|
||||
try:
|
||||
# 使用 `return_exceptions=True` 来防止一个任务的失败导致所有任务失败
|
||||
running_memories, instant_memory = await asyncio.gather(*memory_tasks, return_exceptions=True)
|
||||
|
||||
# 单独处理每个任务的结果,如果是异常则记录并使用默认值
|
||||
if isinstance(running_memories, BaseException):
|
||||
logger.warning(f"长期记忆查询失败: {running_memories}")
|
||||
running_memories = []
|
||||
if isinstance(instant_memory, BaseException):
|
||||
logger.warning(f"即时记忆查询失败: {instant_memory}")
|
||||
instant_memory = None
|
||||
|
||||
except asyncio.TimeoutError:
|
||||
logger.warning("记忆查询超时,使用部分结果")
|
||||
running_memories = []
|
||||
instant_memory = None
|
||||
|
||||
# 将检索到的记忆格式化为提示词
|
||||
if running_memories:
|
||||
try:
|
||||
from src.chat.memory_system.memory_formatter import format_memories_bracket_style
|
||||
|
||||
# 将原始记忆数据转换为格式化器所需的标准格式
|
||||
formatted_memories = []
|
||||
for memory in running_memories:
|
||||
content = memory.get("content", "")
|
||||
display_text = content
|
||||
# 清理内容,移除元数据括号
|
||||
if "(类型:" in content and ")" in content:
|
||||
display_text = content.split("(类型:")[0].strip()
|
||||
|
||||
# 映射记忆主题到标准类型
|
||||
topic = memory.get("topic", "personal_fact")
|
||||
memory_type_mapping = {
|
||||
"relationship": "personal_fact",
|
||||
"opinion": "opinion",
|
||||
"personal_fact": "personal_fact",
|
||||
"preference": "preference",
|
||||
"event": "event",
|
||||
}
|
||||
mapped_type = memory_type_mapping.get(topic, "personal_fact")
|
||||
|
||||
formatted_memories.append(
|
||||
{
|
||||
"display": display_text,
|
||||
"memory_type": mapped_type,
|
||||
"metadata": {
|
||||
"confidence": memory.get("confidence", "未知"),
|
||||
"importance": memory.get("importance", "一般"),
|
||||
"timestamp": memory.get("timestamp", ""),
|
||||
"source": memory.get("source", "unknown"),
|
||||
"relevance_score": memory.get("relevance_score", 0.0),
|
||||
},
|
||||
}
|
||||
)
|
||||
|
||||
# 使用指定的风格进行格式化
|
||||
memory_block = format_memories_bracket_style(
|
||||
formatted_memories, query_context=self.parameters.target
|
||||
)
|
||||
except Exception as e:
|
||||
# 如果格式化失败,提供一个简化的、健壮的备用格式
|
||||
logger.warning(f"记忆格式化失败,使用简化格式: {e}")
|
||||
memory_parts = ["## 相关记忆回顾", ""]
|
||||
for memory in running_memories:
|
||||
content = memory.get("content", "")
|
||||
if "(类型:" in content and ")" in content:
|
||||
clean_content = content.split("(类型:")[0].strip()
|
||||
memory_parts.append(f"- {clean_content}")
|
||||
else:
|
||||
memory_parts.append(f"- {content}")
|
||||
memory_block = "\n".join(memory_parts)
|
||||
else:
|
||||
memory_block = ""
|
||||
|
||||
# 将即时记忆附加到记忆块的末尾
|
||||
if instant_memory:
|
||||
if memory_block:
|
||||
memory_block += f"\n- 最相关记忆:{instant_memory}"
|
||||
else:
|
||||
memory_block = f"- 最相关记忆:{instant_memory}"
|
||||
|
||||
return {"memory_block": memory_block}
|
||||
|
||||
except Exception as e:
|
||||
logger.error(f"构建记忆块失败: {e}")
|
||||
return {"memory_block": ""}
|
||||
|
||||
async def _REMOVED_build_memory_block_fast(self) -> dict[str, Any]:
|
||||
"""已废弃:快速构建记忆块(简化版),作为未预构建时的后备方案."""
|
||||
if not global_config.memory.enable_memory:
|
||||
return {"memory_block": ""}
|
||||
|
||||
try:
|
||||
from src.chat.memory_system.enhanced_memory_activator import enhanced_memory_activator
|
||||
|
||||
# 这个快速版本只查询最高优先级的“即时记忆”,速度更快
|
||||
instant_memory = await enhanced_memory_activator.get_instant_memory(
|
||||
target_message=self.parameters.target, chat_id=self.parameters.chat_id
|
||||
)
|
||||
|
||||
if instant_memory:
|
||||
memory_block = f"- 相关记忆:{instant_memory}"
|
||||
else:
|
||||
memory_block = ""
|
||||
|
||||
return {"memory_block": memory_block}
|
||||
|
||||
except Exception as e:
|
||||
logger.warning(f"快速构建记忆块失败: {e}")
|
||||
return {"memory_block": ""}
|
||||
|
||||
async def _build_relation_info(self) -> dict[str, Any]:
|
||||
"""构建与对话目标相关的关系信息."""
|
||||
try:
|
||||
|
||||
Reference in New Issue
Block a user