2024-09-25 13:46:23 +08:00
|
|
|
|
import os
|
2025-05-09 23:47:16 +08:00
|
|
|
|
import asyncio
|
2024-09-25 13:46:23 +08:00
|
|
|
|
from pathlib import Path
|
2025-05-23 15:30:14 +08:00
|
|
|
|
from langchain.schema.document import Document
|
|
|
|
|
|
from langchain.text_splitter import RecursiveCharacterTextSplitter
|
|
|
|
|
|
from langchain_community.document_loaders import (
|
|
|
|
|
|
TextLoader,
|
|
|
|
|
|
PyPDFLoader,
|
|
|
|
|
|
Docx2txtLoader,
|
|
|
|
|
|
UnstructuredMarkdownLoader,
|
|
|
|
|
|
UnstructuredHTMLLoader,
|
|
|
|
|
|
CSVLoader,
|
|
|
|
|
|
JSONLoader
|
|
|
|
|
|
)
|
2024-09-25 13:46:23 +08:00
|
|
|
|
|
2025-03-20 19:51:46 +08:00
|
|
|
|
from src.utils import hashstr, logger
|
|
|
|
|
|
|
2024-09-25 13:46:23 +08:00
|
|
|
|
|
2025-05-23 15:30:14 +08:00
|
|
|
|
def chunk_with_parser(file_path, params=None):
|
2025-03-12 21:15:04 +08:00
|
|
|
|
"""
|
2025-05-23 15:30:14 +08:00
|
|
|
|
使用文件解析器将文件切分成固定大小的块
|
2025-03-20 19:51:46 +08:00
|
|
|
|
|
2025-03-12 21:15:04 +08:00
|
|
|
|
Args:
|
2025-05-23 15:30:14 +08:00
|
|
|
|
file_path: 文件路径
|
2025-03-12 21:15:04 +08:00
|
|
|
|
params: 参数
|
|
|
|
|
|
"""
|
2024-09-25 13:46:23 +08:00
|
|
|
|
params = params or {}
|
|
|
|
|
|
chunk_size = int(params.get("chunk_size", 500))
|
2025-03-12 21:15:04 +08:00
|
|
|
|
chunk_overlap = int(params.get("chunk_overlap", 100))
|
2025-05-23 15:30:14 +08:00
|
|
|
|
|
|
|
|
|
|
file_type = Path(file_path).suffix.lower()
|
|
|
|
|
|
|
|
|
|
|
|
# 选择合适的加载器
|
|
|
|
|
|
if file_type in ['.txt']:
|
|
|
|
|
|
loader = TextLoader(file_path)
|
|
|
|
|
|
|
|
|
|
|
|
elif file_type in ['.md']:
|
|
|
|
|
|
loader = UnstructuredMarkdownLoader(file_path)
|
|
|
|
|
|
|
|
|
|
|
|
elif file_type in ['.docx', '.doc']:
|
|
|
|
|
|
loader = Docx2txtLoader(file_path)
|
|
|
|
|
|
|
|
|
|
|
|
elif file_type in ['.html', '.htm']:
|
|
|
|
|
|
loader = UnstructuredHTMLLoader(file_path)
|
|
|
|
|
|
|
|
|
|
|
|
elif file_type in ['.json']:
|
|
|
|
|
|
loader = JSONLoader(file_path, jq_schema=".")
|
|
|
|
|
|
|
|
|
|
|
|
elif file_type in ['.csv']:
|
|
|
|
|
|
loader = CSVLoader(file_path)
|
|
|
|
|
|
|
|
|
|
|
|
else:
|
|
|
|
|
|
raise ValueError(f"不支持的文件类型: {file_type}")
|
|
|
|
|
|
|
|
|
|
|
|
# 加载文档
|
|
|
|
|
|
docs = loader.load()
|
|
|
|
|
|
|
|
|
|
|
|
# 创建文本分割器
|
|
|
|
|
|
text_splitter = RecursiveCharacterTextSplitter(
|
2024-09-25 13:46:23 +08:00
|
|
|
|
chunk_size=chunk_size,
|
|
|
|
|
|
chunk_overlap=chunk_overlap,
|
2025-05-23 15:30:14 +08:00
|
|
|
|
separators=["\n\n", "\n", ".", " ", ""],
|
2024-09-25 13:46:23 +08:00
|
|
|
|
)
|
|
|
|
|
|
|
2025-05-23 15:30:14 +08:00
|
|
|
|
# 分割文档
|
|
|
|
|
|
nodes = text_splitter.split_documents(docs)
|
2024-09-26 22:45:02 +08:00
|
|
|
|
|
2025-05-23 15:30:14 +08:00
|
|
|
|
# 添加序号信息到metadata
|
|
|
|
|
|
for i, node in enumerate(nodes):
|
|
|
|
|
|
if node.metadata is None:
|
|
|
|
|
|
node.metadata = {}
|
|
|
|
|
|
node.metadata["chunk_idx"] = i
|
2024-09-25 13:46:23 +08:00
|
|
|
|
|
2024-09-26 22:45:02 +08:00
|
|
|
|
return nodes
|
2025-03-20 19:51:46 +08:00
|
|
|
|
|
2025-05-23 15:30:14 +08:00
|
|
|
|
def chunk_text(text, params=None):
|
|
|
|
|
|
"""
|
|
|
|
|
|
将文本切分成固定大小的块
|
|
|
|
|
|
"""
|
|
|
|
|
|
params = params or {}
|
|
|
|
|
|
chunk_size = int(params.get("chunk_size", 500))
|
|
|
|
|
|
chunk_overlap = int(params.get("chunk_overlap", 100))
|
|
|
|
|
|
|
|
|
|
|
|
# 创建文本分割器
|
|
|
|
|
|
text_splitter = RecursiveCharacterTextSplitter(
|
|
|
|
|
|
chunk_size=chunk_size,
|
|
|
|
|
|
chunk_overlap=chunk_overlap,
|
|
|
|
|
|
separators=["\n\n", "\n", ".", " ", ""]
|
|
|
|
|
|
)
|
2025-03-20 19:51:46 +08:00
|
|
|
|
|
2025-05-23 15:30:14 +08:00
|
|
|
|
# 分割文档
|
|
|
|
|
|
nodes = text_splitter.split_text(text)
|
|
|
|
|
|
|
|
|
|
|
|
# 添加序号信息到metadata
|
|
|
|
|
|
nodes = [{"text": node, "metadata": {"chunk_idx": i}} for i, node in enumerate(nodes)]
|
|
|
|
|
|
return nodes
|
2025-03-20 19:51:46 +08:00
|
|
|
|
|
2025-05-23 15:30:14 +08:00
|
|
|
|
def chunk(text_or_path, params=None):
|
|
|
|
|
|
raise NotImplementedError("chunk is deprecated, use chunk_with_parser or chunk_text instead")
|
|
|
|
|
|
|
|
|
|
|
|
def pdfreader(file_path, params=None):
|
2025-03-20 19:51:46 +08:00
|
|
|
|
"""读取PDF文件并返回text文本"""
|
2025-05-23 15:30:14 +08:00
|
|
|
|
assert file_path.exists(), "File not found"
|
|
|
|
|
|
assert file_path.suffix.lower() == ".pdf", "File format not supported"
|
2025-03-20 19:51:46 +08:00
|
|
|
|
|
2025-05-23 15:30:14 +08:00
|
|
|
|
# 使用LangChain的PDF加载器
|
|
|
|
|
|
loader = PyPDFLoader(str(file_path))
|
|
|
|
|
|
docs = loader.load()
|
2025-03-20 19:51:46 +08:00
|
|
|
|
|
|
|
|
|
|
# 简单的拼接起来之后返回纯文本
|
2025-05-23 15:30:14 +08:00
|
|
|
|
text = "\n\n".join([d.page_content for d in docs])
|
2025-03-20 19:51:46 +08:00
|
|
|
|
return text
|
|
|
|
|
|
|
|
|
|
|
|
def plainreader(file_path):
|
|
|
|
|
|
"""读取普通文本文件并返回text文本"""
|
|
|
|
|
|
assert os.path.exists(file_path), "File not found"
|
|
|
|
|
|
|
2025-05-23 15:30:14 +08:00
|
|
|
|
# 使用LangChain的文本加载器
|
|
|
|
|
|
loader = TextLoader(str(file_path))
|
|
|
|
|
|
docs = loader.load()
|
|
|
|
|
|
text = "\n\n".join([d.page_content for d in docs])
|
2025-03-20 19:51:46 +08:00
|
|
|
|
return text
|
|
|
|
|
|
|
2025-05-23 15:30:14 +08:00
|
|
|
|
def parse_pdf(file, params=None):
|
2025-07-21 19:25:07 +08:00
|
|
|
|
"""
|
|
|
|
|
|
解析PDF文件,支持多种OCR方式
|
|
|
|
|
|
|
|
|
|
|
|
Args:
|
|
|
|
|
|
file: PDF文件路径
|
|
|
|
|
|
params: 参数字典,包含enable_ocr设置
|
2025-03-20 19:51:46 +08:00
|
|
|
|
|
2025-07-21 19:25:07 +08:00
|
|
|
|
Returns:
|
|
|
|
|
|
str: 解析得到的文本
|
2025-03-20 19:51:46 +08:00
|
|
|
|
|
2025-07-21 19:25:07 +08:00
|
|
|
|
Raises:
|
|
|
|
|
|
OCRServiceException: OCR服务不可用时抛出
|
|
|
|
|
|
"""
|
|
|
|
|
|
from src.plugins._ocr import OCRServiceException
|
2025-03-20 19:51:46 +08:00
|
|
|
|
|
2025-07-21 19:25:07 +08:00
|
|
|
|
params = params or {}
|
|
|
|
|
|
opt_ocr = params.get("enable_ocr", "disable")
|
2025-06-23 10:09:51 +08:00
|
|
|
|
|
2025-07-21 19:25:07 +08:00
|
|
|
|
if opt_ocr == "disable":
|
2025-05-23 15:30:14 +08:00
|
|
|
|
return pdfreader(file, params=params)
|
2025-03-20 19:51:46 +08:00
|
|
|
|
|
2025-07-21 19:25:07 +08:00
|
|
|
|
try:
|
|
|
|
|
|
if opt_ocr == "onnx_rapid_ocr":
|
|
|
|
|
|
from src.plugins import ocr
|
|
|
|
|
|
return ocr.process_pdf(file)
|
|
|
|
|
|
|
|
|
|
|
|
elif opt_ocr == "mineru_ocr":
|
|
|
|
|
|
from src.plugins import ocr
|
|
|
|
|
|
return ocr.process_pdf_mineru(file)
|
|
|
|
|
|
|
|
|
|
|
|
elif opt_ocr == "paddlex_ocr":
|
|
|
|
|
|
from src.plugins import ocr
|
|
|
|
|
|
return ocr.process_pdf_paddlex(file)
|
|
|
|
|
|
|
|
|
|
|
|
else:
|
|
|
|
|
|
return pdfreader(file, params=params)
|
|
|
|
|
|
|
|
|
|
|
|
except OCRServiceException as e:
|
|
|
|
|
|
logger.error(f"OCR service failed: {e.service_name} - {str(e)}")
|
|
|
|
|
|
raise
|
|
|
|
|
|
except Exception as e:
|
|
|
|
|
|
logger.error(f"PDF parsing failed: {str(e)}")
|
|
|
|
|
|
raise OCRServiceException(
|
|
|
|
|
|
f"PDF解析失败: {str(e)}",
|
|
|
|
|
|
opt_ocr,
|
|
|
|
|
|
"parsing_failed"
|
|
|
|
|
|
)
|
|
|
|
|
|
|
2025-05-23 15:30:14 +08:00
|
|
|
|
async def parse_pdf_async(file, params=None):
|
|
|
|
|
|
return await asyncio.to_thread(parse_pdf, file, params=params)
|