better:给心流添加了自动停止和销毁,节省资源

This commit is contained in:
SengokuCola
2025-03-29 23:56:14 +08:00
parent b8828e81c6
commit 1641d89ca2
4 changed files with 52 additions and 45 deletions

View File

@@ -6,6 +6,7 @@ from src.plugins.config.config import global_config
from src.plugins.schedule.schedule_generator import bot_schedule
import asyncio
from src.common.logger import get_module_logger, LogConfig, HEARTFLOW_STYLE_CONFIG # noqa: E402
import time
heartflow_config = LogConfig(
# 使用海马体专用样式
@@ -37,12 +38,39 @@ class Heartflow:
self.active_subheartflows_nums = 0
self.personality_info = " ".join(global_config.PROMPT_PERSONALITY)
async def _cleanup_inactive_subheartflows(self):
"""定期清理不活跃的子心流"""
while True:
current_time = time.time()
inactive_subheartflows = []
# 检查所有子心流
for subheartflow_id, subheartflow in self._subheartflows.items():
if current_time - subheartflow.last_active_time > 600: # 10分钟 = 600秒
inactive_subheartflows.append(subheartflow_id)
logger.info(f"发现不活跃的子心流: {subheartflow_id}")
# 清理不活跃的子心流
for subheartflow_id in inactive_subheartflows:
del self._subheartflows[subheartflow_id]
logger.info(f"已清理不活跃的子心流: {subheartflow_id}")
await asyncio.sleep(60) # 每分钟检查一次
async def heartflow_start_working(self):
# 启动清理任务
asyncio.create_task(self._cleanup_inactive_subheartflows())
while True:
# 检查是否存在子心流
if not self._subheartflows:
logger.debug("当前没有子心流,等待新的子心流创建...")
await asyncio.sleep(60) # 每分钟检查一次是否有新的子心流
continue
await self.do_a_thinking()
await asyncio.sleep(600)
await asyncio.sleep(300) # 5分钟思考一次
async def do_a_thinking(self):
logger.info("麦麦大脑袋转起来了")
@@ -72,7 +100,7 @@ class Heartflow:
self.current_mind = reponse
logger.info(f"麦麦的总体脑内状态:{self.current_mind}")
logger.info("麦麦想了想,当前活动:")
# logger.info("麦麦想了想,当前活动:")
await bot_schedule.move_doing(self.current_mind)

View File

@@ -42,6 +42,7 @@ class SubHeartflow:
self.main_heartflow_info = ""
self.last_reply_time = time.time()
self.last_active_time = time.time() # 添加最后激活时间
if not self.current_mind:
self.current_mind = "你什么也没想"
@@ -79,11 +80,11 @@ class SubHeartflow:
while True:
current_time = time.time()
if current_time - self.last_reply_time > 180: # 3分钟 = 180秒
# print(f"{self.observe_chat_id}麦麦已经3分钟没有回复了暂时停止思考")
self.is_active = False
await asyncio.sleep(60) # 每30秒检查一次
await asyncio.sleep(60) # 每60秒检查一次
else:
self.is_active = True
self.last_active_time = current_time # 更新最后激活时间
observation = self.observations[0]
observation.observe()
@@ -93,6 +94,11 @@ class SubHeartflow:
await self.do_a_thinking()
await self.judge_willing()
await asyncio.sleep(60)
# 检查是否超过10分钟没有激活
if current_time - self.last_active_time > 600: # 10分钟 = 600秒
logger.info(f"子心流 {self.subheartflow_id} 已经10分钟没有激活正在销毁...")
break # 退出循环,销毁自己
async def do_a_thinking(self):
@@ -132,7 +138,7 @@ class SubHeartflow:
prompt += f"刚刚你的想法是{current_thinking_info}\n"
prompt += "-----------------------------------\n"
prompt += f"现在你正在上网和qq群里的网友们聊天群里正在聊的话题是{chat_observe_info}\n"
prompt += f"你现在{mood_info}\n"
prompt += f"你现在{mood_info}\n"
prompt += "现在你接下去继续思考,产生新的想法,不要分点输出,输出连贯的内心独白,不要太长,"
prompt += "但是记得结合上述的消息,要记得维持住你的人设,关注聊天和新内容,不要思考太多:"
reponse, reasoning_content = await self.llm_model.generate_response_async(prompt)
@@ -145,7 +151,6 @@ class SubHeartflow:
async def do_after_reply(self,reply_content,chat_talking_prompt):
# print("麦麦脑袋转起来了")
current_thinking_info = self.current_mind
mood_info = self.current_state.mood
@@ -155,18 +160,15 @@ class SubHeartflow:
message_new_info = chat_talking_prompt
reply_info = reply_content
schedule_info = bot_schedule.get_current_num_task(num = 1,time_info = False)
prompt = ""
prompt += f"刚刚在做的事情是:{schedule_info}\n"
prompt += f"现在正在做的事情是:{schedule_info}\n"
prompt += f"{self.personality_info}\n"
prompt += f"现在你正在上网和qq群里的网友们聊天群里正在聊的话题是{chat_observe_info}\n"
# if related_memory_info:
# prompt += f"你想起来{related_memory_info}。"
prompt += f"刚刚你的想法是{current_thinking_info}"
prompt += f"你现在看到了网友们发的新消息:{message_new_info}\n"
prompt += f"你刚刚回复了群友们:{reply_info}"
prompt += f"你现在{mood_info}"
prompt += f"你现在{mood_info}"
prompt += "现在你接下去继续思考,产生新的想法,记得保留你刚刚的想法,不要分点输出,输出连贯的内心独白"
prompt += "不要太长,但是记得结合上述的消息,要记得你的人设,关注聊天和新内容,关注你回复的内容,不要思考太多:"