pull/333/head
春河晴 2025-03-13 19:53:03 +09:00
parent 7e63c36aa4
commit b4e9fd113c
No known key found for this signature in database
1 changed files with 124 additions and 114 deletions

View File

@ -139,14 +139,16 @@ class WillingManager:
if chat_id not in self.chat_high_willing_duration: if chat_id not in self.chat_high_willing_duration:
self.chat_high_willing_duration[chat_id] = random.randint(180, 240) # 3-4分钟 self.chat_high_willing_duration[chat_id] = random.randint(180, 240) # 3-4分钟
async def change_reply_willing_received(self, async def change_reply_willing_received(
chat_stream: ChatStream, self,
topic: str = None, chat_stream: ChatStream,
is_mentioned_bot: bool = False, topic: str = None,
config = None, is_mentioned_bot: bool = False,
is_emoji: bool = False, config=None,
interested_rate: float = 0, is_emoji: bool = False,
sender_id: str = None) -> float: interested_rate: float = 0,
sender_id: str = None,
) -> float:
"""改变指定聊天流的回复意愿并返回回复概率""" """改变指定聊天流的回复意愿并返回回复概率"""
# 获取或创建聊天流 # 获取或创建聊天流
stream = chat_stream stream = chat_stream
@ -180,10 +182,15 @@ class WillingManager:
# 改进的追问检测逻辑 # 改进的追问检测逻辑
time_window = 180 # 扩大到3分钟 time_window = 180 # 扩大到3分钟
max_msgs = 8 # 增加消息数量阈值 max_msgs = 8 # 增加消息数量阈值
# 1. 同一用户短时间内发送多条消息 # 1. 同一用户短时间内发送多条消息
if sender_id and sender_id == last_sender and current_time - last_reply_time < time_window and msg_count <= max_msgs: if (
sender_id
and sender_id == last_sender
and current_time - last_reply_time < time_window
and msg_count <= max_msgs
):
is_follow_up_question = True is_follow_up_question = True
in_conversation_context = True in_conversation_context = True
self.chat_conversation_context[chat_id] = True self.chat_conversation_context[chat_id] = True
@ -191,9 +198,9 @@ class WillingManager:
# 根据消息间隔动态调整回复意愿提升 # 根据消息间隔动态调整回复意愿提升
time_since_last = current_time - last_reply_time time_since_last = current_time - last_reply_time
if time_since_last < 60: # 1分钟内 if time_since_last < 60: # 1分钟内
current_willing += 0.4 # 快速跟进,提高更多 current_willing += 0.4 # 快速跟进,提高更多
else: else:
current_willing += 0.2 # 较慢跟进,提高较少 current_willing += 0.2 # 较慢跟进,提高较少
logger.debug(f"检测到追问 (同一用户), 提高回复意愿, 时间间隔: {time_since_last:.1f}") logger.debug(f"检测到追问 (同一用户), 提高回复意愿, 时间间隔: {time_since_last:.1f}")
@ -201,7 +208,7 @@ class WillingManager:
elif in_conversation_context and current_time - last_reply_time < time_window: elif in_conversation_context and current_time - last_reply_time < time_window:
# 处于活跃对话中,但不是同一用户,视为对话延续 # 处于活跃对话中,但不是同一用户,视为对话延续
in_conversation_context = True in_conversation_context = True
logger.debug(f"检测到对话延续 (不同用户), 保持对话上下文") logger.debug("检测到对话延续 (不同用户), 保持对话上下文")
# 特殊情况处理 # 特殊情况处理
if is_mentioned_bot: if is_mentioned_bot:
@ -249,10 +256,10 @@ class WillingManager:
# 根据连续回复次数调整概率 # 根据连续回复次数调整概率
if consecutive_replies >= 4: if consecutive_replies >= 4:
reply_probability *= 0.01 # 连续回复4次或以上降低到1% reply_probability *= 0.01 # 连续回复4次或以上降低到1%
logger.debug(f"连续回复次数 >= 3, 降低回复概率到1%") logger.debug("连续回复次数 >= 3, 降低回复概率到1%")
elif consecutive_replies >= 3: elif consecutive_replies >= 3:
reply_probability *= 0.1 # 连续回复3次降低到10% reply_probability *= 0.1 # 连续回复3次降低到10%
logger.debug(f"连续回复次数 = 2, 降低回复概率到10%") logger.debug("连续回复次数 = 2, 降低回复概率到10%")
# 检查是否为冷群,提高冷群的回复概率 # 检查是否为冷群,提高冷群的回复概率
if chat_stream.group_info: if chat_stream.group_info:
@ -373,7 +380,7 @@ class WillingManager:
self.chat_last_reply_time, self.chat_last_reply_time,
self.chat_last_sender_id, self.chat_last_sender_id,
self.chat_conversation_context, self.chat_conversation_context,
self.chat_consecutive_replies self.chat_consecutive_replies,
] ]
for dictionary in dictionaries: for dictionary in dictionaries:
@ -444,7 +451,7 @@ class WillingManager:
# 通常聊天流ID中会包含群组ID信息 # 通常聊天流ID中会包含群组ID信息
# 根据实际格式进行提取,这里假设格式为 platform:group_id:user_id # 根据实际格式进行提取,这里假设格式为 platform:group_id:user_id
try: try:
parts = chat_id.split(':') parts = chat_id.split(":")
if len(parts) >= 2: if len(parts) >= 2:
return f"{parts[0]}:{parts[1]}" # platform:group_id作为群组标识 return f"{parts[0]}:{parts[1]}" # platform:group_id作为群组标识
return chat_id return chat_id
@ -509,11 +516,13 @@ class WillingManager:
scaled_active_users = active_users_count / scaled_interval scaled_active_users = active_users_count / scaled_interval
# 判断是否为冷群 # 判断是否为冷群
is_cold = (scaled_active_users < 5.0 and scaled_message_count < 20.0) is_cold = scaled_active_users < 5.0 and scaled_message_count < 20.0
# 更新冷群状态 # 更新冷群状态
self.group_is_cold[group_id] = is_cold self.group_is_cold[group_id] = is_cold
logger.debug(f"{group_id} 活跃度检查: 消息数={message_count}, 活跃用户数={active_users_count}, 时间间隔={interval:.1f}秒, 判定为{'冷群' if is_cold else '活跃群'}") logger.debug(
f"{group_id} 活跃度检查: 消息数={message_count}, 活跃用户数={active_users_count}, 时间间隔={interval:.1f}秒, 判定为{'冷群' if is_cold else '活跃群'}"
)
async def _cold_group_check(self): async def _cold_group_check(self):
"""定期检查所有群组的冷热状态""" """定期检查所有群组的冷热状态"""
@ -550,5 +559,6 @@ class WillingManager:
if inactive_groups: if inactive_groups:
logger.debug(f"已清理 {len(inactive_groups)} 个不活跃的群组记录") logger.debug(f"已清理 {len(inactive_groups)} 个不活跃的群组记录")
# 创建全局实例 # 创建全局实例
willing_manager = WillingManager() willing_manager = WillingManager()