feat:非常好的早期聊天记录压缩系统,麦麦现在有5倍上文记忆量(真的吗?

This commit is contained in:
SengokuCola
2025-04-14 21:33:27 +08:00
parent 7eba42f84a
commit 46347678b4
12 changed files with 314 additions and 446 deletions

View File

@@ -3,7 +3,6 @@ from ..person_info.relationship_manager import relationship_manager
from .chat_stream import chat_manager
from .message_sender import message_manager
from ..storage.storage import MessageStorage
from .auto_speak import auto_speak_manager
__all__ = [
@@ -12,5 +11,4 @@ __all__ = [
"chat_manager",
"message_manager",
"MessageStorage",
"auto_speak_manager",
]

View File

@@ -1,184 +0,0 @@
import time
import asyncio
import random
from random import random as random_float
from typing import Dict
from ..config.config import global_config
from .message import MessageSending, MessageThinking, MessageSet, MessageRecv
from ..message.message_base import UserInfo, Seg
from .message_sender import message_manager
from ..moods.moods import MoodManager
from ..chat_module.reasoning_chat.reasoning_generator import ResponseGenerator
from src.common.logger import get_module_logger
from src.heart_flow.heartflow import heartflow
from ...common.database import db
logger = get_module_logger("auto_speak")
class AutoSpeakManager:
def __init__(self):
self._last_auto_speak_time: Dict[str, float] = {} # 记录每个聊天流上次自主发言的时间
self.mood_manager = MoodManager.get_instance()
self.gpt = ResponseGenerator() # 添加gpt实例
self._started = False
self._check_task = None
self.db = db
async def get_chat_info(self, chat_id: str) -> dict:
"""从数据库获取聊天流信息"""
chat_info = await self.db.chat_streams.find_one({"stream_id": chat_id})
return chat_info
async def start_auto_speak_check(self):
"""启动自动发言检查任务"""
if not self._started:
self._check_task = asyncio.create_task(self._periodic_check())
self._started = True
logger.success("自动发言检查任务已启动")
async def _periodic_check(self):
"""定期检查是否需要自主发言"""
while True and global_config.enable_think_flow:
# 获取所有活跃的子心流
active_subheartflows = []
for chat_id, subheartflow in heartflow._subheartflows.items():
if (
subheartflow.is_active and subheartflow.current_state.willing > 0
): # 只考虑活跃且意愿值大于0.5的子心流
active_subheartflows.append((chat_id, subheartflow))
logger.debug(
f"发现活跃子心流 - 聊天ID: {chat_id}, 意愿值: {subheartflow.current_state.willing:.2f}"
)
if not active_subheartflows:
logger.debug("当前没有活跃的子心流")
await asyncio.sleep(20) # 添加异步等待
continue
# 随机选择一个活跃的子心流
chat_id, subheartflow = random.choice(active_subheartflows)
logger.info(f"随机选择子心流 - 聊天ID: {chat_id}, 意愿值: {subheartflow.current_state.willing:.2f}")
# 检查是否应该自主发言
if await self.check_auto_speak(subheartflow):
logger.info(f"准备自主发言 - 聊天ID: {chat_id}")
# 生成自主发言
bot_user_info = UserInfo(
user_id=global_config.BOT_QQ,
user_nickname=global_config.BOT_NICKNAME,
platform="qq", # 默认使用qq平台
)
# 创建一个空的MessageRecv对象作为上下文
message = MessageRecv(
{
"message_info": {
"user_info": {"user_id": chat_id, "user_nickname": "", "platform": "qq"},
"group_info": None,
"platform": "qq",
"time": time.time(),
},
"processed_plain_text": "",
"raw_message": "",
"is_emoji": False,
}
)
await self.generate_auto_speak(
subheartflow, message, bot_user_info, message.message_info["user_info"], message.message_info
)
else:
logger.debug(f"不满足自主发言条件 - 聊天ID: {chat_id}")
# 每分钟检查一次
await asyncio.sleep(20)
# await asyncio.sleep(5) # 发生错误时等待5秒再继续
async def check_auto_speak(self, subheartflow) -> bool:
"""检查是否应该自主发言"""
if not subheartflow:
return False
current_time = time.time()
chat_id = subheartflow.observe_chat_id
# 获取上次自主发言时间
if chat_id not in self._last_auto_speak_time:
self._last_auto_speak_time[chat_id] = 0
last_speak_time = self._last_auto_speak_time.get(chat_id, 0)
# 如果距离上次自主发言不到5分钟不发言
if current_time - last_speak_time < 30:
logger.debug(
f"距离上次发言时间太短 - 聊天ID: {chat_id}, 剩余时间: {30 - (current_time - last_speak_time):.1f}"
)
return False
# 获取当前意愿值
current_willing = subheartflow.current_state.willing
if current_willing > 0.1 and random_float() < 0.5:
self._last_auto_speak_time[chat_id] = current_time
logger.info(f"满足自主发言条件 - 聊天ID: {chat_id}, 意愿值: {current_willing:.2f}")
return True
logger.debug(f"不满足自主发言条件 - 聊天ID: {chat_id}, 意愿值: {current_willing:.2f}")
return False
async def generate_auto_speak(self, subheartflow, message, bot_user_info: UserInfo, userinfo, messageinfo):
"""生成自主发言内容"""
thinking_time_point = round(time.time(), 2)
think_id = "mt" + str(thinking_time_point)
thinking_message = MessageThinking(
message_id=think_id,
chat_stream=None, # 不需要chat_stream
bot_user_info=bot_user_info,
reply=message,
thinking_start_time=thinking_time_point,
)
message_manager.add_message(thinking_message)
# 生成自主发言内容
try:
response, raw_content = await self.gpt.generate_response(message)
except Exception as e:
logger.error(f"生成自主发言内容时发生错误: {e}")
return False
if response:
message_set = MessageSet(None, think_id) # 不需要chat_stream
mark_head = False
for msg in response:
message_segment = Seg(type="text", data=msg)
bot_message = MessageSending(
message_id=think_id,
chat_stream=None, # 不需要chat_stream
bot_user_info=bot_user_info,
sender_info=userinfo,
message_segment=message_segment,
reply=message,
is_head=not mark_head,
is_emoji=False,
thinking_start_time=thinking_time_point,
)
if not mark_head:
mark_head = True
message_set.add_message(bot_message)
message_manager.add_message(message_set)
# 更新情绪和关系
stance, emotion = await self.gpt._get_emotion_tags(raw_content, message.processed_plain_text)
self.mood_manager.update_mood_from_emotion(emotion, global_config.mood_intensity_factor)
return True
return False
# 创建全局AutoSpeakManager实例
auto_speak_manager = AutoSpeakManager()

View File

@@ -236,7 +236,7 @@ class ThinkFlowChat:
if random() < reply_probability:
try:
do_reply = True
# 回复前处理
await willing_manager.before_generate_reply_handle(message.message_info.message_id)
@@ -252,80 +252,77 @@ class ThinkFlowChat:
info_catcher = info_catcher_manager.get_info_catcher(thinking_id)
info_catcher.catch_decide_to_response(message)
# 观察
try:
# 观察
with Timer("观察", timing_results):
await heartflow.get_subheartflow(chat.stream_id).do_observe()
except Exception as e:
logger.error(f"心流观察失败: {e}")
traceback.print_exc()
info_catcher.catch_after_observe(timing_results["观察"])
# 思考前使用工具
update_relationship = ""
get_mid_memory_id = []
tool_result_info = {}
try:
with Timer("思考前使用工具", timing_results):
tool_result = await self.tool_user.use_tool(message.processed_plain_text, message.message_info.user_info.user_nickname, chat)
tool_result = await self.tool_user.use_tool(
message.processed_plain_text,
message.message_info.user_info.user_nickname,
chat,
heartflow.get_subheartflow(chat.stream_id))
# 如果工具被使用且获得了结果,将收集到的信息合并到思考中
collected_info = ""
# collected_info = ""
if tool_result.get("used_tools", False):
# 如果有收集到的结构化信息,将其格式化后添加到当前思考中
if "structured_info" in tool_result:
info = tool_result["structured_info"]
# 处理记忆信息
if info["memory"]:
collected_info += "\n记忆相关信息:\n"
for mem in info["memory"]:
collected_info += f"- {mem['name']}: {mem['content']}\n"
tool_result_info = tool_result["structured_info"]
# collected_info = ""
get_mid_memory_id = []
update_relationship = ""
# 处理日程信息
if info["schedule"]:
collected_info += "\n日程相关信息:\n"
for sch in info["schedule"]:
collected_info += f"- {sch['name']}: {sch['content']}\n"
# 处理知识信息
if info["knowledge"]:
collected_info += "\n知识相关信息:\n"
for know in info["knowledge"]:
collected_info += f"- {know['name']}: {know['content']}\n"
# 处理关系信息
if info["change_relationship"]:
collected_info += "\n关系相关信息:\n"
for rel in info["change_relationship"]:
collected_info += f"- {rel['name']}: {rel['content']}\n"
# print("11111111111111111111111111111")
update_relationship += rel["content"]
# print(f"11111111111111111111111111111{update_relationship}")
# 动态解析工具结果
for tool_name, tool_data in tool_result_info.items():
# tool_result_info += f"\n{tool_name} 相关信息:\n"
# for item in tool_data:
# tool_result_info += f"- {item['name']}: {item['content']}\n"
# 特殊判定mid_chat_mem
if tool_name == "mid_chat_mem":
for mid_memory in tool_data:
get_mid_memory_id.append(mid_memory['content'])
# 特殊判定change_mood
if tool_name == "change_mood":
for mood in tool_data:
self.mood_manager.update_mood_from_emotion(
mood['content'],
global_config.mood_intensity_factor
)
# 特殊判定change_relationship
if tool_name == "change_relationship":
update_relationship = tool_data[0]["content"]
# 处理心情信息
if info["change_mood"]:
collected_info += "\n心情相关信息:\n"
for mood in info["change_mood"]:
collected_info += f"- {mood['name']}: {mood['content']}\n"
# 处理其他信息
if info["other"]:
collected_info += "\n其他相关信息:\n"
for other in info["other"]:
collected_info += f"- {other['name']}: {other['content']}\n"
except Exception as e:
logger.error(f"思考前工具调用失败: {e}")
logger.error(traceback.format_exc())
# 处理关系更新
if update_relationship:
# ori_response = ",".join(response_set)
# print("22222222222222222222222222222")
stance, emotion = await self.gpt._get_emotion_tags_with_reason("你还没有回复", message.processed_plain_text,update_relationship)
stance, emotion = await self.gpt._get_emotion_tags_with_reason(
"你还没有回复",
message.processed_plain_text,
update_relationship
)
await relationship_manager.calculate_update_relationship_value(
chat_stream=message.chat_stream, label=emotion, stance=stance
)
print("33333333333333333333333333333")
chat_stream=message.chat_stream,
label=emotion,
stance=stance
)
# 思考前脑内状态
try:
with Timer("思考前脑内状态", timing_results):
@@ -335,7 +332,8 @@ class ThinkFlowChat:
message_txt=message.processed_plain_text,
sender_name=message.message_info.user_info.user_nickname,
chat_stream=chat,
extra_info=collected_info
obs_id = get_mid_memory_id,
extra_info=tool_result_info
)
except Exception as e:
logger.error(f"心流思考前脑内状态失败: {e}")
@@ -371,66 +369,6 @@ class ThinkFlowChat:
logger.error(f"心流处理表情包失败: {e}")
# 思考后使用工具
try:
with Timer("思考后使用工具", timing_results):
tool_result = await self.tool_user.use_tool(message.processed_plain_text, message.message_info.user_info.user_nickname, chat)
# 如果工具被使用且获得了结果,将收集到的信息合并到思考中
collected_info = ""
if tool_result.get("used_tools", False):
# 如果有收集到的结构化信息,将其格式化后添加到当前思考中
if "structured_info" in tool_result:
info = tool_result["structured_info"]
# 处理记忆信息
if info["memory"]:
collected_info += "\n记忆相关信息:\n"
for mem in info["memory"]:
collected_info += f"- {mem['name']}: {mem['content']}\n"
# 处理日程信息
if info["schedule"]:
collected_info += "\n日程相关信息:\n"
for sch in info["schedule"]:
collected_info += f"- {sch['name']}: {sch['content']}\n"
# 处理知识信息
if info["knowledge"]:
collected_info += "\n知识相关信息:\n"
for know in info["knowledge"]:
collected_info += f"- {know['name']}: {know['content']}\n"
# 处理关系信息
if info["change_relationship"]:
collected_info += "\n关系相关信息:\n"
for rel in info["change_relationship"]:
collected_info += f"- {rel['name']}: {rel['content']}\n"
# 处理心情信息
if info["change_mood"]:
collected_info += "\n心情相关信息:\n"
for mood in info["change_mood"]:
collected_info += f"- {mood['name']}: {mood['content']}\n"
# 处理其他信息
if info["other"]:
collected_info += "\n其他相关信息:\n"
for other in info["other"]:
collected_info += f"- {other['name']}: {other['content']}\n"
except Exception as e:
logger.error(f"思考后工具调用失败: {e}")
logger.error(traceback.format_exc())
# 更新关系
if info["change_relationship"]:
ori_response = ",".join(response_set)
stance, emotion = await self.gpt._get_emotion_tags(ori_response, message.processed_plain_text,info["change_relationship"]["content"])
await relationship_manager.calculate_update_relationship_value(
chat_stream=message.chat_stream, label=emotion, stance=stance
)
try:
with Timer("思考后脑内状态更新", timing_results):
stream_id = message.chat_stream.stream_id
@@ -440,7 +378,7 @@ class ThinkFlowChat:
stream_id, limit=global_config.MAX_CONTEXT_SIZE, combine=True
)
await heartflow.get_subheartflow(stream_id).do_thinking_after_reply(response_set, chat_talking_prompt,collected_info)
await heartflow.get_subheartflow(stream_id).do_thinking_after_reply(response_set, chat_talking_prompt,tool_result_info)
except Exception as e:
logger.error(f"心流思考后脑内状态更新失败: {e}")

View File

@@ -43,7 +43,7 @@ def init_prompt():
{chat_talking_prompt}
现在"{sender_name}"说的:{message_txt}。引起了你的注意,你想要在群里发言发言或者回复这条消息。\n
你刚刚脑子里在想:{current_mind_info}
现在请你读读之前的聊天记录,然后给出日常,口语化且简短的回复内容,只给出文字的回复内容,不要有内心独白:
现在请你读读之前的聊天记录,然后给出日常,口语化且简短的回复内容,请只对一个话题进行回复,只给出文字的回复内容,不要有内心独白:
""",
"heart_flow_prompt_simple",
)