better:给心流添加了自动停止和销毁,节省资源
This commit is contained in:
@@ -222,10 +222,7 @@ class ChatBot:
|
||||
stream_id, limit=global_config.MAX_CONTEXT_SIZE, combine=True
|
||||
)
|
||||
|
||||
if heartflow.get_subheartflow(stream_id):
|
||||
await heartflow.get_subheartflow(stream_id).do_after_reply(response_set, chat_talking_prompt)
|
||||
else:
|
||||
await heartflow.create_subheartflow(stream_id).do_after_reply(response_set, chat_talking_prompt)
|
||||
heartflow.get_subheartflow(stream_id).do_after_reply(response_set, chat_talking_prompt)
|
||||
|
||||
|
||||
async def _send_response_messages(self, message, chat, response_set, thinking_id):
|
||||
|
||||
@@ -28,10 +28,10 @@ class ScheduleGenerator:
|
||||
def __init__(self):
|
||||
# 使用离线LLM模型
|
||||
self.llm_scheduler_all = LLM_request(
|
||||
model=global_config.llm_reasoning, temperature=0.9, max_tokens=7000, request_type="schedule"
|
||||
model=global_config.llm_reasoning, temperature=0.8, max_tokens=7000, request_type="schedule"
|
||||
)
|
||||
self.llm_scheduler_doing = LLM_request(
|
||||
model=global_config.llm_normal, temperature=0.9, max_tokens=2048, request_type="schedule"
|
||||
model=global_config.llm_normal, temperature=0.6, max_tokens=2048, request_type="schedule"
|
||||
)
|
||||
|
||||
self.today_schedule_text = ""
|
||||
@@ -124,26 +124,23 @@ class ScheduleGenerator:
|
||||
|
||||
prompt = f"你是{self.name},{self.personality},{self.behavior}"
|
||||
prompt += f"你昨天的日程是:{self.yesterday_schedule_text}\n"
|
||||
prompt += f"请为你生成{date_str}({weekday})的日程安排,结合你的个人特点和行为习惯\n"
|
||||
prompt += f"请为你生成{date_str}({weekday}),也就是今天的日程安排,结合你的个人特点和行为习惯以及昨天的安排\n"
|
||||
prompt += "推测你的日程安排,包括你一天都在做什么,从起床到睡眠,有什么发现和思考,具体一些,详细一些,需要1500字以上,精确到每半个小时,记得写明时间\n" # noqa: E501
|
||||
prompt += "直接返回你的日程,从起床到睡觉,不要输出其他内容:"
|
||||
return prompt
|
||||
|
||||
def construct_doing_prompt(self, time: datetime.datetime, mind_thinking: str = ""):
|
||||
now_time = time.strftime("%H:%M")
|
||||
if self.today_done_list:
|
||||
previous_doings = self.get_current_num_task(5, True)
|
||||
# print(previous_doings)
|
||||
else:
|
||||
previous_doings = "你没做什么事情"
|
||||
previous_doings = self.get_current_num_task(5, True)
|
||||
|
||||
prompt = f"你是{self.name},{self.personality},{self.behavior}"
|
||||
prompt += f"你今天的日程是:{self.today_schedule_text}\n"
|
||||
prompt += f"你之前做了的事情是:{previous_doings},从之前到现在已经过去了{self.schedule_doing_update_interval / 60}分钟了\n" # noqa: E501
|
||||
if previous_doings:
|
||||
prompt += f"你之前做了的事情是:{previous_doings},从之前到现在已经过去了{self.schedule_doing_update_interval / 60}分钟了\n" # noqa: E501
|
||||
if mind_thinking:
|
||||
prompt += f"你脑子里在想:{mind_thinking}\n"
|
||||
prompt += f"现在是{now_time},结合你的个人特点和行为习惯,注意关注你今天的日程安排和想法,这很重要,"
|
||||
prompt += "推测你现在在做什么,具体一些,详细一些\n"
|
||||
prompt += f"现在是{now_time},结合你的个人特点和行为习惯,注意关注你今天的日程安排和想法安排你接下来做什么,"
|
||||
prompt += "安排你接下来做什么,具体一些,详细一些\n"
|
||||
prompt += "直接返回你在做的事情,注意是当前时间,不要输出其他内容:"
|
||||
return prompt
|
||||
|
||||
@@ -155,23 +152,6 @@ class ScheduleGenerator:
|
||||
daytime_response, _ = await self.llm_scheduler_all.generate_response_async(daytime_prompt)
|
||||
return daytime_response
|
||||
|
||||
def _time_diff(self, time1: str, time2: str) -> int:
|
||||
"""计算两个时间字符串之间的分钟差"""
|
||||
if time1 == "24:00":
|
||||
time1 = "23:59"
|
||||
if time2 == "24:00":
|
||||
time2 = "23:59"
|
||||
t1 = datetime.datetime.strptime(time1, "%H:%M")
|
||||
t2 = datetime.datetime.strptime(time2, "%H:%M")
|
||||
diff = int((t2 - t1).total_seconds() / 60)
|
||||
# 考虑时间的循环性
|
||||
if diff < -720:
|
||||
diff += 1440 # 加一天的分钟
|
||||
elif diff > 720:
|
||||
diff -= 1440 # 减一天的分钟
|
||||
# print(f"时间1[{time1}]: 时间2[{time2}],差值[{diff}]分钟")
|
||||
return diff
|
||||
|
||||
def print_schedule(self):
|
||||
"""打印完整的日程安排"""
|
||||
if not self.today_schedule_text:
|
||||
|
||||
@@ -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(
|
||||
# 使用海马体专用样式
|
||||
@@ -38,11 +39,38 @@ class Heartflow:
|
||||
|
||||
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)
|
||||
|
||||
|
||||
|
||||
@@ -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()
|
||||
@@ -94,6 +95,11 @@ class SubHeartflow:
|
||||
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):
|
||||
|
||||
current_thinking_info = self.current_mind
|
||||
@@ -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
|
||||
|
||||
@@ -156,17 +161,14 @@ class SubHeartflow:
|
||||
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 += "不要太长,但是记得结合上述的消息,要记得你的人设,关注聊天和新内容,关注你回复的内容,不要思考太多:"
|
||||
|
||||
|
||||
Reference in New Issue
Block a user