fix: skip semantic cache for short follow-up queries to avoid false hits

Short follow-ups like "B3类呢?" are too context-dependent for embedding
similarity matching. Only exact-match cache is used when history exists
and query is under 20 chars.
This commit is contained in:
lacerate551
2026-06-20 22:35:29 +08:00
parent 8f72ac5da4
commit 5bc86a7c1f

View File

@@ -309,7 +309,9 @@ class IntentAnalyzer:
# 2. 尝试从语义缓存获取
cache = self._get_cache()
if cache:
# 短追问(如"B3类呢"严重依赖上下文embedding 区分度不足易误命中
is_short_followup = history and len(query.strip()) < 20
if cache and not is_short_followup:
# 使用 query + 历史关键信息作为缓存键
cache_key = self._build_cache_key(query, history)
cache_emb = self._get_embedding(cache_key)
@@ -325,7 +327,10 @@ class IntentAnalyzer:
else:
logger.warning("意图分析缓存: _get_embedding 返回 None跳过缓存")
else:
logger.warning("意图分析缓存: _get_cache 返回 None缓存不可用")
if is_short_followup:
logger.debug(f"跳过语义缓存(短追问依赖上下文): query='{query[:30]}'")
elif not cache:
logger.warning("意图分析缓存: _get_cache 返回 None缓存不可用")
# 构建 prompt
user_prompt = f"""## 对话历史