diff --git a/src/chat/express/expression_selector.py b/src/chat/express/expression_selector.py index 58a53ac60..88d7e7451 100644 --- a/src/chat/express/expression_selector.py +++ b/src/chat/express/expression_selector.py @@ -23,7 +23,7 @@ def init_prompt(): 以下是可选的表达情境: {all_situations} -请你分析聊天内容的语境、情绪、话题类型,从上述情境中选择最适合当前聊天情境的5-10个情境。 +请你分析聊天内容的语境、情绪、话题类型,从上述情境中选择最适合当前聊天情境的{min_num}-{max_num}个情境。 考虑因素包括: 1. 聊天的情绪氛围(轻松、严肃、幽默等) 2. 话题类型(日常、技术、游戏、情感等) @@ -32,11 +32,11 @@ def init_prompt(): 请以JSON格式输出,只需要输出选中的情境编号: 例如: {{ - "selected_situations": [2, 3, 5, 7, 9, 12, 15, 18, 21, 25] + "selected_situations": [2, 3, 5, 7, 19, 22, 25, 38, 39, 45, 48 , 64] }} 例如: {{ - "selected_situations": [1, 4, 7, 9, 13, 18, 24] + "selected_situations": [1, 4, 7, 9, 23, 38, 44] }} 请严格按照JSON格式输出,不要包含其他内容: @@ -145,8 +145,8 @@ class ExpressionSelector: except Exception as e: logger.error(f"更新表达方式count失败: {e}") - - async def select_suitable_expressions_llm(self, chat_id: str, chat_info: str) -> List[Dict[str, str]]: + + async def select_suitable_expressions_llm(self, chat_id: str, chat_info: str, max_num: int = 10, min_num: int = 5) -> List[Dict[str, str]]: """使用LLM选择适合的表达方式""" # 1. 获取35个随机表达方式(现在按权重抽取) @@ -191,9 +191,10 @@ class ExpressionSelector: bot_name=global_config.bot.nickname, chat_observe_info=chat_info, all_situations=all_situations_str, + min_num=min_num, + max_num=max_num, ) - print(prompt) # 4. 调用LLM try: diff --git a/src/chat/express/exprssion_learner.py b/src/chat/express/exprssion_learner.py index bbe7680cd..13f5a65d4 100644 --- a/src/chat/express/exprssion_learner.py +++ b/src/chat/express/exprssion_learner.py @@ -96,7 +96,7 @@ class ExpressionLearner: current_chat_type = "private" typed_chat_id = f"{platform}:{chat_stream.user_info.user_id}:{current_chat_type}" - logger.info(f"正在为 {typed_chat_id} 查找互通组...") + logger.debug(f"正在为 {typed_chat_id} 查找互通组...") found_group = None for group in expression_groups: @@ -108,7 +108,7 @@ class ExpressionLearner: break if not found_group: - logger.info(f"未找到互通组,仅加载 {chat_id} 的表达方式") + logger.debug(f"未找到互通组,仅加载 {chat_id} 的表达方式") if found_group: # 从带类型的id中解析出原始id @@ -121,7 +121,7 @@ class ExpressionLearner: except Exception: logger.warning(f"无法解析互通组中的ID: {item}") chat_ids_to_load = parsed_ids - logger.info(f"将要加载以下id的表达方式: {chat_ids_to_load}") + logger.debug(f"将要加载以下id的表达方式: {chat_ids_to_load}") learnt_style_expressions = [] learnt_grammar_expressions = [] diff --git a/src/chat/focus_chat/heartflow_message_processor.py b/src/chat/focus_chat/heartflow_message_processor.py index f5bd091f9..5ba0d50fe 100644 --- a/src/chat/focus_chat/heartflow_message_processor.py +++ b/src/chat/focus_chat/heartflow_message_processor.py @@ -180,7 +180,8 @@ class HeartFCMessageReceiver: # 7. 日志记录 mes_name = chat.group_info.group_name if chat.group_info else "私聊" # current_time = time.strftime("%H:%M:%S", time.localtime(message.message_info.time)) - logger.info(f"[{mes_name}]{userinfo.user_nickname}:{message.processed_plain_text}") + current_talk_frequency = global_config.chat.get_current_talk_frequency(chat.stream_id) + logger.info(f"[{mes_name}]{userinfo.user_nickname}:{message.processed_plain_text}[当前回复频率: {current_talk_frequency}]") # 8. 关系处理 if global_config.relationship.enable_relationship: diff --git a/src/chat/focus_chat/info_processors/expression_selector_processor.py b/src/chat/focus_chat/info_processors/expression_selector_processor.py index 11ed4e1d1..4ee3f274f 100644 --- a/src/chat/focus_chat/info_processors/expression_selector_processor.py +++ b/src/chat/focus_chat/info_processors/expression_selector_processor.py @@ -71,9 +71,7 @@ class ExpressionSelectorProcessor(BaseProcessor): try: # LLM模式:调用LLM选择5-10个,然后随机选5个 - selected_expressions = await expression_selector.select_suitable_expressions_llm( - self.subheartflow_id, chat_info - ) + selected_expressions = await expression_selector.select_suitable_expressions_llm(self.subheartflow_id, chat_info, max_num=12, min_num=2) cache_size = len(selected_expressions) if selected_expressions else 0 mode_desc = f"LLM模式(已缓存{cache_size}个)" diff --git a/src/chat/focus_chat/info_processors/relationship_processor.py b/src/chat/focus_chat/info_processors/relationship_processor.py index e9ea6d913..393cbf5f0 100644 --- a/src/chat/focus_chat/info_processors/relationship_processor.py +++ b/src/chat/focus_chat/info_processors/relationship_processor.py @@ -292,8 +292,11 @@ class PersonImpressionpProcessor(BaseProcessor): "message_count": self._count_messages_in_timerange(potential_start_time, message_time), } segments.append(new_segment) + + + person_name = get_person_info_manager().get_value(person_id, "person_name") logger.info( - f"{self.log_prefix} 为用户 {person_id} 创建新消息段: 时间范围 {time.strftime('%H:%M:%S', time.localtime(potential_start_time))} - {time.strftime('%H:%M:%S', time.localtime(message_time))}, 消息数: {new_segment['message_count']}" + f"{self.log_prefix} 眼熟用户 {person_name} 在 {time.strftime('%H:%M:%S', time.localtime(potential_start_time))} - {time.strftime('%H:%M:%S', time.localtime(message_time))} 之间有 {new_segment['message_count']} 条消息" ) self._save_cache() return @@ -339,7 +342,7 @@ class PersonImpressionpProcessor(BaseProcessor): "message_count": self._count_messages_in_timerange(potential_start_time, message_time), } segments.append(new_segment) - logger.info(f"{self.log_prefix} 为用户 {person_id} 创建新消息段(超过10条消息间隔): {new_segment}") + logger.info(f"{self.log_prefix} 重新眼熟用户 {person_name} 创建新消息段(超过10条消息间隔): {new_segment}") self._save_cache() diff --git a/src/chat/message_receive/chat_stream.py b/src/chat/message_receive/chat_stream.py index 93da0bdd1..55d296db9 100644 --- a/src/chat/message_receive/chat_stream.py +++ b/src/chat/message_receive/chat_stream.py @@ -172,12 +172,12 @@ class ChatManager: key = "_".join(components) return hashlib.md5(key.encode()).hexdigest() - def get_stream_id(self, platform: str, chat_id: str, is_group: bool = True) -> str: + def get_stream_id(self, platform: str, id: str, is_group: bool = True) -> str: """获取聊天流ID""" if is_group: - components = [platform, str(chat_id)] + components = [platform, str(id)] else: - components = [platform, str(chat_id), "private"] + components = [platform, str(id), "private"] key = "_".join(components) return hashlib.md5(key.encode()).hexdigest() diff --git a/src/chat/normal_chat/normal_chat.py b/src/chat/normal_chat/normal_chat.py index 5a0cbd420..a46db1d0b 100644 --- a/src/chat/normal_chat/normal_chat.py +++ b/src/chat/normal_chat/normal_chat.py @@ -1000,7 +1000,7 @@ class NormalChat: """ # --- 1. 定义参数 --- evaluation_minutes = 10.0 - target_replies_per_min = global_config.chat.talk_frequency # 目标频率:e.g. 1条/分钟 + target_replies_per_min = global_config.chat.get_current_talk_frequency(self.stream_id) # 目标频率:e.g. 1条/分钟 target_replies_in_window = target_replies_per_min * evaluation_minutes # 10分钟内的目标回复数 if target_replies_in_window <= 0: diff --git a/src/chat/normal_chat/normal_prompt.py b/src/chat/normal_chat/normal_prompt.py index 35a553ecb..a70ec0021 100644 --- a/src/chat/normal_chat/normal_prompt.py +++ b/src/chat/normal_chat/normal_prompt.py @@ -163,7 +163,7 @@ class PromptBuilder: show_actions=True, ) - expressions = await expression_selector.select_suitable_expressions_llm(chat_stream.stream_id, chat_talking_prompt_half) + 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: diff --git a/src/config/official_configs.py b/src/config/official_configs.py index 333516f39..82e02d09e 100644 --- a/src/config/official_configs.py +++ b/src/config/official_configs.py @@ -72,12 +72,171 @@ class ChatConfig(ConfigBase): talk_frequency: float = 1 """回复频率阈值""" + # 修改:基于时段的回复频率配置,改为数组格式 + time_based_talk_frequency: list[str] = field(default_factory=lambda: []) + """ + 基于时段的回复频率配置(全局) + 格式:["HH:MM,frequency", "HH:MM,frequency", ...] + 示例:["8:00,1", "12:00,2", "18:00,1.5", "00:00,0.5"] + 表示从该时间开始使用该频率,直到下一个时间点 + """ + + # 新增:基于聊天流的个性化时段频率配置 + talk_frequency_adjust: list[list[str]] = field(default_factory=lambda: []) + """ + 基于聊天流的个性化时段频率配置 + 格式:[["platform:chat_id:type", "HH:MM,frequency", "HH:MM,frequency", ...], ...] + 示例:[ + ["qq:1026294844:group", "12:20,1", "16:10,2", "20:10,1", "00:10,0.3"], + ["qq:729957033:group", "8:20,1", "12:10,2", "20:10,1.5", "00:10,0.2"] + ] + 每个子列表的第一个元素是聊天流标识符,后续元素是"时间,频率"格式 + 表示从该时间开始使用该频率,直到下一个时间点 + """ + auto_focus_threshold: float = 1.0 """自动切换到专注聊天的阈值,越低越容易进入专注聊天""" exit_focus_threshold: float = 1.0 """自动退出专注聊天的阈值,越低越容易退出专注聊天""" + def get_current_talk_frequency(self, chat_stream_id: str = None) -> float: + """ + 根据当前时间和聊天流获取对应的 talk_frequency + + Args: + chat_stream_id: 聊天流ID,格式为 "platform:chat_id:type" + + Returns: + float: 对应的频率值 + """ + # 优先检查聊天流特定的配置 + if chat_stream_id and self.talk_frequency_adjust: + stream_frequency = self._get_stream_specific_frequency(chat_stream_id) + if stream_frequency is not None: + return stream_frequency + + # 如果没有聊天流特定配置,检查全局时段配置 + if self.time_based_talk_frequency: + global_frequency = self._get_time_based_frequency(self.time_based_talk_frequency) + if global_frequency is not None: + return global_frequency + + # 如果都没有匹配,返回默认值 + return self.talk_frequency + + def _get_time_based_frequency(self, time_freq_list: list[str]) -> float: + """ + 根据时间配置列表获取当前时段的频率 + + Args: + time_freq_list: 时间频率配置列表,格式为 ["HH:MM,frequency", ...] + + Returns: + float: 频率值,如果没有配置则返回 None + """ + from datetime import datetime + current_time = datetime.now().strftime("%H:%M") + current_hour, current_minute = map(int, current_time.split(":")) + current_minutes = current_hour * 60 + current_minute + + # 解析时间频率配置 + time_freq_pairs = [] + for time_freq_str in time_freq_list: + try: + time_str, freq_str = time_freq_str.split(",") + hour, minute = map(int, time_str.split(":")) + frequency = float(freq_str) + minutes = hour * 60 + minute + time_freq_pairs.append((minutes, frequency)) + except (ValueError, IndexError): + continue + + if not time_freq_pairs: + return None + + # 按时间排序 + time_freq_pairs.sort(key=lambda x: x[0]) + + # 查找当前时间对应的频率 + current_frequency = None + for minutes, frequency in time_freq_pairs: + if current_minutes >= minutes: + current_frequency = frequency + else: + break + + # 如果当前时间在所有配置时间之前,使用最后一个时间段的频率(跨天逻辑) + if current_frequency is None and time_freq_pairs: + current_frequency = time_freq_pairs[-1][1] + + return current_frequency + + def _get_stream_specific_frequency(self, chat_stream_id: str) -> float: + """ + 获取特定聊天流在当前时间的频率 + + Args: + chat_stream_id: 聊天流ID(哈希值) + + Returns: + float: 频率值,如果没有配置则返回 None + """ + # 查找匹配的聊天流配置 + for config_item in self.talk_frequency_adjust: + if not config_item or len(config_item) < 2: + continue + + stream_config_str = config_item[0] # 例如 "qq:1026294844:group" + + # 解析配置字符串并生成对应的 chat_id + config_chat_id = self._parse_stream_config_to_chat_id(stream_config_str) + if config_chat_id is None: + continue + + # 比较生成的 chat_id + if config_chat_id != chat_stream_id: + continue + + # 使用通用的时间频率解析方法 + return self._get_time_based_frequency(config_item[1:]) + + return None + + def _parse_stream_config_to_chat_id(self, stream_config_str: str) -> str: + """ + 解析流配置字符串并生成对应的 chat_id + + Args: + stream_config_str: 格式为 "platform:id:type" 的字符串 + + Returns: + str: 生成的 chat_id,如果解析失败则返回 None + """ + try: + parts = stream_config_str.split(":") + if len(parts) != 3: + return None + + platform = parts[0] + id_str = parts[1] + stream_type = parts[2] + + # 判断是否为群聊 + is_group = stream_type == "group" + + # 使用与 ChatStream.get_stream_id 相同的逻辑生成 chat_id + import hashlib + if is_group: + components = [platform, str(id_str)] + else: + components = [platform, str(id_str), "private"] + key = "_".join(components) + return hashlib.md5(key.encode()).hexdigest() + + except (ValueError, IndexError): + return None + @dataclass class MessageReceiveConfig(ConfigBase): diff --git a/src/plugins/built_in/core_actions/no_reply.py b/src/plugins/built_in/core_actions/no_reply.py index c73fa95b4..4929615f1 100644 --- a/src/plugins/built_in/core_actions/no_reply.py +++ b/src/plugins/built_in/core_actions/no_reply.py @@ -241,7 +241,7 @@ class NoReplyAction(BaseAction): if sender_id == user_id: bot_message_count += 1 - talk_frequency_threshold = global_config.chat.talk_frequency * 10 + talk_frequency_threshold = global_config.chat.get_current_talk_frequency(self.chat_id) * 10 if bot_message_count > talk_frequency_threshold: over_count = bot_message_count - talk_frequency_threshold @@ -444,7 +444,7 @@ class NoReplyAction(BaseAction): # 计算阈值频率:使用 exit_focus_threshold * 1.5 threshold_multiplier = global_config.chat.exit_focus_threshold * 1.5 - threshold_frequency = global_config.chat.talk_frequency * threshold_multiplier + threshold_frequency = global_config.chat.get_current_talk_frequency(self.chat_id) * threshold_multiplier # 判断是否超过阈值 if current_frequency > threshold_frequency: diff --git a/template/bot_config_template.toml b/template/bot_config_template.toml index b1c22eb18..f3703e8b3 100644 --- a/template/bot_config_template.toml +++ b/template/bot_config_template.toml @@ -1,5 +1,5 @@ [inner] -version = "2.26.0" +version = "2.27.0" #----以下是给开发人员阅读的,如果你只是部署了麦麦,不需要阅读---- #如果你想要修改配置文件,请在修改后将version的值进行变更 @@ -62,6 +62,31 @@ chat_mode = "normal" # 聊天模式 —— 普通模式:normal,专注模式 talk_frequency = 1 # 麦麦回复频率,越高,麦麦回复越频繁 +time_based_talk_frequency = ["8:00,1", "12:00,1.5", "18:00,2", "01:00,0.5"] +# 基于时段的回复频率配置(可选) +# 格式:time_based_talk_frequency = ["HH:MM,frequency", ...] +# 示例: +# time_based_talk_frequency = ["8:00,1", "12:00,2", "18:00,1.5", "00:00,0.5"] +# 说明:表示从该时间开始使用该频率,直到下一个时间点 +# 注意:如果没有配置,则使用上面的默认 talk_frequency 值 + +talk_frequency_adjust = [ + ["qq:114514:group", "12:20,1", "16:10,2", "20:10,1", "00:10,0.3"], + ["qq:1919810:private", "8:20,1", "12:10,2", "20:10,1.5", "00:10,0.2"] +] + +# 基于聊天流的个性化时段频率配置(可选) +# 格式:talk_frequency_adjust = [["platform:id:type", "HH:MM,frequency", ...], ...] +# 说明: +# - 第一个元素是聊天流标识符,格式为 "platform:id:type" +# - platform: 平台名称(如 qq) +# - id: 群号或用户QQ号 +# - type: group表示群聊,private表示私聊 +# - 后续元素是"时间,频率"格式,表示从该时间开始使用该频率,直到下一个时间点 +# - 优先级:聊天流特定配置 > 全局时段配置 > 默认 talk_frequency +# - 时间支持跨天,例如 "00:10,0.3" 表示从凌晨0:10开始使用频率0.3 +# - 系统会自动将 "platform:id:type" 转换为内部的哈希chat_id进行匹配 + auto_focus_threshold = 1 # 自动切换到专注聊天的阈值,越低越容易进入专注聊天 exit_focus_threshold = 1 # 自动退出专注聊天的阈值,越低越容易退出专注聊天 # 普通模式下,麦麦会针对感兴趣的消息进行回复,token消耗量较低