DocumentPreprocessor
将一系列文本文档分割成一系列更短的文本文档,然后通过清理使它们更具可读性。
| pipeline 中的最常见位置 | 在索引管道中,位于 Converters 之后。 |
| 强制运行变量 | "documents": 文档列表 |
| 输出变量 | "documents": 分割和清理后的文档列表 |
| API 参考 | PreProcessors (预处理器) |
| GitHub 链接 | https://github.com/deepset-ai/haystack/blob/main/haystack/components/preprocessors/document_preprocessor.py |
概述
DocumentPreprocessor 首先分割然后清理文档。
它是一个 SuperComponent,结合了DocumentSplitter 和DocumentCleaner 到一个组件中。
参数
该DocumentPreprocessor 公开了底层DocumentSplitter 和DocumentCleaner 的所有初始化参数,并且它们都是可选的。有关其参数的详细说明,请参阅 respective 文档页面。
用法
单独使用
from haystack import Document
from haystack.components.preprocessors import DocumentPreprocessor
doc = Document(content="I love pizza!")
preprocessor = DocumentPreprocessor()
result = preprocessor.run(documents=[doc])
print(result["documents"])
在 pipeline 中
您可以在索引管道中使用 DocumentPreprocessor。下面的示例需要安装额外的依赖项才能使用MultiFileConverter:
pip install pypdf markdown-it-py mdit_plain trafilatura python-pptx python-docx jq openpyxl tabulate pandas
from haystack import Pipeline
from haystack.components.converters import MultiFileConverter
from haystack.components.preprocessors import DocumentPreprocessor
from haystack.components.writers import DocumentWriter
from haystack.document_stores.in_memory import InMemoryDocumentStore
document_store = InMemoryDocumentStore()
pipeline = Pipeline()
pipeline.add_component("converter", MultiFileConverter())
pipeline.add_component("preprocessor", DocumentPreprocessor())
pipeline.add_component("writer", DocumentWriter(document_store = document_store))
pipeline.connect("converter", "preprocessor")
pipeline.connect("preprocessor", "writer")
result = pipeline.run(data={"sources": ["test.txt", "test.pdf"]})
print(result)
# {'writer': {'documents_written': 3}}
更新于 6 个月前
