PyPDFToDocument
将 PDF 文件转换为 Document 的组件。
| pipeline 中的最常见位置 | 在 预处理器 之前,或在索引管道的开头。 |
| 强制运行变量 | "sources": PDF 文件路径或 ByteStream 对象 |
| 输出变量 | "documents": 文档列表 |
| API 参考 | Converters (转换器) |
| GitHub 链接 | https://github.com/deepset-ai/haystack/blob/main/haystack/components/converters/pypdf.py |
概述
该PyPDFToDocument 组件将 PDF 文件转换为 Document。您可以在索引管道中使用它,将 PDF 文件内容索引到 Document Store 中。它接收文件路径列表或 ByteStream 对象作为输入,并以 Document 列表形式输出转换结果。可选地,您可以通过meta 输入参数向文档添加元数据。
用法
您需要首先安装pypdf 包来使用PyPDFToDocument 转换器
pip install pypdf
单独使用
from pathlib import Path
from haystack.components.converters import PyPDFToDocument
converter = PyPDFToDocument()
docs = converter.run(sources=[Path("my_file.pdf")])
在 pipeline 中
from haystack import Pipeline
from haystack.document_stores.in_memory import InMemoryDocumentStore
from haystack.components.converters import PyPDFToDocument
from haystack.components.preprocessors import DocumentCleaner
from haystack.components.preprocessors import DocumentSplitter
from haystack.components.writers import DocumentWriter
document_store = InMemoryDocumentStore()
pipeline = Pipeline()
pipeline.add_component("converter", PyPDFToDocument())
pipeline.add_component("cleaner", DocumentCleaner())
pipeline.add_component("splitter", DocumentSplitter(split_by="sentence", split_length=5))
pipeline.add_component("writer", DocumentWriter(document_store=document_store))
pipeline.connect("converter", "cleaner")
pipeline.connect("cleaner", "splitter")
pipeline.connect("splitter", "writer")
pipeline.run({"converter": {"sources": file_names}})
其他参考资料
🧑🍳 食谱:使用 Amazon Bedrock 和 Haystack 进行基于 PDF 的问答
📓 教程:预处理不同文件类型
更新于 大约 1 年前
