chore: import upstream snapshot with attribution
CodeQL / Analyze (python) (push) Has been cancelled
Update Platform Components Table / update (push) Has been cancelled
Docker image release / Build base image (push) Has been cancelled
Sync docs with Docusaurus / sync (push) Has been cancelled
Tests / Check if changed (push) Has been cancelled
Tests / format (push) Has been cancelled
Tests / check-imports (push) Has been cancelled
Tests / Unit / macos-latest (push) Has been cancelled
Tests / Unit / ubuntu-latest (push) Has been cancelled
Tests / Unit / windows-latest (push) Has been cancelled
Tests / mypy (push) Has been cancelled
Tests / Integration / ubuntu-latest (push) Has been cancelled
Tests / Integration / macos-latest (push) Has been cancelled
Tests / Integration / windows-latest (push) Has been cancelled
Tests / notify-slack-on-failure (push) Has been cancelled
Tests / Mark tests as completed (push) Has been cancelled
CodeQL / Analyze (python) (push) Has been cancelled
Update Platform Components Table / update (push) Has been cancelled
Docker image release / Build base image (push) Has been cancelled
Sync docs with Docusaurus / sync (push) Has been cancelled
Tests / Check if changed (push) Has been cancelled
Tests / format (push) Has been cancelled
Tests / check-imports (push) Has been cancelled
Tests / Unit / macos-latest (push) Has been cancelled
Tests / Unit / ubuntu-latest (push) Has been cancelled
Tests / Unit / windows-latest (push) Has been cancelled
Tests / mypy (push) Has been cancelled
Tests / Integration / ubuntu-latest (push) Has been cancelled
Tests / Integration / macos-latest (push) Has been cancelled
Tests / Integration / windows-latest (push) Has been cancelled
Tests / notify-slack-on-failure (push) Has been cancelled
Tests / Mark tests as completed (push) Has been cancelled
This commit is contained in:
+92
@@ -0,0 +1,92 @@
|
||||
---
|
||||
title: "AzureOCRDocumentConverter"
|
||||
id: azureocrdocumentconverter
|
||||
slug: "/azureocrdocumentconverter"
|
||||
description: "`AzureOCRDocumentConverter` converts files to documents using Azure's Document Intelligence service. It supports the following file formats: PDF (both searchable and image-only), JPEG, PNG, BMP, TIFF, DOCX, XLSX, PPTX, and HTML."
|
||||
---
|
||||
|
||||
# AzureOCRDocumentConverter
|
||||
|
||||
`AzureOCRDocumentConverter` converts files to documents using Azure's Document Intelligence service. It supports the following file formats: PDF (both searchable and image-only), JPEG, PNG, BMP, TIFF, DOCX, XLSX, PPTX, and HTML.
|
||||
|
||||
<div className="key-value-table">
|
||||
|
||||
| | |
|
||||
| --- | --- |
|
||||
| **Most common position in a pipeline** | Before [PreProcessors](../preprocessors.mdx) , or right at the beginning of an indexing pipeline |
|
||||
| **Mandatory init variables** | `endpoint`: The endpoint of your Azure resource <br /> <br />`api_key`: The API key of your Azure resource. Can be set with `AZURE_AI_API_KEY` environment variable. |
|
||||
| **Mandatory run variables** | `sources`: A list of file paths |
|
||||
| **Output variables** | `documents`: A list of documents <br /> <br />`raw_azure_response`: A list of raw responses from Azure |
|
||||
| **API reference** | [Converters](/reference/converters-api) |
|
||||
| **GitHub link** | https://github.com/deepset-ai/haystack/blob/main/haystack/components/converters/azure.py |
|
||||
|
||||
</div>
|
||||
|
||||
## Overview
|
||||
|
||||
`AzureOCRDocumentConverter` takes a list of file paths or [`ByteStream`](../../concepts/data-classes.mdx#bytestream) objects as input and uses Azure services to convert the files to a list of documents. Optionally, metadata can be attached to the documents through the `meta` input parameter. You need an active Azure account and a Document Intelligence or Cognitive Services resource to use this integration. Follow the steps described in the Azure [documentation](https://learn.microsoft.com/en-us/azure/ai-services/document-intelligence/quickstarts/get-started-sdks-rest-api) to set up your resource.
|
||||
|
||||
The component uses an `AZURE_AI_API_KEY` environment variable by default. Otherwise, you can pass an `api_key` at initialization – see code examples below.
|
||||
|
||||
When you initialize the component, you can optionally set the `model_id`, which refers to the model you want to use. Please refer to [Azure documentation](https://learn.microsoft.com/en-us/azure/ai-services/document-intelligence/choose-model-feature) for a list of available models. The default model is `"prebuilt-read"`.
|
||||
|
||||
The `AzureOCRDocumentConverter` doesn’t extract the tables from a file as plain text but generates separate `Document` objects of type `table` that maintain the two-dimensional structure of the tables.
|
||||
|
||||
## Usage
|
||||
|
||||
You need to install `azure-ai-formrecognizer` package to use the `AzureOCRDocumentConverter`:
|
||||
|
||||
```shell
|
||||
pip install "azure-ai-formrecognizer>=3.2.0b2"
|
||||
```
|
||||
|
||||
### On its own
|
||||
|
||||
```python
|
||||
from pathlib import Path
|
||||
|
||||
from haystack.components.converters import AzureOCRDocumentConverter
|
||||
from haystack.utils import Secret
|
||||
|
||||
converter = AzureOCRDocumentConverter(
|
||||
endpoint="azure_resource_url",
|
||||
api_key=Secret.from_token("<your-api-key>"),
|
||||
)
|
||||
|
||||
converter.run(sources=[Path("my_file.pdf")])
|
||||
```
|
||||
|
||||
### In a pipeline
|
||||
|
||||
```python
|
||||
from haystack import Pipeline
|
||||
from haystack.document_stores.in_memory import InMemoryDocumentStore
|
||||
from haystack.components.converters import AzureOCRDocumentConverter
|
||||
from haystack.components.preprocessors import DocumentCleaner
|
||||
from haystack.components.preprocessors import DocumentSplitter
|
||||
from haystack.components.writers import DocumentWriter
|
||||
from haystack.utils import Secret
|
||||
|
||||
document_store = InMemoryDocumentStore()
|
||||
|
||||
pipeline = Pipeline()
|
||||
pipeline.add_component(
|
||||
"converter",
|
||||
AzureOCRDocumentConverter(
|
||||
endpoint="azure_resource_url",
|
||||
api_key=Secret.from_token("<your-api-key>"),
|
||||
),
|
||||
)
|
||||
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")
|
||||
|
||||
file_names = ["my_file.pdf"]
|
||||
pipeline.run({"converter": {"sources": file_names}})
|
||||
```
|
||||
+74
@@ -0,0 +1,74 @@
|
||||
---
|
||||
title: "CSVToDocument"
|
||||
id: csvtodocument
|
||||
slug: "/csvtodocument"
|
||||
description: "Converts CSV files to documents."
|
||||
---
|
||||
|
||||
# CSVToDocument
|
||||
|
||||
Converts CSV files to documents.
|
||||
|
||||
<div className="key-value-table">
|
||||
|
||||
| | |
|
||||
| :------------------------------------- | :---------------------------------------------------------------------------------------------- |
|
||||
| **Most common position in a pipeline** | Before [PreProcessors](../preprocessors.mdx) , or right at the beginning of an indexing pipeline |
|
||||
| **Mandatory run variables** | `sources`: A list of file paths or [ByteStream](../../concepts/data-classes.mdx#bytestream) objects |
|
||||
| **Output variables** | `documents`: A list of documents |
|
||||
| **API reference** | [Converters](/reference/converters-api) |
|
||||
| **GitHub link** | https://github.com/deepset-ai/haystack/blob/main/haystack/components/converters/csv.py |
|
||||
|
||||
</div>
|
||||
|
||||
## Overview
|
||||
|
||||
`CSVToDocument` converts one or more CSV files into a text document.
|
||||
|
||||
The component uses UTF-8 encoding by default, but you may specify a different encoding if needed during initialization.
|
||||
You can optionally attach metadata to each document with a `meta` parameter when running the component.
|
||||
|
||||
## Usage
|
||||
|
||||
### On its own
|
||||
|
||||
```python
|
||||
from haystack.components.converters.csv import CSVToDocument
|
||||
|
||||
converter = CSVToDocument()
|
||||
results = converter.run(
|
||||
sources=["sample.csv"],
|
||||
meta={"date_added": datetime.now().isoformat()},
|
||||
)
|
||||
documents = results["documents"]
|
||||
|
||||
print(documents[0].content)
|
||||
## 'col1,col2\now1,row1\nrow2row2\n'
|
||||
```
|
||||
|
||||
### In a pipeline
|
||||
|
||||
```python
|
||||
from haystack import Pipeline
|
||||
from haystack.document_stores.in_memory import InMemoryDocumentStore
|
||||
from haystack.components.converters import CSVToDocument
|
||||
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", CSVToDocument())
|
||||
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}})
|
||||
```
|
||||
+153
@@ -0,0 +1,153 @@
|
||||
---
|
||||
title: "DocumentToImageContent"
|
||||
id: documenttoimagecontent
|
||||
slug: "/documenttoimagecontent"
|
||||
description: "`DocumentToImageContent` extracts visual data from image or PDF file-based documents and converts them into `ImageContent` objects. These are ready for multimodal AI pipelines, including tasks like image question-answering and captioning."
|
||||
---
|
||||
|
||||
# DocumentToImageContent
|
||||
|
||||
`DocumentToImageContent` extracts visual data from image or PDF file-based documents and converts them into `ImageContent` objects. These are ready for multimodal AI pipelines, including tasks like image question-answering and captioning.
|
||||
|
||||
<div className="key-value-table">
|
||||
|
||||
| | |
|
||||
| -------------------------------------- | ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
|
||||
| **Most common position in a pipeline** | Before a `ChatPromptBuilder` in a query pipeline |
|
||||
| **Mandatory run variables** | `documents`: A list of documents to process. Each document should have metadata containing at minimum a 'file_path_meta_field' key. PDF documents additionally require a 'page_number' key to specify which page to convert. |
|
||||
| **Output variables** | `image_contents`: A list of `ImageContent` objects |
|
||||
| **API reference** | [Image Converters](/reference/image-converters-api) |
|
||||
| **GitHub link** | https://github.com/deepset-ai/haystack/blob/main/haystack/components/converters/image/document_to_image.py |
|
||||
|
||||
</div>
|
||||
|
||||
## Overview
|
||||
|
||||
`DocumentToImageContent` processes a list of documents containing image or PDF file paths and converts them into `ImageContent` objects.
|
||||
|
||||
- For images, it reads and encodes the file directly.
|
||||
- For PDFs, it extracts the specified page (through `page_number` in metadata) and converts it to an image.
|
||||
|
||||
By default, it looks for the file path in the `file_path` metadata field. You can customize this with the `file_path_meta_field` parameter. The `root_path` lets you specify a common base directory for file resolution.
|
||||
|
||||
This component is typically used in query pipelines right before a `ChatPromptBuilder` when you would like to add Images to your user prompt.
|
||||
|
||||
If `size` is provided, the images will be resized while maintaining aspect ratio. This reduces file size, memory usage, and processing time, which is beneficial when working with models that have resolution constraints or when transmitting images to remote services.
|
||||
|
||||
## Usage
|
||||
|
||||
### On its own
|
||||
|
||||
```python
|
||||
from haystack import Document
|
||||
from haystack.components.converters.image.document_to_image import (
|
||||
DocumentToImageContent,
|
||||
)
|
||||
|
||||
converter = DocumentToImageContent(
|
||||
file_path_meta_field="file_path",
|
||||
root_path="/data/documents",
|
||||
detail="high",
|
||||
size=(800, 600),
|
||||
)
|
||||
|
||||
documents = [
|
||||
Document(content="Photo of a mountain", meta={"file_path": "mountain.jpg"}),
|
||||
Document(
|
||||
content="First page of a report",
|
||||
meta={"file_path": "report.pdf", "page_number": 1},
|
||||
),
|
||||
]
|
||||
|
||||
result = converter.run(documents)
|
||||
image_contents = result["image_contents"]
|
||||
print(image_contents)
|
||||
|
||||
## [
|
||||
## ImageContent(
|
||||
## base64_image="/9j/4A...", mime_type="image/jpeg", detail="high",
|
||||
## meta={"file_path": "mountain.jpg"}
|
||||
## ),
|
||||
## ImageContent(
|
||||
## base64_image="/9j/4A...", mime_type="image/jpeg", detail="high",
|
||||
## meta={"file_path": "report.pdf", "page_number": 1}
|
||||
## )
|
||||
## ]
|
||||
```
|
||||
|
||||
### In a pipeline
|
||||
|
||||
You can use `DocumentToImageContent` in multimodal indexing pipelines before passing to an Embedder or captioning model.
|
||||
|
||||
```python
|
||||
from haystack import Document, Pipeline
|
||||
from haystack.components.builders import ChatPromptBuilder
|
||||
from haystack.components.generators.chat import OpenAIChatGenerator
|
||||
from haystack.components.converters.image.document_to_image import (
|
||||
DocumentToImageContent,
|
||||
)
|
||||
|
||||
## Query pipeline
|
||||
pipeline = Pipeline()
|
||||
pipeline.add_component("image_converter", DocumentToImageContent(detail="auto"))
|
||||
pipeline.add_component(
|
||||
"chat_prompt_builder",
|
||||
ChatPromptBuilder(
|
||||
required_variables=["question"],
|
||||
template="""{% message role="system" %}
|
||||
You are a friendly assistant that answers questions based on provided images.
|
||||
{% endmessage %}
|
||||
|
||||
{%- message role="user" -%}
|
||||
Only provide an answer to the question using the images provided.
|
||||
|
||||
Question: {{ question }}
|
||||
Answer:
|
||||
|
||||
{%- for img in image_contents -%}
|
||||
{{ img | templatize_part }}
|
||||
{%- endfor -%}
|
||||
{%- endmessage -%}
|
||||
""",
|
||||
),
|
||||
)
|
||||
pipeline.add_component("llm", OpenAIChatGenerator(model="gpt-4o-mini"))
|
||||
|
||||
pipeline.connect("image_converter", "chat_prompt_builder.image_contents")
|
||||
pipeline.connect("chat_prompt_builder", "llm")
|
||||
|
||||
documents = [
|
||||
Document(content="Cat image", meta={"file_path": "cat.jpg"}),
|
||||
Document(content="Doc intro", meta={"file_path": "paper.pdf", "page_number": 1}),
|
||||
]
|
||||
|
||||
result = pipeline.run(
|
||||
data={
|
||||
"image_converter": {"documents": documents},
|
||||
"chat_prompt_builder": {"question": "What color is the cat?"},
|
||||
},
|
||||
)
|
||||
print(result)
|
||||
|
||||
## {
|
||||
## "llm": {
|
||||
## "replies": [
|
||||
## ChatMessage(
|
||||
## _role=<ChatRole.ASSISTANT: 'assistant'>,
|
||||
## _content=[TextContent(text="The cat is orange with some black.")],
|
||||
## _name=None,
|
||||
## _meta={
|
||||
## "model": "gpt-4o-mini-2024-07-18",
|
||||
## "index": 0,
|
||||
## "finish_reason": "stop",
|
||||
## "usage": {...},
|
||||
## },
|
||||
## )
|
||||
## ]
|
||||
## }
|
||||
## }
|
||||
```
|
||||
|
||||
## Additional References
|
||||
|
||||
🧑🍳 Cookbook: [Introduction to Multimodality](https://haystack.deepset.ai/cookbook/multimodal_intro)
|
||||
+81
@@ -0,0 +1,81 @@
|
||||
---
|
||||
title: "DOCXToDocument"
|
||||
id: docxtodocument
|
||||
slug: "/docxtodocument"
|
||||
description: "Convert DOCX files to documents."
|
||||
---
|
||||
|
||||
# DOCXToDocument
|
||||
|
||||
Convert DOCX files to documents.
|
||||
|
||||
<div className="key-value-table">
|
||||
|
||||
| | |
|
||||
| :------------------------------------- | :--------------------------------------------------------------------------------------------- |
|
||||
| **Most common position in a pipeline** | Before [PreProcessors](../preprocessors.mdx) or right at the beginning of an indexing pipeline |
|
||||
| **Mandatory run variables** | `sources`: DOCX file paths or [`ByteStream`](../../concepts/data-classes.mdx#bytestream) objects |
|
||||
| **Output variables** | `documents`: A list of documents |
|
||||
| **API reference** | [Converters](/reference/converters-api) |
|
||||
| **GitHub link** | https://github.com/deepset-ai/haystack/blob/main/haystack/components/converters/docx.py |
|
||||
|
||||
</div>
|
||||
|
||||
## Overview
|
||||
|
||||
The `DOCXToDocument` component converts DOCX files into documents. It takes a list of file paths or [`ByteStream`](../../concepts/data-classes.mdx#bytestream) objects as input and outputs the converted result as a list of documents. By defining the table format (CSV or Markdown), you can use this component to extract tables in your DOCX files. Optionally, you can attach metadata to the documents through the `meta` input parameter.
|
||||
|
||||
## Usage
|
||||
|
||||
First, install the`python-docx` package to start using this converter:
|
||||
|
||||
```shell
|
||||
pip install python-docx
|
||||
```
|
||||
|
||||
### On its own
|
||||
|
||||
```python
|
||||
from haystack.components.converters.docx import DOCXToDocument, DOCXTableFormat
|
||||
|
||||
converter = DOCXToDocument()
|
||||
## or define the table format
|
||||
converter = DOCXToDocument(table_format=DOCXTableFormat.CSV)
|
||||
|
||||
results = converter.run(
|
||||
sources=["sample.docx"],
|
||||
meta={"date_added": datetime.now().isoformat()},
|
||||
)
|
||||
documents = results["documents"]
|
||||
|
||||
print(documents[0].content)
|
||||
|
||||
## 'This is the text from the DOCX file.'
|
||||
```
|
||||
|
||||
### In a pipeline
|
||||
|
||||
```python
|
||||
from haystack import Pipeline
|
||||
from haystack.document_stores.in_memory import InMemoryDocumentStore
|
||||
from haystack.components.converters import DOCXToDocument
|
||||
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", DOCXToDocument())
|
||||
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}})
|
||||
```
|
||||
+14
@@ -0,0 +1,14 @@
|
||||
---
|
||||
title: "External Integrations"
|
||||
id: external-integrations-converters
|
||||
slug: "/external-integrations-converters"
|
||||
description: "External integrations that enable extracting data from files in different formats and cast it into the unified document format."
|
||||
---
|
||||
|
||||
# External Integrations
|
||||
|
||||
External integrations that enable extracting data from files in different formats and cast it into the unified document format.
|
||||
|
||||
| Name | Description |
|
||||
| :----------------------------------------------------------- | :----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
|
||||
| [Docling](https://haystack.deepset.ai/integrations/docling/) | Parse PDF, DOCX, HTML, and other document formats into a rich standardized representation (such as layout, tables..), which it can then export to Markdown, JSON, and other formats. |
|
||||
+70
@@ -0,0 +1,70 @@
|
||||
---
|
||||
title: "HTMLToDocument"
|
||||
id: htmltodocument
|
||||
slug: "/htmltodocument"
|
||||
description: "A component that converts HTML files to documents."
|
||||
---
|
||||
|
||||
# HTMLToDocument
|
||||
|
||||
A component that converts HTML files to documents.
|
||||
|
||||
<div className="key-value-table">
|
||||
|
||||
| | |
|
||||
| :------------------------------------- | :---------------------------------------------------------------------------------------------- |
|
||||
| **Most common position in a pipeline** | Before [PreProcessors](../preprocessors.mdx) , or right at the beginning of an indexing pipeline |
|
||||
| **Mandatory run variables** | `sources`: A list of HTML file paths or [`ByteStream`](../../concepts/data-classes.mdx#bytestream) objects |
|
||||
| **Output variables** | `documents`: A list of documents |
|
||||
| **API reference** | [Converters](/reference/converters-api) |
|
||||
| **GitHub link** | https://github.com/deepset-ai/haystack/blob/main/haystack/components/converters/html.py |
|
||||
|
||||
</div>
|
||||
|
||||
## Overview
|
||||
|
||||
The `HTMLToDocument` component converts HTML files into documents. It can be used in an indexing pipeline to index the contents of an HTML file into a Document Store or even in a querying pipeline after the [`LinkContentFetcher`](../fetchers/linkcontentfetcher.mdx). The `HTMLToDocument` component takes a list of HTML file paths or [`ByteStream`](../../concepts/data-classes.mdx#bytestream) objects as input and converts the files to a list of documents. Optionally, you can attach metadata to the documents through the `meta` input parameter.
|
||||
|
||||
When you initialize the component, you can optionally set `extraction_kwargs`, a dictionary containing keyword arguments to customize the extraction process. These are passed to the underlying Trafilatura `extract` function. For the full list of available arguments, see the [Trafilatura documentation](https://trafilatura.readthedocs.io/en/latest/corefunctions.html#extract).
|
||||
|
||||
## Usage
|
||||
|
||||
### On its own
|
||||
|
||||
```python
|
||||
from pathlib import Path
|
||||
from haystack.components.converters import HTMLToDocument
|
||||
|
||||
converter = HTMLToDocument()
|
||||
|
||||
docs = converter.run(sources=[Path("saved_page.html")])
|
||||
```
|
||||
|
||||
### In a pipeline
|
||||
|
||||
Here's an example of an indexing pipeline that writes the contents of an HTML file into an `InMemoryDocumentStore`:
|
||||
|
||||
```python
|
||||
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}})
|
||||
```
|
||||
+104
@@ -0,0 +1,104 @@
|
||||
---
|
||||
title: "ImageFileToDocument"
|
||||
id: imagefiletodocument
|
||||
slug: "/imagefiletodocument"
|
||||
description: "Converts image file references into empty `Document` objects with associated metadata."
|
||||
---
|
||||
|
||||
# ImageFileToDocument
|
||||
|
||||
Converts image file references into empty `Document` objects with associated metadata.
|
||||
|
||||
<div className="key-value-table">
|
||||
|
||||
| | |
|
||||
| -------------------------------------- | --------------------------------------------------------------------------------------------------------------------------- |
|
||||
| **Most common position in a pipeline** | Before a component that processes images, like `SentenceTransformersImageDocumentEmbedder` or `LLMDocumentContentExtractor` |
|
||||
| **Mandatory run variables** | `sources`: A list of image file paths or ByteStreams |
|
||||
| **Output variables** | `documents`: A list of empty Document objects with associated metadata |
|
||||
| **API reference** | [Image Converters](/reference/image-converters-api) |
|
||||
| **GitHub link** | https://github.com/deepset-ai/haystack/blob/main/haystack/components/converters/image/file_to_document.py |
|
||||
|
||||
</div>
|
||||
|
||||
## Overview
|
||||
|
||||
`ImageFileToDocument` converts image file sources into empty `Document` objects with associated metadata.
|
||||
|
||||
This component is useful in pipelines where image file paths need to be wrapped in `Document` objects to be processed by downstream components such as `SentenceTransformersImageDocumentEmbedder` or `LLMDocumentContentExtractor`.
|
||||
|
||||
It _does not_ extract any content from the image files, but instead creates `Document` objects with `None` as their content and attaches metadata such as file path and any user-provided values.
|
||||
|
||||
Each source can be:
|
||||
|
||||
- A file path (string or `Path`), or
|
||||
- A `ByteStream` object.
|
||||
|
||||
Optionally, you can provide metadata using the `meta` parameter. This can be a single dictionary (applied to all documents) or a list matching the length of `sources`.
|
||||
|
||||
## Usage
|
||||
|
||||
### On its own
|
||||
|
||||
This component is primarily meant to be used in pipelines.
|
||||
|
||||
```python
|
||||
|
||||
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'})]
|
||||
```
|
||||
|
||||
### In a pipeline
|
||||
|
||||
In the following Pipeline, image documents are created using the `ImageFileToDocument` component, then they are enriched with image embeddings and saved in the Document Store.
|
||||
|
||||
```python
|
||||
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
|
||||
```
|
||||
|
||||
## Additional References
|
||||
|
||||
🧑🍳 Cookbook: [Introduction to Multimodality](https://haystack.deepset.ai/cookbook/multimodal_intro)
|
||||
+128
@@ -0,0 +1,128 @@
|
||||
---
|
||||
title: "ImageFileToImageContent"
|
||||
id: imagefiletoimagecontent
|
||||
slug: "/imagefiletoimagecontent"
|
||||
description: "`ImageFileToImageContent` reads local image files and converts them into `ImageContent` objects. These are ready for multimodal AI pipelines, including tasks like image captioning, visual QA, or prompt-based generation."
|
||||
---
|
||||
|
||||
# ImageFileToImageContent
|
||||
|
||||
`ImageFileToImageContent` reads local image files and converts them into `ImageContent` objects. These are ready for multimodal AI pipelines, including tasks like image captioning, visual QA, or prompt-based generation.
|
||||
|
||||
<div className="key-value-table">
|
||||
|
||||
| | |
|
||||
| -------------------------------------- | -------------------------------------------------------------------------------------------------------- |
|
||||
| **Most common position in a pipeline** | Before a `ChatPromptBuilder` in a query pipeline |
|
||||
| **Mandatory run variables** | `sources`: A list of image file paths or ByteStreams |
|
||||
| **Output variables** | `image_contents`: A list of ImageContent objects |
|
||||
| **API reference** | [Image Converters](/reference/image-converters-api) |
|
||||
| **GitHub link** | https://github.com/deepset-ai/haystack/blob/main/haystack/components/converters/image/file_to_image.py |
|
||||
|
||||
</div>
|
||||
|
||||
## Overview
|
||||
|
||||
`ImageFileToImageContent` processes a list of image sources and converts them into `ImageContent` objects. These can be used in multimodal pipelines that require base64-encoded image input.
|
||||
|
||||
Each source can be:
|
||||
|
||||
- A file path (string or `Path`), or
|
||||
- A `ByteStream` object.
|
||||
|
||||
Optionally, you can provide metadata using the `meta` parameter. This can be a single dictionary (applied to all images) or a list matching the length of `sources`.
|
||||
|
||||
Use the `size` parameter to resize images while preserving aspect ratio. This reduces memory usage and transmission size, which is helpful when working with remote models or limited-resource environments.
|
||||
|
||||
This component is often used in query pipelines just before a `ChatPromptBuilder`.
|
||||
|
||||
## Usage
|
||||
|
||||
### On its own
|
||||
|
||||
```python
|
||||
|
||||
from haystack.components.converters.image import ImageFileToImageContent
|
||||
|
||||
converter = ImageFileToImageContent(detail="high", size=(800, 600))
|
||||
|
||||
sources = ["cat.jpg", "scenery.png"]
|
||||
|
||||
result = converter.run(sources=sources)
|
||||
image_contents = result["image_contents"]
|
||||
print(image_contents)
|
||||
|
||||
## [
|
||||
## ImageContent(
|
||||
## base64_image="/9j/4A...", mime_type="image/jpeg", detail="high",
|
||||
## meta={"file_path": "cat.jpg"}
|
||||
## ),
|
||||
## ImageContent(
|
||||
## base64_image="/9j/4A...", mime_type="image/png", detail="high",
|
||||
## meta={"file_path": "scenery.png"}
|
||||
## )
|
||||
## ]
|
||||
```
|
||||
|
||||
### In a pipeline
|
||||
|
||||
Use `ImageFileToImageContent` to supply image data to a `ChatPromptBuilder` for multimodal QA or captioning with an LLM.
|
||||
|
||||
```python
|
||||
from haystack import Pipeline
|
||||
from haystack.components.builders import ChatPromptBuilder
|
||||
from haystack.components.generators.chat import OpenAIChatGenerator
|
||||
from haystack.components.converters.image import ImageFileToImageContent
|
||||
|
||||
## Query pipeline
|
||||
pipeline = Pipeline()
|
||||
pipeline.add_component("image_converter", ImageFileToImageContent(detail="auto"))
|
||||
pipeline.add_component(
|
||||
"chat_prompt_builder",
|
||||
ChatPromptBuilder(
|
||||
required_variables=["question"],
|
||||
template="""{% message role="system" %}
|
||||
You are a helpful assistant that answers questions using the provided images.
|
||||
{% endmessage %}
|
||||
|
||||
{% message role="user" %}
|
||||
Question: {{ question }}
|
||||
|
||||
{% for img in image_contents %}
|
||||
{{ img | templatize_part }}
|
||||
{% endfor %}
|
||||
{% endmessage %}
|
||||
""",
|
||||
),
|
||||
)
|
||||
pipeline.add_component("llm", OpenAIChatGenerator(model="gpt-4o-mini"))
|
||||
|
||||
pipeline.connect("image_converter", "chat_prompt_builder.image_contents")
|
||||
pipeline.connect("chat_prompt_builder", "llm")
|
||||
|
||||
sources = ["apple.jpg", "haystack-logo.png"]
|
||||
|
||||
result = pipeline.run(
|
||||
data={
|
||||
"image_converter": {"sources": sources},
|
||||
"chat_prompt_builder": {"question": "Describe the Haystack logo."},
|
||||
},
|
||||
)
|
||||
print(result)
|
||||
|
||||
## {
|
||||
## "llm": {
|
||||
## "replies": [
|
||||
## ChatMessage(
|
||||
## _role=<ChatRole.ASSISTANT: 'assistant'>,
|
||||
## _content=[TextContent(text="The Haystack logo features...")],
|
||||
## ...
|
||||
## )
|
||||
## ]
|
||||
## }
|
||||
## }
|
||||
```
|
||||
|
||||
## Additional References
|
||||
|
||||
🧑🍳 Cookbook: [Introduction to Multimodality](https://haystack.deepset.ai/cookbook/multimodal_intro)
|
||||
+118
@@ -0,0 +1,118 @@
|
||||
---
|
||||
title: "JSONConverter"
|
||||
id: jsonconverter
|
||||
slug: "/jsonconverter"
|
||||
description: "Converts JSON files to text documents."
|
||||
---
|
||||
|
||||
# JSONConverter
|
||||
|
||||
Converts JSON files to text documents.
|
||||
|
||||
<div className="key-value-table">
|
||||
|
||||
| | |
|
||||
| --- | --- |
|
||||
| **Most common position in a pipeline** | Before [PreProcessors](../preprocessors.mdx) , or right at the beginning of an indexing pipeline |
|
||||
| **Mandatory init variables** | ONE OF, OR BOTH: <br /> <br />`jq_schema`: A jq filter string to extract content <br /> <br />`content_key`: A key string to extract document content |
|
||||
| **Mandatory run variables** | `sources`: A list of file paths or [ByteStream](../../concepts/data-classes.mdx#bytestream) objects |
|
||||
| **Output variables** | `documents`: A list of documents |
|
||||
| **API reference** | [Converters](/reference/converters-api) |
|
||||
| **GitHub link** | https://github.com/deepset-ai/haystack/blob/main/haystack/components/converters/json.py |
|
||||
|
||||
</div>
|
||||
|
||||
## Overview
|
||||
|
||||
`JSONConverter` converts one or more JSON files into a text document.
|
||||
|
||||
### Parameters Overview
|
||||
|
||||
To initialize `JSONConverter`, you must provide either `jq_schema`, or `content_key` parameter, or both.
|
||||
|
||||
`jq_schema` parameter filter extracts nested data from JSON files. Refer to the [jq documentation](https://jqlang.github.io/jq/) for filter syntax. If not set, the entire JSON file is used.
|
||||
|
||||
The `content_key` parameter lets you specify which key in the extracted data will be the document's content.
|
||||
|
||||
- If both `jq_schema` and `content_key` are set, the `content_key` is searched in the data extracted by `jq_schema`. Non-object data will be skipped.
|
||||
- If only `jq_schema` is set, the extracted value must be scalar; objects or arrays will be skipped.
|
||||
- If only `content_key` is set, the source must be a JSON object, or it will be skipped.
|
||||
|
||||
Check out the [API reference](../converters.mdx) for the full list of parameters.
|
||||
|
||||
## Usage
|
||||
|
||||
You need to install the `jq` package to use this Converter:
|
||||
|
||||
```shell
|
||||
pip install jq
|
||||
```
|
||||
|
||||
### Example
|
||||
|
||||
Here is an example of simple component usage:
|
||||
|
||||
```python
|
||||
import json
|
||||
|
||||
from haystack.components.converters import JSONConverter
|
||||
from haystack.dataclasses import ByteStream
|
||||
|
||||
source = ByteStream.from_string(
|
||||
json.dumps({"text": "This is the content of my document"}),
|
||||
)
|
||||
|
||||
converter = JSONConverter(content_key="text")
|
||||
results = converter.run(sources=[source])
|
||||
documents = results["documents"]
|
||||
print(documents[0].content)
|
||||
## 'This is the content of my document'
|
||||
```
|
||||
|
||||
In the following more complex example, we provide a `jq_schema` string to filter the JSON source files and `extra_meta_fields` to extract from the filtered data:
|
||||
|
||||
```python
|
||||
import json
|
||||
|
||||
from haystack.components.converters import JSONConverter
|
||||
from haystack.dataclasses import ByteStream
|
||||
|
||||
data = {
|
||||
"laureates": [
|
||||
{
|
||||
"firstname": "Enrico",
|
||||
"surname": "Fermi",
|
||||
"motivation": "for his demonstrations of the existence of new radioactive elements produced "
|
||||
"by neutron irradiation, and for his related discovery of nuclear reactions brought about by"
|
||||
" slow neutrons",
|
||||
},
|
||||
{
|
||||
"firstname": "Rita",
|
||||
"surname": "Levi-Montalcini",
|
||||
"motivation": "for their discoveries of growth factors",
|
||||
},
|
||||
],
|
||||
}
|
||||
source = ByteStream.from_string(json.dumps(data))
|
||||
converter = JSONConverter(
|
||||
jq_schema=".laureates[]",
|
||||
content_key="motivation",
|
||||
extra_meta_fields={"firstname", "surname"},
|
||||
)
|
||||
|
||||
results = converter.run(sources=[source])
|
||||
documents = results["documents"]
|
||||
print(documents[0].content)
|
||||
## 'for his demonstrations of the existence of new radioactive elements produced by
|
||||
## neutron irradiation, and for his related discovery of nuclear reactions brought
|
||||
## about by slow neutrons'
|
||||
|
||||
print(documents[0].meta)
|
||||
## {'firstname': 'Enrico', 'surname': 'Fermi'}
|
||||
|
||||
print(documents[1].content)
|
||||
## 'for their discoveries of growth factors'
|
||||
|
||||
print(documents[1].meta)
|
||||
## {'firstname': 'Rita', 'surname': 'Levi-Montalcini'}
|
||||
```
|
||||
+77
@@ -0,0 +1,77 @@
|
||||
---
|
||||
title: "MarkdownToDocument"
|
||||
id: markdowntodocument
|
||||
slug: "/markdowntodocument"
|
||||
description: "A component that converts Markdown files to documents."
|
||||
---
|
||||
|
||||
# MarkdownToDocument
|
||||
|
||||
A component that converts Markdown files to documents.
|
||||
|
||||
<div className="key-value-table">
|
||||
|
||||
| | |
|
||||
| :------------------------------------- | :---------------------------------------------------------------------------------------------- |
|
||||
| **Most common position in a pipeline** | Before [PreProcessors](../preprocessors.mdx) , or right at the beginning of an indexing pipeline |
|
||||
| **Mandatory run variables** | `sources`: Markdown file paths or [`ByteStream`](../../concepts/data-classes.mdx#bytestream) objects |
|
||||
| **Output variables** | `documents`: A list of documents |
|
||||
| **API reference** | [Converters](/reference/converters-api) |
|
||||
| **GitHub link** | https://github.com/deepset-ai/haystack/blob/main/haystack/components/converters/markdown.py |
|
||||
|
||||
</div>
|
||||
|
||||
## Overview
|
||||
|
||||
The `MarkdownToDocument` component converts Markdown files into documents. You can use it in an indexing pipeline to index the contents of a Markdown file into a Document Store. It takes a list of file paths or [ByteStream](../../concepts/data-classes.mdx#bytestream) objects as input and outputs the converted result as a list of documents. Optionally, you can attach metadata to the documents through the `meta` input parameter.
|
||||
|
||||
When you initialize the component, you can optionally turn off progress bars by setting `progress_bar` to `False`. If you want to convert the contents of tables into a single line, you can enable that through the `table_to_single_line` parameter.
|
||||
|
||||
## Usage
|
||||
|
||||
You need to install `markdown-it-py` and `mdit_plain packages` to use the `MarkdownToDocument` component:
|
||||
|
||||
```shell
|
||||
pip install markdown-it-py mdit_plain
|
||||
```
|
||||
|
||||
### On its own
|
||||
|
||||
```python
|
||||
from haystack.components.converters import MarkdownToDocument
|
||||
|
||||
converter = MarkdownToDocument()
|
||||
|
||||
docs = converter.run(sources=Path("my_file.md"))
|
||||
```
|
||||
|
||||
### In a pipeline
|
||||
|
||||
```python
|
||||
from haystack import Pipeline
|
||||
from haystack.document_stores.in_memory import InMemoryDocumentStore
|
||||
from haystack.components.converters import MarkdownToDocument
|
||||
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", MarkdownToDocument())
|
||||
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}})
|
||||
```
|
||||
|
||||
## Additional References
|
||||
|
||||
:notebook: Tutorial: [Preprocessing Different File Types](https://haystack.deepset.ai/tutorials/30_file_type_preprocessing_index_pipeline)
|
||||
+189
@@ -0,0 +1,189 @@
|
||||
---
|
||||
title: "MistralOCRDocumentConverter"
|
||||
id: mistralocrdocumentconverter
|
||||
slug: "/mistralocrdocumentconverter"
|
||||
description: "`MistralOCRDocumentConverter` extracts text from documents using Mistral's OCR API, with optional structured annotations for both individual image regions and full documents. It supports various input formats including local files, URLs, and Mistral file IDs."
|
||||
---
|
||||
|
||||
# MistralOCRDocumentConverter
|
||||
|
||||
`MistralOCRDocumentConverter` extracts text from documents using Mistral's OCR API, with optional structured annotations for both individual image regions and full documents. It supports various input formats including local files, URLs, and Mistral file IDs.
|
||||
|
||||
<div className="key-value-table">
|
||||
|
||||
| | |
|
||||
| --- | --- |
|
||||
| **Most common position in a pipeline** | Before [PreProcessors](../preprocessors.mdx), or right at the beginning of an indexing pipeline |
|
||||
| **Mandatory init variables** | `api_key`: The Mistral API key. Can be set with `MISTRAL_API_KEY` environment variable. |
|
||||
| **Mandatory run variables** | `sources`: A list of document sources (file paths, ByteStreams, URLs, or Mistral chunks) |
|
||||
| **Output variables** | `documents`: A list of documents <br /> <br />`raw_mistral_response`: A list of raw OCR responses from Mistral API |
|
||||
| **API reference** | [Mistral](/reference/integrations-mistral) |
|
||||
| **GitHub link** | https://github.com/deepset-ai/haystack-core-integrations/tree/main/integrations/mistral |
|
||||
|
||||
</div>
|
||||
|
||||
## Overview
|
||||
|
||||
The `MistralOCRDocumentConverter` takes a list of document sources and uses Mistral's OCR API to extract text from images and PDFs. It supports multiple input formats:
|
||||
|
||||
- **Local files**: File paths (str or Path) or [`ByteStream`](../../concepts/data-classes.mdx#bytestream) objects
|
||||
- **Remote resources**: Document URLs, image URLs using Mistral's `DocumentURLChunk` and `ImageURLChunk`
|
||||
- **Mistral storage**: File IDs using Mistral's `FileChunk` for files previously uploaded to Mistral
|
||||
|
||||
The component returns one Haystack [`Document`](../../concepts/data-classes.mdx#document) per source, with all pages concatenated using form feed characters (`\f`) as separators. This format ensures compatibility with Haystack's [`DocumentSplitter`](../preprocessors/documentsplitter.mdx) for accurate page-wise splitting and overlap handling. The content is returned in markdown format, with images represented as `` tags.
|
||||
|
||||
By default, the component uses the `MISTRAL_API_KEY` environment variable for authentication. You can also pass an `api_key` at initialization. Local files are automatically uploaded to Mistral's storage for processing and deleted afterward (configurable with `cleanup_uploaded_files`).
|
||||
|
||||
When you initialize the component, you can optionally specify which pages to process, set limits on image extraction, configure minimum image sizes, or include base64-encoded images in the response. The default model is `"mistral-ocr-2505"`. See the [Mistral models documentation](https://docs.mistral.ai/getting-started/models/models_overview/) for available models.
|
||||
|
||||
### Structured Annotations
|
||||
|
||||
A unique feature of `MistralOCRDocumentConverter` is its support for structured annotations using Pydantic schemas:
|
||||
|
||||
- **Bounding box annotations** (`bbox_annotation_schema`): Annotate individual image regions with structured data (for example, image type, description, summary). These annotations are inserted inline after the corresponding image tags in the markdown content.
|
||||
- **Document annotations** (`document_annotation_schema`): Annotate the full document with structured data (for example, language, chapter titles, URLs). These annotations are unpacked into the document's metadata with a `source_` prefix (for example, `source_language`, `source_chapter_titles`).
|
||||
|
||||
When annotation schemas are provided, the OCR model first extracts text and structure, then a Vision LLM analyzes the content and generates structured annotations according to your defined Pydantic schemas. Note that document annotation is limited to a maximum of 8 pages. For more details, see the [Mistral documentation on annotations](https://docs.mistral.ai/capabilities/document_ai/annotations/).
|
||||
|
||||
## Usage
|
||||
|
||||
You need to install the `mistral-haystack` integration to use `MistralOCRDocumentConverter`:
|
||||
|
||||
```shell
|
||||
pip install mistral-haystack
|
||||
```
|
||||
|
||||
### On its own
|
||||
|
||||
Basic usage with a local file:
|
||||
|
||||
```python
|
||||
from pathlib import Path
|
||||
from haystack.utils import Secret
|
||||
from haystack_integrations.components.converters.mistral import (
|
||||
MistralOCRDocumentConverter,
|
||||
)
|
||||
|
||||
converter = MistralOCRDocumentConverter(
|
||||
api_key=Secret.from_env_var("MISTRAL_API_KEY"),
|
||||
model="mistral-ocr-2505",
|
||||
)
|
||||
|
||||
result = converter.run(sources=[Path("my_document.pdf")])
|
||||
documents = result["documents"]
|
||||
```
|
||||
|
||||
Processing multiple sources with different types:
|
||||
|
||||
```python
|
||||
from pathlib import Path
|
||||
from haystack.utils import Secret
|
||||
from haystack_integrations.components.converters.mistral import (
|
||||
MistralOCRDocumentConverter,
|
||||
)
|
||||
from mistralai.models import DocumentURLChunk, ImageURLChunk
|
||||
|
||||
converter = MistralOCRDocumentConverter(
|
||||
api_key=Secret.from_env_var("MISTRAL_API_KEY"),
|
||||
model="mistral-ocr-2505",
|
||||
)
|
||||
|
||||
sources = [
|
||||
Path("local_document.pdf"),
|
||||
DocumentURLChunk(document_url="https://example.com/document.pdf"),
|
||||
ImageURLChunk(image_url="https://example.com/receipt.jpg"),
|
||||
]
|
||||
|
||||
result = converter.run(sources=sources)
|
||||
documents = result["documents"] # List of 3 Documents
|
||||
raw_responses = result["raw_mistral_response"] # List of 3 raw responses
|
||||
```
|
||||
|
||||
Using structured annotations:
|
||||
|
||||
```python
|
||||
from pathlib import Path
|
||||
from typing import List
|
||||
from pydantic import BaseModel, Field
|
||||
from haystack.utils import Secret
|
||||
from haystack_integrations.components.converters.mistral import (
|
||||
MistralOCRDocumentConverter,
|
||||
)
|
||||
from mistralai.models import DocumentURLChunk
|
||||
|
||||
|
||||
# Define schema for image region annotations
|
||||
class ImageAnnotation(BaseModel):
|
||||
image_type: str = Field(..., description="The type of image content")
|
||||
short_description: str = Field(
|
||||
...,
|
||||
description="Short natural-language description",
|
||||
)
|
||||
summary: str = Field(..., description="Detailed summary of the image content")
|
||||
|
||||
|
||||
# Define schema for document-level annotations
|
||||
class DocumentAnnotation(BaseModel):
|
||||
language: str = Field(..., description="Primary language of the document")
|
||||
chapter_titles: List[str] = Field(
|
||||
...,
|
||||
description="Detected chapter or section titles",
|
||||
)
|
||||
urls: List[str] = Field(..., description="URLs found in the text")
|
||||
|
||||
|
||||
converter = MistralOCRDocumentConverter(
|
||||
api_key=Secret.from_env_var("MISTRAL_API_KEY"),
|
||||
model="mistral-ocr-2505",
|
||||
)
|
||||
|
||||
sources = [DocumentURLChunk(document_url="https://example.com/report.pdf")]
|
||||
result = converter.run(
|
||||
sources=sources,
|
||||
bbox_annotation_schema=ImageAnnotation,
|
||||
document_annotation_schema=DocumentAnnotation,
|
||||
)
|
||||
|
||||
documents = result["documents"]
|
||||
# Document metadata will include:
|
||||
# - source_language: extracted from DocumentAnnotation
|
||||
# - source_chapter_titles: extracted from DocumentAnnotation
|
||||
# - source_urls: extracted from DocumentAnnotation
|
||||
# Document content will include inline image annotations
|
||||
```
|
||||
|
||||
### In a pipeline
|
||||
|
||||
Here's an example of an indexing pipeline that processes PDFs with OCR and writes them to a Document Store:
|
||||
|
||||
```python
|
||||
from haystack import Pipeline
|
||||
from haystack.document_stores.in_memory import InMemoryDocumentStore
|
||||
from haystack.components.preprocessors import DocumentCleaner, DocumentSplitter
|
||||
from haystack.components.writers import DocumentWriter
|
||||
from haystack.utils import Secret
|
||||
from haystack_integrations.components.converters.mistral import (
|
||||
MistralOCRDocumentConverter,
|
||||
)
|
||||
|
||||
document_store = InMemoryDocumentStore()
|
||||
|
||||
pipeline = Pipeline()
|
||||
pipeline.add_component(
|
||||
"converter",
|
||||
MistralOCRDocumentConverter(
|
||||
api_key=Secret.from_env_var("MISTRAL_API_KEY"),
|
||||
model="mistral-ocr-2505",
|
||||
),
|
||||
)
|
||||
pipeline.add_component("cleaner", DocumentCleaner())
|
||||
pipeline.add_component("splitter", DocumentSplitter(split_by="page", split_length=1))
|
||||
pipeline.add_component("writer", DocumentWriter(document_store=document_store))
|
||||
|
||||
pipeline.connect("converter", "cleaner")
|
||||
pipeline.connect("cleaner", "splitter")
|
||||
pipeline.connect("splitter", "writer")
|
||||
|
||||
file_paths = ["invoice.pdf", "receipt.jpg", "contract.pdf"]
|
||||
pipeline.run({"converter": {"sources": file_paths}})
|
||||
```
|
||||
+77
@@ -0,0 +1,77 @@
|
||||
---
|
||||
title: "MSGToDocument"
|
||||
id: msgtodocument
|
||||
slug: "/msgtodocument"
|
||||
description: "Converts Microsoft Outlook .msg files to documents."
|
||||
---
|
||||
|
||||
# MSGToDocument
|
||||
|
||||
Converts Microsoft Outlook .msg files to documents.
|
||||
|
||||
<div className="key-value-table">
|
||||
|
||||
| | |
|
||||
| --- | --- |
|
||||
| **Most common position in a pipeline** | Before [PreProcessors](../preprocessors.mdx) , or right at the beginning of an indexing pipeline |
|
||||
| **Mandatory run variables** | `sources`: A list of .msg file paths or [ByteStream](../../concepts/data-classes.mdx#bytestream) objects |
|
||||
| **Output variables** | `documents`: A list of documents <br /> <br />`attachments`: A list of ByteStream objects representing file attachments |
|
||||
| **API reference** | [Converters](/reference/converters-api) |
|
||||
| **GitHub link** | https://github.com/deepset-ai/haystack/blob/main/haystack/components/converters/msg.py |
|
||||
|
||||
</div>
|
||||
|
||||
## Overview
|
||||
|
||||
The `MSGToDocument` component converts Microsoft Outlook `.msg` files into documents. This component extracts the email metadata (such as sender, recipients, CC, BCC, subject) and body content. Additionally, any file attachments within the `.msg` file are extracted as `ByteStream` objects.
|
||||
|
||||
## Usage
|
||||
|
||||
First, install the `python-oxmsg` package to start using this converter:
|
||||
|
||||
```
|
||||
pip install python-oxmsg
|
||||
```
|
||||
|
||||
### On its own
|
||||
|
||||
```python
|
||||
from haystack.components.converters.msg import MSGToDocument
|
||||
from datetime import datetime
|
||||
|
||||
converter = MSGToDocument()
|
||||
results = converter.run(
|
||||
sources=["sample.msg"],
|
||||
meta={"date_added": datetime.now().isoformat()},
|
||||
)
|
||||
documents = results["documents"]
|
||||
attachments = results["attachments"]
|
||||
|
||||
print(documents[0].content)
|
||||
```
|
||||
|
||||
### In a pipeline
|
||||
|
||||
The following setup enables efficient extraction, preprocessing, and indexing of `.msg` email files within a Haystack pipeline:
|
||||
|
||||
```python
|
||||
from haystack import Pipeline
|
||||
from haystack.document_stores.in_memory import InMemoryDocumentStore
|
||||
from haystack.components.routers import FileTypeRouter
|
||||
from haystack.components.converters import MSGToDocument
|
||||
from haystack.components.writers import DocumentWriter
|
||||
|
||||
router = FileTypeRouter(mime_types=["application/vnd.ms-outlook"])
|
||||
document_store = InMemoryDocumentStore()
|
||||
|
||||
pipeline = Pipeline()
|
||||
pipeline.add_component("router", router)
|
||||
pipeline.add_component("converter", MSGToDocument())
|
||||
pipeline.add_component("writer", DocumentWriter(document_store=document_store))
|
||||
|
||||
pipeline.connect("router.application/vnd.ms-outlook", "converter.sources")
|
||||
pipeline.connect("converter.documents", "writer.documents")
|
||||
|
||||
file_names = ["email1.msg", "email2.msg"]
|
||||
pipeline.run({"converter": {"sources": file_names}})
|
||||
```
|
||||
+79
@@ -0,0 +1,79 @@
|
||||
---
|
||||
title: "MultiFileConverter"
|
||||
id: multifileconverter
|
||||
slug: "/multifileconverter"
|
||||
description: "Converts CSV, DOCX, HTML, JSON, MD, PPTX, PDF, TXT, and XSLX files to documents."
|
||||
---
|
||||
|
||||
# MultiFileConverter
|
||||
|
||||
Converts CSV, DOCX, HTML, JSON, MD, PPTX, PDF, TXT, and XSLX files to documents.
|
||||
|
||||
<div className="key-value-table">
|
||||
|
||||
| | |
|
||||
| --- | --- |
|
||||
| **Most common position in a pipeline** | Before PreProcessors , or right at the beginning of an indexing pipeline |
|
||||
| **Mandatory run variables** | `sources`: A list of file paths or ByteStream objects |
|
||||
| **Output variables** | `documents`: A list of converted documents <br /> <br />`unclassified`: A list of uncategorized file paths or byte streams |
|
||||
| **API reference** | [Converters](/reference/converters-api) |
|
||||
| **GitHub link** | https://github.com/deepset-ai/haystack/blob/main/haystack/components/converters/multi_file_converter.py |
|
||||
|
||||
</div>
|
||||
|
||||
## Overview
|
||||
|
||||
`MultiFileConverter` converts input files of various file types into documents.
|
||||
|
||||
It is a SuperComponent that combines a [`FileTypeRouter`](../routers/filetyperouter.mdx), nine converters and a [`DocumentJoiner`](../joiners/documentjoiner.mdx) into a single component.
|
||||
|
||||
### Parameters
|
||||
|
||||
To initialize `MultiFileConverter`, there are no mandatory parameters. Optionally, you can provide `encoding` and `json_content_key` parameters.
|
||||
|
||||
The `json_content_key` parameter lets you specify for the JSON files which key in the extracted data will be the document's content. The parameter is passed on to the underlying [`JSONConverter`](jsonconverter.mdx) component.
|
||||
|
||||
The `encoding` parameter lets you specify the default encoding of the TXT, CSV, and MD files. If you don't provide any value, the component uses `utf-8` by default. Note that if the encoding is specified in the metadata of an input ByteStream, it will override this parameter's setting. The parameter is passed on to the underlying [`TextFileToDocument`](textfiletodocument.mdx) and [`CSVToDocument`](csvtodocument.mdx) components.
|
||||
|
||||
## Usage
|
||||
|
||||
Install dependencies for all supported file types to use the `MultiFileConverter`:
|
||||
|
||||
```shell
|
||||
pip install pypdf markdown-it-py mdit_plain trafilatura python-pptx python-docx jq openpyxl tabulate pandas
|
||||
```
|
||||
|
||||
### On its own
|
||||
|
||||
```python
|
||||
from haystack.components.converters import MultiFileConverter
|
||||
|
||||
converter = MultiFileConverter()
|
||||
converter.run(sources=["test.txt", "test.pdf"], meta={})
|
||||
```
|
||||
|
||||
### In a pipeline
|
||||
|
||||
You can also use `MultiFileConverter` in your indexing pipeline.
|
||||
|
||||
```python
|
||||
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}}
|
||||
```
|
||||
+136
@@ -0,0 +1,136 @@
|
||||
---
|
||||
title: "OpenAPIServiceToFunctions"
|
||||
id: openapiservicetofunctions
|
||||
slug: "/openapiservicetofunctions"
|
||||
description: "`OpenAPIServiceToFunctions` is a component that transforms OpenAPI service specifications into a format compatible with OpenAI's function calling mechanism."
|
||||
---
|
||||
|
||||
# OpenAPIServiceToFunctions
|
||||
|
||||
`OpenAPIServiceToFunctions` is a component that transforms OpenAPI service specifications into a format compatible with OpenAI's function calling mechanism.
|
||||
|
||||
<div className="key-value-table">
|
||||
|
||||
| | |
|
||||
| --- | --- |
|
||||
| **Most common position in a pipeline** | Flexible |
|
||||
| **Mandatory run variables** | `sources`: A list of OpenAPI specification sources, which can be file paths or [`ByteStream`](../../concepts/data-classes.mdx#bytestream) objects |
|
||||
| **Output variables** | `functions`: A list of JSON OpenAI function calling definitions objects. For each path definition in OpenAPI specification, a corresponding OpenAI function calling definitions is generated. <br /> <br />`openapi_specs`: A list of JSON/YAML objects with references resolved. Such OpenAPI spec (with references resolved) can, in turn, be used as input to OpenAPIServiceConnector. |
|
||||
| **API reference** | [Converters](/reference/converters-api) |
|
||||
| **GitHub link** | https://github.com/deepset-ai/haystack/blob/main/haystack/components/converters/openapi_functions.py |
|
||||
|
||||
</div>
|
||||
|
||||
## Overview
|
||||
|
||||
`OpenAPIServiceToFunctions` transforms OpenAPI service specifications into an OpenAI function calling format. It takes an OpenAPI specification, processes it to extract function definitions, and formats these definitions to be compatible with OpenAI's function calling JSON format.
|
||||
|
||||
`OpenAPIServiceToFunctions` is valuable when used together with [`OpenAPIServiceConnector`](../connectors/openapiserviceconnector.mdx) component. It converts OpenAPI specifications into definitions suitable for OpenAI's function calls, allowing `OpenAPIServiceConnector` to handle input parameters for the OpenAPI specification and facilitate their use in REST API calls through `OpenAPIServiceConnector`.
|
||||
|
||||
To use `OpenAPIServiceToFunctions`, you need to install an optional `jsonref` dependency with:
|
||||
|
||||
```shell
|
||||
pip install jsonref
|
||||
```
|
||||
|
||||
`OpenAPIServiceToFunctions` component doesn’t have any init parameters.
|
||||
|
||||
## Usage
|
||||
|
||||
### On its own
|
||||
|
||||
This component is primarily meant to be used in pipelines. Using this component alone is useful when you want to convert OpenAPI specification into OpenAI's function call specification and then perhaps save it in a file and subsequently use it in function calling.
|
||||
|
||||
### In a pipeline
|
||||
|
||||
In a pipeline context, `OpenAPIServiceToFunctions` is most valuable when used alongside `OpenAPIServiceConnector`. For instance, let’s consider integrating [serper.dev](http://serper.dev/) search engine bridge into a pipeline. `OpenAPIServiceToFunctions` retrieves the OpenAPI specification of Serper from https://bit.ly/serper_dev_spec, converts this specification into a format that OpenAI's function calling mechanism can understand, and then seamlessly passes this translated specification as `generation_kwargs` for LLM function calling invocation.
|
||||
|
||||
:::note
|
||||
To run the following code snippet, note that you have to have your own Serper and OpenAI API keys.
|
||||
|
||||
:::
|
||||
|
||||
```python
|
||||
import json
|
||||
import requests
|
||||
|
||||
from typing import Dict, Any, List
|
||||
from haystack import Pipeline
|
||||
from haystack.components.generators.utils import print_streaming_chunk
|
||||
from haystack.components.converters import OpenAPIServiceToFunctions, OutputAdapter
|
||||
from haystack.components.generators.chat import OpenAIChatGenerator
|
||||
from haystack.components.connectors import OpenAPIServiceConnector
|
||||
from haystack.components.fetchers import LinkContentFetcher
|
||||
from haystack.dataclasses import ChatMessage, ByteStream
|
||||
from haystack.utils import Secret
|
||||
|
||||
|
||||
def prepare_fc_params(openai_functions_schema: Dict[str, Any]) -> Dict[str, Any]:
|
||||
return {
|
||||
"tools": [{"type": "function", "function": openai_functions_schema}],
|
||||
"tool_choice": {
|
||||
"type": "function",
|
||||
"function": {"name": openai_functions_schema["name"]},
|
||||
},
|
||||
}
|
||||
|
||||
|
||||
system_prompt = requests.get("https://bit.ly/serper_dev_system_prompt").text
|
||||
serper_spec = requests.get("https://bit.ly/serper_dev_spec").text
|
||||
|
||||
pipe = Pipeline()
|
||||
pipe.add_component("spec_to_functions", OpenAPIServiceToFunctions())
|
||||
pipe.add_component(
|
||||
"functions_llm",
|
||||
OpenAIChatGenerator(
|
||||
api_key=Secret.from_token(llm_api_key),
|
||||
model="gpt-3.5-turbo-0613",
|
||||
),
|
||||
)
|
||||
pipe.add_component("openapi_container", OpenAPIServiceConnector())
|
||||
pipe.add_component(
|
||||
"a1",
|
||||
OutputAdapter(
|
||||
"{{functions[0] | prepare_fc}}",
|
||||
Dict[str, Any],
|
||||
{"prepare_fc": prepare_fc_params},
|
||||
),
|
||||
)
|
||||
pipe.add_component("a2", OutputAdapter("{{specs[0]}}", Dict[str, Any]))
|
||||
pipe.add_component(
|
||||
"a3",
|
||||
OutputAdapter("{{system_message + service_response}}", List[ChatMessage]),
|
||||
)
|
||||
pipe.add_component(
|
||||
"llm",
|
||||
OpenAIChatGenerator(
|
||||
api_key=Secret.from_token(llm_api_key),
|
||||
model="gpt-4-1106-preview",
|
||||
streaming_callback=print_streaming_chunk,
|
||||
),
|
||||
)
|
||||
|
||||
pipe.connect("spec_to_functions.functions", "a1.functions")
|
||||
pipe.connect("spec_to_functions.openapi_specs", "a2.specs")
|
||||
pipe.connect("a1", "functions_llm.generation_kwargs")
|
||||
pipe.connect("functions_llm.replies", "openapi_container.messages")
|
||||
pipe.connect("a2", "openapi_container.service_openapi_spec")
|
||||
pipe.connect("openapi_container.service_response", "a3.service_response")
|
||||
pipe.connect("a3", "llm.messages")
|
||||
|
||||
user_prompt = "Why was Sam Altman ousted from OpenAI?"
|
||||
|
||||
result = pipe.run(
|
||||
data={
|
||||
"functions_llm": {
|
||||
"messages": [
|
||||
ChatMessage.from_system("Only do function calling"),
|
||||
ChatMessage.from_user(user_prompt),
|
||||
],
|
||||
},
|
||||
"openapi_container": {"service_credentials": serper_dev_key},
|
||||
"spec_to_functions": {"sources": [ByteStream.from_string(serper_spec)]},
|
||||
"a3": {"system_message": [ChatMessage.from_system(system_prompt)]},
|
||||
},
|
||||
)
|
||||
```
|
||||
+134
@@ -0,0 +1,134 @@
|
||||
---
|
||||
title: "OutputAdapter"
|
||||
id: outputadapter
|
||||
slug: "/outputadapter"
|
||||
description: "This component helps the output of one component fit smoothly into the input of another. It uses Jinja expressions to define how this adaptation occurs."
|
||||
---
|
||||
|
||||
# OutputAdapter
|
||||
|
||||
This component helps the output of one component fit smoothly into the input of another. It uses Jinja expressions to define how this adaptation occurs.
|
||||
|
||||
<div className="key-value-table">
|
||||
|
||||
| | |
|
||||
| --- | --- |
|
||||
| **Most common position in a pipeline** | Flexible |
|
||||
| **Mandatory init variables** | `template`: A Jinja template string that defines how to adapt the data <br /> <br />`output_type`: Type alias that this instance will return |
|
||||
| **Mandatory run variables** | `**kwargs`: Input variables to be used in Jinja expression. See [Variables](#variables) section for more details. |
|
||||
| **Output variables** | The output is specified under the `output` key dictionary |
|
||||
| **API reference** | [Converters](/reference/converters-api) |
|
||||
| **GitHub link** | https://github.com/deepset-ai/haystack/blob/main/haystack/components/converters/output_adapter.py |
|
||||
|
||||
</div>
|
||||
|
||||
## Overview
|
||||
|
||||
To use `OutputAdapter`, you need to specify the adaptation rule that includes:
|
||||
|
||||
- `template`: A Jinja template string that defines how to adapt the input data.
|
||||
- `output_type`: The type of the output data (such as `str`, `List[int]`..). This doesn't change the actual output type and is only needed to validate connection with other components.
|
||||
- `custom_filters`: An optional dictionary of custom Jinja filters to be used in the template.
|
||||
|
||||
### Variables
|
||||
|
||||
The `OutputAdapter` requires all template variables to be present before running and raises an error if any template variable is missing at pipeline connect time.
|
||||
|
||||
```python
|
||||
from haystack.components.converters import OutputAdapter
|
||||
|
||||
adapter = OutputAdapter(template="Hello {{name}}!", output_type=str)
|
||||
```
|
||||
|
||||
### Unsafe behavior
|
||||
|
||||
The `OutputAdapter` internally renders the `template` using Jinja, and by default, this is safe behavior. However, it limits the output types to strings, bytes, numbers, tuples, lists, dicts, sets, booleans, `None`, and `Ellipsis` (`...`), as well as any combination of these structures.
|
||||
|
||||
If you want to use other types such as `ChatMessage`, `Document`, or `Answer`, you must enable unsafe template rendering by setting the `unsafe` init argument to `True`.
|
||||
|
||||
Be cautious, as enabling this can be unsafe and may lead to remote code execution if the `template` is a string customizable by the end user.
|
||||
|
||||
## Usage
|
||||
|
||||
### On its own
|
||||
|
||||
This component is primarily meant to be used in pipelines.
|
||||
|
||||
In this example, `OutputAdapter` simply outputs the content field of the first document in the arrays of documents:
|
||||
|
||||
```python
|
||||
from haystack import Document
|
||||
from haystack.components.converters import OutputAdapter
|
||||
|
||||
adapter = OutputAdapter(template="{{ documents[0].content }}", output_type=str)
|
||||
input_data = {"documents": [Document(content="Test content")]}
|
||||
expected_output = {"output": "Test content"}
|
||||
assert adapter.run(**input_data) == expected_output
|
||||
```
|
||||
|
||||
### In a pipeline
|
||||
|
||||
The example below demonstrates a straightforward pipeline that uses the `OutputAdapter` to capitalize the first document in the list. If needed, you can also utilize the predefined Jinja [filters](https://jinja.palletsprojects.com/en/3.1.x/templates/#builtin-filters).
|
||||
|
||||
```python
|
||||
from haystack import Pipeline, component, Document
|
||||
from haystack.components.converters import OutputAdapter
|
||||
|
||||
|
||||
@component
|
||||
class DocumentProducer:
|
||||
@component.output_types(documents=dict)
|
||||
def run(self):
|
||||
return {"documents": [Document(content="haystack")]}
|
||||
|
||||
|
||||
pipe = Pipeline()
|
||||
pipe.add_component(
|
||||
name="output_adapter",
|
||||
instance=OutputAdapter(
|
||||
template="{{ documents[0].content | capitalize}}",
|
||||
output_type=str,
|
||||
),
|
||||
)
|
||||
pipe.add_component(name="document_producer", instance=DocumentProducer())
|
||||
pipe.connect("document_producer", "output_adapter")
|
||||
result = pipe.run(data={})
|
||||
|
||||
assert result["output_adapter"]["output"] == "Haystack"
|
||||
```
|
||||
|
||||
You can also define your own custom filters, which can then be added to an `OutputAdapter` instance through its init method and used in templates. Here’s an example of this approach:
|
||||
|
||||
```python
|
||||
|
||||
from haystack import Pipeline, component, Document
|
||||
from haystack.components.converters import OutputAdapter
|
||||
|
||||
|
||||
def reverse_string(s):
|
||||
return s[::-1]
|
||||
|
||||
|
||||
@component
|
||||
class DocumentProducer:
|
||||
@component.output_types(documents=dict)
|
||||
def run(self):
|
||||
return {"documents": [Document(content="haystack")]}
|
||||
|
||||
|
||||
pipe = Pipeline()
|
||||
pipe.add_component(
|
||||
name="output_adapter",
|
||||
instance=OutputAdapter(
|
||||
template="{{ documents[0].content | reverse_string}}",
|
||||
output_type=str,
|
||||
custom_filters={"reverse_string": reverse_string},
|
||||
),
|
||||
)
|
||||
|
||||
pipe.add_component(name="document_producer", instance=DocumentProducer())
|
||||
pipe.connect("document_producer", "output_adapter")
|
||||
result = pipe.run(data={})
|
||||
|
||||
assert result["output_adapter"]["output"] == "kcatsyah"
|
||||
```
|
||||
+82
@@ -0,0 +1,82 @@
|
||||
---
|
||||
title: "PDFMinerToDocument"
|
||||
id: pdfminertodocument
|
||||
slug: "/pdfminertodocument"
|
||||
description: "A component that converts complex PDF files to documents using pdfminer arguments."
|
||||
---
|
||||
|
||||
# PDFMinerToDocument
|
||||
|
||||
A component that converts complex PDF files to documents using pdfminer arguments.
|
||||
|
||||
<div className="key-value-table">
|
||||
|
||||
| | |
|
||||
| :------------------------------------- | :--------------------------------------------------------------------------------------------- |
|
||||
| **Most common position in a pipeline** | Before [PreProcessors](../preprocessors.mdx) or right at the beginning of an indexing pipeline |
|
||||
| **Mandatory run variables** | `sources`: PDF file paths or [`ByteStream`](../../concepts/data-classes.mdx#bytestream) objects |
|
||||
| **Output variables** | `documents`: A list of documents |
|
||||
| **API reference** | [Converters](/reference/converters-api) |
|
||||
| **GitHub link** | https://github.com/deepset-ai/haystack/blob/main/haystack/components/converters/pdfminer.py |
|
||||
|
||||
</div>
|
||||
|
||||
## Overview
|
||||
|
||||
The `PDFMinerToDocument` component converts PDF files into documents using [PDFMiner](https://pdfminersix.readthedocs.io/en/latest/) extraction tool arguments.
|
||||
|
||||
You can use it in an indexing pipeline to index the contents of a PDF file in a Document Store. It takes a list of file paths or [`ByteStream`](../../concepts/data-classes.mdx#bytestream)objects as input and outputs the converted result as a list of documents. Optionally, you can attach metadata to the documents through the `meta` input parameter.
|
||||
|
||||
When initializing the component, you can adjust several parameters to fit your PDF. See the full parameter list and descriptions in our [API reference](/reference/converters-api#pdfminertodocument).
|
||||
|
||||
## Usage
|
||||
|
||||
First, install `pdfminer` package to start using this converter:
|
||||
|
||||
```shell
|
||||
pip install pdfminer.six
|
||||
```
|
||||
|
||||
### On its own
|
||||
|
||||
```python
|
||||
from haystack.components.converters import PDFMinerToDocument
|
||||
|
||||
converter = PDFMinerToDocument()
|
||||
results = converter.run(
|
||||
sources=["sample.pdf"],
|
||||
meta={"date_added": datetime.now().isoformat()},
|
||||
)
|
||||
documents = results["documents"]
|
||||
|
||||
print(documents[0].content)
|
||||
|
||||
## 'This is a text from the PDF file.'
|
||||
```
|
||||
|
||||
### In a pipeline
|
||||
|
||||
```python
|
||||
from haystack import Pipeline
|
||||
from haystack.document_stores.in_memory import InMemoryDocumentStore
|
||||
from haystack.components.converters import PDFMinerToDocument
|
||||
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", PDFMinerToDocument())
|
||||
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}})
|
||||
```
|
||||
+116
@@ -0,0 +1,116 @@
|
||||
---
|
||||
title: "PDFToImageContent"
|
||||
id: pdftoimagecontent
|
||||
slug: "/pdftoimagecontent"
|
||||
description: "`PDFToImageContent` reads local PDF files and converts them into `ImageContent` objects. These are ready for multimodal AI pipelines, including tasks like image captioning, visual QA, or prompt-based generation."
|
||||
---
|
||||
|
||||
# PDFToImageContent
|
||||
|
||||
`PDFToImageContent` reads local PDF files and converts them into `ImageContent` objects. These are ready for multimodal AI pipelines, including tasks like image captioning, visual QA, or prompt-based generation.
|
||||
|
||||
<div className="key-value-table">
|
||||
|
||||
| | |
|
||||
| -------------------------------------- | ------------------------------------------------------------------------------------------------------- |
|
||||
| **Most common position in a pipeline** | Before a `ChatPromptBuilder` in a query pipeline |
|
||||
| **Mandatory run variables** | `sources`: A list of PDF file paths or ByteStreams |
|
||||
| **Output variables** | `image_contents`: A list of ImageContent objects |
|
||||
| **API reference** | [Image Converters](/reference/image-converters-api) |
|
||||
| **GitHub link** | https://github.com/deepset-ai/haystack/blob/main/haystack/components/converters/image/pdf_to_image.py |
|
||||
|
||||
</div>
|
||||
|
||||
## Overview
|
||||
|
||||
`PDFToImageContent` processes a list of PDF sources and converts them into `ImageContent` objects, one for each page of the PDF. These can be used in multimodal pipelines that require base64-encoded image input.
|
||||
|
||||
Each source can be:
|
||||
|
||||
- A file path (string or `Path`), or
|
||||
- A `ByteStream` object.
|
||||
|
||||
Optionally, you can provide metadata using the `meta` parameter. This can be a single dictionary (applied to all images) or a list matching the length of `sources`.
|
||||
|
||||
Use the `size` parameter to resize images while preserving aspect ratio. This reduces memory usage and transmission size, which is helpful when working with remote models or limited-resource environments.
|
||||
|
||||
This component is often used in query pipelines just before a `ChatPromptBuilder`.
|
||||
|
||||
## Usage
|
||||
|
||||
### On its own
|
||||
|
||||
```python
|
||||
from haystack.components.converters.image import PDFToImageContent
|
||||
|
||||
converter = PDFToImageContent()
|
||||
|
||||
sources = ["file.pdf", "another_file.pdf"]
|
||||
|
||||
image_contents = converter.run(sources=sources)["image_contents"]
|
||||
print(image_contents)
|
||||
|
||||
## [ImageContent(base64_image='...',
|
||||
## mime_type='application/pdf',
|
||||
## detail=None,
|
||||
## meta={'file_path': 'file.pdf', 'page_number': 1}),
|
||||
## ...]
|
||||
```
|
||||
|
||||
### In a pipeline
|
||||
|
||||
Use `ImageFileToImageContent` to supply image data to a `ChatPromptBuilder` for multimodal QA or captioning with an LLM.
|
||||
|
||||
```python
|
||||
from haystack import Pipeline
|
||||
from haystack.components.builders import ChatPromptBuilder
|
||||
from haystack.components.generators.chat import OpenAIChatGenerator
|
||||
from haystack.components.converters.image import PDFToImageContent
|
||||
|
||||
## Query pipeline
|
||||
pipeline = Pipeline()
|
||||
pipeline.add_component("image_converter", PDFToImageContent(detail="auto"))
|
||||
pipeline.add_component(
|
||||
"chat_prompt_builder",
|
||||
ChatPromptBuilder(
|
||||
required_variables=["question"],
|
||||
template="""{% message role="system" %}
|
||||
You are a helpful assistant that answers questions using the provided images.
|
||||
{% endmessage %}
|
||||
|
||||
{% message role="user" %}
|
||||
Question: {{ question }}
|
||||
|
||||
{% for img in image_contents %}
|
||||
{{ img | templatize_part }}
|
||||
{% endfor %}
|
||||
{% endmessage %}
|
||||
""",
|
||||
),
|
||||
)
|
||||
pipeline.add_component("llm", OpenAIChatGenerator(model="gpt-4o-mini"))
|
||||
|
||||
pipeline.connect("image_converter", "chat_prompt_builder.image_contents")
|
||||
pipeline.connect("chat_prompt_builder", "llm")
|
||||
|
||||
sources = ["flan_paper.pdf"]
|
||||
|
||||
result = pipeline.run(
|
||||
data={
|
||||
"image_converter": {"sources": ["flan_paper.pdf"], "page_range": "9"},
|
||||
"chat_prompt_builder": {"question": "What is the main takeaway of Figure 6?"},
|
||||
},
|
||||
)
|
||||
print(result["replies"][0].text)
|
||||
|
||||
## ('The main takeaway of Figure 6 is that Flan-PaLM demonstrates improved '
|
||||
## 'performance in zero-shot reasoning tasks when utilizing chain-of-thought '
|
||||
## '(CoT) reasoning, as indicated by higher accuracy across different model '
|
||||
## 'sizes compared to PaLM without finetuning. This highlights the importance of '
|
||||
## 'instruction finetuning combined with CoT for enhancing reasoning '
|
||||
## 'capabilities in models.')
|
||||
```
|
||||
|
||||
## Additional References
|
||||
|
||||
🧑🍳 Cookbook: [Introduction to Multimodality](https://haystack.deepset.ai/cookbook/multimodal_intro)
|
||||
+78
@@ -0,0 +1,78 @@
|
||||
---
|
||||
title: "PPTXToDocument"
|
||||
id: pptxtodocument
|
||||
slug: "/pptxtodocument"
|
||||
description: "Convert PPTX files to documents."
|
||||
---
|
||||
|
||||
# PPTXToDocument
|
||||
|
||||
Convert PPTX files to documents.
|
||||
|
||||
<div className="key-value-table">
|
||||
|
||||
| | |
|
||||
| :------------------------------------- | :--------------------------------------------------------------------------------------------- |
|
||||
| **Most common position in a pipeline** | Before [PreProcessors](../preprocessors.mdx) or right at the beginning of an indexing pipeline |
|
||||
| **Mandatory run variables** | `sources`: PPTX file paths or [`ByteStream`](../../concepts/data-classes.mdx#bytestream) objects |
|
||||
| **Output variables** | `documents`: A list of documents |
|
||||
| **API reference** | [Converters](/reference/converters-api) |
|
||||
| **GitHub link** | https://github.com/deepset-ai/haystack/blob/main/haystack/components/converters/pptx.py |
|
||||
|
||||
</div>
|
||||
|
||||
## Overview
|
||||
|
||||
The `PPTXToDocument` component converts PPTX files into documents. It takes a list of file paths or [`ByteStream`](../../concepts/data-classes.mdx#bytestream) objects as input and outputs the converted result as a list of documents. Optionally, you can attach metadata to the documents through the `meta` input parameter.
|
||||
|
||||
## Usage
|
||||
|
||||
First, install the`python-pptx` package to start using this converter:
|
||||
|
||||
```shell
|
||||
pip install python-pptx
|
||||
```
|
||||
|
||||
### On its own
|
||||
|
||||
```python
|
||||
from haystack.components.converters import PPTXToDocument
|
||||
|
||||
converter = PPTXToDocument()
|
||||
results = converter.run(
|
||||
sources=["sample.pptx"],
|
||||
meta={"date_added": datetime.now().isoformat()},
|
||||
)
|
||||
documents = results["documents"]
|
||||
|
||||
print(documents[0].content)
|
||||
|
||||
## 'This is the text from the PPTX file.'
|
||||
```
|
||||
|
||||
### In a pipeline
|
||||
|
||||
```python
|
||||
from haystack import Pipeline
|
||||
from haystack.document_stores.in_memory import InMemoryDocumentStore
|
||||
from haystack.components.converters import PPTXToDocument
|
||||
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", PPTXToDocument())
|
||||
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}})
|
||||
```
|
||||
+78
@@ -0,0 +1,78 @@
|
||||
---
|
||||
title: "PyPDFToDocument"
|
||||
id: pypdftodocument
|
||||
slug: "/pypdftodocument"
|
||||
description: "A component that converts PDF files to Documents."
|
||||
---
|
||||
|
||||
# PyPDFToDocument
|
||||
|
||||
A component that converts PDF files to Documents.
|
||||
|
||||
<div className="key-value-table">
|
||||
|
||||
| | |
|
||||
| :------------------------------------- | :---------------------------------------------------------------------------------------------- |
|
||||
| **Most common position in a pipeline** | Before [PreProcessors](../preprocessors.mdx) , or right at the beginning of an indexing pipeline |
|
||||
| **Mandatory run variables** | `sources`: PDF file paths or [`ByteStream`](../../concepts/data-classes.mdx#bytestream) objects |
|
||||
| **Output variables** | `documents`: A list of documents |
|
||||
| **API reference** | [Converters](/reference/converters-api) |
|
||||
| **GitHub link** | https://github.com/deepset-ai/haystack/blob/main/haystack/components/converters/pypdf.py |
|
||||
|
||||
</div>
|
||||
|
||||
## Overview
|
||||
|
||||
The `PyPDFToDocument` component converts PDF files into documents. You can use it in an indexing pipeline to index the contents of a PDF file into a Document Store. It takes a list of file paths or [ByteStream](../../concepts/data-classes.mdx#bytestream) objects as input and outputs the converted result as a list of documents. Optionally, you can attach metadata to the documents through the `meta` input parameter.
|
||||
|
||||
## Usage
|
||||
|
||||
You need to install `pypdf` package to use the `PyPDFToDocument` converter:
|
||||
|
||||
```shell
|
||||
pip install pypdf
|
||||
```
|
||||
|
||||
### On its own
|
||||
|
||||
```python
|
||||
from pathlib import Path
|
||||
from haystack.components.converters import PyPDFToDocument
|
||||
|
||||
converter = PyPDFToDocument()
|
||||
|
||||
docs = converter.run(sources=[Path("my_file.pdf")])
|
||||
```
|
||||
|
||||
### In a pipeline
|
||||
|
||||
```python
|
||||
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}})
|
||||
```
|
||||
|
||||
## Additional References
|
||||
|
||||
🧑🍳 Cookbook: [PDF-Based Question Answering with Amazon Bedrock and Haystack](https://haystack.deepset.ai/cookbook/amazon_bedrock_for_documentation_qa)
|
||||
|
||||
📓 Tutorial: [Preprocessing Different File Types](https://haystack.deepset.ai/tutorials/30_file_type_preprocessing_index_pipeline)
|
||||
+72
@@ -0,0 +1,72 @@
|
||||
---
|
||||
title: "TextFileToDocument"
|
||||
id: textfiletodocument
|
||||
slug: "/textfiletodocument"
|
||||
description: "Converts text files to documents."
|
||||
---
|
||||
|
||||
# TextFileToDocument
|
||||
|
||||
Converts text files to documents.
|
||||
|
||||
<div className="key-value-table">
|
||||
|
||||
| | |
|
||||
| :------------------------------------- | :--------------------------------------------------------------------------------------------- |
|
||||
| **Most common position in a pipeline** | Before [PreProcessors](../preprocessors.mdx) or right at the beginning of an indexing pipeline |
|
||||
| **Mandatory run variables** | `sources`: A list of paths to text files you want to convert |
|
||||
| **Output variables** | `documents`: A list of documents |
|
||||
| **API reference** | [Converters](/reference/converters-api) |
|
||||
| **GitHub link** | https://github.com/deepset-ai/haystack/blob/main/haystack/components/converters/txt.py |
|
||||
|
||||
</div>
|
||||
|
||||
## Overview
|
||||
|
||||
The `TextFileToDocument` component converts text files into documents. You can use it in an indexing pipeline to index the contents of text files into a Document Store. It takes a list of file paths or [ByteStream](../../concepts/data-classes.mdx#bytestream) objects as input and outputs the converted result as a list of documents. Optionally, you can attach metadata to the documents through the `meta` input parameter.
|
||||
|
||||
When you initialize the component, you can optionally set the default encoding of the text files through the `encoding` parameter. If you don't provide any value, the component uses `"utf-8"` by default. Note that if the encoding is specified in the metadata of an input ByteStream, it will override this parameter's setting.
|
||||
|
||||
## Usage
|
||||
|
||||
### On its own
|
||||
|
||||
```python
|
||||
from pathlib import Path
|
||||
from haystack.components.converters import TextFileToDocument
|
||||
|
||||
converter = TextFileToDocument()
|
||||
|
||||
docs = converter.run(sources=[Path("my_file.txt")])
|
||||
```
|
||||
|
||||
### In a pipeline
|
||||
|
||||
```python
|
||||
from haystack import Pipeline
|
||||
from haystack.document_stores.in_memory import InMemoryDocumentStore
|
||||
from haystack.components.converters import TextFileToDocument
|
||||
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", TextFileToDocument())
|
||||
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}})
|
||||
```
|
||||
|
||||
## Additional References
|
||||
|
||||
:notebook: Tutorial: [Preprocessing Different File Types](https://haystack.deepset.ai/tutorials/30_file_type_preprocessing_index_pipeline)
|
||||
+79
@@ -0,0 +1,79 @@
|
||||
---
|
||||
title: "TikaDocumentConverter"
|
||||
id: tikadocumentconverter
|
||||
slug: "/tikadocumentconverter"
|
||||
description: "An integration for converting files of different types (PDF, DOCX, HTML, and more) to documents."
|
||||
---
|
||||
|
||||
# TikaDocumentConverter
|
||||
|
||||
An integration for converting files of different types (PDF, DOCX, HTML, and more) to documents.
|
||||
|
||||
<div className="key-value-table">
|
||||
|
||||
| | |
|
||||
| :------------------------------------- | :---------------------------------------------------------------------------------------------- |
|
||||
| **Most common position in a pipeline** | Before [PreProcessors](../preprocessors.mdx) , or right at the beginning of an indexing pipeline |
|
||||
| **Mandatory run variables** | `sources`: File paths |
|
||||
| **Output variables** | `documents`: A list of documents |
|
||||
| **API reference** | [Converters](/reference/converters-api) |
|
||||
| **GitHub link** | https://github.com/deepset-ai/haystack/blob/main/haystack/components/converters/tika.py |
|
||||
|
||||
</div>
|
||||
|
||||
## Overview
|
||||
|
||||
The `TikaDocumentConverter` component converts files of different types (pdf, docx, html, and others) into documents. You can use it in an indexing pipeline to index the contents of files into a Document Store. It takes a list of file paths or [`ByteStream`](../../concepts/data-classes.mdx#bytestream) objects as input and outputs the converted result as a list of documents. Optionally, you can attach metadata to the documents through the `meta` input parameter.
|
||||
|
||||
This integration uses [Apache Tika](https://tika.apache.org/) to parse the files and requires a running Tika server.
|
||||
|
||||
The easiest way to run Tika is by using Docker: `docker run -d -p 127.0.0.1:9998:9998 apache/tika:latest`.
|
||||
For more options on running Tika on Docker, see the [Tika documentation](https://github.com/apache/tika-docker/blob/main/README.md#usage).
|
||||
|
||||
When you initialize the `TikaDocumentConverter` component, you can specify a custom URL of the Tika server you are using through the parameter `tika_url`. The default URL is `"http://localhost:9998/tika"`.
|
||||
|
||||
## Usage
|
||||
|
||||
You need to install `tika` package to use the `TikaDocumentConverter` component:
|
||||
|
||||
```shell
|
||||
pip install tika
|
||||
```
|
||||
|
||||
### On its own
|
||||
|
||||
```python
|
||||
from haystack.components.converters import TikaDocumentConverter
|
||||
from pathlib import Path
|
||||
|
||||
converter = TikaDocumentConverter()
|
||||
|
||||
converter.run(sources=[Path("my_file.pdf")])
|
||||
```
|
||||
|
||||
### In a pipeline
|
||||
|
||||
```python
|
||||
from haystack import Pipeline
|
||||
from haystack.document_stores.in_memory import InMemoryDocumentStore
|
||||
from haystack.components.converters import TikaDocumentConverter
|
||||
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", TikaDocumentConverter())
|
||||
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_paths}})
|
||||
```
|
||||
+118
@@ -0,0 +1,118 @@
|
||||
---
|
||||
title: "UnstructuredFileConverter"
|
||||
id: unstructuredfileconverter
|
||||
slug: "/unstructuredfileconverter"
|
||||
description: "Use this component to convert text files and directories to a document."
|
||||
---
|
||||
|
||||
# UnstructuredFileConverter
|
||||
|
||||
Use this component to convert text files and directories to a document.
|
||||
|
||||
<div className="key-value-table">
|
||||
|
||||
| | |
|
||||
| :------------------------------------- | :--------------------------------------------------------------------------------------------- |
|
||||
| **Most common position in a pipeline** | Before [PreProcessors](../preprocessors.mdx) or right at the beginning of an indexing pipeline |
|
||||
| **Mandatory run variables** | `paths`: A union of lists of paths |
|
||||
| **Output variables** | `documents`: A list of documents |
|
||||
| **API reference** | [Unstructured](/reference/integrations-unstructured) |
|
||||
| **GitHub link** | https://github.com/deepset-ai/haystack-core-integrations/tree/main/integrations/unstructured |
|
||||
|
||||
</div>
|
||||
|
||||
## Overview
|
||||
|
||||
`UnstructuredFileConverter` converts files and directories into documents using the Unstructured API.
|
||||
|
||||
[Unstructured](https://docs.unstructured.io/) provides a series of tools to do ETL for LLMs. The `UnstructuredFileConverter` calls the Unstructured API that extracts text and other information from a vast range of file [formats](https://docs.unstructured.io/api-reference/api-services/overview#supported-file-types).
|
||||
|
||||
This Converter supports different modes for creating documents from the elements returned by Unstructured:
|
||||
|
||||
- `"one-doc-per-file"`: One Haystack document per file. All elements are concatenated into one text field.
|
||||
- `"one-doc-per-page"`: One Haystack document per page. All elements on a page are concatenated into one text field.
|
||||
- `"one-doc-per-element"`: One Haystack document per element. Each element is converted to a Haystack document.
|
||||
|
||||
## Usage
|
||||
|
||||
Install the Unstructured integration to use `UnstructuredFileConverter`component:
|
||||
|
||||
```shell
|
||||
pip install unstructured-fileconverter-haystack
|
||||
```
|
||||
|
||||
There are free and paid versions of Unstructured API: **Free Unstructured API** and **Unstructured Serverless API**.
|
||||
|
||||
1. **Free Unstructured API**:
|
||||
- API URL: `https://api.unstructured.io/general/v0/general`
|
||||
- This version is free, but comes with certain limitations.
|
||||
|
||||
2. **Unstructured Serverless API**:
|
||||
- You'll find your unique API URL in your Unstructured account after signing up for the paid version.
|
||||
- This is a full-tier paid version of Unstructured.
|
||||
|
||||
For more details about the two tiers refer to Unstructured [FAQ](https://docs.unstructured.io/faq/faq).
|
||||
|
||||
:::note
|
||||
❗The API keys for the free and paid versions are different and cannot be used interchangeably.
|
||||
|
||||
:::
|
||||
|
||||
Regardless of the chosen tier, we recommend to set the Unstructured API key as an environment variable `UNSTRUCTURED_API_KEY`:
|
||||
|
||||
```shell
|
||||
export UNSTRUCTURED_API_KEY=your_api_key
|
||||
```
|
||||
|
||||
### On its own
|
||||
|
||||
```python
|
||||
import os
|
||||
from haystack_integrations.components.converters.unstructured import (
|
||||
UnstructuredFileConverter,
|
||||
)
|
||||
|
||||
converter = UnstructuredFileConverter()
|
||||
documents = converter.run(paths=["a/file/path.pdf", "a/directory/path"])["documents"]
|
||||
```
|
||||
|
||||
### In a pipeline
|
||||
|
||||
```python
|
||||
import os
|
||||
from haystack import Pipeline
|
||||
from haystack.components.writers import DocumentWriter
|
||||
from haystack.document_stores.in_memory import InMemoryDocumentStore
|
||||
from haystack_integrations.components.converters.unstructured import (
|
||||
UnstructuredFileConverter,
|
||||
)
|
||||
|
||||
document_store = InMemoryDocumentStore()
|
||||
|
||||
indexing = Pipeline()
|
||||
indexing.add_component("converter", UnstructuredFileConverter())
|
||||
indexing.add_component("writer", DocumentWriter(document_store))
|
||||
indexing.connect("converter", "writer")
|
||||
|
||||
indexing.run({"converter": {"paths": ["a/file/path.pdf", "a/directory/path"]}})
|
||||
```
|
||||
|
||||
### With Docker
|
||||
|
||||
To use `UnstructuredFileConverter` through Docker, first, set up an Unstructured Docker container:
|
||||
|
||||
```
|
||||
docker run -p 8000:8000 -d --rm --name unstructured-api quay.io/unstructured-io/unstructured-api:latest --port 8000 --host 0.0.0.0
|
||||
```
|
||||
|
||||
When initializing the component, specify the localhost URL:
|
||||
|
||||
```python
|
||||
from haystack_integrations.components.converters.unstructured import (
|
||||
UnstructuredFileConverter,
|
||||
)
|
||||
|
||||
converter = UnstructuredFileConverter(
|
||||
api_url="http://localhost:8000/general/v0/general",
|
||||
)
|
||||
```
|
||||
+79
@@ -0,0 +1,79 @@
|
||||
---
|
||||
title: "XLSXToDocument"
|
||||
id: xlsxtodocument
|
||||
slug: "/xlsxtodocument"
|
||||
description: "Converts Excel files into documents."
|
||||
---
|
||||
|
||||
# XLSXToDocument
|
||||
|
||||
Converts Excel files into documents.
|
||||
|
||||
<div className="key-value-table">
|
||||
|
||||
| | |
|
||||
| :------------------------------------- | :--------------------------------------------------------------------------------------------- |
|
||||
| **Most common position in a pipeline** | Before [PreProcessors](../preprocessors.mdx) or right at the beginning of an indexing pipeline |
|
||||
| **Mandatory run variables** | `sources`: File paths or [`ByteStream`](../../concepts/data-classes.mdx#bytestream) objects |
|
||||
| **Output variables** | `documents`: A list of documents |
|
||||
| **API reference** | [Converters](/reference/converters-api) |
|
||||
| **GitHub link** | https://github.com/deepset-ai/haystack/blob/main/haystack/components/converters/xlsx.py |
|
||||
|
||||
</div>
|
||||
|
||||
## Overview
|
||||
|
||||
The `XLSXToDocument` component converts XLSX files into Haystack Documents with a CSV (default) or Markdown format. It takes a list of file paths or [`ByteStream`](../../concepts/data-classes.mdx#bytestream) objects as input and outputs the converted result as a list of documents. Optionally, you can attach metadata to the documents through the `meta` input parameter.
|
||||
|
||||
To see the additional parameters that you can specify with the component initialization, check out the [API Reference](/reference/converters-api#xlsxtodocument).
|
||||
|
||||
## Usage
|
||||
|
||||
First, install the openpyxl and tabulate packages to start using this converter:
|
||||
|
||||
```shell
|
||||
pip install pandas openpyxl
|
||||
pip install tabulate
|
||||
```
|
||||
|
||||
### On its own
|
||||
|
||||
```python
|
||||
from haystack.components.converters import XLSXToDocument
|
||||
|
||||
converter = XLSXToDocument()
|
||||
results = converter.run(
|
||||
sources=["sample.xlsx"],
|
||||
meta={"date_added": datetime.now().isoformat()},
|
||||
)
|
||||
documents = results["documents"]
|
||||
print(documents[0].content)
|
||||
## ",A,B\n1,col_a,col_b\n2,1.5,test\n"
|
||||
```
|
||||
|
||||
### In a pipeline
|
||||
|
||||
```python
|
||||
from haystack import Pipeline
|
||||
from haystack.document_stores.in_memory import InMemoryDocumentStore
|
||||
from haystack.components.converters import XLSXToDocument
|
||||
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", XLSXToDocument())
|
||||
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}})
|
||||
```
|
||||
Reference in New Issue
Block a user