优化自动对话逻辑

This commit is contained in:
liuwei
2026-02-02 13:34:32 +08:00
parent d1fc743af9
commit 5be3be48bf
5 changed files with 339 additions and 209 deletions

View File

@@ -1,8 +1,16 @@
import re
import random
from datetime import datetime, time, timedelta
import toml
import os
from loguru import logger
class RoomState:
"""每个群的独立状态"""
def __init__(self):
self.participation_score = 0.0
self.last_active_time = datetime.now()
self.last_bot_reply_time = None # 上次机器人回复的时间
class InterventionBot:
def __init__(self, config_path=None):
@@ -11,62 +19,55 @@ class InterventionBot:
if config_path and os.path.exists(config_path):
self.config = toml.load(config_path)
# 从配置中获取关键词和阈值
# 从配置中获取关键词
keywords = self.config.get("Keywords", {})
time_window = self.config.get("TimeWindow", {})
reply_threshold = self.config.get("ReplyThreshold", {})
# 表情符号库
self.emojis = keywords.get("emojis", [])
# 话题关键词
self.hot_topics = keywords.get("hot_topics", [])
self.fish_keywords = keywords.get("fish_keywords", [])
self.tech_keywords = keywords.get("tech_keywords", [])
self.mechanism_keywords = keywords.get("mechanism_keywords", [])
self.news_keywords = keywords.get("news_keywords",[])
# 早晨签到时间窗口
morning_start_hour = time_window.get("morning_start_hour", 8)
morning_start_minute = time_window.get("morning_start_minute", 0)
morning_end_hour = time_window.get("morning_end_hour", 8)
morning_end_minute = time_window.get("morning_end_minute", 30)
# 拟人化配置
hl_config = self.config.get("HumanLike", {})
self.max_energy = hl_config.get("max_energy", 100.0)
self.energy_recovery_rate = hl_config.get("energy_recovery_per_minute", 1.0)
self.energy_cost = hl_config.get("energy_cost_per_reply", 15.0)
self.participation_inc = hl_config.get("participation_increase_per_msg", 5.0)
self.topic_bonus = hl_config.get("topic_match_bonus", 15.0)
self.participation_threshold = hl_config.get("participation_threshold", 20.0)
self.participation_drop = hl_config.get("participation_drop_factor", 0.8)
self.base_prob = hl_config.get("base_reply_probability", 0.6)
# 机器人全局状态
self.current_energy = self.max_energy
self.last_energy_update_time = datetime.now()
# 群组状态 {room_id: RoomState}
self.room_states = {}
# 辅助功能:早晨时间窗口
time_window = self.config.get("TimeWindow", {})
self.morning_window = (
time(morning_start_hour, morning_start_minute),
time(morning_end_hour, morning_end_minute)
time(time_window.get("morning_start_hour", 8), time_window.get("morning_start_minute", 0)),
time(time_window.get("morning_end_hour", 8), time_window.get("morning_end_minute", 30))
)
# 回复阈值配置
self.messages_per_minute_threshold = reply_threshold.get("messages_per_minute_threshold", 3)
self.analysis_window_minutes = reply_threshold.get("analysis_window_minutes", 5)
def _get_room_state(self, room_id):
if room_id not in self.room_states:
self.room_states[room_id] = RoomState()
return self.room_states[room_id]
# 冷却时间配置(秒)
self.cooldown_seconds = 20
self.last_intervention_time = None
# 最近话题记录
self.last_topic = None
self.last_topic_time = None
self.topic_cooldown = timedelta(seconds=60)
def is_morning_window(self, timestamp):
try:
if isinstance(timestamp, float):
message_datetime = datetime.fromtimestamp(timestamp)
message_time = message_datetime.time()
elif isinstance(timestamp, str):
try:
message_time = datetime.strptime(timestamp, "%Y-%m-%d %H:%M:%S").time()
except ValueError:
try:
message_time = datetime.fromtimestamp(float(timestamp)).time()
except:
return False
else:
return False
return self.morning_window[0] <= message_time <= self.morning_window[1]
except Exception as e:
print(f"[早晨窗口检测] 错误: {e}")
return False
def _update_energy(self):
"""更新全局体力值"""
now = datetime.now()
minutes_passed = (now - self.last_energy_update_time).total_seconds() / 60.0
recovered = minutes_passed * self.energy_recovery_rate
self.current_energy = min(self.max_energy, self.current_energy + recovered)
self.last_energy_update_time = now
logger.debug(f"[Energy] Recovered {recovered:.2f}, Current: {self.current_energy:.2f}")
def detect_topic(self, message):
if not isinstance(message, str):
@@ -76,174 +77,117 @@ class InterventionBot:
return "fish"
if any(keyword in message_lower for keyword in self.tech_keywords):
return "tech"
if any(keyword in message_lower for keyword in self.mechanism_keywords):
return "mechanism"
if any(keyword in message_lower for keyword in self.news_keywords):
return "news"
if any(keyword in message_lower for keyword in self.hot_topics):
return "hot_topic"
return None
def rule_morning_signin(self, timestamp, messages):
return self.is_morning_window(timestamp) and any("签到" in msg or "" in msg for msg in messages[-5:])
def rule_hot_topic(self, message, messages):
return self.detect_topic(message) == "hot_topic" and len(
[m for m in messages[-5:] if self.detect_topic(m) == "hot_topic"]) >= 3
def rule_tech_discussion(self, message, messages):
return self.detect_topic(message) == "tech"
def rule_fish_discussion(self, message, messages):
return self.detect_topic(message) == "fish"
def rule_mechanism_interaction(self, message, messages):
return self.detect_topic(message) == "mechanism"
def rule_humor_tease(self, message, messages):
return any(emoji in message for emoji in self.emojis) or "哈哈" in message or len(
[m for m in messages[-5:] if any(e in m for e in self.emojis)]) >= 2
def rule_news_reaction(self, message, messages):
return self.detect_topic(message) == "news"
def rule_high_reply_rate(self, timestamp, chat_log):
def is_morning_window(self, timestamp):
try:
if isinstance(timestamp, float):
current_time = datetime.fromtimestamp(timestamp)
elif isinstance(timestamp, str):
try:
current_time = datetime.strptime(timestamp, "%Y-%m-%d %H:%M:%S")
except ValueError:
try:
current_time = datetime.fromtimestamp(float(timestamp))
except:
current_time = datetime.now()
else:
current_time = datetime.now()
window_start = current_time - timedelta(minutes=self.analysis_window_minutes)
recent_messages = []
for msg in chat_log:
try:
msg_timestamp = msg.get("timestamp")
if isinstance(msg_timestamp, float):
msg_time = datetime.fromtimestamp(msg_timestamp)
elif isinstance(msg_timestamp, str):
try:
msg_time = datetime.strptime(msg_timestamp, "%Y-%m-%d %H:%M:%S")
except ValueError:
try:
msg_time = datetime.fromtimestamp(float(msg_timestamp))
except:
continue
else:
continue
if window_start <= msg_time <= current_time:
recent_messages.append(msg)
except (ValueError, KeyError, TypeError):
continue
if len(recent_messages) < self.messages_per_minute_threshold:
return False
messages_per_minute = len(recent_messages) / self.analysis_window_minutes
if messages_per_minute >= self.messages_per_minute_threshold:
print(f"[高频率检测] 当前消息频率: {messages_per_minute:.2f}/分钟,阈值: {self.messages_per_minute_threshold}/分钟")
return messages_per_minute >= self.messages_per_minute_threshold
except Exception as e:
print(f"[高频率检测] 错误: {e}")
# 简化时间处理这里假设timestamp通常是当前时间附近
now = datetime.now()
return self.morning_window[0] <= now.time() <= self.morning_window[1]
except:
return False
def should_intervene(self, timestamp, message, messages, chat_log):
rules = [
self.rule_morning_signin,
self.rule_hot_topic,
self.rule_tech_discussion,
self.rule_fish_discussion,
self.rule_mechanism_interaction,
self.rule_humor_tease,
self.rule_news_reaction,
self.rule_high_reply_rate
]
def calculate_participation_boost(self, message, messages, is_at=False):
"""计算这条消息带来的参与度提升"""
if is_at:
return 100.0 # 被AT直接拉满
current_time = datetime.now() if not isinstance(timestamp, datetime) else timestamp
if isinstance(timestamp, str):
try:
current_time = datetime.strptime(timestamp, "%Y-%m-%d %H:%M:%S")
except:
current_time = datetime.now()
elif isinstance(timestamp, float):
current_time = datetime.fromtimestamp(timestamp)
boost = self.participation_inc
# 话题加成
topic = self.detect_topic(message)
if topic:
boost += self.topic_bonus
# 关键词加成 (早安/表情等)
if any(e in message for e in self.emojis):
boost += 5
if "签到" in message or "" in message:
if self.is_morning_window(None):
boost += 20 # 早上问好权重高
return boost
if self.last_intervention_time:
if (current_time - self.last_intervention_time).total_seconds() < self.cooldown_seconds:
return False
def should_intervene(self, room_id, timestamp, message, messages, chat_log, is_at=False):
"""
核心判定逻辑
:param room_id: 群ID
:param timestamp: 消息时间
:param message: 当前消息内容
:param messages: 最近消息列表(文本)
:param chat_log: 完整聊天记录对象
:param is_at: 是否被AT
"""
self._update_energy()
state = self._get_room_state(room_id)
# 1. 增加参与度Listening
boost = self.calculate_participation_boost(message, messages, is_at)
# 连续对话奖励:如果机器人在最近 2 分钟内回复过,说明可能在对话中,参与度增加翻倍
if state.last_bot_reply_time:
time_since_last_reply = (datetime.now() - state.last_bot_reply_time).total_seconds()
if time_since_last_reply < 120: # 2分钟内
boost *= 2.0
logger.debug(f"[{room_id}] 连续对话奖励触发 (上次回复 {int(time_since_last_reply)}s 前)")
current_topic = self.detect_topic(message)
if self.last_topic == current_topic and self.last_topic_time:
if (current_time - self.last_topic_time) < self.topic_cooldown:
return False
state.participation_score += boost
state.last_active_time = datetime.now()
logger.debug(f"[{room_id}] 收到消息: '{message}' | 参与度+{boost} -> {state.participation_score:.2f} | 体力: {self.current_energy:.2f}")
for rule in rules:
if rule == self.rule_morning_signin:
if rule(timestamp, messages):
self.last_intervention_time = current_time
self.last_topic = current_topic
self.last_topic_time = current_time
return True
elif rule == self.rule_high_reply_rate:
if rule(timestamp, chat_log):
self.last_intervention_time = current_time
self.last_topic = current_topic
self.last_topic_time = current_time
return True
elif rule(message, messages):
self.last_intervention_time = current_time
self.last_topic = current_topic
self.last_topic_time = current_time
return True
# 2. 检查阈值
if state.participation_score < self.participation_threshold:
return False
# 3. 检查体力
if self.current_energy < self.energy_cost:
logger.debug(f"[{room_id}] 体力不足 ({self.current_energy:.2f} < {self.energy_cost}),跳过")
return False
# 4. 概率判定
# 参与度越高,概率越高;体力越高,概率越高
# 归一化因子
participation_factor = min(state.participation_score / 100.0, 1.5) # 上限1.5倍
energy_factor = self.current_energy / self.max_energy
final_prob = self.base_prob * participation_factor * energy_factor
# 被AT必然回复
if is_at:
final_prob = 1.0
# 随机判定
rand_val = random.random()
should_reply = rand_val < final_prob
logger.debug(f"[{room_id}] 判定: Prob={final_prob:.2f} (Base={self.base_prob} * Part={participation_factor:.2f} * Energy={energy_factor:.2f}) vs Rand={rand_val:.2f} -> {should_reply}")
if should_reply:
# 扣除消耗
self.current_energy -= self.energy_cost
# 更新状态
state.last_bot_reply_time = datetime.now()
# 降低参与度(满足了表达欲)
# 改为减法,保留部分参与度以便连续对话
# 如果是高参与度(>50减去 30否则减半
if state.participation_score > 50:
state.participation_score = max(0, state.participation_score - 40)
else:
state.participation_score *= 0.5
return True
return False
def process_message(self, timestamp, message, messages, chat_log):
return self.should_intervene(timestamp, message, messages, chat_log)
def process_chat_log(self, chat_log):
messages = [line["message"] for line in chat_log]
results = []
for i, line in enumerate(chat_log):
timestamp = line["timestamp"]
message = line["message"]
intervention = self.process_message(timestamp, message, messages[:i + 1], chat_log)
results.append({
"timestamp": timestamp,
"message": message,
"intervention": intervention
})
return results
if __name__ == "__main__":
sample_chat_log = [
{"timestamp": "2025-03-14 08:06:38", "user_id": "Jyunere", "message": "白嫖马斯克每个月150刀的额度应该能玩很久了。"},
{"timestamp": "2025-03-14 08:06:54", "user_id": "Jyunere", "message": "啥情况?卷了?"},
{"timestamp": "2025-03-14 08:07:20", "user_id": "wxid_qx4z0jq3rp3122", "message": "那你喝咖啡就好了"},
{"timestamp": "2025-03-14 09:12:28", "user_id": "Jyunere", "message": "我同事的鸿蒙确实流畅。"},
{"timestamp": "2025-03-14 09:35:21", "user_id": "Jyunere", "message": "垃圾MIUI"},
{"timestamp": "2025-05-21 14:31:57", "user_id": "wxid_4re8ddo26dxb52", "message": "年轻人随随便便就能深蹲200"},
{"timestamp": "2025-05-21 14:32:20", "user_id": "liu79830956", "message": "@水牛 过分了啊,报错还扣积分 赔我200"},
{"timestamp": "2025-05-21 14:32:39", "user_id": "Jyunere", "message": "哈哈,识别到指令了。"},
{"timestamp": "2025-05-21 14:32:42", "user_id": "wxid_z8uo70zywfpn12", "message": "检测到天 气了"},
{"timestamp": "2025-05-21 14:35:08", "user_id": "liu79830956", "message": "这螺蛳粉估计要明天也吃不上了[旺柴]"}
]
bot = InterventionBot()
results = bot.process_chat_log(sample_chat_log)
for result in results:
print(f"[{result['timestamp']}] Message: {result['message']}")
print(f"Intervention: {result['intervention']}")
print("-" * 50)
def rule_high_reply_rate(self, timestamp, chat_log):
# 保留这个方法以兼容 main.py 的调用,或者在 main.py 中移除
# 这里我们可以简单的返回 False因为新的逻辑已经包含了频率控制通过体力值
return False