feat:客户agent以及记忆模块优化

This commit is contained in:
2026-09-12 19:56:48 +08:00
parent 30160128a4
commit 3103389aec
10 changed files with 86 additions and 54 deletions
+9 -15
View File
@@ -6,7 +6,7 @@ from typing import Any
class BaseConfidenceCalcTool:
"""根据来源、证据、冲突和时间计算单条记忆的长期置信度。"""
"""根据来源、证据和时间计算单条客服记忆的长期置信度。"""
SOURCE_INITIAL = {
"dialogue_confirmed": 0.75,
@@ -20,23 +20,21 @@ class BaseConfidenceCalcTool:
"SERVICE_FACT": 0.75,
}
DEFAULT_THRESHOLD = 0.80
VERSION = "confidence-v1"
VERSION = "confidence-v2"
def calc(
self,
tag: str,
source: str,
evidence_count: int,
conflict_count: int,
age_days: int,
) -> float:
"""计算基础置信度分数,返回范围为 0 到 1 的浮点数。"""
self._validate(tag, source, evidence_count, conflict_count, age_days)
self._validate(tag, source, evidence_count, age_days)
base = self.SOURCE_INITIAL[source]
gain = min(evidence_count * 0.05, 0.30)
penalty = min(conflict_count * 0.10, 0.50)
decay = max(0.80, 1 - age_days / 365 * 0.20)
return max(0.0, min(1.0, (base + gain - penalty) * decay))
return max(0.0, min(1.0, (base + gain) * decay))
def evaluate(
self,
@@ -44,13 +42,12 @@ class BaseConfidenceCalcTool:
tag: str,
source: str,
evidence_count: int,
conflict_count: int,
age_days: int,
memory_type: str | None = None,
threshold: float | None = None,
) -> dict[str, Any]:
"""返回可供记忆模块保存的完整置信度评估结果。"""
score = self.calc(tag, source, evidence_count, conflict_count, age_days)
score = self.calc(tag, source, evidence_count, age_days)
if threshold is None:
threshold = self.MEMORY_THRESHOLDS.get(
memory_type or "", self.DEFAULT_THRESHOLD
@@ -64,11 +61,10 @@ class BaseConfidenceCalcTool:
"confidence": score,
"status": status,
"evidence_count": evidence_count,
"conflict_count": conflict_count,
"age_days": age_days,
"threshold": threshold,
"confidence_reason": self._reason(
source, evidence_count, conflict_count, age_days
source, evidence_count, age_days
),
"confidence_version": self.VERSION,
}
@@ -87,7 +83,6 @@ class BaseConfidenceCalcTool:
tag: str,
source: str,
evidence_count: int,
conflict_count: int,
age_days: int,
) -> None:
"""校验工具输入,避免非法计数污染记忆分数。"""
@@ -97,18 +92,17 @@ class BaseConfidenceCalcTool:
raise ValueError(f"不支持的客服对话来源: {source}")
for name, value in (
("evidence_count", evidence_count),
("conflict_count", conflict_count),
("age_days", age_days),
):
if not isinstance(value, int) or isinstance(value, bool) or value < 0:
raise ValueError(f"{name} 必须是非负整数")
@staticmethod
def _reason(source: str, evidence_count: int, conflict_count: int, age_days: int) -> str:
def _reason(source: str, evidence_count: int, age_days: int) -> str:
"""生成便于审计和排查的评分原因。"""
return (
f"来源={source}; 支持证据={evidence_count}; 冲突证据={conflict_count}; "
f"存在天数={age_days}; 采用证据增益、冲突惩罚和时间衰减"
f"来源={source}; 支持证据={evidence_count}; "
f"存在天数={age_days}; 采用证据增益和时间衰减"
)
+1 -12
View File
@@ -14,8 +14,7 @@ class FinalConfidenceRankTool:
"semantic": 0.30,
"timeliness": 0.20,
"accuracy": 0.20,
"base": 0.25,
"conflict": 0.05,
"base": 0.30,
}
INVALID_STATUSES = frozenset({"expired", "rejected", "archived"})
@@ -39,13 +38,11 @@ class FinalConfidenceRankTool:
timeliness = self._calc_timeliness(unit.get("age_days", 0))
accuracy = self._bounded(unit.get("historical_accuracy", 0.5), 0.5)
base = self._bounded(unit.get("confidence", 0.5), 0.5)
conflict_penalty = min(self._non_negative_int(unit.get("conflict_count", 0)), 5) * 0.1
final_score = (
self.WEIGHTS["semantic"] * semantic
+ self.WEIGHTS["timeliness"] * timeliness
+ self.WEIGHTS["accuracy"] * accuracy
+ self.WEIGHTS["base"] * base
- self.WEIGHTS["conflict"] * conflict_penalty
)
unit["final_score"] = max(0.0, min(1.0, final_score))
ranked.append(unit)
@@ -78,14 +75,6 @@ class FinalConfidenceRankTool:
return default
return max(0.0, min(1.0, value))
@staticmethod
def _non_negative_int(value: Any) -> int:
"""将冲突次数转换为非负整数。"""
try:
return max(0, int(value))
except (TypeError, ValueError):
return 0
@staticmethod
def _calc_timeliness(age_days: Any) -> float:
"""按每年 20% 计算平滑时效分,最低保留 0.8。"""