ImageFileToDocument
将图像文件引用转换为空的Document 对象,并附带相关元数据。
| pipeline 中的最常见位置 | 在处理图像的组件之前,例如SentenceTransformersImageDocumentEmbedder 或LLMDocumentContentExtractor |
| 强制运行变量 | "sources": 图像文件路径或字节流列表 |
| 输出变量 | "documents": 带有相关元数据的空 Document 对象列表 |
| API 参考 | 图像转换器 |
| GitHub 链接 | https://github.com/deepset-ai/haystack/blob/main/haystack/components/converters/image/file_to_document.py |
概述
ImageFileToDocument 将图像文件源转换为空的Document 对象,并附带相关元数据。
此组件在需要将图像文件路径包装到Document 对象中以供下游组件处理时很有用,例如SentenceTransformersImageDocumentEmbedder 或LLMDocumentContentExtractor.
它不从图像文件中提取任何内容,而是创建Document 对象,其内容为None,并附带文件路径和任何用户提供的值等元数据。
每个源都可以是
- 文件路径(字符串或
Path),或 - 一个
ByteStream对象。
可选地,您可以使用meta 参数。这可以是一个字典(应用于所有文档)或一个与长度匹配的列表sources.
用法
单独使用
此组件主要用于管道中。
from haystack.components.converters.image import ImageFileToDocument
converter = ImageFileToDocument()
sources = ["image.jpg", "another_image.png"]
result = converter.run(sources=sources)
documents = result["documents"]
print(documents)
# [Document(id=..., content=None, meta={'file_path': 'image.jpg'}),
# Document(id=..., content=None, meta={'file_path': 'another_image.png'})]
在 pipeline 中
在下面的管道中,使用ImageFileToDocument 组件创建图像文档,然后用图像嵌入对其进行丰富,并将其保存在 Document Store 中。
from haystack import Pipeline
from haystack.components.converters.image import ImageFileToDocument
from haystack.components.embedders.image import SentenceTransformersDocumentImageEmbedder
from haystack.components.writers.document_writer import DocumentWriter
from haystack.document_stores.in_memory import InMemoryDocumentStore
# Create our document store
doc_store = InMemoryDocumentStore()
# Define pipeline with components
indexing_pipe = Pipeline()
indexing_pipe.add_component("image_converter", ImageFileToDocument(store_full_path=True))
indexing_pipe.add_component("image_doc_embedder", SentenceTransformersDocumentImageEmbedder())
indexing_pipe.add_component("document_writer", DocumentWriter(doc_store))
indexing_pipe.connect("image_converter.documents", "image_doc_embedder.documents")
indexing_pipe.connect("image_doc_embedder.documents", "document_writer.documents")
indexing_result = indexing_pipe.run(
data={"image_converter": {"sources": [
"apple.jpg",
"kiwi.png"
]}},
)
indexed_documents = doc_store.filter_documents()
print(f"Indexed {len(indexed_documents)} documents")
# Indexed 2 documents
其他参考资料
🧑🍳 食谱:M 模态简介
更新于 3 个月前
