fix(admin): 优化聊天日志报表
This commit is contained in:
1 parent
c38a56536e
commit
757231e3d5
14 files changed
+584
-108
No files matched your search
@@ -22,21 +22,19 @@ backend/app/services/hot_faq_aggregator.py
|
||||
|
||||
## 最佳答案选取规则(HOT_FAQ_ANSWER_RULES)
|
||||
|
||||
1. **答案聚类**:对组内每条 ChatLog 的 answer 做相似度聚类(SequenceMatcher ≥ FAQ_HOT_ANSWER_SIMILARITY,默认 0.95)。
|
||||
2. **一致性门槛**:最大簇占比 ≥ FAQ_HOT_ANSWER_DOMINANCE(默认 0.6);否则视为答案分歧过大,整组跳过。
|
||||
3. **在最大簇内选代表答案**,优先级:
|
||||
1. ~~**答案聚类**~~(已暂时关闭):原按 FAQ_HOT_ANSWER_SIMILARITY 对 answer 聚类。
|
||||
2. ~~**一致性门槛**~~(已暂时关闭):原要求最大簇占比 ≥ FAQ_HOT_ANSWER_DOMINANCE。
|
||||
3. **在组内选代表答案**,优先级:
|
||||
a. Feedback.score ≥ FAQ_HOT_GOOD_SCORE(默认 4)的记录中,取 created_at 最新;
|
||||
b. 否则排除 Feedback.score ≤ FAQ_HOT_BAD_SCORE(默认 2)的记录,取 created_at 最新;
|
||||
c. 否则取簇内 created_at 最新的 answer。
|
||||
c. 否则取组内 created_at 最新的 answer。
|
||||
4. **canonical_query**:取该组内出现次数最多的原始 query 文本(非归一化,便于人工阅读)。
|
||||
"""
|
||||
from __future__ import annotations
|
||||
|
||||
import re
|
||||
from collections import Counter, defaultdict
|
||||
from dataclasses import dataclass, field
|
||||
from datetime import datetime, timedelta
|
||||
from difflib import SequenceMatcher
|
||||
from typing import Any, Optional
|
||||
|
||||
from flask import current_app
|
||||
@@ -73,8 +71,7 @@ HOT_FAQ_AGGREGATOR_DOC_SELECTION = (
|
||||
)
|
||||
|
||||
HOT_FAQ_AGGREGATOR_DOC_ANSWER = (
|
||||
'答案按相似度聚类,最大簇占比需≥dominance 阈值;'
|
||||
'在最大簇内优先选好评最新,其次无差评最新,最后取最新记录。'
|
||||
'答案聚类与一致性门槛已暂时关闭;在组内按反馈优先级选代表答案。'
|
||||
)
|
||||
|
||||
|
||||
@@ -161,8 +158,10 @@ class HotFaqAggregator:
|
||||
'description': HOT_FAQ_AGGREGATOR_DOC_SELECTION,
|
||||
},
|
||||
'answer': {
|
||||
# 答案聚类 / 一致性门槛已暂时关闭,以下配置保留供日后恢复
|
||||
'answer_similarity': float(cls._cfg('FAQ_HOT_ANSWER_SIMILARITY', 0.95)),
|
||||
'answer_dominance': float(cls._cfg('FAQ_HOT_ANSWER_DOMINANCE', 0.6)),
|
||||
'answer_clustering_enabled': False,
|
||||
'good_feedback_score': int(cls._cfg('FAQ_HOT_GOOD_SCORE', 4)),
|
||||
'bad_feedback_score': int(cls._cfg('FAQ_HOT_BAD_SCORE', 2)),
|
||||
'description': HOT_FAQ_AGGREGATOR_DOC_ANSWER,
|
||||
@@ -332,17 +331,19 @@ class HotFaqAggregator:
|
||||
rows: list[_LogRow],
|
||||
) -> tuple[Optional[HotFaqCandidate], Optional[str]]:
|
||||
"""从一组 ChatLog 构建热点候选;失败时返回 skip reason。"""
|
||||
dominance_threshold = float(cls._cfg('FAQ_HOT_ANSWER_DOMINANCE', 0.6))
|
||||
clusters = cls._cluster_answers(rows)
|
||||
if not clusters:
|
||||
return None, 'empty_answer_clusters'
|
||||
# [DISABLED] 答案聚类 + 一致性门槛(answer_divergence)已暂时关闭,直接在整组内选代表答案。
|
||||
# dominance_threshold = float(cls._cfg('FAQ_HOT_ANSWER_DOMINANCE', 0.6))
|
||||
# similarity_threshold = float(cls._cfg('FAQ_HOT_ANSWER_SIMILARITY', 0.95))
|
||||
# largest_rows = cls._largest_similarity_cluster(rows, similarity_threshold)
|
||||
# dominance = len(largest_rows) / len(rows) if rows else 0.0
|
||||
# if dominance < dominance_threshold:
|
||||
# relaxed = float(cls._cfg('FAQ_HOT_ANSWER_SIMILARITY_RELAXED', 0.70))
|
||||
# ...
|
||||
# if dominance < dominance_threshold:
|
||||
# return None, f'answer_divergence:{dominance:.2f}'
|
||||
|
||||
largest = max(clusters, key=lambda c: len(c['rows']))
|
||||
dominance = len(largest['rows']) / len(rows)
|
||||
if dominance < dominance_threshold:
|
||||
return None, f'answer_divergence:{dominance:.2f}'
|
||||
|
||||
rep = cls._pick_representative_answer(largest['rows'])
|
||||
dominance = 1.0
|
||||
rep = cls._pick_representative_answer(rows)
|
||||
if not rep:
|
||||
return None, 'no_representative_answer'
|
||||
|
||||
@@ -371,44 +372,25 @@ class HotFaqAggregator:
|
||||
retriever_resources=rep.retriever_resources,
|
||||
), None
|
||||
|
||||
@classmethod
|
||||
def _cluster_answers(cls, rows: list[_LogRow]) -> list[dict[str, Any]]:
|
||||
"""
|
||||
将 answer 按相似度聚类(贪心:与簇代表相似度 ≥ 阈值则归入)。
|
||||
@returns [{'representative': str, 'rows': [_LogRow, ...]}, ...]
|
||||
"""
|
||||
threshold = float(cls._cfg('FAQ_HOT_ANSWER_SIMILARITY', 0.95))
|
||||
clusters: list[dict[str, Any]] = []
|
||||
|
||||
for row in rows:
|
||||
answer = row.answer.strip()
|
||||
if not answer:
|
||||
continue
|
||||
placed = False
|
||||
for cluster in clusters:
|
||||
rep = cluster['representative']
|
||||
if cls._answer_similarity(rep, answer) >= threshold:
|
||||
cluster['rows'].append(row)
|
||||
placed = True
|
||||
break
|
||||
if not placed:
|
||||
clusters.append({'representative': answer, 'rows': [row]})
|
||||
return clusters
|
||||
|
||||
@staticmethod
|
||||
def _answer_similarity(a: str, b: str) -> float:
|
||||
"""计算两段答案文本相似度(0~1)。"""
|
||||
left = re.sub(r'\s+', ' ', a.strip())
|
||||
right = re.sub(r'\s+', ' ', b.strip())
|
||||
if not left or not right:
|
||||
return 0.0
|
||||
return SequenceMatcher(None, left, right).ratio()
|
||||
# [DISABLED] 答案聚类相关方法(恢复 FAQ_HOT_ANSWER_SIMILARITY / DOMINANCE 检查时可启用)
|
||||
#
|
||||
# @classmethod
|
||||
# def _largest_similarity_cluster(cls, rows: list[_LogRow], threshold: float) -> list[_LogRow]:
|
||||
# ...
|
||||
#
|
||||
# @classmethod
|
||||
# def _cluster_answers(cls, rows: list[_LogRow], *, threshold: float | None = None) -> list[dict[str, Any]]:
|
||||
# ...
|
||||
#
|
||||
# @staticmethod
|
||||
# def _answer_similarity(a: str, b: str) -> float:
|
||||
# ...
|
||||
|
||||
@classmethod
|
||||
def _pick_representative_answer(cls, rows: list[_LogRow]) -> Optional[_LogRow]:
|
||||
"""
|
||||
在 dominant 簇内按反馈优先级选取最佳 ChatLog 行。
|
||||
优先级:好评最新 > 无差评最新 > 簇内最新。
|
||||
在组内按反馈优先级选取最佳 ChatLog 行。
|
||||
优先级:好评最新 > 无差评最新 > 组内最新。
|
||||
"""
|
||||
good_score = int(cls._cfg('FAQ_HOT_GOOD_SCORE', 4))
|
||||
bad_score = int(cls._cfg('FAQ_HOT_BAD_SCORE', 2))
|
||||
|
||||
Reference in new issue
Block a user