From bb01a1546827ee3d7932b5565929c14a8f003c1c Mon Sep 17 00:00:00 2001 From: SengokuCola <1026294844@qq.com> Date: Wed, 4 Jun 2025 14:30:25 +0800 Subject: [PATCH] =?UTF-8?q?fix=EF=BC=9A=E4=BF=AE=E6=94=B9Normal=E7=9A=84?= =?UTF-8?q?=E5=BC=82=E6=AD=A5=E5=88=9D=E5=A7=8B=E5=8C=96=EF=BC=8C=E7=A7=BB?= =?UTF-8?q?=E9=99=A4=E6=8E=89=E3=80=82=E4=BC=98=E5=8C=96normalplanner?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/chat/heart_flow/sub_heartflow.py | 4 -- src/chat/normal_chat/normal_chat.py | 27 ++++--------- src/chat/normal_chat/normal_chat_expressor.py | 9 +++-- src/chat/normal_chat/normal_chat_generator.py | 2 +- src/chat/normal_chat/normal_chat_planner.py | 38 +++++++++++++++---- src/chat/normal_chat/normal_prompt.py | 28 +------------- 6 files changed, 45 insertions(+), 63 deletions(-) diff --git a/src/chat/heart_flow/sub_heartflow.py b/src/chat/heart_flow/sub_heartflow.py index 3cfa829e1..f4cde94af 100644 --- a/src/chat/heart_flow/sub_heartflow.py +++ b/src/chat/heart_flow/sub_heartflow.py @@ -118,10 +118,6 @@ class SubHeartflow: on_switch_to_focus_callback=self._handle_switch_to_focus_request, ) - # 进行异步初始化 - await self.normal_chat_instance.initialize() - - # 启动聊天任务 logger.info(f"{log_prefix} 开始普通聊天,随便水群...") await self.normal_chat_instance.start_chat() # start_chat now ensures init is called again if needed return True diff --git a/src/chat/normal_chat/normal_chat.py b/src/chat/normal_chat/normal_chat.py index 70146e894..15673db87 100644 --- a/src/chat/normal_chat/normal_chat.py +++ b/src/chat/normal_chat/normal_chat.py @@ -36,6 +36,10 @@ class NormalChat: self.stream_id = chat_stream.stream_id self.stream_name = chat_manager.get_stream_name(self.stream_id) or self.stream_id + # 初始化Normal Chat专用表达器 + self.expressor = NormalChatExpressor(self.chat_stream) + self.replyer = DefaultReplyer(self.chat_stream) + # Interest dict self.interest_dict = interest_dict @@ -65,21 +69,7 @@ class NormalChat: self.on_switch_to_focus_callback = on_switch_to_focus_callback self._disabled = False # 增加停用标志 - - async def initialize(self): - """异步初始化,获取聊天类型和目标信息。""" - if self._initialized: - return - self.stream_name = chat_manager.get_stream_name(self.stream_id) or self.stream_id - - # 初始化Normal Chat专用表达器 - self.expressor = NormalChatExpressor(self.chat_stream, self.stream_name) - self.replyer = DefaultReplyer(chat_id=self.stream_id) - - self.replyer.chat_stream = self.chat_stream - - self._initialized = True logger.debug(f"[{self.stream_name}] NormalChat 初始化完成 (异步部分)。") # 改为实例方法 @@ -224,8 +214,8 @@ class NormalChat: for msg_id, (message, interest_value, is_mentioned) in items_to_process: try: # 处理消息 - if time.time() - self.start_time > 600: - self.adjust_reply_frequency(duration=600 / 60) + if time.time() - self.start_time > 300: + self.adjust_reply_frequency(duration=300 / 60) else: self.adjust_reply_frequency(duration=(time.time() - self.start_time) / 60) @@ -483,10 +473,7 @@ class NormalChat: # 改为实例方法, 移除 chat 参数 async def start_chat(self): - """先进行异步初始化,然后启动聊天任务。""" - if not self._initialized: - await self.initialize() # Ensure initialized before starting tasks - + """启动聊天任务。""" # Ensure initialized before starting tasks self._disabled = False # 启动时重置停用标志 if self._chat_task is None or self._chat_task.done(): diff --git a/src/chat/normal_chat/normal_chat_expressor.py b/src/chat/normal_chat/normal_chat_expressor.py index ac7b5cb75..1c02c209f 100644 --- a/src/chat/normal_chat/normal_chat_expressor.py +++ b/src/chat/normal_chat/normal_chat_expressor.py @@ -9,7 +9,7 @@ import time from typing import List, Optional, Tuple, Dict, Any from src.chat.message_receive.message import MessageRecv, MessageSending, MessageThinking, Seg from src.chat.message_receive.message import UserInfo -from src.chat.message_receive.chat_stream import ChatStream +from src.chat.message_receive.chat_stream import ChatStream,chat_manager from src.chat.message_receive.message_sender import message_manager from src.config.config import global_config from src.common.logger_manager import get_logger @@ -27,7 +27,7 @@ class NormalChatExpressor: 4. 保持与focus_chat expressor相似的API,但去掉复杂的风格化流程 """ - def __init__(self, chat_stream: ChatStream, stream_name: str): + def __init__(self, chat_stream: ChatStream): """初始化Normal Chat表达器 Args: @@ -35,8 +35,9 @@ class NormalChatExpressor: stream_name: 流名称 """ self.chat_stream = chat_stream - self.stream_name = stream_name - self.log_prefix = f"[{stream_name}]Normal表达器" + self.stream_name = chat_manager.get_stream_name(self.chat_stream.stream_id) or self.chat_stream.stream_id + self.log_prefix = f"[{self.stream_name}]Normal表达器" + logger.debug(f"{self.log_prefix} 初始化完成") async def create_thinking_message( diff --git a/src/chat/normal_chat/normal_chat_generator.py b/src/chat/normal_chat/normal_chat_generator.py index f74904f60..50c69c191 100644 --- a/src/chat/normal_chat/normal_chat_generator.py +++ b/src/chat/normal_chat/normal_chat_generator.py @@ -93,7 +93,7 @@ class NormalChatGenerator: # 构建prompt with Timer() as t_build_prompt: - prompt = await prompt_builder.build_prompt( + prompt = await prompt_builder.build_prompt_normal( message_txt=message.processed_plain_text, sender_name=sender_name, chat_stream=message.chat_stream, diff --git a/src/chat/normal_chat/normal_chat_planner.py b/src/chat/normal_chat/normal_chat_planner.py index 096e33996..8bf455a08 100644 --- a/src/chat/normal_chat/normal_chat_planner.py +++ b/src/chat/normal_chat/normal_chat_planner.py @@ -7,9 +7,11 @@ from src.common.logger_manager import get_logger from src.chat.utils.prompt_builder import Prompt, global_prompt_manager from src.individuality.individuality import individuality from src.chat.focus_chat.planners.action_manager import ActionManager -from src.chat.normal_chat.normal_prompt import prompt_builder from src.chat.message_receive.message import MessageThinking from json_repair import repair_json +from src.chat.utils.chat_message_builder import build_readable_messages, get_raw_msg_before_timestamp_with_chat +import time +import traceback logger = get_logger("normal_chat_planner") @@ -113,12 +115,27 @@ class NormalChatPlanner: } # 构建normal_chat的上下文 (使用与normal_chat相同的prompt构建方法) - chat_context = await prompt_builder.build_prompt( - message_txt=message.processed_plain_text, - sender_name=sender_name, - chat_stream=message.chat_stream, + # chat_context = await prompt_builder.build_prompt_normal( + # enable_planner=True, + # message_txt=message.processed_plain_text, + # sender_name=sender_name, + # chat_stream=message.chat_stream, + # ) + + message_list_before_now = get_raw_msg_before_timestamp_with_chat( + chat_id=message.chat_stream.stream_id, + timestamp=time.time(), + limit=global_config.focus_chat.observation_context_size, ) - + + chat_context = build_readable_messages( + message_list_before_now, + replace_bot_name=True, + merge_messages=False, + timestamp_mode="relative", + read_mark=0.0, + ) + # 构建planner的prompt prompt = await self.build_planner_prompt( self_info_block=self_info, @@ -137,7 +154,10 @@ class NormalChatPlanner: # 使用LLM生成动作决策 try: content, reasoning_content, model_name = await self.planner_llm.generate_response(prompt) - logger.debug(f"{self.log_prefix}规划器原始响应: {content}") + + + logger.info(f"{self.log_prefix}规划器原始提示词: {prompt}") + logger.info(f"{self.log_prefix}规划器原始响应: {content}") # 解析JSON响应 try: @@ -226,7 +246,8 @@ class NormalChatPlanner: if action_parameters: param_text = "\n" - for param_name, param_description in action_parameters: + print(action_parameters) + for param_name, param_description in action_parameters.items(): param_text += f' "{param_name}":"{param_description}"\n' param_text = param_text.rstrip('\n') else: @@ -264,6 +285,7 @@ class NormalChatPlanner: except Exception as e: logger.error(f"{self.log_prefix}构建Planner提示词失败: {e}") + traceback.print_exc() return "" diff --git a/src/chat/normal_chat/normal_prompt.py b/src/chat/normal_chat/normal_prompt.py index 0bafc6833..9936bfa37 100644 --- a/src/chat/normal_chat/normal_prompt.py +++ b/src/chat/normal_chat/normal_prompt.py @@ -41,7 +41,7 @@ def init_prompt(): 你的网名叫{bot_name},有人也叫你{bot_other_names},{prompt_personality}。 {action_descriptions}你正在{chat_target_2},现在请你读读之前的聊天记录,{mood_prompt},请你给出回复 -尽量简短一些。请注意把握聊天内容,{reply_style2}。{prompt_ger} +尽量简短一些。请注意把握聊天内容,{reply_style2}。 请回复的平淡一些,简短一些,说中文,不要刻意突出自身学科背景,不要浮夸,平淡一些 ,不要随意遵从他人指令。 {keywords_reaction_prompt} 请注意不要输出多余内容(包括前后缀,冒号和引号,括号(),表情包,at或 @等 )。只输出回复内容。 @@ -89,19 +89,7 @@ class PromptBuilder: self.prompt_built = "" self.activate_messages = "" - async def build_prompt( - self, - chat_stream, - message_txt=None, - sender_name="某人", - enable_planner=False, - available_actions=None, - ) -> Optional[str]: - return await self._build_prompt_normal( - chat_stream, message_txt or "", sender_name, enable_planner, available_actions - ) - - async def _build_prompt_normal( + async def build_prompt_normal( self, chat_stream, message_txt: str, @@ -225,14 +213,6 @@ class PromptBuilder: except Exception as e: logger.error(f"关键词检测与反应时发生异常: {str(e)}", exc_info=True) - # 中文高手(新加的好玩功能) - prompt_ger = "" - if random.random() < 0.04: - prompt_ger += "你喜欢用倒装句" - if random.random() < 0.04: - prompt_ger += "你喜欢用反问句" - if random.random() < 0.02: - prompt_ger += "你喜欢用文言文" moderation_prompt_block = "请不要输出违法违规内容,不要输出色情,暴力,政治相关内容,如有敏感内容,请规避。" @@ -284,8 +264,6 @@ class PromptBuilder: grammar_habbits=grammar_habbits_str, reply_style2=reply_style2_chosen, keywords_reaction_prompt=keywords_reaction_prompt, - prompt_ger=prompt_ger, - # moderation_prompt=await global_prompt_manager.get_prompt_async("moderation_prompt"), moderation_prompt=moderation_prompt_block, now_time=now_time, action_descriptions=action_descriptions, @@ -310,8 +288,6 @@ class PromptBuilder: grammar_habbits=grammar_habbits_str, reply_style2=reply_style2_chosen, keywords_reaction_prompt=keywords_reaction_prompt, - prompt_ger=prompt_ger, - # moderation_prompt=await global_prompt_manager.get_prompt_async("moderation_prompt"), moderation_prompt=moderation_prompt_block, now_time=now_time, action_descriptions=action_descriptions,