perf(cache): 修复缓存失效 Bug + 集成语义缓存 + 删除 AgenticRAG 死代码

- fix: Query Cache GET/SET Key 不匹配导致命中率始终为 0%
- fix: CACHE_MIN_SCORE 阈值 0.3 对 ChromaDB cosine distance 过于严格
- feat: 在 /rag 端点集成语义缓存(命中时跳过检索+生成,92x 加速)
- refactor: 删除 AgenticRAG 死代码路径(10 个 agentic_*.py,约 1950 行)
- cleanup: 移除 engine.py 死方法、路由死函数、初始化死代码
This commit is contained in:
lacerate551
2026-06-05 21:20:53 +08:00
parent 6deba8fae2
commit 505f79860e
16 changed files with 222 additions and 2052 deletions

View File

@@ -224,39 +224,36 @@ class RAGCacheManager:
"""
获取查询缓存结果
始终使用粗粒度 key基于 kb_version确保 GET/SET key 一致。
Args:
query: 查询文本
kb_name: 知识库名称
doc_ids: 相关文档 ID 列表(用于细粒度缓存 key
doc_ids: 保留参数以兼容调用方签名(当前未使用
"""
kb_version = self.get_kb_version(kb_name)
# 计算文档哈希(如果提供了 doc_ids
doc_hash = ""
if doc_ids:
doc_hash = self._compute_doc_hash(kb_name, doc_ids)
key = self._make_query_cache_key(query, kb_name, kb_version, doc_hash)
# 使用粗粒度 key与 SET 保持一致
key = self._make_query_cache_key(query, kb_name, kb_version)
return self.query_cache.get(key)
def set_query_result(self, query: str, kb_name: str, result: Dict, doc_ids: List[str] = None) -> None:
"""
设置查询缓存结果
始终使用粗粒度 key基于 kb_version确保 GET/SET key 一致。
kb_version 在文档变更时自增,触发整个知识库的缓存失效。
Args:
query: 查询文本
kb_name: 知识库名称
result: 缓存结果
doc_ids: 相关文档 ID 列表(用于细粒度失效
doc_ids: 保留参数以兼容调用方签名(当前未使用
"""
kb_version = self.get_kb_version(kb_name)
# 计算相关文档的版本哈希(细粒度失效)
doc_hash = ""
if doc_ids:
doc_hash = self._compute_doc_hash(kb_name, doc_ids)
key = self._make_query_cache_key(query, kb_name, kb_version, doc_hash)
# 使用与 GET 相同的粗粒度 key确保缓存可命中
key = self._make_query_cache_key(query, kb_name, kb_version)
self.query_cache.set(key, result, kb_version=kb_version)
def _compute_doc_hash(self, kb_name: str, doc_ids: List[str]) -> str: