Merge branch 'dev' of https://github.com/MaiM-with-u/MaiBot into dev
This commit is contained in:
@@ -145,8 +145,10 @@ class ExpressionSelector:
|
||||
|
||||
except Exception as e:
|
||||
logger.error(f"更新表达方式count失败: {e}")
|
||||
|
||||
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]]:
|
||||
|
||||
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个随机表达方式(现在按权重抽取)
|
||||
@@ -195,7 +197,6 @@ class ExpressionSelector:
|
||||
max_num=max_num,
|
||||
)
|
||||
|
||||
|
||||
# 4. 调用LLM
|
||||
try:
|
||||
content, (_, _) = await self.llm_model.generate_response_async(prompt=prompt)
|
||||
|
||||
@@ -181,7 +181,9 @@ class HeartFCMessageReceiver:
|
||||
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))
|
||||
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}]")
|
||||
logger.info(
|
||||
f"[{mes_name}]{userinfo.user_nickname}:{message.processed_plain_text}[当前回复频率: {current_talk_frequency}]"
|
||||
)
|
||||
|
||||
# 8. 关系处理
|
||||
if global_config.relationship.enable_relationship:
|
||||
|
||||
@@ -71,7 +71,9 @@ class ExpressionSelectorProcessor(BaseProcessor):
|
||||
|
||||
try:
|
||||
# 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)
|
||||
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}个)"
|
||||
|
||||
|
||||
@@ -292,8 +292,7 @@ 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_name} 在 {time.strftime('%H:%M:%S', time.localtime(potential_start_time))} - {time.strftime('%H:%M:%S', time.localtime(message_time))} 之间有 {new_segment['message_count']} 条消息"
|
||||
|
||||
@@ -1000,7 +1000,9 @@ class NormalChat:
|
||||
"""
|
||||
# --- 1. 定义参数 ---
|
||||
evaluation_minutes = 10.0
|
||||
target_replies_per_min = global_config.chat.get_current_talk_frequency(self.stream_id) # 目标频率: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:
|
||||
|
||||
@@ -162,8 +162,10 @@ class PromptBuilder:
|
||||
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)
|
||||
|
||||
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:
|
||||
|
||||
@@ -103,10 +103,10 @@ class ChatConfig(ConfigBase):
|
||||
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: 对应的频率值
|
||||
"""
|
||||
@@ -115,31 +115,32 @@ class ChatConfig(ConfigBase):
|
||||
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:
|
||||
@@ -151,13 +152,13 @@ class ChatConfig(ConfigBase):
|
||||
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:
|
||||
@@ -165,20 +166,20 @@ class ChatConfig(ConfigBase):
|
||||
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
|
||||
"""
|
||||
@@ -186,30 +187,30 @@ class ChatConfig(ConfigBase):
|
||||
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
|
||||
"""
|
||||
@@ -217,23 +218,24 @@ class ChatConfig(ConfigBase):
|
||||
parts = stream_config_str.split(":")
|
||||
if len(parts) != 3:
|
||||
return None
|
||||
|
||||
|
||||
platform = parts[0]
|
||||
id_str = parts[1]
|
||||
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
|
||||
|
||||
|
||||
Reference in New Issue
Block a user