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

@@ -316,7 +316,8 @@ class IntentAnalyzer:
if cache_emb is not None:
cached = cache.get(cache_emb)
if cached:
# 确保缓存条目是意图分析结果(非 RAG 回答缓存)
if cached and cached.get("cache_type") != "rag_answer":
logger.info(f"意图分析缓存命中: {cached.get('reason', '')[:50]}")
return IntentAnalysis.from_dict(cached)
else:
@@ -386,9 +387,11 @@ class IntentAnalyzer:
intent=intent_type
)
# 存入语义缓存
# 存入语义缓存(标记类型,避免与 RAG 回答缓存混淆)
if cache and cache_emb is not None:
cache.set(cache_emb, analysis.to_dict())
cache_data = analysis.to_dict()
cache_data["cache_type"] = "intent_analysis"
cache.set(cache_emb, cache_data)
# 存入精确匹配缓存
if len(self._exact_cache) < self._exact_cache_max:
@@ -446,6 +449,7 @@ class IntentAnalyzer:
if not history:
return "(无历史对话)"
import re
parts = []
# 提取最近 3 轮对话
@@ -454,6 +458,7 @@ class IntentAnalyzer:
for msg in recent_history:
role = "用户" if msg.get("role") == "user" else "助手"
content = msg.get("content", "")
original_content = content # 保留原始内容用于结构化提取
# 截断过长的内容
if len(content) > 500:
@@ -461,9 +466,10 @@ class IntentAnalyzer:
parts.append(f"{role}{content}")
# 提取图片信息
# 提取结构化信息(从 assistant 消息中提取章节、表格、来源等)
metadata = msg.get("metadata", {})
if isinstance(metadata, dict):
# 已有的图片提取
images = metadata.get("images", [])
if images:
for img in images[:3]:
@@ -472,6 +478,43 @@ class IntentAnalyzer:
img_type = img.get("type", "图片")
parts.append(f" └─ {img_type}: {desc}")
# 来源文件提取
sources = metadata.get("sources", [])
if sources:
source_names = []
for s in sources[:3]:
if isinstance(s, dict):
name = s.get("source", "") or s.get("name", "")
if name:
source_names.append(name)
elif isinstance(s, str):
source_names.append(s)
if source_names:
parts.append(f" └─ 来源文件: {', '.join(source_names)}")
# collections检索知识库提取
colls = metadata.get("collections", [])
if colls:
parts.append(f" └─ 检索知识库: {', '.join(colls)}")
# 从 assistant 原始内容中提取章节路径和表格结构
if role == "助手" and original_content:
# 提取章节路径:━ xxx ━ 格式
sections = re.findall(r'\s*(.+?)\s*━', original_content)
if sections:
unique_sections = list(dict.fromkeys(sections)) # 去重保序
parts.append(f" └─ 涉及章节: {'; '.join(unique_sections[:3])}")
# 提取表格列名:| A | B | C | 格式的表头行
table_headers = re.findall(r'^\|\s*(.+?)\s*\|', original_content, re.MULTILINE)
if table_headers:
# 取第一个表格的列名
first_header = table_headers[0]
cols = [c.strip() for c in first_header.split('|') if c.strip()]
# 排除分隔符行(--- 格式)
if cols and not all(re.match(r'^[-:]+$', c) for c in cols):
parts.append(f" └─ 含表格,列名: {', '.join(cols[:6])}")
# 添加图片上下文
if context_images:
parts.append("\n【上下文中的图片】")