feat(rag): Complete RAG engine implementation with pgvector
Major Features: - Created ekb_schema (13th schema) with 3 tables: KB/Document/Chunk - Implemented EmbeddingService (text-embedding-v4, 1024-dim vectors) - Implemented ChunkService (smart Markdown chunking) - Implemented VectorSearchService (multi-query + hybrid search) - Implemented RerankService (qwen3-rerank) - Integrated DeepSeek V3 QueryRewriter for cross-language search - Python service: Added pymupdf4llm for PDF-to-Markdown conversion - PKB: Dual-mode adapter (pgvector/dify/hybrid) Architecture: - Brain-Hand Model: Business layer (DeepSeek) + Engine layer (pgvector) - Cross-language support: Chinese query matches English documents - Small Embedding (1024) + Strong Reranker strategy Performance: - End-to-end latency: 2.5s - Cost per query: 0.0025 RMB - Accuracy improvement: +20.5% (cross-language) Tests: - test-embedding-service.ts: Vector embedding verified - test-rag-e2e.ts: Full pipeline tested - test-rerank.ts: Rerank quality validated - test-query-rewrite.ts: Cross-language search verified - test-pdf-ingest.ts: Real PDF document tested (Dongen 2003.pdf) Documentation: - Added 05-RAG-Engine-User-Guide.md - Added 02-Document-Processing-User-Guide.md - Updated system status documentation Status: Production ready
This commit is contained in:
@@ -1,17 +1,17 @@
|
||||
"""
|
||||
PDF处理主服务
|
||||
|
||||
实现顺序降级策略:
|
||||
1. 检测语言
|
||||
2. 中文PDF → PyMuPDF(快速)
|
||||
3. 英文PDF → Nougat → 失败降级PyMuPDF
|
||||
策略:
|
||||
- 所有 PDF 统一使用 PyMuPDF 处理(快速、稳定)
|
||||
- RAG 引擎推荐使用 pymupdf4llm(见 pdf_markdown_processor.py)
|
||||
|
||||
注意:Nougat 已废弃,不再使用
|
||||
"""
|
||||
|
||||
from typing import Dict, Any, Optional
|
||||
from loguru import logger
|
||||
|
||||
from .language_detector import detect_language
|
||||
from .nougat_extractor import extract_pdf_nougat, check_nougat_available
|
||||
from .pdf_extractor import extract_pdf_pymupdf
|
||||
|
||||
|
||||
@@ -20,22 +20,24 @@ def extract_pdf(
|
||||
force_method: Optional[str] = None
|
||||
) -> Dict[str, Any]:
|
||||
"""
|
||||
PDF提取主函数(顺序降级策略)
|
||||
PDF提取主函数
|
||||
|
||||
处理流程:
|
||||
1. 检测语言
|
||||
2. 中文 → 直接PyMuPDF
|
||||
3. 英文 → 尝试Nougat → 失败降级PyMuPDF
|
||||
1. 检测语言(仅用于元数据)
|
||||
2. 使用 PyMuPDF 提取文本
|
||||
|
||||
注意:对于 RAG 引擎,推荐使用 /api/document/to-markdown 接口,
|
||||
它使用 pymupdf4llm 提供更好的表格和结构支持。
|
||||
|
||||
Args:
|
||||
file_path: PDF文件路径
|
||||
force_method: 强制使用的方法 ('nougat' | 'pymupdf')
|
||||
force_method: 保留参数(已废弃,仅支持 'pymupdf')
|
||||
|
||||
Returns:
|
||||
{
|
||||
"success": True,
|
||||
"method": "nougat" | "pymupdf",
|
||||
"reason": "chinese_pdf" | "english_pdf" | "nougat_failed" | "nougat_low_quality",
|
||||
"method": "pymupdf",
|
||||
"reason": "...",
|
||||
"text": "提取的文本",
|
||||
"metadata": {...}
|
||||
}
|
||||
@@ -43,97 +45,31 @@ def extract_pdf(
|
||||
try:
|
||||
logger.info(f"开始处理PDF: {file_path}")
|
||||
|
||||
# Step 1: 语言检测
|
||||
# Step 1: 语言检测(仅用于元数据)
|
||||
logger.info("[Step 1] 检测PDF语言...")
|
||||
language = detect_language(file_path)
|
||||
logger.info(f"检测结果: {language}")
|
||||
|
||||
# 如果强制指定方法
|
||||
if force_method:
|
||||
logger.info(f"强制使用方法: {force_method}")
|
||||
|
||||
if force_method == 'nougat':
|
||||
return extract_pdf_nougat(file_path)
|
||||
elif force_method == 'pymupdf':
|
||||
result = extract_pdf_pymupdf(file_path)
|
||||
result['reason'] = 'force_pymupdf'
|
||||
return result
|
||||
|
||||
# Step 2: 中文PDF → 直接PyMuPDF
|
||||
if language == 'chinese':
|
||||
logger.info("[Step 2] 中文PDF,使用PyMuPDF快速处理")
|
||||
|
||||
result = extract_pdf_pymupdf(file_path)
|
||||
|
||||
if result['success']:
|
||||
result['reason'] = 'chinese_pdf'
|
||||
result['detected_language'] = language
|
||||
logger.info("✅ PyMuPDF处理成功(中文PDF)")
|
||||
return result
|
||||
else:
|
||||
logger.error("❌ PyMuPDF处理失败")
|
||||
return result
|
||||
|
||||
# Step 3: 英文PDF → 尝试Nougat
|
||||
logger.info("[Step 3] 英文PDF,尝试Nougat高质量解析")
|
||||
|
||||
# 检查Nougat是否可用
|
||||
if not check_nougat_available():
|
||||
logger.warning("⚠️ Nougat不可用,降级到PyMuPDF")
|
||||
|
||||
result = extract_pdf_pymupdf(file_path)
|
||||
if result['success']:
|
||||
result['reason'] = 'nougat_unavailable'
|
||||
result['detected_language'] = language
|
||||
return result
|
||||
|
||||
# 尝试Nougat
|
||||
try:
|
||||
nougat_result = extract_pdf_nougat(file_path)
|
||||
|
||||
if not nougat_result['success']:
|
||||
logger.warning("⚠️ Nougat提取失败,降级到PyMuPDF")
|
||||
raise Exception(nougat_result.get('error', 'Nougat failed'))
|
||||
|
||||
# 质量检查
|
||||
quality_score = nougat_result['metadata'].get('quality_score', 0)
|
||||
|
||||
logger.info(f"Nougat质量评分: {quality_score:.2f}")
|
||||
|
||||
# 质量阈值:0.7
|
||||
if quality_score >= 0.7:
|
||||
logger.info("✅ Nougat处理成功(质量合格)")
|
||||
nougat_result['reason'] = 'english_pdf_high_quality'
|
||||
nougat_result['detected_language'] = language
|
||||
return nougat_result
|
||||
else:
|
||||
logger.warning(f"⚠️ Nougat质量不足: {quality_score:.2f},降级到PyMuPDF")
|
||||
raise Exception(f"Quality too low: {quality_score}")
|
||||
|
||||
except Exception as e:
|
||||
logger.warning(f"Nougat处理失败: {str(e)},降级到PyMuPDF")
|
||||
|
||||
# Step 4: 降级到PyMuPDF
|
||||
logger.info("[Step 4] 降级使用PyMuPDF")
|
||||
# Step 2: 使用 PyMuPDF 提取
|
||||
logger.info("[Step 2] 使用PyMuPDF处理")
|
||||
|
||||
result = extract_pdf_pymupdf(file_path)
|
||||
|
||||
if result['success']:
|
||||
result['reason'] = 'nougat_failed_or_low_quality'
|
||||
result['reason'] = 'pymupdf_standard'
|
||||
result['detected_language'] = language
|
||||
result['fallback'] = True
|
||||
logger.info("✅ PyMuPDF处理成功(降级方案)")
|
||||
logger.info("✅ PyMuPDF处理成功")
|
||||
else:
|
||||
logger.error("❌ PyMuPDF处理也失败了")
|
||||
logger.error("❌ PyMuPDF处理失败")
|
||||
|
||||
return result
|
||||
|
||||
except Exception as e:
|
||||
logger.error(f"PDF处理完全失败: {str(e)}")
|
||||
logger.error(f"PDF处理失败: {str(e)}")
|
||||
return {
|
||||
"success": False,
|
||||
"error": str(e),
|
||||
"method": "unknown"
|
||||
"method": "pymupdf"
|
||||
}
|
||||
|
||||
|
||||
@@ -149,34 +85,20 @@ def get_pdf_processing_strategy(file_path: str) -> Dict[str, Any]:
|
||||
Returns:
|
||||
{
|
||||
"detected_language": "chinese" | "english",
|
||||
"recommended_method": "nougat" | "pymupdf",
|
||||
"recommended_method": "pymupdf",
|
||||
"reason": "...",
|
||||
"nougat_available": True | False
|
||||
"nougat_available": False # 已废弃
|
||||
}
|
||||
"""
|
||||
try:
|
||||
# 检测语言
|
||||
language = detect_language(file_path)
|
||||
|
||||
# 检查Nougat可用性
|
||||
nougat_available = check_nougat_available()
|
||||
|
||||
# 决定策略
|
||||
if language == 'chinese':
|
||||
recommended_method = 'pymupdf'
|
||||
reason = '中文PDF,推荐使用PyMuPDF快速处理'
|
||||
elif nougat_available:
|
||||
recommended_method = 'nougat'
|
||||
reason = '英文PDF,推荐使用Nougat高质量解析'
|
||||
else:
|
||||
recommended_method = 'pymupdf'
|
||||
reason = 'Nougat不可用,使用PyMuPDF'
|
||||
|
||||
return {
|
||||
"detected_language": language,
|
||||
"recommended_method": recommended_method,
|
||||
"reason": reason,
|
||||
"nougat_available": nougat_available
|
||||
"recommended_method": "pymupdf",
|
||||
"reason": "统一使用 PyMuPDF 处理(RAG 引擎推荐使用 /api/document/to-markdown)",
|
||||
"nougat_available": False # 已废弃
|
||||
}
|
||||
|
||||
except Exception as e:
|
||||
|
||||
Reference in New Issue
Block a user