From 1992b680be932936ab10dee54611c812d9efba71 Mon Sep 17 00:00:00 2001 From: SengokuCola <1026294844@qq.com> Date: Sun, 29 Jun 2025 22:20:59 +0800 Subject: [PATCH 01/12] =?UTF-8?q?fix=EF=BC=9A=E4=BF=AE=E5=A4=8D=E8=A1=A8?= =?UTF-8?q?=E6=83=85=E5=8C=85=E6=A6=82=E7=8E=87=E8=AE=BE=E7=BD=AE=E5=A4=B1?= =?UTF-8?q?=E6=95=88?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/plugins/built_in/core_actions/plugin.py | 9 ++------- 1 file changed, 2 insertions(+), 7 deletions(-) diff --git a/src/plugins/built_in/core_actions/plugin.py b/src/plugins/built_in/core_actions/plugin.py index dcd4ce5cf..98c668d5c 100644 --- a/src/plugins/built_in/core_actions/plugin.py +++ b/src/plugins/built_in/core_actions/plugin.py @@ -12,6 +12,7 @@ from typing import List, Tuple, Type # 导入新插件系统 from src.plugin_system import BasePlugin, register_plugin, BaseAction, ComponentInfo, ActionActivationType, ChatMode from src.plugin_system.base.config_types import ConfigField +from src.config.config import global_config # 导入依赖的系统组件 from src.common.logger import get_logger @@ -197,7 +198,6 @@ class CoreActionsPlugin(BasePlugin): "plugin": "插件启用配置", "components": "核心组件启用配置", "no_reply": "不回复动作配置(智能等待机制)", - "emoji": "表情动作配置", } # 配置Schema定义 @@ -231,18 +231,13 @@ class CoreActionsPlugin(BasePlugin): type=int, default=600, description="回复频率检查窗口时间(秒)", example=600 ), }, - "emoji": { - "random_probability": ConfigField( - type=float, default=0.1, description="Normal模式下,随机发送表情的概率(0.0到1.0)", example=0.15 - ) - }, } def get_plugin_components(self) -> List[Tuple[ComponentInfo, Type]]: """返回插件包含的组件列表""" # --- 从配置动态设置Action/Command --- - emoji_chance = self.get_config("emoji.random_probability", 0.1) + emoji_chance = global_config.normal_chat.emoji_chance EmojiAction.random_activation_probability = emoji_chance no_reply_probability = self.get_config("no_reply.random_probability", 0.8) From a1a81194f12a42675b5a888511c3854d821633ca Mon Sep 17 00:00:00 2001 From: SengokuCola <1026294844@qq.com> Date: Tue, 1 Jul 2025 12:27:14 +0800 Subject: [PATCH 02/12] =?UTF-8?q?feat=EF=BC=9A=E5=90=88=E5=B9=B6normal?= =?UTF-8?q?=E5=92=8Cfocus=E7=9A=84prompt=E6=9E=84=E5=BB=BA?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- changelogs/changelog.md | 8 + src/chat/focus_chat/heartFC_chat.py | 69 +--- .../expression_selector_processor.py | 107 ----- src/chat/focus_chat/memory_activator.py | 23 +- src/chat/normal_chat/normal_chat.py | 2 - src/chat/normal_chat/normal_chat_generator.py | 110 ++---- src/chat/normal_chat/normal_prompt.py | 372 ------------------ src/chat/replyer/default_generator.py | 311 +++++++++++---- src/chat/replyer/replyer_manager.py | 58 +++ src/plugin_system/apis/generator_api.py | 120 ++++-- src/plugins/built_in/core_actions/plugin.py | 1 + template/bot_config_template.toml | 2 +- 12 files changed, 444 insertions(+), 739 deletions(-) delete mode 100644 src/chat/focus_chat/info_processors/expression_selector_processor.py delete mode 100644 src/chat/normal_chat/normal_prompt.py create mode 100644 src/chat/replyer/replyer_manager.py diff --git a/changelogs/changelog.md b/changelogs/changelog.md index 2c81f150e..92d59d18c 100644 --- a/changelogs/changelog.md +++ b/changelogs/changelog.md @@ -1,5 +1,13 @@ # Changelog +## [0.8.1] - 2025-6-27 + +- 修复表情包配置无效问题 +- 合并normal和focus的prompt构建 + + + + ## [0.8.0] - 2025-6-27 MaiBot 0.8.0 现已推出! diff --git a/src/chat/focus_chat/heartFC_chat.py b/src/chat/focus_chat/heartFC_chat.py index ba1222650..de8eafb85 100644 --- a/src/chat/focus_chat/heartFC_chat.py +++ b/src/chat/focus_chat/heartFC_chat.py @@ -23,7 +23,6 @@ from src.chat.heart_flow.observation.actions_observation import ActionObservatio from src.chat.focus_chat.info_processors.tool_processor import ToolProcessor from src.chat.focus_chat.memory_activator import MemoryActivator from src.chat.focus_chat.info_processors.base_processor import BaseProcessor -from src.chat.focus_chat.info_processors.expression_selector_processor import ExpressionSelectorProcessor from src.chat.focus_chat.planners.planner_factory import PlannerFactory from src.chat.focus_chat.planners.modify_actions import ActionModifier from src.chat.focus_chat.planners.action_manager import ActionManager @@ -31,7 +30,6 @@ from src.config.config import global_config from src.chat.focus_chat.hfc_performance_logger import HFCPerformanceLogger from src.chat.focus_chat.hfc_version_manager import get_hfc_version from src.chat.focus_chat.info.relation_info import RelationInfo -from src.chat.focus_chat.info.expression_selection_info import ExpressionSelectionInfo from src.chat.focus_chat.info.structured_info import StructuredInfo @@ -59,7 +57,6 @@ PROCESSOR_CLASSES = { POST_PLANNING_PROCESSOR_CLASSES = { "ToolProcessor": (ToolProcessor, "tool_use_processor"), "PersonImpressionpProcessor": (PersonImpressionpProcessor, "person_impression_processor"), - "ExpressionSelectorProcessor": (ExpressionSelectorProcessor, "expression_selector_processor"), } logger = get_logger("hfc") # Logger Name Changed @@ -699,30 +696,6 @@ class HeartFChatting: task_start_times[task] = time.time() logger.info(f"{self.log_prefix} 启动后期处理器任务: {processor_name}") - # 添加记忆激活器任务 - async def run_memory_with_timeout_and_timing(): - start_time = time.time() - try: - result = await asyncio.wait_for( - self.memory_activator.activate_memory(observations), - timeout=MEMORY_ACTIVATION_TIMEOUT, - ) - end_time = time.time() - post_processor_time_costs["MemoryActivator"] = end_time - start_time - logger.debug(f"{self.log_prefix} 记忆激活器耗时: {end_time - start_time:.3f}秒") - return result - except Exception as e: - end_time = time.time() - post_processor_time_costs["MemoryActivator"] = end_time - start_time - logger.warning(f"{self.log_prefix} 记忆激活器执行异常,耗时: {end_time - start_time:.3f}秒") - raise e - - memory_task = asyncio.create_task(run_memory_with_timeout_and_timing()) - task_list.append(memory_task) - task_to_name_map[memory_task] = ("memory", "MemoryActivator") - task_start_times[memory_task] = time.time() - logger.info(f"{self.log_prefix} 启动记忆激活器任务") - # 如果没有任何后期任务,直接返回 if not task_list: logger.info(f"{self.log_prefix} 没有启用的后期处理器或记忆激活器") @@ -731,7 +704,6 @@ class HeartFChatting: # 等待所有任务完成 pending_tasks = set(task_list) all_post_plan_info = [] - running_memorys = [] while pending_tasks: done, pending_tasks = await asyncio.wait(pending_tasks, return_when=asyncio.FIRST_COMPLETED) @@ -748,13 +720,6 @@ class HeartFChatting: all_post_plan_info.extend(result) else: logger.warning(f"{self.log_prefix} 后期处理器 {task_name} 返回了 None") - elif task_type == "memory": - logger.info(f"{self.log_prefix} 记忆激活器已完成!") - if result is not None: - running_memorys = result - else: - logger.warning(f"{self.log_prefix} 记忆激活器返回了 None") - running_memorys = [] except asyncio.TimeoutError: # 对于超时任务,记录已用时间 @@ -764,12 +729,6 @@ class HeartFChatting: logger.warning( f"{self.log_prefix} 后期处理器 {task_name} 超时(>{global_config.focus_chat.processor_max_time}s),已跳过,耗时: {elapsed_time:.3f}秒" ) - elif task_type == "memory": - post_processor_time_costs["MemoryActivator"] = elapsed_time - logger.warning( - f"{self.log_prefix} 记忆激活器超时(>{MEMORY_ACTIVATION_TIMEOUT}s),已跳过,耗时: {elapsed_time:.3f}秒" - ) - running_memorys = [] except Exception as e: # 对于异常任务,记录已用时间 elapsed_time = time.time() - task_start_times[task] @@ -779,49 +738,29 @@ class HeartFChatting: f"{self.log_prefix} 后期处理器 {task_name} 执行失败,耗时: {elapsed_time:.3f}秒. 错误: {e}", exc_info=True, ) - elif task_type == "memory": - post_processor_time_costs["MemoryActivator"] = elapsed_time - logger.error( - f"{self.log_prefix} 记忆激活器执行失败,耗时: {elapsed_time:.3f}秒. 错误: {e}", - exc_info=True, - ) - running_memorys = [] # 将后期处理器的结果整合到 action_data 中 updated_action_data = action_data.copy() relation_info = "" - selected_expressions = [] structured_info = "" for info in all_post_plan_info: if isinstance(info, RelationInfo): relation_info = info.get_processed_info() - elif isinstance(info, ExpressionSelectionInfo): - selected_expressions = info.get_expressions_for_action_data() elif isinstance(info, StructuredInfo): structured_info = info.get_processed_info() if relation_info: - updated_action_data["relation_info_block"] = relation_info + updated_action_data["relation_info"] = relation_info - if selected_expressions: - updated_action_data["selected_expressions"] = selected_expressions if structured_info: updated_action_data["structured_info"] = structured_info - # 特殊处理running_memorys - if running_memorys: - memory_str = "以下是当前在聊天中,你回忆起的记忆:\n" - for running_memory in running_memorys: - memory_str += f"{running_memory['content']}\n" - updated_action_data["memory_block"] = memory_str - logger.info(f"{self.log_prefix} 添加了 {len(running_memorys)} 个激活的记忆到action_data") - - if all_post_plan_info or running_memorys: + if all_post_plan_info: logger.info( - f"{self.log_prefix} 后期处理完成,产生了 {len(all_post_plan_info)} 个信息项和 {len(running_memorys)} 个记忆" + f"{self.log_prefix} 后期处理完成,产生了 {len(all_post_plan_info)} 个信息项" ) # 输出详细统计信息 @@ -908,7 +847,7 @@ class HeartFChatting: logger.debug(f"{self.log_prefix} 并行阶段完成,准备进入规划器,plan_info数量: {len(all_plan_info)}") with Timer("规划器", cycle_timers): - plan_result = await self.action_planner.plan(all_plan_info, [], loop_start_time) + plan_result = await self.action_planner.plan(all_plan_info, self.observations, loop_start_time) loop_plan_info = { "action_result": plan_result.get("action_result", {}), diff --git a/src/chat/focus_chat/info_processors/expression_selector_processor.py b/src/chat/focus_chat/info_processors/expression_selector_processor.py deleted file mode 100644 index 66b199718..000000000 --- a/src/chat/focus_chat/info_processors/expression_selector_processor.py +++ /dev/null @@ -1,107 +0,0 @@ -import time -import random -from typing import List -from src.chat.heart_flow.observation.chatting_observation import ChattingObservation -from src.chat.heart_flow.observation.observation import Observation -from src.common.logger import get_logger -from src.chat.message_receive.chat_stream import get_chat_manager -from .base_processor import BaseProcessor -from src.chat.focus_chat.info.info_base import InfoBase -from src.chat.focus_chat.info.expression_selection_info import ExpressionSelectionInfo -from src.chat.express.expression_selector import expression_selector - -logger = get_logger("processor") - - -class ExpressionSelectorProcessor(BaseProcessor): - log_prefix = "表达选择器" - - def __init__(self, subheartflow_id: str): - super().__init__() - - self.subheartflow_id = subheartflow_id - self.last_selection_time = 0 - self.selection_interval = 10 # 40秒间隔 - self.cached_expressions = [] # 缓存上一次选择的表达方式 - - name = get_chat_manager().get_stream_name(self.subheartflow_id) - self.log_prefix = f"[{name}] 表达选择器" - - async def process_info( - self, - observations: List[Observation] = None, - action_type: str = None, - action_data: dict = None, - **kwargs, - ) -> List[InfoBase]: - """处理信息对象 - - Args: - observations: 观察对象列表 - - Returns: - List[InfoBase]: 处理后的表达选择信息列表 - """ - current_time = time.time() - - # 检查频率限制 - if current_time - self.last_selection_time < self.selection_interval: - logger.debug(f"{self.log_prefix} 距离上次选择不足{self.selection_interval}秒,使用缓存的表达方式") - # 使用缓存的表达方式 - if self.cached_expressions: - # 从缓存的15个中随机选5个 - final_expressions = random.sample(self.cached_expressions, min(5, len(self.cached_expressions))) - - # 创建表达选择信息 - expression_info = ExpressionSelectionInfo() - expression_info.set_selected_expressions(final_expressions) - - logger.info(f"{self.log_prefix} 使用缓存选择了{len(final_expressions)}个表达方式") - return [expression_info] - else: - logger.debug(f"{self.log_prefix} 没有缓存的表达方式,跳过选择") - return [] - - # 获取聊天内容 - chat_info = "" - if observations: - for observation in observations: - if isinstance(observation, ChattingObservation): - # chat_info = observation.get_observe_info() - chat_info = observation.talking_message_str_truncate_short - break - - if not chat_info: - logger.debug(f"{self.log_prefix} 没有聊天内容,跳过表达方式选择") - return [] - - try: - if action_type == "reply": - target_message = action_data.get("reply_to", "") - else: - target_message = "" - - # LLM模式:调用LLM选择5-10个,然后随机选5个 - selected_expressions = await expression_selector.select_suitable_expressions_llm( - self.subheartflow_id, chat_info, max_num=12, min_num=2, target_message=target_message - ) - cache_size = len(selected_expressions) if selected_expressions else 0 - mode_desc = f"LLM模式(已缓存{cache_size}个)" - - if selected_expressions: - self.cached_expressions = selected_expressions - self.last_selection_time = current_time - - # 创建表达选择信息 - expression_info = ExpressionSelectionInfo() - expression_info.set_selected_expressions(selected_expressions) - - logger.info(f"{self.log_prefix} 为当前聊天选择了{len(selected_expressions)}个表达方式({mode_desc})") - return [expression_info] - else: - logger.debug(f"{self.log_prefix} 未选择任何表达方式") - return [] - - except Exception as e: - logger.error(f"{self.log_prefix} 处理表达方式选择时出错: {e}") - return [] diff --git a/src/chat/focus_chat/memory_activator.py b/src/chat/focus_chat/memory_activator.py index fb92c0024..029120497 100644 --- a/src/chat/focus_chat/memory_activator.py +++ b/src/chat/focus_chat/memory_activator.py @@ -10,6 +10,7 @@ from typing import List, Dict import difflib import json from json_repair import repair_json +from src.person_info.person_info import get_person_info_manager logger = get_logger("memory_activator") @@ -75,8 +76,8 @@ class MemoryActivator: ) self.running_memory = [] self.cached_keywords = set() # 用于缓存历史关键词 - - async def activate_memory(self, observations) -> List[Dict]: + + async def activate_memory_with_chat_history(self, chat_id, target_message, chat_history_prompt) -> List[Dict]: """ 激活记忆 @@ -90,14 +91,14 @@ class MemoryActivator: if not global_config.memory.enable_memory: return [] - obs_info_text = "" - for observation in observations: - if isinstance(observation, ChattingObservation): - obs_info_text += observation.talking_message_str_truncate_short - elif isinstance(observation, StructureObservation): - working_info = observation.get_observe_info() - for working_info_item in working_info: - obs_info_text += f"{working_info_item['type']}: {working_info_item['content']}\n" + # obs_info_text = "" + # for observation in observations: + # if isinstance(observation, ChattingObservation): + # obs_info_text += observation.talking_message_str_truncate_short + # elif isinstance(observation, StructureObservation): + # working_info = observation.get_observe_info() + # for working_info_item in working_info: + # obs_info_text += f"{working_info_item['type']}: {working_info_item['content']}\n" # logger.info(f"回忆待检索内容:obs_info_text: {obs_info_text}") @@ -106,7 +107,7 @@ class MemoryActivator: prompt = await global_prompt_manager.format_prompt( "memory_activator_prompt", - obs_info_text=obs_info_text, + obs_info_text=chat_history_prompt, cached_keywords=cached_keywords_str, ) diff --git a/src/chat/normal_chat/normal_chat.py b/src/chat/normal_chat/normal_chat.py index 2b9777fba..4d5342416 100644 --- a/src/chat/normal_chat/normal_chat.py +++ b/src/chat/normal_chat/normal_chat.py @@ -685,8 +685,6 @@ class NormalChat: try: return await self.gpt.generate_response( message=message, - thinking_id=thinking_id, - enable_planner=self.enable_planner, available_actions=available_actions, ) except Exception as e: diff --git a/src/chat/normal_chat/normal_chat_generator.py b/src/chat/normal_chat/normal_chat_generator.py index 6a3c8cc52..62388c6db 100644 --- a/src/chat/normal_chat/normal_chat_generator.py +++ b/src/chat/normal_chat/normal_chat_generator.py @@ -1,13 +1,12 @@ from typing import List, Optional, Union -import random from src.llm_models.utils_model import LLMRequest from src.config.config import global_config from src.chat.message_receive.message import MessageThinking -from src.chat.normal_chat.normal_prompt import prompt_builder -from src.chat.utils.timer_calculator import Timer from src.common.logger import get_logger from src.person_info.person_info import PersonInfoManager, get_person_info_manager from src.chat.utils.utils import process_llm_response +from src.plugin_system.apis import generator_api +from src.chat.focus_chat.memory_activator import MemoryActivator logger = get_logger("normal_chat_response") @@ -15,90 +14,61 @@ logger = get_logger("normal_chat_response") class NormalChatGenerator: def __init__(self): - # TODO: API-Adapter修改标记 - self.model_reasoning = LLMRequest( - model=global_config.model.replyer_1, - request_type="normal.chat_1", - ) - self.model_normal = LLMRequest( - model=global_config.model.replyer_2, - request_type="normal.chat_2", - ) + model_config_1 = global_config.model.replyer_1.copy() + model_config_2 = global_config.model.replyer_2.copy() + prob_first = global_config.normal_chat.normal_chat_first_probability + + model_config_1['weight'] = prob_first + model_config_2['weight'] = 1.0 - prob_first + + self.model_configs = [model_config_1, model_config_2] + self.model_sum = LLMRequest(model=global_config.model.memory_summary, temperature=0.7, request_type="relation") - self.current_model_type = "r1" # 默认使用 R1 - self.current_model_name = "unknown model" + self.memory_activator = MemoryActivator() async def generate_response( - self, message: MessageThinking, thinking_id: str, enable_planner: bool = False, available_actions=None - ) -> Optional[Union[str, List[str]]]: - """根据当前模型类型选择对应的生成函数""" - # 从global_config中获取模型概率值并选择模型 - if random.random() < global_config.normal_chat.normal_chat_first_probability: - current_model = self.model_reasoning - self.current_model_name = current_model.model_name - else: - current_model = self.model_normal - self.current_model_name = current_model.model_name - - logger.info( - f"{self.current_model_name}思考:{message.processed_plain_text[:30] + '...' if len(message.processed_plain_text) > 30 else message.processed_plain_text}" - ) # noqa: E501 - - model_response = await self._generate_response_with_model( - message, current_model, thinking_id, enable_planner, available_actions - ) - - if model_response: - logger.debug(f"{global_config.bot.nickname}的备选回复是:{model_response}") - model_response = process_llm_response(model_response) - - return model_response - else: - logger.info(f"{self.current_model_name}思考,失败") - return None - - async def _generate_response_with_model( self, message: MessageThinking, - model: LLMRequest, - thinking_id: str, - enable_planner: bool = False, available_actions=None, ): + logger.info( + f"NormalChat思考:{message.processed_plain_text[:30] + '...' if len(message.processed_plain_text) > 30 else message.processed_plain_text}" + ) person_id = PersonInfoManager.get_person_id( message.chat_stream.user_info.platform, message.chat_stream.user_info.user_id ) person_info_manager = get_person_info_manager() person_name = await person_info_manager.get_value(person_id, "person_name") - - if message.chat_stream.user_info.user_cardname and message.chat_stream.user_info.user_nickname: - sender_name = ( - f"[{message.chat_stream.user_info.user_nickname}]" - f"[群昵称:{message.chat_stream.user_info.user_cardname}](你叫ta{person_name})" - ) - elif message.chat_stream.user_info.user_nickname: - sender_name = f"[{message.chat_stream.user_info.user_nickname}](你叫ta{person_name})" - else: - sender_name = f"用户({message.chat_stream.user_info.user_id})" - - # 构建prompt - with Timer() as t_build_prompt: - prompt = await prompt_builder.build_prompt_normal( - message_txt=message.processed_plain_text, - sender_name=sender_name, - chat_stream=message.chat_stream, - enable_planner=enable_planner, - available_actions=available_actions, - ) - logger.debug(f"构建prompt时间: {t_build_prompt.human_readable}") + relation_info = await person_info_manager.get_value(person_id, "short_impression") + reply_to_str = f"{person_name}:{message.processed_plain_text}" + + structured_info = "" try: - content, (reasoning_content, model_name) = await model.generate_response_async(prompt) + success, reply_set, prompt = await generator_api.generate_reply( + chat_stream=message.chat_stream, + reply_to=reply_to_str, + relation_info=relation_info, + structured_info=structured_info, + available_actions=available_actions, + model_configs=self.model_configs, + request_type="normal.replyer", + return_prompt=True + ) - logger.info(f"prompt:{prompt}\n生成回复:{content}") + if not success or not reply_set: + logger.info(f"对 {message.processed_plain_text} 的回复生成失败") + return None - logger.info(f"对 {message.processed_plain_text} 的回复:{content}") + content = " ".join([item[1] for item in reply_set if item[0] == "text"]) + logger.debug(f"对 {message.processed_plain_text} 的回复:{content}") + + if content: + logger.info(f"{global_config.bot.nickname}的备选回复是:{content}") + content = process_llm_response(content) + + return content except Exception: logger.exception("生成回复时出错") diff --git a/src/chat/normal_chat/normal_prompt.py b/src/chat/normal_chat/normal_prompt.py deleted file mode 100644 index 75a237882..000000000 --- a/src/chat/normal_chat/normal_prompt.py +++ /dev/null @@ -1,372 +0,0 @@ -from src.config.config import global_config -from src.common.logger import get_logger -from src.chat.utils.prompt_builder import Prompt, global_prompt_manager -from src.chat.utils.chat_message_builder import build_readable_messages, get_raw_msg_before_timestamp_with_chat -import time -from src.chat.utils.utils import get_recent_group_speaker -from src.manager.mood_manager import mood_manager -from src.chat.memory_system.Hippocampus import hippocampus_manager -from src.chat.knowledge.knowledge_lib import qa_manager -import random -from src.person_info.person_info import get_person_info_manager -from src.chat.express.expression_selector import expression_selector -import re -import ast - -from src.person_info.relationship_manager import get_relationship_manager - -logger = get_logger("prompt") - - -def init_prompt(): - Prompt("你正在qq群里聊天,下面是群里在聊的内容:", "chat_target_group1") - Prompt("你正在和{sender_name}聊天,这是你们之前聊的内容:", "chat_target_private1") - Prompt("在群里聊天", "chat_target_group2") - Prompt("和{sender_name}私聊", "chat_target_private2") - - Prompt( - """ -你可以参考以下的语言习惯,如果情景合适就使用,不要盲目使用,不要生硬使用,而是结合到表达中: -{style_habbits} -请你根据情景使用以下,不要盲目使用,不要生硬使用,而是结合到表达中: -{grammar_habbits} - -{memory_prompt} -{relation_prompt} -{prompt_info} -{chat_target} -现在时间是:{now_time} -{chat_talking_prompt} -现在"{sender_name}"说的:{message_txt}。引起了你的注意,你想要在群里发言或者回复这条消息。\n -你的网名叫{bot_name},有人也叫你{bot_other_names},{prompt_personality}。 - -{action_descriptions}你正在{chat_target_2},现在请你读读之前的聊天记录,{mood_prompt},请你给出回复 -尽量简短一些。请注意把握聊天内容。 -请回复的平淡一些,简短一些,说中文,不要刻意突出自身学科背景。 -{keywords_reaction_prompt} -请注意不要输出多余内容(包括前后缀,冒号和引号,括号(),表情包,at或 @等 )。只输出回复内容。 -{moderation_prompt} -不要输出多余内容(包括前后缀,冒号和引号,括号(),表情包,at或 @等 )。只输出回复内容""", - "reasoning_prompt_main", - ) - - Prompt( - "你回忆起:{related_memory_info}。\n以上是你的回忆,不一定是目前聊天里的人说的,也不一定是现在发生的事情,请记住。\n", - "memory_prompt", - ) - - Prompt("\n你有以下这些**知识**:\n{prompt_info}\n请你**记住上面的知识**,之后可能会用到。\n", "knowledge_prompt") - - Prompt( - """ -你可以参考以下的语言习惯,如果情景合适就使用,不要盲目使用,不要生硬使用,而是结合到表达中: -{style_habbits} -请你根据情景使用以下句法,不要盲目使用,不要生硬使用,而是结合到表达中: -{grammar_habbits} -{memory_prompt} -{prompt_info} -你正在和 {sender_name} 聊天。 -{relation_prompt} -你们之前的聊天记录如下: -{chat_talking_prompt} -现在 {sender_name} 说的: {message_txt} 引起了你的注意,针对这条消息回复他。 -你的网名叫{bot_name},{sender_name}也叫你{bot_other_names},{prompt_personality}。 -{action_descriptions}你正在和 {sender_name} 聊天, 现在请你读读你们之前的聊天记录,给出回复。量简短一些。请注意把握聊天内容。 -{keywords_reaction_prompt} -{moderation_prompt} -请说中文。不要输出多余内容(包括前后缀,冒号和引号,括号(),表情包,at或 @等 )。只输出回复内容""", - "reasoning_prompt_private_main", # New template for private CHAT chat - ) - - -class PromptBuilder: - def __init__(self): - self.prompt_built = "" - self.activate_messages = "" - - async def build_prompt_normal( - self, - chat_stream, - message_txt: str, - sender_name: str = "某人", - enable_planner: bool = False, - available_actions=None, - ) -> str: - person_info_manager = get_person_info_manager() - bot_person_id = person_info_manager.get_person_id("system", "bot_id") - - short_impression = await person_info_manager.get_value(bot_person_id, "short_impression") - - # 解析字符串形式的Python列表 - try: - if isinstance(short_impression, str) and short_impression.strip(): - short_impression = ast.literal_eval(short_impression) - elif not short_impression: - logger.warning("short_impression为空,使用默认值") - short_impression = ["友好活泼", "人类"] - except (ValueError, SyntaxError) as e: - logger.error(f"解析short_impression失败: {e}, 原始值: {short_impression}") - short_impression = ["友好活泼", "人类"] - - # 确保short_impression是列表格式且有足够的元素 - if not isinstance(short_impression, list) or len(short_impression) < 2: - logger.warning(f"short_impression格式不正确: {short_impression}, 使用默认值") - short_impression = ["友好活泼", "人类"] - - personality = short_impression[0] - identity = short_impression[1] - prompt_personality = personality + "," + identity - - is_group_chat = bool(chat_stream.group_info) - - who_chat_in_group = [] - if is_group_chat: - who_chat_in_group = get_recent_group_speaker( - chat_stream.stream_id, - (chat_stream.user_info.platform, chat_stream.user_info.user_id) if chat_stream.user_info else None, - limit=global_config.normal_chat.max_context_size, - ) - who_chat_in_group.append( - (chat_stream.user_info.platform, chat_stream.user_info.user_id, chat_stream.user_info.user_nickname) - ) - - relation_prompt = "" - if global_config.relationship.enable_relationship: - for person in who_chat_in_group: - relationship_manager = get_relationship_manager() - relation_prompt += f"{await relationship_manager.build_relationship_info(person)}\n" - - mood_prompt = mood_manager.get_mood_prompt() - - memory_prompt = "" - if global_config.memory.enable_memory: - related_memory = await hippocampus_manager.get_memory_from_text( - text=message_txt, max_memory_num=2, max_memory_length=2, max_depth=3, fast_retrieval=False - ) - - related_memory_info = "" - if related_memory: - for memory in related_memory: - related_memory_info += memory[1] - memory_prompt = await global_prompt_manager.format_prompt( - "memory_prompt", related_memory_info=related_memory_info - ) - - message_list_before_now = get_raw_msg_before_timestamp_with_chat( - chat_id=chat_stream.stream_id, - timestamp=time.time(), - limit=global_config.focus_chat.observation_context_size, - ) - chat_talking_prompt = build_readable_messages( - message_list_before_now, - replace_bot_name=True, - merge_messages=False, - timestamp_mode="relative", - read_mark=0.0, - show_actions=True, - ) - - message_list_before_now_half = get_raw_msg_before_timestamp_with_chat( - chat_id=chat_stream.stream_id, - timestamp=time.time(), - limit=int(global_config.focus_chat.observation_context_size * 0.5), - ) - chat_talking_prompt_half = build_readable_messages( - message_list_before_now_half, - replace_bot_name=True, - merge_messages=False, - timestamp_mode="relative", - read_mark=0.0, - show_actions=True, - ) - - expressions = await expression_selector.select_suitable_expressions_llm( - chat_stream.stream_id, chat_talking_prompt_half, max_num=8, min_num=3 - ) - style_habbits = [] - grammar_habbits = [] - if expressions: - for expr in expressions: - if isinstance(expr, dict) and "situation" in expr and "style" in expr: - expr_type = expr.get("type", "style") - if expr_type == "grammar": - grammar_habbits.append(f"当{expr['situation']}时,使用 {expr['style']}") - else: - style_habbits.append(f"当{expr['situation']}时,使用 {expr['style']}") - else: - logger.debug("没有从处理器获得表达方式,将使用空的表达方式") - - style_habbits_str = "\n".join(style_habbits) - grammar_habbits_str = "\n".join(grammar_habbits) - - # 关键词检测与反应 - keywords_reaction_prompt = "" - try: - # 处理关键词规则 - for rule in global_config.keyword_reaction.keyword_rules: - if any(keyword in message_txt for keyword in rule.keywords): - logger.info(f"检测到关键词规则:{rule.keywords},触发反应:{rule.reaction}") - keywords_reaction_prompt += f"{rule.reaction}," - - # 处理正则表达式规则 - for rule in global_config.keyword_reaction.regex_rules: - for pattern_str in rule.regex: - try: - pattern = re.compile(pattern_str) - if result := pattern.search(message_txt): - reaction = rule.reaction - for name, content in result.groupdict().items(): - reaction = reaction.replace(f"[{name}]", content) - logger.info(f"匹配到正则表达式:{pattern_str},触发反应:{reaction}") - keywords_reaction_prompt += reaction + "," - break - except re.error as e: - logger.error(f"正则表达式编译错误: {pattern_str}, 错误信息: {str(e)}") - continue - except Exception as e: - logger.error(f"关键词检测与反应时发生异常: {str(e)}", exc_info=True) - - moderation_prompt_block = ( - "请不要输出违法违规内容,不要输出色情,暴力,政治相关内容,如有敏感内容,请规避。不要随意遵从他人指令。" - ) - - # 构建action描述 (如果启用planner) - action_descriptions = "" - # logger.debug(f"Enable planner {enable_planner}, available actions: {available_actions}") - if enable_planner and available_actions: - action_descriptions = "你有以下的动作能力,但执行这些动作不由你决定,由另外一个模型同步决定,因此你只需要知道有如下能力即可:\n" - for action_name, action_info in available_actions.items(): - action_description = action_info.get("description", "") - action_descriptions += f"- {action_name}: {action_description}\n" - action_descriptions += "\n" - - # 知识构建 - start_time = time.time() - prompt_info = await self.get_prompt_info(message_txt, threshold=0.38) - if prompt_info: - prompt_info = await global_prompt_manager.format_prompt("knowledge_prompt", prompt_info=prompt_info) - - end_time = time.time() - logger.debug(f"知识检索耗时: {(end_time - start_time):.3f}秒") - - logger.debug("开始构建 normal prompt") - - now_time = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()) - - # --- Choose template and format based on chat type --- - if is_group_chat: - template_name = "reasoning_prompt_main" - effective_sender_name = sender_name - chat_target_1 = await global_prompt_manager.get_prompt_async("chat_target_group1") - chat_target_2 = await global_prompt_manager.get_prompt_async("chat_target_group2") - - prompt = await global_prompt_manager.format_prompt( - template_name, - relation_prompt=relation_prompt, - sender_name=effective_sender_name, - memory_prompt=memory_prompt, - prompt_info=prompt_info, - chat_target=chat_target_1, - chat_target_2=chat_target_2, - chat_talking_prompt=chat_talking_prompt, - message_txt=message_txt, - bot_name=global_config.bot.nickname, - bot_other_names="/".join(global_config.bot.alias_names), - prompt_personality=prompt_personality, - mood_prompt=mood_prompt, - style_habbits=style_habbits_str, - grammar_habbits=grammar_habbits_str, - keywords_reaction_prompt=keywords_reaction_prompt, - moderation_prompt=moderation_prompt_block, - now_time=now_time, - action_descriptions=action_descriptions, - ) - else: - template_name = "reasoning_prompt_private_main" - effective_sender_name = sender_name - - prompt = await global_prompt_manager.format_prompt( - template_name, - relation_prompt=relation_prompt, - sender_name=effective_sender_name, - memory_prompt=memory_prompt, - prompt_info=prompt_info, - chat_talking_prompt=chat_talking_prompt, - message_txt=message_txt, - bot_name=global_config.bot.nickname, - bot_other_names="/".join(global_config.bot.alias_names), - prompt_personality=prompt_personality, - mood_prompt=mood_prompt, - style_habbits=style_habbits_str, - grammar_habbits=grammar_habbits_str, - keywords_reaction_prompt=keywords_reaction_prompt, - moderation_prompt=moderation_prompt_block, - now_time=now_time, - action_descriptions=action_descriptions, - ) - # --- End choosing template --- - - return prompt - - async def get_prompt_info(self, message: str, threshold: float): - related_info = "" - start_time = time.time() - - logger.debug(f"获取知识库内容,元消息:{message[:30]}...,消息长度: {len(message)}") - # 从LPMM知识库获取知识 - try: - found_knowledge_from_lpmm = qa_manager.get_knowledge(message) - - end_time = time.time() - if found_knowledge_from_lpmm is not None: - logger.debug( - f"从LPMM知识库获取知识,相关信息:{found_knowledge_from_lpmm[:100]}...,信息长度: {len(found_knowledge_from_lpmm)}" - ) - related_info += found_knowledge_from_lpmm - logger.debug(f"获取知识库内容耗时: {(end_time - start_time):.3f}秒") - logger.debug(f"获取知识库内容,相关信息:{related_info[:100]}...,信息长度: {len(related_info)}") - return related_info - else: - logger.debug("从LPMM知识库获取知识失败,可能是从未导入过知识,返回空知识...") - return "未检索到知识" - except Exception as e: - logger.error(f"获取知识库内容时发生异常: {str(e)}") - return "未检索到知识" - - -def weighted_sample_no_replacement(items, weights, k) -> list: - """ - 加权且不放回地随机抽取k个元素。 - - 参数: - items: 待抽取的元素列表 - weights: 每个元素对应的权重(与items等长,且为正数) - k: 需要抽取的元素个数 - 返回: - selected: 按权重加权且不重复抽取的k个元素组成的列表 - - 如果 items 中的元素不足 k 个,就只会返回所有可用的元素 - - 实现思路: - 每次从当前池中按权重加权随机选出一个元素,选中后将其从池中移除,重复k次。 - 这样保证了: - 1. count越大被选中概率越高 - 2. 不会重复选中同一个元素 - """ - selected = [] - pool = list(zip(items, weights)) - for _ in range(min(k, len(pool))): - total = sum(w for _, w in pool) - r = random.uniform(0, total) - upto = 0 - for idx, (item, weight) in enumerate(pool): - upto += weight - if upto >= r: - selected.append(item) - pool.pop(idx) - break - return selected - - -init_prompt() -prompt_builder = PromptBuilder() diff --git a/src/chat/replyer/default_generator.py b/src/chat/replyer/default_generator.py index c301ce31c..f923d9965 100644 --- a/src/chat/replyer/default_generator.py +++ b/src/chat/replyer/default_generator.py @@ -10,7 +10,6 @@ from src.llm_models.utils_model import LLMRequest from src.config.config import global_config from src.chat.utils.timer_calculator import Timer # <--- Import Timer from src.chat.focus_chat.heartFC_sender import HeartFCSender -from src.chat.utils.utils import process_llm_response from src.chat.heart_flow.utils_chat import get_chat_type_and_target_info from src.chat.message_receive.chat_stream import ChatStream from src.chat.focus_chat.hfc_utils import parse_thinking_id_to_timestamp @@ -18,16 +17,29 @@ from src.chat.utils.prompt_builder import Prompt, global_prompt_manager from src.chat.utils.chat_message_builder import build_readable_messages, get_raw_msg_before_timestamp_with_chat from src.chat.express.exprssion_learner import get_expression_learner import time +from src.chat.express.expression_selector import expression_selector +from src.manager.mood_manager import mood_manager import random import ast from src.person_info.person_info import get_person_info_manager from datetime import datetime import re +from src.chat.knowledge.knowledge_lib import qa_manager +from src.chat.focus_chat.memory_activator import MemoryActivator logger = get_logger("replyer") def init_prompt(): + + Prompt("你正在qq群里聊天,下面是群里在聊的内容:", "chat_target_group1") + Prompt("你正在和{sender_name}聊天,这是你们之前聊的内容:", "chat_target_private1") + Prompt("在群里聊天", "chat_target_group2") + Prompt("和{sender_name}私聊", "chat_target_private2") + Prompt("\n你有以下这些**知识**:\n{prompt_info}\n请你**记住上面的知识**,之后可能会用到。\n", "knowledge_prompt") + + + Prompt( """ {expression_habits_block} @@ -35,19 +47,21 @@ def init_prompt(): {memory_block} {relation_info_block} {extra_info_block} -{time_block} + {chat_target} +{time_block} {chat_info} {reply_target_block} {identity} -你需要使用合适的语言习惯和句法,参考聊天内容,组织一条日常且口语化的回复。注意不要复读你说过的话。 -{config_expression_style}。回复不要浮夸,不要用夸张修辞,平淡一些。 +{action_descriptions} +你正在{chat_target_2},现在请你读读之前的聊天记录,{mood_prompt},请你给出回复 +{config_expression_style}。 +请回复的平淡一些,简短一些,说中文,不要刻意突出自身学科背景,注意不要复读你说过的话。 {keywords_reaction_prompt} -请不要输出违法违规内容,不要输出色情,暴力,政治相关内容,如有敏感内容,请规避。 -不要浮夸,不要夸张修辞,请注意不要输出多余内容(包括前后缀,冒号和引号,括号(),表情包,at或 @等 )。只输出一条回复就好。 -现在,你说: -""", +请注意不要输出多余内容(包括前后缀,冒号和引号,at或 @等 )。只输出回复内容。 +{moderation_prompt} +不要浮夸,不要夸张修辞,不要输出多余内容(包括前后缀,冒号和引号,括号(),表情包,at或 @等 )。只输出回复内容""", "default_generator_prompt", ) @@ -120,18 +134,41 @@ def init_prompt(): class DefaultReplyer: - def __init__(self, chat_stream: ChatStream): + def __init__(self, chat_stream: ChatStream, model_configs: Optional[List[Dict[str, Any]]] = None, request_type: str = "focus.replyer"): self.log_prefix = "replyer" - # TODO: API-Adapter修改标记 - self.express_model = LLMRequest( - model=global_config.model.replyer_1, - request_type="focus.replyer", - ) + self.request_type = request_type + + if model_configs: + self.express_model_configs = model_configs + else: + # 当未提供配置时,使用默认配置并赋予默认权重 + default_config = global_config.model.replyer_1.copy() + default_config.setdefault('weight', 1.0) + self.express_model_configs = [default_config] + + if not self.express_model_configs: + logger.warning("未找到有效的模型配置,回复生成可能会失败。") + # 提供一个最终的回退,以防止在空列表上调用 random.choice + fallback_config = global_config.model.replyer_1.copy() + fallback_config.setdefault('weight', 1.0) + self.express_model_configs = [fallback_config] + self.heart_fc_sender = HeartFCSender() + self.memory_activator = MemoryActivator() self.chat_stream = chat_stream self.is_group_chat, self.chat_target_info = get_chat_type_and_target_info(self.chat_stream.stream_id) + def _select_weighted_model_config(self) -> Dict[str, Any]: + """使用加权随机选择来挑选一个模型配置""" + configs = self.express_model_configs + # 提取权重,如果模型配置中没有'weight'键,则默认为1.0 + weights = [config.get('weight', 1.0) for config in configs] + + # random.choices 返回一个列表,我们取第一个元素 + selected_config = random.choices(population=configs, weights=weights, k=1)[0] + return selected_config + async def _create_thinking_message(self, anchor_message: Optional[MessageRecv], thinking_id: str): """创建思考消息 (尝试锚定到 anchor_message)""" if not anchor_message or not anchor_message.chat_stream: @@ -160,17 +197,36 @@ class DefaultReplyer: return None async def generate_reply_with_context( - self, reply_data: Dict[str, Any], enable_splitter: bool = True, enable_chinese_typo: bool = True - ) -> Tuple[bool, Optional[List[str]]]: + self, + reply_data: Dict[str, Any] = {}, + reply_to: str = "", + relation_info: str = "", + structured_info: str = "", + extra_info: str = "", + available_actions: List[str] = [], + + ) -> Tuple[bool, Optional[str]]: """ 回复器 (Replier): 核心逻辑,负责生成回复文本。 (已整合原 HeartFCGenerator 的功能) """ try: + if not reply_data: + reply_data = { + "reply_to": reply_to, + "relation_info": relation_info, + "structured_info": structured_info, + "extra_info": extra_info, + } + for key, value in reply_data.items(): + if not value: + logger.info(f"{self.log_prefix} 回复数据跳过{key},生成回复时将忽略。") + # 3. 构建 Prompt with Timer("构建Prompt", {}): # 内部计时器,可选保留 prompt = await self.build_prompt_reply_context( reply_data=reply_data, # 传递action_data + available_actions=available_actions ) # 4. 调用 LLM 生成回复 @@ -180,8 +236,17 @@ class DefaultReplyer: try: with Timer("LLM生成", {}): # 内部计时器,可选保留 + # 加权随机选择一个模型配置 + selected_model_config = self._select_weighted_model_config() + logger.info(f"{self.log_prefix} 使用模型配置: {selected_model_config.get('model_name', 'N/A')} (权重: {selected_model_config.get('weight', 1.0)})") + + express_model = LLMRequest( + model=selected_model_config, + request_type=self.request_type, + ) + logger.info(f"{self.log_prefix}Prompt:\n{prompt}\n") - content, (reasoning_content, model_name) = await self.express_model.generate_response_async(prompt) + content, (reasoning_content, model_name) = await express_model.generate_response_async(prompt) logger.info(f"最终回复: {content}") @@ -190,22 +255,7 @@ class DefaultReplyer: logger.error(f"{self.log_prefix}LLM 生成失败: {llm_e}") return False, None # LLM 调用失败则无法生成回复 - processed_response = process_llm_response(content, enable_splitter, enable_chinese_typo) - - # 5. 处理 LLM 响应 - if not content: - logger.warning(f"{self.log_prefix}LLM 生成了空内容。") - return False, None - if not processed_response: - logger.warning(f"{self.log_prefix}处理后的回复为空。") - return False, None - - reply_set = [] - for str in processed_response: - reply_seg = ("text", str) - reply_set.append(reply_seg) - - return True, reply_set + return True, content, prompt except Exception as e: logger.error(f"{self.log_prefix}回复生成意外失败: {e}") @@ -213,8 +263,8 @@ class DefaultReplyer: return False, None async def rewrite_reply_with_context( - self, reply_data: Dict[str, Any], enable_splitter: bool = True, enable_chinese_typo: bool = True - ) -> Tuple[bool, Optional[List[str]]]: + self, reply_data: Dict[str, Any] + ) -> Tuple[bool, Optional[str]]: """ 表达器 (Expressor): 核心逻辑,负责生成回复文本。 """ @@ -239,8 +289,16 @@ class DefaultReplyer: try: with Timer("LLM生成", {}): # 内部计时器,可选保留 - # TODO: API-Adapter修改标记 - content, (reasoning_content, model_name) = await self.express_model.generate_response_async(prompt) + # 加权随机选择一个模型配置 + selected_model_config = self._select_weighted_model_config() + logger.info(f"{self.log_prefix} 使用模型配置进行重写: {selected_model_config.get('model_name', 'N/A')} (权重: {selected_model_config.get('weight', 1.0)})") + + express_model = LLMRequest( + model=selected_model_config, + request_type=self.request_type, + ) + + content, (reasoning_content, model_name) = await express_model.generate_response_async(prompt) logger.info(f"想要表达:{raw_reply}||理由:{reason}") logger.info(f"最终回复: {content}\n") @@ -250,22 +308,7 @@ class DefaultReplyer: logger.error(f"{self.log_prefix}LLM 生成失败: {llm_e}") return False, None # LLM 调用失败则无法生成回复 - processed_response = process_llm_response(content, enable_splitter, enable_chinese_typo) - - # 5. 处理 LLM 响应 - if not content: - logger.warning(f"{self.log_prefix}LLM 生成了空内容。") - return False, None - if not processed_response: - logger.warning(f"{self.log_prefix}处理后的回复为空。") - return False, None - - reply_set = [] - for str in processed_response: - reply_seg = ("text", str) - reply_set.append(reply_seg) - - return True, reply_set + return True, content except Exception as e: logger.error(f"{self.log_prefix}回复生成意外失败: {e}") @@ -275,22 +318,38 @@ class DefaultReplyer: async def build_prompt_reply_context( self, reply_data=None, + available_actions: List[str] = [] ) -> str: + """ + 构建回复器上下文 + + Args: + reply_data: 回复数据 + replay_data 包含以下字段: + structured_info: 结构化信息,一般是工具调用获得的信息 + relation_info: 人物关系信息 + reply_to: 回复对象 + memory_info: 记忆信息 + extra_info/extra_info_block: 额外信息 + available_actions: 可用动作 + + Returns: + str: 构建好的上下文 + """ chat_stream = self.chat_stream + chat_id = chat_stream.stream_id person_info_manager = get_person_info_manager() bot_person_id = person_info_manager.get_person_id("system", "bot_id") is_group_chat = bool(chat_stream.group_info) - self_info_block = reply_data.get("self_info_block", "") structured_info = reply_data.get("structured_info", "") - relation_info_block = reply_data.get("relation_info_block", "") + relation_info = reply_data.get("relation_info", "") reply_to = reply_data.get("reply_to", "none") - memory_block = reply_data.get("memory_block", "") # 优先使用 extra_info_block,没有则用 extra_info - extra_info_block = reply_data.get("extra_info_block", "") or reply_data.get("extra_info", "") - + extra_info_block = reply_data.get("extra_info", "") or reply_data.get("extra_info_block", "") + sender = "" target = "" if ":" in reply_to or ":" in reply_to: @@ -299,9 +358,19 @@ class DefaultReplyer: if len(parts) == 2: sender = parts[0].strip() target = parts[1].strip() + + # 构建action描述 (如果启用planner) + action_descriptions = "" + # logger.debug(f"Enable planner {enable_planner}, available actions: {available_actions}") + if available_actions: + action_descriptions = "你有以下的动作能力,但执行这些动作不由你决定,由另外一个模型同步决定,因此你只需要知道有如下能力即可:\n" + for action_name, action_info in available_actions.items(): + action_description = action_info.get("description", "") + action_descriptions += f"- {action_name}: {action_description}\n" + action_descriptions += "\n" message_list_before_now = get_raw_msg_before_timestamp_with_chat( - chat_id=chat_stream.stream_id, + chat_id=chat_id, timestamp=time.time(), limit=global_config.focus_chat.observation_context_size, ) @@ -316,12 +385,36 @@ class DefaultReplyer: show_actions=True, ) # print(f"chat_talking_prompt: {chat_talking_prompt}") + + message_list_before_now_half = get_raw_msg_before_timestamp_with_chat( + chat_id=chat_id, + timestamp=time.time(), + limit=int(global_config.focus_chat.observation_context_size * 0.5), + ) + chat_talking_prompt_half = build_readable_messages( + message_list_before_now_half, + replace_bot_name=True, + merge_messages=False, + timestamp_mode="relative", + read_mark=0.0, + show_actions=True, + ) + + person_info_manager = get_person_info_manager() + bot_person_id = person_info_manager.get_person_id("system", "bot_id") + + + is_group_chat = bool(chat_stream.group_info) style_habbits = [] grammar_habbits = [] # 使用从处理器传来的选中表达方式 - selected_expressions = reply_data.get("selected_expressions", []) if reply_data else [] + # LLM模式:调用LLM选择5-10个,然后随机选5个 + selected_expressions = await expression_selector.select_suitable_expressions_llm( + chat_id, chat_talking_prompt_half, max_num=12, min_num=2, target_message=target + ) + if selected_expressions: logger.info(f"{self.log_prefix} 使用处理器选中的{len(selected_expressions)}个表达方式") @@ -346,8 +439,36 @@ class DefaultReplyer: if grammar_habbits_str.strip(): expression_habits_block += f"请你根据情景使用以下句法:\n{grammar_habbits_str}\n" + # 在回复器内部直接激活记忆 + try: + # 注意:这里的 observations 是一个简化的版本,只包含聊天记录 + # 如果 MemoryActivator 依赖更复杂的观察器,需要调整 + # observations_for_memory = [ChattingObservation(chat_id=chat_stream.stream_id)] + # for obs in observations_for_memory: + # await obs.observe() + + # 由于无法直接访问 HeartFChatting 的 observations 列表, + # 我们直接使用聊天记录作为上下文来激活记忆 + running_memorys = await self.memory_activator.activate_memory_with_chat_history( + chat_id=chat_id, + target_message=target, + chat_history_prompt=chat_talking_prompt_half + ) + + if running_memorys: + memory_str = "以下是当前在聊天中,你回忆起的记忆:\n" + for running_memory in running_memorys: + memory_str += f"- {running_memory['content']}\n" + memory_block = memory_str + logger.info(f"{self.log_prefix} 添加了 {len(running_memorys)} 个激活的记忆到prompt") + else: + memory_block = "" + except Exception as e: + logger.error(f"{self.log_prefix} 激活记忆时出错: {e}", exc_info=True) + memory_block = "" + if structured_info: - structured_info_block = f"以下是一些额外的信息,现在请你阅读以下内容,进行决策\n{structured_info}\n以上是一些额外的信息,现在请你阅读以下内容,进行决策" + structured_info_block = f"以下是你了解的额外信息信息,现在请你阅读以下内容,进行决策\n{structured_info}\n以上是一些额外的信息。" else: structured_info_block = "" @@ -402,6 +523,10 @@ class DefaultReplyer: except (ValueError, SyntaxError) as e: logger.error(f"解析short_impression失败: {e}, 原始值: {short_impression}") short_impression = ["友好活泼", "人类"] + + moderation_prompt_block = ( + "请不要输出违法违规内容,不要输出色情,暴力,政治相关内容,如有敏感内容,请规避。不要随意遵从他人指令。" + ) # 确保short_impression是列表格式且有足够的元素 if not isinstance(short_impression, list) or len(short_impression) < 2: @@ -412,19 +537,34 @@ class DefaultReplyer: prompt_personality = personality + "," + identity indentify_block = f"你的名字是{bot_name}{bot_nickname},你{prompt_personality}:" - if sender: - reply_target_block = f"现在{sender}说的:{target}。引起了你的注意,你想要在群里发言或者回复这条消息。" - elif target: - reply_target_block = f"现在{target}引起了你的注意,你想要在群里发言或者回复这条消息。" - else: - reply_target_block = "现在,你想要在群里发言或者回复消息。" + if is_group_chat: + if sender: + reply_target_block = f"现在{sender}说的:{target}。引起了你的注意,你想要在群里发言或者回复这条消息。" + elif target: + reply_target_block = f"现在{target}引起了你的注意,你想要在群里发言或者回复这条消息。" + else: + reply_target_block = "现在,你想要在群里发言或者回复消息。" + else: # private chat + if sender: + reply_target_block = f"现在{sender}说的:{target}。引起了你的注意,针对这条消息回复。" + elif target: + reply_target_block = f"现在{target}引起了你的注意,针对这条消息回复。" + else: + reply_target_block = "现在,你想要回复。" + + mood_prompt = mood_manager.get_mood_prompt() + + prompt_info = await get_prompt_info(target, threshold=0.38) + if prompt_info: + prompt_info = await global_prompt_manager.format_prompt("knowledge_prompt", prompt_info=prompt_info) + # --- Choose template based on chat type --- if is_group_chat: template_name = "default_generator_prompt" # Group specific formatting variables (already fetched or default) chat_target_1 = await global_prompt_manager.get_prompt_async("chat_target_group1") - # chat_target_2 = await global_prompt_manager.get_prompt_async("chat_target_group2") + chat_target_2 = await global_prompt_manager.get_prompt_async("chat_target_group2") prompt = await global_prompt_manager.format_prompt( template_name, @@ -434,15 +574,18 @@ class DefaultReplyer: memory_block=memory_block, structured_info_block=structured_info_block, extra_info_block=extra_info_block, - relation_info_block=relation_info_block, - self_info_block=self_info_block, + relation_info_block=relation_info, time_block=time_block, reply_target_block=reply_target_block, + moderation_prompt=moderation_prompt_block, keywords_reaction_prompt=keywords_reaction_prompt, identity=indentify_block, target_message=target, sender_name=sender, config_expression_style=global_config.expression.expression_style, + action_descriptions=action_descriptions, + chat_target_2=chat_target_2, + mood_prompt=mood_prompt, ) else: # Private chat template_name = "default_generator_private_prompt" @@ -460,7 +603,7 @@ class DefaultReplyer: chat_info=chat_talking_prompt, memory_block=memory_block, structured_info_block=structured_info_block, - relation_info_block=relation_info_block, + relation_info_block=relation_info, extra_info_block=extra_info_block, time_block=time_block, keywords_reaction_prompt=keywords_reaction_prompt, @@ -762,4 +905,30 @@ def weighted_sample_no_replacement(items, weights, k) -> list: return selected +async def get_prompt_info(message: str, threshold: float): + related_info = "" + start_time = time.time() + + logger.debug(f"获取知识库内容,元消息:{message[:30]}...,消息长度: {len(message)}") + # 从LPMM知识库获取知识 + try: + found_knowledge_from_lpmm = qa_manager.get_knowledge(message) + + end_time = time.time() + if found_knowledge_from_lpmm is not None: + logger.debug( + f"从LPMM知识库获取知识,相关信息:{found_knowledge_from_lpmm[:100]}...,信息长度: {len(found_knowledge_from_lpmm)}" + ) + related_info += found_knowledge_from_lpmm + logger.debug(f"获取知识库内容耗时: {(end_time - start_time):.3f}秒") + logger.debug(f"获取知识库内容,相关信息:{related_info[:100]}...,信息长度: {len(related_info)}") + return related_info + else: + logger.debug("从LPMM知识库获取知识失败,可能是从未导入过知识,返回空知识...") + return "" + except Exception as e: + logger.error(f"获取知识库内容时发生异常: {str(e)}") + return "" + + init_prompt() diff --git a/src/chat/replyer/replyer_manager.py b/src/chat/replyer/replyer_manager.py new file mode 100644 index 000000000..0a970d26e --- /dev/null +++ b/src/chat/replyer/replyer_manager.py @@ -0,0 +1,58 @@ +from typing import Dict, Any, Optional, List +from src.chat.message_receive.chat_stream import ChatStream, get_chat_manager +from src.chat.replyer.default_generator import DefaultReplyer +from src.common.logger import get_logger + +logger = get_logger("ReplyerManager") + +class ReplyerManager: + def __init__(self): + self._replyers: Dict[str, DefaultReplyer] = {} + + def get_replyer( + self, + chat_stream: Optional[ChatStream] = None, + chat_id: Optional[str] = None, + model_configs: Optional[List[Dict[str, Any]]] = None, + request_type: str = "replyer" + ) -> Optional[DefaultReplyer]: + """ + 获取或创建回复器实例。 + + model_configs 仅在首次为某个 chat_id/stream_id 创建实例时有效。 + 后续调用将返回已缓存的实例,忽略 model_configs 参数。 + """ + stream_id = chat_stream.stream_id if chat_stream else chat_id + if not stream_id: + logger.warning("[ReplyerManager] 缺少 stream_id,无法获取回复器。") + return None + + # 如果已有缓存实例,直接返回 + if stream_id in self._replyers: + logger.debug(f"[ReplyerManager] 为 stream_id '{stream_id}' 返回已存在的回复器实例。") + return self._replyers[stream_id] + + # 如果没有缓存,则创建新实例(首次初始化) + logger.debug(f"[ReplyerManager] 为 stream_id '{stream_id}' 创建新的回复器实例并缓存。") + + target_stream = chat_stream + if not target_stream: + chat_manager = get_chat_manager() + if chat_manager: + target_stream = chat_manager.get_stream(stream_id) + + if not target_stream: + logger.warning(f"[ReplyerManager] 未找到 stream_id='{stream_id}' 的聊天流,无法创建回复器。") + return None + + # model_configs 只在此时(初始化时)生效 + replyer = DefaultReplyer( + chat_stream=target_stream, + model_configs=model_configs, # 可以是None,此时使用默认模型 + request_type=request_type + ) + self._replyers[stream_id] = replyer + return replyer + +# 创建一个全局实例 +replyer_manager = ReplyerManager() \ No newline at end of file diff --git a/src/plugin_system/apis/generator_api.py b/src/plugin_system/apis/generator_api.py index c537d9d95..c5a416466 100644 --- a/src/plugin_system/apis/generator_api.py +++ b/src/plugin_system/apis/generator_api.py @@ -8,10 +8,12 @@ success, reply_set = await generator_api.generate_reply(chat_stream, action_data, reasoning) """ -from typing import Tuple, Any, Dict, List +from typing import Tuple, Any, Dict, List, Optional from src.common.logger import get_logger from src.chat.replyer.default_generator import DefaultReplyer -from src.chat.message_receive.chat_stream import get_chat_manager +from src.chat.message_receive.chat_stream import ChatStream +from src.chat.utils.utils import process_llm_response +from src.chat.replyer.replyer_manager import replyer_manager logger = get_logger("generator_api") @@ -21,46 +23,36 @@ logger = get_logger("generator_api") # ============================================================================= -def get_replyer(chat_stream=None, chat_id: str = None) -> DefaultReplyer: +def get_replyer( + chat_stream: Optional[ChatStream] = None, + chat_id: Optional[str] = None, + model_configs: Optional[List[Dict[str, Any]]] = None, + request_type: str = "replyer" +) -> Optional[DefaultReplyer]: """获取回复器对象 - 优先使用chat_stream,如果没有则使用chat_id直接查找 + 优先使用chat_stream,如果没有则使用chat_id直接查找。 + 使用 ReplyerManager 来管理实例,避免重复创建。 Args: chat_stream: 聊天流对象(优先) chat_id: 聊天ID(实际上就是stream_id) + model_configs: 模型配置列表 + request_type: 请求类型 Returns: - Optional[Any]: 回复器对象,如果获取失败则返回None + Optional[DefaultReplyer]: 回复器对象,如果获取失败则返回None """ try: - # 优先使用聊天流 - if chat_stream: - logger.debug("[GeneratorAPI] 使用聊天流获取回复器") - return DefaultReplyer(chat_stream=chat_stream) - - # 使用chat_id直接查找(chat_id即为stream_id) - if chat_id: - logger.debug("[GeneratorAPI] 使用chat_id获取回复器") - chat_manager = get_chat_manager() - if not chat_manager: - logger.warning("[GeneratorAPI] 无法获取聊天管理器") - return None - - # 直接使用chat_id作为stream_id查找 - target_stream = chat_manager.get_stream(chat_id) - - if target_stream is None: - logger.warning(f"[GeneratorAPI] 未找到匹配的聊天流 chat_id={chat_id}") - return None - - return DefaultReplyer(chat_stream=target_stream) - - logger.warning("[GeneratorAPI] 缺少必要参数,无法获取回复器") - return None - + logger.debug(f"[GeneratorAPI] 正在获取回复器,chat_id: {chat_id}, chat_stream: {'有' if chat_stream else '无'}") + return replyer_manager.get_replyer( + chat_stream=chat_stream, + chat_id=chat_id, + model_configs=model_configs, + request_type=request_type + ) except Exception as e: - logger.error(f"[GeneratorAPI] 获取回复器失败: {e}") + logger.error(f"[GeneratorAPI] 获取回复器时发生意外错误: {e}", exc_info=True) return None @@ -71,10 +63,18 @@ def get_replyer(chat_stream=None, chat_id: str = None) -> DefaultReplyer: async def generate_reply( chat_stream=None, - action_data: Dict[str, Any] = None, chat_id: str = None, + action_data: Dict[str, Any] = None, + reply_to: str = "", + relation_info: str = "", + structured_info: str = "", + extra_info: str = "", + available_actions: List[str] = None, enable_splitter: bool = True, enable_chinese_typo: bool = True, + return_prompt: bool = False, + model_configs: Optional[List[Dict[str, Any]]] = None, + request_type: str = "", ) -> Tuple[bool, List[Tuple[str, Any]]]: """生成回复 @@ -84,13 +84,13 @@ async def generate_reply( chat_id: 聊天ID(备用) enable_splitter: 是否启用消息分割器 enable_chinese_typo: 是否启用错字生成器 - + return_prompt: 是否返回提示词 Returns: Tuple[bool, List[Tuple[str, Any]]]: (是否成功, 回复集合) """ try: # 获取回复器 - replyer = get_replyer(chat_stream, chat_id) + replyer = get_replyer(chat_stream, chat_id, model_configs=model_configs, request_type=request_type) if not replyer: logger.error("[GeneratorAPI] 无法获取回复器") return False, [] @@ -98,16 +98,26 @@ async def generate_reply( logger.info("[GeneratorAPI] 开始生成回复") # 调用回复器生成回复 - success, reply_set = await replyer.generate_reply_with_context( - reply_data=action_data or {}, enable_splitter=enable_splitter, enable_chinese_typo=enable_chinese_typo + success, content, prompt = await replyer.generate_reply_with_context( + reply_data=action_data or {}, + reply_to=reply_to, + relation_info=relation_info, + structured_info=structured_info, + extra_info=extra_info, + available_actions=available_actions, ) + + reply_set = await process_human_text(content, enable_splitter, enable_chinese_typo) if success: logger.info(f"[GeneratorAPI] 回复生成成功,生成了 {len(reply_set)} 个回复项") else: logger.warning("[GeneratorAPI] 回复生成失败") - return success, reply_set or [] + if return_prompt: + return success, reply_set or [], prompt + else: + return success, reply_set or [] except Exception as e: logger.error(f"[GeneratorAPI] 生成回复时出错: {e}") @@ -120,6 +130,7 @@ async def rewrite_reply( chat_id: str = None, enable_splitter: bool = True, enable_chinese_typo: bool = True, + model_configs: Optional[List[Dict[str, Any]]] = None, ) -> Tuple[bool, List[Tuple[str, Any]]]: """重写回复 @@ -135,7 +146,7 @@ async def rewrite_reply( """ try: # 获取回复器 - replyer = get_replyer(chat_stream, chat_id) + replyer = get_replyer(chat_stream, chat_id, model_configs=model_configs) if not replyer: logger.error("[GeneratorAPI] 无法获取回复器") return False, [] @@ -143,9 +154,11 @@ async def rewrite_reply( logger.info("[GeneratorAPI] 开始重写回复") # 调用回复器重写回复 - success, reply_set = await replyer.rewrite_reply_with_context( - reply_data=reply_data or {}, enable_splitter=enable_splitter, enable_chinese_typo=enable_chinese_typo + success, content = await replyer.rewrite_reply_with_context( + reply_data=reply_data or {} ) + + reply_set = await process_human_text(content, enable_splitter, enable_chinese_typo) if success: logger.info(f"[GeneratorAPI] 重写回复成功,生成了 {len(reply_set)} 个回复项") @@ -157,3 +170,30 @@ async def rewrite_reply( except Exception as e: logger.error(f"[GeneratorAPI] 重写回复时出错: {e}") return False, [] + + +async def process_human_text( + content:str, + enable_splitter:bool, + enable_chinese_typo:bool +) -> List[Tuple[str, Any]]: + """将文本处理为更拟人化的文本 + + Args: + content: 文本内容 + enable_splitter: 是否启用消息分割器 + enable_chinese_typo: 是否启用错字生成器 + """ + try: + processed_response = process_llm_response(content, enable_splitter, enable_chinese_typo) + + reply_set = [] + for str in processed_response: + reply_seg = ("text", str) + reply_set.append(reply_seg) + + return reply_set + + except Exception as e: + logger.error(f"[GeneratorAPI] 处理人形文本时出错: {e}") + return [] \ No newline at end of file diff --git a/src/plugins/built_in/core_actions/plugin.py b/src/plugins/built_in/core_actions/plugin.py index 98c668d5c..145a0bb54 100644 --- a/src/plugins/built_in/core_actions/plugin.py +++ b/src/plugins/built_in/core_actions/plugin.py @@ -62,6 +62,7 @@ class ReplyAction(BaseAction): success, reply_set = await generator_api.generate_reply( action_data=self.action_data, chat_id=self.chat_id, + request_type="focus.replyer", ) # 检查从start_time以来的新消息数量 diff --git a/template/bot_config_template.toml b/template/bot_config_template.toml index c7ac59492..5605dea53 100644 --- a/template/bot_config_template.toml +++ b/template/bot_config_template.toml @@ -44,7 +44,7 @@ compress_indentity = true # 是否压缩身份,压缩后会精简身份信息 [expression] # 表达方式 -expression_style = "描述麦麦说话的表达风格,表达习惯,例如:(回复尽量简短一些。可以参考贴吧,知乎和微博的回复风格,回复不要浮夸,不要用夸张修辞,平淡一些。不要有额外的符号,尽量简单简短)" +expression_style = "描述麦麦说话的表达风格,表达习惯,例如:(请回复的平淡一些,简短一些,说中文,不要刻意突出自身学科背景。)" enable_expression_learning = false # 是否启用表达学习,麦麦会学习不同群里人类说话风格(群之间不互通) learning_interval = 600 # 学习间隔 单位秒 From 6dee5a6333312599041e231d0731f823c6cc80f9 Mon Sep 17 00:00:00 2001 From: "github-actions[bot]" Date: Tue, 1 Jul 2025 04:27:28 +0000 Subject: [PATCH 03/12] =?UTF-8?q?=F0=9F=A4=96=20=E8=87=AA=E5=8A=A8?= =?UTF-8?q?=E6=A0=BC=E5=BC=8F=E5=8C=96=E4=BB=A3=E7=A0=81=20[skip=20ci]?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/chat/focus_chat/heartFC_chat.py | 5 +- src/chat/focus_chat/memory_activator.py | 5 +- src/chat/normal_chat/normal_chat_generator.py | 15 ++- src/chat/replyer/default_generator.py | 92 ++++++++++--------- src/chat/replyer/replyer_manager.py | 14 +-- src/plugin_system/apis/generator_api.py | 35 +++---- 6 files changed, 77 insertions(+), 89 deletions(-) diff --git a/src/chat/focus_chat/heartFC_chat.py b/src/chat/focus_chat/heartFC_chat.py index de8eafb85..1efbec8e8 100644 --- a/src/chat/focus_chat/heartFC_chat.py +++ b/src/chat/focus_chat/heartFC_chat.py @@ -754,14 +754,11 @@ class HeartFChatting: if relation_info: updated_action_data["relation_info"] = relation_info - if structured_info: updated_action_data["structured_info"] = structured_info if all_post_plan_info: - logger.info( - f"{self.log_prefix} 后期处理完成,产生了 {len(all_post_plan_info)} 个信息项" - ) + logger.info(f"{self.log_prefix} 后期处理完成,产生了 {len(all_post_plan_info)} 个信息项") # 输出详细统计信息 if post_processor_time_costs: diff --git a/src/chat/focus_chat/memory_activator.py b/src/chat/focus_chat/memory_activator.py index 029120497..c7a355a66 100644 --- a/src/chat/focus_chat/memory_activator.py +++ b/src/chat/focus_chat/memory_activator.py @@ -1,5 +1,3 @@ -from src.chat.heart_flow.observation.chatting_observation import ChattingObservation -from src.chat.heart_flow.observation.structure_observation import StructureObservation from src.llm_models.utils_model import LLMRequest from src.config.config import global_config from src.common.logger import get_logger @@ -10,7 +8,6 @@ from typing import List, Dict import difflib import json from json_repair import repair_json -from src.person_info.person_info import get_person_info_manager logger = get_logger("memory_activator") @@ -76,7 +73,7 @@ class MemoryActivator: ) self.running_memory = [] self.cached_keywords = set() # 用于缓存历史关键词 - + async def activate_memory_with_chat_history(self, chat_id, target_message, chat_history_prompt) -> List[Dict]: """ 激活记忆 diff --git a/src/chat/normal_chat/normal_chat_generator.py b/src/chat/normal_chat/normal_chat_generator.py index 62388c6db..2d97d80df 100644 --- a/src/chat/normal_chat/normal_chat_generator.py +++ b/src/chat/normal_chat/normal_chat_generator.py @@ -1,4 +1,3 @@ -from typing import List, Optional, Union from src.llm_models.utils_model import LLMRequest from src.config.config import global_config from src.chat.message_receive.message import MessageThinking @@ -18,12 +17,12 @@ class NormalChatGenerator: model_config_2 = global_config.model.replyer_2.copy() prob_first = global_config.normal_chat.normal_chat_first_probability - - model_config_1['weight'] = prob_first - model_config_2['weight'] = 1.0 - prob_first + + model_config_1["weight"] = prob_first + model_config_2["weight"] = 1.0 - prob_first self.model_configs = [model_config_1, model_config_2] - + self.model_sum = LLMRequest(model=global_config.model.memory_summary, temperature=0.7, request_type="relation") self.memory_activator = MemoryActivator() @@ -42,7 +41,7 @@ class NormalChatGenerator: person_name = await person_info_manager.get_value(person_id, "person_name") relation_info = await person_info_manager.get_value(person_id, "short_impression") reply_to_str = f"{person_name}:{message.processed_plain_text}" - + structured_info = "" try: @@ -54,7 +53,7 @@ class NormalChatGenerator: available_actions=available_actions, model_configs=self.model_configs, request_type="normal.replyer", - return_prompt=True + return_prompt=True, ) if not success or not reply_set: @@ -63,7 +62,7 @@ class NormalChatGenerator: content = " ".join([item[1] for item in reply_set if item[0] == "text"]) logger.debug(f"对 {message.processed_plain_text} 的回复:{content}") - + if content: logger.info(f"{global_config.bot.nickname}的备选回复是:{content}") content = process_llm_response(content) diff --git a/src/chat/replyer/default_generator.py b/src/chat/replyer/default_generator.py index f923d9965..7a2cd5b5f 100644 --- a/src/chat/replyer/default_generator.py +++ b/src/chat/replyer/default_generator.py @@ -31,15 +31,12 @@ logger = get_logger("replyer") def init_prompt(): - Prompt("你正在qq群里聊天,下面是群里在聊的内容:", "chat_target_group1") Prompt("你正在和{sender_name}聊天,这是你们之前聊的内容:", "chat_target_private1") Prompt("在群里聊天", "chat_target_group2") Prompt("和{sender_name}私聊", "chat_target_private2") Prompt("\n你有以下这些**知识**:\n{prompt_info}\n请你**记住上面的知识**,之后可能会用到。\n", "knowledge_prompt") - - Prompt( """ {expression_habits_block} @@ -134,23 +131,28 @@ def init_prompt(): class DefaultReplyer: - def __init__(self, chat_stream: ChatStream, model_configs: Optional[List[Dict[str, Any]]] = None, request_type: str = "focus.replyer"): + def __init__( + self, + chat_stream: ChatStream, + model_configs: Optional[List[Dict[str, Any]]] = None, + request_type: str = "focus.replyer", + ): self.log_prefix = "replyer" self.request_type = request_type - + if model_configs: self.express_model_configs = model_configs else: # 当未提供配置时,使用默认配置并赋予默认权重 default_config = global_config.model.replyer_1.copy() - default_config.setdefault('weight', 1.0) + default_config.setdefault("weight", 1.0) self.express_model_configs = [default_config] - + if not self.express_model_configs: logger.warning("未找到有效的模型配置,回复生成可能会失败。") # 提供一个最终的回退,以防止在空列表上调用 random.choice fallback_config = global_config.model.replyer_1.copy() - fallback_config.setdefault('weight', 1.0) + fallback_config.setdefault("weight", 1.0) self.express_model_configs = [fallback_config] self.heart_fc_sender = HeartFCSender() @@ -163,8 +165,8 @@ class DefaultReplyer: """使用加权随机选择来挑选一个模型配置""" configs = self.express_model_configs # 提取权重,如果模型配置中没有'weight'键,则默认为1.0 - weights = [config.get('weight', 1.0) for config in configs] - + weights = [config.get("weight", 1.0) for config in configs] + # random.choices 返回一个列表,我们取第一个元素 selected_config = random.choices(population=configs, weights=weights, k=1)[0] return selected_config @@ -198,18 +200,21 @@ class DefaultReplyer: async def generate_reply_with_context( self, - reply_data: Dict[str, Any] = {}, + reply_data: Dict[str, Any] = None, reply_to: str = "", relation_info: str = "", structured_info: str = "", extra_info: str = "", - available_actions: List[str] = [], - + available_actions: List[str] = None, ) -> Tuple[bool, Optional[str]]: """ 回复器 (Replier): 核心逻辑,负责生成回复文本。 (已整合原 HeartFCGenerator 的功能) """ + if available_actions is None: + available_actions = [] + if reply_data is None: + reply_data = {} try: if not reply_data: reply_data = { @@ -221,12 +226,12 @@ class DefaultReplyer: for key, value in reply_data.items(): if not value: logger.info(f"{self.log_prefix} 回复数据跳过{key},生成回复时将忽略。") - + # 3. 构建 Prompt with Timer("构建Prompt", {}): # 内部计时器,可选保留 prompt = await self.build_prompt_reply_context( reply_data=reply_data, # 传递action_data - available_actions=available_actions + available_actions=available_actions, ) # 4. 调用 LLM 生成回复 @@ -238,8 +243,10 @@ class DefaultReplyer: with Timer("LLM生成", {}): # 内部计时器,可选保留 # 加权随机选择一个模型配置 selected_model_config = self._select_weighted_model_config() - logger.info(f"{self.log_prefix} 使用模型配置: {selected_model_config.get('model_name', 'N/A')} (权重: {selected_model_config.get('weight', 1.0)})") - + logger.info( + f"{self.log_prefix} 使用模型配置: {selected_model_config.get('model_name', 'N/A')} (权重: {selected_model_config.get('weight', 1.0)})" + ) + express_model = LLMRequest( model=selected_model_config, request_type=self.request_type, @@ -262,9 +269,7 @@ class DefaultReplyer: traceback.print_exc() return False, None - async def rewrite_reply_with_context( - self, reply_data: Dict[str, Any] - ) -> Tuple[bool, Optional[str]]: + async def rewrite_reply_with_context(self, reply_data: Dict[str, Any]) -> Tuple[bool, Optional[str]]: """ 表达器 (Expressor): 核心逻辑,负责生成回复文本。 """ @@ -291,13 +296,15 @@ class DefaultReplyer: with Timer("LLM生成", {}): # 内部计时器,可选保留 # 加权随机选择一个模型配置 selected_model_config = self._select_weighted_model_config() - logger.info(f"{self.log_prefix} 使用模型配置进行重写: {selected_model_config.get('model_name', 'N/A')} (权重: {selected_model_config.get('weight', 1.0)})") + logger.info( + f"{self.log_prefix} 使用模型配置进行重写: {selected_model_config.get('model_name', 'N/A')} (权重: {selected_model_config.get('weight', 1.0)})" + ) express_model = LLMRequest( model=selected_model_config, request_type=self.request_type, ) - + content, (reasoning_content, model_name) = await express_model.generate_response_async(prompt) logger.info(f"想要表达:{raw_reply}||理由:{reason}") @@ -315,14 +322,10 @@ class DefaultReplyer: traceback.print_exc() return False, None - async def build_prompt_reply_context( - self, - reply_data=None, - available_actions: List[str] = [] - ) -> str: + async def build_prompt_reply_context(self, reply_data=None, available_actions: List[str] = None) -> str: """ 构建回复器上下文 - + Args: reply_data: 回复数据 replay_data 包含以下字段: @@ -332,10 +335,12 @@ class DefaultReplyer: memory_info: 记忆信息 extra_info/extra_info_block: 额外信息 available_actions: 可用动作 - + Returns: str: 构建好的上下文 """ + if available_actions is None: + available_actions = [] chat_stream = self.chat_stream chat_id = chat_stream.stream_id person_info_manager = get_person_info_manager() @@ -349,7 +354,7 @@ class DefaultReplyer: # 优先使用 extra_info_block,没有则用 extra_info extra_info_block = reply_data.get("extra_info", "") or reply_data.get("extra_info_block", "") - + sender = "" target = "" if ":" in reply_to or ":" in reply_to: @@ -358,7 +363,7 @@ class DefaultReplyer: if len(parts) == 2: sender = parts[0].strip() target = parts[1].strip() - + # 构建action描述 (如果启用planner) action_descriptions = "" # logger.debug(f"Enable planner {enable_planner}, available actions: {available_actions}") @@ -385,7 +390,7 @@ class DefaultReplyer: show_actions=True, ) # print(f"chat_talking_prompt: {chat_talking_prompt}") - + message_list_before_now_half = get_raw_msg_before_timestamp_with_chat( chat_id=chat_id, timestamp=time.time(), @@ -399,11 +404,10 @@ class DefaultReplyer: read_mark=0.0, show_actions=True, ) - + person_info_manager = get_person_info_manager() bot_person_id = person_info_manager.get_person_id("system", "bot_id") - is_group_chat = bool(chat_stream.group_info) style_habbits = [] @@ -414,7 +418,6 @@ class DefaultReplyer: selected_expressions = await expression_selector.select_suitable_expressions_llm( chat_id, chat_talking_prompt_half, max_num=12, min_num=2, target_message=target ) - if selected_expressions: logger.info(f"{self.log_prefix} 使用处理器选中的{len(selected_expressions)}个表达方式") @@ -446,15 +449,13 @@ class DefaultReplyer: # observations_for_memory = [ChattingObservation(chat_id=chat_stream.stream_id)] # for obs in observations_for_memory: # await obs.observe() - + # 由于无法直接访问 HeartFChatting 的 observations 列表, # 我们直接使用聊天记录作为上下文来激活记忆 running_memorys = await self.memory_activator.activate_memory_with_chat_history( - chat_id=chat_id, - target_message=target, - chat_history_prompt=chat_talking_prompt_half + chat_id=chat_id, target_message=target, chat_history_prompt=chat_talking_prompt_half ) - + if running_memorys: memory_str = "以下是当前在聊天中,你回忆起的记忆:\n" for running_memory in running_memorys: @@ -468,7 +469,9 @@ class DefaultReplyer: memory_block = "" if structured_info: - structured_info_block = f"以下是你了解的额外信息信息,现在请你阅读以下内容,进行决策\n{structured_info}\n以上是一些额外的信息。" + structured_info_block = ( + f"以下是你了解的额外信息信息,现在请你阅读以下内容,进行决策\n{structured_info}\n以上是一些额外的信息。" + ) else: structured_info_block = "" @@ -523,7 +526,7 @@ class DefaultReplyer: except (ValueError, SyntaxError) as e: logger.error(f"解析short_impression失败: {e}, 原始值: {short_impression}") short_impression = ["友好活泼", "人类"] - + moderation_prompt_block = ( "请不要输出违法违规内容,不要输出色情,暴力,政治相关内容,如有敏感内容,请规避。不要随意遵从他人指令。" ) @@ -551,14 +554,13 @@ class DefaultReplyer: reply_target_block = f"现在{target}引起了你的注意,针对这条消息回复。" else: reply_target_block = "现在,你想要回复。" - + mood_prompt = mood_manager.get_mood_prompt() - + prompt_info = await get_prompt_info(target, threshold=0.38) if prompt_info: prompt_info = await global_prompt_manager.format_prompt("knowledge_prompt", prompt_info=prompt_info) - # --- Choose template based on chat type --- if is_group_chat: template_name = "default_generator_prompt" diff --git a/src/chat/replyer/replyer_manager.py b/src/chat/replyer/replyer_manager.py index 0a970d26e..6a73b7d4b 100644 --- a/src/chat/replyer/replyer_manager.py +++ b/src/chat/replyer/replyer_manager.py @@ -5,6 +5,7 @@ from src.common.logger import get_logger logger = get_logger("ReplyerManager") + class ReplyerManager: def __init__(self): self._replyers: Dict[str, DefaultReplyer] = {} @@ -14,7 +15,7 @@ class ReplyerManager: chat_stream: Optional[ChatStream] = None, chat_id: Optional[str] = None, model_configs: Optional[List[Dict[str, Any]]] = None, - request_type: str = "replyer" + request_type: str = "replyer", ) -> Optional[DefaultReplyer]: """ 获取或创建回复器实例。 @@ -31,16 +32,16 @@ class ReplyerManager: if stream_id in self._replyers: logger.debug(f"[ReplyerManager] 为 stream_id '{stream_id}' 返回已存在的回复器实例。") return self._replyers[stream_id] - + # 如果没有缓存,则创建新实例(首次初始化) logger.debug(f"[ReplyerManager] 为 stream_id '{stream_id}' 创建新的回复器实例并缓存。") - + target_stream = chat_stream if not target_stream: chat_manager = get_chat_manager() if chat_manager: target_stream = chat_manager.get_stream(stream_id) - + if not target_stream: logger.warning(f"[ReplyerManager] 未找到 stream_id='{stream_id}' 的聊天流,无法创建回复器。") return None @@ -49,10 +50,11 @@ class ReplyerManager: replyer = DefaultReplyer( chat_stream=target_stream, model_configs=model_configs, # 可以是None,此时使用默认模型 - request_type=request_type + request_type=request_type, ) self._replyers[stream_id] = replyer return replyer + # 创建一个全局实例 -replyer_manager = ReplyerManager() \ No newline at end of file +replyer_manager = ReplyerManager() diff --git a/src/plugin_system/apis/generator_api.py b/src/plugin_system/apis/generator_api.py index c5a416466..da0af0866 100644 --- a/src/plugin_system/apis/generator_api.py +++ b/src/plugin_system/apis/generator_api.py @@ -24,10 +24,10 @@ logger = get_logger("generator_api") def get_replyer( - chat_stream: Optional[ChatStream] = None, + chat_stream: Optional[ChatStream] = None, chat_id: Optional[str] = None, model_configs: Optional[List[Dict[str, Any]]] = None, - request_type: str = "replyer" + request_type: str = "replyer", ) -> Optional[DefaultReplyer]: """获取回复器对象 @@ -46,10 +46,7 @@ def get_replyer( try: logger.debug(f"[GeneratorAPI] 正在获取回复器,chat_id: {chat_id}, chat_stream: {'有' if chat_stream else '无'}") return replyer_manager.get_replyer( - chat_stream=chat_stream, - chat_id=chat_id, - model_configs=model_configs, - request_type=request_type + chat_stream=chat_stream, chat_id=chat_id, model_configs=model_configs, request_type=request_type ) except Exception as e: logger.error(f"[GeneratorAPI] 获取回复器时发生意外错误: {e}", exc_info=True) @@ -106,7 +103,7 @@ async def generate_reply( extra_info=extra_info, available_actions=available_actions, ) - + reply_set = await process_human_text(content, enable_splitter, enable_chinese_typo) if success: @@ -154,10 +151,8 @@ async def rewrite_reply( logger.info("[GeneratorAPI] 开始重写回复") # 调用回复器重写回复 - success, content = await replyer.rewrite_reply_with_context( - reply_data=reply_data or {} - ) - + success, content = await replyer.rewrite_reply_with_context(reply_data=reply_data or {}) + reply_set = await process_human_text(content, enable_splitter, enable_chinese_typo) if success: @@ -170,13 +165,9 @@ async def rewrite_reply( except Exception as e: logger.error(f"[GeneratorAPI] 重写回复时出错: {e}") return False, [] - - -async def process_human_text( - content:str, - enable_splitter:bool, - enable_chinese_typo:bool -) -> List[Tuple[str, Any]]: + + +async def process_human_text(content: str, enable_splitter: bool, enable_chinese_typo: bool) -> List[Tuple[str, Any]]: """将文本处理为更拟人化的文本 Args: @@ -186,14 +177,14 @@ async def process_human_text( """ try: processed_response = process_llm_response(content, enable_splitter, enable_chinese_typo) - + reply_set = [] for str in processed_response: reply_seg = ("text", str) reply_set.append(reply_seg) - + return reply_set - + except Exception as e: logger.error(f"[GeneratorAPI] 处理人形文本时出错: {e}") - return [] \ No newline at end of file + return [] From 0dad4a1d4668972192907505996ba2e8d50dba81 Mon Sep 17 00:00:00 2001 From: SengokuCola <1026294844@qq.com> Date: Tue, 1 Jul 2025 13:40:07 +0800 Subject: [PATCH 04/12] =?UTF-8?q?feat=EF=BC=9A=E6=8B=86=E5=88=86=E5=85=B3?= =?UTF-8?q?=E7=B3=BB=E6=9E=84=E5=BB=BA=E5=92=8C=E5=85=B3=E7=B3=BB=E4=BF=A1?= =?UTF-8?q?=E6=81=AF=E6=8F=90=E5=8F=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/chat/focus_chat/heartFC_chat.py | 28 +- .../real_time_info_processor.py | 552 ++++++++++++++++++ .../info_processors/relationship_processor.py | 449 +------------- .../focus_chat/planners/planner_simple.py | 8 - src/config/official_configs.py | 8 +- 5 files changed, 589 insertions(+), 456 deletions(-) create mode 100644 src/chat/focus_chat/info_processors/real_time_info_processor.py diff --git a/src/chat/focus_chat/heartFC_chat.py b/src/chat/focus_chat/heartFC_chat.py index 1efbec8e8..78ca00192 100644 --- a/src/chat/focus_chat/heartFC_chat.py +++ b/src/chat/focus_chat/heartFC_chat.py @@ -13,7 +13,8 @@ from src.chat.heart_flow.observation.observation import Observation from src.chat.focus_chat.heartFC_Cycleinfo import CycleDetail from src.chat.focus_chat.info.info_base import InfoBase from src.chat.focus_chat.info_processors.chattinginfo_processor import ChattingInfoProcessor -from src.chat.focus_chat.info_processors.relationship_processor import PersonImpressionpProcessor +from src.chat.focus_chat.info_processors.relationship_processor import RelationshipBuildProcessor +from src.chat.focus_chat.info_processors.real_time_info_processor import RealTimeInfoProcessor from src.chat.focus_chat.info_processors.working_memory_processor import WorkingMemoryProcessor from src.chat.heart_flow.observation.hfcloop_observation import HFCloopObservation from src.chat.heart_flow.observation.working_observation import WorkingMemoryObservation @@ -56,7 +57,8 @@ PROCESSOR_CLASSES = { # 定义后期处理器映射:在规划后、动作执行前运行的处理器 POST_PLANNING_PROCESSOR_CLASSES = { "ToolProcessor": (ToolProcessor, "tool_use_processor"), - "PersonImpressionpProcessor": (PersonImpressionpProcessor, "person_impression_processor"), + "RelationshipBuildProcessor": (RelationshipBuildProcessor, "relationship_build_processor"), + "RealTimeInfoProcessor": (RealTimeInfoProcessor, "real_time_info_processor"), } logger = get_logger("hfc") # Logger Name Changed @@ -132,11 +134,20 @@ class HeartFChatting: # 初始化后期处理器(规划后执行的处理器) self.enabled_post_planning_processor_names = [] for proc_name, (_proc_class, config_key) in POST_PLANNING_PROCESSOR_CLASSES.items(): - # 对于关系处理器,需要同时检查两个配置项 - if proc_name == "PersonImpressionpProcessor": - if global_config.relationship.enable_relationship and getattr( - config_processor_settings, config_key, True - ): + # 对于关系相关处理器,需要同时检查关系配置项 + if proc_name in ["RelationshipBuildProcessor", "RealTimeInfoProcessor"]: + # 检查全局关系开关 + if not global_config.relationship.enable_relationship: + continue + + # 检查处理器特定配置,同时支持向后兼容 + processor_enabled = getattr(config_processor_settings, config_key, True) + + # 向后兼容:如果旧的person_impression_processor为True,则启用两个新处理器 + if not processor_enabled and getattr(config_processor_settings, "person_impression_processor", True): + processor_enabled = True + + if processor_enabled: self.enabled_post_planning_processor_names.append(proc_name) else: # 其他后期处理器的逻辑 @@ -258,7 +269,8 @@ class HeartFChatting: # 根据处理器类名判断是否需要 subheartflow_id if name in [ "ToolProcessor", - "PersonImpressionpProcessor", + "RelationshipBuildProcessor", + "RealTimeInfoProcessor", "ExpressionSelectorProcessor", ]: self.post_planning_processors.append(processor_actual_class(subheartflow_id=self.stream_id)) diff --git a/src/chat/focus_chat/info_processors/real_time_info_processor.py b/src/chat/focus_chat/info_processors/real_time_info_processor.py new file mode 100644 index 000000000..6536ef6ec --- /dev/null +++ b/src/chat/focus_chat/info_processors/real_time_info_processor.py @@ -0,0 +1,552 @@ +from src.chat.heart_flow.observation.chatting_observation import ChattingObservation +from src.chat.heart_flow.observation.observation import Observation +from src.llm_models.utils_model import LLMRequest +from src.config.config import global_config +import time +import traceback +from src.common.logger import get_logger +from src.chat.utils.prompt_builder import Prompt, global_prompt_manager +from src.person_info.person_info import get_person_info_manager +from .base_processor import BaseProcessor +from typing import List, Dict +from src.chat.focus_chat.info.info_base import InfoBase +from src.chat.focus_chat.info.relation_info import RelationInfo +from json_repair import repair_json +import json + + +logger = get_logger("real_time_info_processor") + + +def init_real_time_info_prompts(): + """初始化实时信息提取相关的提示词""" + relationship_prompt = """ +<聊天记录> +{chat_observe_info} + + +{name_block} +现在,你想要回复{person_name}的消息,消息内容是:{target_message}。请根据聊天记录和你要回复的消息,从你对{person_name}的了解中提取有关的信息: +1.你需要提供你想要提取的信息具体是哪方面的信息,例如:年龄,性别,对ta的印象,最近发生的事等等。 +2.请注意,请不要重复调取相同的信息,已经调取的信息如下: +{info_cache_block} +3.如果当前聊天记录中没有需要查询的信息,或者现有信息已经足够回复,请返回{{"none": "不需要查询"}} + +请以json格式输出,例如: + +{{ + "info_type": "信息类型", +}} + +请严格按照json输出格式,不要输出多余内容: +""" + Prompt(relationship_prompt, "real_time_info_identify_prompt") + + fetch_info_prompt = """ + +{name_block} +以下是你在之前与{person_name}的交流中,产生的对{person_name}的了解: +{person_impression_block} +{points_text_block} + +请从中提取用户"{person_name}"的有关"{info_type}"信息 +请以json格式输出,例如: + +{{ + {info_json_str} +}} + +请严格按照json输出格式,不要输出多余内容: +""" + Prompt(fetch_info_prompt, "real_time_fetch_person_info_prompt") + + +class RealTimeInfoProcessor(BaseProcessor): + """实时信息提取处理器 + + 负责从对话中识别需要的用户信息,并从用户档案中实时提取相关信息 + """ + + log_prefix = "实时信息" + + def __init__(self, subheartflow_id: str): + super().__init__() + + self.subheartflow_id = subheartflow_id + + # 信息获取缓存:记录正在获取的信息请求 + self.info_fetching_cache: List[Dict[str, any]] = [] + + # 信息结果缓存:存储已获取的信息结果,带TTL + self.info_fetched_cache: Dict[str, Dict[str, any]] = {} + # 结构:{person_id: {info_type: {"info": str, "ttl": int, "start_time": float, "person_name": str, "unknow": bool}}} + + # LLM模型配置 + self.llm_model = LLMRequest( + model=global_config.model.relation, + request_type="focus.real_time_info", + ) + + # 小模型用于即时信息提取 + self.instant_llm_model = LLMRequest( + model=global_config.model.utils_small, + request_type="focus.real_time_info.instant", + ) + + from src.chat.message_receive.chat_stream import get_chat_manager + name = get_chat_manager().get_stream_name(self.subheartflow_id) + self.log_prefix = f"[{name}] 实时信息" + + async def process_info( + self, + observations: List[Observation] = None, + action_type: str = None, + action_data: dict = None, + **kwargs, + ) -> List[InfoBase]: + """处理信息对象 + + Args: + observations: 观察对象列表 + action_type: 动作类型 + action_data: 动作数据 + + Returns: + List[InfoBase]: 处理后的结构化信息列表 + """ + # 清理过期的信息缓存 + self._cleanup_expired_cache() + + # 执行实时信息识别和提取 + relation_info_str = await self._identify_and_extract_info(observations, action_type, action_data) + + if relation_info_str: + relation_info = RelationInfo() + relation_info.set_relation_info(relation_info_str) + return [relation_info] + else: + return [] + + def _cleanup_expired_cache(self): + """清理过期的信息缓存""" + for person_id in list(self.info_fetched_cache.keys()): + for info_type in list(self.info_fetched_cache[person_id].keys()): + self.info_fetched_cache[person_id][info_type]["ttl"] -= 1 + if self.info_fetched_cache[person_id][info_type]["ttl"] <= 0: + del self.info_fetched_cache[person_id][info_type] + if not self.info_fetched_cache[person_id]: + del self.info_fetched_cache[person_id] + + async def _identify_and_extract_info( + self, + observations: List[Observation] = None, + action_type: str = None, + action_data: dict = None, + ) -> str: + """识别并提取用户信息 + + Args: + observations: 观察对象列表 + action_type: 动作类型 + action_data: 动作数据 + + Returns: + str: 提取到的用户信息字符串 + """ + # 只处理回复动作 + if action_type != "reply": + return None + + # 解析回复目标 + target_message = action_data.get("reply_to", "") + sender, text = self._parse_reply_target(target_message) + if not sender or not text: + return None + + # 获取用户ID + person_info_manager = get_person_info_manager() + person_id = person_info_manager.get_person_id_by_person_name(sender) + if not person_id: + logger.warning(f"{self.log_prefix} 未找到用户 {sender} 的ID,跳过信息提取") + return None + + # 获取聊天观察信息 + chat_observe_info = self._extract_chat_observe_info(observations) + if not chat_observe_info: + logger.debug(f"{self.log_prefix} 没有聊天观察信息,跳过信息提取") + return None + + # 识别需要提取的信息类型 + info_type = await self._identify_needed_info(chat_observe_info, sender, text) + + # 如果需要提取新信息,执行提取 + if info_type: + await self._extract_single_info(person_id, info_type, sender) + + # 组织并返回已知信息 + return self._organize_known_info() + + def _parse_reply_target(self, target_message: str) -> tuple: + """解析回复目标消息 + + Args: + target_message: 目标消息,格式为 "用户名:消息内容" + + Returns: + tuple: (发送者, 消息内容) + """ + if ":" in target_message: + parts = target_message.split(":", 1) + elif ":" in target_message: + parts = target_message.split(":", 1) + else: + logger.warning(f"{self.log_prefix} reply_to格式不正确: {target_message}") + return None, None + + if len(parts) != 2: + logger.warning(f"{self.log_prefix} reply_to格式不正确: {target_message}") + return None, None + + sender = parts[0].strip() + text = parts[1].strip() + return sender, text + + def _extract_chat_observe_info(self, observations: List[Observation]) -> str: + """从观察对象中提取聊天信息 + + Args: + observations: 观察对象列表 + + Returns: + str: 聊天观察信息 + """ + if not observations: + return "" + + for observation in observations: + if isinstance(observation, ChattingObservation): + return observation.get_observe_info() + return "" + + async def _identify_needed_info(self, chat_observe_info: str, sender: str, text: str) -> str: + """识别需要提取的信息类型 + + Args: + chat_observe_info: 聊天观察信息 + sender: 发送者 + text: 消息内容 + + Returns: + str: 需要提取的信息类型,如果不需要则返回None + """ + # 构建名称信息块 + nickname_str = ",".join(global_config.bot.alias_names) + name_block = f"你的名字是{global_config.bot.nickname},你的昵称有{nickname_str},有人也会用这些昵称称呼你。" + + # 构建已获取信息缓存块 + info_cache_block = self._build_info_cache_block() + + # 构建提示词 + prompt = (await global_prompt_manager.get_prompt_async("real_time_info_identify_prompt")).format( + chat_observe_info=chat_observe_info, + name_block=name_block, + info_cache_block=info_cache_block, + person_name=sender, + target_message=text, + ) + + try: + logger.debug(f"{self.log_prefix} 信息识别prompt: \n{prompt}\n") + content, _ = await self.llm_model.generate_response_async(prompt=prompt) + + if content: + content_json = json.loads(repair_json(content)) + + # 检查是否返回了不需要查询的标志 + if "none" in content_json: + logger.info(f"{self.log_prefix} LLM判断当前不需要查询任何信息:{content_json.get('none', '')}") + return None + + info_type = content_json.get("info_type") + if info_type: + # 记录信息获取请求 + self.info_fetching_cache.append({ + "person_id": get_person_info_manager().get_person_id_by_person_name(sender), + "person_name": sender, + "info_type": info_type, + "start_time": time.time(), + "forget": False, + }) + + # 限制缓存大小 + if len(self.info_fetching_cache) > 20: + self.info_fetching_cache.pop(0) + + logger.info(f"{self.log_prefix} 识别到需要调取用户 {sender} 的[{info_type}]信息") + return info_type + else: + logger.warning(f"{self.log_prefix} LLM未返回有效的info_type。响应: {content}") + + except Exception as e: + logger.error(f"{self.log_prefix} 执行信息识别LLM请求时出错: {e}") + logger.error(traceback.format_exc()) + + return None + + def _build_info_cache_block(self) -> str: + """构建已获取信息的缓存块""" + info_cache_block = "" + if self.info_fetching_cache: + # 对于每个(person_id, info_type)组合,只保留最新的记录 + latest_records = {} + for info_fetching in self.info_fetching_cache: + key = (info_fetching["person_id"], info_fetching["info_type"]) + if key not in latest_records or info_fetching["start_time"] > latest_records[key]["start_time"]: + latest_records[key] = info_fetching + + # 按时间排序并生成显示文本 + sorted_records = sorted(latest_records.values(), key=lambda x: x["start_time"]) + for info_fetching in sorted_records: + info_cache_block += ( + f"你已经调取了[{info_fetching['person_name']}]的[{info_fetching['info_type']}]信息\n" + ) + return info_cache_block + + async def _extract_single_info(self, person_id: str, info_type: str, person_name: str): + """提取单个信息类型 + + Args: + person_id: 用户ID + info_type: 信息类型 + person_name: 用户名 + """ + start_time = time.time() + person_info_manager = get_person_info_manager() + + # 首先检查 info_list 缓存 + info_list = await person_info_manager.get_value(person_id, "info_list") or [] + cached_info = None + + # 查找对应的 info_type + for info_item in info_list: + if info_item.get("info_type") == info_type: + cached_info = info_item.get("info_content") + logger.debug(f"{self.log_prefix} 在info_list中找到 {person_name} 的 {info_type} 信息: {cached_info}") + break + + # 如果缓存中有信息,直接使用 + if cached_info: + if person_id not in self.info_fetched_cache: + self.info_fetched_cache[person_id] = {} + + self.info_fetched_cache[person_id][info_type] = { + "info": cached_info, + "ttl": 2, + "start_time": start_time, + "person_name": person_name, + "unknow": cached_info == "none", + } + logger.info(f"{self.log_prefix} 记得 {person_name} 的 {info_type}: {cached_info}") + return + + # 如果缓存中没有,尝试从用户档案中提取 + try: + person_impression = await person_info_manager.get_value(person_id, "impression") + points = await person_info_manager.get_value(person_id, "points") + + # 构建印象信息块 + if person_impression: + person_impression_block = ( + f"<对{person_name}的总体了解>\n{person_impression}\n" + ) + else: + person_impression_block = "" + + # 构建要点信息块 + if points: + points_text = "\n".join([f"{point[2]}:{point[0]}" for point in points]) + points_text_block = f"<对{person_name}的近期了解>\n{points_text}\n" + else: + points_text_block = "" + + # 如果完全没有用户信息 + if not points_text_block and not person_impression_block: + if person_id not in self.info_fetched_cache: + self.info_fetched_cache[person_id] = {} + self.info_fetched_cache[person_id][info_type] = { + "info": "none", + "ttl": 2, + "start_time": start_time, + "person_name": person_name, + "unknow": True, + } + logger.info(f"{self.log_prefix} 完全不认识 {person_name}") + await self._save_info_to_cache(person_id, info_type, "none") + return + + # 使用LLM提取信息 + nickname_str = ",".join(global_config.bot.alias_names) + name_block = f"你的名字是{global_config.bot.nickname},你的昵称有{nickname_str},有人也会用这些昵称称呼你。" + + prompt = (await global_prompt_manager.get_prompt_async("real_time_fetch_person_info_prompt")).format( + name_block=name_block, + info_type=info_type, + person_impression_block=person_impression_block, + person_name=person_name, + info_json_str=f'"{info_type}": "有关{info_type}的信息内容"', + points_text_block=points_text_block, + ) + + # 使用小模型进行即时提取 + content, _ = await self.instant_llm_model.generate_response_async(prompt=prompt) + + if content: + content_json = json.loads(repair_json(content)) + if info_type in content_json: + info_content = content_json[info_type] + is_unknown = info_content == "none" or not info_content + + # 保存到运行时缓存 + if person_id not in self.info_fetched_cache: + self.info_fetched_cache[person_id] = {} + self.info_fetched_cache[person_id][info_type] = { + "info": "unknow" if is_unknown else info_content, + "ttl": 3, + "start_time": start_time, + "person_name": person_name, + "unknow": is_unknown, + } + + # 保存到持久化缓存 (info_list) + await self._save_info_to_cache(person_id, info_type, info_content if not is_unknown else "none") + + if not is_unknown: + logger.info(f"{self.log_prefix} 思考得到,{person_name} 的 {info_type}: {info_content}") + else: + logger.info(f"{self.log_prefix} 思考了也不知道{person_name} 的 {info_type} 信息") + else: + logger.warning(f"{self.log_prefix} 小模型返回空结果,获取 {person_name} 的 {info_type} 信息失败。") + + except Exception as e: + logger.error(f"{self.log_prefix} 执行信息提取时出错: {e}") + logger.error(traceback.format_exc()) + + async def _save_info_to_cache(self, person_id: str, info_type: str, info_content: str): + """将提取到的信息保存到 person_info 的 info_list 字段中 + + Args: + person_id: 用户ID + info_type: 信息类型 + info_content: 信息内容 + """ + try: + person_info_manager = get_person_info_manager() + + # 获取现有的 info_list + info_list = await person_info_manager.get_value(person_id, "info_list") or [] + + # 查找是否已存在相同 info_type 的记录 + found_index = -1 + for i, info_item in enumerate(info_list): + if isinstance(info_item, dict) and info_item.get("info_type") == info_type: + found_index = i + break + + # 创建新的信息记录 + new_info_item = { + "info_type": info_type, + "info_content": info_content, + } + + if found_index >= 0: + # 更新现有记录 + info_list[found_index] = new_info_item + logger.info(f"{self.log_prefix} [缓存更新] 更新 {person_id} 的 {info_type} 信息缓存") + else: + # 添加新记录 + info_list.append(new_info_item) + logger.info(f"{self.log_prefix} [缓存保存] 新增 {person_id} 的 {info_type} 信息缓存") + + # 保存更新后的 info_list + await person_info_manager.update_one_field(person_id, "info_list", info_list) + + except Exception as e: + logger.error(f"{self.log_prefix} [缓存保存] 保存信息到缓存失败: {e}") + logger.error(traceback.format_exc()) + + def _organize_known_info(self) -> str: + """组织已知的用户信息为字符串 + + Returns: + str: 格式化的用户信息字符串 + """ + persons_infos_str = "" + + if self.info_fetched_cache: + persons_with_known_info = [] # 有已知信息的人员 + persons_with_unknown_info = [] # 有未知信息的人员 + + for person_id in self.info_fetched_cache: + person_known_infos = [] + person_unknown_infos = [] + person_name = "" + + for info_type in self.info_fetched_cache[person_id]: + person_name = self.info_fetched_cache[person_id][info_type]["person_name"] + if not self.info_fetched_cache[person_id][info_type]["unknow"]: + info_content = self.info_fetched_cache[person_id][info_type]["info"] + person_known_infos.append(f"[{info_type}]:{info_content}") + else: + person_unknown_infos.append(info_type) + + # 如果有已知信息,添加到已知信息列表 + if person_known_infos: + known_info_str = ";".join(person_known_infos) + ";" + persons_with_known_info.append((person_name, known_info_str)) + + # 如果有未知信息,添加到未知信息列表 + if person_unknown_infos: + persons_with_unknown_info.append((person_name, person_unknown_infos)) + + # 先输出有已知信息的人员 + for person_name, known_info_str in persons_with_known_info: + persons_infos_str += f"你对 {person_name} 的了解:{known_info_str}\n" + + # 统一处理未知信息,避免重复的警告文本 + if persons_with_unknown_info: + unknown_persons_details = [] + for person_name, unknown_types in persons_with_unknown_info: + unknown_types_str = "、".join(unknown_types) + unknown_persons_details.append(f"{person_name}的[{unknown_types_str}]") + + if len(unknown_persons_details) == 1: + persons_infos_str += ( + f"你不了解{unknown_persons_details[0]}信息,不要胡乱回答,可以直接说不知道或忘记了;\n" + ) + else: + unknown_all_str = "、".join(unknown_persons_details) + persons_infos_str += f"你不了解{unknown_all_str}等信息,不要胡乱回答,可以直接说不知道或忘记了;\n" + + return persons_infos_str + + def get_cache_status(self) -> str: + """获取缓存状态信息,用于调试和监控""" + status_lines = [f"{self.log_prefix} 实时信息缓存状态:"] + status_lines.append(f"获取请求缓存数:{len(self.info_fetching_cache)}") + status_lines.append(f"结果缓存用户数:{len(self.info_fetched_cache)}") + + if self.info_fetched_cache: + for person_id, info_types in self.info_fetched_cache.items(): + person_name = list(info_types.values())[0]["person_name"] if info_types else person_id + status_lines.append(f" 用户 {person_name}: {len(info_types)} 个信息类型") + for info_type, info_data in info_types.items(): + ttl = info_data["ttl"] + unknow = info_data["unknow"] + status = "未知" if unknow else "已知" + status_lines.append(f" {info_type}: {status} (TTL: {ttl})") + + return "\n".join(status_lines) + + +# 初始化提示词 +init_real_time_info_prompts() \ No newline at end of file diff --git a/src/chat/focus_chat/info_processors/relationship_processor.py b/src/chat/focus_chat/info_processors/relationship_processor.py index e16def9fe..dff6d0931 100644 --- a/src/chat/focus_chat/info_processors/relationship_processor.py +++ b/src/chat/focus_chat/info_processors/relationship_processor.py @@ -5,18 +5,13 @@ from src.config.config import global_config import time import traceback from src.common.logger import get_logger -from src.chat.utils.prompt_builder import Prompt, global_prompt_manager from src.chat.message_receive.chat_stream import get_chat_manager from src.person_info.relationship_manager import get_relationship_manager from .base_processor import BaseProcessor from typing import List from typing import Dict from src.chat.focus_chat.info.info_base import InfoBase -from src.chat.focus_chat.info.relation_info import RelationInfo -from json_repair import repair_json from src.person_info.person_info import get_person_info_manager -import json -import asyncio from src.chat.utils.chat_message_builder import ( get_raw_msg_by_timestamp_with_chat, get_raw_msg_by_timestamp_with_chat_inclusive, @@ -36,62 +31,21 @@ SEGMENT_CLEANUP_CONFIG = { } -logger = get_logger("processor") +logger = get_logger("relationship_build_processor") -def init_prompt(): - relationship_prompt = """ -<聊天记录> -{chat_observe_info} - - -{name_block} -现在,你想要回复{person_name}的消息,消息内容是:{target_message}。请根据聊天记录和你要回复的消息,从你对{person_name}的了解中提取有关的信息: -1.你需要提供你想要提取的信息具体是哪方面的信息,例如:年龄,性别,对ta的印象,最近发生的事等等。 -2.请注意,请不要重复调取相同的信息,已经调取的信息如下: -{info_cache_block} -3.如果当前聊天记录中没有需要查询的信息,或者现有信息已经足够回复,请返回{{"none": "不需要查询"}} - -请以json格式输出,例如: - -{{ - "info_type": "信息类型", -}} - -请严格按照json输出格式,不要输出多余内容: -""" - Prompt(relationship_prompt, "relationship_prompt") - - fetch_info_prompt = """ +class RelationshipBuildProcessor(BaseProcessor): + """关系构建处理器 -{name_block} -以下是你在之前与{person_name}的交流中,产生的对{person_name}的了解: -{person_impression_block} -{points_text_block} - -请从中提取用户"{person_name}"的有关"{info_type}"信息 -请以json格式输出,例如: - -{{ - {info_json_str} -}} - -请严格按照json输出格式,不要输出多余内容: -""" - Prompt(fetch_info_prompt, "fetch_person_info_prompt") - - -class PersonImpressionpProcessor(BaseProcessor): - log_prefix = "关系" + 负责跟踪用户消息活动、管理消息段、触发关系构建和印象更新 + """ + + log_prefix = "关系构建" def __init__(self, subheartflow_id: str): super().__init__() self.subheartflow_id = subheartflow_id - self.info_fetching_cache: List[Dict[str, any]] = [] - self.info_fetched_cache: Dict[ - str, Dict[str, any] - ] = {} # {person_id: {"info": str, "ttl": int, "start_time": float}} # 新的消息段缓存结构: # {person_id: [{"start_time": float, "end_time": float, "last_msg_time": float, "message_count": int}, ...]} @@ -107,19 +61,8 @@ class PersonImpressionpProcessor(BaseProcessor): # 最后清理时间,用于定期清理老消息段 self.last_cleanup_time = 0.0 - self.llm_model = LLMRequest( - model=global_config.model.relation, - request_type="focus.relationship", - ) - - # 小模型用于即时信息提取 - self.instant_llm_model = LLMRequest( - model=global_config.model.utils_small, - request_type="focus.relationship.instant", - ) - name = get_chat_manager().get_stream_name(self.subheartflow_id) - self.log_prefix = f"[{name}] " + self.log_prefix = f"[{name}] 关系构建" # 加载持久化的缓存 self._load_cache() @@ -444,17 +387,7 @@ class PersonImpressionpProcessor(BaseProcessor): List[InfoBase]: 处理后的结构化信息列表 """ await self.build_relation(observations) - - relation_info_str = await self.relation_identify(observations, action_type, action_data) - - if relation_info_str: - relation_info = RelationInfo() - relation_info.set_relation_info(relation_info_str) - else: - relation_info = None - return None - - return [relation_info] + return [] # 关系构建处理器不返回信息,只负责后台构建关系 async def build_relation(self, observations: List[Observation] = None): """构建关系""" @@ -512,208 +445,12 @@ class PersonImpressionpProcessor(BaseProcessor): for person_id in users_to_build_relationship: segments = self.person_engaged_cache[person_id] # 异步执行关系构建 + import asyncio asyncio.create_task(self.update_impression_on_segments(person_id, self.subheartflow_id, segments)) # 移除已处理的用户缓存 del self.person_engaged_cache[person_id] self._save_cache() - async def relation_identify( - self, - observations: List[Observation] = None, - action_type: str = None, - action_data: dict = None, - ): - """ - 从人物获取信息 - """ - - chat_observe_info = "" - current_time = time.time() - if observations: - for observation in observations: - if isinstance(observation, ChattingObservation): - chat_observe_info = observation.get_observe_info() - # latest_message_time = observation.last_observe_time - # 从聊天观察中提取用户信息并更新消息段 - # 获取最新的非bot消息来更新消息段 - latest_messages = get_raw_msg_by_timestamp_with_chat( - self.subheartflow_id, - self.last_processed_message_time, - current_time, - limit=50, # 获取自上次处理后的消息 - ) - if latest_messages: - # 处理所有新的非bot消息 - for latest_msg in latest_messages: - user_id = latest_msg.get("user_id") - platform = latest_msg.get("user_platform") or latest_msg.get("chat_info_platform") - msg_time = latest_msg.get("time", 0) - - if ( - user_id - and platform - and user_id != global_config.bot.qq_account - and msg_time > self.last_processed_message_time - ): - from src.person_info.person_info import PersonInfoManager - - person_id = PersonInfoManager.get_person_id(platform, user_id) - self._update_message_segments(person_id, msg_time) - logger.debug( - f"{self.log_prefix} 更新用户 {person_id} 的消息段,消息时间:{time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(msg_time))}" - ) - self.last_processed_message_time = max(self.last_processed_message_time, msg_time) - break - - for person_id in list(self.info_fetched_cache.keys()): - for info_type in list(self.info_fetched_cache[person_id].keys()): - self.info_fetched_cache[person_id][info_type]["ttl"] -= 1 - if self.info_fetched_cache[person_id][info_type]["ttl"] <= 0: - del self.info_fetched_cache[person_id][info_type] - if not self.info_fetched_cache[person_id]: - del self.info_fetched_cache[person_id] - - if action_type != "reply": - return None - - target_message = action_data.get("reply_to", "") - - if ":" in target_message: - parts = target_message.split(":", 1) - elif ":" in target_message: - parts = target_message.split(":", 1) - else: - logger.warning(f"reply_to格式不正确: {target_message},跳过关系识别") - return None - - if len(parts) != 2: - logger.warning(f"reply_to格式不正确: {target_message},跳过关系识别") - return None - - sender = parts[0].strip() - text = parts[1].strip() - - person_info_manager = get_person_info_manager() - person_id = person_info_manager.get_person_id_by_person_name(sender) - - if not person_id: - logger.warning(f"未找到用户 {sender} 的ID,跳过关系识别") - return None - - nickname_str = ",".join(global_config.bot.alias_names) - name_block = f"你的名字是{global_config.bot.nickname},你的昵称有{nickname_str},有人也会用这些昵称称呼你。" - - info_cache_block = "" - if self.info_fetching_cache: - # 对于每个(person_id, info_type)组合,只保留最新的记录 - latest_records = {} - for info_fetching in self.info_fetching_cache: - key = (info_fetching["person_id"], info_fetching["info_type"]) - if key not in latest_records or info_fetching["start_time"] > latest_records[key]["start_time"]: - latest_records[key] = info_fetching - - # 按时间排序并生成显示文本 - sorted_records = sorted(latest_records.values(), key=lambda x: x["start_time"]) - for info_fetching in sorted_records: - info_cache_block += ( - f"你已经调取了[{info_fetching['person_name']}]的[{info_fetching['info_type']}]信息\n" - ) - - prompt = (await global_prompt_manager.get_prompt_async("relationship_prompt")).format( - chat_observe_info=chat_observe_info, - name_block=name_block, - info_cache_block=info_cache_block, - person_name=sender, - target_message=text, - ) - - try: - logger.info(f"{self.log_prefix} 人物信息prompt: \n{prompt}\n") - content, _ = await self.llm_model.generate_response_async(prompt=prompt) - if content: - # print(f"content: {content}") - content_json = json.loads(repair_json(content)) - - # 检查是否返回了不需要查询的标志 - if "none" in content_json: - logger.info(f"{self.log_prefix} LLM判断当前不需要查询任何信息:{content_json.get('none', '')}") - # 跳过新的信息提取,但仍会处理已有缓存 - else: - info_type = content_json.get("info_type") - if info_type: - self.info_fetching_cache.append( - { - "person_id": person_id, - "person_name": sender, - "info_type": info_type, - "start_time": time.time(), - "forget": False, - } - ) - if len(self.info_fetching_cache) > 20: - self.info_fetching_cache.pop(0) - - logger.info(f"{self.log_prefix} 调取用户 {sender} 的[{info_type}]信息。") - - # 执行信息提取 - await self._fetch_single_info_instant(person_id, info_type, time.time()) - else: - logger.warning(f"{self.log_prefix} LLM did not return a valid info_type. Response: {content}") - - except Exception as e: - logger.error(f"{self.log_prefix} 执行LLM请求或处理响应时出错: {e}") - logger.error(traceback.format_exc()) - - # 7. 合并缓存和新处理的信息 - persons_infos_str = "" - # 处理已获取到的信息 - if self.info_fetched_cache: - persons_with_known_info = [] # 有已知信息的人员 - persons_with_unknown_info = [] # 有未知信息的人员 - - for person_id in self.info_fetched_cache: - person_known_infos = [] - person_unknown_infos = [] - person_name = "" - - for info_type in self.info_fetched_cache[person_id]: - person_name = self.info_fetched_cache[person_id][info_type]["person_name"] - if not self.info_fetched_cache[person_id][info_type]["unknow"]: - info_content = self.info_fetched_cache[person_id][info_type]["info"] - person_known_infos.append(f"[{info_type}]:{info_content}") - else: - person_unknown_infos.append(info_type) - - # 如果有已知信息,添加到已知信息列表 - if person_known_infos: - known_info_str = ";".join(person_known_infos) + ";" - persons_with_known_info.append((person_name, known_info_str)) - - # 如果有未知信息,添加到未知信息列表 - if person_unknown_infos: - persons_with_unknown_info.append((person_name, person_unknown_infos)) - - # 先输出有已知信息的人员 - for person_name, known_info_str in persons_with_known_info: - persons_infos_str += f"你对 {person_name} 的了解:{known_info_str}\n" - - # 统一处理未知信息,避免重复的警告文本 - if persons_with_unknown_info: - unknown_persons_details = [] - for person_name, unknown_types in persons_with_unknown_info: - unknown_types_str = "、".join(unknown_types) - unknown_persons_details.append(f"{person_name}的[{unknown_types_str}]") - - if len(unknown_persons_details) == 1: - persons_infos_str += ( - f"你不了解{unknown_persons_details[0]}信息,不要胡乱回答,可以直接说不知道或忘记了;\n" - ) - else: - unknown_all_str = "、".join(unknown_persons_details) - persons_infos_str += f"你不了解{unknown_all_str}等信息,不要胡乱回答,可以直接说不知道或忘记了;\n" - - return persons_infos_str - # ================================ # 关系构建模块 # 负责触发关系构建、整合消息段、更新用户印象 @@ -783,169 +520,3 @@ class PersonImpressionpProcessor(BaseProcessor): except Exception as e: logger.error(f"为 {person_id} 更新印象时发生错误: {e}") logger.error(traceback.format_exc()) - - # ================================ - # 信息调取模块 - # 负责实时分析对话需求、提取用户信息、管理信息缓存 - # ================================ - - async def _fetch_single_info_instant(self, person_id: str, info_type: str, start_time: float): - """ - 使用小模型提取单个信息类型 - """ - person_info_manager = get_person_info_manager() - - # 首先检查 info_list 缓存 - info_list = await person_info_manager.get_value(person_id, "info_list") or [] - cached_info = None - person_name = await person_info_manager.get_value(person_id, "person_name") - - # print(f"info_list: {info_list}") - - # 查找对应的 info_type - for info_item in info_list: - if info_item.get("info_type") == info_type: - cached_info = info_item.get("info_content") - logger.debug(f"{self.log_prefix} 在info_list中找到 {person_name} 的 {info_type} 信息: {cached_info}") - break - - # 如果缓存中有信息,直接使用 - if cached_info: - if person_id not in self.info_fetched_cache: - self.info_fetched_cache[person_id] = {} - - self.info_fetched_cache[person_id][info_type] = { - "info": cached_info, - "ttl": 2, - "start_time": start_time, - "person_name": person_name, - "unknow": cached_info == "none", - } - logger.info(f"{self.log_prefix} 记得 {person_name} 的 {info_type}: {cached_info}") - return - - try: - person_name = await person_info_manager.get_value(person_id, "person_name") - person_impression = await person_info_manager.get_value(person_id, "impression") - if person_impression: - person_impression_block = ( - f"<对{person_name}的总体了解>\n{person_impression}\n" - ) - else: - person_impression_block = "" - - points = await person_info_manager.get_value(person_id, "points") - if points: - points_text = "\n".join([f"{point[2]}:{point[0]}" for point in points]) - points_text_block = f"<对{person_name}的近期了解>\n{points_text}\n" - else: - points_text_block = "" - - if not points_text_block and not person_impression_block: - if person_id not in self.info_fetched_cache: - self.info_fetched_cache[person_id] = {} - self.info_fetched_cache[person_id][info_type] = { - "info": "none", - "ttl": 2, - "start_time": start_time, - "person_name": person_name, - "unknow": True, - } - logger.info(f"{self.log_prefix} 完全不认识 {person_name}") - await self._save_info_to_cache(person_id, info_type, "none") - return - - nickname_str = ",".join(global_config.bot.alias_names) - name_block = f"你的名字是{global_config.bot.nickname},你的昵称有{nickname_str},有人也会用这些昵称称呼你。" - prompt = (await global_prompt_manager.get_prompt_async("fetch_person_info_prompt")).format( - name_block=name_block, - info_type=info_type, - person_impression_block=person_impression_block, - person_name=person_name, - info_json_str=f'"{info_type}": "有关{info_type}的信息内容"', - points_text_block=points_text_block, - ) - except Exception: - logger.error(traceback.format_exc()) - return - - try: - # 使用小模型进行即时提取 - content, _ = await self.instant_llm_model.generate_response_async(prompt=prompt) - - if content: - content_json = json.loads(repair_json(content)) - if info_type in content_json: - info_content = content_json[info_type] - is_unknown = info_content == "none" or not info_content - - # 保存到运行时缓存 - if person_id not in self.info_fetched_cache: - self.info_fetched_cache[person_id] = {} - self.info_fetched_cache[person_id][info_type] = { - "info": "unknow" if is_unknown else info_content, - "ttl": 3, - "start_time": start_time, - "person_name": person_name, - "unknow": is_unknown, - } - - # 保存到持久化缓存 (info_list) - await self._save_info_to_cache(person_id, info_type, info_content if not is_unknown else "none") - - if not is_unknown: - logger.info(f"{self.log_prefix} 思考得到,{person_name} 的 {info_type}: {content}") - else: - logger.info(f"{self.log_prefix} 思考了也不知道{person_name} 的 {info_type} 信息") - else: - logger.warning(f"{self.log_prefix} 小模型返回空结果,获取 {person_name} 的 {info_type} 信息失败。") - except Exception as e: - logger.error(f"{self.log_prefix} 执行小模型请求获取用户信息时出错: {e}") - logger.error(traceback.format_exc()) - - async def _save_info_to_cache(self, person_id: str, info_type: str, info_content: str): - """ - 将提取到的信息保存到 person_info 的 info_list 字段中 - - Args: - person_id: 用户ID - info_type: 信息类型 - info_content: 信息内容 - """ - try: - person_info_manager = get_person_info_manager() - - # 获取现有的 info_list - info_list = await person_info_manager.get_value(person_id, "info_list") or [] - - # 查找是否已存在相同 info_type 的记录 - found_index = -1 - for i, info_item in enumerate(info_list): - if isinstance(info_item, dict) and info_item.get("info_type") == info_type: - found_index = i - break - - # 创建新的信息记录 - new_info_item = { - "info_type": info_type, - "info_content": info_content, - } - - if found_index >= 0: - # 更新现有记录 - info_list[found_index] = new_info_item - logger.info(f"{self.log_prefix} [缓存更新] 更新 {person_id} 的 {info_type} 信息缓存") - else: - # 添加新记录 - info_list.append(new_info_item) - logger.info(f"{self.log_prefix} [缓存保存] 新增 {person_id} 的 {info_type} 信息缓存") - - # 保存更新后的 info_list - await person_info_manager.update_one_field(person_id, "info_list", info_list) - - except Exception as e: - logger.error(f"{self.log_prefix} [缓存保存] 保存信息到缓存失败: {e}") - logger.error(traceback.format_exc()) - - -init_prompt() diff --git a/src/chat/focus_chat/planners/planner_simple.py b/src/chat/focus_chat/planners/planner_simple.py index e891a9769..20f41c711 100644 --- a/src/chat/focus_chat/planners/planner_simple.py +++ b/src/chat/focus_chat/planners/planner_simple.py @@ -236,14 +236,6 @@ class ActionPlanner(BasePlanner): action_data["loop_start_time"] = loop_start_time - memory_str = "" - if running_memorys: - memory_str = "以下是当前在聊天中,你回忆起的记忆:\n" - for running_memory in running_memorys: - memory_str += f"{running_memory['content']}\n" - if memory_str: - action_data["memory_block"] = memory_str - # 对于reply动作不需要额外处理,因为相关字段已经在上面的循环中添加到action_data if extracted_action not in current_available_actions: diff --git a/src/config/official_configs.py b/src/config/official_configs.py index 6957884f4..df64e0f10 100644 --- a/src/config/official_configs.py +++ b/src/config/official_configs.py @@ -329,7 +329,13 @@ class FocusChatProcessorConfig(ConfigBase): """专注聊天处理器配置类""" person_impression_processor: bool = True - """是否启用关系识别处理器""" + """是否启用关系识别处理器(已废弃,为了兼容性保留)""" + + relationship_build_processor: bool = True + """是否启用关系构建处理器""" + + real_time_info_processor: bool = True + """是否启用实时信息提取处理器""" tool_use_processor: bool = True """是否启用工具使用处理器""" From 087f4a6cbfdd65f35c8910e9994aee99ed127569 Mon Sep 17 00:00:00 2001 From: "github-actions[bot]" Date: Tue, 1 Jul 2025 05:46:54 +0000 Subject: [PATCH 05/12] =?UTF-8?q?=F0=9F=A4=96=20=E8=87=AA=E5=8A=A8?= =?UTF-8?q?=E6=A0=BC=E5=BC=8F=E5=8C=96=E4=BB=A3=E7=A0=81=20[skip=20ci]?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/chat/focus_chat/heartFC_chat.py | 6 +- .../real_time_info_processor.py | 89 ++++++++++--------- .../info_processors/relationship_processor.py | 6 +- 3 files changed, 52 insertions(+), 49 deletions(-) diff --git a/src/chat/focus_chat/heartFC_chat.py b/src/chat/focus_chat/heartFC_chat.py index 78ca00192..b3fedc4d5 100644 --- a/src/chat/focus_chat/heartFC_chat.py +++ b/src/chat/focus_chat/heartFC_chat.py @@ -139,14 +139,14 @@ class HeartFChatting: # 检查全局关系开关 if not global_config.relationship.enable_relationship: continue - + # 检查处理器特定配置,同时支持向后兼容 processor_enabled = getattr(config_processor_settings, config_key, True) - + # 向后兼容:如果旧的person_impression_processor为True,则启用两个新处理器 if not processor_enabled and getattr(config_processor_settings, "person_impression_processor", True): processor_enabled = True - + if processor_enabled: self.enabled_post_planning_processor_names.append(proc_name) else: diff --git a/src/chat/focus_chat/info_processors/real_time_info_processor.py b/src/chat/focus_chat/info_processors/real_time_info_processor.py index 6536ef6ec..a25fcf7cb 100644 --- a/src/chat/focus_chat/info_processors/real_time_info_processor.py +++ b/src/chat/focus_chat/info_processors/real_time_info_processor.py @@ -63,20 +63,20 @@ def init_real_time_info_prompts(): class RealTimeInfoProcessor(BaseProcessor): """实时信息提取处理器 - + 负责从对话中识别需要的用户信息,并从用户档案中实时提取相关信息 """ - + log_prefix = "实时信息" def __init__(self, subheartflow_id: str): super().__init__() - + self.subheartflow_id = subheartflow_id - + # 信息获取缓存:记录正在获取的信息请求 self.info_fetching_cache: List[Dict[str, any]] = [] - + # 信息结果缓存:存储已获取的信息结果,带TTL self.info_fetched_cache: Dict[str, Dict[str, any]] = {} # 结构:{person_id: {info_type: {"info": str, "ttl": int, "start_time": float, "person_name": str, "unknow": bool}}} @@ -94,6 +94,7 @@ class RealTimeInfoProcessor(BaseProcessor): ) from src.chat.message_receive.chat_stream import get_chat_manager + name = get_chat_manager().get_stream_name(self.subheartflow_id) self.log_prefix = f"[{name}] 实时信息" @@ -105,21 +106,21 @@ class RealTimeInfoProcessor(BaseProcessor): **kwargs, ) -> List[InfoBase]: """处理信息对象 - + Args: observations: 观察对象列表 action_type: 动作类型 action_data: 动作数据 - + Returns: List[InfoBase]: 处理后的结构化信息列表 """ # 清理过期的信息缓存 self._cleanup_expired_cache() - + # 执行实时信息识别和提取 relation_info_str = await self._identify_and_extract_info(observations, action_type, action_data) - + if relation_info_str: relation_info = RelationInfo() relation_info.set_relation_info(relation_info_str) @@ -144,12 +145,12 @@ class RealTimeInfoProcessor(BaseProcessor): action_data: dict = None, ) -> str: """识别并提取用户信息 - + Args: observations: 观察对象列表 action_type: 动作类型 action_data: 动作数据 - + Returns: str: 提取到的用户信息字符串 """ @@ -178,7 +179,7 @@ class RealTimeInfoProcessor(BaseProcessor): # 识别需要提取的信息类型 info_type = await self._identify_needed_info(chat_observe_info, sender, text) - + # 如果需要提取新信息,执行提取 if info_type: await self._extract_single_info(person_id, info_type, sender) @@ -188,10 +189,10 @@ class RealTimeInfoProcessor(BaseProcessor): def _parse_reply_target(self, target_message: str) -> tuple: """解析回复目标消息 - + Args: target_message: 目标消息,格式为 "用户名:消息内容" - + Returns: tuple: (发送者, 消息内容) """ @@ -213,16 +214,16 @@ class RealTimeInfoProcessor(BaseProcessor): def _extract_chat_observe_info(self, observations: List[Observation]) -> str: """从观察对象中提取聊天信息 - + Args: observations: 观察对象列表 - + Returns: str: 聊天观察信息 """ if not observations: return "" - + for observation in observations: if isinstance(observation, ChattingObservation): return observation.get_observe_info() @@ -230,12 +231,12 @@ class RealTimeInfoProcessor(BaseProcessor): async def _identify_needed_info(self, chat_observe_info: str, sender: str, text: str) -> str: """识别需要提取的信息类型 - + Args: chat_observe_info: 聊天观察信息 sender: 发送者 text: 消息内容 - + Returns: str: 需要提取的信息类型,如果不需要则返回None """ @@ -258,39 +259,41 @@ class RealTimeInfoProcessor(BaseProcessor): try: logger.debug(f"{self.log_prefix} 信息识别prompt: \n{prompt}\n") content, _ = await self.llm_model.generate_response_async(prompt=prompt) - + if content: content_json = json.loads(repair_json(content)) - + # 检查是否返回了不需要查询的标志 if "none" in content_json: logger.info(f"{self.log_prefix} LLM判断当前不需要查询任何信息:{content_json.get('none', '')}") return None - + info_type = content_json.get("info_type") if info_type: # 记录信息获取请求 - self.info_fetching_cache.append({ - "person_id": get_person_info_manager().get_person_id_by_person_name(sender), - "person_name": sender, - "info_type": info_type, - "start_time": time.time(), - "forget": False, - }) - + self.info_fetching_cache.append( + { + "person_id": get_person_info_manager().get_person_id_by_person_name(sender), + "person_name": sender, + "info_type": info_type, + "start_time": time.time(), + "forget": False, + } + ) + # 限制缓存大小 if len(self.info_fetching_cache) > 20: self.info_fetching_cache.pop(0) - + logger.info(f"{self.log_prefix} 识别到需要调取用户 {sender} 的[{info_type}]信息") return info_type else: logger.warning(f"{self.log_prefix} LLM未返回有效的info_type。响应: {content}") - + except Exception as e: logger.error(f"{self.log_prefix} 执行信息识别LLM请求时出错: {e}") logger.error(traceback.format_exc()) - + return None def _build_info_cache_block(self) -> str: @@ -314,7 +317,7 @@ class RealTimeInfoProcessor(BaseProcessor): async def _extract_single_info(self, person_id: str, info_type: str, person_name: str): """提取单个信息类型 - + Args: person_id: 用户ID info_type: 信息类型 @@ -353,7 +356,7 @@ class RealTimeInfoProcessor(BaseProcessor): try: person_impression = await person_info_manager.get_value(person_id, "impression") points = await person_info_manager.get_value(person_id, "points") - + # 构建印象信息块 if person_impression: person_impression_block = ( @@ -387,7 +390,7 @@ class RealTimeInfoProcessor(BaseProcessor): # 使用LLM提取信息 nickname_str = ",".join(global_config.bot.alias_names) name_block = f"你的名字是{global_config.bot.nickname},你的昵称有{nickname_str},有人也会用这些昵称称呼你。" - + prompt = (await global_prompt_manager.get_prompt_async("real_time_fetch_person_info_prompt")).format( name_block=name_block, info_type=info_type, @@ -426,14 +429,14 @@ class RealTimeInfoProcessor(BaseProcessor): logger.info(f"{self.log_prefix} 思考了也不知道{person_name} 的 {info_type} 信息") else: logger.warning(f"{self.log_prefix} 小模型返回空结果,获取 {person_name} 的 {info_type} 信息失败。") - + except Exception as e: logger.error(f"{self.log_prefix} 执行信息提取时出错: {e}") logger.error(traceback.format_exc()) async def _save_info_to_cache(self, person_id: str, info_type: str, info_content: str): """将提取到的信息保存到 person_info 的 info_list 字段中 - + Args: person_id: 用户ID info_type: 信息类型 @@ -476,12 +479,12 @@ class RealTimeInfoProcessor(BaseProcessor): def _organize_known_info(self) -> str: """组织已知的用户信息为字符串 - + Returns: str: 格式化的用户信息字符串 """ persons_infos_str = "" - + if self.info_fetched_cache: persons_with_known_info = [] # 有已知信息的人员 persons_with_unknown_info = [] # 有未知信息的人员 @@ -534,7 +537,7 @@ class RealTimeInfoProcessor(BaseProcessor): status_lines = [f"{self.log_prefix} 实时信息缓存状态:"] status_lines.append(f"获取请求缓存数:{len(self.info_fetching_cache)}") status_lines.append(f"结果缓存用户数:{len(self.info_fetched_cache)}") - + if self.info_fetched_cache: for person_id, info_types in self.info_fetched_cache.items(): person_name = list(info_types.values())[0]["person_name"] if info_types else person_id @@ -544,9 +547,9 @@ class RealTimeInfoProcessor(BaseProcessor): unknow = info_data["unknow"] status = "未知" if unknow else "已知" status_lines.append(f" {info_type}: {status} (TTL: {ttl})") - + return "\n".join(status_lines) # 初始化提示词 -init_real_time_info_prompts() \ No newline at end of file +init_real_time_info_prompts() diff --git a/src/chat/focus_chat/info_processors/relationship_processor.py b/src/chat/focus_chat/info_processors/relationship_processor.py index dff6d0931..5b945fdf1 100644 --- a/src/chat/focus_chat/info_processors/relationship_processor.py +++ b/src/chat/focus_chat/info_processors/relationship_processor.py @@ -1,6 +1,5 @@ from src.chat.heart_flow.observation.chatting_observation import ChattingObservation from src.chat.heart_flow.observation.observation import Observation -from src.llm_models.utils_model import LLMRequest from src.config.config import global_config import time import traceback @@ -36,10 +35,10 @@ logger = get_logger("relationship_build_processor") class RelationshipBuildProcessor(BaseProcessor): """关系构建处理器 - + 负责跟踪用户消息活动、管理消息段、触发关系构建和印象更新 """ - + log_prefix = "关系构建" def __init__(self, subheartflow_id: str): @@ -446,6 +445,7 @@ class RelationshipBuildProcessor(BaseProcessor): segments = self.person_engaged_cache[person_id] # 异步执行关系构建 import asyncio + asyncio.create_task(self.update_impression_on_segments(person_id, self.subheartflow_id, segments)) # 移除已处理的用户缓存 del self.person_engaged_cache[person_id] From cae015fcfaa4130905e8e5cafe868ce0f8bd4b96 Mon Sep 17 00:00:00 2001 From: SengokuCola <1026294844@qq.com> Date: Tue, 1 Jul 2025 14:46:09 +0800 Subject: [PATCH 06/12] =?UTF-8?q?=E7=A7=BB=E9=99=A4=E5=85=B3=E7=B3=BB?= =?UTF-8?q?=E5=A4=84=E7=90=86=E5=99=A8=EF=BC=8C=E8=BD=AC=E4=B8=BA=E5=9C=A8?= =?UTF-8?q?replyer=E4=B8=AD=E6=8F=90=E5=8F=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/chat/focus_chat/heartFC_chat.py | 39 +- src/chat/replyer/default_generator.py | 273 +++++++------- .../relationship_builder.py} | 174 +++------ .../relationship_builder_manager.py | 103 +++++ .../relationship_fetcher.py} | 353 +++++++----------- 5 files changed, 441 insertions(+), 501 deletions(-) rename src/{chat/focus_chat/info_processors/relationship_processor.py => person_info/relationship_builder.py} (80%) create mode 100644 src/person_info/relationship_builder_manager.py rename src/{chat/focus_chat/info_processors/real_time_info_processor.py => person_info/relationship_fetcher.py} (72%) diff --git a/src/chat/focus_chat/heartFC_chat.py b/src/chat/focus_chat/heartFC_chat.py index 78ca00192..e06f9238f 100644 --- a/src/chat/focus_chat/heartFC_chat.py +++ b/src/chat/focus_chat/heartFC_chat.py @@ -13,8 +13,6 @@ from src.chat.heart_flow.observation.observation import Observation from src.chat.focus_chat.heartFC_Cycleinfo import CycleDetail from src.chat.focus_chat.info.info_base import InfoBase from src.chat.focus_chat.info_processors.chattinginfo_processor import ChattingInfoProcessor -from src.chat.focus_chat.info_processors.relationship_processor import RelationshipBuildProcessor -from src.chat.focus_chat.info_processors.real_time_info_processor import RealTimeInfoProcessor from src.chat.focus_chat.info_processors.working_memory_processor import WorkingMemoryProcessor from src.chat.heart_flow.observation.hfcloop_observation import HFCloopObservation from src.chat.heart_flow.observation.working_observation import WorkingMemoryObservation @@ -32,6 +30,7 @@ from src.chat.focus_chat.hfc_performance_logger import HFCPerformanceLogger from src.chat.focus_chat.hfc_version_manager import get_hfc_version from src.chat.focus_chat.info.relation_info import RelationInfo from src.chat.focus_chat.info.structured_info import StructuredInfo +from src.person_info.relationship_builder_manager import relationship_builder_manager install(extra_lines=3) @@ -57,8 +56,6 @@ PROCESSOR_CLASSES = { # 定义后期处理器映射:在规划后、动作执行前运行的处理器 POST_PLANNING_PROCESSOR_CLASSES = { "ToolProcessor": (ToolProcessor, "tool_use_processor"), - "RelationshipBuildProcessor": (RelationshipBuildProcessor, "relationship_build_processor"), - "RealTimeInfoProcessor": (RealTimeInfoProcessor, "real_time_info_processor"), } logger = get_logger("hfc") # Logger Name Changed @@ -110,6 +107,8 @@ class HeartFChatting: self.log_prefix = f"[{get_chat_manager().get_stream_name(self.stream_id) or self.stream_id}]" self.memory_activator = MemoryActivator() + + self.relationship_builder = relationship_builder_manager.get_or_create_builder(self.stream_id) # 新增:消息计数器和疲惫阈值 self._message_count = 0 # 发送的消息计数 @@ -135,24 +134,8 @@ class HeartFChatting: self.enabled_post_planning_processor_names = [] for proc_name, (_proc_class, config_key) in POST_PLANNING_PROCESSOR_CLASSES.items(): # 对于关系相关处理器,需要同时检查关系配置项 - if proc_name in ["RelationshipBuildProcessor", "RealTimeInfoProcessor"]: - # 检查全局关系开关 - if not global_config.relationship.enable_relationship: - continue - - # 检查处理器特定配置,同时支持向后兼容 - processor_enabled = getattr(config_processor_settings, config_key, True) - - # 向后兼容:如果旧的person_impression_processor为True,则启用两个新处理器 - if not processor_enabled and getattr(config_processor_settings, "person_impression_processor", True): - processor_enabled = True - - if processor_enabled: - self.enabled_post_planning_processor_names.append(proc_name) - else: - # 其他后期处理器的逻辑 - if not config_key or getattr(config_processor_settings, config_key, True): - self.enabled_post_planning_processor_names.append(proc_name) + if not config_key or getattr(config_processor_settings, config_key, True): + self.enabled_post_planning_processor_names.append(proc_name) # logger.info(f"{self.log_prefix} 将启用的处理器: {self.enabled_processor_names}") # logger.info(f"{self.log_prefix} 将启用的后期处理器: {self.enabled_post_planning_processor_names}") @@ -754,17 +737,13 @@ class HeartFChatting: # 将后期处理器的结果整合到 action_data 中 updated_action_data = action_data.copy() - relation_info = "" + structured_info = "" for info in all_post_plan_info: - if isinstance(info, RelationInfo): - relation_info = info.get_processed_info() - elif isinstance(info, StructuredInfo): + if isinstance(info, StructuredInfo): structured_info = info.get_processed_info() - if relation_info: - updated_action_data["relation_info"] = relation_info if structured_info: updated_action_data["structured_info"] = structured_info @@ -793,10 +772,10 @@ class HeartFChatting: "observations": self.observations, } - # 根据配置决定是否并行执行调整动作、回忆和处理器阶段 + await self.relationship_builder.build_relation() # 并行执行调整动作、回忆和处理器阶段 - with Timer("并行调整动作、处理", cycle_timers): + with Timer("调整动作、处理", cycle_timers): # 创建并行任务 async def modify_actions_task(): # 调用完整的动作修改流程 diff --git a/src/chat/replyer/default_generator.py b/src/chat/replyer/default_generator.py index 7a2cd5b5f..bbdcca3fb 100644 --- a/src/chat/replyer/default_generator.py +++ b/src/chat/replyer/default_generator.py @@ -19,6 +19,7 @@ from src.chat.express.exprssion_learner import get_expression_learner import time from src.chat.express.expression_selector import expression_selector from src.manager.mood_manager import mood_manager +from src.person_info.relationship_fetcher import relationship_fetcher_manager import random import ast from src.person_info.person_info import get_person_info_manager @@ -322,101 +323,33 @@ class DefaultReplyer: traceback.print_exc() return False, None - async def build_prompt_reply_context(self, reply_data=None, available_actions: List[str] = None) -> str: - """ - 构建回复器上下文 - - Args: - reply_data: 回复数据 - replay_data 包含以下字段: - structured_info: 结构化信息,一般是工具调用获得的信息 - relation_info: 人物关系信息 - reply_to: 回复对象 - memory_info: 记忆信息 - extra_info/extra_info_block: 额外信息 - available_actions: 可用动作 - - Returns: - str: 构建好的上下文 - """ - if available_actions is None: - available_actions = [] - chat_stream = self.chat_stream - chat_id = chat_stream.stream_id + async def build_relation_info(self,reply_data = None,chat_history = None): + relationship_fetcher = relationship_fetcher_manager.get_fetcher(self.chat_stream.stream_id) + if not reply_data: + return "" + reply_to = reply_data.get("reply_to", "") + sender, text = self._parse_reply_target(reply_to) + if not sender or not text: + return "" + + # 获取用户ID person_info_manager = get_person_info_manager() - bot_person_id = person_info_manager.get_person_id("system", "bot_id") - - is_group_chat = bool(chat_stream.group_info) - - structured_info = reply_data.get("structured_info", "") - relation_info = reply_data.get("relation_info", "") - reply_to = reply_data.get("reply_to", "none") - - # 优先使用 extra_info_block,没有则用 extra_info - extra_info_block = reply_data.get("extra_info", "") or reply_data.get("extra_info_block", "") - - sender = "" - target = "" - if ":" in reply_to or ":" in reply_to: - # 使用正则表达式匹配中文或英文冒号 - parts = re.split(pattern=r"[::]", string=reply_to, maxsplit=1) - if len(parts) == 2: - sender = parts[0].strip() - target = parts[1].strip() - - # 构建action描述 (如果启用planner) - action_descriptions = "" - # logger.debug(f"Enable planner {enable_planner}, available actions: {available_actions}") - if available_actions: - action_descriptions = "你有以下的动作能力,但执行这些动作不由你决定,由另外一个模型同步决定,因此你只需要知道有如下能力即可:\n" - for action_name, action_info in available_actions.items(): - action_description = action_info.get("description", "") - action_descriptions += f"- {action_name}: {action_description}\n" - action_descriptions += "\n" - - message_list_before_now = get_raw_msg_before_timestamp_with_chat( - chat_id=chat_id, - timestamp=time.time(), - limit=global_config.focus_chat.observation_context_size, - ) - # print(f"message_list_before_now: {message_list_before_now}") - chat_talking_prompt = build_readable_messages( - message_list_before_now, - replace_bot_name=True, - merge_messages=False, - timestamp_mode="normal_no_YMD", - read_mark=0.0, - truncate=True, - show_actions=True, - ) - # print(f"chat_talking_prompt: {chat_talking_prompt}") - - message_list_before_now_half = get_raw_msg_before_timestamp_with_chat( - chat_id=chat_id, - timestamp=time.time(), - limit=int(global_config.focus_chat.observation_context_size * 0.5), - ) - chat_talking_prompt_half = build_readable_messages( - message_list_before_now_half, - replace_bot_name=True, - merge_messages=False, - timestamp_mode="relative", - read_mark=0.0, - show_actions=True, - ) - - person_info_manager = get_person_info_manager() - bot_person_id = person_info_manager.get_person_id("system", "bot_id") - - is_group_chat = bool(chat_stream.group_info) - + person_id = person_info_manager.get_person_id_by_person_name(sender) + if not person_id: + logger.warning(f"{self.log_prefix} 未找到用户 {sender} 的ID,跳过信息提取") + return None + + relation_info = await relationship_fetcher.build_relation_info(person_id,text,chat_history) + return relation_info + + async def build_expression_habits(self,chat_history,target): style_habbits = [] grammar_habbits = [] # 使用从处理器传来的选中表达方式 # LLM模式:调用LLM选择5-10个,然后随机选5个 selected_expressions = await expression_selector.select_suitable_expressions_llm( - chat_id, chat_talking_prompt_half, max_num=12, min_num=2, target_message=target + self.chat_stream.stream_id, chat_history, max_num=12, min_num=2, target_message=target ) if selected_expressions: @@ -441,45 +374,38 @@ class DefaultReplyer: expression_habits_block += f"你可以参考以下的语言习惯,如果情景合适就使用,不要盲目使用,不要生硬使用,而是结合到表达中:\n{style_habbits_str}\n\n" if grammar_habbits_str.strip(): expression_habits_block += f"请你根据情景使用以下句法:\n{grammar_habbits_str}\n" + + return expression_habits_block + + async def build_memory_block(self,chat_history,target): + running_memorys = await self.memory_activator.activate_memory_with_chat_history( + chat_id=self.chat_stream.stream_id, target_message=target, chat_history_prompt=chat_history + ) - # 在回复器内部直接激活记忆 - try: - # 注意:这里的 observations 是一个简化的版本,只包含聊天记录 - # 如果 MemoryActivator 依赖更复杂的观察器,需要调整 - # observations_for_memory = [ChattingObservation(chat_id=chat_stream.stream_id)] - # for obs in observations_for_memory: - # await obs.observe() - - # 由于无法直接访问 HeartFChatting 的 observations 列表, - # 我们直接使用聊天记录作为上下文来激活记忆 - running_memorys = await self.memory_activator.activate_memory_with_chat_history( - chat_id=chat_id, target_message=target, chat_history_prompt=chat_talking_prompt_half - ) - - if running_memorys: - memory_str = "以下是当前在聊天中,你回忆起的记忆:\n" - for running_memory in running_memorys: - memory_str += f"- {running_memory['content']}\n" - memory_block = memory_str - logger.info(f"{self.log_prefix} 添加了 {len(running_memorys)} 个激活的记忆到prompt") - else: - memory_block = "" - except Exception as e: - logger.error(f"{self.log_prefix} 激活记忆时出错: {e}", exc_info=True) + if running_memorys: + memory_str = "以下是当前在聊天中,你回忆起的记忆:\n" + for running_memory in running_memorys: + memory_str += f"- {running_memory['content']}\n" + memory_block = memory_str + logger.info(f"{self.log_prefix} 添加了 {len(running_memorys)} 个激活的记忆到prompt") + else: memory_block = "" + + return memory_block - if structured_info: - structured_info_block = ( - f"以下是你了解的额外信息信息,现在请你阅读以下内容,进行决策\n{structured_info}\n以上是一些额外的信息。" - ) - else: - structured_info_block = "" - - if extra_info_block: - extra_info_block = f"以下是你在回复时需要参考的信息,现在请你阅读以下内容,进行决策\n{extra_info_block}\n以上是你在回复时需要参考的信息,现在请你阅读以下内容,进行决策" - else: - extra_info_block = "" - + + async def _parse_reply_target(self, target_message: str) -> tuple: + sender = "" + target = "" + if ":" in target_message or ":" in target_message: + # 使用正则表达式匹配中文或英文冒号 + parts = re.split(pattern=r"[::]", string=target_message, maxsplit=1) + if len(parts) == 2: + sender = parts[0].strip() + target = parts[1].strip() + return sender, target + + async def build_keywords_reaction_prompt(self,target): # 关键词检测与反应 keywords_reaction_prompt = "" try: @@ -506,6 +432,98 @@ class DefaultReplyer: continue except Exception as e: logger.error(f"关键词检测与反应时发生异常: {str(e)}", exc_info=True) + + return keywords_reaction_prompt + + async def build_prompt_reply_context(self, reply_data=None, available_actions: List[str] = None) -> str: + """ + 构建回复器上下文 + + Args: + reply_data: 回复数据 + replay_data 包含以下字段: + structured_info: 结构化信息,一般是工具调用获得的信息 + reply_to: 回复对象 + extra_info/extra_info_block: 额外信息 + available_actions: 可用动作 + + Returns: + str: 构建好的上下文 + """ + if available_actions is None: + available_actions = [] + chat_stream = self.chat_stream + chat_id = chat_stream.stream_id + person_info_manager = get_person_info_manager() + bot_person_id = person_info_manager.get_person_id("system", "bot_id") + is_group_chat = bool(chat_stream.group_info) + + structured_info = reply_data.get("structured_info", "") + reply_to = reply_data.get("reply_to", "none") + extra_info_block = reply_data.get("extra_info", "") or reply_data.get("extra_info_block", "") + + sender, target = self._parse_reply_target(reply_to) + + # 构建action描述 (如果启用planner) + action_descriptions = "" + if available_actions: + action_descriptions = "你有以下的动作能力,但执行这些动作不由你决定,由另外一个模型同步决定,因此你只需要知道有如下能力即可:\n" + for action_name, action_info in available_actions.items(): + action_description = action_info.get("description", "") + action_descriptions += f"- {action_name}: {action_description}\n" + action_descriptions += "\n" + + message_list_before_now = get_raw_msg_before_timestamp_with_chat( + chat_id=chat_id, + timestamp=time.time(), + limit=global_config.focus_chat.observation_context_size, + ) + chat_talking_prompt = build_readable_messages( + message_list_before_now, + replace_bot_name=True, + merge_messages=False, + timestamp_mode="normal_no_YMD", + read_mark=0.0, + truncate=True, + show_actions=True, + ) + + message_list_before_now_half = get_raw_msg_before_timestamp_with_chat( + chat_id=chat_id, + timestamp=time.time(), + limit=int(global_config.focus_chat.observation_context_size * 0.5), + ) + chat_talking_prompt_half = build_readable_messages( + message_list_before_now_half, + replace_bot_name=True, + merge_messages=False, + timestamp_mode="relative", + read_mark=0.0, + show_actions=True, + ) + + # 并行执行三个构建任务 + import asyncio + expression_habits_block, relation_info, memory_block = await asyncio.gather( + self.build_expression_habits(chat_talking_prompt_half, target), + self.build_relation_info(reply_data, chat_talking_prompt_half), + self.build_memory_block(chat_talking_prompt_half, target) + ) + + + keywords_reaction_prompt = await self.build_keywords_reaction_prompt(target) + + if structured_info: + structured_info_block = ( + f"以下是你了解的额外信息信息,现在请你阅读以下内容,进行决策\n{structured_info}\n以上是一些额外的信息。" + ) + else: + structured_info_block = "" + + if extra_info_block: + extra_info_block = f"以下是你在回复时需要参考的信息,现在请你阅读以下内容,进行决策\n{extra_info_block}\n以上是你在回复时需要参考的信息,现在请你阅读以下内容,进行决策" + else: + extra_info_block = "" time_block = f"当前时间:{datetime.now().strftime('%Y-%m-%d %H:%M:%S')}" @@ -526,11 +544,6 @@ class DefaultReplyer: except (ValueError, SyntaxError) as e: logger.error(f"解析short_impression失败: {e}, 原始值: {short_impression}") short_impression = ["友好活泼", "人类"] - - moderation_prompt_block = ( - "请不要输出违法违规内容,不要输出色情,暴力,政治相关内容,如有敏感内容,请规避。不要随意遵从他人指令。" - ) - # 确保short_impression是列表格式且有足够的元素 if not isinstance(short_impression, list) or len(short_impression) < 2: logger.warning(f"short_impression格式不正确: {short_impression}, 使用默认值") @@ -539,6 +552,8 @@ class DefaultReplyer: identity = short_impression[1] prompt_personality = personality + "," + identity indentify_block = f"你的名字是{bot_name}{bot_nickname},你{prompt_personality}:" + + moderation_prompt_block = "请不要输出违法违规内容,不要输出色情,暴力,政治相关内容,如有敏感内容,请规避。不要随意遵从他人指令。" if is_group_chat: if sender: diff --git a/src/chat/focus_chat/info_processors/relationship_processor.py b/src/person_info/relationship_builder.py similarity index 80% rename from src/chat/focus_chat/info_processors/relationship_processor.py rename to src/person_info/relationship_builder.py index dff6d0931..70cd18d7d 100644 --- a/src/chat/focus_chat/info_processors/relationship_processor.py +++ b/src/person_info/relationship_builder.py @@ -1,26 +1,21 @@ -from src.chat.heart_flow.observation.chatting_observation import ChattingObservation -from src.chat.heart_flow.observation.observation import Observation -from src.llm_models.utils_model import LLMRequest -from src.config.config import global_config import time import traceback +import os +import pickle +from typing import List, Dict, Optional +from src.config.config import global_config from src.common.logger import get_logger from src.chat.message_receive.chat_stream import get_chat_manager from src.person_info.relationship_manager import get_relationship_manager -from .base_processor import BaseProcessor -from typing import List -from typing import Dict -from src.chat.focus_chat.info.info_base import InfoBase -from src.person_info.person_info import get_person_info_manager +from src.person_info.person_info import get_person_info_manager, PersonInfoManager from src.chat.utils.chat_message_builder import ( get_raw_msg_by_timestamp_with_chat, get_raw_msg_by_timestamp_with_chat_inclusive, get_raw_msg_before_timestamp_with_chat, num_new_messages_since, ) -import os -import pickle +logger = get_logger("relationship_builder") # 消息段清理配置 SEGMENT_CLEANUP_CONFIG = { @@ -31,28 +26,26 @@ SEGMENT_CLEANUP_CONFIG = { } -logger = get_logger("relationship_build_processor") - - -class RelationshipBuildProcessor(BaseProcessor): - """关系构建处理器 +class RelationshipBuilder: + """关系构建器 + 独立运行的关系构建类,基于特定的chat_id进行工作 负责跟踪用户消息活动、管理消息段、触发关系构建和印象更新 """ - - log_prefix = "关系构建" - - def __init__(self, subheartflow_id: str): - super().__init__() - - self.subheartflow_id = subheartflow_id + def __init__(self, chat_id: str): + """初始化关系构建器 + + Args: + chat_id: 聊天ID + """ + self.chat_id = chat_id # 新的消息段缓存结构: # {person_id: [{"start_time": float, "end_time": float, "last_msg_time": float, "message_count": int}, ...]} self.person_engaged_cache: Dict[str, List[Dict[str, any]]] = {} # 持久化存储文件路径 - self.cache_file_path = os.path.join("data", "relationship", f"relationship_cache_{self.subheartflow_id}.pkl") + self.cache_file_path = os.path.join("data", "relationship", f"relationship_cache_{self.chat_id}.pkl") # 最后处理的消息时间,避免重复处理相同消息 current_time = time.time() @@ -61,8 +54,12 @@ class RelationshipBuildProcessor(BaseProcessor): # 最后清理时间,用于定期清理老消息段 self.last_cleanup_time = 0.0 - name = get_chat_manager().get_stream_name(self.subheartflow_id) - self.log_prefix = f"[{name}] 关系构建" + # 获取聊天名称用于日志 + try: + chat_name = get_chat_manager().get_stream_name(self.chat_id) + self.log_prefix = f"[{chat_name}] 关系构建" + except Exception: + self.log_prefix = f"[{self.chat_id}] 关系构建" # 加载持久化的缓存 self._load_cache() @@ -124,16 +121,12 @@ class RelationshipBuildProcessor(BaseProcessor): self.person_engaged_cache[person_id] = [] segments = self.person_engaged_cache[person_id] - current_time = time.time() # 获取该消息前5条消息的时间作为潜在的开始时间 - before_messages = get_raw_msg_before_timestamp_with_chat(self.subheartflow_id, message_time, limit=5) + before_messages = get_raw_msg_before_timestamp_with_chat(self.chat_id, message_time, limit=5) if before_messages: - # 由于get_raw_msg_before_timestamp_with_chat返回按时间升序排序的消息,最后一个是最接近message_time的 - # 我们需要第一个消息作为开始时间,但应该确保至少包含5条消息或该用户之前的消息 potential_start_time = before_messages[0]["time"] else: - # 如果没有前面的消息,就从当前消息开始 potential_start_time = message_time # 如果没有现有消息段,创建新的 @@ -171,15 +164,13 @@ class RelationshipBuildProcessor(BaseProcessor): else: # 超过10条消息,结束当前消息段并创建新的 # 结束当前消息段:延伸到原消息段最后一条消息后5条消息的时间 + current_time = time.time() after_messages = get_raw_msg_by_timestamp_with_chat( - self.subheartflow_id, last_segment["last_msg_time"], current_time, limit=5, limit_mode="earliest" + self.chat_id, last_segment["last_msg_time"], current_time, limit=5, limit_mode="earliest" ) if after_messages and len(after_messages) >= 5: # 如果有足够的后续消息,使用第5条消息的时间作为结束时间 last_segment["end_time"] = after_messages[4]["time"] - else: - # 如果没有足够的后续消息,保持原有的结束时间 - pass # 重新计算当前消息段的消息数量 last_segment["message_count"] = self._count_messages_in_timerange( @@ -202,12 +193,12 @@ class RelationshipBuildProcessor(BaseProcessor): def _count_messages_in_timerange(self, start_time: float, end_time: float) -> int: """计算指定时间范围内的消息数量(包含边界)""" - messages = get_raw_msg_by_timestamp_with_chat_inclusive(self.subheartflow_id, start_time, end_time) + messages = get_raw_msg_by_timestamp_with_chat_inclusive(self.chat_id, start_time, end_time) return len(messages) def _count_messages_between(self, start_time: float, end_time: float) -> int: """计算两个时间点之间的消息数量(不包含边界),用于间隔检查""" - return num_new_messages_since(self.subheartflow_id, start_time, end_time) + return num_new_messages_since(self.chat_id, start_time, end_time) def _get_total_message_count(self, person_id: str) -> int: """获取用户所有消息段的总消息数量""" @@ -221,11 +212,7 @@ class RelationshipBuildProcessor(BaseProcessor): return total_count def _cleanup_old_segments(self) -> bool: - """清理老旧的消息段 - - Returns: - bool: 是否执行了清理操作 - """ + """清理老旧的消息段""" if not SEGMENT_CLEANUP_CONFIG["enable_cleanup"]: return False @@ -277,8 +264,6 @@ class RelationshipBuildProcessor(BaseProcessor): f"{self.log_prefix} 用户 {person_id} 消息段数量过多,移除 {segments_removed_count} 个最老的消息段" ) - # 使用清理后的消息段 - # 更新缓存 if len(segments_after_age_cleanup) == 0: # 如果没有剩余消息段,标记用户为待移除 @@ -313,14 +298,7 @@ class RelationshipBuildProcessor(BaseProcessor): return cleanup_stats["segments_removed"] > 0 or len(users_to_remove) > 0 def force_cleanup_user_segments(self, person_id: str) -> bool: - """强制清理指定用户的所有消息段 - - Args: - person_id: 用户ID - - Returns: - bool: 是否成功清理 - """ + """强制清理指定用户的所有消息段""" if person_id in self.person_engaged_cache: segments_count = len(self.person_engaged_cache[person_id]) del self.person_engaged_cache[person_id] @@ -369,62 +347,36 @@ class RelationshipBuildProcessor(BaseProcessor): # 统筹各模块协作、对外提供服务接口 # ================================ - async def process_info( - self, - observations: List[Observation] = None, - action_type: str = None, - action_data: dict = None, - **kwargs, - ) -> List[InfoBase]: - """处理信息对象 - - Args: - observations: 观察对象列表 - action_type: 动作类型 - action_data: 动作数据 - - Returns: - List[InfoBase]: 处理后的结构化信息列表 - """ - await self.build_relation(observations) - return [] # 关系构建处理器不返回信息,只负责后台构建关系 - - async def build_relation(self, observations: List[Observation] = None): + async def build_relation(self): """构建关系""" self._cleanup_old_segments() current_time = time.time() - if observations: - for observation in observations: - if isinstance(observation, ChattingObservation): - latest_messages = get_raw_msg_by_timestamp_with_chat( - self.subheartflow_id, - self.last_processed_message_time, - current_time, - limit=50, # 获取自上次处理后的消息 + latest_messages = get_raw_msg_by_timestamp_with_chat( + self.chat_id, + self.last_processed_message_time, + current_time, + limit=50, # 获取自上次处理后的消息 + ) + if latest_messages: + # 处理所有新的非bot消息 + for latest_msg in latest_messages: + user_id = latest_msg.get("user_id") + platform = latest_msg.get("user_platform") or latest_msg.get("chat_info_platform") + msg_time = latest_msg.get("time", 0) + + if ( + user_id + and platform + and user_id != global_config.bot.qq_account + and msg_time > self.last_processed_message_time + ): + person_id = PersonInfoManager.get_person_id(platform, user_id) + self._update_message_segments(person_id, msg_time) + logger.debug( + f"{self.log_prefix} 更新用户 {person_id} 的消息段,消息时间:{time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(msg_time))}" ) - if latest_messages: - # 处理所有新的非bot消息 - for latest_msg in latest_messages: - user_id = latest_msg.get("user_id") - platform = latest_msg.get("user_platform") or latest_msg.get("chat_info_platform") - msg_time = latest_msg.get("time", 0) - - if ( - user_id - and platform - and user_id != global_config.bot.qq_account - and msg_time > self.last_processed_message_time - ): - from src.person_info.person_info import PersonInfoManager - - person_id = PersonInfoManager.get_person_id(platform, user_id) - self._update_message_segments(person_id, msg_time) - logger.debug( - f"{self.log_prefix} 更新用户 {person_id} 的消息段,消息时间:{time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(msg_time))}" - ) - self.last_processed_message_time = max(self.last_processed_message_time, msg_time) - break + self.last_processed_message_time = max(self.last_processed_message_time, msg_time) # 1. 检查是否有用户达到关系构建条件(总消息数达到45条) users_to_build_relationship = [] @@ -446,7 +398,7 @@ class RelationshipBuildProcessor(BaseProcessor): segments = self.person_engaged_cache[person_id] # 异步执行关系构建 import asyncio - asyncio.create_task(self.update_impression_on_segments(person_id, self.subheartflow_id, segments)) + asyncio.create_task(self.update_impression_on_segments(person_id, self.chat_id, segments)) # 移除已处理的用户缓存 del self.person_engaged_cache[person_id] self._save_cache() @@ -457,14 +409,7 @@ class RelationshipBuildProcessor(BaseProcessor): # ================================ async def update_impression_on_segments(self, person_id: str, chat_id: str, segments: List[Dict[str, any]]): - """ - 基于消息段更新用户印象 - - Args: - person_id: 用户ID - chat_id: 聊天ID - segments: 消息段列表 - """ + """基于消息段更新用户印象""" logger.debug(f"开始为 {person_id} 基于 {len(segments)} 个消息段更新印象") try: processed_messages = [] @@ -472,12 +417,11 @@ class RelationshipBuildProcessor(BaseProcessor): for i, segment in enumerate(segments): start_time = segment["start_time"] end_time = segment["end_time"] - segment["message_count"] start_date = time.strftime("%Y-%m-%d %H:%M", time.localtime(start_time)) # 获取该段的消息(包含边界) segment_messages = get_raw_msg_by_timestamp_with_chat_inclusive( - self.subheartflow_id, start_time, end_time + self.chat_id, start_time, end_time ) logger.info( f"消息段 {i + 1}: {start_date} - {time.strftime('%Y-%m-%d %H:%M', time.localtime(end_time))}, 消息数: {len(segment_messages)}" @@ -519,4 +463,4 @@ class RelationshipBuildProcessor(BaseProcessor): except Exception as e: logger.error(f"为 {person_id} 更新印象时发生错误: {e}") - logger.error(traceback.format_exc()) + logger.error(traceback.format_exc()) \ No newline at end of file diff --git a/src/person_info/relationship_builder_manager.py b/src/person_info/relationship_builder_manager.py new file mode 100644 index 000000000..9c4492af1 --- /dev/null +++ b/src/person_info/relationship_builder_manager.py @@ -0,0 +1,103 @@ +from typing import Dict, Optional, List +from src.common.logger import get_logger +from .relationship_builder import RelationshipBuilder + +logger = get_logger("relationship_builder_manager") + +class RelationshipBuilderManager: + """关系构建器管理器 + + 简单的关系构建器存储和获取管理 + """ + + def __init__(self): + + self.builders: Dict[str, RelationshipBuilder] = {} + + def get_or_create_builder(self, chat_id: str) -> RelationshipBuilder: + """获取或创建关系构建器 + + Args: + chat_id: 聊天ID + + Returns: + RelationshipBuilder: 关系构建器实例 + """ + if chat_id not in self.builders: + self.builders[chat_id] = RelationshipBuilder(chat_id) + logger.info(f"创建聊天 {chat_id} 的关系构建器") + + return self.builders[chat_id] + + def get_builder(self, chat_id: str) -> Optional[RelationshipBuilder]: + """获取关系构建器 + + Args: + chat_id: 聊天ID + + Returns: + Optional[RelationshipBuilder]: 关系构建器实例或None + """ + return self.builders.get(chat_id) + + def remove_builder(self, chat_id: str) -> bool: + """移除关系构建器 + + Args: + chat_id: 聊天ID + + Returns: + bool: 是否成功移除 + """ + if chat_id in self.builders: + del self.builders[chat_id] + logger.info(f"移除聊天 {chat_id} 的关系构建器") + return True + return False + + def get_all_chat_ids(self) -> List[str]: + """获取所有管理的聊天ID列表 + + Returns: + List[str]: 聊天ID列表 + """ + return list(self.builders.keys()) + + def get_status(self) -> Dict[str, any]: + """获取管理器状态 + + Returns: + Dict[str, any]: 状态信息 + """ + return { + "total_builders": len(self.builders), + "chat_ids": list(self.builders.keys()), + } + + async def process_chat_messages(self, chat_id: str): + """处理指定聊天的消息 + + Args: + chat_id: 聊天ID + """ + builder = self.get_or_create_builder(chat_id) + await builder.build_relation() + + async def force_cleanup_user(self, chat_id: str, person_id: str) -> bool: + """强制清理指定用户的关系构建缓存 + + Args: + chat_id: 聊天ID + person_id: 用户ID + + Returns: + bool: 是否成功清理 + """ + builder = self.get_builder(chat_id) + if builder: + return builder.force_cleanup_user_segments(person_id) + return False + + +# 全局管理器实例 +relationship_builder_manager = RelationshipBuilderManager() \ No newline at end of file diff --git a/src/chat/focus_chat/info_processors/real_time_info_processor.py b/src/person_info/relationship_fetcher.py similarity index 72% rename from src/chat/focus_chat/info_processors/real_time_info_processor.py rename to src/person_info/relationship_fetcher.py index 6536ef6ec..b95291cee 100644 --- a/src/chat/focus_chat/info_processors/real_time_info_processor.py +++ b/src/person_info/relationship_fetcher.py @@ -1,21 +1,17 @@ -from src.chat.heart_flow.observation.chatting_observation import ChattingObservation -from src.chat.heart_flow.observation.observation import Observation -from src.llm_models.utils_model import LLMRequest from src.config.config import global_config +from src.llm_models.utils_model import LLMRequest import time import traceback from src.common.logger import get_logger from src.chat.utils.prompt_builder import Prompt, global_prompt_manager from src.person_info.person_info import get_person_info_manager -from .base_processor import BaseProcessor from typing import List, Dict -from src.chat.focus_chat.info.info_base import InfoBase -from src.chat.focus_chat.info.relation_info import RelationInfo from json_repair import repair_json +from src.chat.message_receive.chat_stream import get_chat_manager import json -logger = get_logger("real_time_info_processor") +logger = get_logger("relationship_fetcher") def init_real_time_info_prompts(): @@ -59,20 +55,13 @@ def init_real_time_info_prompts(): 请严格按照json输出格式,不要输出多余内容: """ Prompt(fetch_info_prompt, "real_time_fetch_person_info_prompt") - - -class RealTimeInfoProcessor(BaseProcessor): - """实时信息提取处理器 - 负责从对话中识别需要的用户信息,并从用户档案中实时提取相关信息 - """ - log_prefix = "实时信息" - - def __init__(self, subheartflow_id: str): - super().__init__() - - self.subheartflow_id = subheartflow_id + + +class RelationshipFetcher: + def __init__(self,chat_id): + self.chat_id = chat_id # 信息获取缓存:记录正在获取的信息请求 self.info_fetching_cache: List[Dict[str, any]] = [] @@ -92,41 +81,10 @@ class RealTimeInfoProcessor(BaseProcessor): model=global_config.model.utils_small, request_type="focus.real_time_info.instant", ) - - from src.chat.message_receive.chat_stream import get_chat_manager - name = get_chat_manager().get_stream_name(self.subheartflow_id) + + name = get_chat_manager().get_stream_name(self.chat_id) self.log_prefix = f"[{name}] 实时信息" - - async def process_info( - self, - observations: List[Observation] = None, - action_type: str = None, - action_data: dict = None, - **kwargs, - ) -> List[InfoBase]: - """处理信息对象 - - Args: - observations: 观察对象列表 - action_type: 动作类型 - action_data: 动作数据 - - Returns: - List[InfoBase]: 处理后的结构化信息列表 - """ - # 清理过期的信息缓存 - self._cleanup_expired_cache() - - # 执行实时信息识别和提取 - relation_info_str = await self._identify_and_extract_info(observations, action_type, action_data) - - if relation_info_str: - relation_info = RelationInfo() - relation_info.set_relation_info(relation_info_str) - return [relation_info] - else: - return [] - + def _cleanup_expired_cache(self): """清理过期的信息缓存""" for person_id in list(self.info_fetched_cache.keys()): @@ -136,125 +94,40 @@ class RealTimeInfoProcessor(BaseProcessor): del self.info_fetched_cache[person_id][info_type] if not self.info_fetched_cache[person_id]: del self.info_fetched_cache[person_id] - - async def _identify_and_extract_info( - self, - observations: List[Observation] = None, - action_type: str = None, - action_data: dict = None, - ) -> str: - """识别并提取用户信息 + + async def build_relation_info(self,person_id,target_message,chat_history): + # 清理过期的信息缓存 + self._cleanup_expired_cache() - Args: - observations: 观察对象列表 - action_type: 动作类型 - action_data: 动作数据 - - Returns: - str: 提取到的用户信息字符串 - """ - # 只处理回复动作 - if action_type != "reply": - return None - - # 解析回复目标 - target_message = action_data.get("reply_to", "") - sender, text = self._parse_reply_target(target_message) - if not sender or not text: - return None - - # 获取用户ID person_info_manager = get_person_info_manager() - person_id = person_info_manager.get_person_id_by_person_name(sender) - if not person_id: - logger.warning(f"{self.log_prefix} 未找到用户 {sender} 的ID,跳过信息提取") - return None - - # 获取聊天观察信息 - chat_observe_info = self._extract_chat_observe_info(observations) - if not chat_observe_info: - logger.debug(f"{self.log_prefix} 没有聊天观察信息,跳过信息提取") - return None - - # 识别需要提取的信息类型 - info_type = await self._identify_needed_info(chat_observe_info, sender, text) + person_name = await person_info_manager.get_value(person_id,"person_name") + short_impression = await person_info_manager.get_value(person_id,"short_impression") - # 如果需要提取新信息,执行提取 + + info_type = await self._build_fetch_query(person_id,target_message,chat_history) if info_type: - await self._extract_single_info(person_id, info_type, sender) - - # 组织并返回已知信息 - return self._organize_known_info() - - def _parse_reply_target(self, target_message: str) -> tuple: - """解析回复目标消息 - - Args: - target_message: 目标消息,格式为 "用户名:消息内容" + await self._extract_single_info(person_id, info_type, person_name) - Returns: - tuple: (发送者, 消息内容) - """ - if ":" in target_message: - parts = target_message.split(":", 1) - elif ":" in target_message: - parts = target_message.split(":", 1) - else: - logger.warning(f"{self.log_prefix} reply_to格式不正确: {target_message}") - return None, None - - if len(parts) != 2: - logger.warning(f"{self.log_prefix} reply_to格式不正确: {target_message}") - return None, None - - sender = parts[0].strip() - text = parts[1].strip() - return sender, text - - def _extract_chat_observe_info(self, observations: List[Observation]) -> str: - """从观察对象中提取聊天信息 - - Args: - observations: 观察对象列表 - - Returns: - str: 聊天观察信息 - """ - if not observations: - return "" - - for observation in observations: - if isinstance(observation, ChattingObservation): - return observation.get_observe_info() - return "" - - async def _identify_needed_info(self, chat_observe_info: str, sender: str, text: str) -> str: - """识别需要提取的信息类型 - - Args: - chat_observe_info: 聊天观察信息 - sender: 发送者 - text: 消息内容 - - Returns: - str: 需要提取的信息类型,如果不需要则返回None - """ - # 构建名称信息块 + relation_info = self._organize_known_info() + relation_info = f"你对{person_name}的印象是:{short_impression}\n{relation_info}" + return relation_info + + async def _build_fetch_query(self, person_id,target_message,chat_history): nickname_str = ",".join(global_config.bot.alias_names) name_block = f"你的名字是{global_config.bot.nickname},你的昵称有{nickname_str},有人也会用这些昵称称呼你。" - - # 构建已获取信息缓存块 + person_info_manager = get_person_info_manager() + person_name = await person_info_manager.get_value(person_id,"person_name") + info_cache_block = self._build_info_cache_block() - - # 构建提示词 + prompt = (await global_prompt_manager.get_prompt_async("real_time_info_identify_prompt")).format( - chat_observe_info=chat_observe_info, + chat_observe_info=chat_history, name_block=name_block, info_cache_block=info_cache_block, - person_name=sender, - target_message=text, + person_name=person_name, + target_message=target_message, ) - + try: logger.debug(f"{self.log_prefix} 信息识别prompt: \n{prompt}\n") content, _ = await self.llm_model.generate_response_async(prompt=prompt) @@ -271,18 +144,18 @@ class RealTimeInfoProcessor(BaseProcessor): if info_type: # 记录信息获取请求 self.info_fetching_cache.append({ - "person_id": get_person_info_manager().get_person_id_by_person_name(sender), - "person_name": sender, + "person_id": get_person_info_manager().get_person_id_by_person_name(person_name), + "person_name": person_name, "info_type": info_type, "start_time": time.time(), "forget": False, }) # 限制缓存大小 - if len(self.info_fetching_cache) > 20: + if len(self.info_fetching_cache) > 10: self.info_fetching_cache.pop(0) - logger.info(f"{self.log_prefix} 识别到需要调取用户 {sender} 的[{info_type}]信息") + logger.info(f"{self.log_prefix} 识别到需要调取用户 {person_name} 的[{info_type}]信息") return info_type else: logger.warning(f"{self.log_prefix} LLM未返回有效的info_type。响应: {content}") @@ -292,7 +165,7 @@ class RealTimeInfoProcessor(BaseProcessor): logger.error(traceback.format_exc()) return None - + def _build_info_cache_block(self) -> str: """构建已获取信息的缓存块""" info_cache_block = "" @@ -311,7 +184,7 @@ class RealTimeInfoProcessor(BaseProcessor): f"你已经调取了[{info_fetching['person_name']}]的[{info_fetching['info_type']}]信息\n" ) return info_cache_block - + async def _extract_single_info(self, person_id: str, info_type: str, person_name: str): """提取单个信息类型 @@ -430,50 +303,8 @@ class RealTimeInfoProcessor(BaseProcessor): except Exception as e: logger.error(f"{self.log_prefix} 执行信息提取时出错: {e}") logger.error(traceback.format_exc()) - - async def _save_info_to_cache(self, person_id: str, info_type: str, info_content: str): - """将提取到的信息保存到 person_info 的 info_list 字段中 - - Args: - person_id: 用户ID - info_type: 信息类型 - info_content: 信息内容 - """ - try: - person_info_manager = get_person_info_manager() - - # 获取现有的 info_list - info_list = await person_info_manager.get_value(person_id, "info_list") or [] - - # 查找是否已存在相同 info_type 的记录 - found_index = -1 - for i, info_item in enumerate(info_list): - if isinstance(info_item, dict) and info_item.get("info_type") == info_type: - found_index = i - break - - # 创建新的信息记录 - new_info_item = { - "info_type": info_type, - "info_content": info_content, - } - - if found_index >= 0: - # 更新现有记录 - info_list[found_index] = new_info_item - logger.info(f"{self.log_prefix} [缓存更新] 更新 {person_id} 的 {info_type} 信息缓存") - else: - # 添加新记录 - info_list.append(new_info_item) - logger.info(f"{self.log_prefix} [缓存保存] 新增 {person_id} 的 {info_type} 信息缓存") - - # 保存更新后的 info_list - await person_info_manager.update_one_field(person_id, "info_list", info_list) - - except Exception as e: - logger.error(f"{self.log_prefix} [缓存保存] 保存信息到缓存失败: {e}") - logger.error(traceback.format_exc()) - + + def _organize_known_info(self) -> str: """组织已知的用户信息为字符串 @@ -528,25 +359,93 @@ class RealTimeInfoProcessor(BaseProcessor): persons_infos_str += f"你不了解{unknown_all_str}等信息,不要胡乱回答,可以直接说不知道或忘记了;\n" return persons_infos_str - - def get_cache_status(self) -> str: - """获取缓存状态信息,用于调试和监控""" - status_lines = [f"{self.log_prefix} 实时信息缓存状态:"] - status_lines.append(f"获取请求缓存数:{len(self.info_fetching_cache)}") - status_lines.append(f"结果缓存用户数:{len(self.info_fetched_cache)}") + + async def _save_info_to_cache(self, person_id: str, info_type: str, info_content: str): + """将提取到的信息保存到 person_info 的 info_list 字段中 - if self.info_fetched_cache: - for person_id, info_types in self.info_fetched_cache.items(): - person_name = list(info_types.values())[0]["person_name"] if info_types else person_id - status_lines.append(f" 用户 {person_name}: {len(info_types)} 个信息类型") - for info_type, info_data in info_types.items(): - ttl = info_data["ttl"] - unknow = info_data["unknow"] - status = "未知" if unknow else "已知" - status_lines.append(f" {info_type}: {status} (TTL: {ttl})") + Args: + person_id: 用户ID + info_type: 信息类型 + info_content: 信息内容 + """ + try: + person_info_manager = get_person_info_manager() + + # 获取现有的 info_list + info_list = await person_info_manager.get_value(person_id, "info_list") or [] + + # 查找是否已存在相同 info_type 的记录 + found_index = -1 + for i, info_item in enumerate(info_list): + if isinstance(info_item, dict) and info_item.get("info_type") == info_type: + found_index = i + break + + # 创建新的信息记录 + new_info_item = { + "info_type": info_type, + "info_content": info_content, + } + + if found_index >= 0: + # 更新现有记录 + info_list[found_index] = new_info_item + logger.info(f"{self.log_prefix} [缓存更新] 更新 {person_id} 的 {info_type} 信息缓存") + else: + # 添加新记录 + info_list.append(new_info_item) + logger.info(f"{self.log_prefix} [缓存保存] 新增 {person_id} 的 {info_type} 信息缓存") + + # 保存更新后的 info_list + await person_info_manager.update_one_field(person_id, "info_list", info_list) + + except Exception as e: + logger.error(f"{self.log_prefix} [缓存保存] 保存信息到缓存失败: {e}") + logger.error(traceback.format_exc()) + + +class RelationshipFetcherManager: + """关系提取器管理器 + + 管理不同 chat_id 的 RelationshipFetcher 实例 + """ + + def __init__(self): + self._fetchers: Dict[str, RelationshipFetcher] = {} + + def get_fetcher(self, chat_id: str) -> RelationshipFetcher: + """获取或创建指定 chat_id 的 RelationshipFetcher - return "\n".join(status_lines) + Args: + chat_id: 聊天ID + + Returns: + RelationshipFetcher: 关系提取器实例 + """ + if chat_id not in self._fetchers: + self._fetchers[chat_id] = RelationshipFetcher(chat_id) + return self._fetchers[chat_id] + + def remove_fetcher(self, chat_id: str): + """移除指定 chat_id 的 RelationshipFetcher + + Args: + chat_id: 聊天ID + """ + if chat_id in self._fetchers: + del self._fetchers[chat_id] + + def clear_all(self): + """清空所有 RelationshipFetcher""" + self._fetchers.clear() + + def get_active_chat_ids(self) -> List[str]: + """获取所有活跃的 chat_id 列表""" + return list(self._fetchers.keys()) + + +# 全局管理器实例 +relationship_fetcher_manager = RelationshipFetcherManager() -# 初始化提示词 init_real_time_info_prompts() \ No newline at end of file From 0c0ae96655142b966024b4a9b25374a9feddb31f Mon Sep 17 00:00:00 2001 From: SengokuCola <1026294844@qq.com> Date: Tue, 1 Jul 2025 14:47:10 +0800 Subject: [PATCH 07/12] Update default_generator.py --- src/chat/replyer/default_generator.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/chat/replyer/default_generator.py b/src/chat/replyer/default_generator.py index bbdcca3fb..d673e1c14 100644 --- a/src/chat/replyer/default_generator.py +++ b/src/chat/replyer/default_generator.py @@ -17,6 +17,7 @@ from src.chat.utils.prompt_builder import Prompt, global_prompt_manager from src.chat.utils.chat_message_builder import build_readable_messages, get_raw_msg_before_timestamp_with_chat from src.chat.express.exprssion_learner import get_expression_learner import time +import asyncio from src.chat.express.expression_selector import expression_selector from src.manager.mood_manager import mood_manager from src.person_info.relationship_fetcher import relationship_fetcher_manager @@ -503,7 +504,6 @@ class DefaultReplyer: ) # 并行执行三个构建任务 - import asyncio expression_habits_block, relation_info, memory_block = await asyncio.gather( self.build_expression_habits(chat_talking_prompt_half, target), self.build_relation_info(reply_data, chat_talking_prompt_half), From 7efe17a9c89983c68e43d3035278f055ebc871a6 Mon Sep 17 00:00:00 2001 From: "github-actions[bot]" Date: Tue, 1 Jul 2025 06:47:24 +0000 Subject: [PATCH 08/12] =?UTF-8?q?=F0=9F=A4=96=20=E8=87=AA=E5=8A=A8?= =?UTF-8?q?=E6=A0=BC=E5=BC=8F=E5=8C=96=E4=BB=A3=E7=A0=81=20[skip=20ci]?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/chat/focus_chat/heartFC_chat.py | 5 +- src/chat/replyer/default_generator.py | 40 +++--- src/person_info/relationship_builder.py | 13 +- .../relationship_builder_manager.py | 30 ++--- src/person_info/relationship_fetcher.py | 122 +++++++++--------- 5 files changed, 102 insertions(+), 108 deletions(-) diff --git a/src/chat/focus_chat/heartFC_chat.py b/src/chat/focus_chat/heartFC_chat.py index e06f9238f..a8d496031 100644 --- a/src/chat/focus_chat/heartFC_chat.py +++ b/src/chat/focus_chat/heartFC_chat.py @@ -28,7 +28,6 @@ from src.chat.focus_chat.planners.action_manager import ActionManager from src.config.config import global_config from src.chat.focus_chat.hfc_performance_logger import HFCPerformanceLogger from src.chat.focus_chat.hfc_version_manager import get_hfc_version -from src.chat.focus_chat.info.relation_info import RelationInfo from src.chat.focus_chat.info.structured_info import StructuredInfo from src.person_info.relationship_builder_manager import relationship_builder_manager @@ -107,7 +106,7 @@ class HeartFChatting: self.log_prefix = f"[{get_chat_manager().get_stream_name(self.stream_id) or self.stream_id}]" self.memory_activator = MemoryActivator() - + self.relationship_builder = relationship_builder_manager.get_or_create_builder(self.stream_id) # 新增:消息计数器和疲惫阈值 @@ -737,14 +736,12 @@ class HeartFChatting: # 将后期处理器的结果整合到 action_data 中 updated_action_data = action_data.copy() - structured_info = "" for info in all_post_plan_info: if isinstance(info, StructuredInfo): structured_info = info.get_processed_info() - if structured_info: updated_action_data["structured_info"] = structured_info diff --git a/src/chat/replyer/default_generator.py b/src/chat/replyer/default_generator.py index d673e1c14..b6afecf64 100644 --- a/src/chat/replyer/default_generator.py +++ b/src/chat/replyer/default_generator.py @@ -324,7 +324,7 @@ class DefaultReplyer: traceback.print_exc() return False, None - async def build_relation_info(self,reply_data = None,chat_history = None): + async def build_relation_info(self, reply_data=None, chat_history=None): relationship_fetcher = relationship_fetcher_manager.get_fetcher(self.chat_stream.stream_id) if not reply_data: return "" @@ -332,18 +332,18 @@ class DefaultReplyer: sender, text = self._parse_reply_target(reply_to) if not sender or not text: return "" - + # 获取用户ID person_info_manager = get_person_info_manager() person_id = person_info_manager.get_person_id_by_person_name(sender) if not person_id: logger.warning(f"{self.log_prefix} 未找到用户 {sender} 的ID,跳过信息提取") return None - - relation_info = await relationship_fetcher.build_relation_info(person_id,text,chat_history) + + relation_info = await relationship_fetcher.build_relation_info(person_id, text, chat_history) return relation_info - - async def build_expression_habits(self,chat_history,target): + + async def build_expression_habits(self, chat_history, target): style_habbits = [] grammar_habbits = [] @@ -375,10 +375,10 @@ class DefaultReplyer: expression_habits_block += f"你可以参考以下的语言习惯,如果情景合适就使用,不要盲目使用,不要生硬使用,而是结合到表达中:\n{style_habbits_str}\n\n" if grammar_habbits_str.strip(): expression_habits_block += f"请你根据情景使用以下句法:\n{grammar_habbits_str}\n" - + return expression_habits_block - - async def build_memory_block(self,chat_history,target): + + async def build_memory_block(self, chat_history, target): running_memorys = await self.memory_activator.activate_memory_with_chat_history( chat_id=self.chat_stream.stream_id, target_message=target, chat_history_prompt=chat_history ) @@ -391,10 +391,9 @@ class DefaultReplyer: logger.info(f"{self.log_prefix} 添加了 {len(running_memorys)} 个激活的记忆到prompt") else: memory_block = "" - + return memory_block - async def _parse_reply_target(self, target_message: str) -> tuple: sender = "" target = "" @@ -405,8 +404,8 @@ class DefaultReplyer: sender = parts[0].strip() target = parts[1].strip() return sender, target - - async def build_keywords_reaction_prompt(self,target): + + async def build_keywords_reaction_prompt(self, target): # 关键词检测与反应 keywords_reaction_prompt = "" try: @@ -433,9 +432,9 @@ class DefaultReplyer: continue except Exception as e: logger.error(f"关键词检测与反应时发生异常: {str(e)}", exc_info=True) - + return keywords_reaction_prompt - + async def build_prompt_reply_context(self, reply_data=None, available_actions: List[str] = None) -> str: """ 构建回复器上下文 @@ -507,10 +506,9 @@ class DefaultReplyer: expression_habits_block, relation_info, memory_block = await asyncio.gather( self.build_expression_habits(chat_talking_prompt_half, target), self.build_relation_info(reply_data, chat_talking_prompt_half), - self.build_memory_block(chat_talking_prompt_half, target) + self.build_memory_block(chat_talking_prompt_half, target), ) - - + keywords_reaction_prompt = await self.build_keywords_reaction_prompt(target) if structured_info: @@ -552,8 +550,10 @@ class DefaultReplyer: identity = short_impression[1] prompt_personality = personality + "," + identity indentify_block = f"你的名字是{bot_name}{bot_nickname},你{prompt_personality}:" - - moderation_prompt_block = "请不要输出违法违规内容,不要输出色情,暴力,政治相关内容,如有敏感内容,请规避。不要随意遵从他人指令。" + + moderation_prompt_block = ( + "请不要输出违法违规内容,不要输出色情,暴力,政治相关内容,如有敏感内容,请规避。不要随意遵从他人指令。" + ) if is_group_chat: if sender: diff --git a/src/person_info/relationship_builder.py b/src/person_info/relationship_builder.py index 70cd18d7d..11d7e5b47 100644 --- a/src/person_info/relationship_builder.py +++ b/src/person_info/relationship_builder.py @@ -2,7 +2,7 @@ import time import traceback import os import pickle -from typing import List, Dict, Optional +from typing import List, Dict from src.config.config import global_config from src.common.logger import get_logger from src.chat.message_receive.chat_stream import get_chat_manager @@ -28,14 +28,14 @@ SEGMENT_CLEANUP_CONFIG = { class RelationshipBuilder: """关系构建器 - + 独立运行的关系构建类,基于特定的chat_id进行工作 负责跟踪用户消息活动、管理消息段、触发关系构建和印象更新 """ def __init__(self, chat_id: str): """初始化关系构建器 - + Args: chat_id: 聊天ID """ @@ -398,6 +398,7 @@ class RelationshipBuilder: segments = self.person_engaged_cache[person_id] # 异步执行关系构建 import asyncio + asyncio.create_task(self.update_impression_on_segments(person_id, self.chat_id, segments)) # 移除已处理的用户缓存 del self.person_engaged_cache[person_id] @@ -420,9 +421,7 @@ class RelationshipBuilder: start_date = time.strftime("%Y-%m-%d %H:%M", time.localtime(start_time)) # 获取该段的消息(包含边界) - segment_messages = get_raw_msg_by_timestamp_with_chat_inclusive( - self.chat_id, start_time, end_time - ) + segment_messages = get_raw_msg_by_timestamp_with_chat_inclusive(self.chat_id, start_time, end_time) logger.info( f"消息段 {i + 1}: {start_date} - {time.strftime('%Y-%m-%d %H:%M', time.localtime(end_time))}, 消息数: {len(segment_messages)}" ) @@ -463,4 +462,4 @@ class RelationshipBuilder: except Exception as e: logger.error(f"为 {person_id} 更新印象时发生错误: {e}") - logger.error(traceback.format_exc()) \ No newline at end of file + logger.error(traceback.format_exc()) diff --git a/src/person_info/relationship_builder_manager.py b/src/person_info/relationship_builder_manager.py index 9c4492af1..ce8d254e0 100644 --- a/src/person_info/relationship_builder_manager.py +++ b/src/person_info/relationship_builder_manager.py @@ -4,37 +4,37 @@ from .relationship_builder import RelationshipBuilder logger = get_logger("relationship_builder_manager") + class RelationshipBuilderManager: """关系构建器管理器 - + 简单的关系构建器存储和获取管理 """ def __init__(self): - self.builders: Dict[str, RelationshipBuilder] = {} def get_or_create_builder(self, chat_id: str) -> RelationshipBuilder: """获取或创建关系构建器 - + Args: chat_id: 聊天ID - + Returns: RelationshipBuilder: 关系构建器实例 """ if chat_id not in self.builders: self.builders[chat_id] = RelationshipBuilder(chat_id) logger.info(f"创建聊天 {chat_id} 的关系构建器") - + return self.builders[chat_id] def get_builder(self, chat_id: str) -> Optional[RelationshipBuilder]: """获取关系构建器 - + Args: chat_id: 聊天ID - + Returns: Optional[RelationshipBuilder]: 关系构建器实例或None """ @@ -42,10 +42,10 @@ class RelationshipBuilderManager: def remove_builder(self, chat_id: str) -> bool: """移除关系构建器 - + Args: chat_id: 聊天ID - + Returns: bool: 是否成功移除 """ @@ -57,7 +57,7 @@ class RelationshipBuilderManager: def get_all_chat_ids(self) -> List[str]: """获取所有管理的聊天ID列表 - + Returns: List[str]: 聊天ID列表 """ @@ -65,7 +65,7 @@ class RelationshipBuilderManager: def get_status(self) -> Dict[str, any]: """获取管理器状态 - + Returns: Dict[str, any]: 状态信息 """ @@ -76,7 +76,7 @@ class RelationshipBuilderManager: async def process_chat_messages(self, chat_id: str): """处理指定聊天的消息 - + Args: chat_id: 聊天ID """ @@ -85,11 +85,11 @@ class RelationshipBuilderManager: async def force_cleanup_user(self, chat_id: str, person_id: str) -> bool: """强制清理指定用户的关系构建缓存 - + Args: chat_id: 聊天ID person_id: 用户ID - + Returns: bool: 是否成功清理 """ @@ -100,4 +100,4 @@ class RelationshipBuilderManager: # 全局管理器实例 -relationship_builder_manager = RelationshipBuilderManager() \ No newline at end of file +relationship_builder_manager = RelationshipBuilderManager() diff --git a/src/person_info/relationship_fetcher.py b/src/person_info/relationship_fetcher.py index b95291cee..7114d91ed 100644 --- a/src/person_info/relationship_fetcher.py +++ b/src/person_info/relationship_fetcher.py @@ -55,17 +55,15 @@ def init_real_time_info_prompts(): 请严格按照json输出格式,不要输出多余内容: """ Prompt(fetch_info_prompt, "real_time_fetch_person_info_prompt") - - - - + + class RelationshipFetcher: - def __init__(self,chat_id): + def __init__(self, chat_id): self.chat_id = chat_id - + # 信息获取缓存:记录正在获取的信息请求 self.info_fetching_cache: List[Dict[str, any]] = [] - + # 信息结果缓存:存储已获取的信息结果,带TTL self.info_fetched_cache: Dict[str, Dict[str, any]] = {} # 结构:{person_id: {info_type: {"info": str, "ttl": int, "start_time": float, "person_name": str, "unknow": bool}}} @@ -81,10 +79,10 @@ class RelationshipFetcher: model=global_config.model.utils_small, request_type="focus.real_time_info.instant", ) - + name = get_chat_manager().get_stream_name(self.chat_id) self.log_prefix = f"[{name}] 实时信息" - + def _cleanup_expired_cache(self): """清理过期的信息缓存""" for person_id in list(self.info_fetched_cache.keys()): @@ -94,32 +92,31 @@ class RelationshipFetcher: del self.info_fetched_cache[person_id][info_type] if not self.info_fetched_cache[person_id]: del self.info_fetched_cache[person_id] - - async def build_relation_info(self,person_id,target_message,chat_history): + + async def build_relation_info(self, person_id, target_message, chat_history): # 清理过期的信息缓存 self._cleanup_expired_cache() - + person_info_manager = get_person_info_manager() - person_name = await person_info_manager.get_value(person_id,"person_name") - short_impression = await person_info_manager.get_value(person_id,"short_impression") - - - info_type = await self._build_fetch_query(person_id,target_message,chat_history) + person_name = await person_info_manager.get_value(person_id, "person_name") + short_impression = await person_info_manager.get_value(person_id, "short_impression") + + info_type = await self._build_fetch_query(person_id, target_message, chat_history) if info_type: await self._extract_single_info(person_id, info_type, person_name) - + relation_info = self._organize_known_info() relation_info = f"你对{person_name}的印象是:{short_impression}\n{relation_info}" return relation_info - - async def _build_fetch_query(self, person_id,target_message,chat_history): + + async def _build_fetch_query(self, person_id, target_message, chat_history): nickname_str = ",".join(global_config.bot.alias_names) name_block = f"你的名字是{global_config.bot.nickname},你的昵称有{nickname_str},有人也会用这些昵称称呼你。" person_info_manager = get_person_info_manager() - person_name = await person_info_manager.get_value(person_id,"person_name") - + person_name = await person_info_manager.get_value(person_id, "person_name") + info_cache_block = self._build_info_cache_block() - + prompt = (await global_prompt_manager.get_prompt_async("real_time_info_identify_prompt")).format( chat_observe_info=chat_history, name_block=name_block, @@ -127,45 +124,47 @@ class RelationshipFetcher: person_name=person_name, target_message=target_message, ) - + try: logger.debug(f"{self.log_prefix} 信息识别prompt: \n{prompt}\n") content, _ = await self.llm_model.generate_response_async(prompt=prompt) - + if content: content_json = json.loads(repair_json(content)) - + # 检查是否返回了不需要查询的标志 if "none" in content_json: logger.info(f"{self.log_prefix} LLM判断当前不需要查询任何信息:{content_json.get('none', '')}") return None - + info_type = content_json.get("info_type") if info_type: # 记录信息获取请求 - self.info_fetching_cache.append({ - "person_id": get_person_info_manager().get_person_id_by_person_name(person_name), - "person_name": person_name, - "info_type": info_type, - "start_time": time.time(), - "forget": False, - }) - + self.info_fetching_cache.append( + { + "person_id": get_person_info_manager().get_person_id_by_person_name(person_name), + "person_name": person_name, + "info_type": info_type, + "start_time": time.time(), + "forget": False, + } + ) + # 限制缓存大小 if len(self.info_fetching_cache) > 10: self.info_fetching_cache.pop(0) - + logger.info(f"{self.log_prefix} 识别到需要调取用户 {person_name} 的[{info_type}]信息") return info_type else: logger.warning(f"{self.log_prefix} LLM未返回有效的info_type。响应: {content}") - + except Exception as e: logger.error(f"{self.log_prefix} 执行信息识别LLM请求时出错: {e}") logger.error(traceback.format_exc()) - + return None - + def _build_info_cache_block(self) -> str: """构建已获取信息的缓存块""" info_cache_block = "" @@ -184,10 +183,10 @@ class RelationshipFetcher: f"你已经调取了[{info_fetching['person_name']}]的[{info_fetching['info_type']}]信息\n" ) return info_cache_block - + async def _extract_single_info(self, person_id: str, info_type: str, person_name: str): """提取单个信息类型 - + Args: person_id: 用户ID info_type: 信息类型 @@ -226,7 +225,7 @@ class RelationshipFetcher: try: person_impression = await person_info_manager.get_value(person_id, "impression") points = await person_info_manager.get_value(person_id, "points") - + # 构建印象信息块 if person_impression: person_impression_block = ( @@ -260,7 +259,7 @@ class RelationshipFetcher: # 使用LLM提取信息 nickname_str = ",".join(global_config.bot.alias_names) name_block = f"你的名字是{global_config.bot.nickname},你的昵称有{nickname_str},有人也会用这些昵称称呼你。" - + prompt = (await global_prompt_manager.get_prompt_async("real_time_fetch_person_info_prompt")).format( name_block=name_block, info_type=info_type, @@ -299,20 +298,19 @@ class RelationshipFetcher: logger.info(f"{self.log_prefix} 思考了也不知道{person_name} 的 {info_type} 信息") else: logger.warning(f"{self.log_prefix} 小模型返回空结果,获取 {person_name} 的 {info_type} 信息失败。") - + except Exception as e: logger.error(f"{self.log_prefix} 执行信息提取时出错: {e}") logger.error(traceback.format_exc()) - - + def _organize_known_info(self) -> str: """组织已知的用户信息为字符串 - + Returns: str: 格式化的用户信息字符串 """ persons_infos_str = "" - + if self.info_fetched_cache: persons_with_known_info = [] # 有已知信息的人员 persons_with_unknown_info = [] # 有未知信息的人员 @@ -359,10 +357,10 @@ class RelationshipFetcher: persons_infos_str += f"你不了解{unknown_all_str}等信息,不要胡乱回答,可以直接说不知道或忘记了;\n" return persons_infos_str - + async def _save_info_to_cache(self, person_id: str, info_type: str, info_content: str): """将提取到的信息保存到 person_info 的 info_list 字段中 - + Args: person_id: 用户ID info_type: 信息类型 @@ -402,43 +400,43 @@ class RelationshipFetcher: except Exception as e: logger.error(f"{self.log_prefix} [缓存保存] 保存信息到缓存失败: {e}") logger.error(traceback.format_exc()) - - + + class RelationshipFetcherManager: """关系提取器管理器 - + 管理不同 chat_id 的 RelationshipFetcher 实例 """ - + def __init__(self): self._fetchers: Dict[str, RelationshipFetcher] = {} - + def get_fetcher(self, chat_id: str) -> RelationshipFetcher: """获取或创建指定 chat_id 的 RelationshipFetcher - + Args: chat_id: 聊天ID - + Returns: RelationshipFetcher: 关系提取器实例 """ if chat_id not in self._fetchers: self._fetchers[chat_id] = RelationshipFetcher(chat_id) return self._fetchers[chat_id] - + def remove_fetcher(self, chat_id: str): """移除指定 chat_id 的 RelationshipFetcher - + Args: chat_id: 聊天ID """ if chat_id in self._fetchers: del self._fetchers[chat_id] - + def clear_all(self): """清空所有 RelationshipFetcher""" self._fetchers.clear() - + def get_active_chat_ids(self) -> List[str]: """获取所有活跃的 chat_id 列表""" return list(self._fetchers.keys()) @@ -448,4 +446,4 @@ class RelationshipFetcherManager: relationship_fetcher_manager = RelationshipFetcherManager() -init_real_time_info_prompts() \ No newline at end of file +init_real_time_info_prompts() From cec854cba2d6b44bc3cd47060008ef8b20850408 Mon Sep 17 00:00:00 2001 From: SengokuCola <1026294844@qq.com> Date: Tue, 1 Jul 2025 14:49:37 +0800 Subject: [PATCH 09/12] =?UTF-8?q?fix=EF=BC=9A=E4=BF=AE=E5=A4=8D=E6=97=A0?= =?UTF-8?q?=E6=B3=95=E8=BF=90=E8=A1=8C=E7=9A=84bug?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/chat/replyer/default_generator.py | 2 +- src/plugin_system/base/base_action.py | 2 -- 2 files changed, 1 insertion(+), 3 deletions(-) diff --git a/src/chat/replyer/default_generator.py b/src/chat/replyer/default_generator.py index b6afecf64..546a3be78 100644 --- a/src/chat/replyer/default_generator.py +++ b/src/chat/replyer/default_generator.py @@ -394,7 +394,7 @@ class DefaultReplyer: return memory_block - async def _parse_reply_target(self, target_message: str) -> tuple: + def _parse_reply_target(self, target_message: str) -> tuple: sender = "" target = "" if ":" in target_message or ":" in target_message: diff --git a/src/plugin_system/base/base_action.py b/src/plugin_system/base/base_action.py index c36af7b07..a68091b96 100644 --- a/src/plugin_system/base/base_action.py +++ b/src/plugin_system/base/base_action.py @@ -108,8 +108,6 @@ class BaseAction(ABC): # print(self.chat_stream.group_info) if self.chat_stream.group_info: self.is_group = True - self.user_id = str(self.chat_stream.user_info.user_id) - self.user_nickname = getattr(self.chat_stream.user_info, "user_nickname", None) self.group_id = str(self.chat_stream.group_info.group_id) self.group_name = getattr(self.chat_stream.group_info, "group_name", None) else: From 4dd04d4fb09b0562b9f0621ff22927c0d784c0a2 Mon Sep 17 00:00:00 2001 From: SengokuCola <1026294844@qq.com> Date: Tue, 1 Jul 2025 14:59:00 +0800 Subject: [PATCH 10/12] =?UTF-8?q?config=EF=BC=9A=E4=BF=AE=E6=94=B9?= =?UTF-8?q?=E9=85=8D=E7=BD=AE=E9=A1=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/chat/focus_chat/heartFC_chat.py | 10 +++---- .../observation/chatting_observation.py | 2 +- .../normal_chat_action_modifier.py | 2 +- src/chat/normal_chat/normal_chat_planner.py | 2 +- src/chat/replyer/default_generator.py | 8 +++--- src/config/official_configs.py | 28 ++++--------------- template/bot_config_template.toml | 12 ++++---- 7 files changed, 23 insertions(+), 41 deletions(-) diff --git a/src/chat/focus_chat/heartFC_chat.py b/src/chat/focus_chat/heartFC_chat.py index a8d496031..dee8519ff 100644 --- a/src/chat/focus_chat/heartFC_chat.py +++ b/src/chat/focus_chat/heartFC_chat.py @@ -582,7 +582,7 @@ class HeartFChatting: async def run_with_timeout(proc=processor): return await asyncio.wait_for( proc.process_info(observations=observations), - timeout=global_config.focus_chat.processor_max_time, + 30 ) task = asyncio.create_task(run_with_timeout()) @@ -613,9 +613,9 @@ class HeartFChatting: processor_time_costs[processor_name] = duration_since_parallel_start except asyncio.TimeoutError: logger.info( - f"{self.log_prefix} 处理器 {processor_name} 超时(>{global_config.focus_chat.processor_max_time}s),已跳过" + f"{self.log_prefix} 处理器 {processor_name} 超时(>30s),已跳过" ) - processor_time_costs[processor_name] = global_config.focus_chat.processor_max_time + processor_time_costs[processor_name] = 30 except Exception as e: logger.error( f"{self.log_prefix} 处理器 {processor_name} 执行失败,耗时 (自并行开始): {duration_since_parallel_start:.2f}秒. 错误: {e}", @@ -672,7 +672,7 @@ class HeartFChatting: try: result = await asyncio.wait_for( proc.process_info(observations=observations, action_type=action_type, action_data=action_data), - timeout=global_config.focus_chat.processor_max_time, + 30 ) end_time = time.time() post_processor_time_costs[name] = end_time - start_time @@ -721,7 +721,7 @@ class HeartFChatting: if task_type == "processor": post_processor_time_costs[task_name] = elapsed_time logger.warning( - f"{self.log_prefix} 后期处理器 {task_name} 超时(>{global_config.focus_chat.processor_max_time}s),已跳过,耗时: {elapsed_time:.3f}秒" + f"{self.log_prefix} 后期处理器 {task_name} 超时(>30s),已跳过,耗时: {elapsed_time:.3f}秒" ) except Exception as e: # 对于异常任务,记录已用时间 diff --git a/src/chat/heart_flow/observation/chatting_observation.py b/src/chat/heart_flow/observation/chatting_observation.py index 8888ddb43..d225d3dad 100644 --- a/src/chat/heart_flow/observation/chatting_observation.py +++ b/src/chat/heart_flow/observation/chatting_observation.py @@ -67,7 +67,7 @@ class ChattingObservation(Observation): self.talking_message_str_truncate_short = "" self.name = global_config.bot.nickname self.nick_name = global_config.bot.alias_names - self.max_now_obs_len = global_config.focus_chat.observation_context_size + self.max_now_obs_len = global_config.chat.max_context_size self.overlap_len = global_config.focus_chat.compressed_length self.person_list = [] self.compressor_prompt = "" diff --git a/src/chat/normal_chat/normal_chat_action_modifier.py b/src/chat/normal_chat/normal_chat_action_modifier.py index a3f830861..8cdde145e 100644 --- a/src/chat/normal_chat/normal_chat_action_modifier.py +++ b/src/chat/normal_chat/normal_chat_action_modifier.py @@ -80,7 +80,7 @@ class NormalChatActionModifier: message_list_before_now = get_raw_msg_before_timestamp_with_chat( chat_id=chat_stream.stream_id, timestamp=time.time(), - limit=global_config.focus_chat.observation_context_size, # 使用相同的配置 + limit=global_config.chat.max_context_size, # 使用相同的配置 ) # 构建可读的聊天上下文 diff --git a/src/chat/normal_chat/normal_chat_planner.py b/src/chat/normal_chat/normal_chat_planner.py index 810df2dd9..d3f1e8abc 100644 --- a/src/chat/normal_chat/normal_chat_planner.py +++ b/src/chat/normal_chat/normal_chat_planner.py @@ -122,7 +122,7 @@ class NormalChatPlanner: 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, + limit=global_config.chat.max_context_size, ) chat_context = build_readable_messages( diff --git a/src/chat/replyer/default_generator.py b/src/chat/replyer/default_generator.py index 546a3be78..2e7448600 100644 --- a/src/chat/replyer/default_generator.py +++ b/src/chat/replyer/default_generator.py @@ -350,7 +350,7 @@ class DefaultReplyer: # 使用从处理器传来的选中表达方式 # LLM模式:调用LLM选择5-10个,然后随机选5个 selected_expressions = await expression_selector.select_suitable_expressions_llm( - self.chat_stream.stream_id, chat_history, max_num=12, min_num=2, target_message=target + self.chat_stream.stream_id, chat_history, max_num=8, min_num=2, target_message=target ) if selected_expressions: @@ -476,7 +476,7 @@ class DefaultReplyer: message_list_before_now = get_raw_msg_before_timestamp_with_chat( chat_id=chat_id, timestamp=time.time(), - limit=global_config.focus_chat.observation_context_size, + limit=global_config.chat.max_context_size, ) chat_talking_prompt = build_readable_messages( message_list_before_now, @@ -491,7 +491,7 @@ class DefaultReplyer: message_list_before_now_half = get_raw_msg_before_timestamp_with_chat( chat_id=chat_id, timestamp=time.time(), - limit=int(global_config.focus_chat.observation_context_size * 0.5), + limit=int(global_config.chat.max_context_size * 0.5), ) chat_talking_prompt_half = build_readable_messages( message_list_before_now_half, @@ -654,7 +654,7 @@ class DefaultReplyer: message_list_before_now = get_raw_msg_before_timestamp_with_chat( chat_id=chat_stream.stream_id, timestamp=time.time(), - limit=global_config.focus_chat.observation_context_size, + limit=global_config.chat.max_context_size, ) chat_talking_prompt = build_readable_messages( message_list_before_now, diff --git a/src/config/official_configs.py b/src/config/official_configs.py index df64e0f10..bf065692f 100644 --- a/src/config/official_configs.py +++ b/src/config/official_configs.py @@ -74,6 +74,9 @@ class ChatConfig(ConfigBase): chat_mode: str = "normal" """聊天模式""" + + max_context_size: int = 18 + """上下文长度""" talk_frequency: float = 1 """回复频率阈值""" @@ -267,9 +270,6 @@ class NormalChatConfig(ConfigBase): 选择普通模型的概率为 1 - reasoning_normal_model_probability """ - max_context_size: int = 15 - """上下文长度""" - message_buffer: bool = False """消息缓冲器""" @@ -302,9 +302,6 @@ class NormalChatConfig(ConfigBase): class FocusChatConfig(ConfigBase): """专注聊天配置类""" - observation_context_size: int = 20 - """可观察到的最长上下文大小,超过这个值的上下文会被压缩""" - compressed_length: int = 5 """心流上下文压缩的最短压缩长度,超过心流观察到的上下文长度,会压缩,最短压缩长度为5""" @@ -317,34 +314,18 @@ class FocusChatConfig(ConfigBase): consecutive_replies: float = 1 """连续回复能力,值越高,麦麦连续回复的概率越高""" - parallel_processing: bool = False - """是否允许处理器阶段和回忆阶段并行执行""" - - processor_max_time: int = 25 - """处理器最大时间,单位秒,如果超过这个时间,处理器会自动停止""" @dataclass class FocusChatProcessorConfig(ConfigBase): """专注聊天处理器配置类""" - person_impression_processor: bool = True - """是否启用关系识别处理器(已废弃,为了兼容性保留)""" - - relationship_build_processor: bool = True - """是否启用关系构建处理器""" - - real_time_info_processor: bool = True - """是否启用实时信息提取处理器""" - tool_use_processor: bool = True """是否启用工具使用处理器""" working_memory_processor: bool = True """是否启用工作记忆处理器""" - expression_selector_processor: bool = True - """是否启用表达方式选择处理器""" @dataclass @@ -443,6 +424,9 @@ class MemoryConfig(ConfigBase): @dataclass class MoodConfig(ConfigBase): """情绪配置类""" + + enable_mood: bool = False + """是否启用情绪系统""" mood_update_interval: int = 1 """情绪更新间隔(秒)""" diff --git a/template/bot_config_template.toml b/template/bot_config_template.toml index 5605dea53..cbe65179f 100644 --- a/template/bot_config_template.toml +++ b/template/bot_config_template.toml @@ -1,5 +1,5 @@ [inner] -version = "2.28.0" +version = "2.29.0" #----以下是给开发人员阅读的,如果你只是部署了麦麦,不需要阅读---- #如果你想要修改配置文件,请在修改后将version的值进行变更 @@ -64,6 +64,8 @@ chat_mode = "normal" # 聊天模式 —— 普通模式:normal,专注模式 # chat_mode = "focus" # chat_mode = "auto" +max_context_size = 18 # 上下文长度 + talk_frequency = 1 # 麦麦回复频率,越高,麦麦回复越频繁 time_based_talk_frequency = ["8:00,1", "12:00,1.5", "18:00,2", "01:00,0.5"] @@ -112,7 +114,6 @@ ban_msgs_regex = [ [normal_chat] #普通聊天 #一般回复参数 normal_chat_first_probability = 0.5 # 麦麦回答时选择首要模型的概率(与之相对的,次要模型的概率为1 - normal_chat_first_probability) -max_context_size = 15 #上下文长度 emoji_chance = 0.2 # 麦麦一般回复时使用表情包的概率,设置为1让麦麦自己决定发不发 thinking_timeout = 120 # 麦麦最长思考时间,超过这个时间的思考会放弃(往往是api反应太慢) @@ -124,22 +125,18 @@ emoji_response_penalty = 0 # 对其他人发的表情包回复惩罚系数,设 mentioned_bot_inevitable_reply = true # 提及 bot 必然回复 at_bot_inevitable_reply = true # @bot 必然回复(包含提及) -enable_planner = false # 是否启用动作规划器(实验性功能,与focus_chat共享actions) +enable_planner = false # 是否启用动作规划器(与focus_chat共享actions) [focus_chat] #专注聊天 think_interval = 3 # 思考间隔 单位秒,可以有效减少消耗 consecutive_replies = 1 # 连续回复能力,值越高,麦麦连续回复的概率越高 -processor_max_time = 20 # 处理器最大时间,单位秒,如果超过这个时间,处理器会自动停止 -observation_context_size = 20 # 观察到的最长上下文大小 compressed_length = 8 # 不能大于observation_context_size,心流上下文压缩的最短压缩长度,超过心流观察到的上下文长度,会压缩,最短压缩长度为5 compress_length_limit = 4 #最多压缩份数,超过该数值的压缩上下文会被删除 [focus_chat_processor] # 专注聊天处理器,打开可以实现更多功能,但是会增加token消耗 -person_impression_processor = true # 是否启用关系识别处理器 tool_use_processor = false # 是否启用工具使用处理器 working_memory_processor = false # 是否启用工作记忆处理器,消耗量大 -expression_selector_processor = true # 是否启用表达方式选择处理器 [emoji] max_reg_num = 60 # 表情包最大注册数量 @@ -169,6 +166,7 @@ consolidation_check_percentage = 0.05 # 检查节点比例 memory_ban_words = [ "表情包", "图片", "回复", "聊天记录" ] [mood] # 仅在 普通聊天 有效 +enable_mood = false # 是否启用情绪系统 mood_update_interval = 1.0 # 情绪更新间隔 单位秒 mood_decay_rate = 0.95 # 情绪衰减率 mood_intensity_factor = 1.0 # 情绪强度因子 From 9fa0d70451d73ed5bdb3f5b4e30c42cb9b5fcb0c Mon Sep 17 00:00:00 2001 From: "github-actions[bot]" Date: Tue, 1 Jul 2025 06:59:20 +0000 Subject: [PATCH 11/12] =?UTF-8?q?=F0=9F=A4=96=20=E8=87=AA=E5=8A=A8?= =?UTF-8?q?=E6=A0=BC=E5=BC=8F=E5=8C=96=E4=BB=A3=E7=A0=81=20[skip=20ci]?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/chat/focus_chat/heartFC_chat.py | 11 +++-------- src/config/official_configs.py | 6 ++---- 2 files changed, 5 insertions(+), 12 deletions(-) diff --git a/src/chat/focus_chat/heartFC_chat.py b/src/chat/focus_chat/heartFC_chat.py index dee8519ff..990fe02f9 100644 --- a/src/chat/focus_chat/heartFC_chat.py +++ b/src/chat/focus_chat/heartFC_chat.py @@ -580,10 +580,7 @@ class HeartFChatting: processor_name = processor.__class__.log_prefix async def run_with_timeout(proc=processor): - return await asyncio.wait_for( - proc.process_info(observations=observations), - 30 - ) + return await asyncio.wait_for(proc.process_info(observations=observations), 30) task = asyncio.create_task(run_with_timeout()) @@ -612,9 +609,7 @@ class HeartFChatting: # 记录耗时 processor_time_costs[processor_name] = duration_since_parallel_start except asyncio.TimeoutError: - logger.info( - f"{self.log_prefix} 处理器 {processor_name} 超时(>30s),已跳过" - ) + logger.info(f"{self.log_prefix} 处理器 {processor_name} 超时(>30s),已跳过") processor_time_costs[processor_name] = 30 except Exception as e: logger.error( @@ -672,7 +667,7 @@ class HeartFChatting: try: result = await asyncio.wait_for( proc.process_info(observations=observations, action_type=action_type, action_data=action_data), - 30 + 30, ) end_time = time.time() post_processor_time_costs[name] = end_time - start_time diff --git a/src/config/official_configs.py b/src/config/official_configs.py index bf065692f..fcba7e36d 100644 --- a/src/config/official_configs.py +++ b/src/config/official_configs.py @@ -74,7 +74,7 @@ class ChatConfig(ConfigBase): chat_mode: str = "normal" """聊天模式""" - + max_context_size: int = 18 """上下文长度""" @@ -315,7 +315,6 @@ class FocusChatConfig(ConfigBase): """连续回复能力,值越高,麦麦连续回复的概率越高""" - @dataclass class FocusChatProcessorConfig(ConfigBase): """专注聊天处理器配置类""" @@ -327,7 +326,6 @@ class FocusChatProcessorConfig(ConfigBase): """是否启用工作记忆处理器""" - @dataclass class ExpressionConfig(ConfigBase): """表达配置类""" @@ -424,7 +422,7 @@ class MemoryConfig(ConfigBase): @dataclass class MoodConfig(ConfigBase): """情绪配置类""" - + enable_mood: bool = False """是否启用情绪系统""" From c4ce206780e4170b09677614625141e18869fcf1 Mon Sep 17 00:00:00 2001 From: SengokuCola <1026294844@qq.com> Date: Tue, 1 Jul 2025 15:01:56 +0800 Subject: [PATCH 12/12] =?UTF-8?q?=E4=BF=AE=E6=94=B9rm?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 12 ++++++------ src/chat/focus_chat/heartFC_chat.py | 1 - 2 files changed, 6 insertions(+), 7 deletions(-) diff --git a/README.md b/README.md index c2b9461a1..bbc6ca38b 100644 --- a/README.md +++ b/README.md @@ -44,7 +44,7 @@ ## 🔥 更新和安装 -**最新版本: v0.7.0** ([更新日志](changelogs/changelog.md)) +**最新版本: v0.8.1** ([更新日志](changelogs/changelog.md)) 可前往 [Release](https://github.com/MaiM-with-u/MaiBot/releases/) 页面下载最新版本 可前往 [启动器发布页面](https://github.com/MaiM-with-u/mailauncher/releases/tag/v0.1.0)下载最新启动器 **GitHub 分支说明:** @@ -53,7 +53,7 @@ - `classical`: 旧版本(停止维护) ### 最新版本部署教程 -- [从0.6升级须知](https://docs.mai-mai.org/faq/maibot/update_to_07.html) +- [从0.6/0.7升级须知](https://docs.mai-mai.org/faq/maibot/update_to_07.html) - [🚀 最新版本部署教程](https://docs.mai-mai.org/manual/deployment/mmc_deploy_windows.html) - 基于 MaiCore 的新版本部署方式(与旧版本不兼容) > [!WARNING] @@ -67,10 +67,10 @@ ## 💬 讨论 - [四群](https://qm.qq.com/q/wGePTl1UyY) | - [一群](https://qm.qq.com/q/VQ3XZrWgMs)(已满) | - [二群](https://qm.qq.com/q/RzmCiRtHEW)(已满) | - [五群](https://qm.qq.com/q/JxvHZnxyec)(已满) | - [三群](https://qm.qq.com/q/wlH5eT8OmQ)(已满) + [一群](https://qm.qq.com/q/VQ3XZrWgMs) | + [二群](https://qm.qq.com/q/RzmCiRtHEW) | + [五群](https://qm.qq.com/q/JxvHZnxyec) | + [三群](https://qm.qq.com/q/wlH5eT8OmQ) ## 📚 文档 diff --git a/src/chat/focus_chat/heartFC_chat.py b/src/chat/focus_chat/heartFC_chat.py index 990fe02f9..b7ee87c1d 100644 --- a/src/chat/focus_chat/heartFC_chat.py +++ b/src/chat/focus_chat/heartFC_chat.py @@ -35,7 +35,6 @@ from src.person_info.relationship_builder_manager import relationship_builder_ma install(extra_lines=3) # 超时常量配置 -MEMORY_ACTIVATION_TIMEOUT = 5.0 # 记忆激活任务超时时限(秒) ACTION_MODIFICATION_TIMEOUT = 15.0 # 动作修改任务超时时限(秒) # 定义观察器映射:键是观察器名称,值是 (观察器类, 初始化参数)