fix: 修复去重误判问题,使用exclude_self=True避免自我比较 (Task #1654)
问题:record_user_message调用_is_similar_to_history时使用exclude_self=False 导致自我比较产生100%相似度,误丢弃正常消息(如'服务器基本信息') 修复: 1. 使用exclude_self=True避免自我比较,只与历史消息比对 2. 添加防止重复入库的检查(插入前检查是否已存在完全相同的消息) 3. 测试验证:'服务器基本信息' vs '服务器I' → 20% < 50% → 通过检查
This commit is contained in:
+18
-3
@@ -541,12 +541,27 @@ class MessageHistory:
|
||||
|
||||
def record_user_message(self, platform: str, content: str):
|
||||
"""记录用户消息到user_messages表(环形缓冲区,max 10条/平台)"""
|
||||
# ⭐ 先进行数据库查重:清理后计算相似度,重复率≥50%就跳过
|
||||
if self._is_similar_to_history(platform, content, threshold=0.5):
|
||||
# ⭐ Task #1654: 修复去重误判问题
|
||||
# 问题:exclude_self=False 导致自我比较产生100%相似度,误丢弃正常消息
|
||||
# 修复:使用 exclude_self=True 避免自我比较,只与历史消息比对
|
||||
if self._is_similar_to_history(platform, content, threshold=0.5, exclude_self=True):
|
||||
logger.info(f"[{platform}] 消息去重: 与历史消息相似,跳过入库: {content[:60]!r}")
|
||||
return # 重复消息,不入库
|
||||
|
||||
|
||||
content_hash = hashlib.md5(content.strip().encode('utf-8')).hexdigest()
|
||||
|
||||
# ⭐ 防止重复入库:检查是否已存在完全相同的消息
|
||||
conn = self._get_conn()
|
||||
try:
|
||||
cursor = conn.execute(
|
||||
"SELECT COUNT(*) FROM user_messages WHERE platform=? AND content=?",
|
||||
(platform, content.strip())
|
||||
)
|
||||
if cursor.fetchone()[0] > 0:
|
||||
logger.debug(f"[{platform}] 消息已存在,跳过入库: {content[:60]!r}")
|
||||
return # 已存在,不重复入库
|
||||
finally:
|
||||
conn.close()
|
||||
conn = self._get_conn()
|
||||
try:
|
||||
from datetime import datetime
|
||||
|
||||
Reference in New Issue
Block a user