文档API 参考📓 教程🧑‍🍳 食谱🤝 集成💜 Discord🎨 Studio
文档

HTMLToDocument

将 HTML 文件转换为文档的组件。

pipeline 中的最常见位置预处理器 之前,或在索引管道的开头。
强制运行变量"sources": HTML 文件路径或 ByteStream 对象的列表。
输出变量"documents": 文档列表
API 参考Converters (转换器)
GitHub 链接https://github.com/deepset-ai/haystack/blob/main/haystack/components/converters/html.py

概述

HTMLToDocument 组件将 HTML 文件转换为文档。它可以用于索引管道,将 HTML 文件内容索引到 Document Store 中,甚至可以在 LinkContentFetcher 之后用于查询管道。HTMLToDocument 组件接受 HTML 文件路径或 ByteStream 对象列表作为输入,并将文件转换为文档列表。您可以选择通过meta 输入参数向文档添加元数据。

初始化组件时,您可以选择设置extraction_kwargs,这是一个包含关键字参数的字典,用于自定义提取过程。这些参数将传递给底层的 Trafilaturaextract 函数。有关可用参数的完整列表,请参阅 Trafilatura 文档

用法

单独使用

from pathlib import Path
from haystack.components.converters import HTMLToDocument

converter = HTMLToDocument()

docs = converter.run(sources=[Path("saved_page.html")])

在 pipeline 中

这是一个将 HTML 文件内容写入 MemoryDocumentStore 的索引管道示例。InMemoryDocumentStore:

from haystack import Pipeline
from haystack.document_stores.in_memory import InMemoryDocumentStore
from haystack.components.converters import HTMLToDocument
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", HTMLToDocument())
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}})

相关链接

在我们的 API 参考中查看参数详情