DS4SD/docling 项目使用指南:文档转换与分块技术详解
docling Get your documents ready for gen AI 项目地址: https://gitcode.com/gh_mirrors/do/docling
项目概述
DS4SD/docling 是一个功能强大的文档处理工具集,专注于文档格式转换和内容分块两大核心功能。该项目采用模块化设计,支持多种文档格式的相互转换,并提供了智能分块能力,特别适合处理技术文档、学术论文等结构化内容。
文档转换功能
基础转换操作
文档转换是 docling 的核心功能之一,支持从 PDF 等多种格式转换为 Markdown 等目标格式。以下是基础使用方法:
from docling.document_converter import DocumentConverter
# 指定PDF文档路径或URL
source = "https://arxiv.org/pdf/2408.09869"
converter = DocumentConverter()
result = converter.convert(source)
# 输出Markdown格式内容
print(result.document.export_to_markdown())
命令行界面(CLI)使用
对于习惯使用命令行的用户,docling 提供了便捷的 CLI 工具:
docling https://arxiv.org/pdf/2206.01062
还可以指定使用视觉语言模型(VLM)进行处理:
docling --pipeline vlm --vlm-model smoldocling https://arxiv.org/pdf/2206.01062
高级配置选项
模型预加载与离线使用
在受限网络环境下,可以预先下载所需模型:
docling-tools models download
下载完成后,可通过以下方式指定模型路径:
from docling.datamodel.base_models import InputFormat
from docling.datamodel.pipeline_options import EasyOcrOptions, PdfPipelineOptions
from docling.document_converter import DocumentConverter, PdfFormatOption
artifacts_path = "/local/path/to/models"
pipeline_options = PdfPipelineOptions(artifacts_path=artifacts_path)
doc_converter = DocumentConverter(
format_options={
InputFormat.PDF: PdfFormatOption(pipeline_options=pipeline_options)
}
)
远程服务集成
虽然 docling 主要设计为本地运行,但也支持集成远程服务:
from docling.datamodel.base_models import InputFormat
from docling.datamodel.pipeline_options import PdfPipelineOptions
from docling.document_converter import DocumentConverter, PdfFormatOption
pipeline_options = PdfPipelineOptions(enable_remote_services=True)
doc_converter = DocumentConverter(
format_options={
InputFormat.PDF: PdfFormatOption(pipeline_options=pipeline_options)
}
)
表格提取优化
针对复杂表格结构,可以调整提取策略:
from docling.datamodel.base_models import InputFormat
from docling.document_converter import DocumentConverter, PdfFormatOption
from docling.datamodel.pipeline_options import PdfPipelineOptions, TableFormerMode
pipeline_options = PdfPipelineOptions(do_table_structure=True)
# 使用更精确的表格识别模式
pipeline_options.table_structure_options.mode = TableFormerMode.ACCURATE
doc_converter = DocumentConverter(
format_options={
InputFormat.PDF: PdfFormatOption(pipeline_options=pipeline_options)
}
)
资源限制
可以设置文档处理的大小限制:
converter = DocumentConverter()
# 限制最大页数为100,文件大小为20MB
result = converter.convert(source, max_num_pages=100, max_file_size=20971520)
文档分块功能
智能分块技术
docling 提供了先进的文档分块能力,可以按照语义和结构将长文档分割为有意义的片段:
from docling.document_converter import DocumentConverter
from docling.chunking import HybridChunker
# 首先转换文档
conv_res = DocumentConverter().convert("https://arxiv.org/pdf/2206.01062")
doc = conv_res.document
# 创建分块器实例
chunker = HybridChunker(tokenizer="BAAI/bge-small-en-v1.5")
# 生成分块迭代器
chunk_iter = chunker.chunk(doc)
分块结果包含文本内容及丰富的元数据:
{
"text": "In this paper, we present the DocLayNet dataset. [...]",
"meta": {
"doc_items": [{
"self_ref": "#/texts/28",
"label": "text",
"prov": [{
"page_no": 2,
"bbox": {"l": 53.29, "t": 287.14, "r": 295.56, "b": 212.37, ...},
}], ...,
}, ...],
"headings": ["1 INTRODUCTION"],
}
}
最佳实践建议
- 性能优化:对于批量处理大量文档,建议预先下载所有模型并设置合适的资源限制
- 表格处理:遇到复杂表格时,优先尝试使用
TableFormerMode.ACCURATE
模式 - 分块策略:根据下游应用需求选择合适的 tokenizer 和分块大小
- 错误处理:对于大文档处理,添加适当的异常捕获和重试机制
通过灵活运用 docling 的各项功能,用户可以高效地完成各类文档处理任务,为后续的文本分析、信息检索等应用奠定良好基础。
docling Get your documents ready for gen AI 项目地址: https://gitcode.com/gh_mirrors/do/docling
创作声明:本文部分内容由AI辅助生成(AIGC),仅供参考