feat: sync core logic from main (cache fix, intent analyzer, engine improvements)

- core/cache.py: fix GET/SET key mismatch, use coarse-grained kb_version keys
- core/intent_analyzer.py: add cache_type tagging, enhance history parsing
- core/engine.py: adaptive topk score source fix, BM25 top-3 preservation, section cluster boost
This commit is contained in:
lacerate551
2026-06-20 22:23:27 +08:00
parent 3a3627f73c
commit 8f72ac5da4
3 changed files with 346 additions and 300 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: