feat:客户agent以及记忆模块优化
This commit is contained in:
+9
-15
@@ -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}; 采用证据增益和时间衰减"
|
||||
)
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user