This commit is contained in:
SengokuCola
2025-04-25 23:09:10 +08:00
parent ecd3b7f673
commit 6471f5e227
9 changed files with 96 additions and 68 deletions

View File

@@ -1,8 +1,10 @@
import time
from typing import List, Optional, Dict, Any
class CycleInfo:
"""循环信息记录类"""
def __init__(self, cycle_id: int):
self.cycle_id = cycle_id
self.start_time = time.time()
@@ -13,16 +15,16 @@ class CycleInfo:
self.timers: Dict[str, float] = {}
self.thinking_id = ""
self.replanned = False
# 添加响应信息相关字段
self.response_info: Dict[str, Any] = {
"response_text": [], # 回复的文本列表
"emoji_info": "", # 表情信息
"emoji_info": "", # 表情信息
"anchor_message_id": "", # 锚点消息ID
"reply_message_ids": [], # 回复消息ID列表
"sub_mind_thinking": "", # 子思维思考内容
}
def to_dict(self) -> Dict[str, Any]:
"""将循环信息转换为字典格式"""
return {
@@ -34,29 +36,31 @@ class CycleInfo:
"reasoning": self.reasoning,
"timers": self.timers,
"thinking_id": self.thinking_id,
"response_info": self.response_info
"response_info": self.response_info,
}
def complete_cycle(self):
"""完成循环,记录结束时间"""
self.end_time = time.time()
def set_action_info(self, action_type: str, reasoning: str, action_taken: bool):
"""设置动作信息"""
self.action_type = action_type
self.reasoning = reasoning
self.action_taken = action_taken
def set_thinking_id(self, thinking_id: str):
"""设置思考消息ID"""
self.thinking_id = thinking_id
def set_response_info(self,
response_text: Optional[List[str]] = None,
emoji_info: Optional[str] = None,
anchor_message_id: Optional[str] = None,
reply_message_ids: Optional[List[str]] = None,
sub_mind_thinking: Optional[str] = None):
def set_response_info(
self,
response_text: Optional[List[str]] = None,
emoji_info: Optional[str] = None,
anchor_message_id: Optional[str] = None,
reply_message_ids: Optional[List[str]] = None,
sub_mind_thinking: Optional[str] = None,
):
"""设置响应信息"""
if response_text is not None:
self.response_info["response_text"] = response_text
@@ -67,4 +71,4 @@ class CycleInfo:
if reply_message_ids is not None:
self.response_info["reply_message_ids"] = reply_message_ids
if sub_mind_thinking is not None:
self.response_info["sub_mind_thinking"] = sub_mind_thinking
self.response_info["sub_mind_thinking"] = sub_mind_thinking

View File

@@ -147,6 +147,7 @@ class SenderError(HeartFCError):
pass
class HeartFChatting:
"""
管理一个连续的Plan-Replier-Sender循环
@@ -289,12 +290,10 @@ class HeartFChatting:
# 记录规划开始时间点
planner_start_db_time = time.time()
# 主循环:思考->决策->执行
action_taken, thinking_id = await self._think_plan_execute_loop(
cycle_timers, planner_start_db_time
)
action_taken, thinking_id = await self._think_plan_execute_loop(cycle_timers, planner_start_db_time)
# 更新循环信息
self._current_cycle.set_thinking_id(thinking_id)
self._current_cycle.timers = cycle_timers
@@ -377,16 +376,16 @@ class HeartFChatting:
# 记录子思维思考内容
if self._current_cycle:
self._current_cycle.set_response_info(sub_mind_thinking=current_mind)
# plan:决策
with Timer("决策", cycle_timers):
planner_result = await self._planner(current_mind, cycle_timers)
action = planner_result.get("action", "error")
reasoning = planner_result.get("reasoning", "未提供理由")
self._current_cycle.set_action_info(action, reasoning, False)
# 在获取规划结果后检查新消息
if await self._check_new_messages(planner_start_db_time):
if random.random() < 0.3:
@@ -407,11 +406,13 @@ class HeartFChatting:
if planner_result.get("llm_error"):
logger.error(f"{self.log_prefix} LLM失败: {reasoning}")
return False, ""
# execute:执行
with Timer("执行动作", cycle_timers):
return await self._handle_action(action, reasoning, planner_result.get("emoji_query", ""), cycle_timers, planner_start_db_time)
return await self._handle_action(
action, reasoning, planner_result.get("emoji_query", ""), cycle_timers, planner_start_db_time
)
except PlannerError as e:
logger.error(f"{self.log_prefix} 规划错误: {e}")
# 更新循环信息
@@ -505,7 +506,7 @@ class HeartFChatting:
response_set=reply,
send_emoji=emoji_query,
)
return True, thinking_id
except (ReplierError, SenderError) as e:
@@ -645,9 +646,7 @@ class HeartFChatting:
with Timer("思考", cycle_timers):
# 获取上一个循环的动作
# 传递上一个循环的信息给 do_thinking_before_reply
current_mind, _past_mind = await self.sub_mind.do_thinking_before_reply(
last_cycle=last_cycle
)
current_mind, _past_mind = await self.sub_mind.do_thinking_before_reply(last_cycle=last_cycle)
return current_mind
except Exception as e:
logger.error(f"{self.log_prefix}[SubMind] 思考失败: {e}")
@@ -854,19 +853,21 @@ class HeartFChatting:
logger.warning(f"{self.log_prefix} 已释放处理锁")
logger.info(f"{self.log_prefix} HeartFChatting关闭完成")
async def _build_replan_prompt(
self, action: str, reasoning: str
) -> str:
async def _build_replan_prompt(self, action: str, reasoning: str) -> str:
"""构建 Replanner LLM 的提示词"""
prompt = (await global_prompt_manager.get_prompt_async("replan_prompt")).format(
action=action,
reasoning=reasoning,
)
return prompt
async def _build_planner_prompt(
self, observed_messages_str: str, current_mind: Optional[str], structured_info: Dict[str, Any], replan_prompt: str
self,
observed_messages_str: str,
current_mind: Optional[str],
structured_info: Dict[str, Any],
replan_prompt: str,
) -> str:
"""构建 Planner LLM 的提示词"""

View File

@@ -88,9 +88,12 @@ def init_prompt():
"planner_prompt",
)
Prompt('''你原本打算{action},因为:{reasoning}
但是你看到了新的消息,你决定重新决定行动。''', "replan_prompt")
Prompt(
"""你原本打算{action},因为:{reasoning}
但是你看到了新的消息,你决定重新决定行动。""",
"replan_prompt",
)
Prompt("你正在qq群里聊天下面是群里在聊的内容", "chat_target_group1")
Prompt("和群里聊天", "chat_target_group2")
Prompt("你正在和{sender_name}聊天,这是你们之前聊的内容:", "chat_target_private1")