mirror of https://github.com/Mai-with-u/MaiBot.git
feat: 增加记忆功能开关
parent
4d5456ed4b
commit
dd80f7d0b2
|
|
@ -238,7 +238,8 @@ class BrainChatting:
|
||||||
|
|
||||||
async with global_prompt_manager.async_message_scope(self.chat_stream.context.get_template_name()):
|
async with global_prompt_manager.async_message_scope(self.chat_stream.context.get_template_name()):
|
||||||
asyncio.create_task(self.expression_learner.trigger_learning_for_chat())
|
asyncio.create_task(self.expression_learner.trigger_learning_for_chat())
|
||||||
asyncio.create_task(global_memory_chest.build_running_content(chat_id=self.stream_id))
|
if getattr(global_config.memory, "enable_memory", True):
|
||||||
|
asyncio.create_task(global_memory_chest.build_running_content(chat_id=self.stream_id))
|
||||||
|
|
||||||
cycle_timers, thinking_id = self.start_cycle()
|
cycle_timers, thinking_id = self.start_cycle()
|
||||||
logger.info(f"{self.log_prefix} 开始第{self._cycle_counter}次思考")
|
logger.info(f"{self.log_prefix} 开始第{self._cycle_counter}次思考")
|
||||||
|
|
|
||||||
|
|
@ -331,7 +331,8 @@ class HeartFChatting:
|
||||||
|
|
||||||
async with global_prompt_manager.async_message_scope(self.chat_stream.context.get_template_name()):
|
async with global_prompt_manager.async_message_scope(self.chat_stream.context.get_template_name()):
|
||||||
asyncio.create_task(self.expression_learner.trigger_learning_for_chat())
|
asyncio.create_task(self.expression_learner.trigger_learning_for_chat())
|
||||||
asyncio.create_task(global_memory_chest.build_running_content(chat_id=self.stream_id))
|
if getattr(global_config.memory, "enable_memory", True):
|
||||||
|
asyncio.create_task(global_memory_chest.build_running_content(chat_id=self.stream_id))
|
||||||
asyncio.create_task(frequency_control_manager.get_or_create_frequency_control(self.stream_id).trigger_frequency_adjust())
|
asyncio.create_task(frequency_control_manager.get_or_create_frequency_control(self.stream_id).trigger_frequency_adjust())
|
||||||
|
|
||||||
# 添加curious检测任务 - 检测聊天记录中的矛盾、冲突或需要提问的内容
|
# 添加curious检测任务 - 检测聊天记录中的矛盾、冲突或需要提问的内容
|
||||||
|
|
|
||||||
|
|
@ -278,8 +278,8 @@ class DefaultReplyer:
|
||||||
async def build_memory_block(self) -> str:
|
async def build_memory_block(self) -> str:
|
||||||
"""构建记忆块
|
"""构建记忆块
|
||||||
"""
|
"""
|
||||||
# if not global_config.memory.enable_memory:
|
if not getattr(global_config.memory, "enable_memory", True):
|
||||||
# return ""
|
return ""
|
||||||
|
|
||||||
if global_memory_chest.get_chat_memories_as_string(self.chat_stream.stream_id):
|
if global_memory_chest.get_chat_memories_as_string(self.chat_stream.stream_id):
|
||||||
return f"你有以下记忆:\n{global_memory_chest.get_chat_memories_as_string(self.chat_stream.stream_id)}"
|
return f"你有以下记忆:\n{global_memory_chest.get_chat_memories_as_string(self.chat_stream.stream_id)}"
|
||||||
|
|
|
||||||
|
|
@ -294,6 +294,9 @@ class PrivateReplyer:
|
||||||
async def build_memory_block(self) -> str:
|
async def build_memory_block(self) -> str:
|
||||||
"""构建记忆块
|
"""构建记忆块
|
||||||
"""
|
"""
|
||||||
|
if not getattr(global_config.memory, "enable_memory", True):
|
||||||
|
return ""
|
||||||
|
|
||||||
if global_memory_chest.get_chat_memories_as_string(self.chat_stream.stream_id):
|
if global_memory_chest.get_chat_memories_as_string(self.chat_stream.stream_id):
|
||||||
return f"你有以下记忆:\n{global_memory_chest.get_chat_memories_as_string(self.chat_stream.stream_id)}"
|
return f"你有以下记忆:\n{global_memory_chest.get_chat_memories_as_string(self.chat_stream.stream_id)}"
|
||||||
else:
|
else:
|
||||||
|
|
@ -1022,4 +1025,3 @@ def weighted_sample_no_replacement(items, weights, k) -> list:
|
||||||
return selected
|
return selected
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -315,6 +315,9 @@ class MessageReceiveConfig(ConfigBase):
|
||||||
class MemoryConfig(ConfigBase):
|
class MemoryConfig(ConfigBase):
|
||||||
"""记忆配置类"""
|
"""记忆配置类"""
|
||||||
|
|
||||||
|
enable_memory: bool = True
|
||||||
|
"""是否启用记忆系统"""
|
||||||
|
|
||||||
max_memory_number: int = 100
|
max_memory_number: int = 100
|
||||||
"""记忆最大数量"""
|
"""记忆最大数量"""
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -94,8 +94,11 @@ class MainSystem:
|
||||||
logger.info("聊天管理器初始化成功")
|
logger.info("聊天管理器初始化成功")
|
||||||
|
|
||||||
# 添加记忆管理任务
|
# 添加记忆管理任务
|
||||||
await async_task_manager.add_task(MemoryManagementTask())
|
if getattr(global_config.memory, "enable_memory", True):
|
||||||
logger.info("记忆管理任务已启动")
|
await async_task_manager.add_task(MemoryManagementTask())
|
||||||
|
logger.info("记忆管理任务已启动")
|
||||||
|
else:
|
||||||
|
logger.info("记忆功能已禁用,跳过记忆管理任务")
|
||||||
|
|
||||||
# await asyncio.sleep(0.5) #防止logger输出飞了
|
# await asyncio.sleep(0.5) #防止logger输出飞了
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -52,6 +52,10 @@ class MemoryChest:
|
||||||
Returns:
|
Returns:
|
||||||
str: 构建后的运行内容
|
str: 构建后的运行内容
|
||||||
"""
|
"""
|
||||||
|
if not getattr(global_config.memory, "enable_memory", True):
|
||||||
|
logger.debug("记忆系统已禁用,跳过记忆构建任务")
|
||||||
|
return ""
|
||||||
|
|
||||||
# 检查是否需要更新:基于消息数量和最新消息时间差的智能更新机制
|
# 检查是否需要更新:基于消息数量和最新消息时间差的智能更新机制
|
||||||
#
|
#
|
||||||
# 更新机制说明:
|
# 更新机制说明:
|
||||||
|
|
@ -225,6 +229,10 @@ class MemoryChest:
|
||||||
"""
|
"""
|
||||||
根据问题获取答案
|
根据问题获取答案
|
||||||
"""
|
"""
|
||||||
|
if not getattr(global_config.memory, "enable_memory", True):
|
||||||
|
logger.debug("记忆系统已禁用,跳过记忆检索")
|
||||||
|
return ""
|
||||||
|
|
||||||
logger.info(f"正在回忆问题答案: {question}")
|
logger.info(f"正在回忆问题答案: {question}")
|
||||||
|
|
||||||
title = await self.select_title_by_question(question)
|
title = await self.select_title_by_question(question)
|
||||||
|
|
@ -282,6 +290,9 @@ class MemoryChest:
|
||||||
Returns:
|
Returns:
|
||||||
str: 格式化的记忆字符串,格式:问题:xxx,答案:xxxxx\n问题:xxx,答案:xxxxx\n...
|
str: 格式化的记忆字符串,格式:问题:xxx,答案:xxxxx\n问题:xxx,答案:xxxxx\n...
|
||||||
"""
|
"""
|
||||||
|
if not getattr(global_config.memory, "enable_memory", True):
|
||||||
|
return ""
|
||||||
|
|
||||||
try:
|
try:
|
||||||
memories = []
|
memories = []
|
||||||
|
|
||||||
|
|
@ -796,4 +807,4 @@ MutePlugin 是禁言插件的名称
|
||||||
return 0
|
return 0
|
||||||
|
|
||||||
|
|
||||||
global_memory_chest = MemoryChest()
|
global_memory_chest = MemoryChest()
|
||||||
|
|
|
||||||
|
|
@ -34,6 +34,10 @@ class MemoryManagementTask(AsyncTask):
|
||||||
|
|
||||||
async def start_task(self, abort_flag: asyncio.Event):
|
async def start_task(self, abort_flag: asyncio.Event):
|
||||||
"""重写start_task方法,支持动态调整执行间隔"""
|
"""重写start_task方法,支持动态调整执行间隔"""
|
||||||
|
if not getattr(global_config.memory, "enable_memory", True):
|
||||||
|
logger.info("记忆管理任务未启动:记忆系统已禁用")
|
||||||
|
return
|
||||||
|
|
||||||
if self.wait_before_start > 0:
|
if self.wait_before_start > 0:
|
||||||
# 等待指定时间后开始任务
|
# 等待指定时间后开始任务
|
||||||
await asyncio.sleep(self.wait_before_start)
|
await asyncio.sleep(self.wait_before_start)
|
||||||
|
|
@ -86,6 +90,10 @@ class MemoryManagementTask(AsyncTask):
|
||||||
|
|
||||||
async def run(self):
|
async def run(self):
|
||||||
"""执行记忆管理任务"""
|
"""执行记忆管理任务"""
|
||||||
|
if not getattr(global_config.memory, "enable_memory", True):
|
||||||
|
logger.debug("记忆系统已禁用,跳过记忆管理任务执行")
|
||||||
|
return
|
||||||
|
|
||||||
try:
|
try:
|
||||||
|
|
||||||
# 获取当前记忆数量
|
# 获取当前记忆数量
|
||||||
|
|
|
||||||
|
|
@ -94,14 +94,17 @@ class GetMemoryTool(BaseTool):
|
||||||
if has_time_params and not self.chat_id:
|
if has_time_params and not self.chat_id:
|
||||||
return {"content": f"问题:{question},无法获取聊天记录:缺少chat_id"}
|
return {"content": f"问题:{question},无法获取聊天记录:缺少chat_id"}
|
||||||
|
|
||||||
|
memory_enabled = getattr(global_config.memory, "enable_memory", True)
|
||||||
|
|
||||||
# 创建并行任务
|
# 创建并行任务
|
||||||
tasks = []
|
tasks = []
|
||||||
|
|
||||||
# 原任务:从记忆仓库获取答案
|
# 原任务:从记忆仓库获取答案
|
||||||
memory_task = asyncio.create_task(
|
if memory_enabled:
|
||||||
global_memory_chest.get_answer_by_question(question=question)
|
memory_task = asyncio.create_task(
|
||||||
)
|
global_memory_chest.get_answer_by_question(question=question)
|
||||||
tasks.append(("memory", memory_task))
|
)
|
||||||
|
tasks.append(("memory", memory_task))
|
||||||
|
|
||||||
# 新任务:从聊天记录获取答案(如果指定了时间参数)
|
# 新任务:从聊天记录获取答案(如果指定了时间参数)
|
||||||
chat_task = None
|
chat_task = None
|
||||||
|
|
@ -121,13 +124,15 @@ class GetMemoryTool(BaseTool):
|
||||||
results[task_name] = None
|
results[task_name] = None
|
||||||
|
|
||||||
# 处理结果
|
# 处理结果
|
||||||
memory_answer = results.get("memory")
|
memory_answer = results.get("memory") if memory_enabled else None
|
||||||
chat_answer = results.get("chat")
|
chat_answer = results.get("chat")
|
||||||
|
|
||||||
# 构建返回内容
|
# 构建返回内容
|
||||||
content_parts = [f"问题:{question}"]
|
content_parts = [f"问题:{question}"]
|
||||||
|
|
||||||
if memory_answer:
|
if not memory_enabled:
|
||||||
|
content_parts.append("记忆功能已关闭,未执行记忆检索")
|
||||||
|
elif memory_answer:
|
||||||
content_parts.append(f"对问题'{question}',你回忆的信息是:{memory_answer}")
|
content_parts.append(f"对问题'{question}',你回忆的信息是:{memory_answer}")
|
||||||
else:
|
else:
|
||||||
content_parts.append(f"对问题'{question}',没有什么印象")
|
content_parts.append(f"对问题'{question}',没有什么印象")
|
||||||
|
|
@ -276,8 +281,16 @@ class GetMemoryAction(BaseAction):
|
||||||
|
|
||||||
async def execute(self) -> Tuple[bool, str]:
|
async def execute(self) -> Tuple[bool, str]:
|
||||||
"""执行关系动作"""
|
"""执行关系动作"""
|
||||||
|
|
||||||
question = self.action_data.get("question", "")
|
question = self.action_data.get("question", "")
|
||||||
|
|
||||||
|
if not getattr(global_config.memory, "enable_memory", True):
|
||||||
|
await self.store_action_info(
|
||||||
|
action_build_into_prompt=True,
|
||||||
|
action_prompt_display=f"你尝试回忆问题:{question},但记忆功能已关闭",
|
||||||
|
action_done=True,
|
||||||
|
)
|
||||||
|
return False, f"问题:{question},记忆功能已关闭"
|
||||||
|
|
||||||
answer = await global_memory_chest.get_answer_by_question(self.chat_id, question)
|
answer = await global_memory_chest.get_answer_by_question(self.chat_id, question)
|
||||||
if not answer:
|
if not answer:
|
||||||
await self.store_action_info(
|
await self.store_action_info(
|
||||||
|
|
|
||||||
|
|
@ -1,5 +1,5 @@
|
||||||
[inner]
|
[inner]
|
||||||
version = "6.19.1"
|
version = "6.19.2"
|
||||||
|
|
||||||
#----以下是给开发人员阅读的,如果你只是部署了麦麦,不需要阅读----
|
#----以下是给开发人员阅读的,如果你只是部署了麦麦,不需要阅读----
|
||||||
#如果你想要修改配置文件,请递增version的值
|
#如果你想要修改配置文件,请递增version的值
|
||||||
|
|
@ -120,6 +120,7 @@ auto_chat_value_rules = [
|
||||||
]
|
]
|
||||||
|
|
||||||
[memory]
|
[memory]
|
||||||
|
enable_memory = true # 是否启用记忆系统
|
||||||
max_memory_number = 100 # 记忆最大数量
|
max_memory_number = 100 # 记忆最大数量
|
||||||
max_memory_size = 2048 # 记忆最大大小
|
max_memory_size = 2048 # 记忆最大大小
|
||||||
memory_build_frequency = 1 # 记忆构建频率
|
memory_build_frequency = 1 # 记忆构建频率
|
||||||
|
|
@ -242,4 +243,4 @@ none = false # 暂无
|
||||||
|
|
||||||
#此系统暂时移除,无效配置
|
#此系统暂时移除,无效配置
|
||||||
[relationship]
|
[relationship]
|
||||||
enable_relationship = true # 是否启用关系系统
|
enable_relationship = true # 是否启用关系系统
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue