Haystack 集成指南:用 KreuzbergConverter 在本地完成多格式文档转 Document 的索引管线 Haystack 集成指南用 KreuzbergConverter 在本地完成多格式文档转 Document 的索引管线【免费下载链接】haystackOpen-source AI orchestration framework for building context-engineered, production-ready LLM applications. Design modular pipelines and agent workflows with explicit control over retrieval, routing, memory, and generation. Built for scalable agents, RAG, multimodal applications, semantic search, and conversational systems.项目地址: https://gitcode.com/GitHub_Trending/ha/haystackKreuzbergConverter是 Haystack 生态中面向文档智能document intelligence的转换器组件它基于 KreuzbergRust 内核在本地从 PDF、Office 文档、图片以及 75 种文件格式中提取文本全程不发起任何外部 API 调用。本文将围绕 Kreuzberg API 参考文档 展开完整讲解组件的安装、run调用方式、ExtractionConfig定制、OCR 图像预处理、token 压缩以及如何在 Haystack 索引管线中与其他组件串联帮助你在 RAG、语义搜索和 LLM 应用中直接落地一套本地化、可并行的文档解析方案。KreuzbergConverter 是什么KreuzbergConverter位于haystack_integrations.components.converters.kreuzberg.converter模块作用是把文件路径、目录路径或ByteStream对象转换为 Haystack 的Document。Kreuzberg 本身是一个文档智能框架可提取 PDF、Office 文档、图片等 75 格式的文本所有处理均在本地完成、无外部 API 调用。在管线中的典型位置是索引管线的起点PreProcessor 之前输入为sources输出为documents。安装方式为pip install kreuzberg-haystack支持的格式类别文档类PDF、DOCX、DOC、PPTX、PPT、XLSX、XLS、ODT、ODS、ODP、RTF、Pages、Keynote、Numbers 等图片类经 OCRPNG、JPEG、TIFF、GIF、BMP、WebP、JPEG 2000、SVG文本/标记类Markdown、HTML、XML、LaTeX、Typst、JSON、YAML、reStructuredText、Jupyter Notebook邮件类EML、MSG支持附件提取压缩包类ZIP、TAR、GZIP、7Z递归提取并处理内部内容电子书与学术类EPUB、BibTeX、DocBook、JATS默认情况下组件每个 source 返回一个 Document启用按页提取或分块chunking后则每个页面或每个块返回一个 Document。返回的 Document 会携带丰富的元数据例如质量评分quality scores、检测到的语言、提取的关键词、表格数据以及 PDF 注解等。最小使用示例from haystack_integrations.components.converters.kreuzberg import ( KreuzbergConverter, ) converter KreuzbergConverter() result converter.run(sources[document.pdf, report.docx]) documents result[documents]构造参数详解initKreuzbergConverter的构造签名如下__init__( *, config: ExtractionConfig | None None, config_path: str | Path | None None, store_full_path: bool False, batch: bool True, easyocr_kwargs: dict[str, Any] | None None ) - Noneconfig可选的kreuzberg.ExtractionConfig对象用于定制提取行为例如输出格式、OCR 后端与语言、强制 OCR 模式、按页提取、分块、关键词提取等。若不提供则使用 Kreuzberg 的默认配置。from kreuzberg import ExtractionConfig, OcrConfig converter KreuzbergConverter( configExtractionConfig( output_formatmarkdown, ocrOcrConfig(backendtesseract, languageeng), ), )config_path指向 Kreuzberg 配置文件.toml、.yaml或.json的路径作用与config相同但二者不能同时使用converter KreuzbergConverter(config_pathextraction_config.toml)store_full_path若为True将文件的完整路径存入 Document 元数据若为False仅保存文件名。batch若为True默认值使用 Kreuzberg 的批量提取 API底层借助 Rust 的 rayon 线程池实现并行处理若为False则逐个 source 顺序提取。适合在批量索引大批量文件时开启以获得吞吐量提升在资源受限或需要严格串行时关闭。easyocr_kwargs使用easyocrOCR 后端时的可选关键字参数支持 GPU、beam width、模型存储位置等 EasyOCR 专属选项。run 方法输入输出与元数据规则run( sources: list[str | Path | ByteStream], meta: dict[str, Any] | list[dict[str, Any]] | None None, ) - dict[str, list[Document]]sources文件路径、目录路径或ByteStream对象的列表。目录路径会被展开为其直接子文件非递归、按字母序排序。meta可选附加到 Document 上的元数据。可以是单个字典内容添加到所有产出的 Document 元数据中也可以是字典列表长度必须与 sources 数量一致两者按位置 zip 对应。若sources中包含ByteStream对象其自身的meta也会合并进输出 Document。注意当sources中出现目录时meta必须传单个字典而非列表因为目录内文件数量在运行前无法预知。返回包含documents键的字典值为创建的Document列表。按页提取与分块通过ExtractionConfig的page配置可开启按页提取让每个 PDF 页面生成一个独立 Document并在元数据中记录page_numberfrom haystack_integrations.components.converters.kreuzberg import KreuzbergConverter from kreuzberg import ExtractionConfig, PageConfig converter KreuzbergConverter( configExtractionConfig( pagePageConfig(extract_pagesTrue), ), ) result converter.run(sources[multipage.pdf]) # 每个页面一个 Document元数据中含 page_numberToken 压缩为 LLM 消费瘦身当提取结果需要送入 LLM 时可通过ExtractionConfig(token_reductionTokenReductionConfig(modemoderate))缩减输出体积。Token 压缩基于 TF-IDF 抽取式摘要识别并保留最重要的词与短语逐步剔除多余空白、填充词和冗余表述压缩后的文本直接出现在Document.content中。共有五档级别mode说明大致压缩比例off不压缩0%light轻度压缩约 15%moderate中度压缩约 30%aggressive激进压缩约 50%maximum最大压缩超过 50%from haystack_integrations.components.converters.kreuzberg import KreuzbergConverter from kreuzberg import ExtractionConfig, TokenReductionConfig converter KreuzbergConverter( configExtractionConfig( token_reductionTokenReductionConfig(modemoderate), ), )OCR 图像预处理对扫描件或图片类文档可通过OcrConfig嵌套的TesseractConfig精细调优 OCR 前的图像预处理OcrConfig( tesseract_configTesseractConfig( preprocessingImagePreprocessingConfig( # 目标 DPI、自动旋转、去倾斜deskew、去噪、 # 对比度增强、二值化方法等 ), ), )可配置项包括目标 DPI、自动旋转auto-rotate、去倾斜deskew、去噪denoise、对比度增强以及二值化方法用于改善低质量扫描件的 OCR 准确率。在 Haystack 管线中使用KreuzbergConverter天然是 Haystack 组件可与其他组件自由串联。以下示例将其与DocumentSplitter、DocumentWriter及InMemoryDocumentStore组成一条完整的索引管线from haystack import Pipeline from haystack.components.preprocessors import DocumentSplitter from haystack.components.writers import DocumentWriter from haystack.document_stores.in_memory import InMemoryDocumentStore from haystack_integrations.components.converters.kreuzberg import KreuzbergConverter document_store InMemoryDocumentStore() pipeline Pipeline() pipeline.add_component(converter, KreuzbergConverter()) pipeline.add_component( splitter, DocumentSplitter(split_bysentence, split_length5), ) pipeline.add_component(writer, DocumentWriter(document_storedocument_store)) pipeline.connect(converter, splitter) pipeline.connect(splitter, writer) pipeline.run({converter: {sources: [report.pdf, presentation.pptx]}})该组件的角色与用法也可对照 Converters 总览 和 Preprocessors 文档 来理解转换器负责把异构文件统一成Document预处理阶段再对Document.content做切分与清洗。关于ByteStream与Document数据结构可参考 数据类文档。序列化支持to_dict 与 from_dict组件遵循 Haystack 的序列化约定可无缝用于管线 YAML 定义与配置持久化to_dict() - dict[str, Any]将组件序列化为字典便于存入配置文件。from_dict(data: dict[str, Any]) - KreuzbergConverter从字典反序列化并重建组件实例。这意味着你可以把包含KreuzbergConverter的整条索引管线导出为 YAML/JSON在部署环境中原样恢复无需重新编写构造逻辑。总结KreuzbergConverter提供了一条「零外部 API、本地并行、多格式覆盖」的文档解析路径默认的批量模式借助 Rust rayon 线程池提升吞吐ExtractionConfig支持输出格式、OCR 后端、按页提取、分块与关键词提取等丰富定制TokenReductionConfig可显著降低送入 LLM 的 token 开销to_dict/from_dict则保证了管线配置的可序列化与可移植。对于需要在 Haystack 中构建本地化、生产级 RAG 索引的应用它是值得优先考虑的通用文件转换入口。【免费下载链接】haystackOpen-source AI orchestration framework for building context-engineered, production-ready LLM applications. Design modular pipelines and agent workflows with explicit control over retrieval, routing, memory, and generation. Built for scalable agents, RAG, multimodal applications, semantic search, and conversational systems.项目地址: https://gitcode.com/GitHub_Trending/ha/haystack创作声明:本文部分内容由AI辅助生成(AIGC),仅供参考