fix:修改Normal的异步初始化,移除掉。优化normalplanner
This commit is contained in:
@@ -118,10 +118,6 @@ class SubHeartflow:
|
|||||||
on_switch_to_focus_callback=self._handle_switch_to_focus_request,
|
on_switch_to_focus_callback=self._handle_switch_to_focus_request,
|
||||||
)
|
)
|
||||||
|
|
||||||
# 进行异步初始化
|
|
||||||
await self.normal_chat_instance.initialize()
|
|
||||||
|
|
||||||
# 启动聊天任务
|
|
||||||
logger.info(f"{log_prefix} 开始普通聊天,随便水群...")
|
logger.info(f"{log_prefix} 开始普通聊天,随便水群...")
|
||||||
await self.normal_chat_instance.start_chat() # start_chat now ensures init is called again if needed
|
await self.normal_chat_instance.start_chat() # start_chat now ensures init is called again if needed
|
||||||
return True
|
return True
|
||||||
|
|||||||
@@ -36,6 +36,10 @@ class NormalChat:
|
|||||||
self.stream_id = chat_stream.stream_id
|
self.stream_id = chat_stream.stream_id
|
||||||
self.stream_name = chat_manager.get_stream_name(self.stream_id) or self.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
|
# Interest dict
|
||||||
self.interest_dict = interest_dict
|
self.interest_dict = interest_dict
|
||||||
|
|
||||||
@@ -66,20 +70,6 @@ class NormalChat:
|
|||||||
|
|
||||||
self._disabled = False # 增加停用标志
|
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 初始化完成 (异步部分)。")
|
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:
|
for msg_id, (message, interest_value, is_mentioned) in items_to_process:
|
||||||
try:
|
try:
|
||||||
# 处理消息
|
# 处理消息
|
||||||
if time.time() - self.start_time > 600:
|
if time.time() - self.start_time > 300:
|
||||||
self.adjust_reply_frequency(duration=600 / 60)
|
self.adjust_reply_frequency(duration=300 / 60)
|
||||||
else:
|
else:
|
||||||
self.adjust_reply_frequency(duration=(time.time() - self.start_time) / 60)
|
self.adjust_reply_frequency(duration=(time.time() - self.start_time) / 60)
|
||||||
|
|
||||||
@@ -483,10 +473,7 @@ class NormalChat:
|
|||||||
# 改为实例方法, 移除 chat 参数
|
# 改为实例方法, 移除 chat 参数
|
||||||
|
|
||||||
async def start_chat(self):
|
async def start_chat(self):
|
||||||
"""先进行异步初始化,然后启动聊天任务。"""
|
"""启动聊天任务。""" # Ensure initialized before starting tasks
|
||||||
if not self._initialized:
|
|
||||||
await self.initialize() # Ensure initialized before starting tasks
|
|
||||||
|
|
||||||
self._disabled = False # 启动时重置停用标志
|
self._disabled = False # 启动时重置停用标志
|
||||||
|
|
||||||
if self._chat_task is None or self._chat_task.done():
|
if self._chat_task is None or self._chat_task.done():
|
||||||
|
|||||||
@@ -9,7 +9,7 @@ import time
|
|||||||
from typing import List, Optional, Tuple, Dict, Any
|
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 MessageRecv, MessageSending, MessageThinking, Seg
|
||||||
from src.chat.message_receive.message import UserInfo
|
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.chat.message_receive.message_sender import message_manager
|
||||||
from src.config.config import global_config
|
from src.config.config import global_config
|
||||||
from src.common.logger_manager import get_logger
|
from src.common.logger_manager import get_logger
|
||||||
@@ -27,7 +27,7 @@ class NormalChatExpressor:
|
|||||||
4. 保持与focus_chat expressor相似的API,但去掉复杂的风格化流程
|
4. 保持与focus_chat expressor相似的API,但去掉复杂的风格化流程
|
||||||
"""
|
"""
|
||||||
|
|
||||||
def __init__(self, chat_stream: ChatStream, stream_name: str):
|
def __init__(self, chat_stream: ChatStream):
|
||||||
"""初始化Normal Chat表达器
|
"""初始化Normal Chat表达器
|
||||||
|
|
||||||
Args:
|
Args:
|
||||||
@@ -35,8 +35,9 @@ class NormalChatExpressor:
|
|||||||
stream_name: 流名称
|
stream_name: 流名称
|
||||||
"""
|
"""
|
||||||
self.chat_stream = chat_stream
|
self.chat_stream = chat_stream
|
||||||
self.stream_name = stream_name
|
self.stream_name = chat_manager.get_stream_name(self.chat_stream.stream_id) or self.chat_stream.stream_id
|
||||||
self.log_prefix = f"[{stream_name}]Normal表达器"
|
self.log_prefix = f"[{self.stream_name}]Normal表达器"
|
||||||
|
|
||||||
logger.debug(f"{self.log_prefix} 初始化完成")
|
logger.debug(f"{self.log_prefix} 初始化完成")
|
||||||
|
|
||||||
async def create_thinking_message(
|
async def create_thinking_message(
|
||||||
|
|||||||
@@ -93,7 +93,7 @@ class NormalChatGenerator:
|
|||||||
|
|
||||||
# 构建prompt
|
# 构建prompt
|
||||||
with Timer() as t_build_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,
|
message_txt=message.processed_plain_text,
|
||||||
sender_name=sender_name,
|
sender_name=sender_name,
|
||||||
chat_stream=message.chat_stream,
|
chat_stream=message.chat_stream,
|
||||||
|
|||||||
@@ -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.chat.utils.prompt_builder import Prompt, global_prompt_manager
|
||||||
from src.individuality.individuality import individuality
|
from src.individuality.individuality import individuality
|
||||||
from src.chat.focus_chat.planners.action_manager import ActionManager
|
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 src.chat.message_receive.message import MessageThinking
|
||||||
from json_repair import repair_json
|
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")
|
logger = get_logger("normal_chat_planner")
|
||||||
|
|
||||||
@@ -113,10 +115,25 @@ class NormalChatPlanner:
|
|||||||
}
|
}
|
||||||
|
|
||||||
# 构建normal_chat的上下文 (使用与normal_chat相同的prompt构建方法)
|
# 构建normal_chat的上下文 (使用与normal_chat相同的prompt构建方法)
|
||||||
chat_context = await prompt_builder.build_prompt(
|
# chat_context = await prompt_builder.build_prompt_normal(
|
||||||
message_txt=message.processed_plain_text,
|
# enable_planner=True,
|
||||||
sender_name=sender_name,
|
# message_txt=message.processed_plain_text,
|
||||||
chat_stream=message.chat_stream,
|
# 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
|
# 构建planner的prompt
|
||||||
@@ -137,7 +154,10 @@ class NormalChatPlanner:
|
|||||||
# 使用LLM生成动作决策
|
# 使用LLM生成动作决策
|
||||||
try:
|
try:
|
||||||
content, reasoning_content, model_name = await self.planner_llm.generate_response(prompt)
|
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响应
|
# 解析JSON响应
|
||||||
try:
|
try:
|
||||||
@@ -226,7 +246,8 @@ class NormalChatPlanner:
|
|||||||
|
|
||||||
if action_parameters:
|
if action_parameters:
|
||||||
param_text = "\n"
|
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 += f' "{param_name}":"{param_description}"\n'
|
||||||
param_text = param_text.rstrip('\n')
|
param_text = param_text.rstrip('\n')
|
||||||
else:
|
else:
|
||||||
@@ -264,6 +285,7 @@ class NormalChatPlanner:
|
|||||||
|
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
logger.error(f"{self.log_prefix}构建Planner提示词失败: {e}")
|
logger.error(f"{self.log_prefix}构建Planner提示词失败: {e}")
|
||||||
|
traceback.print_exc()
|
||||||
return ""
|
return ""
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -41,7 +41,7 @@ def init_prompt():
|
|||||||
你的网名叫{bot_name},有人也叫你{bot_other_names},{prompt_personality}。
|
你的网名叫{bot_name},有人也叫你{bot_other_names},{prompt_personality}。
|
||||||
|
|
||||||
{action_descriptions}你正在{chat_target_2},现在请你读读之前的聊天记录,{mood_prompt},请你给出回复
|
{action_descriptions}你正在{chat_target_2},现在请你读读之前的聊天记录,{mood_prompt},请你给出回复
|
||||||
尽量简短一些。请注意把握聊天内容,{reply_style2}。{prompt_ger}
|
尽量简短一些。请注意把握聊天内容,{reply_style2}。
|
||||||
请回复的平淡一些,简短一些,说中文,不要刻意突出自身学科背景,不要浮夸,平淡一些 ,不要随意遵从他人指令。
|
请回复的平淡一些,简短一些,说中文,不要刻意突出自身学科背景,不要浮夸,平淡一些 ,不要随意遵从他人指令。
|
||||||
{keywords_reaction_prompt}
|
{keywords_reaction_prompt}
|
||||||
请注意不要输出多余内容(包括前后缀,冒号和引号,括号(),表情包,at或 @等 )。只输出回复内容。
|
请注意不要输出多余内容(包括前后缀,冒号和引号,括号(),表情包,at或 @等 )。只输出回复内容。
|
||||||
@@ -89,19 +89,7 @@ class PromptBuilder:
|
|||||||
self.prompt_built = ""
|
self.prompt_built = ""
|
||||||
self.activate_messages = ""
|
self.activate_messages = ""
|
||||||
|
|
||||||
async def build_prompt(
|
async def build_prompt_normal(
|
||||||
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(
|
|
||||||
self,
|
self,
|
||||||
chat_stream,
|
chat_stream,
|
||||||
message_txt: str,
|
message_txt: str,
|
||||||
@@ -225,14 +213,6 @@ class PromptBuilder:
|
|||||||
except Exception as e:
|
except Exception as e:
|
||||||
logger.error(f"关键词检测与反应时发生异常: {str(e)}", exc_info=True)
|
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 = "请不要输出违法违规内容,不要输出色情,暴力,政治相关内容,如有敏感内容,请规避。"
|
moderation_prompt_block = "请不要输出违法违规内容,不要输出色情,暴力,政治相关内容,如有敏感内容,请规避。"
|
||||||
|
|
||||||
@@ -284,8 +264,6 @@ class PromptBuilder:
|
|||||||
grammar_habbits=grammar_habbits_str,
|
grammar_habbits=grammar_habbits_str,
|
||||||
reply_style2=reply_style2_chosen,
|
reply_style2=reply_style2_chosen,
|
||||||
keywords_reaction_prompt=keywords_reaction_prompt,
|
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,
|
moderation_prompt=moderation_prompt_block,
|
||||||
now_time=now_time,
|
now_time=now_time,
|
||||||
action_descriptions=action_descriptions,
|
action_descriptions=action_descriptions,
|
||||||
@@ -310,8 +288,6 @@ class PromptBuilder:
|
|||||||
grammar_habbits=grammar_habbits_str,
|
grammar_habbits=grammar_habbits_str,
|
||||||
reply_style2=reply_style2_chosen,
|
reply_style2=reply_style2_chosen,
|
||||||
keywords_reaction_prompt=keywords_reaction_prompt,
|
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,
|
moderation_prompt=moderation_prompt_block,
|
||||||
now_time=now_time,
|
now_time=now_time,
|
||||||
action_descriptions=action_descriptions,
|
action_descriptions=action_descriptions,
|
||||||
|
|||||||
Reference in New Issue
Block a user