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:
@@ -0,0 +1,172 @@
|
||||
---
|
||||
title: "AmazonBedrockDocumentEmbedder"
|
||||
id: amazonbedrockdocumentembedder
|
||||
slug: "/amazonbedrockdocumentembedder"
|
||||
description: "This component computes embeddings for documents using models through Amazon Bedrock API."
|
||||
---
|
||||
|
||||
# AmazonBedrockDocumentEmbedder
|
||||
|
||||
This component computes embeddings for documents using models through Amazon Bedrock API.
|
||||
|
||||
<div className="key-value-table">
|
||||
|
||||
| | |
|
||||
| --- | --- |
|
||||
| **Most common position in a pipeline** | Before a [`DocumentWriter`](../writers/documentwriter.mdx) in an indexing pipeline |
|
||||
| **Mandatory init variables** | `model`: The embedding model to use <br /> <br />`aws_access_key_id`: AWS access key ID. Can be set with `AWS_ACCESS_KEY_ID` env var. <br /> <br />`aws_secret_access_key`: AWS secret access key. Can be set with `AWS_SECRET_ACCESS_KEY` env var. <br /> <br />`aws_region_name`: AWS region name. Can be set with `AWS_DEFAULT_REGION` env var. |
|
||||
| **Mandatory run variables** | `documents`: A list of documents to be embedded |
|
||||
| **Output variables** | `documents`: A list of documents (enriched with embeddings) |
|
||||
| **API reference** | [Amazon Bedrock](/reference/integrations-amazon-bedrock) |
|
||||
| **GitHub link** | https://github.com/deepset-ai/haystack-core-integrations/tree/main/integrations/amazon_bedrock |
|
||||
| **Package name** | `amazon-bedrock-haystack` |
|
||||
|
||||
</div>
|
||||
|
||||
## Overview
|
||||
|
||||
[Amazon Bedrock](https://docs.aws.amazon.com/bedrock/latest/userguide/what-is-bedrock.html) is a fully managed service that makes language models from leading AI startups and Amazon available for your use through a unified API.
|
||||
|
||||
Supported models are `amazon.titan-embed-text-v1`, `cohere.embed-english-v3`, `cohere.embed-multilingual-v3`, and `amazon.titan-embed-text-v2:0`.
|
||||
|
||||
:::info[Batch Inference]
|
||||
|
||||
Note that only Cohere models support batch inference – computing embeddings for more documents with the same request.
|
||||
:::
|
||||
|
||||
This component should be used to embed a list of documents. To embed a string, you should use the [`AmazonBedrockTextEmbedder`](amazonbedrocktextembedder.mdx).
|
||||
|
||||
### Authentication
|
||||
|
||||
`AmazonBedrockDocumentEmbedder` uses AWS for authentication. You can either provide credentials as parameters directly to the component or use the AWS CLI and authenticate through your IAM. For more information on how to set up an IAM identity-based policy, see the [official documentation](https://docs.aws.amazon.com/bedrock/latest/userguide/security_iam_id-based-policy-examples.html).
|
||||
To initialize `AmazonBedrockDocumentEmbedder` and authenticate by providing credentials, provide the `model_name`, as well as `aws_access_key_id`, `aws_secret_access_key` and `aws_region_name`. Other parameters are optional. You can check them out in our [API reference](/reference/integrations-amazon-bedrock#amazonbedrockdocumentembedder).
|
||||
|
||||
### Model-specific parameters
|
||||
|
||||
Even if Haystack provides a unified interface, each model offered by Bedrock can accept specific parameters. You can pass these parameters at initialization.
|
||||
|
||||
For example, Cohere models support `input_type` and `truncate`, as seen in [Bedrock documentation](https://docs.aws.amazon.com/bedrock/latest/userguide/model-parameters.html).
|
||||
|
||||
```python
|
||||
from haystack_integrations.components.embedders.amazon_bedrock import (
|
||||
AmazonBedrockDocumentEmbedder,
|
||||
)
|
||||
|
||||
embedder = AmazonBedrockDocumentEmbedder(
|
||||
model="cohere.embed-english-v3",
|
||||
input_type="search_document",
|
||||
truncate="LEFT",
|
||||
)
|
||||
```
|
||||
|
||||
### Embedding Metadata
|
||||
|
||||
Text documents often come with a set of metadata. If they are distinctive and semantically meaningful, you can embed them along with the text of the document to improve retrieval.
|
||||
|
||||
You can do this easily by using the Document Embedder:
|
||||
|
||||
```python
|
||||
from haystack import Document
|
||||
from haystack_integrations.components.embedders.amazon_bedrock import (
|
||||
AmazonBedrockDocumentEmbedder,
|
||||
)
|
||||
|
||||
doc = Document(content="some text", meta={"title": "relevant title", "page number": 18})
|
||||
|
||||
embedder = AmazonBedrockDocumentEmbedder(
|
||||
model="cohere.embed-english-v3",
|
||||
meta_fields_to_embed=["title"],
|
||||
)
|
||||
|
||||
docs_w_embeddings = embedder.run(documents=[doc])["documents"]
|
||||
```
|
||||
|
||||
## Usage
|
||||
|
||||
### Installation
|
||||
|
||||
You need to install `amazon-bedrock-haystack` package to use the `AmazonBedrockTextEmbedder`:
|
||||
|
||||
```shell
|
||||
pip install amazon-bedrock-haystack
|
||||
```
|
||||
|
||||
### On its own
|
||||
|
||||
Basic usage:
|
||||
|
||||
```python
|
||||
import os
|
||||
from haystack_integrations.components.embedders.amazon_bedrock import AmazonBedrockDocumentEmbedder
|
||||
from haystack.dataclasses import DOcument
|
||||
|
||||
os.environ["AWS_ACCESS_KEY_ID"] = "..."
|
||||
os.environ["AWS_SECRET_ACCESS_KEY"] = "..."
|
||||
os.environ["AWS_DEFAULT_REGION"] = "us-east-1" # just an example
|
||||
|
||||
doc = Document(content="I love pizza!")
|
||||
|
||||
embedder = AmazonBedrockDocumentEmbedder(model="cohere.embed-english-v3",
|
||||
input_type="search_document"
|
||||
|
||||
result = document_embedder.run([doc])
|
||||
print(result['documents'][0].embedding)
|
||||
|
||||
# [0.017020374536514282, -0.023255806416273117, ...]
|
||||
```
|
||||
|
||||
### In a pipeline
|
||||
|
||||
In a RAG pipeline:
|
||||
|
||||
```python
|
||||
from haystack import Document
|
||||
from haystack import Pipeline
|
||||
from haystack.document_stores.in_memory import InMemoryDocumentStore
|
||||
from haystack_integrations.components.embedders.amazon_bedrock import (
|
||||
AmazonBedrockDocumentEmbedder,
|
||||
AmazonBedrockTextEmbedder,
|
||||
)
|
||||
from haystack.components.retrievers.in_memory import InMemoryEmbeddingRetriever
|
||||
|
||||
document_store = InMemoryDocumentStore(embedding_similarity_function="cosine")
|
||||
|
||||
documents = [
|
||||
Document(content="My name is Wolfgang and I live in Berlin"),
|
||||
Document(content="I saw a black horse running"),
|
||||
Document(content="Germany has many big cities"),
|
||||
]
|
||||
|
||||
indexing_pipeline = Pipeline()
|
||||
indexing_pipeline.add_component(
|
||||
"embedder",
|
||||
AmazonBedrockDocumentEmbedder(model="cohere.embed-english-v3"),
|
||||
)
|
||||
indexing_pipeline.add_component("writer", DocumentWriter(document_store=document_store))
|
||||
indexing_pipeline.connect("embedder", "writer")
|
||||
|
||||
indexing_pipeline.run({"embedder": {"documents": documents}})
|
||||
|
||||
query_pipeline = Pipeline()
|
||||
query_pipeline.add_component(
|
||||
"text_embedder",
|
||||
AmazonBedrockTextEmbedder(model="cohere.embed-english-v3"),
|
||||
)
|
||||
query_pipeline.add_component(
|
||||
"retriever",
|
||||
InMemoryEmbeddingRetriever(document_store=document_store),
|
||||
)
|
||||
query_pipeline.connect("text_embedder.embedding", "retriever.query_embedding")
|
||||
|
||||
query = "Who lives in Berlin?"
|
||||
|
||||
result = query_pipeline.run({"text_embedder": {"text": query}})
|
||||
|
||||
print(result["retriever"]["documents"][0])
|
||||
|
||||
# Document(id=..., content: 'My name is Wolfgang and I live in Berlin')
|
||||
```
|
||||
|
||||
## Additional References
|
||||
|
||||
🧑🍳 Cookbook: [PDF-Based Question Answering with Amazon Bedrock and Haystack](https://haystack.deepset.ai/cookbook/amazon_bedrock_for_documentation_qa)
|
||||
+165
@@ -0,0 +1,165 @@
|
||||
---
|
||||
title: "AmazonBedrockDocumentImageEmbedder"
|
||||
id: amazonbedrockdocumentimageembedder
|
||||
slug: "/amazonbedrockdocumentimageembedder"
|
||||
description: "`AmazonBedrockDocumentImageEmbedder` computes image embeddings for documents using models exposed through the Amazon Bedrock API. It stores the obtained vectors in the embedding field of each document."
|
||||
---
|
||||
|
||||
# AmazonBedrockDocumentImageEmbedder
|
||||
|
||||
`AmazonBedrockDocumentImageEmbedder` computes image embeddings for documents using models exposed through the Amazon Bedrock API. It stores the obtained vectors in the embedding field of each document.
|
||||
|
||||
<div className="key-value-table">
|
||||
|
||||
| | |
|
||||
| --- | --- |
|
||||
| **Most common position in a pipeline** | Before a [`DocumentWriter`](../writers/documentwriter.mdx) in an indexing pipeline |
|
||||
| **Mandatory init variables** | `model`: The multimodal embedding model to use. <br /> <br />`aws_access_key_id`: AWS access key ID. Can be set with `AWS_ACCESS_KEY_ID` env var. <br /> <br />`aws_secret_access_key`: AWS secret access key. Can be set with `AWS_SECRET_ACCESS_KEY` env var. <br /> <br />`aws_region_name`: AWS region name. Can be set with `AWS_DEFAULT_REGION` env var. |
|
||||
| **Mandatory run variables** | `documents`: A list of documents, with a meta field containing an image file path |
|
||||
| **Output variables** | `documents`: A list of documents (enriched with embeddings) |
|
||||
| **API reference** | [Amazon Bedrock](/reference/integrations-amazon-bedrock) |
|
||||
| **GitHub link** | https://github.com/deepset-ai/haystack-core-integrations/tree/main/integrations/amazon_bedrock |
|
||||
| **Package name** | `amazon-bedrock-haystack` |
|
||||
|
||||
</div>
|
||||
|
||||
## Overview
|
||||
|
||||
Amazon Bedrock is a fully managed service that provides access to foundation models through a unified API.
|
||||
|
||||
`AmazonBedrockDocumentImageEmbedder` expects a list of documents containing an image or a PDF file path in a meta field. The meta field can be specified with the `file_path_meta_field` init parameter of this component.
|
||||
|
||||
The embedder efficiently loads the images, computes the embeddings using selected Bedrock model, and stores each of them in the `embedding` field of the document.
|
||||
|
||||
Supported models are `amazon.titan-embed-image-v1`, `cohere.embed-english-v3` , and `cohere.embed-multilingual-v3`.
|
||||
|
||||
`AmazonBedrockDocumentImageEmbedder` is commonly used in indexing pipelines. At retrieval time, you need to use the same model with `AmazonBedrockTextEmbedder` to embed the query, before using an Embedding Retriever.
|
||||
|
||||
### Installation
|
||||
|
||||
To start using this integration with Haystack, install the package with:
|
||||
|
||||
```shell
|
||||
pip install amazon-bedrock-haystack
|
||||
```
|
||||
|
||||
### Authentication
|
||||
|
||||
`AmazonBedrockDocumentImageEmbedder` uses AWS for authentication. You can either provide credentials as parameters directly to the component or use the AWS CLI and authenticate through your IAM. For more information on how to set up an IAM identity-based policy, see the [official documentation](https://docs.aws.amazon.com/bedrock/latest/userguide/security_iam_id-based-policy-examples.html).
|
||||
|
||||
To initialize `AmazonBedrockDocumentImageEmbedder` and authenticate by providing credentials, provide the `model` name, as well as `aws_access_key_id`, `aws_secret_access_key`, and `aws_region_name`. Other parameters are optional, you can check them out in our [API reference](/reference/integrations-amazon-bedrock#amazonbedrocktextembedder).
|
||||
|
||||
### Model-specific parameters
|
||||
|
||||
Even if Haystack provides a unified interface, each model offered by Bedrock can accept specific parameters. You can pass these parameters at initialization.
|
||||
|
||||
- **Amazon Titan**: Use `embeddingConfig` to control embedding behavior.
|
||||
- **Cohere v3**: Use `embedding_types` to select a single embedding type for images.
|
||||
|
||||
```python
|
||||
from haystack_integrations.components.embedders.amazon_bedrock import (
|
||||
AmazonBedrockDocumentImageEmbedder,
|
||||
)
|
||||
|
||||
embedder = AmazonBedrockDocumentImageEmbedder(
|
||||
model="cohere.embed-english-v3",
|
||||
embedding_types=["float"], # single value only
|
||||
)
|
||||
```
|
||||
|
||||
Note that only _one_ value in `embedding_types` is supported by this component. Passing multiple values raises an error.
|
||||
|
||||
## Usage
|
||||
|
||||
### On its own
|
||||
|
||||
```python
|
||||
import os
|
||||
from haystack import Document
|
||||
from haystack_integrations.components.embedders.amazon_bedrock import (
|
||||
AmazonBedrockDocumentImageEmbedder,
|
||||
)
|
||||
|
||||
os.environ["AWS_ACCESS_KEY_ID"] = "..."
|
||||
os.environ["AWS_SECRET_ACCESS_KEY"] = "..."
|
||||
os.environ["AWS_DEFAULT_REGION"] = "us-east-1" # example
|
||||
|
||||
# Point Documents to image/PDF files via metadata (default key: "file_path")
|
||||
documents = [
|
||||
Document(content="A photo of a cat", meta={"file_path": "cat.jpg"}),
|
||||
Document(
|
||||
content="Invoice page",
|
||||
meta={
|
||||
"file_path": "invoice.pdf",
|
||||
"mime_type": "application/pdf",
|
||||
"page_number": 1,
|
||||
},
|
||||
),
|
||||
]
|
||||
|
||||
embedder = AmazonBedrockDocumentImageEmbedder(
|
||||
model="amazon.titan-embed-image-v1",
|
||||
image_size=(1024, 1024), # optional downscaling
|
||||
)
|
||||
|
||||
result = embedder.run(documents=documents)
|
||||
embedded_docs = result["documents"]
|
||||
```
|
||||
|
||||
### In a pipeline
|
||||
|
||||
In this example, we can see an indexing pipeline with 3 components:
|
||||
|
||||
- `ImageFileToDocument` Converter that creates empty documents with a reference to an image in the `meta.file_path` field;
|
||||
- `AmazonBedrockDocumentImageEmbedder` that loads the images, computes embeddings and stores them in documents;
|
||||
- `DocumentWriter` that write the documents in the `InMemoryDocumentStore`.
|
||||
|
||||
There is also a multimodal retrieval pipeline, composed of an `AmazonBedrockTextEmbedder` (using the same model as before) and an `InMemoryEmbeddingRetriever`.
|
||||
|
||||
```python
|
||||
from haystack import Document, Pipeline
|
||||
from haystack.document_stores.in_memory import InMemoryDocumentStore
|
||||
from haystack.components.writers import DocumentWriter
|
||||
from haystack.components.retrievers.in_memory import InMemoryEmbeddingRetriever
|
||||
from haystack_integrations.components.embedders.amazon_bedrock import (
|
||||
AmazonBedrockDocumentImageEmbedder,
|
||||
AmazonBedrockTextEmbedder,
|
||||
)
|
||||
|
||||
# Document store using vector similarity for retrieval
|
||||
document_store = InMemoryDocumentStore(embedding_similarity_function="cosine")
|
||||
|
||||
# Sample corpus with file paths in metadata
|
||||
documents = [
|
||||
Document(content="A sketch of a horse", meta={"file_path": "horse.png"}),
|
||||
Document(content="A city map", meta={"file_path": "map.jpg"}),
|
||||
]
|
||||
|
||||
# Indexing pipeline: image embeddings -> write to store
|
||||
indexing = Pipeline()
|
||||
indexing.add_component(
|
||||
"image_embedder",
|
||||
AmazonBedrockDocumentImageEmbedder(model="cohere.embed-english-v3"),
|
||||
)
|
||||
indexing.add_component("writer", DocumentWriter(document_store=document_store))
|
||||
indexing.connect("image_embedder", "writer")
|
||||
indexing.run({"image_embedder": {"documents": documents}})
|
||||
|
||||
# Query pipeline: text -> embedding -> vector retriever
|
||||
query = Pipeline()
|
||||
query.add_component(
|
||||
"text_embedder",
|
||||
AmazonBedrockTextEmbedder(model="cohere.embed-english-v3"),
|
||||
)
|
||||
query.add_component(
|
||||
"retriever",
|
||||
InMemoryEmbeddingRetriever(document_store=document_store),
|
||||
)
|
||||
query.connect("text_embedder.embedding", "retriever.query_embedding")
|
||||
|
||||
res = query.run({"text_embedder": {"text": "Which document shows a horse?"}})
|
||||
```
|
||||
|
||||
## Additional References
|
||||
|
||||
:notebook: Tutorial: [Creating Vision+Text RAG Pipelines](https://haystack.deepset.ai/tutorials/46_multimodal_rag)
|
||||
@@ -0,0 +1,140 @@
|
||||
---
|
||||
title: "AmazonBedrockTextEmbedder"
|
||||
id: amazonbedrocktextembedder
|
||||
slug: "/amazonbedrocktextembedder"
|
||||
description: "This component computes embeddings for text (such as a query) using models through Amazon Bedrock API."
|
||||
---
|
||||
|
||||
# AmazonBedrockTextEmbedder
|
||||
|
||||
This component computes embeddings for text (such as a query) using models through Amazon Bedrock API.
|
||||
|
||||
<div className="key-value-table">
|
||||
|
||||
| | |
|
||||
| --- | --- |
|
||||
| **Most common position in a pipeline** | Before an embedding [Retriever](../retrievers.mdx) in a query/RAG pipeline |
|
||||
| **Mandatory init variables** | `model`: The embedding model to use <br /> <br />`aws_access_key_id`: AWS access key ID. Can be set with `AWS_ACCESS_KEY_ID` env var. <br /> <br />`aws_secret_access_key`: AWS secret access key. Can be set with `AWS_SECRET_ACCESS_KEY` env var. <br /> <br />`aws_region_name`: AWS region name. Can be set with `AWS_DEFAULT_REGION` env var. |
|
||||
| **Mandatory run variables** | `text`: A string |
|
||||
| **Output variables** | `embedding`: A list of float numbers (vector) |
|
||||
| **API reference** | [Amazon Bedrock](/reference/integrations-amazon-bedrock) |
|
||||
| **GitHub link** | https://github.com/deepset-ai/haystack-core-integrations/tree/main/integrations/amazon_bedrock |
|
||||
| **Package name** | `amazon-bedrock-haystack` |
|
||||
|
||||
</div>
|
||||
|
||||
## Overview
|
||||
|
||||
[Amazon Bedrock](https://docs.aws.amazon.com/bedrock/latest/userguide/what-is-bedrock.html) is a fully managed service that makes language models from leading AI startups and Amazon available for your use through a unified API.
|
||||
|
||||
Supported models are `amazon.titan-embed-text-v1`, `cohere.embed-english-v3` and `cohere.embed-multilingual-v3`.
|
||||
|
||||
Use `AmazonBedrockTextEmbedder` to embed a simple string (such as a query) into a vector. Use the [`AmazonBedrockDocumentEmbedder`](amazonbedrockdocumentembedder.mdx) to enrich the documents with the computed embedding, also known as vector.
|
||||
|
||||
### Authentication
|
||||
|
||||
`AmazonBedrockTextEmbedder` uses AWS for authentication. You can either provide credentials as parameters directly to the component or use the AWS CLI and authenticate through your IAM. For more information on how to set up an IAM identity-based policy, see the [official documentation](https://docs.aws.amazon.com/bedrock/latest/userguide/security_iam_id-based-policy-examples.html).
|
||||
To initialize `AmazonBedrockTextEmbedder` and authenticate by providing credentials, provide the `model` name, as well as `aws_access_key_id`, `aws_secret_access_key`, and `aws_region_name`. Other parameters are optional, you can check them out in our [API reference](/reference/integrations-amazon-bedrock#amazonbedrocktextembedder).
|
||||
|
||||
### Model-specific parameters
|
||||
|
||||
Even if Haystack provides a unified interface, each model offered by Bedrock can accept specific parameters. You can pass these parameters at initialization.
|
||||
|
||||
For example, the Cohere models support `input_type` and `truncate`, as seen in [Bedrock documentation](https://docs.aws.amazon.com/bedrock/latest/userguide/model-parameters.html).
|
||||
|
||||
```python
|
||||
from haystack_integrations.components.embedders.amazon_bedrock import (
|
||||
AmazonBedrockTextEmbedder,
|
||||
)
|
||||
|
||||
embedder = AmazonBedrockTextEmbedder(
|
||||
model="cohere.embed-english-v3",
|
||||
input_type="search_query",
|
||||
truncate="LEFT",
|
||||
)
|
||||
```
|
||||
|
||||
## Usage
|
||||
|
||||
### Installation
|
||||
|
||||
You need to install `amazon-bedrock-haystack` package to use the `AmazonBedrockTextEmbedder`:
|
||||
|
||||
```shell
|
||||
pip install amazon-bedrock-haystack
|
||||
```
|
||||
|
||||
### On its own
|
||||
|
||||
Basic usage:
|
||||
|
||||
```python
|
||||
import os
|
||||
from haystack_integrations.components.embedders.amazon_bedrock import (
|
||||
AmazonBedrockTextEmbedder,
|
||||
)
|
||||
|
||||
os.environ["AWS_ACCESS_KEY_ID"] = "..."
|
||||
os.environ["AWS_SECRET_ACCESS_KEY"] = "..."
|
||||
os.environ["AWS_DEFAULT_REGION"] = "us-east-1" # just an example
|
||||
|
||||
text_to_embed = "I love pizza!"
|
||||
|
||||
text_embedder = AmazonBedrockTextEmbedder(
|
||||
model="cohere.embed-english-v3",
|
||||
input_type="search_query",
|
||||
)
|
||||
|
||||
print(text_embedder.run(text_to_embed))
|
||||
# {'embedding': [-0.453125, 1.2236328, 2.0058594, 0.67871094...]}
|
||||
```
|
||||
|
||||
### In a pipeline
|
||||
|
||||
In a RAG pipeline:
|
||||
|
||||
```python
|
||||
from haystack import Document
|
||||
from haystack import Pipeline
|
||||
from haystack.document_stores.in_memory import InMemoryDocumentStore
|
||||
from haystack_integrations.components.embedders.amazon_bedrock import (
|
||||
AmazonBedrockDocumentEmbedder,
|
||||
AmazonBedrockTextEmbedder,
|
||||
)
|
||||
from haystack.components.retrievers.in_memory import InMemoryEmbeddingRetriever
|
||||
|
||||
document_store = InMemoryDocumentStore(embedding_similarity_function="cosine")
|
||||
|
||||
documents = [
|
||||
Document(content="My name is Wolfgang and I live in Berlin"),
|
||||
Document(content="I saw a black horse running"),
|
||||
Document(content="Germany has many big cities"),
|
||||
]
|
||||
|
||||
document_embedder = AmazonBedrockDocumentEmbedder(model="cohere.embed-english-v3")
|
||||
documents_with_embeddings = document_embedder.run(documents)["documents"]
|
||||
document_store.write_documents(documents_with_embeddings)
|
||||
|
||||
query_pipeline = Pipeline()
|
||||
query_pipeline.add_component(
|
||||
"text_embedder",
|
||||
AmazonBedrockTextEmbedder(model="cohere.embed-english-v3"),
|
||||
)
|
||||
query_pipeline.add_component(
|
||||
"retriever",
|
||||
InMemoryEmbeddingRetriever(document_store=document_store),
|
||||
)
|
||||
query_pipeline.connect("text_embedder.embedding", "retriever.query_embedding")
|
||||
|
||||
query = "Who lives in Berlin?"
|
||||
|
||||
result = query_pipeline.run({"text_embedder": {"text": query}})
|
||||
|
||||
print(result["retriever"]["documents"][0])
|
||||
|
||||
# Document(id=..., content: 'My name is Wolfgang and I live in Berlin')
|
||||
```
|
||||
|
||||
## Additional References
|
||||
|
||||
🧑🍳 Cookbook: [PDF-Based Question Answering with Amazon Bedrock and Haystack](https://haystack.deepset.ai/cookbook/amazon_bedrock_for_documentation_qa)
|
||||
@@ -0,0 +1,128 @@
|
||||
---
|
||||
title: "AzureOpenAIDocumentEmbedder"
|
||||
id: azureopenaidocumentembedder
|
||||
slug: "/azureopenaidocumentembedder"
|
||||
description: "This component computes the embeddings of a list of documents and stores the obtained vectors in the embedding field of each document. It uses Azure cognitive services for text and document embedding with models deployed on Azure."
|
||||
---
|
||||
|
||||
# AzureOpenAIDocumentEmbedder
|
||||
|
||||
This component computes the embeddings of a list of documents and stores the obtained vectors in the embedding field of each document. It uses Azure cognitive services for text and document embedding with models deployed on Azure.
|
||||
|
||||
<div className="key-value-table">
|
||||
|
||||
| | |
|
||||
| --- | --- |
|
||||
| **Most common position in a pipeline** | Before a [`DocumentWriter`](../writers/documentwriter.mdx) |
|
||||
| **Mandatory init variables** | `api_key`: The Azure OpenAI API key. Can be set with `AZURE_OPENAI_API_KEY` env var. <br />`azure_endpoint`: The endpoint of the model deployed on Azure. |
|
||||
| **Mandatory run variables** | `documents`: A list of documents |
|
||||
| **Output variables** | `documents`: A list of documents (enriched with embeddings) <br /> <br />`meta`: A dictionary of metadata |
|
||||
| **API reference** | [Embedders](/reference/embedders-api) |
|
||||
| **GitHub link** | https://github.com/deepset-ai/haystack/blob/main/haystack/components/embedders/azure_document_embedder.py |
|
||||
| **Package name** | `haystack-ai` |
|
||||
|
||||
</div>
|
||||
|
||||
## Overview
|
||||
|
||||
The vectors computed by this component are necessary to perform embedding retrieval on a collection of documents. At retrieval time, the vector representing the query is compared with those of the documents to find the most similar or relevant documents.
|
||||
|
||||
To see the list of compatible embedding models, head over to Azure [documentation](https://learn.microsoft.com/en-us/azure/ai-services/openai/concepts/models?source=recommendations). The default model for `AzureOpenAITextEmbedder` is `text-embedding-ada-002`.
|
||||
|
||||
This component should be used to embed a list of documents. To embed a string, you should use the [`AzureOpenAITextEmbedder`](azureopenaitextembedder.mdx).
|
||||
|
||||
To work with Azure components, you will need an Azure OpenAI API key, as well as an Azure OpenAI Endpoint. You can learn more about them in Azure [documentation](https://learn.microsoft.com/en-us/azure/ai-services/openai/reference).
|
||||
|
||||
The component uses `AZURE_OPENAI_API_KEY` or `AZURE_OPENAI_AD_TOKEN` environment variables by default. Otherwise, you can pass `api_key` or `azure_ad_token` at initialization:
|
||||
|
||||
```python
|
||||
client = AzureOpenAIDocumentEmbedder(
|
||||
azure_endpoint="<Your Azure endpoint e.g. `https://your-company.azure.openai.com/>",
|
||||
api_key=Secret.from_token("<your-api-key>"),
|
||||
azure_deployment="<a model name>",
|
||||
)
|
||||
```
|
||||
|
||||
:::info
|
||||
We recommend using environment variables instead of initialization parameters.
|
||||
:::
|
||||
|
||||
### Embedding Metadata
|
||||
|
||||
Text documents often come with a set of metadata. If they are distinctive and semantically meaningful, you can embed them along with the text of the document to improve retrieval.
|
||||
|
||||
You can do this easily by using the Document Embedder:
|
||||
|
||||
```python
|
||||
from haystack import Document
|
||||
from haystack.components.embedders import AzureOpenAIDocumentEmbedder
|
||||
|
||||
doc = Document(content="some text", meta={"title": "relevant title", "page number": 18})
|
||||
|
||||
embedder = AzureOpenAIDocumentEmbedder(meta_fields_to_embed=["title"])
|
||||
|
||||
docs_w_embeddings = embedder.run(documents=[doc])["documents"]
|
||||
```
|
||||
|
||||
## Usage
|
||||
|
||||
### On its own
|
||||
|
||||
```python
|
||||
from haystack import Document
|
||||
from haystack.components.embedders import AzureOpenAIDocumentEmbedder
|
||||
|
||||
doc = Document(content="I love pizza!")
|
||||
|
||||
document_embedder = AzureOpenAIDocumentEmbedder()
|
||||
|
||||
result = document_embedder.run([doc])
|
||||
print(result["documents"][0].embedding)
|
||||
|
||||
# [0.017020374536514282, -0.023255806416273117, ...]
|
||||
```
|
||||
|
||||
### In a pipeline
|
||||
|
||||
```python
|
||||
from haystack import Pipeline
|
||||
from haystack.document_stores.in_memory import InMemoryDocumentStore
|
||||
from haystack.components.embedders import (
|
||||
AzureOpenAITextEmbedder,
|
||||
AzureOpenAIDocumentEmbedder,
|
||||
)
|
||||
from haystack.components.writers import DocumentWriter
|
||||
from haystack.components.retrievers.in_memory import InMemoryEmbeddingRetriever
|
||||
|
||||
document_store = InMemoryDocumentStore(embedding_similarity_function="cosine")
|
||||
|
||||
documents = [
|
||||
Document(content="My name is Wolfgang and I live in Berlin"),
|
||||
Document(content="I saw a black horse running"),
|
||||
Document(content="Germany has many big cities"),
|
||||
]
|
||||
|
||||
indexing_pipeline = Pipeline()
|
||||
indexing_pipeline.add_component("embedder", AzureOpenAIDocumentEmbedder())
|
||||
indexing_pipeline.add_component("writer", DocumentWriter(document_store=document_store))
|
||||
indexing_pipeline.connect("embedder", "writer")
|
||||
|
||||
indexing_pipeline.run({"embedder": {"documents": documents}})
|
||||
|
||||
query_pipeline = Pipeline()
|
||||
query_pipeline.add_component("text_embedder", AzureOpenAITextEmbedder())
|
||||
query_pipeline.add_component(
|
||||
"retriever",
|
||||
InMemoryEmbeddingRetriever(document_store=document_store),
|
||||
)
|
||||
query_pipeline.connect("text_embedder.embedding", "retriever.query_embedding")
|
||||
|
||||
query = "Who lives in Berlin?"
|
||||
|
||||
result = query_pipeline.run({"text_embedder": {"text": query}})
|
||||
|
||||
print(result["retriever"]["documents"][0])
|
||||
|
||||
# Document(id=..., mimetype: 'text/plain',
|
||||
# text: 'My name is Wolfgang and I live in Berlin')
|
||||
```
|
||||
@@ -0,0 +1,110 @@
|
||||
---
|
||||
title: "AzureOpenAITextEmbedder"
|
||||
id: azureopenaitextembedder
|
||||
slug: "/azureopenaitextembedder"
|
||||
description: "When you perform embedding retrieval, you use this component to transform your query into a vector. Then, the embedding Retriever looks for similar or relevant documents."
|
||||
---
|
||||
|
||||
# AzureOpenAITextEmbedder
|
||||
|
||||
When you perform embedding retrieval, you use this component to transform your query into a vector. Then, the embedding Retriever looks for similar or relevant documents.
|
||||
|
||||
<div className="key-value-table">
|
||||
|
||||
| | |
|
||||
| --- | --- |
|
||||
| **Most common position in a pipeline** | Before an embedding [Retriever](../retrievers.mdx) in a query/RAG pipeline |
|
||||
| **Mandatory init variables** | `api_key`: The Azure OpenAI API key. Can be set with `AZURE_OPENAI_API_KEY` env var. <br />`azure_endpoint`: The endpoint of the model deployed on Azure. |
|
||||
| **Mandatory run variables** | `text`: A string |
|
||||
| **Output variables** | `embedding`: A list of float numbers <br /> <br />`meta`: A dictionary of metadata |
|
||||
| **API reference** | [Embedders](/reference/embedders-api) |
|
||||
| **GitHub link** | https://github.com/deepset-ai/haystack/blob/main/haystack/components/embedders/azure_text_embedder.py |
|
||||
| **Package name** | `haystack-ai` |
|
||||
|
||||
</div>
|
||||
|
||||
## Overview
|
||||
|
||||
`AzureOpenAITextEmbedder` transforms a string into a vector that captures its semantics using an OpenAI embedding model. It uses Azure cognitive services for text and document embedding with models deployed on Azure.
|
||||
|
||||
To see the list of compatible embedding models, head over to Azure [documentation](https://learn.microsoft.com/en-us/azure/ai-services/openai/concepts/models?source=recommendations). The default model for `AzureOpenAITextEmbedder` is `text-embedding-ada-002`.
|
||||
|
||||
Use `AzureOpenAITextEmbedder` to embed a simple string (such as a query) into a vector. For embedding lists of documents, use the [`AzureOpenAIDocumentEmbedder`](azureopenaidocumentembedder.mdx), which enriches the documents with the computed embedding, also known as vector.
|
||||
|
||||
To work with Azure components, you will need an Azure OpenAI API key, as well as an Azure OpenAI Endpoint. You can learn more about them in Azure [documentation](https://learn.microsoft.com/en-us/azure/ai-services/openai/reference).
|
||||
|
||||
The component uses `AZURE_OPENAI_API_KEY` or `AZURE_OPENAI_AD_TOKEN` environment variables by default. Otherwise, you can pass `api_key` or `azure_ad_token` at initialization:
|
||||
|
||||
```python
|
||||
client = AzureOpenAITextEmbedder(
|
||||
azure_endpoint="<Your Azure endpoint e.g. `https://your-company.azure.openai.com/>",
|
||||
api_key=Secret.from_token("<your-api-key>"),
|
||||
azure_deployment="<a model name>",
|
||||
)
|
||||
```
|
||||
|
||||
:::info
|
||||
We recommend using environment variables instead of initialization parameters.
|
||||
:::
|
||||
|
||||
## Usage
|
||||
|
||||
### On its own
|
||||
|
||||
Here is how you can use the component on its own:
|
||||
|
||||
```python
|
||||
from haystack.components.embedders import AzureOpenAITextEmbedder
|
||||
|
||||
text_to_embed = "I love pizza!"
|
||||
|
||||
text_embedder = AzureOpenAITextEmbedder()
|
||||
|
||||
print(text_embedder.run(text_to_embed))
|
||||
|
||||
# {'embedding': [0.017020374536514282, -0.023255806416273117, ...],
|
||||
# 'meta': {'model': 'text-embedding-ada-002-v2',
|
||||
# 'usage': {'prompt_tokens': 4, 'total_tokens': 4}}}
|
||||
```
|
||||
|
||||
### In a pipeline
|
||||
|
||||
```python
|
||||
from haystack import Document
|
||||
from haystack import Pipeline
|
||||
from haystack.document_stores.in_memory import InMemoryDocumentStore
|
||||
from haystack.components.embedders import (
|
||||
AzureOpenAITextEmbedder,
|
||||
AzureOpenAIDocumentEmbedder,
|
||||
)
|
||||
from haystack.components.retrievers.in_memory import InMemoryEmbeddingRetriever
|
||||
|
||||
document_store = InMemoryDocumentStore(embedding_similarity_function="cosine")
|
||||
|
||||
documents = [
|
||||
Document(content="My name is Wolfgang and I live in Berlin"),
|
||||
Document(content="I saw a black horse running"),
|
||||
Document(content="Germany has many big cities"),
|
||||
]
|
||||
|
||||
document_embedder = AzureOpenAIDocumentEmbedder()
|
||||
documents_with_embeddings = document_embedder.run(documents)["documents"]
|
||||
document_store.write_documents(documents_with_embeddings)
|
||||
|
||||
query_pipeline = Pipeline()
|
||||
query_pipeline.add_component("text_embedder", AzureOpenAITextEmbedder())
|
||||
query_pipeline.add_component(
|
||||
"retriever",
|
||||
InMemoryEmbeddingRetriever(document_store=document_store),
|
||||
)
|
||||
query_pipeline.connect("text_embedder.embedding", "retriever.query_embedding")
|
||||
|
||||
query = "Who lives in Berlin?"
|
||||
|
||||
result = query_pipeline.run({"text_embedder": {"text": query}})
|
||||
|
||||
print(result["retriever"]["documents"][0])
|
||||
|
||||
# Document(id=..., mimetype: 'text/plain',
|
||||
# text: 'My name is Wolfgang and I live in Berlin')
|
||||
```
|
||||
@@ -0,0 +1,61 @@
|
||||
---
|
||||
title: "Choosing the Right Embedder"
|
||||
id: choosing-the-right-embedder
|
||||
slug: "/choosing-the-right-embedder"
|
||||
description: "This page provides information on choosing the right Embedder when working with Haystack. It explains the distinction between Text and Document Embedders and discusses API-based Embedders and Embedders with models running on-premise."
|
||||
---
|
||||
|
||||
# Choosing the Right Embedder
|
||||
|
||||
This page provides information on choosing the right Embedder when working with Haystack. It explains the distinction between Text and Document Embedders and discusses API-based Embedders and Embedders with models running on-premise.
|
||||
|
||||
Embedders in Haystack transform texts or documents into vector representations using pre-trained models. The embeddings produced by Haystack Embedders are fixed-length vectors. They capture contextual information and semantic relationships within the text.
|
||||
|
||||
Embeddings in isolation are only used for information retrieval purposes (to do semantic search/vector search). You can use the embeddings in your pipeline for tasks like question answering. The QA pipeline with embedding retrieval would then include the following steps:
|
||||
|
||||
1. Transform the query into a vector/embedding.
|
||||
2. Find similar documents based on the embedding similarity.
|
||||
3. Pass the query and the retrieved documents to a Language Model, which can be extractive or generative.
|
||||
|
||||
## Text and Document Embedders
|
||||
|
||||
There are two types of Embedders: text and document.
|
||||
|
||||
Text Embedders work with text strings and are most often used at the beginning of query pipelines. They convert query text into vector embeddings and send them to a Retriever.
|
||||
|
||||
Document Embedders embed Document objects and are most often used in indexing pipelines, after Converters, and before a DocumentWriter. They preserve the Document object format and add an embedding field with a list of float numbers.
|
||||
|
||||
You must use the same embedding model for text and documents. This means that if you use CohereDocumentEmbedder in your indexing pipeline, you must then use CohereTextEmbedder with the same model in your query pipeline.
|
||||
|
||||
## API-Based Embedders
|
||||
|
||||
These Embedders use external APIs to generate embeddings. They give you access to powerful models without needing to handle the computing yourself.
|
||||
|
||||
The costs associated with these solutions can vary. Depending on the solution you choose, you pay for the tokens consumed, both sent and generated, or for the hosting of the model, often billed per hour. Refer to the individual providers’ websites for detailed information.
|
||||
|
||||
Haystack supports the models offered by a variety of providers: **OpenAI**, **Cohere**, **Jina**, **Azure**, **Mistral**, and **Amazon Bedrock**, with more being added constantly.
|
||||
|
||||
Additionally, you could use Haystack’s **Hugging Face API Embedders** for prototyping with [HF Serverless Inference API](https://huggingface.co/docs/api-inference/en/index) or the [paid HF Inference Endpoints](https://huggingface.co/inference-endpoints/dedicated).
|
||||
|
||||
## On-Premise Embedders
|
||||
|
||||
On-premise Embedders allow you to host open models on your machine/infrastructure. This choice is ideal for local experimentation.
|
||||
|
||||
When you self-host an embedder, you can choose the model from plenty of open model options. The [Massive Text Embedding Benchmark (MTEB) Leaderboard](https://huggingface.co/spaces/mteb/leaderboard) can be a good reference point for understanding retrieval performance and model size.
|
||||
|
||||
It is suitable in production scenarios where data privacy concerns drive the decision not to transmit data to external providers and you have ample computational resources (CPU or GPU).
|
||||
|
||||
Here are some options available in Haystack:
|
||||
|
||||
- **Sentence Transformers**: This library mostly uses PyTorch, so it can be a fast-running option if you’re using a GPU. On the other hand, Sentence Transformers are progressively adding support for more efficient backends, which do not require GPU.
|
||||
- **Hugging Face Text Embedding Inference**: This is a library for efficiently serving open embedding models on both CPU and GPU. In Haystack, it can be used via HuggingFace API Embedders.
|
||||
- **Hugging Face Optimum:** These Embedders are designed to run models faster on targeted hardware. They implement optimizations that are specific for a certain hardware, such as Intel IPEX.
|
||||
- **Fastembed**: Fastembed is optimized for running on standard machines even with low resources. It supports several types of embeddings, including sparse techniques (BM25, SPLADE) and classic dense embeddings.
|
||||
- **Ollama:** These Embedders run quantized models on CPU(+GPU). Embedding quality might be lower due to the quantization of regular models. However, this makes these models run efficiently on standard machines.
|
||||
- **Nvidia**: Nvidia Embedders are built on Nvidia's NIM and hosted on their optimized cloud platform. They give you both options: using models through their API or deploying models locally with Nvidia NIM.
|
||||
|
||||
***
|
||||
|
||||
:::info
|
||||
See the full list of Embedders available in Haystack on the main [Embedders](../embedders.mdx) page.
|
||||
:::
|
||||
@@ -0,0 +1,136 @@
|
||||
---
|
||||
title: "CohereDocumentEmbedder"
|
||||
id: coheredocumentembedder
|
||||
slug: "/coheredocumentembedder"
|
||||
description: "This component computes the embeddings of a list of documents and stores the obtained vectors in the embedding field of each document. It uses Cohere embedding models."
|
||||
---
|
||||
|
||||
# CohereDocumentEmbedder
|
||||
|
||||
This component computes the embeddings of a list of documents and stores the obtained vectors in the embedding field of each document. It uses Cohere embedding models.
|
||||
|
||||
The vectors computed by this component are necessary to perform embedding retrieval on a collection of documents. At retrieval time, the vector that represents the query is compared with those of the documents to find the most similar or relevant documents.
|
||||
|
||||
<div className="key-value-table">
|
||||
|
||||
| | |
|
||||
| --- | --- |
|
||||
| **Most common position in a pipeline** | Before a [`DocumentWriter`](../writers/documentwriter.mdx) in an indexing pipeline |
|
||||
| **Mandatory init variables** | `api_key`: The Cohere API key. Can be set with `COHERE_API_KEY` or `CO_API_KEY` env var. |
|
||||
| **Mandatory run variables** | `documents`: A list of documents to be embedded |
|
||||
| **Output variables** | `documents`: A list of documents (enriched with embeddings) <br /> <br />`meta`: A dictionary of metadata strings |
|
||||
| **API reference** | [Cohere](/reference/integrations-cohere) |
|
||||
| **GitHub link** | https://github.com/deepset-ai/haystack-core-integrations/tree/main/integrations/cohere |
|
||||
| **Package name** | `cohere-haystack` |
|
||||
|
||||
</div>
|
||||
|
||||
## Overview
|
||||
|
||||
`CohereDocumentEmbedder` enriches the metadata of documents with an embedding of their content. To embed a string, you should use the [`CohereTextEmbedder`](coheretextembedder.mdx).
|
||||
|
||||
The component supports the following Cohere models:
|
||||
`"embed-english-v3.0"`, `"embed-english-light-v3.0"`, `"embed-multilingual-v3.0"`,
|
||||
`"embed-multilingual-light-v3.0"`, `"embed-english-v2.0"`, `"embed-english-light-v2.0"`,
|
||||
`"embed-multilingual-v2.0"`. The default model is `embed-english-v2.0`. This list of all supported models can be found in Cohere’s [model documentation](https://docs.cohere.com/docs/models#representation).
|
||||
|
||||
To start using this integration with Haystack, install it with:
|
||||
|
||||
```shell
|
||||
pip install cohere-haystack
|
||||
```
|
||||
|
||||
The component uses a `COHERE_API_KEY` or `CO_API_KEY` environment variable by default. Otherwise, you can pass an API key at initialization with `api_key`:
|
||||
|
||||
```python
|
||||
embedder = CohereDocumentEmbedder(api_key=Secret.from_token("<your-api-key>"))
|
||||
```
|
||||
|
||||
To get a Cohere API key, head over to https://cohere.com/.
|
||||
|
||||
### Embedding Metadata
|
||||
|
||||
Text documents often come with a set of metadata. If they are distinctive and semantically meaningful, you can embed them along with the text of the document to improve retrieval.
|
||||
|
||||
You can do this by using the Document Embedder:
|
||||
|
||||
```python
|
||||
from haystack import Document
|
||||
from cohere_haystack.embedders.document_embedder import CohereDocumentEmbedder
|
||||
|
||||
doc = Document(content="some text", meta={"title": "relevant title", "page number": 18})
|
||||
|
||||
embedder = CohereDocumentEmbedder(api_key=Secret.from_token("<your-api-key>", meta_fields_to_embed=["title"])
|
||||
|
||||
docs_w_embeddings = embedder.run(documents=[doc])["documents"]
|
||||
```
|
||||
|
||||
## Usage
|
||||
|
||||
### On its own
|
||||
|
||||
Remember to set `COHERE_API_KEY` as an environment variable first, or pass it in directly.
|
||||
|
||||
Here is how you can use the component on its own:
|
||||
|
||||
```python
|
||||
from haystack import Document
|
||||
from haystack_integrations.components.embedders.cohere.document_embedder import (
|
||||
CohereDocumentEmbedder,
|
||||
)
|
||||
|
||||
doc = Document(content="I love pizza!")
|
||||
|
||||
embedder = CohereDocumentEmbedder()
|
||||
|
||||
result = embedder.run([doc])
|
||||
print(result["documents"][0].embedding)
|
||||
# [-0.453125, 1.2236328, 2.0058594, 0.67871094...]
|
||||
```
|
||||
|
||||
### In a pipeline
|
||||
|
||||
```python
|
||||
from haystack import Pipeline
|
||||
from haystack.document_stores.in_memory import InMemoryDocumentStore
|
||||
from haystack.components.writers import DocumentWriter
|
||||
from haystack.components.retrievers.in_memory import InMemoryEmbeddingRetriever
|
||||
|
||||
from haystack_integrations.components.embedders.cohere.document_embedder import (
|
||||
CohereDocumentEmbedder,
|
||||
)
|
||||
from haystack_integrations.components.embedders.cohere.text_embedder import (
|
||||
CohereTextEmbedder,
|
||||
)
|
||||
|
||||
document_store = InMemoryDocumentStore(embedding_similarity_function="cosine")
|
||||
|
||||
documents = [
|
||||
Document(content="My name is Wolfgang and I live in Berlin"),
|
||||
Document(content="I saw a black horse running"),
|
||||
Document(content="Germany has many big cities"),
|
||||
]
|
||||
|
||||
indexing_pipeline = Pipeline()
|
||||
indexing_pipeline.add_component("embedder", CohereDocumentEmbedder())
|
||||
indexing_pipeline.add_component("writer", DocumentWriter(document_store=document_store))
|
||||
indexing_pipeline.connect("embedder", "writer")
|
||||
|
||||
indexing_pipeline.run({"embedder": {"documents": documents}})
|
||||
|
||||
query_pipeline = Pipeline()
|
||||
query_pipeline.add_component("text_embedder", CohereTextEmbedder())
|
||||
query_pipeline.add_component(
|
||||
"retriever",
|
||||
InMemoryEmbeddingRetriever(document_store=document_store),
|
||||
)
|
||||
query_pipeline.connect("text_embedder.embedding", "retriever.query_embedding")
|
||||
|
||||
query = "Who lives in Berlin?"
|
||||
|
||||
result = query_pipeline.run({"text_embedder": {"text": query}})
|
||||
|
||||
print(result["retriever"]["documents"][0])
|
||||
|
||||
# Document(id=..., text: 'My name is Wolfgang and I live in Berlin')
|
||||
```
|
||||
@@ -0,0 +1,166 @@
|
||||
---
|
||||
title: "CohereDocumentImageEmbedder"
|
||||
id: coheredocumentimageembedder
|
||||
slug: "/coheredocumentimageembedder"
|
||||
description: "`CohereDocumentImageEmbedder` computes the image embeddings of a list of documents and stores the obtained vectors in the embedding field of each document. It uses Cohere embedding models with the ability to embed text and images into the same vector space."
|
||||
---
|
||||
|
||||
# CohereDocumentImageEmbedder
|
||||
|
||||
`CohereDocumentImageEmbedder` computes the image embeddings of a list of documents and stores the obtained vectors in the embedding field of each document. It uses Cohere embedding models with the ability to embed text and images into the same vector space.
|
||||
|
||||
<div className="key-value-table">
|
||||
|
||||
| | |
|
||||
| --- | --- |
|
||||
| **Most common position in a pipeline** | Before a [`DocumentWriter`](../writers/documentwriter.mdx) in an indexing pipeline |
|
||||
| **Mandatory init variables** | `api_key`: The Cohere API key. Can be set with `COHERE_API_KEY` or `CO_API_KEY` env var. |
|
||||
| **Mandatory run variables** | `documents`: A list of documents, with a meta field containing an image file path |
|
||||
| **Output variables** | `documents`: A list of documents (enriched with embeddings) |
|
||||
| **API reference** | [Cohere](/reference/integrations-cohere) |
|
||||
| **GitHub link** | https://github.com/deepset-ai/haystack-core-integrations/tree/main/integrations/cohere |
|
||||
| **Package name** | `cohere-haystack` |
|
||||
|
||||
</div>
|
||||
|
||||
## Overview
|
||||
|
||||
`CohereDocumentImageEmbedder` expects a list of documents containing an image or a PDF file path in a meta field. The meta field can be specified with the `file_path_meta_field` init parameter of this component.
|
||||
|
||||
The embedder efficiently loads the images, computes the embeddings using a Cohere model, and stores each of them in the `embedding` field of the document.
|
||||
|
||||
`CohereDocumentImageEmbedder` is commonly used in indexing pipelines. At retrieval time, you need to use the same model with a `CohereTextEmbedder` to embed the query, before using an Embedding Retriever.
|
||||
|
||||
This component is compatible with Cohere Embed models v3 and later. For a complete list of supported models, see the [Cohere documentation](https://docs.cohere.com/docs/models#embed).
|
||||
|
||||
### Installation
|
||||
|
||||
To start using this integration with Haystack, install the package with:
|
||||
|
||||
```shell
|
||||
pip install cohere-haystack
|
||||
```
|
||||
|
||||
### Authentication
|
||||
|
||||
The component uses a `COHERE_API_KEY` or `CO_API_KEY` environment variable by default. Otherwise, you can pass an API key at initialization with a [Secret](../../concepts/secret-management.mdx) and `Secret.from_token` method:
|
||||
|
||||
```python
|
||||
embedder = CohereTextEmbedder(api_key=Secret.from_token("<your-api-key>"))
|
||||
```
|
||||
|
||||
To get a Cohere API key, head over to https://cohere.com/.
|
||||
|
||||
## Usage
|
||||
|
||||
### On its own
|
||||
|
||||
Remember to set `COHERE_API_KEY` as an environment variable first.
|
||||
|
||||
```python
|
||||
from haystack import Document
|
||||
from haystack_integrations.components.embedders.cohere import (
|
||||
CohereDocumentImageEmbedder,
|
||||
)
|
||||
|
||||
embedder = CohereDocumentImageEmbedder(model="embed-v4.0")
|
||||
|
||||
documents = [
|
||||
Document(content="A photo of a cat", meta={"file_path": "cat.jpg"}),
|
||||
Document(content="A photo of a dog", meta={"file_path": "dog.jpg"}),
|
||||
]
|
||||
|
||||
result = embedder.run(documents=documents)
|
||||
documents_with_embeddings = result["documents"]
|
||||
print(documents_with_embeddings)
|
||||
|
||||
# [Document(id=...,
|
||||
# content='A photo of a cat',
|
||||
# meta={'file_path': 'cat.jpg',
|
||||
# 'embedding_source': {'type': 'image', 'file_path_meta_field': 'file_path'}},
|
||||
# embedding=vector of size 1536),
|
||||
# ...]
|
||||
```
|
||||
|
||||
### In a pipeline
|
||||
|
||||
In this example, we can see an indexing pipeline with three components:
|
||||
|
||||
- `ImageFileToDocument` converter that creates empty documents with a reference to an image in the `meta.file_path` field;
|
||||
- `CohereDocumentImageEmbedder` that loads the images, computes embeddings and store them in documents;
|
||||
- `DocumentWriter` that writes the documents in the `InMemoryDocumentStore`.
|
||||
|
||||
There is also a multimodal retrieval pipeline, composed of a `CohereTextEmbedder` (using the same model as before) and an `InMemoryEmbeddingRetriever`.
|
||||
|
||||
```python
|
||||
from haystack import Pipeline
|
||||
from haystack.components.converters.image import ImageFileToDocument
|
||||
from haystack.components.retrievers.in_memory import InMemoryEmbeddingRetriever
|
||||
from haystack.components.writers import DocumentWriter
|
||||
from haystack.document_stores.in_memory import InMemoryDocumentStore
|
||||
|
||||
from haystack_integrations.components.embedders.cohere import (
|
||||
CohereDocumentImageEmbedder,
|
||||
CohereTextEmbedder,
|
||||
)
|
||||
|
||||
document_store = InMemoryDocumentStore()
|
||||
|
||||
# Indexing pipeline
|
||||
indexing_pipeline = Pipeline()
|
||||
indexing_pipeline.add_component("image_converter", ImageFileToDocument())
|
||||
indexing_pipeline.add_component(
|
||||
"embedder",
|
||||
CohereDocumentImageEmbedder(model="embed-v4.0"),
|
||||
)
|
||||
indexing_pipeline.add_component("writer", DocumentWriter(document_store=document_store))
|
||||
indexing_pipeline.connect("image_converter", "embedder")
|
||||
indexing_pipeline.connect("embedder", "writer")
|
||||
|
||||
indexing_pipeline.run(data={"image_converter": {"sources": ["dog.jpg", "hyena.jpeg"]}})
|
||||
|
||||
# Multimodal retrieval pipeline
|
||||
retrieval_pipeline = Pipeline()
|
||||
retrieval_pipeline.add_component("embedder", CohereTextEmbedder(model="embed-v4.0"))
|
||||
retrieval_pipeline.add_component(
|
||||
"retriever",
|
||||
InMemoryEmbeddingRetriever(document_store=document_store, top_k=2),
|
||||
)
|
||||
retrieval_pipeline.connect("embedder.embedding", "retriever.query_embedding")
|
||||
|
||||
result = retrieval_pipeline.run(data={"text": "man's best friend"})
|
||||
print(result)
|
||||
|
||||
# {
|
||||
# 'retriever': {
|
||||
# 'documents': [
|
||||
# Document(
|
||||
# id=0c96...,
|
||||
# meta={
|
||||
# 'file_path': 'dog.jpg',
|
||||
# 'embedding_source': {
|
||||
# 'type': 'image',
|
||||
# 'file_path_meta_field': 'file_path'
|
||||
# }
|
||||
# },
|
||||
# score=0.288
|
||||
# ),
|
||||
# Document(
|
||||
# id=5e76...,
|
||||
# meta={
|
||||
# 'file_path': 'hyena.jpeg',
|
||||
# 'embedding_source': {
|
||||
# 'type': 'image',
|
||||
# 'file_path_meta_field': 'file_path'
|
||||
# }
|
||||
# },
|
||||
# score=0.248
|
||||
# )
|
||||
# ]
|
||||
# }
|
||||
# }
|
||||
```
|
||||
|
||||
## Additional References
|
||||
|
||||
:notebook: Tutorial: [Creating Vision+Text RAG Pipelines](https://haystack.deepset.ai/tutorials/46_multimodal_rag)
|
||||
@@ -0,0 +1,110 @@
|
||||
---
|
||||
title: "CohereTextEmbedder"
|
||||
id: coheretextembedder
|
||||
slug: "/coheretextembedder"
|
||||
description: "This component transforms a string into a vector that captures its semantics using a Cohere embedding model. When you perform embedding retrieval, you use this component to transform your query into a vector. Then, the embedding Retriever looks for similar or relevant documents."
|
||||
---
|
||||
|
||||
# CohereTextEmbedder
|
||||
|
||||
This component transforms a string into a vector that captures its semantics using a Cohere embedding model. When you perform embedding retrieval, you use this component to transform your query into a vector. Then, the embedding Retriever looks for similar or relevant documents.
|
||||
|
||||
<div className="key-value-table">
|
||||
|
||||
| | |
|
||||
| --- | --- |
|
||||
| **Most common position in a pipeline** | Before an embedding [Retriever](../retrievers.mdx) in a query/RAG pipeline |
|
||||
| **Mandatory init variables** | `api_key`: The Cohere API key. Can be set with `COHERE_API_KEY` or `CO_API_KEY` env var. |
|
||||
| **Mandatory run variables** | `text`: A string |
|
||||
| **Output variables** | `embedding`: A list of float numbers (vectors) <br /> <br />`meta`: A dictionary of metadata strings |
|
||||
| **API reference** | [Cohere](/reference/integrations-cohere) |
|
||||
| **GitHub link** | https://github.com/deepset-ai/haystack-core-integrations/tree/main/integrations/cohere |
|
||||
| **Package name** | `cohere-haystack` |
|
||||
|
||||
</div>
|
||||
|
||||
## Overview
|
||||
|
||||
`CohereTextEmbedder` embeds a simple string (such as a query) into a vector. For embedding lists of documents, use the use the [`CohereDocumentEmbedder`](coheredocumentembedder.mdx), which enriches the document with the computed embedding, also known as vector.
|
||||
|
||||
The component supports the following Cohere models:
|
||||
`"embed-english-v3.0"`, `"embed-english-light-v3.0"`, `"embed-multilingual-v3.0"`,
|
||||
`"embed-multilingual-light-v3.0"`, `"embed-english-v2.0"`, `"embed-english-light-v2.0"`,
|
||||
`"embed-multilingual-v2.0"`. The default model is `embed-english-v2.0`. This list of all supported models can be found in Cohere’s [model documentation](https://docs.cohere.com/docs/models#representation).
|
||||
|
||||
To start using this integration with Haystack, install it with:
|
||||
|
||||
```shell
|
||||
pip install cohere-haystack
|
||||
```
|
||||
|
||||
The component uses a `COHERE_API_KEY` or `CO_API_KEY` environment variable by default. Otherwise, you can pass an API key at initialization with a [Secret](../../concepts/secret-management.mdx) and `Secret.from_token` static method:
|
||||
|
||||
```python
|
||||
embedder = CohereTextEmbedder(api_key=Secret.from_token("<your-api-key>"))
|
||||
```
|
||||
|
||||
To get a Cohere API key, head over to https://cohere.com/.
|
||||
|
||||
## Usage
|
||||
|
||||
### On its own
|
||||
|
||||
Here is how you can use the component on its own. You’ll need to pass in your Cohere API key via Secret or set it as an environment variable called `COHERE_API_KEY`. The examples below assume you've set the environment variable.
|
||||
|
||||
```python
|
||||
from haystack_integrations.components.embedders.cohere.text_embedder import (
|
||||
CohereTextEmbedder,
|
||||
)
|
||||
|
||||
text_to_embed = "I love pizza!"
|
||||
|
||||
text_embedder = CohereTextEmbedder()
|
||||
|
||||
print(text_embedder.run(text_to_embed))
|
||||
# {'embedding': [-0.453125, 1.2236328, 2.0058594, 0.67871094...],
|
||||
# 'meta': {'api_version': {'version': '1'}, 'billed_units': {'input_tokens': 4}}}
|
||||
```
|
||||
|
||||
### In a pipeline
|
||||
|
||||
```python
|
||||
from haystack import Document
|
||||
from haystack import Pipeline
|
||||
from haystack.document_stores.in_memory import InMemoryDocumentStore
|
||||
from haystack_integrations.components.embedders.cohere.text_embedder import (
|
||||
CohereTextEmbedder,
|
||||
)
|
||||
from haystack_integrations.components.embedders.cohere.document_embedder import (
|
||||
CohereDocumentEmbedder,
|
||||
)
|
||||
from haystack.components.retrievers.in_memory import InMemoryEmbeddingRetriever
|
||||
|
||||
document_store = InMemoryDocumentStore(embedding_similarity_function="cosine")
|
||||
|
||||
documents = [
|
||||
Document(content="My name is Wolfgang and I live in Berlin"),
|
||||
Document(content="I saw a black horse running"),
|
||||
Document(content="Germany has many big cities"),
|
||||
]
|
||||
|
||||
document_embedder = CohereDocumentEmbedder()
|
||||
documents_with_embeddings = document_embedder.run(documents)["documents"]
|
||||
document_store.write_documents(documents_with_embeddings)
|
||||
|
||||
query_pipeline = Pipeline()
|
||||
query_pipeline.add_component("text_embedder", CohereTextEmbedder())
|
||||
query_pipeline.add_component(
|
||||
"retriever",
|
||||
InMemoryEmbeddingRetriever(document_store=document_store),
|
||||
)
|
||||
query_pipeline.connect("text_embedder.embedding", "retriever.query_embedding")
|
||||
|
||||
query = "Who lives in Berlin?"
|
||||
|
||||
result = query_pipeline.run({"text_embedder": {"text": query}})
|
||||
|
||||
print(result["retriever"]["documents"][0])
|
||||
|
||||
# Document(id=..., content: 'My name is Wolfgang and I live in Berlin')
|
||||
```
|
||||
@@ -0,0 +1,16 @@
|
||||
---
|
||||
title: "External Integrations"
|
||||
id: external-integrations-embedders
|
||||
slug: "/external-integrations-embedders"
|
||||
description: "External integrations that enable transforming texts or documents into vector representations using pre-trained models."
|
||||
---
|
||||
|
||||
# External Integrations
|
||||
|
||||
External integrations that enable transforming texts or documents into vector representations using pre-trained models.
|
||||
|
||||
| Name | Description |
|
||||
| --- | --- |
|
||||
| [mixedbread ai](https://haystack.deepset.ai/integrations/mixedbread-ai) | Compute embeddings for text and documents using mixedbread's API. |
|
||||
| [Isaacus](https://haystack.deepset.ai/integrations/isaacus) | Use the latest foundational legal AI models from Isaacus in Haystack. |
|
||||
| [Voyage AI](https://haystack.deepset.ai/integrations/voyage) | Computing embeddings for text and documents using Voyage AI embedding models. |
|
||||
@@ -0,0 +1,172 @@
|
||||
---
|
||||
title: "FastembedDocumentEmbedder"
|
||||
id: fastembeddocumentembedder
|
||||
slug: "/fastembeddocumentembedder"
|
||||
description: "This component computes the embeddings of a list of documents using the models supported by FastEmbed."
|
||||
---
|
||||
|
||||
# FastembedDocumentEmbedder
|
||||
|
||||
This component computes the embeddings of a list of documents using the models supported by FastEmbed.
|
||||
|
||||
<div className="key-value-table">
|
||||
|
||||
| | |
|
||||
| --- | --- |
|
||||
| **Most common position in a pipeline** | Before a [`DocumentWriter`](../writers/documentwriter.mdx) in an indexing pipeline |
|
||||
| **Mandatory run variables** | `documents`: A list of documents |
|
||||
| **Output variables** | `documents`: A list of documents (enriched with embeddings) |
|
||||
| **API reference** | [FastEmbed](/reference/fastembed-embedders) |
|
||||
| **GitHub link** | https://github.com/deepset-ai/haystack-core-integrations/tree/main/integrations/fastembed |
|
||||
| **Package name** | `fastembed-haystack` |
|
||||
|
||||
</div>
|
||||
|
||||
This component should be used to embed a list of documents. To embed a string, use the [`FastembedTextEmbedder`](fastembedtextembedder.mdx).
|
||||
|
||||
## Overview
|
||||
|
||||
`FastembedDocumentEmbedder` computes the embeddings of a list of documents and stores the obtained vectors in the embedding field of each document. It uses embedding [models supported by FastEmbed](https://qdrant.github.io/fastembed/examples/Supported_Models/).
|
||||
|
||||
The vectors computed by this component are necessary to perform embedding retrieval on a collection of documents. At retrieval time, the vector that represents the query is compared with those of the documents in order to find the most similar or relevant documents.
|
||||
|
||||
### Compatible models
|
||||
|
||||
You can find the original models in the [FastEmbed documentation](https://qdrant.github.io/fastembed/).
|
||||
|
||||
Nowadays, most of the models in the [Massive Text Embedding Benchmark (MTEB) Leaderboard](https://huggingface.co/spaces/mteb/leaderboard) are compatible with FastEmbed. You can look for compatibility in the [supported model list](https://qdrant.github.io/fastembed/examples/Supported_Models/).
|
||||
|
||||
### Installation
|
||||
|
||||
To start using this integration with Haystack, install the package with:
|
||||
|
||||
```shell
|
||||
pip install fastembed-haystack
|
||||
```
|
||||
|
||||
### Parameters
|
||||
|
||||
You can set the path where the model will be stored in a cache directory. Also, you can set the number of threads a single `onnxruntime` session can use.
|
||||
|
||||
```python
|
||||
cache_dir= "/your_cacheDirectory"
|
||||
embedder = FastembedDocumentEmbedder(
|
||||
*model="*BAAI/bge-large-en-v1.5",
|
||||
cache_dir=cache_dir,
|
||||
threads=2
|
||||
)
|
||||
```
|
||||
|
||||
If you want to use the data parallel encoding, you can set the parameters `parallel` and `batch_size`.
|
||||
|
||||
- If parallel > 1, data-parallel encoding will be used. This is recommended for offline encoding of large datasets.
|
||||
- If parallel is 0, use all available cores.
|
||||
- If None, don't use data-parallel processing; use default `onnxruntime` threading instead.
|
||||
|
||||
:::tip
|
||||
If you create a Text Embedder and a Document Embedder based on the same model, Haystack uses the same resource behind the scenes to save resources.
|
||||
:::
|
||||
|
||||
### Embedding Metadata
|
||||
|
||||
Text documents often come with a set of metadata. If they are distinctive and semantically meaningful, you can embed them along with the text of the document to improve retrieval.
|
||||
|
||||
You can do this easily by using the Document Embedder:
|
||||
|
||||
```python
|
||||
from haystack.preview import Document
|
||||
from haystack_integrations.components.embedders.fastembed import (
|
||||
FastembedDocumentEmbedder,
|
||||
)
|
||||
|
||||
doc = Document(
|
||||
text="some text",
|
||||
metadata={"title": "relevant title", "page number": 18},
|
||||
)
|
||||
|
||||
embedder = FastembedDocumentEmbedder(
|
||||
model="BAAI/bge-small-en-v1.5",
|
||||
batch_size=256,
|
||||
metadata_fields_to_embed=["title"],
|
||||
)
|
||||
|
||||
docs_w_embeddings = embedder.run(documents=[doc])["documents"]
|
||||
```
|
||||
|
||||
## Usage
|
||||
|
||||
### On its own
|
||||
|
||||
```python
|
||||
from haystack.dataclasses import Document
|
||||
from haystack_integrations.components.embedders.fastembed import (
|
||||
FastembedDocumentEmbedder,
|
||||
)
|
||||
|
||||
document_list = [
|
||||
Document(content="I love pizza!"),
|
||||
Document(content="I like spaghetti"),
|
||||
]
|
||||
|
||||
doc_embedder = FastembedDocumentEmbedder()
|
||||
|
||||
result = doc_embedder.run(document_list)
|
||||
print(result["documents"][0].embedding)
|
||||
|
||||
# [-0.04235665127635002, 0.021791068837046623, ...]
|
||||
```
|
||||
|
||||
### In a pipeline
|
||||
|
||||
```python
|
||||
from haystack import Document, Pipeline
|
||||
from haystack.components.writers import DocumentWriter
|
||||
from haystack.components.retrievers.in_memory import InMemoryEmbeddingRetriever
|
||||
from haystack.document_stores.in_memory import InMemoryDocumentStore
|
||||
from haystack.document_stores.types import DuplicatePolicy
|
||||
from haystack_integrations.components.embedders.fastembed import (
|
||||
FastembedDocumentEmbedder,
|
||||
FastembedTextEmbedder,
|
||||
)
|
||||
|
||||
document_store = InMemoryDocumentStore(embedding_similarity_function="cosine")
|
||||
|
||||
documents = [
|
||||
Document(content="My name is Wolfgang and I live in Berlin"),
|
||||
Document(content="I saw a black horse running"),
|
||||
Document(content="Germany has many big cities"),
|
||||
Document(content="fastembed is supported by and maintained by Qdrant."),
|
||||
]
|
||||
|
||||
document_embedder = FastembedDocumentEmbedder()
|
||||
writer = DocumentWriter(document_store=document_store, policy=DuplicatePolicy.OVERWRITE)
|
||||
|
||||
indexing_pipeline = Pipeline()
|
||||
indexing_pipeline.add_component("document_embedder", document_embedder)
|
||||
indexing_pipeline.add_component("writer", writer)
|
||||
indexing_pipeline.connect("document_embedder", "writer")
|
||||
|
||||
indexing_pipeline.run({"document_embedder": {"documents": documents}})
|
||||
|
||||
query_pipeline = Pipeline()
|
||||
query_pipeline.add_component("text_embedder", FastembedTextEmbedder())
|
||||
query_pipeline.add_component(
|
||||
"retriever",
|
||||
InMemoryEmbeddingRetriever(document_store=document_store),
|
||||
)
|
||||
query_pipeline.connect("text_embedder.embedding", "retriever.query_embedding")
|
||||
|
||||
query = "Who supports fastembed?"
|
||||
|
||||
result = query_pipeline.run({"text_embedder": {"text": query}})
|
||||
|
||||
print(result["retriever"]["documents"][0]) # noqa: T201
|
||||
|
||||
# Document(id=...,
|
||||
# content: 'fastembed is supported by and maintained by Qdrant.',
|
||||
# score: 0.758..)
|
||||
```
|
||||
|
||||
## Additional References
|
||||
|
||||
🧑🍳 Cookbook: [RAG Pipeline Using FastEmbed for Embeddings Generation](https://haystack.deepset.ai/cookbook/rag_fastembed)
|
||||
@@ -0,0 +1,191 @@
|
||||
---
|
||||
title: "FastembedSparseDocumentEmbedder"
|
||||
id: fastembedsparsedocumentembedder
|
||||
slug: "/fastembedsparsedocumentembedder"
|
||||
description: "Use this component to enrich a list of documents with their sparse embeddings."
|
||||
---
|
||||
|
||||
# FastembedSparseDocumentEmbedder
|
||||
|
||||
Use this component to enrich a list of documents with their sparse embeddings.
|
||||
|
||||
<div className="key-value-table">
|
||||
|
||||
| | |
|
||||
| --- | --- |
|
||||
| **Most common position in a pipeline** | Before a [`DocumentWriter`](../writers/documentwriter.mdx) in an indexing pipeline |
|
||||
| **Mandatory run variables** | `documents`: A list of documents |
|
||||
| **Output variables** | `documents`: A list of documents (enriched with sparse embeddings) |
|
||||
| **API reference** | [FastEmbed](/reference/fastembed-embedders) |
|
||||
| **GitHub link** | https://github.com/deepset-ai/haystack-core-integrations/tree/main/integrations/fastembed |
|
||||
| **Package name** | `fastembed-haystack` |
|
||||
|
||||
</div>
|
||||
|
||||
To compute a sparse embedding for a string, use the [`FastembedSparseTextEmbedder`](fastembedsparsetextembedder.mdx).
|
||||
|
||||
## Overview
|
||||
|
||||
`FastembedSparseDocumentEmbedder` computes the sparse embeddings of a list of documents and stores the obtained vectors in the `sparse_embedding` field of each document. It uses sparse embedding [models](https://qdrant.github.io/fastembed/examples/Supported_Models/#supported-sparse-text-embedding-models) supported by FastEmbed.
|
||||
|
||||
The vectors calculated by this component are necessary for performing sparse embedding retrieval on a set of documents. During retrieval, the sparse vector representing the query is compared to those of the documents to identify the most similar or relevant ones.
|
||||
|
||||
### Compatible models
|
||||
|
||||
You can find the supported models in the [FastEmbed documentation](https://qdrant.github.io/fastembed/examples/Supported_Models/#supported-sparse-text-embedding-models).
|
||||
|
||||
Currently, supported models are based on SPLADE, a technique for producing sparse representations for text, where each non-zero value in the embedding is the importance weight of a term in the BERT WordPiece vocabulary. For more information, see [our docs](../retrievers.mdx#sparse-embedding-based-retrievers) that explain sparse embedding-based Retrievers further.
|
||||
|
||||
### Installation
|
||||
|
||||
To start using this integration with Haystack, install the package with:
|
||||
|
||||
```shell
|
||||
pip install fastembed-haystack
|
||||
```
|
||||
|
||||
### Parameters
|
||||
|
||||
You can set the path where the model will be stored in a cache directory. Also, you can set the number of threads a single `onnxruntime` session can use:
|
||||
|
||||
```python
|
||||
cache_dir = "/your_cacheDirectory"
|
||||
embedder = FastembedSparseDocumentEmbedder(
|
||||
model="prithivida/Splade_PP_en_v1",
|
||||
cache_dir=cache_dir,
|
||||
threads=2,
|
||||
)
|
||||
```
|
||||
|
||||
If you want to use the data parallel encoding, you can set the parameters `parallel` and `batch_size`.
|
||||
|
||||
- If `parallel` > 1, data-parallel encoding will be used. This is recommended for offline encoding of large datasets.
|
||||
- If `parallel` is 0, use all available cores.
|
||||
- If None, don't use data-parallel processing; use default `onnxruntime` threading instead.
|
||||
|
||||
:::tip
|
||||
If you create both a Sparse Text Embedder and a Sparse Document Embedder based on the same model, Haystack utilizes a shared resource behind the scenes to conserve resources.
|
||||
:::
|
||||
|
||||
### Embedding Metadata
|
||||
|
||||
Text documents often include metadata. If the metadata is distinctive and semantically meaningful, you can embed it along with the document's text to improve retrieval.
|
||||
|
||||
You can do this easily by using the sparse Document Embedder:
|
||||
|
||||
```python
|
||||
from haystack.preview import Document
|
||||
from haystack_integrations.components.embedders.fastembed import (
|
||||
FastembedSparseDocumentEmbedder,
|
||||
)
|
||||
|
||||
doc = Document(
|
||||
text="some text",
|
||||
metadata={"title": "relevant title", "page number": 18},
|
||||
)
|
||||
|
||||
embedder = FastembedSparseDocumentEmbedder(
|
||||
model="prithivida/Splade_PP_en_v1",
|
||||
metadata_fields_to_embed=["title"],
|
||||
)
|
||||
|
||||
docs_w_sparse_embeddings = embedder.run(documents=[doc])["documents"]
|
||||
```
|
||||
|
||||
## Usage
|
||||
|
||||
### On its own
|
||||
|
||||
```python
|
||||
from haystack.dataclasses import Document
|
||||
from haystack_integrations.components.embedders.fastembed import (
|
||||
FastembedSparseDocumentEmbedder,
|
||||
)
|
||||
|
||||
document_list = [
|
||||
Document(content="I love pizza!"),
|
||||
Document(content="I like spaghetti"),
|
||||
]
|
||||
|
||||
doc_embedder = FastembedSparseDocumentEmbedder()
|
||||
|
||||
result = doc_embedder.run(document_list)
|
||||
print(result["documents"][0])
|
||||
|
||||
# Document(id=...,
|
||||
# content: 'I love pizza!',
|
||||
# sparse_embedding: vector with 24 non-zero elements)
|
||||
```
|
||||
|
||||
### In a pipeline
|
||||
|
||||
Currently, sparse embedding retrieval is only supported by `QdrantDocumentStore`.
|
||||
First, install the package with:
|
||||
|
||||
```shell
|
||||
pip install qdrant-haystack
|
||||
```
|
||||
|
||||
Then, try out this pipeline:
|
||||
|
||||
```python
|
||||
from haystack import Document, Pipeline
|
||||
from haystack.components.writers import DocumentWriter
|
||||
from haystack_integrations.components.retrievers.qdrant import (
|
||||
QdrantSparseEmbeddingRetriever,
|
||||
)
|
||||
from haystack_integrations.document_stores.qdrant import QdrantDocumentStore
|
||||
from haystack.document_stores.types import DuplicatePolicy
|
||||
from haystack_integrations.components.embedders.fastembed import (
|
||||
FastembedDocumentEmbedder,
|
||||
FastembedTextEmbedder,
|
||||
)
|
||||
|
||||
document_store = QdrantDocumentStore(
|
||||
":memory:",
|
||||
recreate_index=True,
|
||||
use_sparse_embeddings=True,
|
||||
)
|
||||
|
||||
documents = [
|
||||
Document(content="My name is Wolfgang and I live in Berlin"),
|
||||
Document(content="I saw a black horse running"),
|
||||
Document(content="Germany has many big cities"),
|
||||
Document(content="fastembed is supported by and maintained by Qdrant."),
|
||||
]
|
||||
|
||||
sparse_document_embedder = FastembedSparseDocumentEmbedder()
|
||||
writer = DocumentWriter(document_store=document_store, policy=DuplicatePolicy.OVERWRITE)
|
||||
|
||||
indexing_pipeline = Pipeline()
|
||||
indexing_pipeline.add_component("sparse_document_embedder", sparse_document_embedder)
|
||||
indexing_pipeline.add_component("writer", writer)
|
||||
indexing_pipeline.connect("sparse_document_embedder", "writer")
|
||||
|
||||
indexing_pipeline.run({"sparse_document_embedder": {"documents": documents}})
|
||||
|
||||
query_pipeline = Pipeline()
|
||||
query_pipeline.add_component("sparse_text_embedder", FastembedSparseTextEmbedder())
|
||||
query_pipeline.add_component(
|
||||
"sparse_retriever",
|
||||
QdrantSparseEmbeddingRetriever(document_store=document_store),
|
||||
)
|
||||
query_pipeline.connect(
|
||||
"sparse_text_embedder.sparse_embedding",
|
||||
"sparse_retriever.query_sparse_embedding",
|
||||
)
|
||||
|
||||
query = "Who supports fastembed?"
|
||||
|
||||
result = query_pipeline.run({"sparse_text_embedder": {"text": query}})
|
||||
|
||||
print(result["sparse_retriever"]["documents"][0]) # noqa: T201
|
||||
|
||||
# Document(id=...,
|
||||
# content: 'fastembed is supported by and maintained by Qdrant.',
|
||||
# score: 0.758..)
|
||||
```
|
||||
|
||||
## Additional References
|
||||
|
||||
🧑🍳 Cookbook: [Sparse Embedding Retrieval with Qdrant and FastEmbed](https://haystack.deepset.ai/cookbook/sparse_embedding_retrieval)
|
||||
@@ -0,0 +1,155 @@
|
||||
---
|
||||
title: "FastembedSparseTextEmbedder"
|
||||
id: fastembedsparsetextembedder
|
||||
slug: "/fastembedsparsetextembedder"
|
||||
description: "Use this component to embed a simple string (such as a query) into a sparse vector."
|
||||
---
|
||||
|
||||
# FastembedSparseTextEmbedder
|
||||
|
||||
Use this component to embed a simple string (such as a query) into a sparse vector.
|
||||
|
||||
<div className="key-value-table">
|
||||
|
||||
| | |
|
||||
| --- | --- |
|
||||
| **Most common position in a pipeline** | Before a sparse embedding [Retriever](../retrievers.mdx) in a query/RAG pipeline |
|
||||
| **Mandatory run variables** | `text`: A string |
|
||||
| **Output variables** | `sparse_embedding`: A [`SparseEmbedding`](../../concepts/data-classes.mdx#sparseembedding) object |
|
||||
| **API reference** | [FastEmbed](/reference/fastembed-embedders) |
|
||||
| **GitHub link** | https://github.com/deepset-ai/haystack-core-integrations/tree/main/integrations/fastembed |
|
||||
| **Package name** | `fastembed-haystack` |
|
||||
|
||||
</div>
|
||||
|
||||
For embedding lists of documents, use the [`FastembedSparseDocumentEmbedder`](fastembedsparsedocumentembedder.mdx), which enriches the document with the computed sparse embedding.
|
||||
|
||||
## Overview
|
||||
|
||||
`FastembedSparseTextEmbedder` transforms a string into a sparse vector using sparse embedding [models](https://qdrant.github.io/fastembed/examples/Supported_Models/#supported-sparse-text-embedding-models) supported by FastEmbed.
|
||||
|
||||
When you perform sparse embedding retrieval, use this component first to transform your query into a sparse vector. Then, the sparse embedding Retriever will use the vector to search for similar or relevant documents.
|
||||
|
||||
### Compatible Models
|
||||
|
||||
You can find the supported models in the [FastEmbed documentation](https://qdrant.github.io/fastembed/examples/Supported_Models/#supported-sparse-text-embedding-models).
|
||||
|
||||
Currently, supported models are based on SPLADE, a technique for producing sparse representations for text, where each non-zero value in the embedding is the importance weight of a term in the BERT WordPiece vocabulary. For more information, see [our docs](../retrievers.mdx#sparse-embedding-based-retrievers) that explain sparse embedding-based Retrievers further.
|
||||
|
||||
### Installation
|
||||
|
||||
To start using this integration with Haystack, install the package with:
|
||||
|
||||
```shell
|
||||
pip install fastembed-haystack
|
||||
```
|
||||
|
||||
### Parameters
|
||||
|
||||
You can set the path where the model will be stored in a cache directory. Also, you can set the number of threads a single `onnxruntime` session can use:
|
||||
|
||||
```python
|
||||
cache_dir = "/your_cacheDirectory"
|
||||
embedder = FastembedSparseTextEmbedder(
|
||||
model="prithivida/Splade_PP_en_v1",
|
||||
cache_dir=cache_dir,
|
||||
threads=2,
|
||||
)
|
||||
```
|
||||
|
||||
If you want to use the data parallel encoding, you can set the `parallel` parameter.
|
||||
|
||||
- If `parallel` > 1, data-parallel encoding will be used. This is recommended for offline encoding of large datasets.
|
||||
- If `parallel` is 0, use all available cores.
|
||||
- If None, don't use data-parallel processing; use the default `onnxruntime` threading instead.
|
||||
|
||||
:::tip
|
||||
If you create both a Sparse Text Embedder and a Sparse Document Embedder based on the same model, Haystack utilizes a shared resource behind the scenes to conserve resources.
|
||||
:::
|
||||
|
||||
## Usage
|
||||
|
||||
### On its own
|
||||
|
||||
```python
|
||||
from haystack_integrations.components.embedders.fastembed import (
|
||||
FastembedSparseTextEmbedder,
|
||||
)
|
||||
|
||||
text = """It clearly says online this will work on a Mac OS system.
|
||||
The disk comes and it does not, only Windows.
|
||||
Do Not order this if you have a Mac!!"""
|
||||
|
||||
text_embedder = FastembedSparseTextEmbedder(model="prithivida/Splade_PP_en_v1")
|
||||
|
||||
sparse_embedding = text_embedder.run(text)["sparse_embedding"]
|
||||
```
|
||||
|
||||
### In a pipeline
|
||||
|
||||
Currently, sparse embedding retrieval is only supported by `QdrantDocumentStore`.
|
||||
First, install the package with:
|
||||
|
||||
```shell
|
||||
pip install qdrant-haystack
|
||||
```
|
||||
|
||||
Then, try out this pipeline:
|
||||
|
||||
```python
|
||||
from haystack import Document, Pipeline
|
||||
from haystack_integrations.document_stores.qdrant import QdrantDocumentStore
|
||||
from haystack_integrations.components.retrievers.qdrant import (
|
||||
QdrantSparseEmbeddingRetriever,
|
||||
)
|
||||
from haystack_integrations.components.embedders.fastembed import (
|
||||
FastembedSparseTextEmbedder,
|
||||
FastembedSparseDocumentEmbedder,
|
||||
FastembedTextEmbedder,
|
||||
)
|
||||
|
||||
document_store = QdrantDocumentStore(
|
||||
":memory:",
|
||||
recreate_index=True,
|
||||
use_sparse_embeddings=True,
|
||||
)
|
||||
|
||||
documents = [
|
||||
Document(content="My name is Wolfgang and I live in Berlin"),
|
||||
Document(content="I saw a black horse running"),
|
||||
Document(content="Germany has many big cities"),
|
||||
Document(content="fastembed is supported by and maintained by Qdrant."),
|
||||
]
|
||||
|
||||
sparse_document_embedder = FastembedSparseDocumentEmbedder(
|
||||
model="prithivida/Splade_PP_en_v1",
|
||||
)
|
||||
|
||||
documents_with_sparse_embeddings = sparse_document_embedder.run(documents)["documents"]
|
||||
document_store.write_documents(documents_with_sparse_embeddings)
|
||||
|
||||
query_pipeline = Pipeline()
|
||||
query_pipeline.add_component("sparse_text_embedder", FastembedSparseTextEmbedder())
|
||||
query_pipeline.add_component(
|
||||
"sparse_retriever",
|
||||
QdrantSparseEmbeddingRetriever(document_store=document_store),
|
||||
)
|
||||
query_pipeline.connect(
|
||||
"sparse_text_embedder.sparse_embedding",
|
||||
"sparse_retriever.query_sparse_embedding",
|
||||
)
|
||||
|
||||
query = "Who supports fastembed?"
|
||||
|
||||
result = query_pipeline.run({"sparse_text_embedder": {"text": query}})
|
||||
|
||||
print(result["sparse_retriever"]["documents"][0]) # noqa: T201
|
||||
|
||||
# Document(id=...,
|
||||
# content: 'fastembed is supported by and maintained by Qdrant.',
|
||||
# score: 0.561..)
|
||||
```
|
||||
|
||||
## Additional References
|
||||
|
||||
🧑🍳 Cookbook: [Sparse Embedding Retrieval with Qdrant and FastEmbed](https://haystack.deepset.ai/cookbook/sparse_embedding_retrieval)
|
||||
@@ -0,0 +1,143 @@
|
||||
---
|
||||
title: "FastembedTextEmbedder"
|
||||
id: fastembedtextembedder
|
||||
slug: "/fastembedtextembedder"
|
||||
description: "This component computes the embeddings of a string using embedding models supported by FastEmbed."
|
||||
---
|
||||
|
||||
# FastembedTextEmbedder
|
||||
|
||||
This component computes the embeddings of a string using embedding models supported by FastEmbed.
|
||||
|
||||
<div className="key-value-table">
|
||||
|
||||
| | |
|
||||
| --- | --- |
|
||||
| **Most common position in a pipeline** | Before an embedding [Retriever](../retrievers.mdx) in a query/RAG pipeline |
|
||||
| **Mandatory run variables** | `text`: A string |
|
||||
| **Output variables** | `embedding`: A vector (list of float numbers) |
|
||||
| **API reference** | [FastEmbed](/reference/fastembed-embedders) |
|
||||
| **GitHub link** | https://github.com/deepset-ai/haystack-core-integrations/tree/main/integrations/fastembed |
|
||||
| **Package name** | `fastembed-haystack` |
|
||||
|
||||
</div>
|
||||
|
||||
This component should be used to embed a simple string (such as a query) into a vector. For embedding lists of documents, use the [`FastembedDocumentEmbedder`](fastembeddocumentembedder.mdx), which enriches the document with the computed embedding, known as vector.
|
||||
|
||||
## Overview
|
||||
|
||||
`FastembedTextEmbedder` transforms a string into a vector that captures its semantics using embedding [models supported by FastEmbed](https://qdrant.github.io/fastembed/examples/Supported_Models/).
|
||||
|
||||
When you perform embedding retrieval, use this component first to transform your query into a vector. Then, the embedding Retriever will use the vector to search for similar or relevant documents.
|
||||
|
||||
### Compatible models
|
||||
|
||||
You can find the original models in the [FastEmbed documentation](https://qdrant.github.io/fastembed/).
|
||||
|
||||
Currently, most of the models in the [Massive Text Embedding Benchmark (MTEB) Leaderboard](https://huggingface.co/spaces/mteb/leaderboard) are compatible with FastEmbed. You can look for compatibility in the [supported model list](https://qdrant.github.io/fastembed/examples/Supported_Models/).
|
||||
|
||||
### Installation
|
||||
|
||||
To start using this integration with Haystack, install the package with:
|
||||
|
||||
```bash
|
||||
pip install fastembed-haystack
|
||||
```
|
||||
|
||||
### Instructions
|
||||
|
||||
Some recent models that you can find in MTEB require prepending the text with an instruction to work better for retrieval.
|
||||
For example, if you use `[BAAI/bge-large-en-v1.5](https://huggingface.co/BAAI/bge-large-en-v1.5#model-list)` model, you should prefix your query with the `instruction: “passage:”`.
|
||||
|
||||
This is how it works with `FastembedTextEmbedder`:
|
||||
|
||||
```python
|
||||
instruction = "passage:"
|
||||
embedder = FastembedTextEmbedder(
|
||||
*model="*BAAI/bge-large-en-v1.5",
|
||||
prefix=instruction)
|
||||
```
|
||||
|
||||
### Parameters
|
||||
|
||||
You can set the path where the model will be stored in a cache directory. Also, you can set the number of threads a single `onnxruntime` session can use.
|
||||
|
||||
```python
|
||||
cache_dir= "/your_cacheDirectory"
|
||||
embedder = FastembedTextEmbedder(
|
||||
*model="*BAAI/bge-large-en-v1.5",
|
||||
cache_dir=cache_dir,
|
||||
threads=2
|
||||
)
|
||||
```
|
||||
|
||||
If you want to use the data parallel encoding, you can set the parameters `parallel` and `batch_size`.
|
||||
|
||||
- If parallel > 1, data-parallel encoding will be used. This is recommended for offline encoding of large datasets.
|
||||
- If parallel is 0, use all available cores.
|
||||
- If None, don't use data-parallel processing; use default `onnxruntime` threading instead.
|
||||
|
||||
:::tip
|
||||
If you create a Text Embedder and a Document Embedder based on the same model, Haystack uses the same resource behind the scenes to save resources.
|
||||
:::
|
||||
|
||||
## Usage
|
||||
|
||||
### On its own
|
||||
|
||||
```python
|
||||
from haystack_integrations.components.embedders.fastembed import FastembedTextEmbedder
|
||||
|
||||
text = """It clearly says online this will work on a Mac OS system.
|
||||
The disk comes and it does not, only Windows.
|
||||
Do Not order this if you have a Mac!!"""
|
||||
text_embedder = FastembedTextEmbedder(model="BAAI/bge-small-en-v1.5")
|
||||
embedding = text_embedder.run(text)["embedding"]
|
||||
```
|
||||
|
||||
### In a pipeline
|
||||
|
||||
```python
|
||||
from haystack import Document, Pipeline
|
||||
from haystack.components.retrievers.in_memory import InMemoryEmbeddingRetriever
|
||||
from haystack.document_stores.in_memory import InMemoryDocumentStore
|
||||
from haystack_integrations.components.embedders.fastembed import (
|
||||
FastembedDocumentEmbedder,
|
||||
FastembedTextEmbedder,
|
||||
)
|
||||
|
||||
document_store = InMemoryDocumentStore(embedding_similarity_function="cosine")
|
||||
|
||||
documents = [
|
||||
Document(content="My name is Wolfgang and I live in Berlin"),
|
||||
Document(content="I saw a black horse running"),
|
||||
Document(content="Germany has many big cities"),
|
||||
Document(content="fastembed is supported by and maintained by Qdrant."),
|
||||
]
|
||||
|
||||
document_embedder = FastembedDocumentEmbedder()
|
||||
documents_with_embeddings = document_embedder.run(documents)["documents"]
|
||||
document_store.write_documents(documents_with_embeddings)
|
||||
|
||||
query_pipeline = Pipeline()
|
||||
query_pipeline.add_component("text_embedder", FastembedTextEmbedder())
|
||||
query_pipeline.add_component(
|
||||
"retriever",
|
||||
InMemoryEmbeddingRetriever(document_store=document_store),
|
||||
)
|
||||
query_pipeline.connect("text_embedder.embedding", "retriever.query_embedding")
|
||||
|
||||
query = "Who supports FastEmbed?"
|
||||
|
||||
result = query_pipeline.run({"text_embedder": {"text": query}})
|
||||
|
||||
print(result["retriever"]["documents"][0]) # noqa: T201
|
||||
|
||||
# Document(id=...,
|
||||
# content: 'FastEmbed is supported by and maintained by Qdrant.',
|
||||
# score: 0.758..)
|
||||
```
|
||||
|
||||
## Additional References
|
||||
|
||||
🧑🍳 Cookbook: [RAG Pipeline Using FastEmbed for Embeddings Generation](https://haystack.deepset.ai/cookbook/rag_fastembed)
|
||||
@@ -0,0 +1,182 @@
|
||||
---
|
||||
title: "GoogleGenAIDocumentEmbedder"
|
||||
id: googlegenaidocumentembedder
|
||||
slug: "/googlegenaidocumentembedder"
|
||||
description: "The vectors computed by this component are necessary to perform embedding retrieval on a collection of documents. At retrieval time, the vector representing the query is compared with those of the documents to find the most similar or relevant documents."
|
||||
---
|
||||
|
||||
# GoogleGenAIDocumentEmbedder
|
||||
|
||||
The vectors computed by this component are necessary to perform embedding retrieval on a collection of documents. At retrieval time, the vector representing the query is compared with those of the documents to find the most similar or relevant documents.
|
||||
|
||||
<div className="key-value-table">
|
||||
|
||||
| | |
|
||||
| --- | --- |
|
||||
| **Most common position in a pipeline** | Before a [DocumentWriter](../writers/documentwriter.mdx) in an indexing pipeline |
|
||||
| **Mandatory init variables** | `api_key`: The Google API key. Can be set with `GOOGLE_API_KEY` or `GEMINI_API_KEY` env var. |
|
||||
| **Mandatory run variables** | `documents`: A list of documents to be embedded |
|
||||
| **Output variables** | `documents`: A list of documents (enriched with embeddings) <br /> <br />`meta`: A dictionary of metadata |
|
||||
| **API reference** | [Google GenAI](/reference/integrations-google-genai) |
|
||||
| **GitHub link** | https://github.com/deepset-ai/haystack-core-integrations/tree/main/integrations/google_genai |
|
||||
| **Package name** | `google-genai-haystack` |
|
||||
|
||||
</div>
|
||||
|
||||
## Overview
|
||||
|
||||
`GoogleGenAIDocumentEmbedder` enriches the metadata of documents with an embedding of their content. To embed a string, you should use the [`GoogleGenAITextEmbedder`](googlegenaitextembedder.mdx).
|
||||
|
||||
The component supports [Google AI Embedding models](https://ai.google.dev/gemini-api/docs/embeddings#model-versions).
|
||||
|
||||
`gemini-embedding-001` is the default model.
|
||||
|
||||
To start using this integration with Haystack, install it with:
|
||||
|
||||
```shell
|
||||
pip install google-genai-haystack
|
||||
```
|
||||
|
||||
### Authentication
|
||||
|
||||
Google Gen AI is compatible with both the Gemini Developer API and the Vertex AI API.
|
||||
|
||||
To use this component with the Gemini Developer API and get an API key, visit [Google AI Studio](https://aistudio.google.com/).
|
||||
To use this component with the Vertex AI API, visit [Google Cloud > Vertex AI](https://cloud.google.com/vertex-ai).
|
||||
|
||||
The component uses a `GOOGLE_API_KEY` or `GEMINI_API_KEY` environment variable by default. Otherwise, you can pass an API key at initialization with a [Secret](../../concepts/secret-management.mdx) and `Secret.from_token` static method:
|
||||
|
||||
```python
|
||||
embedder = GoogleGenAIDocumentEmbedder(api_key=Secret.from_token("<your-api-key>"))
|
||||
```
|
||||
|
||||
The following examples show how to use the component with the Gemini Developer API and the Vertex AI API.
|
||||
|
||||
#### Gemini Developer API (API Key Authentication)
|
||||
|
||||
```python
|
||||
from haystack_integrations.components.embedders.google_genai import (
|
||||
GoogleGenAIDocumentEmbedder,
|
||||
)
|
||||
|
||||
# set the environment variable (GOOGLE_API_KEY or GEMINI_API_KEY)
|
||||
chat_generator = GoogleGenAIDocumentEmbedder()
|
||||
```
|
||||
|
||||
#### Vertex AI (Application Default Credentials)
|
||||
|
||||
```python
|
||||
from haystack_integrations.components.embedders.google_genai import (
|
||||
GoogleGenAIDocumentEmbedder,
|
||||
)
|
||||
|
||||
# Using Application Default Credentials (requires gcloud auth setup)
|
||||
chat_generator = GoogleGenAIDocumentEmbedder(
|
||||
api="vertex",
|
||||
vertex_ai_project="my-project",
|
||||
vertex_ai_location="us-central1",
|
||||
)
|
||||
```
|
||||
|
||||
#### Vertex AI (API Key Authentication)
|
||||
|
||||
```python
|
||||
from haystack_integrations.components.embedders.google_genai import (
|
||||
GoogleGenAIDocumentEmbedder,
|
||||
)
|
||||
|
||||
# set the environment variable (GOOGLE_API_KEY or GEMINI_API_KEY)
|
||||
chat_generator = GoogleGenAIDocumentEmbedder(api="vertex")
|
||||
```
|
||||
|
||||
## Usage
|
||||
|
||||
### Embedding Metadata
|
||||
|
||||
Text documents often come with a set of metadata. If they are distinctive and semantically meaningful, you can embed them along with the text of the document to improve retrieval.
|
||||
|
||||
You can do this by using the Document Embedder:
|
||||
|
||||
```python
|
||||
from haystack import Document
|
||||
from haystack_integrations.components.embedders.google_genai import (
|
||||
GoogleGenAIDocumentEmbedder,
|
||||
)
|
||||
|
||||
doc = Document(content="some text", meta={"title": "relevant title", "page number": 18})
|
||||
|
||||
embedder = GoogleGenAIDocumentEmbedder(
|
||||
api_key=Secret.from_token("<your-api-key>"),
|
||||
meta_fields_to_embed=["title"],
|
||||
)
|
||||
|
||||
docs_w_embeddings = embedder.run(documents=[doc])["documents"]
|
||||
```
|
||||
|
||||
## Usage
|
||||
|
||||
### On its own
|
||||
|
||||
Here is how you can use the component on its own. You'll need to pass in your Google API key via Secret or set it as an environment variable called `GOOGLE_API_KEY` or `GEMINI_API_KEY`. The examples below assume you've set the environment variable.
|
||||
|
||||
```python
|
||||
from haystack import Document
|
||||
from haystack_integrations.components.embedders.google_genai import (
|
||||
GoogleGenAIDocumentEmbedder,
|
||||
)
|
||||
|
||||
doc = Document(content="I love pizza!")
|
||||
|
||||
document_embedder = GoogleGenAIDocumentEmbedder()
|
||||
|
||||
result = document_embedder.run([doc])
|
||||
print(result["documents"][0].embedding)
|
||||
# [0.017020374536514282, -0.023255806416273117, ...]
|
||||
```
|
||||
|
||||
### In a pipeline
|
||||
|
||||
```python
|
||||
from haystack import Document
|
||||
from haystack import Pipeline
|
||||
from haystack.document_stores.in_memory import InMemoryDocumentStore
|
||||
from haystack_integrations.components.embedders.google_genai import (
|
||||
GoogleGenAITextEmbedder,
|
||||
)
|
||||
from haystack_integrations.components.embedders.google_genai import (
|
||||
GoogleGenAIDocumentEmbedder,
|
||||
)
|
||||
from haystack.components.writers import DocumentWriter
|
||||
from haystack.components.retrievers.in_memory import InMemoryEmbeddingRetriever
|
||||
|
||||
document_store = InMemoryDocumentStore(embedding_similarity_function="cosine")
|
||||
|
||||
documents = [
|
||||
Document(content="My name is Wolfgang and I live in Berlin"),
|
||||
Document(content="I saw a black horse running"),
|
||||
Document(content="Germany has many big cities"),
|
||||
]
|
||||
|
||||
indexing_pipeline = Pipeline()
|
||||
indexing_pipeline.add_component("embedder", GoogleGenAIDocumentEmbedder())
|
||||
indexing_pipeline.add_component("writer", DocumentWriter(document_store=document_store))
|
||||
indexing_pipeline.connect("embedder", "writer")
|
||||
|
||||
indexing_pipeline.run({"embedder": {"documents": documents}})
|
||||
|
||||
query_pipeline = Pipeline()
|
||||
query_pipeline.add_component("text_embedder", GoogleGenAITextEmbedder())
|
||||
query_pipeline.add_component(
|
||||
"retriever",
|
||||
InMemoryEmbeddingRetriever(document_store=document_store),
|
||||
)
|
||||
query_pipeline.connect("text_embedder.embedding", "retriever.query_embedding")
|
||||
|
||||
query = "Who lives in Berlin?"
|
||||
|
||||
result = query_pipeline.run({"text_embedder": {"text": query}})
|
||||
|
||||
print(result["retriever"]["documents"][0])
|
||||
|
||||
# Document(id=..., content: 'My name is Wolfgang and I live in Berlin')
|
||||
```
|
||||
+197
@@ -0,0 +1,197 @@
|
||||
---
|
||||
title: "GoogleGenAIMultimodalDocumentEmbedder"
|
||||
id: googlegenaimultimodaldocumentembedder
|
||||
slug: "/googlegenaimultimodaldocumentembedder"
|
||||
description: "`GoogleGenAIMultimodalDocumentEmbedder` computes the embeddings of a list of non-textual documents and stores the obtained vectors in the embedding field of each document."
|
||||
---
|
||||
|
||||
# GoogleGenAIMultimodalDocumentEmbedder
|
||||
|
||||
`GoogleGenAIMultimodalDocumentEmbedder` computes the embeddings of a list of non-textual documents and stores the obtained vectors in the embedding field of each document.
|
||||
It uses Google AI multimodal embedding models with the ability to embed text, images, videos, and audio into the same vector space.
|
||||
<div className="key-value-table">
|
||||
|
||||
| | |
|
||||
| --- | --- |
|
||||
| **Most common position in a pipeline** | Before a [DocumentWriter](../writers/documentwriter.mdx) in an indexing pipeline |
|
||||
| **Mandatory init variables** | `api_key`: The Google API key. Can be set with `GOOGLE_API_KEY` or `GEMINI_API_KEY` env var. |
|
||||
| **Mandatory run variables** | `documents`: A list of documents, with a meta field containing an image file path |
|
||||
| **Output variables** | `documents`: A list of documents (enriched with embeddings) <br /> <br />`meta`: A dictionary of metadata |
|
||||
| **API reference** | [Google GenAI](/reference/integrations-google-genai) |
|
||||
| **GitHub link** | https://github.com/deepset-ai/haystack-core-integrations/tree/main/integrations/google_genai |
|
||||
| **Package name** | `google-genai-haystack` |
|
||||
|
||||
</div>
|
||||
|
||||
## Overview
|
||||
|
||||
`GoogleGenAIMultimodalDocumentEmbedder` expects a list of documents containing a file path in a meta field. The meta field can be specified with the `file_path_meta_field` init parameter of this component.
|
||||
|
||||
The embedder efficiently loads the files, computes the embeddings using a Google AI model, and stores each of them in the `embedding` field of the document.
|
||||
|
||||
`GoogleGenAIMultimodalDocumentEmbedder` is commonly used in indexing pipelines. At retrieval time, you need to use the same model with a `GoogleGenAITextEmbedder` to embed the query, before using an Embedding Retriever.
|
||||
|
||||
This component is compatible with Gemini multimodal models: `gemini-embedding-2` and later. For a complete list of supported models, see the [Google AI documentation](https://ai.google.dev/gemini-api/docs/embeddings).
|
||||
|
||||
To embed a textual document, you should use the [`GoogleGenAIDocumentEmbedder`](googlegenaidocumentembedder.mdx).
|
||||
To embed a string, you should use the [`GoogleGenAITextEmbedder`](googlegenaitextembedder.mdx).
|
||||
|
||||
To start using this integration with Haystack, install it with:
|
||||
|
||||
```shell
|
||||
pip install google-genai-haystack
|
||||
```
|
||||
|
||||
### Authentication
|
||||
|
||||
Google Gen AI is compatible with both the Gemini Developer API and the Vertex AI API.
|
||||
|
||||
To use this component with the Gemini Developer API and get an API key, visit [Google AI Studio](https://aistudio.google.com/).
|
||||
To use this component with the Vertex AI API, visit [Google Cloud > Vertex AI](https://cloud.google.com/vertex-ai).
|
||||
|
||||
The component uses a `GOOGLE_API_KEY` or `GEMINI_API_KEY` environment variable by default. Otherwise, you can pass an API key at initialization with a [Secret](../../concepts/secret-management.mdx) and `Secret.from_token` static method:
|
||||
|
||||
```python
|
||||
embedder = GoogleGenAIMultimodalDocumentEmbedder(
|
||||
api_key=Secret.from_token("<your-api-key>"),
|
||||
)
|
||||
```
|
||||
|
||||
The following examples show how to use the component with the Gemini Developer API and the Vertex AI API.
|
||||
|
||||
#### Gemini Developer API (API Key Authentication)
|
||||
|
||||
```python
|
||||
from haystack_integrations.components.embedders.google_genai import (
|
||||
GoogleGenAIMultimodalDocumentEmbedder,
|
||||
)
|
||||
|
||||
# set the environment variable (GOOGLE_API_KEY or GEMINI_API_KEY)
|
||||
embedder = GoogleGenAIMultimodalDocumentEmbedder()
|
||||
```
|
||||
|
||||
#### Vertex AI (Application Default Credentials)
|
||||
|
||||
```python
|
||||
from haystack_integrations.components.embedders.google_genai import (
|
||||
GoogleGenAIMultimodalDocumentEmbedder,
|
||||
)
|
||||
|
||||
# Using Application Default Credentials (requires gcloud auth setup)
|
||||
embedder = GoogleGenAIMultimodalDocumentEmbedder(
|
||||
api="vertex",
|
||||
vertex_ai_project="my-project",
|
||||
vertex_ai_location="us-central1",
|
||||
)
|
||||
```
|
||||
|
||||
#### Vertex AI (API Key Authentication)
|
||||
|
||||
```python
|
||||
from haystack_integrations.components.embedders.google_genai import (
|
||||
GoogleGenAIMultimodalDocumentEmbedder,
|
||||
)
|
||||
|
||||
# set the environment variable (GOOGLE_API_KEY or GEMINI_API_KEY)
|
||||
embedder = GoogleGenAIMultimodalDocumentEmbedder(api="vertex")
|
||||
```
|
||||
|
||||
## Usage
|
||||
|
||||
### On its own
|
||||
|
||||
Here is how you can use the component on its own. You'll need to pass in your Google API key via Secret or set it as an environment variable called `GOOGLE_API_KEY` or `GEMINI_API_KEY`.
|
||||
The examples below assume you've set the environment variable.
|
||||
|
||||
```python
|
||||
from haystack import Document
|
||||
from haystack_integrations.components.embedders.google_genai import (
|
||||
GoogleGenAIMultimodalDocumentEmbedder,
|
||||
)
|
||||
|
||||
docs = [
|
||||
Document(meta={"file_path": "path/to/image.jpg"}),
|
||||
Document(meta={"file_path": "path/to/video.mp4"}),
|
||||
Document(meta={"file_path": "path/to/pdf.pdf", "page_number": 1}),
|
||||
Document(meta={"file_path": "path/to/pdf.pdf", "page_number": 3}),
|
||||
]
|
||||
|
||||
document_embedder = GoogleGenAIMultimodalDocumentEmbedder()
|
||||
|
||||
result = document_embedder.run(documents=docs)
|
||||
print(result["documents"][0].embedding)
|
||||
# [0.017020374536514282, -0.023255806416273117, ...]
|
||||
```
|
||||
|
||||
### Setting embedding dimensions
|
||||
|
||||
Models like `gemini-embedding-2` have a default embedding dimension of 3072, but, thanks to
|
||||
Matryoshka Representation Learning, it's possible to reduce embedding size while keeping similar performance.
|
||||
|
||||
Check the [Google AI documentation](https://ai.google.dev/gemini-api/docs/embeddings#control-embedding-size) for more information.
|
||||
|
||||
```python
|
||||
from haystack import Document
|
||||
|
||||
from haystack_integrations.components.embedders.google_genai import (
|
||||
GoogleGenAIMultimodalDocumentEmbedder,
|
||||
)
|
||||
|
||||
docs = [Document(meta={"file_path": "path/to/image.jpg"})]
|
||||
|
||||
doc_multimodal_embedder = GoogleGenAIMultimodalDocumentEmbedder(
|
||||
config={"output_dimensionality": 768},
|
||||
)
|
||||
docs_with_embeddings = doc_multimodal_embedder.run(docs)["documents"]
|
||||
```
|
||||
|
||||
### In a pipeline
|
||||
|
||||
In the following example, we look for a specific plot in the "Scaling Instruction-Finetuned Language Models" paper (PDF format).
|
||||
|
||||
You first need to download the PDF file from https://arxiv.org/pdf/2210.11416.pdf.
|
||||
|
||||
```python
|
||||
from haystack import Document
|
||||
from haystack import Pipeline
|
||||
from haystack.document_stores.in_memory import InMemoryDocumentStore
|
||||
from haystack_integrations.components.embedders.google_genai import (
|
||||
GoogleGenAITextEmbedder,
|
||||
)
|
||||
from haystack_integrations.components.embedders.google_genai import (
|
||||
GoogleGenAIMultimodalDocumentEmbedder,
|
||||
)
|
||||
from haystack.components.writers import DocumentWriter
|
||||
from haystack.components.retrievers.in_memory import InMemoryEmbeddingRetriever
|
||||
|
||||
document_store = InMemoryDocumentStore(embedding_similarity_function="cosine")
|
||||
|
||||
paper_path = "2210.11416.pdf"
|
||||
|
||||
documents = [
|
||||
Document(meta={"file_path": paper_path, "page_number": i}) for i in range(1, 16)
|
||||
]
|
||||
|
||||
indexing_pipeline = Pipeline()
|
||||
indexing_pipeline.add_component("embedder", GoogleGenAIMultimodalDocumentEmbedder())
|
||||
indexing_pipeline.add_component("writer", DocumentWriter(document_store=document_store))
|
||||
indexing_pipeline.connect("embedder", "writer")
|
||||
|
||||
indexing_pipeline.run({"embedder": {"documents": documents}})
|
||||
|
||||
query_pipeline = Pipeline()
|
||||
query_pipeline.add_component("text_embedder", GoogleGenAITextEmbedder())
|
||||
query_pipeline.add_component(
|
||||
"retriever",
|
||||
InMemoryEmbeddingRetriever(document_store=document_store),
|
||||
)
|
||||
query_pipeline.connect("text_embedder.embedding", "retriever.query_embedding")
|
||||
|
||||
query = "plot showing BBH accuracy"
|
||||
|
||||
result = query_pipeline.run({"text_embedder": {"text": query}})
|
||||
|
||||
print(result["retriever"]["documents"][0].meta)
|
||||
|
||||
# {'file_path': '2210.11416.pdf', 'page_number': 9}
|
||||
```
|
||||
@@ -0,0 +1,154 @@
|
||||
---
|
||||
title: "GoogleGenAITextEmbedder"
|
||||
id: googlegenaitextembedder
|
||||
slug: "/googlegenaitextembedder"
|
||||
description: "This component transforms a string into a vector that captures its semantics using a Google AI embedding models. When you perform embedding retrieval, you use this component to transform your query into a vector. Then, the embedding Retriever looks for similar or relevant documents."
|
||||
---
|
||||
|
||||
# GoogleGenAITextEmbedder
|
||||
|
||||
This component transforms a string into a vector that captures its semantics using a Google AI embedding models. When you perform embedding retrieval, you use this component to transform your query into a vector. Then, the embedding Retriever looks for similar or relevant documents.
|
||||
|
||||
<div className="key-value-table">
|
||||
|
||||
| | |
|
||||
| --- | --- |
|
||||
| **Most common position in a pipeline** | Before an embedding [Retriever](../retrievers.mdx) in a query/RAG pipeline |
|
||||
| **Mandatory init variables** | `api_key`: The Google API key. Can be set with `GOOGLE_API_KEY` or `GEMINI_API_KEY` env var. |
|
||||
| **Mandatory run variables** | `text`: A string |
|
||||
| **Output variables** | `embedding`: A list of float numbers <br /> <br />`meta`: A dictionary of metadata |
|
||||
| **API reference** | [Google GenAI](/reference/integrations-google-genai) |
|
||||
| **GitHub link** | https://github.com/deepset-ai/haystack-core-integrations/tree/main/integrations/google_genai |
|
||||
| **Package name** | `google-genai-haystack` |
|
||||
|
||||
</div>
|
||||
|
||||
## Overview
|
||||
|
||||
`GoogleGenAITextEmbedder` embeds a simple string (such as a query) into a vector. For embedding lists of documents, use the [`GoogleGenAIDocumentEmbedder`](googlegenaidocumentembedder.mdx), which enriches the document with the computed embedding, also known as vector.
|
||||
|
||||
The component supports [Google AI Embedding models](https://ai.google.dev/gemini-api/docs/embeddings#model-versions).
|
||||
|
||||
`gemini-embedding-001` is the default model.
|
||||
|
||||
To start using this integration with Haystack, install it with:
|
||||
|
||||
```shell
|
||||
pip install google-genai-haystack
|
||||
```
|
||||
|
||||
### Authentication
|
||||
|
||||
Google Gen AI is compatible with both the Gemini Developer API and the Vertex AI API.
|
||||
|
||||
To use this component with the Gemini Developer API and get an API key, visit [Google AI Studio](https://aistudio.google.com/).
|
||||
To use this component with the Vertex AI API, visit [Google Cloud > Vertex AI](https://cloud.google.com/vertex-ai).
|
||||
|
||||
The component uses a `GOOGLE_API_KEY` or `GEMINI_API_KEY` environment variable by default. Otherwise, you can pass an API key at initialization with a [Secret](../../concepts/secret-management.mdx) and `Secret.from_token` static method:
|
||||
|
||||
```python
|
||||
embedder = GoogleGenAITextEmbedder(api_key=Secret.from_token("<your-api-key>"))
|
||||
```
|
||||
|
||||
The following examples show how to use the component with the Gemini Developer API and the Vertex AI API.
|
||||
|
||||
#### Gemini Developer API (API Key Authentication)
|
||||
|
||||
```python
|
||||
from haystack_integrations.components.embedders.google_genai import (
|
||||
GoogleGenAITextEmbedder,
|
||||
)
|
||||
|
||||
# set the environment variable (GOOGLE_API_KEY or GEMINI_API_KEY)
|
||||
chat_generator = GoogleGenAITextEmbedder()
|
||||
```
|
||||
|
||||
#### Vertex AI (Application Default Credentials)
|
||||
|
||||
```python
|
||||
from haystack_integrations.components.embedders.google_genai import (
|
||||
GoogleGenAITextEmbedder,
|
||||
)
|
||||
|
||||
# Using Application Default Credentials (requires gcloud auth setup)
|
||||
chat_generator = GoogleGenAITextEmbedder(
|
||||
api="vertex",
|
||||
vertex_ai_project="my-project",
|
||||
vertex_ai_location="us-central1",
|
||||
)
|
||||
```
|
||||
|
||||
#### Vertex AI (API Key Authentication)
|
||||
|
||||
```python
|
||||
from haystack_integrations.components.embedders.google_genai import (
|
||||
GoogleGenAITextEmbedder,
|
||||
)
|
||||
|
||||
# set the environment variable (GOOGLE_API_KEY or GEMINI_API_KEY)
|
||||
chat_generator = GoogleGenAITextEmbedder(api="vertex")
|
||||
```
|
||||
|
||||
## Usage
|
||||
|
||||
### On its own
|
||||
|
||||
Here is how you can use the component on its own. You'll need to pass in your Google API key with a Secret or set it as an environment variable called `GOOGLE_API_KEY` or `GEMINI_API_KEY`. The examples below assume you've set the environment variable.
|
||||
|
||||
```python
|
||||
from haystack_integrations.components.embedders.google_genai import (
|
||||
GoogleGenAITextEmbedder,
|
||||
)
|
||||
|
||||
text_to_embed = "I love pizza!"
|
||||
|
||||
text_embedder = GoogleGenAITextEmbedder()
|
||||
|
||||
print(text_embedder.run(text_to_embed))
|
||||
# {'embedding': [0.017020374536514282, -0.023255806416273117, ...],
|
||||
# 'meta': {'model': 'gemini-embedding-001',
|
||||
# 'usage': {'prompt_tokens': 4, 'total_tokens': 4}}}
|
||||
```
|
||||
|
||||
### In a pipeline
|
||||
|
||||
```python
|
||||
from haystack import Document
|
||||
from haystack import Pipeline
|
||||
from haystack.document_stores.in_memory import InMemoryDocumentStore
|
||||
from haystack_integrations.components.embedders.google_genai import (
|
||||
GoogleGenAITextEmbedder,
|
||||
)
|
||||
from haystack_integrations.components.embedders.google_genai import (
|
||||
GoogleGenAIDocumentEmbedder,
|
||||
)
|
||||
from haystack.components.retrievers.in_memory import InMemoryEmbeddingRetriever
|
||||
|
||||
document_store = InMemoryDocumentStore(embedding_similarity_function="cosine")
|
||||
|
||||
documents = [
|
||||
Document(content="My name is Wolfgang and I live in Berlin"),
|
||||
Document(content="I saw a black horse running"),
|
||||
Document(content="Germany has many big cities"),
|
||||
]
|
||||
|
||||
document_embedder = GoogleGenAIDocumentEmbedder()
|
||||
documents_with_embeddings = document_embedder.run(documents)["documents"]
|
||||
document_store.write_documents(documents_with_embeddings)
|
||||
|
||||
query_pipeline = Pipeline()
|
||||
query_pipeline.add_component("text_embedder", GoogleGenAITextEmbedder())
|
||||
query_pipeline.add_component(
|
||||
"retriever",
|
||||
InMemoryEmbeddingRetriever(document_store=document_store),
|
||||
)
|
||||
query_pipeline.connect("text_embedder.embedding", "retriever.query_embedding")
|
||||
|
||||
query = "Who lives in Berlin?"
|
||||
|
||||
result = query_pipeline.run({"text_embedder": {"text": query}})
|
||||
|
||||
print(result["retriever"]["documents"][0])
|
||||
|
||||
# Document(id=..., content: 'My name is Wolfgang and I live in Berlin')
|
||||
```
|
||||
@@ -0,0 +1,209 @@
|
||||
---
|
||||
title: "HuggingFaceAPIDocumentEmbedder"
|
||||
id: huggingfaceapidocumentembedder
|
||||
slug: "/huggingfaceapidocumentembedder"
|
||||
description: "Use this component to compute document embeddings using various Hugging Face APIs."
|
||||
---
|
||||
|
||||
# HuggingFaceAPIDocumentEmbedder
|
||||
|
||||
Use this component to compute document embeddings using various Hugging Face APIs.
|
||||
|
||||
<div className="key-value-table">
|
||||
|
||||
| | |
|
||||
| --- | --- |
|
||||
| **Most common position in a pipeline** | Before a [`DocumentWriter`](../writers/documentwriter.mdx) in an indexing pipeline |
|
||||
| **Mandatory init variables** | `api_type`: The type of Hugging Face API to use <br /> <br />`api_params`: A dictionary with one of the following keys: <br /> <br />- `model`: Hugging Face model ID. Required when `api_type` is `SERVERLESS_INFERENCE_API`.**OR** - `url`: URL of the inference endpoint. Required when `api_type` is `INFERENCE_ENDPOINTS` or `TEXT_EMBEDDINGS_INFERENCE`. <br /> <br />`token`: The Hugging Face API token. Can be set with `HF_API_TOKEN` or `HF_TOKEN` env var. |
|
||||
| **Mandatory run variables** | `documents`: A list of documents to be embedded |
|
||||
| **Output variables** | `documents`: A list of documents to be embedded (enriched with embeddings) |
|
||||
| **API reference** | [Hugging Face API](/reference/integrations-huggingface-api) |
|
||||
| **GitHub link** | https://github.com/deepset-ai/haystack-core-integrations/tree/main/integrations/huggingface_api |
|
||||
| **Package name** | `huggingface-api-haystack` |
|
||||
|
||||
</div>
|
||||
|
||||
## Overview
|
||||
|
||||
`HuggingFaceAPIDocumentEmbedder` can be used to compute document embeddings using different Hugging Face APIs:
|
||||
|
||||
- [Free Serverless Inference API](https://huggingface.co/inference-api)
|
||||
- [Paid Inference Endpoints](https://huggingface.co/inference-endpoints)
|
||||
- [Self-hosted Text Embeddings Inference](https://github.com/huggingface/text-embeddings-inference)
|
||||
|
||||
:::info
|
||||
This component should be used to embed a list of documents. To embed a string, use [`HuggingFaceAPITextEmbedder`](huggingfaceapitextembedder.mdx).
|
||||
:::
|
||||
|
||||
The component uses a `HF_API_TOKEN` environment variable by default. Otherwise, you can pass a Hugging Face API token at initialization with `token` – see code examples below.
|
||||
The token is needed:
|
||||
|
||||
- If you use the Serverless Inference API, or
|
||||
- If you use the Inference Endpoints.
|
||||
|
||||
## Usage
|
||||
|
||||
Install the `huggingface-api-haystack` package to use the `HuggingFaceAPIDocumentEmbedder`:
|
||||
|
||||
```shell
|
||||
pip install huggingface-api-haystack
|
||||
```
|
||||
|
||||
Similarly to other Document Embedders, this component allows adding prefixes (and postfixes) to include instruction and embedding metadata.
|
||||
For more fine-grained details, refer to the component’s [API reference](/reference/integrations-huggingface-api#huggingfaceapidocumentembedder).
|
||||
|
||||
### On its own
|
||||
|
||||
#### Using Free Serverless Inference API
|
||||
|
||||
Formerly known as (free) Hugging Face Inference API, this API allows you to quickly experiment with many models hosted on the Hugging Face Hub, offloading the inference to Hugging Face servers. It’s rate-limited and not meant for production.
|
||||
|
||||
To use this API, you need a [free Hugging Face token](https://huggingface.co/settings/tokens).
|
||||
The Embedder expects the `model` in `api_params`.
|
||||
|
||||
```python
|
||||
from haystack_integrations.components.embedders.huggingface_api import (
|
||||
HuggingFaceAPIDocumentEmbedder,
|
||||
)
|
||||
from haystack.utils import Secret
|
||||
from haystack.dataclasses import Document
|
||||
|
||||
doc = Document(content="I love pizza!")
|
||||
|
||||
document_embedder = HuggingFaceAPIDocumentEmbedder(
|
||||
api_type="serverless_inference_api",
|
||||
api_params={"model": "BAAI/bge-small-en-v1.5"},
|
||||
token=Secret.from_token("<your-api-key>"),
|
||||
)
|
||||
|
||||
result = document_embedder.run([doc])
|
||||
print(result["documents"][0].embedding)
|
||||
|
||||
# [0.017020374536514282, -0.023255806416273117, ...]
|
||||
```
|
||||
|
||||
#### Using Paid Inference Endpoints
|
||||
|
||||
In this case, a private instance of the model is deployed by Hugging Face, and you typically pay per hour.
|
||||
|
||||
To understand how to spin up an Inference Endpoint, visit [Hugging Face documentation](https://huggingface.co/inference-endpoints/dedicated).
|
||||
|
||||
Additionally, in this case, you need to provide your Hugging Face token.
|
||||
The Embedder expects the `url` of your endpoint in `api_params`.
|
||||
|
||||
```python
|
||||
from haystack_integrations.components.embedders.huggingface_api import (
|
||||
HuggingFaceAPIDocumentEmbedder,
|
||||
)
|
||||
from haystack.utils import Secret
|
||||
from haystack.dataclasses import Document
|
||||
|
||||
doc = Document(content="I love pizza!")
|
||||
|
||||
document_embedder = HuggingFaceAPIDocumentEmbedder(
|
||||
api_type="inference_endpoints",
|
||||
api_params={"url": "<your-inference-endpoint-url>"},
|
||||
token=Secret.from_token("<your-api-key>"),
|
||||
)
|
||||
|
||||
result = document_embedder.run([doc])
|
||||
print(result["documents"][0].embedding)
|
||||
|
||||
# [0.017020374536514282, -0.023255806416273117, ...]
|
||||
```
|
||||
|
||||
#### Using Self-Hosted Text Embeddings Inference (TEI)
|
||||
|
||||
[Hugging Face Text Embeddings Inference](https://github.com/huggingface/text-embeddings-inference) is a toolkit for efficiently deploying and serving text embedding models.
|
||||
|
||||
While it powers the most recent versions of Serverless Inference API and Inference Endpoints, it can be used easily on-premise through Docker.
|
||||
|
||||
For example, you can run a TEI container as follows:
|
||||
|
||||
```shell
|
||||
model=BAAI/bge-large-en-v1.5
|
||||
revision=refs/pr/5
|
||||
volume=$PWD/data # share a volume with the Docker container to avoid downloading weights every run
|
||||
|
||||
docker run --gpus all -p 8080:80 -v $volume:/data --pull always ghcr.io/huggingface/text-embeddings-inference:1.2 --model-id $model --revision $revision
|
||||
```
|
||||
|
||||
For more information, refer to the [official TEI repository](https://github.com/huggingface/text-embeddings-inference).
|
||||
|
||||
The Embedder expects the `url` of your TEI instance in `api_params`.
|
||||
|
||||
```python
|
||||
from haystack_integrations.components.embedders.huggingface_api import (
|
||||
HuggingFaceAPIDocumentEmbedder,
|
||||
)
|
||||
from haystack.dataclasses import Document
|
||||
|
||||
doc = Document(content="I love pizza!")
|
||||
|
||||
document_embedder = HuggingFaceAPIDocumentEmbedder(
|
||||
api_type="text_embeddings_inference",
|
||||
api_params={"url": "http://localhost:8080"},
|
||||
)
|
||||
|
||||
result = document_embedder.run([doc])
|
||||
print(result["documents"][0].embedding)
|
||||
|
||||
# [0.017020374536514282, -0.023255806416273117, ...]
|
||||
```
|
||||
|
||||
### In a pipeline
|
||||
|
||||
```python
|
||||
from haystack import Document
|
||||
from haystack import Pipeline
|
||||
from haystack.document_stores.in_memory import InMemoryDocumentStore
|
||||
from haystack_integrations.components.embedders.huggingface_api import (
|
||||
HuggingFaceAPITextEmbedder,
|
||||
HuggingFaceAPIDocumentEmbedder,
|
||||
)
|
||||
from haystack.components.writers import DocumentWriter
|
||||
from haystack.components.retrievers.in_memory import InMemoryEmbeddingRetriever
|
||||
|
||||
document_store = InMemoryDocumentStore(embedding_similarity_function="cosine")
|
||||
|
||||
documents = [
|
||||
Document(content="My name is Wolfgang and I live in Berlin"),
|
||||
Document(content="I saw a black horse running"),
|
||||
Document(content="Germany has many big cities"),
|
||||
]
|
||||
|
||||
document_embedder = HuggingFaceAPIDocumentEmbedder(
|
||||
api_type="serverless_inference_api",
|
||||
api_params={"model": "BAAI/bge-small-en-v1.5"},
|
||||
)
|
||||
|
||||
indexing_pipeline = Pipeline()
|
||||
indexing_pipeline.add_component("document_embedder", document_embedder)
|
||||
indexing_pipeline.add_component(
|
||||
"doc_writer",
|
||||
DocumentWriter(document_store=document_store),
|
||||
)
|
||||
indexing_pipeline.connect("document_embedder", "doc_writer")
|
||||
indexing_pipeline.run({"document_embedder": {"documents": documents}})
|
||||
|
||||
text_embedder = HuggingFaceAPITextEmbedder(
|
||||
api_type="serverless_inference_api",
|
||||
api_params={"model": "BAAI/bge-small-en-v1.5"},
|
||||
)
|
||||
|
||||
query_pipeline = Pipeline()
|
||||
query_pipeline.add_component("text_embedder", text_embedder)
|
||||
query_pipeline.add_component(
|
||||
"retriever",
|
||||
InMemoryEmbeddingRetriever(document_store=document_store),
|
||||
)
|
||||
query_pipeline.connect("text_embedder.embedding", "retriever.query_embedding")
|
||||
|
||||
query = "Who lives in Berlin?"
|
||||
|
||||
result = query_pipeline.run({"text_embedder": {"text": query}})
|
||||
|
||||
print(result["retriever"]["documents"][0])
|
||||
|
||||
# Document(id=..., content: 'My name is Wolfgang and I live in Berlin', ...)
|
||||
```
|
||||
@@ -0,0 +1,190 @@
|
||||
---
|
||||
title: "HuggingFaceAPITextEmbedder"
|
||||
id: huggingfaceapitextembedder
|
||||
slug: "/huggingfaceapitextembedder"
|
||||
description: "Use this component to embed strings using various Hugging Face APIs."
|
||||
---
|
||||
|
||||
# HuggingFaceAPITextEmbedder
|
||||
|
||||
Use this component to embed strings using various Hugging Face APIs.
|
||||
|
||||
<div className="key-value-table">
|
||||
|
||||
| | |
|
||||
| --- | --- |
|
||||
| **Most common position in a pipeline** | Before an embedding [Retriever](../retrievers.mdx) in a query/RAG pipeline |
|
||||
| **Mandatory init variables** | `api_type`: The type of Hugging Face API to use <br /> <br />`api_params`: A dictionary with one of the following keys: <br /> <br />- `model`: Hugging Face model ID. Required when `api_type` is `SERVERLESS_INFERENCE_API`.**OR** - `url`: URL of the inference endpoint. Required when `api_type` is `INFERENCE_ENDPOINTS` or `TEXT_EMBEDDINGS_INFERENCE`. <br /> <br />`token`: The Hugging Face API token. Can be set with `HF_API_TOKEN` or `HF_TOKEN` env var. |
|
||||
| **Mandatory run variables** | `text`: A string |
|
||||
| **Output variables** | `embedding`: A list of float numbers |
|
||||
| **API reference** | [Hugging Face API](/reference/integrations-huggingface-api) |
|
||||
| **GitHub link** | https://github.com/deepset-ai/haystack-core-integrations/tree/main/integrations/huggingface_api |
|
||||
| **Package name** | `huggingface-api-haystack` |
|
||||
|
||||
</div>
|
||||
|
||||
## Overview
|
||||
|
||||
`HuggingFaceAPITextEmbedder` can be used to embed strings using different Hugging Face APIs:
|
||||
|
||||
- [Free Serverless Inference API](https://huggingface.co/inference-api)
|
||||
- [Paid Inference Endpoints](https://huggingface.co/inference-endpoints)
|
||||
- [Self-hosted Text Embeddings Inference](https://github.com/huggingface/text-embeddings-inference)
|
||||
|
||||
:::info
|
||||
This component should be used to embed plain text. To embed a list of documents, use [`HuggingFaceAPIDocumentEmbedder`](huggingfaceapidocumentembedder.mdx).
|
||||
:::
|
||||
|
||||
The component uses a `HF_API_TOKEN` environment variable by default. Otherwise, you can pass a Hugging Face API token at initialization with `token` – see code examples below.
|
||||
The token is needed:
|
||||
|
||||
- If you use the Serverless Inference API, or
|
||||
- If you use the Inference Endpoints.
|
||||
|
||||
## Usage
|
||||
|
||||
Install the `huggingface-api-haystack` package to use the `HuggingFaceAPITextEmbedder`:
|
||||
|
||||
```shell
|
||||
pip install huggingface-api-haystack
|
||||
```
|
||||
|
||||
Similarly to other text Embedders, this component allows adding prefixes (and postfixes) to include instructions.
|
||||
For more fine-grained details, refer to the component’s [API reference](/reference/integrations-huggingface-api#huggingfaceapitextembedder).
|
||||
|
||||
### On its own
|
||||
|
||||
#### Using Free Serverless Inference API
|
||||
|
||||
Formerly known as (free) Hugging Face Inference API, this API allows you to quickly experiment with many models hosted on the Hugging Face Hub, offloading the inference to Hugging Face servers. It’s rate-limited and not meant for production.
|
||||
|
||||
To use this API, you need a [free Hugging Face token](https://huggingface.co/settings/tokens).
|
||||
The Embedder expects the `model` in `api_params`.
|
||||
|
||||
```python
|
||||
from haystack_integrations.components.embedders.huggingface_api import (
|
||||
HuggingFaceAPITextEmbedder,
|
||||
)
|
||||
from haystack.utils import Secret
|
||||
|
||||
text_embedder = HuggingFaceAPITextEmbedder(
|
||||
api_type="serverless_inference_api",
|
||||
api_params={"model": "BAAI/bge-small-en-v1.5"},
|
||||
token=Secret.from_token("<your-api-key>"),
|
||||
)
|
||||
|
||||
print(text_embedder.run("I love pizza!"))
|
||||
|
||||
# {'embedding': [0.017020374536514282, -0.023255806416273117, ...]}
|
||||
```
|
||||
|
||||
#### Using Paid Inference Endpoints
|
||||
|
||||
In this case, a private instance of the model is deployed by Hugging Face, and you typically pay per hour.
|
||||
|
||||
To understand how to spin up an Inference Endpoint, visit [Hugging Face documentation](https://huggingface.co/inference-endpoints/dedicated).
|
||||
|
||||
Additionally, in this case, you need to provide your Hugging Face token.
|
||||
The Embedder expects the `url` of your endpoint in `api_params`.
|
||||
|
||||
```python
|
||||
from haystack_integrations.components.embedders.huggingface_api import (
|
||||
HuggingFaceAPITextEmbedder,
|
||||
)
|
||||
from haystack.utils import Secret
|
||||
|
||||
text_embedder = HuggingFaceAPITextEmbedder(
|
||||
api_type="inference_endpoints",
|
||||
api_params={"model": "BAAI/bge-small-en-v1.5"},
|
||||
token=Secret.from_token("<your-api-key>"),
|
||||
)
|
||||
|
||||
print(text_embedder.run("I love pizza!"))
|
||||
|
||||
# {'embedding': [0.017020374536514282, -0.023255806416273117, ...]}
|
||||
```
|
||||
|
||||
#### Using Self-Hosted Text Embeddings Inference (TEI)
|
||||
|
||||
[Hugging Face Text Embeddings Inference](https://github.com/huggingface/text-embeddings-inference) is a toolkit for efficiently deploying and serving text embedding models.
|
||||
|
||||
While it powers the most recent versions of Serverless Inference API and Inference Endpoints, it can be used easily on-premise through Docker.
|
||||
|
||||
For example, you can run a TEI container as follows:
|
||||
|
||||
```shell
|
||||
model=BAAI/bge-large-en-v1.5
|
||||
revision=refs/pr/5
|
||||
volume=$PWD/data # share a volume with the Docker container to avoid downloading weights every run
|
||||
|
||||
docker run --gpus all -p 8080:80 -v $volume:/data --pull always ghcr.io/huggingface/text-embeddings-inference:1.2 --model-id $model --revision $revision
|
||||
```
|
||||
|
||||
For more information, refer to the [official TEI repository](https://github.com/huggingface/text-embeddings-inference).
|
||||
|
||||
The Embedder expects the `url` of your TEI instance in `api_params`.
|
||||
|
||||
```python
|
||||
from haystack_integrations.components.embedders.huggingface_api import (
|
||||
HuggingFaceAPITextEmbedder,
|
||||
)
|
||||
from haystack.utils import Secret
|
||||
|
||||
text_embedder = HuggingFaceAPITextEmbedder(
|
||||
api_type="text_embeddings_inference",
|
||||
api_params={"url": "http://localhost:8080"},
|
||||
)
|
||||
|
||||
print(text_embedder.run("I love pizza!"))
|
||||
|
||||
# {'embedding': [0.017020374536514282, -0.023255806416273117, ...],
|
||||
```
|
||||
|
||||
### In a pipeline
|
||||
|
||||
```python
|
||||
from haystack import Document
|
||||
from haystack import Pipeline
|
||||
from haystack.document_stores.in_memory import InMemoryDocumentStore
|
||||
from haystack_integrations.components.embedders.huggingface_api import (
|
||||
HuggingFaceAPITextEmbedder,
|
||||
HuggingFaceAPIDocumentEmbedder,
|
||||
)
|
||||
from haystack.components.retrievers.in_memory import InMemoryEmbeddingRetriever
|
||||
|
||||
document_store = InMemoryDocumentStore(embedding_similarity_function="cosine")
|
||||
|
||||
documents = [
|
||||
Document(content="My name is Wolfgang and I live in Berlin"),
|
||||
Document(content="I saw a black horse running"),
|
||||
Document(content="Germany has many big cities"),
|
||||
]
|
||||
|
||||
document_embedder = HuggingFaceAPIDocumentEmbedder(
|
||||
api_type="serverless_inference_api",
|
||||
api_params={"model": "BAAI/bge-small-en-v1.5"},
|
||||
)
|
||||
documents_with_embeddings = document_embedder.run(documents)["documents"]
|
||||
document_store.write_documents(documents_with_embeddings)
|
||||
|
||||
text_embedder = HuggingFaceAPITextEmbedder(
|
||||
api_type="serverless_inference_api",
|
||||
api_params={"model": "BAAI/bge-small-en-v1.5"},
|
||||
)
|
||||
|
||||
query_pipeline = Pipeline()
|
||||
query_pipeline.add_component("text_embedder", text_embedder)
|
||||
query_pipeline.add_component(
|
||||
"retriever",
|
||||
InMemoryEmbeddingRetriever(document_store=document_store),
|
||||
)
|
||||
query_pipeline.connect("text_embedder.embedding", "retriever.query_embedding")
|
||||
|
||||
query = "Who lives in Berlin?"
|
||||
|
||||
result = query_pipeline.run({"text_embedder": {"text": query}})
|
||||
|
||||
print(result["retriever"]["documents"][0])
|
||||
|
||||
# Document(id=..., content: 'My name is Wolfgang and I live in Berlin', ...)
|
||||
```
|
||||
@@ -0,0 +1,138 @@
|
||||
---
|
||||
title: "JinaDocumentEmbedder"
|
||||
id: jinadocumentembedder
|
||||
slug: "/jinadocumentembedder"
|
||||
description: "This component computes the embeddings of a list of documents and stores the obtained vectors in the embedding field of each document. It uses Jina AI Embeddings models. The vectors computed by this component are necessary to perform embedding retrieval on a collection of documents. At retrieval time, the vector representing the query is compared with those of the documents to find the most similar or relevant documents."
|
||||
---
|
||||
|
||||
# JinaDocumentEmbedder
|
||||
|
||||
This component computes the embeddings of a list of documents and stores the obtained vectors in the embedding field of each document. It uses Jina AI Embeddings models. The vectors computed by this component are necessary to perform embedding retrieval on a collection of documents. At retrieval time, the vector representing the query is compared with those of the documents to find the most similar or relevant documents.
|
||||
|
||||
<div className="key-value-table">
|
||||
|
||||
| | |
|
||||
| --- | --- |
|
||||
| **Most common position in a pipeline** | Before a [`DocumentWriter`](../writers/documentwriter.mdx) in an indexing pipeline |
|
||||
| **Mandatory init variables** | `api_key`: The Jina API key. Can be set with `JINA_API_KEY` env var. |
|
||||
| **Mandatory run variables** | `documents`: A list of documents |
|
||||
| **Output variables** | `documents`: A list of documents (enriched with embeddings) <br /> <br />`meta`: A dictionary of metadata |
|
||||
| **API reference** | [Jina](/reference/integrations-jina) |
|
||||
| **GitHub link** | https://github.com/deepset-ai/haystack-core-integrations/tree/main/integrations/jina |
|
||||
| **Package name** | `jina-haystack` |
|
||||
|
||||
</div>
|
||||
|
||||
## Overview
|
||||
|
||||
`JinaDocumentEmbedder` enriches the metadata of documents with an embedding of their content. To embed a string, you should use the [`JinaTextEmbedder`](jinatextembedder.mdx). To see the list of compatible Jina Embeddings models, head to Jina AI’s [website](https://jina.ai/embeddings/). The default model for `JinaDocumentEmbedder` is `jina-embeddings-v2-base-en`.
|
||||
|
||||
To start using this integration with Haystack, install the package with:
|
||||
|
||||
```shell
|
||||
pip install jina-haystack
|
||||
```
|
||||
|
||||
The component uses a `JINA_API_KEY` environment variable by default. Otherwise, you can pass an API key at initialization with `api_key`:
|
||||
|
||||
```python
|
||||
embedder = JinaDocumentEmbedder(api_key=Secret.from_token("<your-api-key>"))
|
||||
```
|
||||
|
||||
To get a Jina Embeddings API key, head to https://jina.ai/embeddings/.
|
||||
|
||||
### Embedding Metadata
|
||||
|
||||
Text documents often come with a set of metadata. If they are distinctive and semantically meaningful, you can embed them along with the text of the document to improve retrieval.
|
||||
|
||||
You can do this easily by using the Document Embedder:
|
||||
|
||||
```python
|
||||
from haystack import Document
|
||||
from haystack_integrations.components.embedders.jina import JinaDocumentEmbedder
|
||||
|
||||
doc = Document(content="some text", meta={"title": "relevant title", "page number": 18})
|
||||
|
||||
embedder = JinaDocumentEmbedder(
|
||||
api_key=Secret.from_token("<your-api-key>"),
|
||||
meta_fields_to_embed=["title"],
|
||||
)
|
||||
|
||||
docs_w_embeddings = embedder.run(documents=[doc])["documents"]
|
||||
```
|
||||
|
||||
## Usage
|
||||
|
||||
### On its own
|
||||
|
||||
Here is how you can use the component on its own:
|
||||
|
||||
```python
|
||||
from haystack_integrations.components.embedders.jina import JinaDocumentEmbedder
|
||||
|
||||
doc = Document(content="I love pizza!")
|
||||
|
||||
document_embedder = JinaDocumentEmbedder(api_key=Secret.from_token("<your-api-key>"))
|
||||
|
||||
result = document_embedder.run([doc])
|
||||
print(result["documents"][0].embedding)
|
||||
|
||||
# [0.017020374536514282, -0.023255806416273117, ...]
|
||||
```
|
||||
|
||||
:::info
|
||||
We recommend setting JINA_API_KEY as an environment variable instead of setting it as a parameter.
|
||||
:::
|
||||
|
||||
### In a pipeline
|
||||
|
||||
```python
|
||||
from haystack import Pipeline
|
||||
from haystack.document_stores.in_memory import InMemoryDocumentStore
|
||||
from haystack_integrations.components.embedders.jina import JinaDocumentEmbedder
|
||||
from haystack_integrations.components.embedders.jina import JinaTextEmbedder
|
||||
from haystack.components.writers import DocumentWriter
|
||||
from haystack.components.retrievers.in_memory import InMemoryEmbeddingRetriever
|
||||
|
||||
document_store = InMemoryDocumentStore(embedding_similarity_function="cosine")
|
||||
|
||||
documents = [
|
||||
Document(content="My name is Wolfgang and I live in Berlin"),
|
||||
Document(content="I saw a black horse running"),
|
||||
Document(content="Germany has many big cities"),
|
||||
]
|
||||
|
||||
indexing_pipeline = Pipeline()
|
||||
indexing_pipeline.add_component(
|
||||
"embedder",
|
||||
JinaDocumentEmbedder(api_key=Secret.from_token("<your-api-key>")),
|
||||
)
|
||||
indexing_pipeline.add_component("writer", DocumentWriter(document_store=document_store))
|
||||
indexing_pipeline.connect("embedder", "writer")
|
||||
|
||||
indexing_pipeline.run({"embedder": {"documents": documents}})
|
||||
|
||||
query_pipeline = Pipeline()
|
||||
query_pipeline.add_component(
|
||||
"text_embedder",
|
||||
JinaTextEmbedder(api_key=Secret.from_token("<your-api-key>")),
|
||||
)
|
||||
query_pipeline.add_component(
|
||||
"retriever",
|
||||
InMemoryEmbeddingRetriever(document_store=document_store),
|
||||
)
|
||||
query_pipeline.connect("text_embedder.embedding", "retriever.query_embedding")
|
||||
|
||||
query = "Who lives in Berlin?"
|
||||
|
||||
result = query_pipeline.run({"text_embedder": {"text": query}})
|
||||
|
||||
print(result["retriever"]["documents"][0])
|
||||
|
||||
# Document(id=..., mimetype: 'text/plain',
|
||||
# text: 'My name is Wolfgang and I live in Berlin')
|
||||
```
|
||||
|
||||
## Additional References
|
||||
|
||||
🧑🍳 Cookbook: [Using the Jina-embeddings-v2-base-en model in a Haystack RAG pipeline for legal document analysis](https://haystack.deepset.ai/cookbook/jina-embeddings-v2-legal-analysis-rag)
|
||||
@@ -0,0 +1,168 @@
|
||||
---
|
||||
title: "JinaDocumentImageEmbedder"
|
||||
id: jinadocumentimageembedder
|
||||
slug: "/jinadocumentimageembedder"
|
||||
description: "`JinaDocumentImageEmbedder` computes the image embeddings of a list of documents and stores the obtained vectors in the embedding field of each document. It uses Jina embedding models with the ability to embed text and images into the same vector space."
|
||||
---
|
||||
|
||||
# JinaDocumentImageEmbedder
|
||||
|
||||
`JinaDocumentImageEmbedder` computes the image embeddings of a list of documents and stores the obtained vectors in the embedding field of each document. It uses Jina embedding models with the ability to embed text and images into the same vector space.
|
||||
|
||||
<div className="key-value-table">
|
||||
|
||||
| | |
|
||||
| --- | --- |
|
||||
| **Most common position in a pipeline** | Before a [`DocumentWriter`](../writers/documentwriter.mdx) in an indexing pipeline |
|
||||
| **Mandatory init variables** | `api_key`: The Jina API key. Can be set with `JINA_API_KEY` env var. |
|
||||
| **Mandatory run variables** | `documents`: A list of documents, with a meta field containing an image file path |
|
||||
| **Output variables** | `documents`: A list of documents (enriched with embeddings) |
|
||||
| **API reference** | [Jina](/reference/integrations-jina) |
|
||||
| **GitHub link** | [https://github.com/deepset-ai/haystack-core-integrations/tree/main/integrations/jina](https://github.com/deepset-ai/haystack-core-integrations/tree/main/integrations/cohere) |
|
||||
| **Package name** | `jina-haystack` |
|
||||
|
||||
</div>
|
||||
|
||||
## Overview
|
||||
|
||||
`JinaDocumentImageEmbedder` expects a list of documents containing an image or a PDF file path in a meta field. The meta field can be specified with the `file_path_meta_field` init parameter of this component.
|
||||
|
||||
The embedder efficiently loads the images, computes the embeddings using a Jina model, and stores each of them in the `embedding` field of the document.
|
||||
|
||||
`JinaDocumentImageEmbedder` is commonly used in indexing pipelines. At retrieval time, you need to use the same model with a `JinaTextEmbedder` to embed the query, before using an Embedding Retriever.
|
||||
|
||||
This component is compatible with Jina multimodal embedding models:
|
||||
|
||||
- `jina-clip-v1`
|
||||
- `jina-clip-v2` (default)
|
||||
- `jina-embeddings-v4` (non-commercial research only)
|
||||
|
||||
### Installation
|
||||
|
||||
To start using this integration with Haystack, install the package with:
|
||||
|
||||
```shell
|
||||
pip install jina-haystack
|
||||
```
|
||||
|
||||
### Authentication
|
||||
|
||||
The component uses a `JINA_API_KEY` environment variable by default. Otherwise, you can pass an API key at initialization with a [Secret](../../concepts/secret-management.mdx) and `Secret.from_token` method:
|
||||
|
||||
```python
|
||||
embedder = JinaDocumentImageEmbedder(api_key=Secret.from_token("<your-api-key>"))
|
||||
```
|
||||
|
||||
To get a Cohere API key, head over to https://jina.ai/embeddings/.
|
||||
|
||||
## Usage
|
||||
|
||||
### On its own
|
||||
|
||||
Remember to set `JINA_API_KEY` as an environment variable first.
|
||||
|
||||
```python
|
||||
from haystack import Document
|
||||
from haystack_integrations.components.embedders.jina import JinaDocumentImageEmbedder
|
||||
|
||||
embedder = JinaDocumentImageEmbedder(model="jina-clip-v2")
|
||||
|
||||
documents = [
|
||||
Document(content="A photo of a cat", meta={"file_path": "cat.jpg"}),
|
||||
Document(content="A photo of a dog", meta={"file_path": "dog.jpg"}),
|
||||
]
|
||||
|
||||
result = embedder.run(documents=documents)
|
||||
documents_with_embeddings = result["documents"]
|
||||
print(documents_with_embeddings)
|
||||
|
||||
# [Document(id=...,
|
||||
# content='A photo of a cat',
|
||||
# meta={'file_path': 'cat.jpg',
|
||||
# 'embedding_source': {'type': 'image', 'file_path_meta_field': 'file_path'}},
|
||||
# embedding=vector of size 1024),
|
||||
# ...]
|
||||
```
|
||||
|
||||
### In a pipeline
|
||||
|
||||
In this example, we can see an indexing pipeline with 3 components:
|
||||
|
||||
- `ImageFileToDocument` Converter that creates empty documents with a reference to an image in the `meta.file_path` field.
|
||||
- `JinaDocumentImageEmbedder` that loads the images, computes embeddings and store them in documents. Here, we set the `image_size` parameter to resize the image to fit within the specified dimensions while maintaining aspect ratio. This reduces API usage.
|
||||
- `DocumentWriter` that writes the documents in the `InMemoryDocumentStore`.
|
||||
|
||||
There is also a multimodal retrieval pipeline, composed of a `JinaTextEmbedder` (using the same model as before) and an `InMemoryEmbeddingRetriever`.
|
||||
|
||||
```python
|
||||
from haystack import Pipeline
|
||||
from haystack.components.converters.image import ImageFileToDocument
|
||||
from haystack.components.retrievers.in_memory import InMemoryEmbeddingRetriever
|
||||
from haystack.components.writers import DocumentWriter
|
||||
from haystack.document_stores.in_memory import InMemoryDocumentStore
|
||||
|
||||
from haystack_integrations.components.embedders.jina import (
|
||||
JinaDocumentImageEmbedder,
|
||||
JinaTextEmbedder,
|
||||
)
|
||||
|
||||
document_store = InMemoryDocumentStore()
|
||||
|
||||
# Indexing pipeline
|
||||
indexing_pipeline = Pipeline()
|
||||
indexing_pipeline.add_component("image_converter", ImageFileToDocument())
|
||||
indexing_pipeline.add_component(
|
||||
"embedder",
|
||||
JinaDocumentImageEmbedder(model="jina-clip-v2", image_size=(200, 200)),
|
||||
)
|
||||
indexing_pipeline.add_component("writer", DocumentWriter(document_store=document_store))
|
||||
indexing_pipeline.connect("image_converter", "embedder")
|
||||
indexing_pipeline.connect("embedder", "writer")
|
||||
|
||||
indexing_pipeline.run(data={"image_converter": {"sources": ["dog.jpg", "cat.jpg"]}})
|
||||
|
||||
# Multimodal retrieval pipeline
|
||||
retrieval_pipeline = Pipeline()
|
||||
retrieval_pipeline.add_component("embedder", JinaTextEmbedder(model="jina-clip-v2"))
|
||||
retrieval_pipeline.add_component(
|
||||
"retriever",
|
||||
InMemoryEmbeddingRetriever(document_store=document_store, top_k=2),
|
||||
)
|
||||
retrieval_pipeline.connect("embedder.embedding", "retriever.query_embedding")
|
||||
|
||||
result = retrieval_pipeline.run(data={"text": "man's best friend"})
|
||||
print(result)
|
||||
|
||||
# {
|
||||
# 'retriever': {
|
||||
# 'documents': [
|
||||
# Document(
|
||||
# id=0c96...,
|
||||
# meta={
|
||||
# 'file_path': 'dog.jpg',
|
||||
# 'embedding_source': {
|
||||
# 'type': 'image',
|
||||
# 'file_path_meta_field': 'file_path'
|
||||
# }
|
||||
# },
|
||||
# score=0.246
|
||||
# ),
|
||||
# Document(
|
||||
# id=5e76...,
|
||||
# meta={
|
||||
# 'file_path': 'cat.jpg',
|
||||
# 'embedding_source': {
|
||||
# 'type': 'image',
|
||||
# 'file_path_meta_field': 'file_path'
|
||||
# }
|
||||
# },
|
||||
# score=0.199
|
||||
# )
|
||||
# ]
|
||||
# }
|
||||
# }
|
||||
```
|
||||
|
||||
## Additional References
|
||||
|
||||
:notebook: Tutorial: [Creating Vision+Text RAG Pipelines](https://haystack.deepset.ai/tutorials/46_multimodal_rag)
|
||||
@@ -0,0 +1,113 @@
|
||||
---
|
||||
title: "JinaTextEmbedder"
|
||||
id: jinatextembedder
|
||||
slug: "/jinatextembedder"
|
||||
description: "This component transforms a string into a vector that captures its semantics using a Jina Embeddings model. When you perform embedding retrieval, you use this component to transform your query into a vector. Then, the embedding Retriever looks for similar or relevant documents."
|
||||
---
|
||||
|
||||
# JinaTextEmbedder
|
||||
|
||||
This component transforms a string into a vector that captures its semantics using a Jina Embeddings model. When you perform embedding retrieval, you use this component to transform your query into a vector. Then, the embedding Retriever looks for similar or relevant documents.
|
||||
|
||||
<div className="key-value-table">
|
||||
|
||||
| | |
|
||||
| --- | --- |
|
||||
| **Most common position in a pipeline** | Before an embedding [Retriever](../retrievers.mdx) in a query/RAG pipeline |
|
||||
| **Mandatory init variables** | `api_key`: The Jina API key. Can be set with `JINA_API_KEY` env var. |
|
||||
| **Mandatory run variables** | `text`: A string |
|
||||
| **Output variables** | `embedding`: A list of float numbers <br /> <br />`meta`: A dictionary of metadata |
|
||||
| **API reference** | [Jina](/reference/integrations-jina) |
|
||||
| **GitHub link** | https://github.com/deepset-ai/haystack-core-integrations/tree/main/integrations/jina |
|
||||
| **Package name** | `jina-haystack` |
|
||||
|
||||
</div>
|
||||
|
||||
## Overview
|
||||
|
||||
`JinaTextEmbedder` embeds a simple string (such as a query) into a vector. For embedding lists of documents, use the use the [`JinaDocumentEmbedder`](jinadocumentembedder.mdx), which enriches the document with the computed embedding, also known as vector. To see the list of compatible Jina Embeddings models, head to Jina AI’s [website](https://jina.ai/embeddings/). The default model for `JinaTextEmbedder` is `jina-embeddings-v2-base-en`.
|
||||
|
||||
To start using this integration with Haystack, install the package with:
|
||||
|
||||
```shell
|
||||
pip install jina-haystack
|
||||
```
|
||||
|
||||
The component uses a `JINA_API_KEY` environment variable by default. Otherwise, you can pass an API key at initialization with `api_key`:
|
||||
|
||||
```python
|
||||
embedder = JinaTextEmbedder(api_key=Secret.from_token("<your-api-key>"))
|
||||
```
|
||||
|
||||
To get a Jina Embeddings API key, head to https://jina.ai/embeddings/.
|
||||
|
||||
## Usage
|
||||
|
||||
### On its own
|
||||
|
||||
Here is how you can use the component on its own:
|
||||
|
||||
```python
|
||||
from haystack_integrations.components.embedders.jina import JinaTextEmbedder
|
||||
|
||||
text_to_embed = "I love pizza!"
|
||||
|
||||
text_embedder = JinaTextEmbedder(api_key=Secret.from_token("<your-api-key>"))
|
||||
|
||||
print(text_embedder.run(text_to_embed))
|
||||
|
||||
# {'embedding': [0.017020374536514282, -0.023255806416273117, ...],
|
||||
# 'meta': {'model': 'text-embedding-ada-002-v2',
|
||||
# 'usage': {'prompt_tokens': 4, 'total_tokens': 4}}}
|
||||
```
|
||||
|
||||
:::info
|
||||
We recommend setting JINA_API_KEY as an environment variable instead of setting it as a parameter.
|
||||
:::
|
||||
|
||||
### In a pipeline
|
||||
|
||||
```python
|
||||
from haystack import Document
|
||||
from haystack import Pipeline
|
||||
from haystack.document_stores.in_memory import InMemoryDocumentStore
|
||||
from haystack_integrations.components.embedders.jina import JinaDocumentEmbedder
|
||||
from haystack_integrations.components.embedders.jina import JinaTextEmbedder
|
||||
from haystack.components.retrievers.in_memory import InMemoryEmbeddingRetriever
|
||||
|
||||
document_store = InMemoryDocumentStore(embedding_similarity_function="cosine")
|
||||
|
||||
documents = [
|
||||
Document(content="My name is Wolfgang and I live in Berlin"),
|
||||
Document(content="I saw a black horse running"),
|
||||
Document(content="Germany has many big cities"),
|
||||
]
|
||||
|
||||
document_embedder = JinaDocumentEmbedder(api_key=Secret.from_token("<your-api-key>"))
|
||||
documents_with_embeddings = document_embedder.run(documents)["documents"]
|
||||
document_store.write_documents(documents_with_embeddings)
|
||||
|
||||
query_pipeline = Pipeline()
|
||||
query_pipeline.add_component(
|
||||
"text_embedder",
|
||||
JinaTextEmbedder(api_key=Secret.from_token("<your-api-key>")),
|
||||
)
|
||||
query_pipeline.add_component(
|
||||
"retriever",
|
||||
InMemoryEmbeddingRetriever(document_store=document_store),
|
||||
)
|
||||
query_pipeline.connect("text_embedder.embedding", "retriever.query_embedding")
|
||||
|
||||
query = "Who lives in Berlin?"
|
||||
|
||||
result = query_pipeline.run({"text_embedder": {"text": query}})
|
||||
|
||||
print(result["retriever"]["documents"][0])
|
||||
|
||||
# Document(id=..., mimetype: 'text/plain',
|
||||
# text: 'My name is Wolfgang and I live in Berlin')
|
||||
```
|
||||
|
||||
## Additional References
|
||||
|
||||
🧑🍳 Cookbook: [Using the Jina-embeddings-v2-base-en model in a Haystack RAG pipeline for legal document analysis](https://haystack.deepset.ai/cookbook/jina-embeddings-v2-legal-analysis-rag)
|
||||
@@ -0,0 +1,111 @@
|
||||
---
|
||||
title: "MistralDocumentEmbedder"
|
||||
id: mistraldocumentembedder
|
||||
slug: "/mistraldocumentembedder"
|
||||
description: "This component computes the embeddings of a list of documents using the Mistral API and models."
|
||||
---
|
||||
|
||||
# MistralDocumentEmbedder
|
||||
|
||||
This component computes the embeddings of a list of documents using the Mistral API and models.
|
||||
|
||||
<div className="key-value-table">
|
||||
|
||||
| | |
|
||||
| --- | --- |
|
||||
| **Most common position in a pipeline** | Before a [`DocumentWriter`](../writers/documentwriter.mdx) in an indexing pipeline |
|
||||
| **Mandatory init variables** | `api_key`: The Mistral API key. Can be set with `MISTRAL_API_KEY` env var. |
|
||||
| **Mandatory run variables** | `documents`: A list of documents to be embedded |
|
||||
| **Output variables** | `documents`: A list of documents (enriched with embeddings) <br /> <br />`meta`: A dictionary of metadata strings |
|
||||
| **API reference** | [Mistral](/reference/integrations-mistral) |
|
||||
| **GitHub link** | https://github.com/deepset-ai/haystack-core-integrations/tree/main/integrations/mistral |
|
||||
| **Package name** | `mistral-haystack` |
|
||||
|
||||
</div>
|
||||
|
||||
This component should be used to embed a list of Documents. To embed a string, use the [`MistralTextEmbedder`](mistraltextembedder.mdx).
|
||||
|
||||
## Overview
|
||||
|
||||
`MistralDocumentEmbedder` computes the embeddings of a list of documents and stores the obtained vectors in the embedding field of each document. It uses the Mistral API and its embedding models.
|
||||
|
||||
The component currently supports the `mistral-embed` embedding model. The list of all supported models can be found in Mistral’s [embedding models documentation](https://docs.mistral.ai/platform/endpoints/#embedding-models).
|
||||
|
||||
To start using this integration with Haystack, install it with:
|
||||
|
||||
```shell
|
||||
pip install mistral-haystack
|
||||
```
|
||||
|
||||
`MistralDocumentEmbedder` needs a Mistral API key to work. It uses an `MISTRAL_API_KEY` environment variable by default. Otherwise, you can pass an API key at initialization with `api_key`:
|
||||
|
||||
```python
|
||||
embedder = MistralDocumentEmbedder(
|
||||
api_key=Secret.from_token("<your-api-key>"),
|
||||
model="mistral-embed",
|
||||
)
|
||||
```
|
||||
|
||||
## Usage
|
||||
|
||||
### On its own
|
||||
|
||||
Remember first to set the`MISTRAL_API_KEY` as an environment variable or pass it in directly.
|
||||
|
||||
Here is how you can use the component on its own:
|
||||
|
||||
```python
|
||||
from haystack import Document
|
||||
from haystack_integrations.components.embedders.mistral.document_embedder import (
|
||||
MistralDocumentEmbedder,
|
||||
)
|
||||
|
||||
doc = Document(content="I love pizza!")
|
||||
|
||||
embedder = MistralDocumentEmbedder(
|
||||
api_key=Secret.from_token("<your-api-key>"),
|
||||
model="mistral-embed",
|
||||
)
|
||||
|
||||
result = embedder.run([doc])
|
||||
print(result["documents"][0].embedding)
|
||||
# [-0.453125, 1.2236328, 2.0058594, 0.67871094...]
|
||||
```
|
||||
|
||||
### In a pipeline
|
||||
|
||||
Below is an example of the `MistralDocumentEmbedder` in an indexing pipeline. We are indexing the contents of a webpage into an `InMemoryDocumentStore`.
|
||||
|
||||
```python
|
||||
from haystack import Pipeline
|
||||
from haystack.components.converters import HTMLToDocument
|
||||
from haystack.components.fetchers import LinkContentFetcher
|
||||
from haystack.components.preprocessors import DocumentSplitter
|
||||
from haystack.components.writers import DocumentWriter
|
||||
from haystack.document_stores.in_memory import InMemoryDocumentStore
|
||||
from haystack_integrations.components.embedders.mistral.document_embedder import (
|
||||
MistralDocumentEmbedder,
|
||||
)
|
||||
|
||||
document_store = InMemoryDocumentStore()
|
||||
fetcher = LinkContentFetcher()
|
||||
converter = HTMLToDocument()
|
||||
chunker = DocumentSplitter()
|
||||
embedder = MistralDocumentEmbedder()
|
||||
writer = DocumentWriter(document_store=document_store)
|
||||
|
||||
indexing = Pipeline()
|
||||
|
||||
indexing.add_component(name="fetcher", instance=fetcher)
|
||||
indexing.add_component(name="converter", instance=converter)
|
||||
indexing.add_component(name="chunker", instance=chunker)
|
||||
indexing.add_component(name="embedder", instance=embedder)
|
||||
indexing.add_component(name="writer", instance=writer)
|
||||
|
||||
indexing.connect("fetcher", "converter")
|
||||
indexing.connect("converter", "chunker")
|
||||
indexing.connect("chunker", "embedder")
|
||||
indexing.connect("embedder", "writer")
|
||||
|
||||
indexing.run(data={"fetcher": {"urls": ["https://mistral.ai/news/la-plateforme/"]}})
|
||||
```
|
||||
@@ -0,0 +1,170 @@
|
||||
---
|
||||
title: "MistralTextEmbedder"
|
||||
id: mistraltextembedder
|
||||
slug: "/mistraltextembedder"
|
||||
description: "This component transforms a string into a vector using the Mistral API and models. Use it for embedding retrieval to transform your query into an embedding."
|
||||
---
|
||||
|
||||
# MistralTextEmbedder
|
||||
|
||||
This component transforms a string into a vector using the Mistral API and models. Use it for embedding retrieval to transform your query into an embedding.
|
||||
|
||||
<div className="key-value-table">
|
||||
|
||||
| | |
|
||||
| --- | --- |
|
||||
| **Most common position in a pipeline** | Before an embedding [Retriever](../retrievers.mdx) in a query/RAG pipeline |
|
||||
| **Mandatory init variables** | `api_key`: The Mistral API key. Can be set with `MISTRAL_API_KEY` env var. |
|
||||
| **Mandatory run variables** | `text`: A string |
|
||||
| **Output variables** | `embedding`: A list of float numbers (vectors) <br /> <br />`meta`: A dictionary of metadata strings |
|
||||
| **API reference** | [Mistral](/reference/integrations-mistral) |
|
||||
| **GitHub link** | https://github.com/deepset-ai/haystack-core-integrations/tree/main/integrations/mistral |
|
||||
| **Package name** | `mistral-haystack` |
|
||||
|
||||
</div>
|
||||
|
||||
Use `MistalTextEmbedder` to embed a simple string (such as a query) into a vector. For embedding lists of documents, use the [`MistralDocumentEmbedder`](mistraldocumentembedder.mdx), which enriches the document with the computed embedding, also known as vector.
|
||||
|
||||
## Overview
|
||||
|
||||
`MistralTextEmbedder` transforms a string into a vector that captures its semantics using a Mistral embedding model.
|
||||
|
||||
The component currently supports the `mistral-embed` embedding model. The list of all supported models can be found in Mistral’s [embedding models documentation](https://docs.mistral.ai/platform/endpoints/#embedding-models).
|
||||
|
||||
To start using this integration with Haystack, install it with:
|
||||
|
||||
```shell
|
||||
pip install mistral-haystack
|
||||
```
|
||||
|
||||
`MistralTextEmbedder` needs a Mistral API key to work. It uses a `MISTRAL_API_KEY` environment variable by default. Otherwise, you can pass an API key at initialization with `api_key`:
|
||||
|
||||
```python
|
||||
embedder = MistralTextEmbedder(
|
||||
api_key=Secret.from_token("<your-api-key>"),
|
||||
model="mistral-embed",
|
||||
)
|
||||
```
|
||||
|
||||
## Usage
|
||||
|
||||
### On its own
|
||||
|
||||
Remember to set the`MISTRAL_API_KEY` as an environment variable first or pass it in directly.
|
||||
|
||||
Here is how you can use the component on its own:
|
||||
|
||||
```python
|
||||
|
||||
from haystack_integrations.components.embedders.mistral.text_embedder import (
|
||||
MistralTextEmbedder,
|
||||
)
|
||||
|
||||
embedder = MistralTextEmbedder(
|
||||
api_key=Secret.from_token("<your-api-key>"),
|
||||
model="mistral-embed",
|
||||
)
|
||||
|
||||
result = embedder.run(text="How can I ise the Mistral embedding models with Haystack?")
|
||||
|
||||
print(result["embedding"])
|
||||
# [-0.0015687942504882812, 0.052154541015625, 0.037109375...]
|
||||
```
|
||||
|
||||
### In a pipeline
|
||||
|
||||
Below is an example of the `MistralTextEmbedder` in a document search pipeline. We are building this pipeline on top of an `InMemoryDocumentStore` where we index the contents of two URLs.
|
||||
|
||||
```python
|
||||
from haystack import Document, Pipeline
|
||||
from haystack.utils import Secret
|
||||
from haystack.components.builders.chat_prompt_builder import ChatPromptBuilder
|
||||
from haystack.components.fetchers import LinkContentFetcher
|
||||
from haystack.components.converters import HTMLToDocument
|
||||
from haystack.components.retrievers.in_memory import InMemoryEmbeddingRetriever
|
||||
from haystack.components.writers import DocumentWriter
|
||||
from haystack.document_stores.in_memory import InMemoryDocumentStore
|
||||
from haystack_integrations.components.embedders.mistral.document_embedder import (
|
||||
MistralDocumentEmbedder,
|
||||
)
|
||||
from haystack_integrations.components.embedders.mistral.text_embedder import (
|
||||
MistralTextEmbedder,
|
||||
)
|
||||
from haystack.components.generators.chat import OpenAIChatGenerator
|
||||
from haystack.dataclasses import ChatMessage
|
||||
|
||||
# Initialize document store
|
||||
document_store = InMemoryDocumentStore(embedding_similarity_function="cosine")
|
||||
|
||||
# Indexing components
|
||||
fetcher = LinkContentFetcher()
|
||||
converter = HTMLToDocument()
|
||||
embedder = MistralDocumentEmbedder()
|
||||
writer = DocumentWriter(document_store=document_store)
|
||||
|
||||
indexing = Pipeline()
|
||||
indexing.add_component(name="fetcher", instance=fetcher)
|
||||
indexing.add_component(name="converter", instance=converter)
|
||||
indexing.add_component(name="embedder", instance=embedder)
|
||||
indexing.add_component(name="writer", instance=writer)
|
||||
|
||||
indexing.connect("fetcher", "converter")
|
||||
indexing.connect("converter", "embedder")
|
||||
indexing.connect("embedder", "writer")
|
||||
|
||||
indexing.run(
|
||||
data={
|
||||
"fetcher": {
|
||||
"urls": [
|
||||
"https://docs.mistral.ai/self-deployment/cloudflare/",
|
||||
"https://docs.mistral.ai/platform/endpoints/",
|
||||
],
|
||||
},
|
||||
},
|
||||
)
|
||||
|
||||
# Retrieval components
|
||||
text_embedder = MistralTextEmbedder()
|
||||
retriever = InMemoryEmbeddingRetriever(document_store=document_store)
|
||||
|
||||
# Define prompt template
|
||||
prompt_template = [
|
||||
ChatMessage.from_system("You are a helpful assistant."),
|
||||
ChatMessage.from_user(
|
||||
"Given the retrieved documents, answer the question.\nDocuments:\n"
|
||||
"{% for document in documents %}{{ document.content }}{% endfor %}\n"
|
||||
"Question: {{ query }}\nAnswer:",
|
||||
),
|
||||
]
|
||||
|
||||
prompt_builder = ChatPromptBuilder(
|
||||
template=prompt_template,
|
||||
required_variables={"query", "documents"},
|
||||
)
|
||||
llm = OpenAIChatGenerator(
|
||||
model="gpt-4o-mini",
|
||||
api_key=Secret.from_token("<your-api-key>"),
|
||||
)
|
||||
|
||||
doc_search = Pipeline()
|
||||
doc_search.add_component("text_embedder", text_embedder)
|
||||
doc_search.add_component("retriever", retriever)
|
||||
doc_search.add_component("prompt_builder", prompt_builder)
|
||||
doc_search.add_component("llm", llm)
|
||||
|
||||
doc_search.connect("text_embedder.embedding", "retriever.query_embedding")
|
||||
doc_search.connect("retriever.documents", "prompt_builder.documents")
|
||||
doc_search.connect("prompt_builder.prompt", "llm.messages")
|
||||
|
||||
query = "How can I deploy Mistral models with Cloudflare?"
|
||||
|
||||
result = doc_search.run(
|
||||
{
|
||||
"text_embedder": {"text": query},
|
||||
"retriever": {"top_k": 1},
|
||||
"prompt_builder": {"query": query},
|
||||
},
|
||||
)
|
||||
|
||||
print(result["llm"]["replies"])
|
||||
```
|
||||
@@ -0,0 +1,94 @@
|
||||
---
|
||||
title: "MockDocumentEmbedder"
|
||||
id: mockdocumentembedder
|
||||
slug: "/mockdocumentembedder"
|
||||
description: "A Document Embedder that returns deterministic embeddings without calling any API, for tests and quick prototypes."
|
||||
---
|
||||
|
||||
# MockDocumentEmbedder
|
||||
|
||||
A Document Embedder that returns deterministic embeddings without calling any API, for tests and quick prototypes.
|
||||
|
||||
<div className="key-value-table">
|
||||
|
||||
| | |
|
||||
| --- | --- |
|
||||
| **Most common position in a pipeline** | In place of a real Document Embedder, in tests and prototypes |
|
||||
| **Mandatory init variables** | None |
|
||||
| **Mandatory run variables** | `documents`: A list of documents |
|
||||
| **Output variables** | `documents`: A list of documents enriched with embeddings <br /> <br />`meta`: A dictionary of metadata |
|
||||
| **API reference** | [Embedders](/reference/embedders-api) |
|
||||
| **GitHub link** | https://github.com/deepset-ai/haystack/blob/main/haystack/components/embedders/mock_document_embedder.py |
|
||||
| **Package name** | `haystack-ai` |
|
||||
|
||||
</div>
|
||||
|
||||
## Overview
|
||||
|
||||
`MockDocumentEmbedder` is a deterministic, zero-cost drop-in replacement for real Document Embedders such as `OpenAIDocumentEmbedder`. It implements `run`, `run_async`, and serialization like any other embedder but never contacts an external service, which makes it ideal for unit tests, smoke tests, and quick prototypes.
|
||||
|
||||
The embedding is selected based on how the component is configured:
|
||||
|
||||
- **Deterministic (default)**: With no configuration, each document's embedding is derived from a stable hash of its prepared text. The same text always yields the same unit-length embedding, and different texts yield different embeddings, so the mock works in retrieval pipelines and is reproducible across runs and processes.
|
||||
- **Fixed embedding**: Pass an `embedding` vector. The same vector is assigned to every document.
|
||||
- **Dynamic embedding**: Pass an `embedding_fn` callable that receives the prepared text of a document and returns the embedding. To support serialization, pass a named function.
|
||||
|
||||
`embedding` and `embedding_fn` are mutually exclusive.
|
||||
|
||||
Further optional parameters:
|
||||
|
||||
- `dimension`: The number of dimensions of the deterministic embedding. Defaults to `768`. Ignored when `embedding` or `embedding_fn` is provided, since their length is determined by the value or callable.
|
||||
- `model`: The model name reported in the metadata. Defaults to `"mock-model"`.
|
||||
- `meta`: Additional metadata merged into the output `meta`.
|
||||
- `prefix` / `suffix`: Strings added to the beginning and end of each text before embedding, mirroring real embedders.
|
||||
- `meta_fields_to_embed` / `embedding_separator`: Like real Document Embedders, the metadata fields listed in `meta_fields_to_embed` are concatenated with the document content before embedding, so the deterministic embedding reflects the embedded metadata.
|
||||
- `progress_bar`: Accepted for interface compatibility with real Document Embedders and ignored.
|
||||
|
||||
:::info
|
||||
The deterministic embeddings are derived from a hash: identical texts get identical vectors and the similarity between different texts is stable but arbitrary. For exact-match retrieval in tests this is exactly what you want. Do not expect semantically similar texts to end up close together.
|
||||
:::
|
||||
|
||||
Use `MockDocumentEmbedder` for documents and its counterpart [`MockTextEmbedder`](mocktextembedder.mdx) for queries. With the default deterministic mode, a query whose text matches a document's content produces the same vector, so the document is retrieved as the top hit.
|
||||
|
||||
## Usage
|
||||
|
||||
### On its own
|
||||
|
||||
```python
|
||||
from haystack import Document
|
||||
from haystack.components.embedders import MockDocumentEmbedder
|
||||
|
||||
embedder = MockDocumentEmbedder(dimension=8)
|
||||
result = embedder.run([Document(content="I love pizza!")])
|
||||
print(result["documents"][0].embedding) # a deterministic list of 8 floats
|
||||
```
|
||||
|
||||
### In a pipeline
|
||||
|
||||
Use it in an indexing pipeline exactly like a real Document Embedder — no API key needed:
|
||||
|
||||
```python
|
||||
from haystack import Document, Pipeline
|
||||
from haystack.components.embedders import MockDocumentEmbedder
|
||||
from haystack.components.writers import DocumentWriter
|
||||
from haystack.document_stores.in_memory import InMemoryDocumentStore
|
||||
|
||||
document_store = InMemoryDocumentStore()
|
||||
|
||||
indexing_pipeline = Pipeline()
|
||||
indexing_pipeline.add_component("embedder", MockDocumentEmbedder(dimension=8))
|
||||
indexing_pipeline.add_component("writer", DocumentWriter(document_store=document_store))
|
||||
indexing_pipeline.connect("embedder.documents", "writer.documents")
|
||||
|
||||
indexing_pipeline.run(
|
||||
{
|
||||
"embedder": {
|
||||
"documents": [
|
||||
Document(content="My name is Wolfgang and I live in Berlin"),
|
||||
Document(content="I saw a black horse running"),
|
||||
],
|
||||
},
|
||||
},
|
||||
)
|
||||
print(document_store.count_documents()) # 2
|
||||
```
|
||||
@@ -0,0 +1,94 @@
|
||||
---
|
||||
title: "MockTextEmbedder"
|
||||
id: mocktextembedder
|
||||
slug: "/mocktextembedder"
|
||||
description: "A Text Embedder that returns deterministic embeddings without calling any API, for tests and quick prototypes."
|
||||
---
|
||||
|
||||
# MockTextEmbedder
|
||||
|
||||
A Text Embedder that returns deterministic embeddings without calling any API, for tests and quick prototypes.
|
||||
|
||||
<div className="key-value-table">
|
||||
|
||||
| | |
|
||||
| --- | --- |
|
||||
| **Most common position in a pipeline** | In place of a real Text Embedder, in tests and prototypes |
|
||||
| **Mandatory init variables** | None |
|
||||
| **Mandatory run variables** | `text`: A string |
|
||||
| **Output variables** | `embedding`: A list of float numbers <br /> <br />`meta`: A dictionary of metadata |
|
||||
| **API reference** | [Embedders](/reference/embedders-api) |
|
||||
| **GitHub link** | https://github.com/deepset-ai/haystack/blob/main/haystack/components/embedders/mock_text_embedder.py |
|
||||
| **Package name** | `haystack-ai` |
|
||||
|
||||
</div>
|
||||
|
||||
## Overview
|
||||
|
||||
`MockTextEmbedder` is a deterministic, zero-cost drop-in replacement for real Text Embedders such as `OpenAITextEmbedder`. It implements `run`, `run_async`, and serialization like any other embedder but never contacts an external service, which makes it ideal for unit tests, smoke tests, and quick prototypes.
|
||||
|
||||
The embedding is selected based on how the component is configured:
|
||||
|
||||
- **Deterministic (default)**: With no configuration, the embedding is derived from a stable hash of the input text. The same text always yields the same unit-length embedding, and different texts yield different embeddings, so the mock works in retrieval pipelines and is reproducible across runs and processes.
|
||||
- **Fixed embedding**: Pass an `embedding` vector. The same vector is returned for every input.
|
||||
- **Dynamic embedding**: Pass an `embedding_fn` callable that receives the prepared text (after `prefix`/`suffix` are applied) and returns the embedding. To support serialization, pass a named function.
|
||||
|
||||
`embedding` and `embedding_fn` are mutually exclusive.
|
||||
|
||||
Further optional parameters:
|
||||
|
||||
- `dimension`: The number of dimensions of the deterministic embedding. Defaults to `768`. Ignored when `embedding` or `embedding_fn` is provided, since their length is determined by the value or callable.
|
||||
- `model`: The model name reported in the metadata. Defaults to `"mock-model"`.
|
||||
- `meta`: Additional metadata merged into the output `meta`.
|
||||
- `prefix` / `suffix`: Strings added to the beginning and end of the text before embedding, mirroring real embedders.
|
||||
|
||||
:::info
|
||||
The deterministic embeddings are derived from a hash: identical texts get identical vectors and the similarity between different texts is stable but arbitrary. For exact-match retrieval in tests this is exactly what you want. Do not expect semantically similar texts to end up close together.
|
||||
:::
|
||||
|
||||
Use `MockTextEmbedder` for queries and its counterpart [`MockDocumentEmbedder`](mockdocumentembedder.mdx) for documents. With the default deterministic mode, a query whose text matches a document's content produces the same vector, so the document is retrieved as the top hit.
|
||||
|
||||
## Usage
|
||||
|
||||
### On its own
|
||||
|
||||
```python
|
||||
from haystack.components.embedders import MockTextEmbedder
|
||||
|
||||
embedder = MockTextEmbedder(dimension=8)
|
||||
result = embedder.run("I love pizza!")
|
||||
print(result["embedding"]) # a deterministic list of 8 floats
|
||||
```
|
||||
|
||||
### In a pipeline
|
||||
|
||||
A retrieval pipeline built with mock embedders runs without any API key and always returns the same result for the same input:
|
||||
|
||||
```python
|
||||
from haystack import Document, Pipeline
|
||||
from haystack.components.embedders import MockDocumentEmbedder, MockTextEmbedder
|
||||
from haystack.components.retrievers.in_memory import InMemoryEmbeddingRetriever
|
||||
from haystack.document_stores.in_memory import InMemoryDocumentStore
|
||||
|
||||
document_store = InMemoryDocumentStore()
|
||||
documents = [
|
||||
Document(content="My name is Wolfgang and I live in Berlin"),
|
||||
Document(content="I saw a black horse running"),
|
||||
]
|
||||
|
||||
indexed = MockDocumentEmbedder(dimension=8).run(documents=documents)
|
||||
document_store.write_documents(indexed["documents"])
|
||||
|
||||
query_pipeline = Pipeline()
|
||||
query_pipeline.add_component("text_embedder", MockTextEmbedder(dimension=8))
|
||||
query_pipeline.add_component(
|
||||
"retriever",
|
||||
InMemoryEmbeddingRetriever(document_store=document_store),
|
||||
)
|
||||
query_pipeline.connect("text_embedder.embedding", "retriever.query_embedding")
|
||||
|
||||
result = query_pipeline.run(
|
||||
{"text_embedder": {"text": "I saw a black horse running"}},
|
||||
)
|
||||
print(result["retriever"]["documents"][0].content) # "I saw a black horse running"
|
||||
```
|
||||
@@ -0,0 +1,154 @@
|
||||
---
|
||||
title: "NvidiaDocumentEmbedder"
|
||||
id: nvidiadocumentembedder
|
||||
slug: "/nvidiadocumentembedder"
|
||||
description: "This component computes the embeddings of a list of documents and stores the obtained vectors in the embedding field of each document."
|
||||
---
|
||||
|
||||
# NvidiaDocumentEmbedder
|
||||
|
||||
This component computes the embeddings of a list of documents and stores the obtained vectors in the embedding field of each document.
|
||||
|
||||
<div className="key-value-table">
|
||||
|
||||
| | |
|
||||
| --- | --- |
|
||||
| **Most common position in a pipeline** | Before a [`DocumentWriter`](../writers/documentwriter.mdx) in an indexing pipeline |
|
||||
| **Mandatory init variables** | `api_key`: API key for the NVIDIA NIM. Can be set with `NVIDIA_API_KEY` env var. |
|
||||
| **Mandatory run variables** | `documents`: A list of documents |
|
||||
| **Output variables** | `documents`: A list of documents (enriched with embeddings) <br /> <br />`meta`: A dictionary of metadata |
|
||||
| **API reference** | [NVIDIA](/reference/integrations-nvidia) |
|
||||
| **GitHub link** | https://github.com/deepset-ai/haystack-core-integrations/tree/main/integrations/nvidia |
|
||||
| **Package name** | `nvidia-haystack` |
|
||||
|
||||
</div>
|
||||
|
||||
## Overview
|
||||
|
||||
`NvidiaDocumentEmbedder` enriches documents with an embedding of their content.
|
||||
|
||||
You can use this component with self-hosted models using NVIDIA NIM or models hosted on the [NVIDIA API Catalog](https://build.nvidia.com/explore/discover).
|
||||
|
||||
To embed a string, use [`NvidiaTextEmbedder`](nvidiatextembedder.mdx).
|
||||
|
||||
## Usage
|
||||
|
||||
To start using `NvidiaDocumentEmbedder`, install the `nvidia-haystack` package:
|
||||
|
||||
```shell
|
||||
pip install nvidia-haystack
|
||||
```
|
||||
|
||||
You can use `NvidiaDocumentEmbedder` with all the embedding models available on the [NVIDIA API Catalog](https://docs.api.nvidia.com/nim/reference) or with a model deployed using NVIDIA NIM. For more information, refer to [Deploying Text Embedding Models](https://developer.nvidia.com/docs/nemo-microservices/embedding/source/deploy.html).
|
||||
|
||||
### On its own
|
||||
|
||||
To use models from the NVIDIA API Catalog, you need to specify the `api_url` and your API key. You can get your API key from the [NVIDIA API Catalog](https://build.nvidia.com/explore/discover).
|
||||
|
||||
`NvidiaDocumentEmbedder` uses the `NVIDIA_API_KEY` environment variable by default. Otherwise, you can pass an API key at initialization with the `api_key` parameter:
|
||||
|
||||
```python
|
||||
from haystack import Document
|
||||
from haystack.utils.auth import Secret
|
||||
from haystack_integrations.components.embedders.nvidia import NvidiaDocumentEmbedder
|
||||
|
||||
documents = [
|
||||
Document(content="A transformer is a deep learning architecture"),
|
||||
Document(content="Large language models use transformer architectures"),
|
||||
]
|
||||
|
||||
embedder = NvidiaDocumentEmbedder(
|
||||
model="nvidia/nv-embedqa-e5-v5",
|
||||
api_url="https://integrate.api.nvidia.com/v1",
|
||||
api_key=Secret.from_token("<your-api-key>"),
|
||||
)
|
||||
|
||||
result = embedder.run(documents=documents)
|
||||
print(result["documents"])
|
||||
print(result["meta"])
|
||||
```
|
||||
|
||||
To use a locally deployed model, set the `api_url` to your localhost and set `api_key` to `None`:
|
||||
|
||||
```python
|
||||
from haystack import Document
|
||||
from haystack_integrations.components.embedders.nvidia import NvidiaDocumentEmbedder
|
||||
|
||||
documents = [
|
||||
Document(content="A transformer is a deep learning architecture"),
|
||||
Document(content="Large language models use transformer architectures"),
|
||||
]
|
||||
|
||||
embedder = NvidiaDocumentEmbedder(
|
||||
model="nvidia/nv-embedqa-e5-v5",
|
||||
api_url="http://localhost:9999/v1",
|
||||
api_key=None,
|
||||
)
|
||||
|
||||
result = embedder.run(documents=documents)
|
||||
print(result["documents"])
|
||||
print(result["meta"])
|
||||
```
|
||||
|
||||
### In a pipeline
|
||||
|
||||
The following example shows how to use `NvidiaDocumentEmbedder` in a RAG pipeline:
|
||||
|
||||
```python
|
||||
from haystack import Pipeline, Document
|
||||
from haystack.document_stores.in_memory import InMemoryDocumentStore
|
||||
from haystack.components.writers import DocumentWriter
|
||||
from haystack.components.retrievers.in_memory import InMemoryEmbeddingRetriever
|
||||
from haystack.utils.auth import Secret
|
||||
from haystack_integrations.components.embedders.nvidia import (
|
||||
NvidiaTextEmbedder,
|
||||
NvidiaDocumentEmbedder,
|
||||
)
|
||||
|
||||
document_store = InMemoryDocumentStore(embedding_similarity_function="cosine")
|
||||
|
||||
documents = [
|
||||
Document(content="My name is Wolfgang and I live in Berlin"),
|
||||
Document(content="I saw a black horse running"),
|
||||
Document(content="Germany has many big cities"),
|
||||
]
|
||||
|
||||
indexing_pipeline = Pipeline()
|
||||
indexing_pipeline.add_component(
|
||||
"embedder",
|
||||
NvidiaDocumentEmbedder(
|
||||
model="nvidia/nv-embedqa-e5-v5",
|
||||
api_url="https://integrate.api.nvidia.com/v1",
|
||||
api_key=Secret.from_token("<your-api-key>"),
|
||||
),
|
||||
)
|
||||
indexing_pipeline.add_component("writer", DocumentWriter(document_store=document_store))
|
||||
indexing_pipeline.connect("embedder", "writer")
|
||||
|
||||
indexing_pipeline.run({"embedder": {"documents": documents}})
|
||||
|
||||
query_pipeline = Pipeline()
|
||||
query_pipeline.add_component(
|
||||
"text_embedder",
|
||||
NvidiaTextEmbedder(
|
||||
model="nvidia/nv-embedqa-e5-v5",
|
||||
api_url="https://integrate.api.nvidia.com/v1",
|
||||
api_key=Secret.from_token("<your-api-key>"),
|
||||
),
|
||||
)
|
||||
query_pipeline.add_component(
|
||||
"retriever",
|
||||
InMemoryEmbeddingRetriever(document_store=document_store),
|
||||
)
|
||||
query_pipeline.connect("text_embedder.embedding", "retriever.query_embedding")
|
||||
|
||||
query = "Who lives in Berlin?"
|
||||
|
||||
result = query_pipeline.run({"text_embedder": {"text": query}})
|
||||
|
||||
print(result["retriever"]["documents"][0])
|
||||
```
|
||||
|
||||
## Related
|
||||
|
||||
- Cookbook: [Haystack RAG Pipeline with Self-Deployed AI models using NVIDIA NIMs](https://haystack.deepset.ai/cookbook/rag-with-nims)
|
||||
@@ -0,0 +1,142 @@
|
||||
---
|
||||
title: "NvidiaTextEmbedder"
|
||||
id: nvidiatextembedder
|
||||
slug: "/nvidiatextembedder"
|
||||
description: "This component transforms a string into a vector that captures its semantics using NVIDIA-hosted models."
|
||||
---
|
||||
|
||||
# NvidiaTextEmbedder
|
||||
|
||||
This component transforms a string into a vector that captures its semantics using NVIDIA-hosted models.
|
||||
|
||||
<div className="key-value-table">
|
||||
|
||||
| | |
|
||||
| --- | --- |
|
||||
| **Most common position in a pipeline** | Before an embedding [Retriever](../retrievers.mdx) in a query/RAG pipeline |
|
||||
| **Mandatory init variables** | `api_key`: API key for the NVIDIA NIM. Can be set with `NVIDIA_API_KEY` env var. |
|
||||
| **Mandatory run variables** | `text`: A string |
|
||||
| **Output variables** | `embedding`: A list of float numbers (vectors) <br /> <br />`meta`: A dictionary of metadata strings |
|
||||
| **API reference** | [NVIDIA](/reference/integrations-nvidia) |
|
||||
| **GitHub link** | https://github.com/deepset-ai/haystack-core-integrations/tree/main/integrations/nvidia |
|
||||
| **Package name** | `nvidia-haystack` |
|
||||
|
||||
</div>
|
||||
|
||||
## Overview
|
||||
|
||||
`NvidiaTextEmbedder` embeds a simple string (such as a query) into a vector.
|
||||
|
||||
You can use this component with self-hosted models using NVIDIA NIM or models hosted on the [NVIDIA API Catalog](https://build.nvidia.com/explore/discover).
|
||||
|
||||
To embed a list of documents, use [`NvidiaDocumentEmbedder`](nvidiadocumentembedder.mdx), which enriches each document with the computed embedding.
|
||||
|
||||
## Usage
|
||||
|
||||
To start using `NvidiaTextEmbedder`, install the `nvidia-haystack` package:
|
||||
|
||||
```shell
|
||||
pip install nvidia-haystack
|
||||
```
|
||||
|
||||
You can use `NvidiaTextEmbedder` with all the embedding models available on the [NVIDIA API Catalog](https://docs.api.nvidia.com/nim/reference) or with a model deployed using NVIDIA NIM. For more information, refer to [Deploying Text Embedding Models](https://developer.nvidia.com/docs/nemo-microservices/embedding/source/deploy.html).
|
||||
|
||||
### On its own
|
||||
|
||||
To use models from the NVIDIA API Catalog, you need to specify the `api_url` and your API key. You can get your API key from the [NVIDIA API Catalog](https://build.nvidia.com/explore/discover).
|
||||
|
||||
`NvidiaTextEmbedder` uses the `NVIDIA_API_KEY` environment variable by default. Otherwise, you can pass an API key at initialization with the `api_key` parameter:
|
||||
|
||||
```python
|
||||
from haystack.utils.auth import Secret
|
||||
from haystack_integrations.components.embedders.nvidia import NvidiaTextEmbedder
|
||||
|
||||
embedder = NvidiaTextEmbedder(
|
||||
model="nvidia/nv-embedqa-e5-v5",
|
||||
api_url="https://integrate.api.nvidia.com/v1",
|
||||
api_key=Secret.from_token("<your-api-key>"),
|
||||
)
|
||||
|
||||
result = embedder.run("A transformer is a deep learning architecture")
|
||||
print(result["embedding"])
|
||||
print(result["meta"])
|
||||
```
|
||||
|
||||
To use a locally deployed model, set the `api_url` to your localhost and set `api_key` to `None`:
|
||||
|
||||
```python
|
||||
from haystack_integrations.components.embedders.nvidia import NvidiaTextEmbedder
|
||||
|
||||
embedder = NvidiaTextEmbedder(
|
||||
model="nvidia/nv-embedqa-e5-v5",
|
||||
api_url="http://localhost:9999/v1",
|
||||
api_key=None,
|
||||
)
|
||||
|
||||
result = embedder.run("A transformer is a deep learning architecture")
|
||||
print(result["embedding"])
|
||||
print(result["meta"])
|
||||
```
|
||||
|
||||
### In a pipeline
|
||||
|
||||
The following example shows how to use `NvidiaTextEmbedder` in a RAG pipeline:
|
||||
|
||||
```python
|
||||
from haystack import Pipeline, Document
|
||||
from haystack.document_stores.in_memory import InMemoryDocumentStore
|
||||
from haystack.components.writers import DocumentWriter
|
||||
from haystack.components.retrievers.in_memory import InMemoryEmbeddingRetriever
|
||||
from haystack.utils.auth import Secret
|
||||
from haystack_integrations.components.embedders.nvidia import (
|
||||
NvidiaTextEmbedder,
|
||||
NvidiaDocumentEmbedder,
|
||||
)
|
||||
|
||||
document_store = InMemoryDocumentStore(embedding_similarity_function="cosine")
|
||||
|
||||
documents = [
|
||||
Document(content="My name is Wolfgang and I live in Berlin"),
|
||||
Document(content="I saw a black horse running"),
|
||||
Document(content="Germany has many big cities"),
|
||||
]
|
||||
|
||||
indexing_pipeline = Pipeline()
|
||||
indexing_pipeline.add_component(
|
||||
"embedder",
|
||||
NvidiaDocumentEmbedder(
|
||||
model="nvidia/nv-embedqa-e5-v5",
|
||||
api_url="https://integrate.api.nvidia.com/v1",
|
||||
api_key=Secret.from_token("<your-api-key>"),
|
||||
),
|
||||
)
|
||||
indexing_pipeline.add_component("writer", DocumentWriter(document_store=document_store))
|
||||
indexing_pipeline.connect("embedder", "writer")
|
||||
|
||||
indexing_pipeline.run({"embedder": {"documents": documents}})
|
||||
|
||||
query_pipeline = Pipeline()
|
||||
query_pipeline.add_component(
|
||||
"text_embedder",
|
||||
NvidiaTextEmbedder(
|
||||
model="nvidia/nv-embedqa-e5-v5",
|
||||
api_url="https://integrate.api.nvidia.com/v1",
|
||||
api_key=Secret.from_token("<your-api-key>"),
|
||||
),
|
||||
)
|
||||
query_pipeline.add_component(
|
||||
"retriever",
|
||||
InMemoryEmbeddingRetriever(document_store=document_store),
|
||||
)
|
||||
query_pipeline.connect("text_embedder.embedding", "retriever.query_embedding")
|
||||
|
||||
query = "Who lives in Berlin?"
|
||||
|
||||
result = query_pipeline.run({"text_embedder": {"text": query}})
|
||||
|
||||
print(result["retriever"]["documents"][0])
|
||||
```
|
||||
|
||||
## Related
|
||||
|
||||
- Cookbook: [Haystack RAG Pipeline with Self-Deployed AI models using NVIDIA NIMs](https://haystack.deepset.ai/cookbook/rag-with-nims)
|
||||
@@ -0,0 +1,124 @@
|
||||
---
|
||||
title: "OllamaDocumentEmbedder"
|
||||
id: ollamadocumentembedder
|
||||
slug: "/ollamadocumentembedder"
|
||||
description: "This component computes the embeddings of a list of documents using embedding models compatible with the Ollama Library."
|
||||
---
|
||||
|
||||
# OllamaDocumentEmbedder
|
||||
|
||||
This component computes the embeddings of a list of documents using embedding models compatible with the Ollama Library.
|
||||
|
||||
<div className="key-value-table">
|
||||
|
||||
| | |
|
||||
| --- | --- |
|
||||
| **Most common position in a pipeline** | Before a [`DocumentWriter`](../writers/documentwriter.mdx) in an indexing pipeline |
|
||||
| **Mandatory run variables** | `documents`: A list of documents to be embedded |
|
||||
| **Output variables** | `documents`: A list of documents (enriched with embeddings) <br /> <br />`meta`: A dictionary of metadata strings |
|
||||
| **API reference** | [Ollama](/reference/integrations-ollama) |
|
||||
| **GitHub link** | https://github.com/deepset-ai/haystack-core-integrations/tree/main/integrations/ollama |
|
||||
| **Package name** | `ollama-haystack` |
|
||||
|
||||
</div>
|
||||
|
||||
`OllamaDocumentEmbedder` computes the embeddings of a list of documents and stores the obtained vectors in the embedding field of each document. It uses embedding models compatible with the Ollama Library.
|
||||
|
||||
The vectors computed by this component are necessary to perform embedding retrieval on a collection of documents. At retrieval time, the vector that represents the query is compared with those of the documents to find the most similar or relevant documents.
|
||||
|
||||
## Overview
|
||||
|
||||
`OllamaDocumentEmbedder` should be used to embed a list of documents. For embedding a string only, use the [`OllamaTextEmbedder`](ollamatextembedder.mdx).
|
||||
|
||||
The component uses `http://localhost:11434` as the default URL as most available setups (Mac, Linux, Docker) default to port 11434.
|
||||
|
||||
### Compatible Models
|
||||
|
||||
Unless specified otherwise while initializing this component, the default embedding model is "nomic-embed-text". See other possible pre-built models in Ollama's [library](https://ollama.ai/library). To load your own custom model, follow the [instructions](https://github.com/ollama/ollama/blob/main/docs/modelfile.md) from Ollama.
|
||||
|
||||
### Installation
|
||||
|
||||
To start using this integration with Haystack, install the package with:
|
||||
|
||||
```shell
|
||||
pip install ollama-haystack
|
||||
```
|
||||
|
||||
Make sure that you have a running Ollama model (either through a docker container, or locally hosted). No other configuration is necessary as Ollama has the embedding API built in.
|
||||
|
||||
### Embedding Metadata
|
||||
|
||||
Most embedded metadata contains information about the model name and type. You can pass [optional arguments](https://github.com/jmorganca/ollama/blob/main/docs/modelfile.md#valid-parameters-and-values), such as temperature, top_p, and others, to the Ollama generation endpoint.
|
||||
|
||||
The name of the model used will be automatically appended as part of the document metadata. An example payload using the nomic-embed-text model will look like this:
|
||||
|
||||
```python
|
||||
{"meta": {"model": "nomic-embed-text"}}
|
||||
```
|
||||
|
||||
## Usage
|
||||
|
||||
### On its own
|
||||
|
||||
```python
|
||||
from haystack import Document
|
||||
from haystack_integrations.components.embedders.ollama import OllamaDocumentEmbedder
|
||||
|
||||
doc = Document(content="What do llamas say once you have thanked them? No probllama!")
|
||||
document_embedder = OllamaDocumentEmbedder()
|
||||
|
||||
result = document_embedder.run([doc])
|
||||
print(result["documents"][0].embedding)
|
||||
|
||||
# Calculating embeddings: 100%|██████████| 1/1 [00:02<00:00, 2.82s/it]
|
||||
|
||||
# [-0.16412407159805298, -3.8359334468841553, ... ]
|
||||
```
|
||||
|
||||
### In a pipeline
|
||||
|
||||
```python
|
||||
from haystack import Pipeline
|
||||
|
||||
from haystack_integrations.components.embedders.ollama import OllamaDocumentEmbedder
|
||||
|
||||
from haystack.components.preprocessors import DocumentCleaner, DocumentSplitter
|
||||
|
||||
from haystack.components.converters import PyPDFToDocument
|
||||
from haystack.components.writers import DocumentWriter
|
||||
from haystack.document_stores.types import DuplicatePolicy
|
||||
from haystack.document_stores.in_memory import InMemoryDocumentStore
|
||||
|
||||
document_store = InMemoryDocumentStore(embedding_similarity_function="cosine")
|
||||
|
||||
embedder = OllamaDocumentEmbedder(
|
||||
model="nomic-embed-text",
|
||||
url="http://localhost:11434",
|
||||
) # This is the default model and URL
|
||||
|
||||
cleaner = DocumentCleaner()
|
||||
splitter = DocumentSplitter()
|
||||
file_converter = PyPDFToDocument()
|
||||
writer = DocumentWriter(document_store=document_store, policy=DuplicatePolicy.OVERWRITE)
|
||||
|
||||
indexing_pipeline = Pipeline()
|
||||
|
||||
# Add components to pipeline
|
||||
indexing_pipeline.add_component("embedder", embedder)
|
||||
indexing_pipeline.add_component("converter", file_converter)
|
||||
indexing_pipeline.add_component("cleaner", cleaner)
|
||||
indexing_pipeline.add_component("splitter", splitter)
|
||||
indexing_pipeline.add_component("writer", writer)
|
||||
|
||||
# Connect components in pipeline
|
||||
indexing_pipeline.connect("converter", "cleaner")
|
||||
indexing_pipeline.connect("cleaner", "splitter")
|
||||
indexing_pipeline.connect("splitter", "embedder")
|
||||
indexing_pipeline.connect("embedder", "writer")
|
||||
|
||||
# Run Pipeline
|
||||
indexing_pipeline.run({"converter": {"sources": ["files/test_pdf_data.pdf"]}})
|
||||
|
||||
# Calculating embeddings: 100%|██████████| 115/115
|
||||
# {'embedder': {'meta': {'model': 'nomic-embed-text'}}, 'writer': {'documents_written': 115}}
|
||||
```
|
||||
@@ -0,0 +1,110 @@
|
||||
---
|
||||
title: "OllamaTextEmbedder"
|
||||
id: ollamatextembedder
|
||||
slug: "/ollamatextembedder"
|
||||
description: "This component computes the embeddings of a string using embedding models compatible with the Ollama Library."
|
||||
---
|
||||
|
||||
# OllamaTextEmbedder
|
||||
|
||||
This component computes the embeddings of a string using embedding models compatible with the Ollama Library.
|
||||
|
||||
<div className="key-value-table">
|
||||
|
||||
| | |
|
||||
| --- | --- |
|
||||
| **Most common position in a pipeline** | Before an embedding [Retriever](../retrievers.mdx) in a query/RAG pipeline |
|
||||
| **Mandatory run variables** | `text`: A string |
|
||||
| **Output variables** | `embedding`: A list of float numbers (vectors) <br /> <br />`meta`: A dictionary of metadata strings |
|
||||
| **API reference** | [Ollama](/reference/integrations-ollama) |
|
||||
| **GitHub link** | https://github.com/deepset-ai/haystack-core-integrations/tree/main/integrations/ollama |
|
||||
| **Package name** | `ollama-haystack` |
|
||||
|
||||
</div>
|
||||
|
||||
`OllamaDocumentEmbedder` computes the embeddings of a list of documents and stores the obtained vectors in the embedding field of each document. It uses embedding models compatible with the Ollama Library.
|
||||
|
||||
The vectors computed by this component are necessary to perform embedding retrieval on a collection of documents. At retrieval time, the vector that represents the query is compared with those of the documents to find the most similar or relevant documents.
|
||||
|
||||
## Overview
|
||||
|
||||
`OllamaTextEmbedder` should be used to embed a string. For embedding a list of documents, use the [`OllamaDocumentEmbedder`](ollamadocumentembedder.mdx).
|
||||
|
||||
The component uses `http://localhost:11434` as the default URL as most available setups (Mac, Linux, Docker) default to port 11434.
|
||||
|
||||
### Compatible Models
|
||||
|
||||
Unless specified otherwise while initializing this component, the default embedding model is "nomic-embed-text". See other possible pre-built models in Ollama's [library](https://ollama.ai/library). To load your own custom model, follow the [instructions](https://github.com/ollama/ollama/blob/main/docs/modelfile.md) from Ollama.
|
||||
|
||||
### Installation
|
||||
|
||||
To start using this integration with Haystack, install the package with:
|
||||
|
||||
```shell
|
||||
pip install ollama-haystack
|
||||
```
|
||||
|
||||
Make sure that you have a running Ollama model (either through a docker container, or locally hosted). No other configuration is necessary as Ollama has the embedding API built in.
|
||||
|
||||
### Embedding Metadata
|
||||
|
||||
Most embedded metadata contains information about the model name and type. You can pass [optional arguments](https://github.com/jmorganca/ollama/blob/main/docs/modelfile.md#valid-parameters-and-values), such as temperature, top_p, and others, to the Ollama generation endpoint.
|
||||
|
||||
The name of the model used will be automatically appended as part of the metadata. An example payload using the nomic-embed-text model will look like this:
|
||||
|
||||
```python
|
||||
{"meta": {"model": "nomic-embed-text"}}
|
||||
```
|
||||
|
||||
## Usage
|
||||
|
||||
### On its own
|
||||
|
||||
```python
|
||||
from haystack_integrations.components.embedders.ollama import OllamaTextEmbedder
|
||||
|
||||
embedder = OllamaTextEmbedder()
|
||||
|
||||
result = embedder.run(
|
||||
text="What do llamas say once you have thanked them? No probllama!",
|
||||
)
|
||||
|
||||
print(result["embedding"])
|
||||
```
|
||||
|
||||
### In a pipeline
|
||||
|
||||
```python
|
||||
from haystack import Document
|
||||
from haystack import Pipeline
|
||||
from haystack.document_stores.in_memory import InMemoryDocumentStore
|
||||
from cohere_haystack.embedders.text_embedder import OllamaTextEmbedder
|
||||
from cohere_haystack.embedders.document_embedder import OllamaDocumentEmbedder
|
||||
from haystack.components.retrievers.in_memory import InMemoryEmbeddingRetriever
|
||||
|
||||
document_store = InMemoryDocumentStore(embedding_similarity_function="cosine")
|
||||
|
||||
documents = [
|
||||
Document(content="My name is Wolfgang and I live in Berlin"),
|
||||
Document(content="I saw a black horse running"),
|
||||
Document(content="Germany has many big cities"),
|
||||
]
|
||||
|
||||
document_embedder = OllamaDocumentEmbedder()
|
||||
documents_with_embeddings = document_embedder.run(documents)["documents"]
|
||||
document_store.write_documents(documents_with_embeddings)
|
||||
|
||||
query_pipeline = Pipeline()
|
||||
query_pipeline.add_component("text_embedder", OllamaTextEmbedder())
|
||||
query_pipeline.add_component(
|
||||
"retriever",
|
||||
InMemoryEmbeddingRetriever(document_store=document_store),
|
||||
)
|
||||
query_pipeline.connect("text_embedder.embedding", "retriever.query_embedding")
|
||||
|
||||
query = "Who lives in Berlin?"
|
||||
|
||||
result = query_pipeline.run({"text_embedder": {"text": query}})
|
||||
|
||||
print(result["retriever"]["documents"][0])
|
||||
```
|
||||
@@ -0,0 +1,120 @@
|
||||
---
|
||||
title: "OpenAIDocumentEmbedder"
|
||||
id: openaidocumentembedder
|
||||
slug: "/openaidocumentembedder"
|
||||
description: "OpenAIDocumentEmbedder computes the embeddings of a list of documents and stores the obtained vectors in the embedding field of each document. It uses OpenAI embedding models."
|
||||
---
|
||||
|
||||
# OpenAIDocumentEmbedder
|
||||
|
||||
OpenAIDocumentEmbedder computes the embeddings of a list of documents and stores the obtained vectors in the embedding field of each document. It uses OpenAI embedding models.
|
||||
|
||||
The vectors computed by this component are necessary to perform embedding retrieval on a collection of documents. At retrieval time, the vector representing the query is compared with those of the documents to find the most similar or relevant documents.
|
||||
|
||||
<div className="key-value-table">
|
||||
|
||||
| | |
|
||||
| --- | --- |
|
||||
| **Most common position in a pipeline** | Before a [`DocumentWriter`](../writers/documentwriter.mdx) in an indexing pipeline |
|
||||
| **Mandatory init variables** | `api_key`: An OpenAI API key. Can be set with `OPENAI_API_KEY` env var. |
|
||||
| **Mandatory run variables** | `documents`: A list of documents |
|
||||
| **Output variables** | `documents`: A list of documents (enriched with embeddings) <br /> <br />`meta`: A dictionary of metadata |
|
||||
| **API reference** | [Embedders](/reference/embedders-api) |
|
||||
| **GitHub link** | https://github.com/deepset-ai/haystack/blob/main/haystack/components/embedders/openai_document_embedder.py |
|
||||
| **Package name** | `haystack-ai` |
|
||||
|
||||
</div>
|
||||
|
||||
## Overview
|
||||
|
||||
To see the list of compatible OpenAI embedding models, head over to OpenAI [documentation](https://platform.openai.com/docs/guides/embeddings/embedding-models). The default model for `OpenAIDocumentEmbedder` is `text-embedding-ada-002`. You can specify another model with the `model` parameter when initializing this component.
|
||||
|
||||
This component should be used to embed a list of documents. To embed a string, use the [OpenAITextEmbedder](openaitextembedder.mdx).
|
||||
|
||||
The component uses an `OPENAI_API_KEY` environment variable by default. Otherwise, you can pass an API key at initialization with `api_key`:
|
||||
|
||||
```
|
||||
embedder = OpenAIDocumentEmbedder(api_key=Secret.from_token("<your-api-key>"))
|
||||
```
|
||||
|
||||
### Embedding Metadata
|
||||
|
||||
Text documents often come with a set of metadata. If they are distinctive and semantically meaningful, you can embed them along with the text of the document to improve retrieval.
|
||||
|
||||
You can do this easily by using the Document Embedder:
|
||||
|
||||
```python
|
||||
from haystack import Document
|
||||
from haystack.components.embedders import OpenAIDocumentEmbedder
|
||||
|
||||
doc = Document(content="some text", meta={"title": "relevant title", "page number": 18})
|
||||
|
||||
embedder = OpenAIDocumentEmbedder(meta_fields_to_embed=["title"])
|
||||
|
||||
docs_w_embeddings = embedder.run(documents=[doc])["documents"]
|
||||
```
|
||||
|
||||
## Usage
|
||||
|
||||
### On its own
|
||||
|
||||
Here is how you can use the component on its own:
|
||||
|
||||
```python
|
||||
from haystack.components.embedders import OpenAIDocumentEmbedder
|
||||
|
||||
doc = Document(content="I love pizza!")
|
||||
|
||||
document_embedder = OpenAIDocumentEmbedder(api_key=Secret.from_token("<your-api-key>"))
|
||||
|
||||
result = document_embedder.run([doc])
|
||||
print(result["documents"][0].embedding)
|
||||
|
||||
# [0.017020374536514282, -0.023255806416273117, ...]
|
||||
```
|
||||
|
||||
:::info
|
||||
We recommend setting OPENAI_API_KEY as an environment variable instead of setting it as a parameter.
|
||||
:::
|
||||
|
||||
### In a pipeline
|
||||
|
||||
```python
|
||||
from haystack import Pipeline
|
||||
from haystack.document_stores.in_memory import InMemoryDocumentStore
|
||||
from haystack.components.embedders import OpenAITextEmbedder, OpenAIDocumentEmbedder
|
||||
from haystack.components.writers import DocumentWriter
|
||||
from haystack.components.retrievers.in_memory import InMemoryEmbeddingRetriever
|
||||
|
||||
document_store = InMemoryDocumentStore(embedding_similarity_function="cosine")
|
||||
|
||||
documents = [
|
||||
Document(content="My name is Wolfgang and I live in Berlin"),
|
||||
Document(content="I saw a black horse running"),
|
||||
Document(content="Germany has many big cities"),
|
||||
]
|
||||
|
||||
indexing_pipeline = Pipeline()
|
||||
indexing_pipeline.add_component("embedder", OpenAIDocumentEmbedder())
|
||||
indexing_pipeline.add_component("writer", DocumentWriter(document_store=document_store))
|
||||
indexing_pipeline.connect("embedder", "writer")
|
||||
|
||||
indexing_pipeline.run({"embedder": {"documents": documents}})
|
||||
|
||||
query_pipeline = Pipeline()
|
||||
query_pipeline.add_component("text_embedder", OpenAITextEmbedder())
|
||||
query_pipeline.add_component(
|
||||
"retriever",
|
||||
InMemoryEmbeddingRetriever(document_store=document_store),
|
||||
)
|
||||
query_pipeline.connect("text_embedder.embedding", "retriever.query_embedding")
|
||||
|
||||
query = "Who lives in Berlin?"
|
||||
|
||||
result = query_pipeline.run({"text_embedder": {"text": query}})
|
||||
|
||||
print(result["retriever"]["documents"][0])
|
||||
|
||||
# Document(id=..., mimetype: 'text/plain',
|
||||
# text: 'My name is Wolfgang and I live in Berlin')
|
||||
```
|
||||
@@ -0,0 +1,101 @@
|
||||
---
|
||||
title: "OpenAITextEmbedder"
|
||||
id: openaitextembedder
|
||||
slug: "/openaitextembedder"
|
||||
description: "OpenAITextEmbedder transforms a string into a vector that captures its semantics using an OpenAI embedding model."
|
||||
---
|
||||
|
||||
# OpenAITextEmbedder
|
||||
|
||||
OpenAITextEmbedder transforms a string into a vector that captures its semantics using an OpenAI embedding model.
|
||||
|
||||
When you perform embedding retrieval, you use this component to transform your query into a vector. Then, the embedding Retriever looks for similar or relevant documents.
|
||||
|
||||
<div className="key-value-table">
|
||||
|
||||
| | |
|
||||
| --- | --- |
|
||||
| **Most common position in a pipeline** | Before an embedding [Retriever](../retrievers.mdx) in a query/RAG pipeline |
|
||||
| **Mandatory init variables** | `api_key`: An OpenAI API key. Can be set with `OPENAI_API_KEY` env var. |
|
||||
| **Mandatory run variables** | `text`: A string |
|
||||
| **Output variables** | `embedding`: A list of float numbers <br /> <br />`meta`: A dictionary of metadata |
|
||||
| **API reference** | [Embedders](/reference/embedders-api) |
|
||||
| **GitHub link** | https://github.com/deepset-ai/haystack/blob/main/haystack/components/embedders/openai_text_embedder.py |
|
||||
| **Package name** | `haystack-ai` |
|
||||
|
||||
</div>
|
||||
|
||||
## Overview
|
||||
|
||||
To see the list of compatible OpenAI embedding models, head over to OpenAI [documentation](https://platform.openai.com/docs/guides/embeddings/embedding-models). The default model for `OpenAITextEmbedder` is `text-embedding-ada-002`. You can specify another model with the `model` parameter when initializing this component.
|
||||
|
||||
Use `OpenAITextEmbedder` to embed a simple string (such as a query) into a vector. For embedding lists of documents, use the [OpenAIDocumentEmbedder](openaidocumentembedder.mdx), which enriches the document with the computed embedding, also known as vector.
|
||||
|
||||
The component uses an `OPENAI_API_KEY` environment variable by default. Otherwise, you can pass an API key at initialization with `api_key`:
|
||||
|
||||
```python
|
||||
embedder = OpenAITextEmbedder(api_key=Secret.from_token("<your-api-key>"))
|
||||
```
|
||||
|
||||
## Usage
|
||||
|
||||
### On its own
|
||||
|
||||
Here is how you can use the component on its own:
|
||||
|
||||
```python
|
||||
from haystack.components.embedders import OpenAITextEmbedder
|
||||
|
||||
text_to_embed = "I love pizza!"
|
||||
|
||||
text_embedder = OpenAITextEmbedder(api_key=Secret.from_token("<your-api-key>"))
|
||||
|
||||
print(text_embedder.run(text_to_embed))
|
||||
|
||||
# {'embedding': [0.017020374536514282, -0.023255806416273117, ...],
|
||||
# 'meta': {'model': 'text-embedding-ada-002-v2',
|
||||
# 'usage': {'prompt_tokens': 4, 'total_tokens': 4}}}
|
||||
```
|
||||
|
||||
:::info
|
||||
We recommend setting OPENAI_API_KEY as an environment variable instead of setting it as a parameter.
|
||||
:::
|
||||
|
||||
### In a pipeline
|
||||
|
||||
```python
|
||||
from haystack import Document
|
||||
from haystack import Pipeline
|
||||
from haystack.document_stores.in_memory import InMemoryDocumentStore
|
||||
from haystack.components.embedders import OpenAITextEmbedder, OpenAIDocumentEmbedder
|
||||
from haystack.components.retrievers.in_memory import InMemoryEmbeddingRetriever
|
||||
|
||||
document_store = InMemoryDocumentStore(embedding_similarity_function="cosine")
|
||||
|
||||
documents = [
|
||||
Document(content="My name is Wolfgang and I live in Berlin"),
|
||||
Document(content="I saw a black horse running"),
|
||||
Document(content="Germany has many big cities"),
|
||||
]
|
||||
|
||||
document_embedder = OpenAIDocumentEmbedder()
|
||||
documents_with_embeddings = document_embedder.run(documents)["documents"]
|
||||
document_store.write_documents(documents_with_embeddings)
|
||||
|
||||
query_pipeline = Pipeline()
|
||||
query_pipeline.add_component("text_embedder", OpenAITextEmbedder())
|
||||
query_pipeline.add_component(
|
||||
"retriever",
|
||||
InMemoryEmbeddingRetriever(document_store=document_store),
|
||||
)
|
||||
query_pipeline.connect("text_embedder.embedding", "retriever.query_embedding")
|
||||
|
||||
query = "Who lives in Berlin?"
|
||||
|
||||
result = query_pipeline.run({"text_embedder": {"text": query}})
|
||||
|
||||
print(result["retriever"]["documents"][0])
|
||||
|
||||
# Document(id=..., mimetype: 'text/plain',
|
||||
# text: 'My name is Wolfgang and I live in Berlin')
|
||||
```
|
||||
@@ -0,0 +1,109 @@
|
||||
---
|
||||
title: "OptimumDocumentEmbedder"
|
||||
id: optimumdocumentembedder
|
||||
slug: "/optimumdocumentembedder"
|
||||
description: "A component to compute documents’ embeddings using models loaded with the Hugging Face Optimum library."
|
||||
---
|
||||
|
||||
# OptimumDocumentEmbedder
|
||||
|
||||
A component to compute documents’ embeddings using models loaded with the Hugging Face Optimum library.
|
||||
|
||||
<div className="key-value-table">
|
||||
|
||||
| | |
|
||||
| --- | --- |
|
||||
| **Most common position in a pipeline** | Before a [`DocumentWriter`](../writers/documentwriter.mdx) in an indexing pipeline |
|
||||
| **Mandatory run variables** | `documents`: A list of documents |
|
||||
| **Output variables** | `documents`: A list of documents enriched with embeddings |
|
||||
| **API reference** | [Optimum](/reference/integrations-optimum) |
|
||||
| **GitHub link** | https://github.com/deepset-ai/haystack-core-integrations/tree/main/integrations/optimum |
|
||||
| **Package name** | `optimum-haystack` |
|
||||
|
||||
</div>
|
||||
|
||||
## Overview
|
||||
|
||||
`OptimumDocumentEmbedder` embeds text strings using models loaded with the [HuggingFace Optimum](https://huggingface.co/docs/optimum/index) library. It uses the [ONNX runtime](https://onnxruntime.ai/) for high-speed inference.
|
||||
|
||||
The default model is `sentence-transformers/all-mpnet-base-v2`.
|
||||
|
||||
Similarly to other Embedders, this component allows adding prefixes (and suffixes) to include instructions. For more details, refer to the component’s API reference.
|
||||
|
||||
There are three useful parameters specific to the Optimum Embedder that you can control with various modes:
|
||||
|
||||
- [Pooling](/reference/integrations-optimum#optimumembedderpooling): generate a fixed-sized sentence embedding from a variable-sized sentence embedding
|
||||
- [Optimization](https://huggingface.co/docs/optimum/onnxruntime/usage_guides/optimization): apply graph optimization to the model and improve inference speed
|
||||
- [Quantization](https://huggingface.co/docs/optimum/onnxruntime/usage_guides/quantization): reduce the computational and memory costs
|
||||
|
||||
Find all the available mode details in our Optimum [API Reference](/reference/integrations-optimum).
|
||||
|
||||
### Authentication
|
||||
|
||||
Authentication with a Hugging Face API Token is only required to access private or gated models through Serverless Inference API or the Inference Endpoints.
|
||||
|
||||
The component uses an `HF_API_TOKEN` or `HF_TOKEN` environment variable, or you can pass a Hugging Face API token at initialization. See our [Secret Management](../../concepts/secret-management.mdx) page for more information.
|
||||
|
||||
## Usage
|
||||
|
||||
To start using this integration with Haystack, install it with:
|
||||
|
||||
```shell
|
||||
pip install optimum-haystack
|
||||
```
|
||||
|
||||
### On its own
|
||||
|
||||
```python
|
||||
from haystack.dataclasses import Document
|
||||
from haystack_integrations.components.embedders.optimum import OptimumDocumentEmbedder
|
||||
|
||||
doc = Document(content="I love pizza!")
|
||||
|
||||
document_embedder = OptimumDocumentEmbedder(
|
||||
model="sentence-transformers/all-mpnet-base-v2",
|
||||
)
|
||||
|
||||
result = document_embedder.run([doc])
|
||||
print(result["documents"][0].embedding)
|
||||
|
||||
# [0.017020374536514282, -0.023255806416273117, ...]
|
||||
```
|
||||
|
||||
### In a pipeline
|
||||
|
||||
```python
|
||||
from haystack import Pipeline
|
||||
from haystack import Document
|
||||
from haystack_integrations.components.embedders.optimum import (
|
||||
OptimumDocumentEmbedder,
|
||||
OptimumEmbedderPooling,
|
||||
OptimumEmbedderOptimizationConfig,
|
||||
OptimumEmbedderOptimizationMode,
|
||||
)
|
||||
|
||||
documents = [
|
||||
Document(content="My name is Wolfgang and I live in Berlin"),
|
||||
Document(content="I saw a black horse running"),
|
||||
Document(content="Germany has many big cities"),
|
||||
]
|
||||
|
||||
embedder = OptimumDocumentEmbedder(
|
||||
model="intfloat/e5-base-v2",
|
||||
normalize_embeddings=True,
|
||||
onnx_execution_provider="CUDAExecutionProvider",
|
||||
optimizer_settings=OptimumEmbedderOptimizationConfig(
|
||||
mode=OptimumEmbedderOptimizationMode.O4,
|
||||
for_gpu=True,
|
||||
),
|
||||
working_dir="/tmp/optimum",
|
||||
pooling_mode=OptimumEmbedderPooling.MEAN,
|
||||
)
|
||||
|
||||
pipeline = Pipeline()
|
||||
pipeline.add_component("embedder", embedder)
|
||||
|
||||
pipeline.run({"embedder": {"documents": documents}})
|
||||
|
||||
print(results["embedder"]["embedding"])
|
||||
```
|
||||
@@ -0,0 +1,106 @@
|
||||
---
|
||||
title: "OptimumTextEmbedder"
|
||||
id: optimumtextembedder
|
||||
slug: "/optimumtextembedder"
|
||||
description: "A component to embed text using models loaded with the Hugging Face Optimum library."
|
||||
---
|
||||
|
||||
# OptimumTextEmbedder
|
||||
|
||||
A component to embed text using models loaded with the Hugging Face Optimum library.
|
||||
|
||||
<div className="key-value-table">
|
||||
|
||||
| | |
|
||||
| --- | --- |
|
||||
| **Most common position in a pipeline** | Before an embedding [Retriever](../retrievers.mdx) in a query/RAG pipeline |
|
||||
| **Mandatory run variables** | `text`: A string |
|
||||
| **Output variables** | `embedding`: A list of float numbers (vectors) |
|
||||
| **API reference** | [Optimum](/reference/integrations-optimum) |
|
||||
| **GitHub link** | https://github.com/deepset-ai/haystack-core-integrations/tree/main/integrations/optimum |
|
||||
| **Package name** | `optimum-haystack` |
|
||||
|
||||
</div>
|
||||
|
||||
## Overview
|
||||
|
||||
`OptimumTextEmbedder` embeds text strings using models loaded with the [HuggingFace Optimum](https://huggingface.co/docs/optimum/index) library. It uses the [ONNX runtime](https://onnxruntime.ai/) for high-speed inference.
|
||||
|
||||
The default model is `sentence-transformers/all-mpnet-base-v2`.
|
||||
|
||||
Similarly to other Embedders, this component allows adding prefixes (and suffixes) to include instructions. For more details, refer to the component’s API reference.
|
||||
|
||||
There are three useful parameters specific to the Optimum Embedder that you can control with various modes:
|
||||
|
||||
- [Pooling](/reference/integrations-optimum#optimumembedderpooling): generate a fixed-sized sentence embedding from a variable-sized sentence embedding
|
||||
- [Optimization](https://huggingface.co/docs/optimum/onnxruntime/usage_guides/optimization): apply graph optimization to the model and improve inference speed
|
||||
- [Quantization](https://huggingface.co/docs/optimum/onnxruntime/usage_guides/quantization): reduce the computational and memory costs
|
||||
|
||||
Find all the available mode details in our Optimum [API Reference](/reference/integrations-optimum).
|
||||
|
||||
### Authentication
|
||||
|
||||
Authentication with a Hugging Face API Token is only required to access private or gated models through Serverless Inference API or the Inference Endpoints.
|
||||
|
||||
The component uses an `HF_API_TOKEN` or `HF_TOKEN` environment variable, or you can pass a Hugging Face API token at initialization. See our [Secret Management](../../concepts/secret-management.mdx) page for more information.
|
||||
|
||||
## Usage
|
||||
|
||||
To start using this integration with Haystack, install it with:
|
||||
|
||||
```shell
|
||||
pip install optimum-haystack
|
||||
```
|
||||
|
||||
### On its own
|
||||
|
||||
```python
|
||||
from haystack_integrations.components.embedders.optimum import OptimumTextEmbedder
|
||||
|
||||
text_to_embed = "I love pizza!"
|
||||
|
||||
text_embedder = OptimumTextEmbedder(model="sentence-transformers/all-mpnet-base-v2")
|
||||
|
||||
print(text_embedder.run(text_to_embed))
|
||||
|
||||
# {'embedding': [-0.07804739475250244, 0.1498992145061493,, ...]}
|
||||
```
|
||||
|
||||
### In a pipeline
|
||||
|
||||
Note that this example requires GPU support to execute.
|
||||
|
||||
```python
|
||||
from haystack import Pipeline
|
||||
|
||||
from haystack_integrations.components.embedders.optimum import (
|
||||
OptimumTextEmbedder,
|
||||
OptimumEmbedderPooling,
|
||||
OptimumEmbedderOptimizationConfig,
|
||||
OptimumEmbedderOptimizationMode,
|
||||
)
|
||||
|
||||
pipeline = Pipeline()
|
||||
embedder = OptimumTextEmbedder(
|
||||
model="intfloat/e5-base-v2",
|
||||
normalize_embeddings=True,
|
||||
onnx_execution_provider="CUDAExecutionProvider",
|
||||
optimizer_settings=OptimumEmbedderOptimizationConfig(
|
||||
mode=OptimumEmbedderOptimizationMode.O4,
|
||||
for_gpu=True,
|
||||
),
|
||||
working_dir="/tmp/optimum",
|
||||
pooling_mode=OptimumEmbedderPooling.MEAN,
|
||||
)
|
||||
pipeline.add_component("embedder", embedder)
|
||||
|
||||
results = pipeline.run(
|
||||
{
|
||||
"embedder": {
|
||||
"text": "Ex profunditate antique doctrinae, Ad caelos supra semper, Hoc incantamentum evoco, draco apparet, Incantamentum iam transactum est",
|
||||
},
|
||||
},
|
||||
)
|
||||
|
||||
print(results["embedder"]["embedding"])
|
||||
```
|
||||
@@ -0,0 +1,123 @@
|
||||
---
|
||||
title: "PerplexityDocumentEmbedder"
|
||||
id: perplexitydocumentembedder
|
||||
slug: "/perplexitydocumentembedder"
|
||||
description: "`PerplexityDocumentEmbedder` computes embeddings for a list of documents using Perplexity embedding models and stores the vectors in each document's `embedding` field."
|
||||
---
|
||||
|
||||
# PerplexityDocumentEmbedder
|
||||
|
||||
`PerplexityDocumentEmbedder` computes the embeddings of a list of documents and stores the obtained vectors in the `embedding` field of each document. It uses Perplexity embedding models.
|
||||
|
||||
The vectors computed by this component are necessary to perform embedding retrieval on a collection of documents. At retrieval time, the vector representing the query is compared with those of the documents to find the most similar or relevant documents.
|
||||
|
||||
<div className="key-value-table">
|
||||
|
||||
| | |
|
||||
| --- | --- |
|
||||
| **Most common position in a pipeline** | Before a [`DocumentWriter`](../writers/documentwriter.mdx) in an indexing pipeline |
|
||||
| **Mandatory init variables** | `api_key`: A Perplexity API key. Can be set with `PERPLEXITY_API_KEY` env var. |
|
||||
| **Mandatory run variables** | `documents`: A list of documents |
|
||||
| **Output variables** | `documents`: A list of documents (enriched with embeddings) <br /> <br />`meta`: A dictionary of metadata |
|
||||
| **API reference** | [Integrations](/reference/integrations-perplexity) |
|
||||
| **GitHub link** | https://github.com/deepset-ai/haystack-core-integrations/blob/main/integrations/perplexity/src/haystack_integrations/components/embedders/perplexity/document_embedder.py |
|
||||
| **Package name** | `perplexity-haystack` |
|
||||
|
||||
</div>
|
||||
|
||||
## Overview
|
||||
|
||||
`PerplexityDocumentEmbedder` supports the following embedding models:
|
||||
|
||||
- `pplx-embed-v1-0.6b` (default)
|
||||
- `pplx-embed-v1-4b`
|
||||
|
||||
Use this component to embed a list of documents. To embed a single string (such as a query), use [PerplexityTextEmbedder](perplexitytextembedder.mdx).
|
||||
|
||||
The component uses a `PERPLEXITY_API_KEY` environment variable by default. You can also pass an API key directly at initialization:
|
||||
|
||||
```python
|
||||
from haystack_integrations.components.embedders.perplexity import (
|
||||
PerplexityDocumentEmbedder,
|
||||
)
|
||||
from haystack.utils import Secret
|
||||
|
||||
embedder = PerplexityDocumentEmbedder(api_key=Secret.from_token("<your-api-key>"))
|
||||
```
|
||||
|
||||
### Embedding Metadata
|
||||
|
||||
If your documents have semantically meaningful metadata fields, you can embed them alongside the document text to improve retrieval quality:
|
||||
|
||||
```python
|
||||
from haystack import Document
|
||||
from haystack_integrations.components.embedders.perplexity import (
|
||||
PerplexityDocumentEmbedder,
|
||||
)
|
||||
|
||||
doc = Document(content="some text", meta={"title": "relevant title", "page_number": 18})
|
||||
|
||||
embedder = PerplexityDocumentEmbedder(meta_fields_to_embed=["title"])
|
||||
docs_with_embeddings = embedder.run(documents=[doc])["documents"]
|
||||
```
|
||||
|
||||
## Usage
|
||||
|
||||
### On its own
|
||||
|
||||
```python
|
||||
from haystack import Document
|
||||
from haystack_integrations.components.embedders.perplexity import (
|
||||
PerplexityDocumentEmbedder,
|
||||
)
|
||||
|
||||
doc = Document(content="I love pizza!")
|
||||
|
||||
document_embedder = PerplexityDocumentEmbedder()
|
||||
result = document_embedder.run([doc])
|
||||
print(result["documents"][0].embedding)
|
||||
|
||||
# [0.017020374536514282, -0.023255806416273117, ...]
|
||||
```
|
||||
|
||||
:::info
|
||||
We recommend setting `PERPLEXITY_API_KEY` as an environment variable instead of passing it as a parameter.
|
||||
:::
|
||||
|
||||
### In a pipeline
|
||||
|
||||
```python
|
||||
from haystack import Document, Pipeline
|
||||
from haystack.document_stores.in_memory import InMemoryDocumentStore
|
||||
from haystack.components.retrievers.in_memory import InMemoryEmbeddingRetriever
|
||||
from haystack.components.writers import DocumentWriter
|
||||
from haystack_integrations.components.embedders.perplexity import (
|
||||
PerplexityTextEmbedder,
|
||||
PerplexityDocumentEmbedder,
|
||||
)
|
||||
|
||||
document_store = InMemoryDocumentStore(embedding_similarity_function="cosine")
|
||||
|
||||
documents = [
|
||||
Document(content="My name is Wolfgang and I live in Berlin"),
|
||||
Document(content="I saw a black horse running"),
|
||||
Document(content="Germany has many big cities"),
|
||||
]
|
||||
|
||||
indexing_pipeline = Pipeline()
|
||||
indexing_pipeline.add_component("embedder", PerplexityDocumentEmbedder())
|
||||
indexing_pipeline.add_component("writer", DocumentWriter(document_store=document_store))
|
||||
indexing_pipeline.connect("embedder", "writer")
|
||||
indexing_pipeline.run({"embedder": {"documents": documents}})
|
||||
|
||||
query_pipeline = Pipeline()
|
||||
query_pipeline.add_component("text_embedder", PerplexityTextEmbedder())
|
||||
query_pipeline.add_component(
|
||||
"retriever",
|
||||
InMemoryEmbeddingRetriever(document_store=document_store),
|
||||
)
|
||||
query_pipeline.connect("text_embedder.embedding", "retriever.query_embedding")
|
||||
|
||||
result = query_pipeline.run({"text_embedder": {"text": "Who lives in Berlin?"}})
|
||||
print(result["retriever"]["documents"][0])
|
||||
```
|
||||
@@ -0,0 +1,97 @@
|
||||
---
|
||||
title: "PerplexityTextEmbedder"
|
||||
id: perplexitytextembedder
|
||||
slug: "/perplexitytextembedder"
|
||||
description: "`PerplexityTextEmbedder` transforms a string into a vector using a Perplexity embedding model."
|
||||
---
|
||||
|
||||
# PerplexityTextEmbedder
|
||||
|
||||
`PerplexityTextEmbedder` transforms a string into a vector that captures its semantics using a Perplexity embedding model.
|
||||
|
||||
When you perform embedding retrieval, use this component to transform your query into a vector. Then, the embedding Retriever looks for similar or relevant documents.
|
||||
|
||||
<div className="key-value-table">
|
||||
|
||||
| | |
|
||||
| --- | --- |
|
||||
| **Most common position in a pipeline** | Before an embedding [Retriever](../retrievers.mdx) in a query/RAG pipeline |
|
||||
| **Mandatory init variables** | `api_key`: A Perplexity API key. Can be set with `PERPLEXITY_API_KEY` env var. |
|
||||
| **Mandatory run variables** | `text`: A string |
|
||||
| **Output variables** | `embedding`: A list of float numbers <br /> <br />`meta`: A dictionary of metadata |
|
||||
| **API reference** | [Integrations](/reference/integrations-perplexity) |
|
||||
| **GitHub link** | https://github.com/deepset-ai/haystack-core-integrations/blob/main/integrations/perplexity/src/haystack_integrations/components/embedders/perplexity/text_embedder.py |
|
||||
| **Package name** | `perplexity-haystack` |
|
||||
|
||||
</div>
|
||||
|
||||
## Overview
|
||||
|
||||
`PerplexityTextEmbedder` supports the following embedding models:
|
||||
|
||||
- `pplx-embed-v1-0.6b` (default)
|
||||
- `pplx-embed-v1-4b`
|
||||
|
||||
Use `PerplexityTextEmbedder` to embed a single string, such as a query. For embedding lists of documents, use [PerplexityDocumentEmbedder](perplexitydocumentembedder.mdx).
|
||||
|
||||
The component uses a `PERPLEXITY_API_KEY` environment variable by default. You can also pass an API key directly at initialization:
|
||||
|
||||
```python
|
||||
from haystack_integrations.components.embedders.perplexity import PerplexityTextEmbedder
|
||||
from haystack.utils import Secret
|
||||
|
||||
embedder = PerplexityTextEmbedder(api_key=Secret.from_token("<your-api-key>"))
|
||||
```
|
||||
|
||||
## Usage
|
||||
|
||||
### On its own
|
||||
|
||||
```python
|
||||
from haystack_integrations.components.embedders.perplexity import PerplexityTextEmbedder
|
||||
|
||||
text_embedder = PerplexityTextEmbedder()
|
||||
result = text_embedder.run("I love pizza!")
|
||||
print(result["embedding"])
|
||||
|
||||
# [0.017020374536514282, -0.023255806416273117, ...]
|
||||
```
|
||||
|
||||
:::info
|
||||
We recommend setting `PERPLEXITY_API_KEY` as an environment variable instead of passing it as a parameter.
|
||||
:::
|
||||
|
||||
### In a pipeline
|
||||
|
||||
```python
|
||||
from haystack import Document, Pipeline
|
||||
from haystack.document_stores.in_memory import InMemoryDocumentStore
|
||||
from haystack.components.retrievers.in_memory import InMemoryEmbeddingRetriever
|
||||
from haystack_integrations.components.embedders.perplexity import (
|
||||
PerplexityTextEmbedder,
|
||||
PerplexityDocumentEmbedder,
|
||||
)
|
||||
|
||||
document_store = InMemoryDocumentStore(embedding_similarity_function="cosine")
|
||||
|
||||
documents = [
|
||||
Document(content="My name is Wolfgang and I live in Berlin"),
|
||||
Document(content="I saw a black horse running"),
|
||||
Document(content="Germany has many big cities"),
|
||||
]
|
||||
|
||||
document_embedder = PerplexityDocumentEmbedder()
|
||||
documents_with_embeddings = document_embedder.run(documents)["documents"]
|
||||
document_store.write_documents(documents_with_embeddings)
|
||||
|
||||
query_pipeline = Pipeline()
|
||||
query_pipeline.add_component("text_embedder", PerplexityTextEmbedder())
|
||||
query_pipeline.add_component(
|
||||
"retriever",
|
||||
InMemoryEmbeddingRetriever(document_store=document_store),
|
||||
)
|
||||
query_pipeline.connect("text_embedder.embedding", "retriever.query_embedding")
|
||||
|
||||
result = query_pipeline.run({"text_embedder": {"text": "Who lives in Berlin?"}})
|
||||
print(result["retriever"]["documents"][0])
|
||||
```
|
||||
+153
@@ -0,0 +1,153 @@
|
||||
---
|
||||
title: "SentenceTransformersDocumentEmbedder"
|
||||
id: sentencetransformersdocumentembedder
|
||||
slug: "/sentencetransformersdocumentembedder"
|
||||
description: "SentenceTransformersDocumentEmbedder computes the embeddings of a list of documents and stores the obtained vectors in the embedding field of each document. It uses embedding models compatible with the Sentence Transformers library."
|
||||
---
|
||||
|
||||
# SentenceTransformersDocumentEmbedder
|
||||
|
||||
SentenceTransformersDocumentEmbedder computes the embeddings of a list of documents and stores the obtained vectors in the embedding field of each document. It uses embedding models compatible with the Sentence Transformers library.
|
||||
|
||||
The vectors computed by this component are necessary to perform embedding retrieval on a collection of documents. At retrieval time, the vector that represents the query is compared with those of the documents to find the most similar or relevant documents.
|
||||
|
||||
<div className="key-value-table">
|
||||
|
||||
| | |
|
||||
| --- | --- |
|
||||
| **Most common position in a pipeline** | Before a [`DocumentWriter`](../writers/documentwriter.mdx) in an indexing pipeline |
|
||||
| **Mandatory run variables** | `documents`: A list of documents |
|
||||
| **Output variables** | `documents`: A list of documents (enriched with embeddings) |
|
||||
| **API reference** | [Sentence Transformers](/reference/integrations-sentence-transformers) |
|
||||
| **GitHub link** | https://github.com/deepset-ai/haystack-core-integrations/tree/main/integrations/sentence_transformers |
|
||||
| **Package name** | `sentence-transformers-haystack` |
|
||||
|
||||
</div>
|
||||
|
||||
## Overview
|
||||
|
||||
`SentenceTransformersDocumentEmbedder` should be used to embed a list of documents. To embed a string, use the [SentenceTransformersTextEmbedder](sentencetransformerstextembedder.mdx).
|
||||
|
||||
### Authentication
|
||||
|
||||
Authentication with a Hugging Face API Token is only required to access private or gated models through Serverless Inference API or the Inference Endpoints.
|
||||
|
||||
The component uses an `HF_API_TOKEN` or `HF_TOKEN` environment variable, or you can pass a Hugging Face API token at initialization. See our [Secret Management](../../concepts/secret-management.mdx) page for more information.
|
||||
|
||||
```python
|
||||
document_embedder = SentenceTransformersDocumentEmbedder(
|
||||
token=Secret.from_token("<your-api-key>"),
|
||||
)
|
||||
```
|
||||
|
||||
### Compatible Models
|
||||
|
||||
The default embedding model is [\`sentence-transformers/all-mpnet-base-v2](https://huggingface.co/sentence-transformers/all-mpnet-base-v2)\`. You can specify another model with the `model` parameter when initializing this component.
|
||||
|
||||
See the original models in the Sentence Transformers [documentation](https://www.sbert.net/docs/pretrained_models.html).
|
||||
|
||||
Nowadays, most of the models in the [Massive Text Embedding Benchmark (MTEB) Leaderboard](https://huggingface.co/spaces/mteb/leaderboard) are compatible with Sentence Transformers.
|
||||
You can look for compatibility in the model card: [an example related to BGE models](https://huggingface.co/BAAI/bge-large-en-v1.5#using-sentence-transformers).
|
||||
|
||||
### Instructions
|
||||
|
||||
Some recent models that you can find in MTEB require prepending the text with an instruction to work better for retrieval.
|
||||
For example, if you use [intfloat/e5-large-v2](https://huggingface.co/BAAI/bge-large-en-v1.5#model-list), you should prefix your document with the following instruction: “passage:”
|
||||
|
||||
This is how it works with `SentenceTransformersDocumentEmbedder`:
|
||||
|
||||
```python
|
||||
embedder = SentenceTransformersDocumentEmbedder(
|
||||
model="intfloat/e5-large-v2",
|
||||
prefix="passage",
|
||||
)
|
||||
```
|
||||
|
||||
### Embedding Metadata
|
||||
|
||||
Text documents often come with a set of metadata. If they are distinctive and semantically meaningful, you can embed them along with the text of the document to improve retrieval.
|
||||
|
||||
You can do this easily by using the Document Embedder:
|
||||
|
||||
```python
|
||||
from haystack import Document
|
||||
from haystack_integrations.components.embedders.sentence_transformers import (
|
||||
SentenceTransformersDocumentEmbedder,
|
||||
)
|
||||
|
||||
doc = Document(content="some text", meta={"title": "relevant title", "page number": 18})
|
||||
|
||||
embedder = SentenceTransformersDocumentEmbedder(meta_fields_to_embed=["title"])
|
||||
|
||||
docs_w_embeddings = embedder.run(documents=[doc])["documents"]
|
||||
```
|
||||
|
||||
## Usage
|
||||
|
||||
Install the `sentence-transformers-haystack` package to use the `SentenceTransformersDocumentEmbedder`:
|
||||
|
||||
```shell
|
||||
pip install sentence-transformers-haystack
|
||||
```
|
||||
|
||||
### On its own
|
||||
|
||||
```python
|
||||
from haystack import Document
|
||||
from haystack_integrations.components.embedders.sentence_transformers import (
|
||||
SentenceTransformersDocumentEmbedder,
|
||||
)
|
||||
|
||||
doc = Document(content="I love pizza!")
|
||||
doc_embedder = SentenceTransformersDocumentEmbedder()
|
||||
|
||||
result = doc_embedder.run([doc])
|
||||
print(result["documents"][0].embedding)
|
||||
|
||||
# [-0.07804739475250244, 0.1498992145061493, ...]
|
||||
```
|
||||
|
||||
### In a pipeline
|
||||
|
||||
```python
|
||||
from haystack import Document
|
||||
from haystack import Pipeline
|
||||
from haystack.document_stores.in_memory import InMemoryDocumentStore
|
||||
from haystack_integrations.components.embedders.sentence_transformers import (
|
||||
SentenceTransformersTextEmbedder,
|
||||
SentenceTransformersDocumentEmbedder,
|
||||
)
|
||||
from haystack.components.writers import DocumentWriter
|
||||
from haystack.components.retrievers.in_memory import InMemoryEmbeddingRetriever
|
||||
|
||||
document_store = InMemoryDocumentStore(embedding_similarity_function="cosine")
|
||||
|
||||
documents = [
|
||||
Document(content="My name is Wolfgang and I live in Berlin"),
|
||||
Document(content="I saw a black horse running"),
|
||||
Document(content="Germany has many big cities"),
|
||||
]
|
||||
|
||||
indexing_pipeline = Pipeline()
|
||||
indexing_pipeline.add_component("embedder", SentenceTransformersDocumentEmbedder())
|
||||
indexing_pipeline.add_component("writer", DocumentWriter(document_store=document_store))
|
||||
indexing_pipeline.connect("embedder", "writer")
|
||||
|
||||
query_pipeline = Pipeline()
|
||||
query_pipeline.add_component("text_embedder", SentenceTransformersTextEmbedder())
|
||||
query_pipeline.add_component(
|
||||
"retriever",
|
||||
InMemoryEmbeddingRetriever(document_store=document_store),
|
||||
)
|
||||
query_pipeline.connect("text_embedder.embedding", "retriever.query_embedding")
|
||||
|
||||
query = "Who lives in Berlin?"
|
||||
|
||||
indexing_pipeline.run({"documents": documents})
|
||||
result = query_pipeline.run({"text_embedder": {"text": query}})
|
||||
|
||||
print(result["retriever"]["documents"][0])
|
||||
|
||||
# Document(id=..., mimetype: 'text/plain',
|
||||
# text: 'My name is Wolfgang and I live in Berlin')
|
||||
```
|
||||
+180
@@ -0,0 +1,180 @@
|
||||
---
|
||||
title: "SentenceTransformersDocumentImageEmbedder"
|
||||
id: sentencetransformersdocumentimageembedder
|
||||
slug: "/sentencetransformersdocumentimageembedder"
|
||||
description: "`SentenceTransformersDocumentImageEmbedder` computes the image embeddings of a list of documents and stores the obtained vectors in the embedding field of each document. It uses Sentence Transformers embedding models with the ability to embed text and images into the same vector space."
|
||||
---
|
||||
|
||||
# SentenceTransformersDocumentImageEmbedder
|
||||
|
||||
`SentenceTransformersDocumentImageEmbedder` computes the image embeddings of a list of documents and stores the obtained vectors in the embedding field of each document. It uses Sentence Transformers embedding models with the ability to embed text and images into the same vector space.
|
||||
|
||||
<div className="key-value-table">
|
||||
|
||||
| | |
|
||||
| --- | --- |
|
||||
| **Most common position in a pipeline** | Before a [`DocumentWriter`](../writers/documentwriter.mdx) in an indexing pipeline |
|
||||
| **Mandatory init variables** | `token` (only for private models): The Hugging Face API token. Can be set with `HF_API_TOKEN` or `HF_TOKEN` env var. |
|
||||
| **Mandatory run variables** | `documents`: A list of documents, with a meta field containing an image file path |
|
||||
| **Output variables** | `documents`: A list of documents (enriched with embeddings) |
|
||||
| **API reference** | [Sentence Transformers](/reference/integrations-sentence-transformers) |
|
||||
| **GitHub link** | https://github.com/deepset-ai/haystack-core-integrations/tree/main/integrations/sentence_transformers |
|
||||
| **Package name** | `sentence-transformers-haystack` |
|
||||
|
||||
</div>
|
||||
|
||||
## Overview
|
||||
|
||||
`SentenceTransformersDocumentImageEmbedder` expects a list of documents containing an image or a PDF file path in a meta field. The meta field can be specified with the `file_path_meta_field` init parameter of this component.
|
||||
|
||||
The embedder efficiently loads the images, computes the embeddings using a Sentence Transformers models, and stores each of them in the `embedding` field of the document.
|
||||
|
||||
`SentenceTransformersDocumentImageEmbedder` is commonly used in indexing pipelines. At retrieval time, you need to use the same model with a `SentenceTransformersTextEmbedder` to embed the query before using an Embedding Retriever.
|
||||
|
||||
You can set the `device` parameter to use HF models on your CPU or GPU.
|
||||
|
||||
Additionally, you can select the backend to use for the Sentence Transformers mode with the `backend` parameterl: `torch` (default), `onnx`, or `openvino`. ONNX and OpenVINO allow specific speed optimizations; for more information, read the [Sentence Transformers documentation](https://sbert.net/docs/sentence_transformer/usage/efficiency.html).
|
||||
|
||||
### Authentication
|
||||
|
||||
Authentication with a Hugging Face API Token is only required to access private or gated models.
|
||||
|
||||
The component uses an `HF_API_TOKEN` or `HF_TOKEN` environment variable, or you can pass a Hugging Face API token at initialization. See our [Secret Management](../../concepts/secret-management.mdx) page for more information.
|
||||
|
||||
### Compatible Models
|
||||
|
||||
To be used with this component, the model must be compatible with Sentence Transformers and
|
||||
|
||||
able to embed images and text into the same vector space. Compatible models include:
|
||||
|
||||
- `sentence-transformers/clip-ViT-B-32` (default)
|
||||
- `sentence-transformers/clip-ViT-L-14`
|
||||
- `sentence-transformers/clip-ViT-B-16`
|
||||
- `sentence-transformers/clip-ViT-B-32-multilingual-v1`
|
||||
- `jinaai/jina-embeddings-v4`
|
||||
- `jinaai/jina-clip-v1`
|
||||
- `jinaai/jina-clip-v2`
|
||||
|
||||
## Usage
|
||||
|
||||
Install the `sentence-transformers-haystack` package to use the `SentenceTransformersDocumentImageEmbedder`:
|
||||
|
||||
```shell
|
||||
pip install sentence-transformers-haystack
|
||||
```
|
||||
|
||||
### On its own
|
||||
|
||||
```python
|
||||
from haystack import Document
|
||||
from haystack_integrations.components.embedders.sentence_transformers import (
|
||||
SentenceTransformersDocumentImageEmbedder,
|
||||
)
|
||||
|
||||
embedder = SentenceTransformersDocumentImageEmbedder(
|
||||
model="sentence-transformers/clip-ViT-B-32",
|
||||
)
|
||||
|
||||
documents = [
|
||||
Document(content="A photo of a cat", meta={"file_path": "cat.jpg"}),
|
||||
Document(content="A photo of a dog", meta={"file_path": "dog.jpg"}),
|
||||
]
|
||||
|
||||
result = embedder.run(documents=documents)
|
||||
documents_with_embeddings = result["documents"]
|
||||
print(documents_with_embeddings)
|
||||
|
||||
# [Document(id=...,
|
||||
# content='A photo of a cat',
|
||||
# meta={'file_path': 'cat.jpg',
|
||||
# 'embedding_source': {'type': 'image', 'file_path_meta_field': 'file_path'}},
|
||||
# embedding=vector of size 512),
|
||||
# ...]
|
||||
```
|
||||
|
||||
### In a pipeline
|
||||
|
||||
In this example, we can see an indexing pipeline with 3 components:
|
||||
|
||||
- `ImageFileToDocument` Converter that creates empty documents with a reference to an image in the `meta.file_path` field,
|
||||
- `SentenceTransformersDocumentImageEmbedder` that loads the images, computes embeddings and stores them in documents,
|
||||
- `DocumentWriter` that writes the documents in the `InMemoryDocumentStore`
|
||||
|
||||
There is also a multimodal retrieval pipeline, composed by a `SentenceTransformersTextEmbedder` (using the same model as before) and an `InMemoryEmbeddingRetriever`.
|
||||
|
||||
```python
|
||||
from haystack import Pipeline
|
||||
from haystack.components.converters.image import ImageFileToDocument
|
||||
from haystack_integrations.components.embedders.sentence_transformers import (
|
||||
SentenceTransformersTextEmbedder,
|
||||
SentenceTransformersDocumentImageEmbedder,
|
||||
)
|
||||
from haystack.components.retrievers.in_memory import InMemoryEmbeddingRetriever
|
||||
from haystack.components.writers import DocumentWriter
|
||||
from haystack.document_stores.in_memory import InMemoryDocumentStore
|
||||
|
||||
document_store = InMemoryDocumentStore()
|
||||
|
||||
# Indexing pipeline
|
||||
indexing_pipeline = Pipeline()
|
||||
indexing_pipeline.add_component("image_converter", ImageFileToDocument())
|
||||
indexing_pipeline.add_component(
|
||||
"embedder",
|
||||
SentenceTransformersDocumentImageEmbedder(
|
||||
model="sentence-transformers/clip-ViT-B-32",
|
||||
),
|
||||
)
|
||||
indexing_pipeline.add_component("writer", DocumentWriter(document_store=document_store))
|
||||
indexing_pipeline.connect("image_converter", "embedder")
|
||||
indexing_pipeline.connect("embedder", "writer")
|
||||
|
||||
indexing_pipeline.run(data={"image_converter": {"sources": ["dog.jpg", "hyena.jpeg"]}})
|
||||
|
||||
# Multimodal retrieval pipeline
|
||||
retrieval_pipeline = Pipeline()
|
||||
retrieval_pipeline.add_component(
|
||||
"embedder",
|
||||
SentenceTransformersTextEmbedder(model="sentence-transformers/clip-ViT-B-32"),
|
||||
)
|
||||
retrieval_pipeline.add_component(
|
||||
"retriever",
|
||||
InMemoryEmbeddingRetriever(document_store=document_store, top_k=2),
|
||||
)
|
||||
retrieval_pipeline.connect("embedder", "retriever")
|
||||
|
||||
result = retrieval_pipeline.run(data={"text": "man's best friend"})
|
||||
print(result)
|
||||
|
||||
# {
|
||||
# 'retriever': {
|
||||
# 'documents': [
|
||||
# Document(
|
||||
# id=0c96...,
|
||||
# meta={
|
||||
# 'file_path': 'dog.jpg',
|
||||
# 'embedding_source': {
|
||||
# 'type': 'image',
|
||||
# 'file_path_meta_field': 'file_path'
|
||||
# }
|
||||
# },
|
||||
# score=32.025817780129856
|
||||
# ),
|
||||
# Document(
|
||||
# id=5e76...,
|
||||
# meta={
|
||||
# 'file_path': 'hyena.jpeg',
|
||||
# 'embedding_source': {
|
||||
# 'type': 'image',
|
||||
# 'file_path_meta_field': 'file_path'
|
||||
# }
|
||||
# },
|
||||
# score=20.648225327085242
|
||||
# )
|
||||
# ]
|
||||
# }
|
||||
# }
|
||||
```
|
||||
|
||||
## Additional References
|
||||
|
||||
🧑🍳 Cookbook: [Introduction to Multimodality](https://haystack.deepset.ai/cookbook/multimodal_intro)
|
||||
+197
@@ -0,0 +1,197 @@
|
||||
---
|
||||
title: "SentenceTransformersSparseDocumentEmbedder"
|
||||
id: sentencetransformerssparsedocumentembedder
|
||||
slug: "/sentencetransformerssparsedocumentembedder"
|
||||
description: "Use this component to enrich a list of documents with their sparse embeddings using Sentence Transformers models."
|
||||
---
|
||||
|
||||
# SentenceTransformersSparseDocumentEmbedder
|
||||
|
||||
Use this component to enrich a list of documents with their sparse embeddings using Sentence Transformers models.
|
||||
|
||||
<div className="key-value-table">
|
||||
|
||||
| | |
|
||||
| --- | --- |
|
||||
| **Most common position in a pipeline** | Before a [`DocumentWriter`](../writers/documentwriter.mdx) in an indexing pipeline |
|
||||
| **Mandatory run variables** | `documents`: A list of documents |
|
||||
| **Output variables** | `documents`: A list of documents (enriched with sparse embeddings) |
|
||||
| **API reference** | [Sentence Transformers](/reference/integrations-sentence-transformers) |
|
||||
| **GitHub link** | https://github.com/deepset-ai/haystack-core-integrations/tree/main/integrations/sentence_transformers |
|
||||
| **Package name** | `sentence-transformers-haystack` |
|
||||
|
||||
</div>
|
||||
|
||||
To compute a sparse embedding for a string, use the [`SentenceTransformersSparseTextEmbedder`](sentencetransformerssparsetextembedder.mdx).
|
||||
|
||||
## Overview
|
||||
|
||||
`SentenceTransformersSparseDocumentEmbedder` computes the sparse embeddings of a list of documents and stores the obtained vectors in the `sparse_embedding` field of each document. It uses sparse embedding models supported by the Sentence Transformers library.
|
||||
|
||||
The vectors computed by this component are necessary to perform sparse embedding retrieval on a collection of documents. At retrieval time, the sparse vector representing the query is compared with those of the documents to find the most similar or relevant ones.
|
||||
|
||||
### Compatible Models
|
||||
|
||||
The default embedding model is [`prithivida/Splade_PP_en_v2`](https://huggingface.co/prithivida/Splade_PP_en_v2). You can specify another model with the `model` parameter when initializing this component.
|
||||
|
||||
Compatible models are based on SPLADE (SParse Lexical AnD Expansion), a technique for producing sparse representations for text, where each non-zero value in the embedding is the importance weight of a term in the vocabulary. This approach combines the benefits of learned sparse representations with the efficiency of traditional sparse retrieval methods. For more information, see [our docs](../retrievers.mdx#sparse-embedding-based-retrievers) that explain sparse embedding-based Retrievers further.
|
||||
|
||||
You can find compatible SPLADE models on the [Hugging Face Model Hub](https://huggingface.co/models?search=splade).
|
||||
|
||||
### Authentication
|
||||
|
||||
Authentication with a Hugging Face API Token is only required to access private or gated models.
|
||||
|
||||
The component uses an `HF_API_TOKEN` or `HF_TOKEN` environment variable, or you can pass a Hugging Face API token at initialization. See our [Secret Management](../../concepts/secret-management.mdx) page for more information.
|
||||
|
||||
```python
|
||||
from haystack.utils import Secret
|
||||
from haystack_integrations.components.embedders.sentence_transformers import (
|
||||
SentenceTransformersSparseDocumentEmbedder,
|
||||
)
|
||||
|
||||
document_embedder = SentenceTransformersSparseDocumentEmbedder(
|
||||
token=Secret.from_token("<your-api-key>"),
|
||||
)
|
||||
```
|
||||
|
||||
### Backend Options
|
||||
|
||||
This component supports multiple backends for model execution:
|
||||
|
||||
- **torch** (default): Standard PyTorch backend
|
||||
- **onnx**: Optimized ONNX Runtime backend for faster inference
|
||||
- **openvino**: Intel OpenVINO backend for additional optimizations on Intel hardware
|
||||
|
||||
You can specify the backend during initialization:
|
||||
|
||||
```python
|
||||
embedder = SentenceTransformersSparseDocumentEmbedder(
|
||||
model="prithivida/Splade_PP_en_v2",
|
||||
backend="onnx",
|
||||
)
|
||||
```
|
||||
|
||||
For more information on acceleration and quantization options, refer to the [Sentence Transformers documentation](https://sbert.net/docs/sentence_transformer/usage/efficiency.html).
|
||||
|
||||
### Embedding Metadata
|
||||
|
||||
Text documents often include metadata. If the metadata is distinctive and semantically meaningful, you can embed it along with the document's text to improve retrieval.
|
||||
|
||||
You can do this easily by using the Sparse Document Embedder:
|
||||
|
||||
```python
|
||||
from haystack import Document
|
||||
from haystack_integrations.components.embedders.sentence_transformers import (
|
||||
SentenceTransformersSparseDocumentEmbedder,
|
||||
)
|
||||
|
||||
doc = Document(content="some text", meta={"title": "relevant title", "page number": 18})
|
||||
|
||||
embedder = SentenceTransformersSparseDocumentEmbedder(meta_fields_to_embed=["title"])
|
||||
|
||||
docs_w_sparse_embeddings = embedder.run(documents=[doc])["documents"]
|
||||
```
|
||||
|
||||
## Usage
|
||||
|
||||
Install the `sentence-transformers-haystack` package to use the `SentenceTransformersSparseDocumentEmbedder`:
|
||||
|
||||
```shell
|
||||
pip install sentence-transformers-haystack
|
||||
```
|
||||
|
||||
### On its own
|
||||
|
||||
```python
|
||||
from haystack import Document
|
||||
from haystack_integrations.components.embedders.sentence_transformers import (
|
||||
SentenceTransformersSparseDocumentEmbedder,
|
||||
)
|
||||
|
||||
doc = Document(content="I love pizza!")
|
||||
doc_embedder = SentenceTransformersSparseDocumentEmbedder()
|
||||
|
||||
result = doc_embedder.run([doc])
|
||||
print(result["documents"][0].sparse_embedding)
|
||||
|
||||
# SparseEmbedding(indices=[999, 1045, ...], values=[0.918, 0.867, ...])
|
||||
```
|
||||
|
||||
### In a pipeline
|
||||
|
||||
Currently, sparse embedding retrieval is only supported by `QdrantDocumentStore`.
|
||||
|
||||
First, install the required package:
|
||||
|
||||
```shell
|
||||
pip install qdrant-haystack
|
||||
```
|
||||
|
||||
Then, try out this pipeline:
|
||||
|
||||
```python
|
||||
from haystack import Document, Pipeline
|
||||
from haystack_integrations.components.embedders.sentence_transformers import (
|
||||
SentenceTransformersSparseDocumentEmbedder,
|
||||
SentenceTransformersSparseTextEmbedder,
|
||||
)
|
||||
from haystack.components.writers import DocumentWriter
|
||||
from haystack_integrations.components.retrievers.qdrant import (
|
||||
QdrantSparseEmbeddingRetriever,
|
||||
)
|
||||
from haystack_integrations.document_stores.qdrant import QdrantDocumentStore
|
||||
from haystack.document_stores.types import DuplicatePolicy
|
||||
|
||||
document_store = QdrantDocumentStore(
|
||||
":memory:",
|
||||
recreate_index=True,
|
||||
use_sparse_embeddings=True,
|
||||
)
|
||||
|
||||
documents = [
|
||||
Document(content="My name is Wolfgang and I live in Berlin"),
|
||||
Document(content="I saw a black horse running"),
|
||||
Document(content="Germany has many big cities"),
|
||||
Document(content="Sentence Transformers provides sparse embedding models."),
|
||||
]
|
||||
|
||||
# Indexing pipeline
|
||||
indexing_pipeline = Pipeline()
|
||||
indexing_pipeline.add_component(
|
||||
"sparse_document_embedder",
|
||||
SentenceTransformersSparseDocumentEmbedder(),
|
||||
)
|
||||
indexing_pipeline.add_component(
|
||||
"writer",
|
||||
DocumentWriter(document_store=document_store, policy=DuplicatePolicy.OVERWRITE),
|
||||
)
|
||||
indexing_pipeline.connect("sparse_document_embedder", "writer")
|
||||
|
||||
indexing_pipeline.run({"sparse_document_embedder": {"documents": documents}})
|
||||
|
||||
# Query pipeline
|
||||
query_pipeline = Pipeline()
|
||||
query_pipeline.add_component(
|
||||
"sparse_text_embedder",
|
||||
SentenceTransformersSparseTextEmbedder(),
|
||||
)
|
||||
query_pipeline.add_component(
|
||||
"sparse_retriever",
|
||||
QdrantSparseEmbeddingRetriever(document_store=document_store),
|
||||
)
|
||||
query_pipeline.connect(
|
||||
"sparse_text_embedder.sparse_embedding",
|
||||
"sparse_retriever.query_sparse_embedding",
|
||||
)
|
||||
|
||||
query = "Who provides sparse embedding models?"
|
||||
|
||||
result = query_pipeline.run({"sparse_text_embedder": {"text": query}})
|
||||
|
||||
print(result["sparse_retriever"]["documents"][0])
|
||||
|
||||
# Document(id=...,
|
||||
# content: 'Sentence Transformers provides sparse embedding models.',
|
||||
# score: 0.75...)
|
||||
```
|
||||
+184
@@ -0,0 +1,184 @@
|
||||
---
|
||||
title: "SentenceTransformersSparseTextEmbedder"
|
||||
id: sentencetransformerssparsetextembedder
|
||||
slug: "/sentencetransformerssparsetextembedder"
|
||||
description: "Use this component to embed a simple string (such as a query) into a sparse vector using Sentence Transformers models."
|
||||
---
|
||||
|
||||
# SentenceTransformersSparseTextEmbedder
|
||||
|
||||
Use this component to embed a simple string (such as a query) into a sparse vector using Sentence Transformers models.
|
||||
|
||||
<div className="key-value-table">
|
||||
|
||||
| | |
|
||||
| --- | --- |
|
||||
| **Most common position in a pipeline** | Before a sparse embedding [Retriever](../retrievers.mdx) in a query/RAG pipeline |
|
||||
| **Mandatory run variables** | `text`: A string |
|
||||
| **Output variables** | `sparse_embedding`: A [`SparseEmbedding`](../../concepts/data-classes.mdx#sparseembedding) object |
|
||||
| **API reference** | [Sentence Transformers](/reference/integrations-sentence-transformers) |
|
||||
| **GitHub link** | https://github.com/deepset-ai/haystack-core-integrations/tree/main/integrations/sentence_transformers |
|
||||
| **Package name** | `sentence-transformers-haystack` |
|
||||
|
||||
</div>
|
||||
|
||||
For embedding lists of documents, use the [`SentenceTransformersSparseDocumentEmbedder`](sentencetransformerssparsedocumentembedder.mdx), which enriches the document with the computed sparse embedding.
|
||||
|
||||
## Overview
|
||||
|
||||
`SentenceTransformersSparseTextEmbedder` transforms a string into a sparse vector using sparse embedding models supported by the Sentence Transformers library.
|
||||
|
||||
When you perform sparse embedding retrieval, use this component first to transform your query into a sparse vector. Then, the Retriever will use the sparse vector to search for similar or relevant documents.
|
||||
|
||||
### Compatible Models
|
||||
|
||||
The default embedding model is [`prithivida/Splade_PP_en_v2`](https://huggingface.co/prithivida/Splade_PP_en_v2). You can specify another model with the `model` parameter when initializing this component.
|
||||
|
||||
Compatible models are based on SPLADE (SParse Lexical AnD Expansion), a technique for producing sparse representations for text, where each non-zero value in the embedding is the importance weight of a term in the vocabulary. This approach combines the benefits of learned sparse representations with the efficiency of traditional sparse retrieval methods. For more information, see [our docs](../retrievers.mdx#sparse-embedding-based-retrievers) that explain sparse embedding-based Retrievers further.
|
||||
|
||||
You can find compatible SPLADE models on the [Hugging Face Model Hub](https://huggingface.co/models?search=splade).
|
||||
|
||||
### Authentication
|
||||
|
||||
Authentication with a Hugging Face API Token is only required to access private or gated models.
|
||||
|
||||
The component uses an `HF_API_TOKEN` or `HF_TOKEN` environment variable, or you can pass a Hugging Face API token at initialization. See our [Secret Management](../../concepts/secret-management.mdx) page for more information.
|
||||
|
||||
```python
|
||||
from haystack.utils import Secret
|
||||
from haystack_integrations.components.embedders.sentence_transformers import (
|
||||
SentenceTransformersSparseTextEmbedder,
|
||||
)
|
||||
|
||||
text_embedder = SentenceTransformersSparseTextEmbedder(
|
||||
token=Secret.from_token("<your-api-key>"),
|
||||
)
|
||||
```
|
||||
|
||||
### Backend Options
|
||||
|
||||
This component supports multiple backends for model execution:
|
||||
|
||||
- **torch** (default): Standard PyTorch backend
|
||||
- **onnx**: Optimized ONNX Runtime backend for faster inference
|
||||
- **openvino**: Intel OpenVINO backend for additional optimizations on Intel hardware
|
||||
|
||||
You can specify the backend during initialization:
|
||||
|
||||
```python
|
||||
embedder = SentenceTransformersSparseTextEmbedder(
|
||||
model="prithivida/Splade_PP_en_v2",
|
||||
backend="onnx",
|
||||
)
|
||||
```
|
||||
|
||||
For more information on acceleration and quantization options, refer to the [Sentence Transformers documentation](https://sbert.net/docs/sentence_transformer/usage/efficiency.html).
|
||||
|
||||
### Prefix and Suffix
|
||||
|
||||
Some models may benefit from adding a prefix or suffix to the text before embedding. You can specify these during initialization:
|
||||
|
||||
```python
|
||||
embedder = SentenceTransformersSparseTextEmbedder(
|
||||
model="prithivida/Splade_PP_en_v2",
|
||||
prefix="query: ",
|
||||
suffix="",
|
||||
)
|
||||
```
|
||||
|
||||
:::tip
|
||||
If you create a Sparse Text Embedder and a Sparse Document Embedder based on the same model, Haystack takes care of using the same resource behind the scenes in order to save resources.
|
||||
:::
|
||||
|
||||
## Usage
|
||||
|
||||
Install the `sentence-transformers-haystack` package to use the `SentenceTransformersSparseTextEmbedder`:
|
||||
|
||||
```shell
|
||||
pip install sentence-transformers-haystack
|
||||
```
|
||||
|
||||
### On its own
|
||||
|
||||
```python
|
||||
from haystack_integrations.components.embedders.sentence_transformers import (
|
||||
SentenceTransformersSparseTextEmbedder,
|
||||
)
|
||||
|
||||
text_to_embed = "I love pizza!"
|
||||
|
||||
text_embedder = SentenceTransformersSparseTextEmbedder()
|
||||
|
||||
print(text_embedder.run(text_to_embed))
|
||||
|
||||
# {'sparse_embedding': SparseEmbedding(indices=[999, 1045, ...], values=[0.918, 0.867, ...])}
|
||||
```
|
||||
|
||||
### In a pipeline
|
||||
|
||||
Currently, sparse embedding retrieval is only supported by `QdrantDocumentStore`.
|
||||
|
||||
First, install the required package:
|
||||
|
||||
```shell
|
||||
pip install qdrant-haystack
|
||||
```
|
||||
|
||||
Then, try out this pipeline:
|
||||
|
||||
```python
|
||||
from haystack import Document, Pipeline
|
||||
from haystack_integrations.components.embedders.sentence_transformers import (
|
||||
SentenceTransformersSparseDocumentEmbedder,
|
||||
SentenceTransformersSparseTextEmbedder,
|
||||
)
|
||||
from haystack_integrations.components.retrievers.qdrant import (
|
||||
QdrantSparseEmbeddingRetriever,
|
||||
)
|
||||
from haystack_integrations.document_stores.qdrant import QdrantDocumentStore
|
||||
|
||||
document_store = QdrantDocumentStore(
|
||||
":memory:",
|
||||
recreate_index=True,
|
||||
use_sparse_embeddings=True,
|
||||
)
|
||||
|
||||
documents = [
|
||||
Document(content="My name is Wolfgang and I live in Berlin"),
|
||||
Document(content="I saw a black horse running"),
|
||||
Document(content="Germany has many big cities"),
|
||||
Document(content="Sentence Transformers provides sparse embedding models."),
|
||||
]
|
||||
|
||||
# Embed and write documents
|
||||
sparse_document_embedder = SentenceTransformersSparseDocumentEmbedder(
|
||||
model="prithivida/Splade_PP_en_v2",
|
||||
)
|
||||
documents_with_sparse_embeddings = sparse_document_embedder.run(documents)["documents"]
|
||||
document_store.write_documents(documents_with_sparse_embeddings)
|
||||
|
||||
# Query pipeline
|
||||
query_pipeline = Pipeline()
|
||||
query_pipeline.add_component(
|
||||
"sparse_text_embedder",
|
||||
SentenceTransformersSparseTextEmbedder(),
|
||||
)
|
||||
query_pipeline.add_component(
|
||||
"sparse_retriever",
|
||||
QdrantSparseEmbeddingRetriever(document_store=document_store),
|
||||
)
|
||||
query_pipeline.connect(
|
||||
"sparse_text_embedder.sparse_embedding",
|
||||
"sparse_retriever.query_sparse_embedding",
|
||||
)
|
||||
|
||||
query = "Who provides sparse embedding models?"
|
||||
|
||||
result = query_pipeline.run({"sparse_text_embedder": {"text": query}})
|
||||
|
||||
print(result["sparse_retriever"]["documents"][0])
|
||||
|
||||
# Document(id=...,
|
||||
# content: 'Sentence Transformers provides sparse embedding models.',
|
||||
# score: 0.56...)
|
||||
```
|
||||
@@ -0,0 +1,134 @@
|
||||
---
|
||||
title: "SentenceTransformersTextEmbedder"
|
||||
id: sentencetransformerstextembedder
|
||||
slug: "/sentencetransformerstextembedder"
|
||||
description: "SentenceTransformersTextEmbedder transforms a string into a vector that captures its semantics using an embedding model compatible with the Sentence Transformers library."
|
||||
---
|
||||
|
||||
# SentenceTransformersTextEmbedder
|
||||
|
||||
SentenceTransformersTextEmbedder transforms a string into a vector that captures its semantics using an embedding model compatible with the Sentence Transformers library.
|
||||
|
||||
When you perform embedding retrieval, use this component first to transform your query into a vector. Then, the embedding Retriever will use the vector to search for similar or relevant documents.
|
||||
|
||||
<div className="key-value-table">
|
||||
|
||||
| | |
|
||||
| --- | --- |
|
||||
| **Most common position in a pipeline** | Before an embedding [Retriever](../retrievers.mdx) in a query/RAG pipeline |
|
||||
| **Mandatory run variables** | `text`: A string |
|
||||
| **Output variables** | `embedding`: A list of float numbers |
|
||||
| **API reference** | [Sentence Transformers](/reference/integrations-sentence-transformers) |
|
||||
| **GitHub link** | https://github.com/deepset-ai/haystack-core-integrations/tree/main/integrations/sentence_transformers |
|
||||
| **Package name** | `sentence-transformers-haystack` |
|
||||
|
||||
</div>
|
||||
|
||||
## Overview
|
||||
|
||||
This component should be used to embed a simple string (such as a query) into a vector. For embedding lists of documents, use the [SentenceTransformersDocumentEmbedder](sentencetransformersdocumentembedder.mdx), which enriches the document with the computed embedding, known as vector.
|
||||
|
||||
### Authentication
|
||||
|
||||
Authentication with a Hugging Face API Token is only required to access private or gated models through Serverless Inference API or the Inference Endpoints.
|
||||
|
||||
The component uses an `HF_API_TOKEN` or `HF_TOKEN` environment variable, or you can pass a Hugging Face API token at initialization. See our [Secret Management](../../concepts/secret-management.mdx) page for more information.
|
||||
|
||||
```python
|
||||
text_embedder = SentenceTransformersTextEmbedder(
|
||||
token=Secret.from_token("<your-api-key>"),
|
||||
)
|
||||
```
|
||||
|
||||
### Compatible Models
|
||||
|
||||
The default embedding model is [\`sentence-transformers/all-mpnet-base-v2](https://huggingface.co/sentence-transformers/all-mpnet-base-v2)\`. You can specify another model with the `model` parameter when initializing this component.
|
||||
|
||||
See the original models in the Sentence Transformers [documentation](https://www.sbert.net/docs/pretrained_models.html).
|
||||
|
||||
Nowadays, most of the models in the [Massive Text Embedding Benchmark (MTEB) Leaderboard](https://huggingface.co/spaces/mteb/leaderboard) are compatible with Sentence Transformers.
|
||||
You can look for compatibility in the model card: [an example related to BGE models](https://huggingface.co/BAAI/bge-large-en-v1.5#using-sentence-transformers).
|
||||
|
||||
### Instructions
|
||||
|
||||
Some recent models that you can find in MTEB require prepending the text with an instruction to work better for retrieval.
|
||||
For example, if you use [BAAI/bge-large-en-v1.5](https://huggingface.co/BAAI/bge-large-en-v1.5#model-list), you should prefix your query with the following instruction: “Represent this sentence for searching relevant passages:”
|
||||
|
||||
This is how it works with `SentenceTransformersTextEmbedder`:
|
||||
|
||||
```python
|
||||
instruction = "Represent this sentence for searching relevant passages:"
|
||||
embedder = SentenceTransformersTextEmbedder(
|
||||
*model="*BAAI/bge-large-en-v1.5",
|
||||
prefix=instruction)
|
||||
```
|
||||
|
||||
:::tip
|
||||
If you create a Text Embedder and a Document Embedder based on the same model, Haystack takes care of using the same resource behind the scenes in order to save resources.
|
||||
:::
|
||||
|
||||
## Usage
|
||||
|
||||
Install the `sentence-transformers-haystack` package to use the `SentenceTransformersTextEmbedder`:
|
||||
|
||||
```shell
|
||||
pip install sentence-transformers-haystack
|
||||
```
|
||||
|
||||
### On its own
|
||||
|
||||
```python
|
||||
from haystack_integrations.components.embedders.sentence_transformers import (
|
||||
SentenceTransformersTextEmbedder,
|
||||
)
|
||||
|
||||
text_to_embed = "I love pizza!"
|
||||
|
||||
text_embedder = SentenceTransformersTextEmbedder()
|
||||
|
||||
print(text_embedder.run(text_to_embed))
|
||||
|
||||
# {'embedding': [-0.07804739475250244, 0.1498992145061493,, ...]}
|
||||
```
|
||||
|
||||
### In a pipeline
|
||||
|
||||
```python
|
||||
from haystack import Document
|
||||
from haystack import Pipeline
|
||||
from haystack.document_stores.in_memory import InMemoryDocumentStore
|
||||
from haystack_integrations.components.embedders.sentence_transformers import (
|
||||
SentenceTransformersTextEmbedder,
|
||||
SentenceTransformersDocumentEmbedder,
|
||||
)
|
||||
from haystack.components.retrievers.in_memory import InMemoryEmbeddingRetriever
|
||||
|
||||
document_store = InMemoryDocumentStore(embedding_similarity_function="cosine")
|
||||
|
||||
documents = [
|
||||
Document(content="My name is Wolfgang and I live in Berlin"),
|
||||
Document(content="I saw a black horse running"),
|
||||
Document(content="Germany has many big cities"),
|
||||
]
|
||||
|
||||
document_embedder = SentenceTransformersDocumentEmbedder()
|
||||
documents_with_embeddings = document_embedder.run(documents)["documents"]
|
||||
document_store.write_documents(documents_with_embeddings)
|
||||
|
||||
query_pipeline = Pipeline()
|
||||
query_pipeline.add_component("text_embedder", SentenceTransformersTextEmbedder())
|
||||
query_pipeline.add_component(
|
||||
"retriever",
|
||||
InMemoryEmbeddingRetriever(document_store=document_store),
|
||||
)
|
||||
query_pipeline.connect("text_embedder.embedding", "retriever.query_embedding")
|
||||
|
||||
query = "Who lives in Berlin?"
|
||||
|
||||
result = query_pipeline.run({"text_embedder": {"text": query}})
|
||||
|
||||
print(result["retriever"]["documents"][0])
|
||||
|
||||
# Document(id=..., mimetype: 'text/plain',
|
||||
# text: 'My name is Wolfgang and I live in Berlin')
|
||||
```
|
||||
@@ -0,0 +1,110 @@
|
||||
---
|
||||
title: "STACKITDocumentEmbedder"
|
||||
id: stackitdocumentembedder
|
||||
slug: "/stackitdocumentembedder"
|
||||
description: "This component enables document embedding using the STACKIT API."
|
||||
---
|
||||
|
||||
# STACKITDocumentEmbedder
|
||||
|
||||
This component enables document embedding using the STACKIT API.
|
||||
|
||||
<div className="key-value-table">
|
||||
|
||||
| | |
|
||||
| --- | --- |
|
||||
| **Most common position in a pipeline** | Before a [DocumentWriter](../writers/documentwriter.mdx) in an indexing pipeline |
|
||||
| **Mandatory init variables** | `model`: The model used through the STACKIT API |
|
||||
| **Mandatory run variables** | `documents`: A list of documents to be embedded |
|
||||
| **Output variables** | `documents`: A list of documents enriched with embeddings |
|
||||
| **API reference** | [STACKIT](/reference/integrations-stackit) |
|
||||
| **GitHub link** | https://github.com/deepset-ai/haystack-core-integrations/tree/main/integrations/stackit |
|
||||
| **Package name** | `stackit-haystack` |
|
||||
|
||||
</div>
|
||||
|
||||
## Overview
|
||||
|
||||
`STACKITDocumentEmbedder` enables document embedding models served by STACKIT through their API.
|
||||
|
||||
### Parameters
|
||||
|
||||
To use the `STACKITDocumentEmbedder`, ensure you have set a `STACKIT_API_KEY` as an environment variable. Alternatively, provide the API key as an environment variable with a different name or a token by setting `api_key` and using Haystack’s [secret management](../../concepts/secret-management.mdx).
|
||||
|
||||
Set your preferred supported model with the `model` parameter when initializing the component. See the full list of all supported models on the [STACKIT website](https://docs.stackit.cloud/stackit/en/models-licenses-319914532.html).
|
||||
|
||||
Optionally, you can change the default `api_base_url`, which is `"https://api.openai-compat.model-serving.eu01.onstackit.cloud/v1"`.
|
||||
|
||||
You can pass any text generation parameters valid for the STACKIT Chat Completion API directly to this component with the `generation_kwargs` parameter in the init or run methods.
|
||||
|
||||
Then component needs a list of documents as input to operate.
|
||||
|
||||
## Usage
|
||||
|
||||
Install the `stackit-haystack` package to use the `STACKITDocumentEmbedder` and set an environment variable called `STACKIT_API_KEY` to your API key.
|
||||
|
||||
```shell
|
||||
pip install stackit-haystack
|
||||
```
|
||||
|
||||
### On its own
|
||||
|
||||
```python
|
||||
from haystack_integrations.components.embedders.stackit import STACKITDocumentEmbedder
|
||||
|
||||
doc = Document(content="I love pizza!")
|
||||
|
||||
document_embedder = STACKITDocumentEmbedder(model="intfloat/e5-mistral-7b-instruct")
|
||||
|
||||
result = document_embedder.run([doc])
|
||||
print(result["documents"][0].embedding)
|
||||
|
||||
# [0.0215301513671875, 0.01499176025390625, ...]
|
||||
```
|
||||
|
||||
### In a pipeline
|
||||
|
||||
You can also use `STACKITDocumentEmbedder` in your pipeline in a following way.
|
||||
|
||||
```python
|
||||
from haystack import Document
|
||||
from haystack import Pipeline
|
||||
from haystack.document_stores.in_memory import InMemoryDocumentStore
|
||||
from haystack_integrations.components.embedders.stackit import (
|
||||
STACKITTextEmbedder,
|
||||
STACKITDocumentEmbedder,
|
||||
)
|
||||
from haystack.components.retrievers.in_memory import InMemoryEmbeddingRetriever
|
||||
|
||||
document_store = InMemoryDocumentStore()
|
||||
|
||||
documents = [
|
||||
Document(content="My name is Wolfgang and I live in Berlin"),
|
||||
Document(content="I saw a black horse running"),
|
||||
Document(content="Germany has many big cities"),
|
||||
]
|
||||
|
||||
document_embedder = STACKITDocumentEmbedder(model="intfloat/e5-mistral-7b-instruct")
|
||||
documents_with_embeddings = document_embedder.run(documents)["documents"]
|
||||
document_store.write_documents(documents_with_embeddings)
|
||||
|
||||
text_embedder = STACKITTextEmbedder(model="intfloat/e5-mistral-7b-instruct")
|
||||
|
||||
query_pipeline = Pipeline()
|
||||
query_pipeline.add_component("text_embedder", text_embedder)
|
||||
query_pipeline.add_component(
|
||||
"retriever",
|
||||
InMemoryEmbeddingRetriever(document_store=document_store),
|
||||
)
|
||||
query_pipeline.connect("text_embedder.embedding", "retriever.query_embedding")
|
||||
|
||||
query = "Where does Wolfgang live?"
|
||||
|
||||
result = query_pipeline.run({"text_embedder": {"text": query}})
|
||||
|
||||
print(result["retriever"]["documents"][0])
|
||||
|
||||
# Document(id=..., content: 'My name is Wolfgang and I live in Berlin', score: ...)
|
||||
```
|
||||
|
||||
You can find more usage examples in the STACKIT integration [repository](https://github.com/deepset-ai/haystack-core-integrations/tree/main/integrations/stackit/examples) and its [integration page](https://haystack.deepset.ai/integrations/stackit).
|
||||
@@ -0,0 +1,107 @@
|
||||
---
|
||||
title: "STACKITTextEmbedder"
|
||||
id: stackittextembedder
|
||||
slug: "/stackittextembedder"
|
||||
description: "This component enables text embedding using the STACKIT API."
|
||||
---
|
||||
|
||||
# STACKITTextEmbedder
|
||||
|
||||
This component enables text embedding using the STACKIT API.
|
||||
|
||||
<div className="key-value-table">
|
||||
|
||||
| | |
|
||||
| --- | --- |
|
||||
| **Most common position in a pipeline** | Before an embedding [Retriever](../retrievers.mdx) in a query/RAG pipeline |
|
||||
| **Mandatory init variables** | `model`: The model used through the STACKIT API |
|
||||
| **Mandatory run variables** | `text`: A string |
|
||||
| **Output variables** | `embedding`: A list of float numbers |
|
||||
| **API reference** | [STACKIT](/reference/integrations-stackit) |
|
||||
| **GitHub link** | https://github.com/deepset-ai/haystack-core-integrations/tree/main/integrations/stackit |
|
||||
| **Package name** | `stackit-haystack` |
|
||||
|
||||
</div>
|
||||
|
||||
## Overview
|
||||
|
||||
`STACKITTextEmbedder` enables text embedding models served by STACKIT through their API.
|
||||
|
||||
### Parameters
|
||||
|
||||
To use the `STACKITTextEmbedder`, ensure you have set a `STACKIT_API_KEY` as an environment variable. Alternatively, provide the API key as an environment variable with a different name or a token by setting `api_key` and using Haystack’s [secret management](../../concepts/secret-management.mdx).
|
||||
|
||||
Set your preferred supported model with the `model` parameter when initializing the component. See the full list of all supported models on the [STACKIT website](https://docs.stackit.cloud/stackit/en/models-licenses-319914532.html).
|
||||
|
||||
Optionally, you can change the default `api_base_url`, which is `"https://api.openai-compat.model-serving.eu01.onstackit.cloud/v1"`.
|
||||
|
||||
You can pass any text generation parameters valid for the STACKIT Chat Completion API directly to this component with the `generation_kwargs` parameter in the init or run methods.
|
||||
|
||||
The component needs a text input to operate.
|
||||
|
||||
## Usage
|
||||
|
||||
Install the `stackit-haystack` package to use the `STACKITTextEmbedder` and set an environment variable called `STACKIT_API_KEY` to your API key.
|
||||
|
||||
```shell
|
||||
pip install stackit-haystack
|
||||
```
|
||||
|
||||
### On its own
|
||||
|
||||
```python
|
||||
from haystack_integrations.components.embedders.stackit import STACKITTextEmbedder
|
||||
|
||||
text_embedder = STACKITTextEmbedder(model="intfloat/e5-mistral-7b-instruct")
|
||||
|
||||
print(text_embedder.run("I love pizza!"))
|
||||
|
||||
# {'embedding': [0.0215301513671875, 0.01499176025390625, ...]}
|
||||
```
|
||||
|
||||
### In a pipeline
|
||||
|
||||
You can also use `STACKITTextEmbedder` in your pipeline.
|
||||
|
||||
```python
|
||||
from haystack import Document
|
||||
from haystack import Pipeline
|
||||
from haystack.document_stores.in_memory import InMemoryDocumentStore
|
||||
from haystack_integrations.components.embedders.stackit import (
|
||||
STACKITTextEmbedder,
|
||||
STACKITDocumentEmbedder,
|
||||
)
|
||||
from haystack.components.retrievers.in_memory import InMemoryEmbeddingRetriever
|
||||
|
||||
document_store = InMemoryDocumentStore()
|
||||
|
||||
documents = [
|
||||
Document(content="My name is Wolfgang and I live in Berlin"),
|
||||
Document(content="I saw a black horse running"),
|
||||
Document(content="Germany has many big cities"),
|
||||
]
|
||||
|
||||
document_embedder = STACKITDocumentEmbedder(model="intfloat/e5-mistral-7b-instruct")
|
||||
documents_with_embeddings = document_embedder.run(documents)["documents"]
|
||||
document_store.write_documents(documents_with_embeddings)
|
||||
|
||||
text_embedder = STACKITTextEmbedder(model="intfloat/e5-mistral-7b-instruct")
|
||||
|
||||
query_pipeline = Pipeline()
|
||||
query_pipeline.add_component("text_embedder", text_embedder)
|
||||
query_pipeline.add_component(
|
||||
"retriever",
|
||||
InMemoryEmbeddingRetriever(document_store=document_store),
|
||||
)
|
||||
query_pipeline.connect("text_embedder.embedding", "retriever.query_embedding")
|
||||
|
||||
query = "Where does Wolfgang live?"
|
||||
|
||||
result = query_pipeline.run({"text_embedder": {"text": query}})
|
||||
|
||||
print(result["retriever"]["documents"][0])
|
||||
|
||||
# Document(id=..., content: 'My name is Wolfgang and I live in Berlin', score: ...)
|
||||
```
|
||||
|
||||
You can find more usage examples in the STACKIT integration [repository](https://github.com/deepset-ai/haystack-core-integrations/tree/main/integrations/stackit/examples) and its [integration page](https://haystack.deepset.ai/integrations/stackit).
|
||||
@@ -0,0 +1,134 @@
|
||||
---
|
||||
title: "TwelveLabsDocumentEmbedder"
|
||||
id: twelvelabsdocumentembedder
|
||||
slug: "/twelvelabsdocumentembedder"
|
||||
description: "This component computes the embeddings of a list of documents using the TwelveLabs Marengo multimodal embedding model and stores the obtained vectors in the embedding field of each document. The vectors are necessary to perform embedding retrieval on a collection of documents."
|
||||
---
|
||||
|
||||
# TwelveLabsDocumentEmbedder
|
||||
|
||||
This component computes the embeddings of a list of documents using the TwelveLabs Marengo multimodal embedding model and stores the obtained vectors in the embedding field of each document. The vectors computed by this component are necessary to perform embedding retrieval on a collection of documents. At retrieval time, the vector representing the query is compared with those of the documents to find the most similar or relevant documents.
|
||||
|
||||
<div className="key-value-table">
|
||||
|
||||
| | |
|
||||
| --- | --- |
|
||||
| **Most common position in a pipeline** | Before a [`DocumentWriter`](../writers/documentwriter.mdx) in an indexing pipeline |
|
||||
| **Mandatory init variables** | `api_key`: The TwelveLabs API key. Can be set with `TWELVELABS_API_KEY` env var. |
|
||||
| **Mandatory run variables** | `documents`: A list of documents |
|
||||
| **Output variables** | `documents`: A list of documents (enriched with embeddings) <br /> <br />`meta`: A dictionary of metadata |
|
||||
| **API reference** | [TwelveLabs](/reference/integrations-twelvelabs) |
|
||||
| **GitHub link** | https://github.com/deepset-ai/haystack-core-integrations/tree/main/integrations/twelvelabs |
|
||||
| **Package name** | `twelvelabs-haystack` |
|
||||
|
||||
</div>
|
||||
|
||||
## Overview
|
||||
|
||||
`TwelveLabsDocumentEmbedder` enriches each document with an embedding of its content. To embed a string, use the [`TwelveLabsTextEmbedder`](twelvelabstextembedder.mdx). The default model is `marengo3.0`.
|
||||
|
||||
Because Marengo embeds text, images, audio, and video into a single shared space, these embeddings support cross-modal retrieval.
|
||||
|
||||
To start using this integration with Haystack, install the package with:
|
||||
|
||||
```shell
|
||||
pip install twelvelabs-haystack
|
||||
```
|
||||
|
||||
The component uses a `TWELVELABS_API_KEY` environment variable by default. Otherwise, you can pass an API key at initialization with `api_key`:
|
||||
|
||||
```python
|
||||
from haystack.utils import Secret
|
||||
from haystack_integrations.components.embedders.twelvelabs import (
|
||||
TwelveLabsDocumentEmbedder,
|
||||
)
|
||||
|
||||
embedder = TwelveLabsDocumentEmbedder(api_key=Secret.from_token("<your-api-key>"))
|
||||
```
|
||||
|
||||
To get an API key, head to [playground.twelvelabs.io](https://playground.twelvelabs.io).
|
||||
|
||||
### Embedding Metadata
|
||||
|
||||
Text documents often come with a set of metadata. If they are distinctive and semantically meaningful, you can embed them along with the text of the document to improve retrieval.
|
||||
|
||||
You can do this by passing the relevant meta field names with `meta_fields_to_embed`:
|
||||
|
||||
```python
|
||||
from haystack import Document
|
||||
from haystack_integrations.components.embedders.twelvelabs import (
|
||||
TwelveLabsDocumentEmbedder,
|
||||
)
|
||||
|
||||
doc = Document(content="some text", meta={"title": "relevant title", "page number": 18})
|
||||
|
||||
embedder = TwelveLabsDocumentEmbedder(meta_fields_to_embed=["title"])
|
||||
|
||||
docs_w_embeddings = embedder.run(documents=[doc])["documents"]
|
||||
```
|
||||
|
||||
## Usage
|
||||
|
||||
### On its own
|
||||
|
||||
Here is how you can use the component on its own:
|
||||
|
||||
```python
|
||||
from haystack import Document
|
||||
from haystack_integrations.components.embedders.twelvelabs import (
|
||||
TwelveLabsDocumentEmbedder,
|
||||
)
|
||||
|
||||
doc = Document(content="a cat playing piano")
|
||||
|
||||
document_embedder = TwelveLabsDocumentEmbedder()
|
||||
|
||||
result = document_embedder.run(documents=[doc])
|
||||
print(result["documents"][0].embedding)
|
||||
|
||||
# [-0.043398008, -0.025287028, -0.0061081843, ...]
|
||||
```
|
||||
|
||||
:::info
|
||||
We recommend setting `TWELVELABS_API_KEY` as an environment variable instead of setting it as a parameter.
|
||||
:::
|
||||
|
||||
### In a pipeline
|
||||
|
||||
```python
|
||||
from haystack import Document, Pipeline
|
||||
from haystack.document_stores.in_memory import InMemoryDocumentStore
|
||||
from haystack.components.writers import DocumentWriter
|
||||
from haystack.components.retrievers.in_memory import InMemoryEmbeddingRetriever
|
||||
from haystack_integrations.components.embedders.twelvelabs import (
|
||||
TwelveLabsDocumentEmbedder,
|
||||
TwelveLabsTextEmbedder,
|
||||
)
|
||||
|
||||
document_store = InMemoryDocumentStore(embedding_similarity_function="cosine")
|
||||
|
||||
documents = [
|
||||
Document(content="a cat playing piano"),
|
||||
Document(content="a dog catching a frisbee at the beach"),
|
||||
Document(content="a timelapse of a city skyline at night"),
|
||||
]
|
||||
|
||||
indexing_pipeline = Pipeline()
|
||||
indexing_pipeline.add_component("embedder", TwelveLabsDocumentEmbedder())
|
||||
indexing_pipeline.add_component("writer", DocumentWriter(document_store=document_store))
|
||||
indexing_pipeline.connect("embedder", "writer")
|
||||
indexing_pipeline.run({"embedder": {"documents": documents}})
|
||||
|
||||
query_pipeline = Pipeline()
|
||||
query_pipeline.add_component("text_embedder", TwelveLabsTextEmbedder())
|
||||
query_pipeline.add_component(
|
||||
"retriever",
|
||||
InMemoryEmbeddingRetriever(document_store=document_store),
|
||||
)
|
||||
query_pipeline.connect("text_embedder.embedding", "retriever.query_embedding")
|
||||
|
||||
result = query_pipeline.run({"text_embedder": {"text": "feline making music"}})
|
||||
print(result["retriever"]["documents"][0].content)
|
||||
|
||||
# a cat playing piano
|
||||
```
|
||||
@@ -0,0 +1,111 @@
|
||||
---
|
||||
title: "TwelveLabsTextEmbedder"
|
||||
id: twelvelabstextembedder
|
||||
slug: "/twelvelabstextembedder"
|
||||
description: "This component transforms a string into a vector using the TwelveLabs Marengo multimodal embedding model. Because Marengo embeds text, images, audio, and video into one shared vector space, the resulting embeddings support cross-modal retrieval. Use this component to embed a query before searching with an embedding Retriever."
|
||||
---
|
||||
|
||||
# TwelveLabsTextEmbedder
|
||||
|
||||
This component transforms a string into a vector using the TwelveLabs Marengo multimodal embedding model. Because Marengo embeds text, images, audio, and video into one shared vector space, the resulting embeddings support cross-modal retrieval (for example, searching a video collection with a text query). Use this component to embed a query before searching with an embedding Retriever.
|
||||
|
||||
<div className="key-value-table">
|
||||
|
||||
| | |
|
||||
| --- | --- |
|
||||
| **Most common position in a pipeline** | Before an embedding [Retriever](../retrievers.mdx) in a query/RAG pipeline |
|
||||
| **Mandatory init variables** | `api_key`: The TwelveLabs API key. Can be set with `TWELVELABS_API_KEY` env var. |
|
||||
| **Mandatory run variables** | `text`: A string |
|
||||
| **Output variables** | `embedding`: A list of float numbers <br /> <br />`meta`: A dictionary of metadata |
|
||||
| **API reference** | [TwelveLabs](/reference/integrations-twelvelabs) |
|
||||
| **GitHub link** | https://github.com/deepset-ai/haystack-core-integrations/tree/main/integrations/twelvelabs |
|
||||
| **Package name** | `twelvelabs-haystack` |
|
||||
|
||||
</div>
|
||||
|
||||
## Overview
|
||||
|
||||
`TwelveLabsTextEmbedder` embeds a simple string (such as a query) into a vector. For embedding lists of documents, use the [`TwelveLabsDocumentEmbedder`](twelvelabsdocumentembedder.mdx), which enriches each document with the computed embedding. The default model is `marengo3.0`.
|
||||
|
||||
Because Marengo embeds into a single shared space, embeddings produced from text are directly comparable (cosine similarity) with embeddings of images, audio, and video from the same model.
|
||||
|
||||
To start using this integration with Haystack, install the package with:
|
||||
|
||||
```shell
|
||||
pip install twelvelabs-haystack
|
||||
```
|
||||
|
||||
The component uses a `TWELVELABS_API_KEY` environment variable by default. Otherwise, you can pass an API key at initialization with `api_key`:
|
||||
|
||||
```python
|
||||
from haystack.utils import Secret
|
||||
from haystack_integrations.components.embedders.twelvelabs import TwelveLabsTextEmbedder
|
||||
|
||||
embedder = TwelveLabsTextEmbedder(api_key=Secret.from_token("<your-api-key>"))
|
||||
```
|
||||
|
||||
To get an API key, head to [playground.twelvelabs.io](https://playground.twelvelabs.io).
|
||||
|
||||
## Usage
|
||||
|
||||
### On its own
|
||||
|
||||
Here is how you can use the component on its own:
|
||||
|
||||
```python
|
||||
from haystack_integrations.components.embedders.twelvelabs import TwelveLabsTextEmbedder
|
||||
|
||||
text_embedder = TwelveLabsTextEmbedder()
|
||||
|
||||
result = text_embedder.run(text="a cat playing piano")
|
||||
print(result["embedding"])
|
||||
|
||||
# [-0.043398008, -0.025287028, -0.0061081843, ...]
|
||||
print(result["meta"])
|
||||
|
||||
# {'model': 'marengo3.0'}
|
||||
```
|
||||
|
||||
:::info
|
||||
We recommend setting `TWELVELABS_API_KEY` as an environment variable instead of setting it as a parameter.
|
||||
:::
|
||||
|
||||
### In a pipeline
|
||||
|
||||
```python
|
||||
from haystack import Document, Pipeline
|
||||
from haystack.document_stores.in_memory import InMemoryDocumentStore
|
||||
from haystack.components.writers import DocumentWriter
|
||||
from haystack.components.retrievers.in_memory import InMemoryEmbeddingRetriever
|
||||
from haystack_integrations.components.embedders.twelvelabs import (
|
||||
TwelveLabsDocumentEmbedder,
|
||||
TwelveLabsTextEmbedder,
|
||||
)
|
||||
|
||||
document_store = InMemoryDocumentStore(embedding_similarity_function="cosine")
|
||||
|
||||
documents = [
|
||||
Document(content="a cat playing piano"),
|
||||
Document(content="a dog catching a frisbee at the beach"),
|
||||
Document(content="a timelapse of a city skyline at night"),
|
||||
]
|
||||
|
||||
indexing_pipeline = Pipeline()
|
||||
indexing_pipeline.add_component("embedder", TwelveLabsDocumentEmbedder())
|
||||
indexing_pipeline.add_component("writer", DocumentWriter(document_store=document_store))
|
||||
indexing_pipeline.connect("embedder", "writer")
|
||||
indexing_pipeline.run({"embedder": {"documents": documents}})
|
||||
|
||||
query_pipeline = Pipeline()
|
||||
query_pipeline.add_component("text_embedder", TwelveLabsTextEmbedder())
|
||||
query_pipeline.add_component(
|
||||
"retriever",
|
||||
InMemoryEmbeddingRetriever(document_store=document_store),
|
||||
)
|
||||
query_pipeline.connect("text_embedder.embedding", "retriever.query_embedding")
|
||||
|
||||
result = query_pipeline.run({"text_embedder": {"text": "feline making music"}})
|
||||
print(result["retriever"]["documents"][0].content)
|
||||
|
||||
# a cat playing piano
|
||||
```
|
||||
@@ -0,0 +1,122 @@
|
||||
---
|
||||
title: "VertexAIDocumentEmbedder"
|
||||
id: vertexaidocumentembedder
|
||||
slug: "/vertexaidocumentembedder"
|
||||
description: "This component computes embeddings for documents using models through VertexAI Embeddings API."
|
||||
---
|
||||
|
||||
# VertexAIDocumentEmbedder
|
||||
|
||||
This component computes embeddings for documents using models through VertexAI Embeddings API.
|
||||
|
||||
:::warning[Deprecation Notice]
|
||||
|
||||
This integration uses the deprecated google-generativeai SDK, which will lose support after August 2025.
|
||||
|
||||
We recommend switching to the new [GoogleGenAIDocumentEmbedder](googlegenaidocumentembedder.mdx) integration instead.
|
||||
:::
|
||||
|
||||
<div className="key-value-table">
|
||||
|
||||
| | |
|
||||
| --- | --- |
|
||||
| **Most common position in a pipeline** | Before a [DocumentWriter](../writers/documentwriter.mdx) in an indexing pipeline |
|
||||
| **Mandatory init variables** | `model`: The model used through the VertexAI Embeddings API |
|
||||
| **Mandatory run variables** | `documents`: A list of documents to be embedded |
|
||||
| **Output variables** | `documents`: A list of documents enriched with embeddings |
|
||||
| **API reference** | [Google Vertex](/reference/integrations-google-vertex) |
|
||||
| **GitHub link** | https://github.com/deepset-ai/haystack-core-integrations/tree/main/integrations/google_vertex |
|
||||
| **Package name** | `google-vertex-haystack` |
|
||||
|
||||
</div>
|
||||
|
||||
`VertexAIDocumentEmbedder` enriches the metadata of documents with an embedding of their content. To embed a string, use the [`VertexAITextEmbedder`](vertexaitextembedder.mdx).
|
||||
|
||||
To use the `VertexAIDocumentEmbedder`, initialize it with:
|
||||
|
||||
- `model`: The supported models are:
|
||||
- "text-embedding-004"
|
||||
- "text-embedding-005"
|
||||
- "textembedding-gecko-multilingual@001"
|
||||
- "text-multilingual-embedding-002"
|
||||
- "text-embedding-large-exp-03-07"
|
||||
- `task_type`: "RETRIEVAL_DOCUMENT” is the default. You can find all task types in the official [Google documentation](https://cloud.google.com/vertex-ai/generative-ai/docs/model-reference/text-embeddings-api#tasktype).
|
||||
|
||||
### Authentication
|
||||
|
||||
`VertexAIDocumentEmbedder` uses Google Cloud Application Default Credentials (ADCs) for authentication. For more information on how to set up ADCs, see the [official documentation](https://cloud.google.com/docs/authentication/provide-credentials-adc).
|
||||
|
||||
Keep in mind that it’s essential to use an account that has access to a project authorized to use Google Vertex AI endpoints.
|
||||
|
||||
You can find your project ID in the [GCP resource manager](https://console.cloud.google.com/cloud-resource-manager) or locally by running `gcloud projects list` in your terminal. For more info on the gcloud CLI, see its [official documentation](https://cloud.google.com/cli).
|
||||
|
||||
## Usage
|
||||
|
||||
Install the `google-vertex-haystack` package to use this Embedder:
|
||||
|
||||
```shell
|
||||
pip install google-vertex-haystack
|
||||
```
|
||||
|
||||
### On its own
|
||||
|
||||
```python
|
||||
from haystack import Document
|
||||
from haystack_integrations.components.embedders.google_vertex import (
|
||||
VertexAIDocumentEmbedder,
|
||||
)
|
||||
|
||||
doc = Document(content="I love pizza!")
|
||||
|
||||
document_embedder = VertexAIDocumentEmbedder(model="text-embedding-005")
|
||||
|
||||
result = document_embedder.run([doc])
|
||||
print(result["documents"][0].embedding)
|
||||
# [-0.044606007635593414, 0.02857724390923977, -0.03549133986234665,
|
||||
```
|
||||
|
||||
### In a pipeline
|
||||
|
||||
```python
|
||||
from haystack import Document
|
||||
from haystack import Pipeline
|
||||
from haystack.document_stores.in_memory import InMemoryDocumentStore
|
||||
from haystack_integrations.components.embedders.google_vertex import (
|
||||
VertexAITextEmbedder,
|
||||
)
|
||||
from haystack_integrations.components.embedders.google_vertex import (
|
||||
VertexAIDocumentEmbedder,
|
||||
)
|
||||
from haystack.components.retrievers.in_memory import InMemoryEmbeddingRetriever
|
||||
|
||||
document_store = InMemoryDocumentStore(embedding_similarity_function="cosine")
|
||||
|
||||
documents = [
|
||||
Document(content="My name is Wolfgang and I live in Berlin"),
|
||||
Document(content="I saw a black horse running"),
|
||||
Document(content="Germany has many big cities"),
|
||||
]
|
||||
|
||||
document_embedder = VertexAIDocumentEmbedder(model="text-embedding-005")
|
||||
documents_with_embeddings = document_embedder.run(documents)["documents"]
|
||||
document_store.write_documents(documents_with_embeddings)
|
||||
|
||||
query_pipeline = Pipeline()
|
||||
query_pipeline.add_component(
|
||||
"text_embedder",
|
||||
VertexAITextEmbedder(model="text-embedding-005"),
|
||||
)
|
||||
query_pipeline.add_component(
|
||||
"retriever",
|
||||
InMemoryEmbeddingRetriever(document_store=document_store),
|
||||
)
|
||||
query_pipeline.connect("text_embedder.embedding", "retriever.query_embedding")
|
||||
|
||||
query = "Who lives in Berlin?"
|
||||
|
||||
result = query_pipeline.run({"text_embedder": {"text": query}})
|
||||
|
||||
print(result["retriever"]["documents"][0])
|
||||
|
||||
# Document(id=..., content: 'My name is Wolfgang and I live in Berlin')
|
||||
```
|
||||
@@ -0,0 +1,122 @@
|
||||
---
|
||||
title: "VertexAITextEmbedder"
|
||||
id: vertexaitextembedder
|
||||
slug: "/vertexaitextembedder"
|
||||
description: "This component computes embeddings for text (such as a query) using models through VertexAI Embeddings API."
|
||||
---
|
||||
|
||||
# VertexAITextEmbedder
|
||||
|
||||
This component computes embeddings for text (such as a query) using models through VertexAI Embeddings API.
|
||||
|
||||
:::warning[Deprecation Notice]
|
||||
|
||||
This integration uses the deprecated google-generativeai SDK, which will lose support after August 2025.
|
||||
|
||||
We recommend switching to the new [GoogleGenAITextEmbedder](googlegenaitextembedder.mdx) integration instead.
|
||||
:::
|
||||
|
||||
<div className="key-value-table">
|
||||
|
||||
| | |
|
||||
| --- | --- |
|
||||
| **Most common position in a pipeline** | Before an embedding [Retriever](../retrievers.mdx) in a query/RAG pipeline |
|
||||
| **Mandatory init variables** | `model`: The model used through the VertexAI Embeddings API |
|
||||
| **Mandatory run variables** | `text`: A string |
|
||||
| **Output variables** | `embedding`: A list of float numbers |
|
||||
| **API reference** | [Google Vertex](/reference/integrations-google-vertex) |
|
||||
| **GitHub link** | https://github.com/deepset-ai/haystack-core-integrations/tree/main/integrations/google_vertex |
|
||||
| **Package name** | `google-vertex-haystack` |
|
||||
|
||||
</div>
|
||||
|
||||
## Overview
|
||||
|
||||
`VertexAITextEmbedder` embeds a simple string (such as a query) into a vector. For embedding lists of documents, use the [`VertexAIDocumentEmbedder`](vertexaidocumentembedder.mdx) which enriches the document with the computed embedding, also known as vector.
|
||||
|
||||
To start using the `VertexAITextEmbedder`, initialize it with:
|
||||
|
||||
- `model`: The supported models are:
|
||||
- "text-embedding-004"
|
||||
- "text-embedding-005"
|
||||
- "textembedding-gecko-multilingual@001"
|
||||
- "text-multilingual-embedding-002"
|
||||
- "text-embedding-large-exp-03-07"
|
||||
- `task_type`: "RETRIEVAL_QUERY” is the default. You can find all task types in the official [Google documentation](https://cloud.google.com/vertex-ai/generative-ai/docs/model-reference/text-embeddings-api#tasktype).
|
||||
|
||||
### Authentication
|
||||
|
||||
`VertexAITextEmbedder` uses Google Cloud Application Default Credentials (ADCs) for authentication. For more information on how to set up ADCs, see the [official documentation](https://cloud.google.com/docs/authentication/provide-credentials-adc).
|
||||
|
||||
Keep in mind that it’s essential to use an account that has access to a project authorized to use Google Vertex AI endpoints.
|
||||
|
||||
You can find your project ID in the [GCP resource manager](https://console.cloud.google.com/cloud-resource-manager) or locally by running `gcloud projects list` in your terminal. For more info on the gcloud CLI, see its [official documentation](https://cloud.google.com/cli).
|
||||
|
||||
## Usage
|
||||
|
||||
Install the `google-vertex-haystack` package to use this Embedder:
|
||||
|
||||
```shell
|
||||
pip install google-vertex-haystack
|
||||
```
|
||||
|
||||
### On its own
|
||||
|
||||
```python
|
||||
from haystack_integrations.components.embedders.google_vertex import (
|
||||
VertexAITextEmbedder,
|
||||
)
|
||||
|
||||
text_to_embed = "I love pizza!"
|
||||
|
||||
text_embedder = VertexAITextEmbedder(model="text-embedding-005")
|
||||
|
||||
print(text_embedder.run(text_to_embed))
|
||||
# {'embedding': [-0.08127457648515701, 0.03399784862995148, -0.05116401985287666, ...]
|
||||
```
|
||||
|
||||
### In a pipeline
|
||||
|
||||
```python
|
||||
from haystack import Document
|
||||
from haystack import Pipeline
|
||||
from haystack.document_stores.in_memory import InMemoryDocumentStore
|
||||
from haystack_integrations.components.embedders.google_vertex import (
|
||||
VertexAITextEmbedder,
|
||||
)
|
||||
from haystack_integrations.components.embedders.google_vertex import (
|
||||
VertexAIDocumentEmbedder,
|
||||
)
|
||||
from haystack.components.retrievers.in_memory import InMemoryEmbeddingRetriever
|
||||
|
||||
document_store = InMemoryDocumentStore(embedding_similarity_function="cosine")
|
||||
|
||||
documents = [
|
||||
Document(content="My name is Wolfgang and I live in Berlin"),
|
||||
Document(content="I saw a black horse running"),
|
||||
Document(content="Germany has many big cities"),
|
||||
]
|
||||
|
||||
document_embedder = VertexAIDocumentEmbedder(model="text-embedding-005")
|
||||
documents_with_embeddings = document_embedder.run(documents)["documents"]
|
||||
document_store.write_documents(documents_with_embeddings)
|
||||
|
||||
query_pipeline = Pipeline()
|
||||
query_pipeline.add_component(
|
||||
"text_embedder",
|
||||
VertexAITextEmbedder(model="text-embedding-005"),
|
||||
)
|
||||
query_pipeline.add_component(
|
||||
"retriever",
|
||||
InMemoryEmbeddingRetriever(document_store=document_store),
|
||||
)
|
||||
query_pipeline.connect("text_embedder.embedding", "retriever.query_embedding")
|
||||
|
||||
query = "Who lives in Berlin?"
|
||||
|
||||
result = query_pipeline.run({"text_embedder": {"text": query}})
|
||||
|
||||
print(result["retriever"]["documents"][0])
|
||||
|
||||
# Document(id=..., content: 'My name is Wolfgang and I live in Berlin')
|
||||
```
|
||||
@@ -0,0 +1,176 @@
|
||||
---
|
||||
title: "VLLMDocumentEmbedder"
|
||||
id: vllmdocumentembedder
|
||||
slug: "/vllmdocumentembedder"
|
||||
description: "This component computes the embeddings of a list of documents using models served with vLLM."
|
||||
---
|
||||
|
||||
# VLLMDocumentEmbedder
|
||||
|
||||
This component computes the embeddings of a list of documents using models served with [vLLM](https://docs.vllm.ai/).
|
||||
|
||||
<div className="key-value-table">
|
||||
|
||||
| | |
|
||||
| --- | --- |
|
||||
| **Most common position in a pipeline** | Before a [`DocumentWriter`](../writers/documentwriter.mdx) in an indexing pipeline |
|
||||
| **Mandatory init variables** | `model`: The name of the model served by vLLM |
|
||||
| **Mandatory run variables** | `documents`: A list of documents |
|
||||
| **Output variables** | `documents`: A list of documents (enriched with embeddings) |
|
||||
| **API reference** | [vLLM](/reference/integrations-vllm) |
|
||||
| **GitHub link** | https://github.com/deepset-ai/haystack-core-integrations/tree/main/integrations/vllm |
|
||||
| **Package name** | `vllm-haystack` |
|
||||
|
||||
</div>
|
||||
|
||||
## Overview
|
||||
|
||||
[vLLM](https://docs.vllm.ai/) is a high-throughput and memory-efficient inference and serving engine for LLMs. It exposes an OpenAI-compatible HTTP server, which `VLLMDocumentEmbedder` uses to compute embeddings through the Embeddings API.
|
||||
|
||||
`VLLMDocumentEmbedder` computes the embeddings of a list of documents and stores the obtained vectors in the `embedding` field of each document. It expects a vLLM server to be running and accessible at the `api_base_url` parameter (by default, `http://localhost:8000/v1`). To embed a string (such as a query), use the [`VLLMTextEmbedder`](vllmtextembedder.mdx).
|
||||
|
||||
The vectors computed by this component are necessary to perform embedding retrieval on a collection of documents. At retrieval time, the vector that represents the query is compared with those of the documents to find the most similar or relevant ones.
|
||||
|
||||
If the vLLM server was started with `--api-key`, provide the API key through the `VLLM_API_KEY` environment variable or the `api_key` init parameter using Haystack's [Secret](../../concepts/secret-management.mdx) API.
|
||||
|
||||
### Compatible models
|
||||
|
||||
vLLM supports a range of embedding models. Check the [vLLM pooling models docs](https://docs.vllm.ai/en/stable/models/pooling_models) for the list of supported architectures and models.
|
||||
|
||||
### vLLM-specific parameters
|
||||
|
||||
You can pass vLLM-specific parameters through the `extra_parameters` dictionary. These are forwarded as `extra_body` to the OpenAI-compatible embeddings endpoint. Use this to pass parameters that are not part of the standard OpenAI Embeddings API, such as `truncate_prompt_tokens` or `truncation_side`. See the [vLLM Embeddings API docs](https://docs.vllm.ai/en/stable/models/pooling_models/embed/#openai-compatible-embeddings-api) for details.
|
||||
|
||||
```python
|
||||
embedder = VLLMDocumentEmbedder(
|
||||
model="google/embeddinggemma-300m",
|
||||
extra_parameters={"truncate_prompt_tokens": 256, "truncation_side": "right"},
|
||||
)
|
||||
```
|
||||
|
||||
### Matryoshka embeddings
|
||||
|
||||
If the model was trained with Matryoshka Representation Learning, you can reduce the dimensionality of the output vector through the `dimensions` parameter. See the [vLLM Matryoshka docs](https://docs.vllm.ai/en/stable/models/pooling_models/embed/#matryoshka-embeddings) for details.
|
||||
|
||||
### Batching and failure handling
|
||||
|
||||
`VLLMDocumentEmbedder` encodes documents in batches. Use `batch_size` (default `32`) to control how many documents are sent in a single request to the vLLM server, and `progress_bar` to toggle the progress indicator.
|
||||
|
||||
By default (`raise_on_failure=False`), failed embedding requests are logged and processing continues with the remaining documents. Set `raise_on_failure=True` to raise an exception instead.
|
||||
|
||||
### Instructions
|
||||
|
||||
Some embedding models require prepending the document text with an instruction to work better for retrieval. For example, if you use [intfloat/e5-large-v2](https://huggingface.co/intfloat/e5-large-v2), you should prefix your document with the following instruction: "passage:".
|
||||
|
||||
This is how it works with `VLLMDocumentEmbedder`:
|
||||
|
||||
```python
|
||||
instruction = "passage:"
|
||||
embedder = VLLMDocumentEmbedder(
|
||||
model="intfloat/e5-large-v2",
|
||||
prefix=instruction,
|
||||
)
|
||||
```
|
||||
|
||||
### Embedding metadata
|
||||
|
||||
Documents often come with a set of metadata. If they are distinctive and semantically meaningful, you can embed them along with the text of the document to improve retrieval. Pass the relevant fields through `meta_fields_to_embed`; they are concatenated to the document text using `embedding_separator` (a newline by default):
|
||||
|
||||
```python
|
||||
from haystack import Document
|
||||
from haystack_integrations.components.embedders.vllm import VLLMDocumentEmbedder
|
||||
|
||||
doc = Document(content="some text", meta={"title": "relevant title", "page_number": 18})
|
||||
|
||||
embedder = VLLMDocumentEmbedder(
|
||||
model="google/embeddinggemma-300m",
|
||||
meta_fields_to_embed=["title"],
|
||||
)
|
||||
|
||||
docs_with_embeddings = embedder.run(documents=[doc])["documents"]
|
||||
```
|
||||
|
||||
## Usage
|
||||
|
||||
Install the `vllm-haystack` package to use the `VLLMDocumentEmbedder`:
|
||||
|
||||
```shell
|
||||
pip install vllm-haystack
|
||||
```
|
||||
|
||||
### Starting the vLLM server
|
||||
|
||||
Before using this component, start a vLLM server with an embedding model:
|
||||
|
||||
```bash
|
||||
vllm serve google/embeddinggemma-300m
|
||||
```
|
||||
|
||||
For details on server options, see the [vLLM CLI docs](https://docs.vllm.ai/en/stable/cli/serve/).
|
||||
|
||||
### On its own
|
||||
|
||||
```python
|
||||
from haystack import Document
|
||||
from haystack_integrations.components.embedders.vllm import VLLMDocumentEmbedder
|
||||
|
||||
doc = Document(content="I love pizza!")
|
||||
|
||||
document_embedder = VLLMDocumentEmbedder(model="google/embeddinggemma-300m")
|
||||
|
||||
result = document_embedder.run([doc])
|
||||
print(result["documents"][0].embedding)
|
||||
|
||||
# [-0.0215301513671875, 0.01499176025390625, ...]
|
||||
```
|
||||
|
||||
### In a pipeline
|
||||
|
||||
```python
|
||||
from haystack import Document, Pipeline
|
||||
from haystack.components.retrievers.in_memory import InMemoryEmbeddingRetriever
|
||||
from haystack.components.writers import DocumentWriter
|
||||
from haystack.document_stores.in_memory import InMemoryDocumentStore
|
||||
from haystack.document_stores.types import DuplicatePolicy
|
||||
from haystack_integrations.components.embedders.vllm import (
|
||||
VLLMDocumentEmbedder,
|
||||
VLLMTextEmbedder,
|
||||
)
|
||||
|
||||
document_store = InMemoryDocumentStore(embedding_similarity_function="cosine")
|
||||
|
||||
documents = [
|
||||
Document(content="My name is Wolfgang and I live in Berlin"),
|
||||
Document(content="I saw a black horse running"),
|
||||
Document(content="Germany has many big cities"),
|
||||
]
|
||||
|
||||
document_embedder = VLLMDocumentEmbedder(model="google/embeddinggemma-300m")
|
||||
writer = DocumentWriter(document_store=document_store, policy=DuplicatePolicy.OVERWRITE)
|
||||
|
||||
indexing_pipeline = Pipeline()
|
||||
indexing_pipeline.add_component("document_embedder", document_embedder)
|
||||
indexing_pipeline.add_component("writer", writer)
|
||||
indexing_pipeline.connect("document_embedder", "writer")
|
||||
|
||||
indexing_pipeline.run({"document_embedder": {"documents": documents}})
|
||||
|
||||
query_pipeline = Pipeline()
|
||||
query_pipeline.add_component(
|
||||
"text_embedder",
|
||||
VLLMTextEmbedder(model="google/embeddinggemma-300m"),
|
||||
)
|
||||
query_pipeline.add_component(
|
||||
"retriever",
|
||||
InMemoryEmbeddingRetriever(document_store=document_store),
|
||||
)
|
||||
query_pipeline.connect("text_embedder.embedding", "retriever.query_embedding")
|
||||
|
||||
query = "Who lives in Berlin?"
|
||||
|
||||
result = query_pipeline.run({"text_embedder": {"text": query}})
|
||||
|
||||
print(result["retriever"]["documents"][0])
|
||||
|
||||
# Document(id=..., content: 'My name is Wolfgang and I live in Berlin', score: ...)
|
||||
```
|
||||
@@ -0,0 +1,139 @@
|
||||
---
|
||||
title: "VLLMTextEmbedder"
|
||||
id: vllmtextembedder
|
||||
slug: "/vllmtextembedder"
|
||||
description: "This component computes the embeddings of a string using models served with vLLM."
|
||||
---
|
||||
|
||||
# VLLMTextEmbedder
|
||||
|
||||
This component computes the embeddings of a string using models served with [vLLM](https://docs.vllm.ai/).
|
||||
|
||||
<div className="key-value-table">
|
||||
|
||||
| | |
|
||||
| --- | --- |
|
||||
| **Most common position in a pipeline** | Before an embedding [Retriever](../retrievers.mdx) in a query/RAG pipeline |
|
||||
| **Mandatory init variables** | `model`: The name of the model served by vLLM |
|
||||
| **Mandatory run variables** | `text`: A string |
|
||||
| **Output variables** | `embedding`: A vector (list of float numbers) |
|
||||
| **API reference** | [vLLM](/reference/integrations-vllm) |
|
||||
| **GitHub link** | https://github.com/deepset-ai/haystack-core-integrations/tree/main/integrations/vllm |
|
||||
| **Package name** | `vllm-haystack` |
|
||||
|
||||
</div>
|
||||
|
||||
## Overview
|
||||
|
||||
[vLLM](https://docs.vllm.ai/) is a high-throughput and memory-efficient inference and serving engine for LLMs. It exposes an OpenAI-compatible HTTP server, which `VLLMTextEmbedder` uses to compute embeddings through the Embeddings API.
|
||||
|
||||
`VLLMTextEmbedder` expects a vLLM server to be running and accessible at the `api_base_url` parameter (by default, `http://localhost:8000/v1`). Use this component to embed a simple string (such as a query) into a vector. For embedding lists of documents, use the [`VLLMDocumentEmbedder`](vllmdocumentembedder.mdx).
|
||||
|
||||
When you perform embedding retrieval, use this component first to transform your query into a vector. Then, the embedding Retriever will use the vector to search for similar or relevant documents.
|
||||
|
||||
If the vLLM server was started with `--api-key`, provide the API key through the `VLLM_API_KEY` environment variable or the `api_key` init parameter using Haystack's [Secret](../../concepts/secret-management.mdx) API.
|
||||
|
||||
### Compatible models
|
||||
|
||||
vLLM supports a range of embedding models. Check the [vLLM pooling models docs](https://docs.vllm.ai/en/stable/models/pooling_models) for the list of supported architectures and models.
|
||||
|
||||
### vLLM-specific parameters
|
||||
|
||||
You can pass vLLM-specific parameters through the `extra_parameters` dictionary. These are forwarded as `extra_body` to the OpenAI-compatible embeddings endpoint. Use this to pass parameters that are not part of the standard OpenAI Embeddings API, such as `truncate_prompt_tokens` or `truncation_side`. See the [vLLM Embeddings API docs](https://docs.vllm.ai/en/stable/models/pooling_models/embed/#openai-compatible-embeddings-api) for details.
|
||||
|
||||
```python
|
||||
embedder = VLLMTextEmbedder(
|
||||
model="google/embeddinggemma-300m",
|
||||
extra_parameters={"truncate_prompt_tokens": 256, "truncation_side": "right"},
|
||||
)
|
||||
```
|
||||
|
||||
### Matryoshka embeddings
|
||||
|
||||
If the model was trained with Matryoshka Representation Learning, you can reduce the dimensionality of the output vector through the `dimensions` parameter. See the [vLLM Matryoshka docs](https://docs.vllm.ai/en/stable/models/pooling_models/embed/#matryoshka-embeddings) for details.
|
||||
|
||||
### Instructions
|
||||
|
||||
Some embedding models require prepending the text with an instruction to work better for retrieval. For example, if you use [BAAI/bge-large-en-v1.5](https://huggingface.co/BAAI/bge-large-en-v1.5#model-list), you should prefix your query with the following instruction: "Represent this sentence for searching relevant passages:".
|
||||
|
||||
This is how it works with `VLLMTextEmbedder`:
|
||||
|
||||
```python
|
||||
instruction = "Represent this sentence for searching relevant passages:"
|
||||
embedder = VLLMTextEmbedder(
|
||||
model="BAAI/bge-large-en-v1.5",
|
||||
prefix=instruction,
|
||||
)
|
||||
```
|
||||
|
||||
## Usage
|
||||
|
||||
Install the `vllm-haystack` package to use the `VLLMTextEmbedder`:
|
||||
|
||||
```shell
|
||||
pip install vllm-haystack
|
||||
```
|
||||
|
||||
### Starting the vLLM server
|
||||
|
||||
Before using this component, start a vLLM server with an embedding model:
|
||||
|
||||
```bash
|
||||
vllm serve google/embeddinggemma-300m
|
||||
```
|
||||
|
||||
For details on server options, see the [vLLM CLI docs](https://docs.vllm.ai/en/stable/cli/serve/).
|
||||
|
||||
### On its own
|
||||
|
||||
```python
|
||||
from haystack_integrations.components.embedders.vllm import VLLMTextEmbedder
|
||||
|
||||
text_embedder = VLLMTextEmbedder(model="google/embeddinggemma-300m")
|
||||
print(text_embedder.run("I love pizza!"))
|
||||
|
||||
# {'embedding': [-0.0215301513671875, 0.01499176025390625, ...], 'meta': {...}}
|
||||
```
|
||||
|
||||
### In a pipeline
|
||||
|
||||
```python
|
||||
from haystack import Document, Pipeline
|
||||
from haystack.components.retrievers.in_memory import InMemoryEmbeddingRetriever
|
||||
from haystack.document_stores.in_memory import InMemoryDocumentStore
|
||||
from haystack_integrations.components.embedders.vllm import (
|
||||
VLLMDocumentEmbedder,
|
||||
VLLMTextEmbedder,
|
||||
)
|
||||
|
||||
document_store = InMemoryDocumentStore(embedding_similarity_function="cosine")
|
||||
|
||||
documents = [
|
||||
Document(content="My name is Wolfgang and I live in Berlin"),
|
||||
Document(content="I saw a black horse running"),
|
||||
Document(content="Germany has many big cities"),
|
||||
]
|
||||
|
||||
document_embedder = VLLMDocumentEmbedder(model="google/embeddinggemma-300m")
|
||||
documents_with_embeddings = document_embedder.run(documents)["documents"]
|
||||
document_store.write_documents(documents_with_embeddings)
|
||||
|
||||
query_pipeline = Pipeline()
|
||||
query_pipeline.add_component(
|
||||
"text_embedder",
|
||||
VLLMTextEmbedder(model="google/embeddinggemma-300m"),
|
||||
)
|
||||
query_pipeline.add_component(
|
||||
"retriever",
|
||||
InMemoryEmbeddingRetriever(document_store=document_store),
|
||||
)
|
||||
query_pipeline.connect("text_embedder.embedding", "retriever.query_embedding")
|
||||
|
||||
query = "Who lives in Berlin?"
|
||||
|
||||
result = query_pipeline.run({"text_embedder": {"text": query}})
|
||||
|
||||
print(result["retriever"]["documents"][0])
|
||||
|
||||
# Document(id=..., content: 'My name is Wolfgang and I live in Berlin', score: ...)
|
||||
```
|
||||
@@ -0,0 +1,147 @@
|
||||
---
|
||||
title: "WatsonxDocumentEmbedder"
|
||||
id: watsonxdocumentembedder
|
||||
slug: "/watsonxdocumentembedder"
|
||||
description: "The vectors computed by this component are necessary to perform embedding retrieval on a collection of documents. At retrieval time, the vector that represents the query is compared with those of the documents to find the most similar or relevant documents."
|
||||
---
|
||||
|
||||
# WatsonxDocumentEmbedder
|
||||
|
||||
The vectors computed by this component are necessary to perform embedding retrieval on a collection of documents. At retrieval time, the vector that represents the query is compared with those of the documents to find the most similar or relevant documents.
|
||||
|
||||
<div className="key-value-table">
|
||||
|
||||
| | |
|
||||
| --- | --- |
|
||||
| **Most common position in a pipeline** | Before a [`DocumentWriter`](../writers/documentwriter.mdx) in an indexing pipeline |
|
||||
| **Mandatory init variables** | `api_key`: The IBM Cloud API key. Can be set with `WATSONX_API_KEY` env var. <br /> <br />`project_id`: The IBM Cloud project ID. Can be set with `WATSONX_PROJECT_ID` env var. |
|
||||
| **Mandatory run variables** | `documents`: A list of documents to be embedded |
|
||||
| **Output variables** | `documents`: A list of documents (enriched with embeddings) <br /> <br />`meta`: A dictionary of metadata strings |
|
||||
| **API reference** | [Watsonx](/reference/integrations-watsonx) |
|
||||
| **GitHub link** | https://github.com/deepset-ai/haystack-core-integrations/tree/main/integrations/watsonx |
|
||||
| **Package name** | `watsonx-haystack` |
|
||||
|
||||
</div>
|
||||
|
||||
## Overview
|
||||
|
||||
`WatsonxDocumentEmbedder` enriches the metadata of documents with an embedding of their content. To embed a string, you should use the [`WatsonxTextEmbedder`](watsonxtextembedder.mdx).
|
||||
|
||||
The component supports IBM watsonx.ai embedding models such as `ibm/slate-30m-english-rtrvr` and similar. The default model is `ibm/slate-30m-english-rtrvr`. This list of all supported models can be found in IBM's [model documentation](https://dataplatform.cloud.ibm.com/docs/content/wsj/analyze-data/fm-models-embed.html?context=wx).
|
||||
|
||||
To start using this integration with Haystack, install it with:
|
||||
|
||||
```shell
|
||||
pip install watsonx-haystack
|
||||
```
|
||||
|
||||
The component uses `WATSONX_API_KEY` and `WATSONX_PROJECT_ID` environment variables by default. Otherwise, you can pass API credentials at initialization with `api_key` and `project_id`:
|
||||
|
||||
```python
|
||||
embedder = WatsonxDocumentEmbedder(
|
||||
api_key=Secret.from_token("<your-api-key>"),
|
||||
project_id=Secret.from_token("<your-project-id>"),
|
||||
)
|
||||
```
|
||||
|
||||
To get IBM Cloud credentials, head over to https://cloud.ibm.com/.
|
||||
|
||||
### Embedding Metadata
|
||||
|
||||
Text documents often come with a set of metadata. If they are distinctive and semantically meaningful, you can embed them along with the text of the document to improve retrieval.
|
||||
|
||||
You can do this by using the Document Embedder:
|
||||
|
||||
```python
|
||||
from haystack import Document
|
||||
from haystack_integrations.components.embedders.watsonx.document_embedder import (
|
||||
WatsonxDocumentEmbedder,
|
||||
)
|
||||
from haystack.utils import Secret
|
||||
|
||||
doc = Document(content="some text", meta={"title": "relevant title", "page number": 18})
|
||||
|
||||
embedder = WatsonxDocumentEmbedder(
|
||||
api_key=Secret.from_env_var("WATSONX_API_KEY"),
|
||||
project_id=Secret.from_env_var("WATSONX_PROJECT_ID"),
|
||||
meta_fields_to_embed=["title"],
|
||||
)
|
||||
|
||||
docs_w_embeddings = embedder.run(documents=[doc])["documents"]
|
||||
```
|
||||
|
||||
## Usage
|
||||
|
||||
Install the `watsonx-haystack` package to use the `WatsonxDocumentEmbedder`:
|
||||
|
||||
```shell
|
||||
pip install watsonx-haystack
|
||||
```
|
||||
|
||||
### On its own
|
||||
|
||||
Remember to set `WATSONX_API_KEY` and `WATSONX_PROJECT_ID` as environment variables first, or pass them in directly.
|
||||
|
||||
Here is how you can use the component on its own:
|
||||
|
||||
```python
|
||||
from haystack import Document
|
||||
from haystack_integrations.components.embedders.watsonx.document_embedder import (
|
||||
WatsonxDocumentEmbedder,
|
||||
)
|
||||
|
||||
doc = Document(content="I love pizza!")
|
||||
|
||||
embedder = WatsonxDocumentEmbedder()
|
||||
|
||||
result = embedder.run([doc])
|
||||
print(result["documents"][0].embedding)
|
||||
# [-0.453125, 1.2236328, 2.0058594, 0.67871094...]
|
||||
```
|
||||
|
||||
### In a pipeline
|
||||
|
||||
```python
|
||||
from haystack import Pipeline
|
||||
from haystack.document_stores.in_memory import InMemoryDocumentStore
|
||||
from haystack.components.writers import DocumentWriter
|
||||
from haystack.components.retrievers.in_memory import InMemoryEmbeddingRetriever
|
||||
|
||||
from haystack_integrations.components.embedders.watsonx.document_embedder import (
|
||||
WatsonxDocumentEmbedder,
|
||||
)
|
||||
from haystack_integrations.components.embedders.watsonx.text_embedder import (
|
||||
WatsonxTextEmbedder,
|
||||
)
|
||||
|
||||
document_store = InMemoryDocumentStore(embedding_similarity_function="cosine")
|
||||
|
||||
documents = [
|
||||
Document(content="My name is Wolfgang and I live in Berlin"),
|
||||
Document(content="I saw a black horse running"),
|
||||
Document(content="Germany has many big cities"),
|
||||
]
|
||||
|
||||
indexing_pipeline = Pipeline()
|
||||
indexing_pipeline.add_component("embedder", WatsonxDocumentEmbedder())
|
||||
indexing_pipeline.add_component("writer", DocumentWriter(document_store=document_store))
|
||||
indexing_pipeline.connect("embedder", "writer")
|
||||
|
||||
indexing_pipeline.run({"embedder": {"documents": documents}})
|
||||
|
||||
query_pipeline = Pipeline()
|
||||
query_pipeline.add_component("text_embedder", WatsonxTextEmbedder())
|
||||
query_pipeline.add_component(
|
||||
"retriever",
|
||||
InMemoryEmbeddingRetriever(document_store=document_store),
|
||||
)
|
||||
query_pipeline.connect("text_embedder.embedding", "retriever.query_embedding")
|
||||
|
||||
query = "Who lives in Berlin?"
|
||||
|
||||
result = query_pipeline.run({"text_embedder": {"text": query}})
|
||||
|
||||
print(result["retriever"]["documents"][0])
|
||||
|
||||
# Document(id=..., text: 'My name is Wolfgang and I live in Berlin')
|
||||
```
|
||||
@@ -0,0 +1,120 @@
|
||||
---
|
||||
title: "WatsonxTextEmbedder"
|
||||
id: watsonxtextembedder
|
||||
slug: "/watsonxtextembedder"
|
||||
description: "When you perform embedding retrieval, you use this component to transform your query into a vector. Then, the embedding Retriever looks for similar or relevant documents."
|
||||
---
|
||||
|
||||
# WatsonxTextEmbedder
|
||||
|
||||
When you perform embedding retrieval, you use this component to transform your query into a vector. Then, the embedding Retriever looks for similar or relevant documents.
|
||||
|
||||
<div className="key-value-table">
|
||||
|
||||
| | |
|
||||
| --- | --- |
|
||||
| **Most common position in a pipeline** | Before an embedding [Retriever](../retrievers.mdx) in a query/RAG pipeline |
|
||||
| **Mandatory init variables** | `api_key`: An IBM Cloud API key. Can be set with `WATSONX_API_KEY` env var. <br /> <br />`project_id`: An IBM Cloud project ID. Can be set with `WATSONX_PROJECT_ID` env var. |
|
||||
| **Mandatory run variables** | `text`: A string |
|
||||
| **Output variables** | `embedding`: A list of float numbers <br /> <br />`meta`: A dictionary of metadata |
|
||||
| **API reference** | [Watsonx](/reference/integrations-watsonx) |
|
||||
| **GitHub link** | https://github.com/deepset-ai/haystack-core-integrations/tree/main/integrations/watsonx |
|
||||
| **Package name** | `watsonx-haystack` |
|
||||
|
||||
</div>
|
||||
|
||||
## Overview
|
||||
|
||||
To see the list of compatible IBM watsonx.ai embedding models, head over to IBM [documentation](https://dataplatform.cloud.ibm.com/docs/content/wsj/analyze-data/fm-models-embed.html?context=wx). The default model for `WatsonxTextEmbedder` is `ibm/slate-30m-english-rtrvr`. You can specify another model with the `model` parameter when initializing this component.
|
||||
|
||||
Use `WatsonxTextEmbedder` to embed a simple string (such as a query) into a vector. For embedding lists of documents, use the [`WatsonxDocumentEmbedder`](watsonxdocumentembedder.mdx), which enriches the document with the computed embedding, also known as vector.
|
||||
|
||||
The component uses `WATSONX_API_KEY` and `WATSONX_PROJECT_ID` environment variables by default. Otherwise, you can pass API credentials at initialization with `api_key` and `project_id`:
|
||||
|
||||
```python
|
||||
embedder = WatsonxTextEmbedder(
|
||||
api_key=Secret.from_token("<your-api-key>"),
|
||||
project_id=Secret.from_token("<your-project-id>"),
|
||||
)
|
||||
```
|
||||
|
||||
## Usage
|
||||
|
||||
Install the `watsonx-haystack` package to use the `WatsonxTextEmbedder`:
|
||||
|
||||
```shell
|
||||
pip install watsonx-haystack
|
||||
```
|
||||
|
||||
### On its own
|
||||
|
||||
Here is how you can use the component on its own:
|
||||
|
||||
```python
|
||||
from haystack_integrations.components.embedders.watsonx.text_embedder import (
|
||||
WatsonxTextEmbedder,
|
||||
)
|
||||
from haystack.utils import Secret
|
||||
|
||||
text_to_embed = "I love pizza!"
|
||||
|
||||
text_embedder = WatsonxTextEmbedder(
|
||||
api_key=Secret.from_env_var("WATSONX_API_KEY"),
|
||||
project_id=Secret.from_env_var("WATSONX_PROJECT_ID"),
|
||||
model="ibm/slate-30m-english-rtrvr",
|
||||
)
|
||||
|
||||
print(text_embedder.run(text_to_embed))
|
||||
|
||||
# {'embedding': [0.017020374536514282, -0.023255806416273117, ...],
|
||||
# 'meta': {'model': 'ibm/slate-30m-english-rtrvr',
|
||||
# 'truncated_input_tokens': 3}}
|
||||
```
|
||||
|
||||
:::info
|
||||
We recommend setting WATSONX_API_KEY and WATSONX_PROJECT_ID as environment variables instead of setting them as parameters.
|
||||
:::
|
||||
|
||||
### In a pipeline
|
||||
|
||||
```python
|
||||
from haystack import Document
|
||||
from haystack import Pipeline
|
||||
from haystack.document_stores.in_memory import InMemoryDocumentStore
|
||||
from haystack_integrations.components.embedders.watsonx.text_embedder import (
|
||||
WatsonxTextEmbedder,
|
||||
)
|
||||
from haystack_integrations.components.embedders.watsonx.document_embedder import (
|
||||
WatsonxDocumentEmbedder,
|
||||
)
|
||||
from haystack.components.retrievers.in_memory import InMemoryEmbeddingRetriever
|
||||
|
||||
document_store = InMemoryDocumentStore(embedding_similarity_function="cosine")
|
||||
|
||||
documents = [
|
||||
Document(content="My name is Wolfgang and I live in Berlin"),
|
||||
Document(content="I saw a black horse running"),
|
||||
Document(content="Germany has many big cities"),
|
||||
]
|
||||
|
||||
document_embedder = WatsonxDocumentEmbedder()
|
||||
documents_with_embeddings = document_embedder.run(documents)["documents"]
|
||||
document_store.write_documents(documents_with_embeddings)
|
||||
|
||||
query_pipeline = Pipeline()
|
||||
query_pipeline.add_component("text_embedder", WatsonxTextEmbedder())
|
||||
query_pipeline.add_component(
|
||||
"retriever",
|
||||
InMemoryEmbeddingRetriever(document_store=document_store),
|
||||
)
|
||||
query_pipeline.connect("text_embedder.embedding", "retriever.query_embedding")
|
||||
|
||||
query = "Who lives in Berlin?"
|
||||
|
||||
result = query_pipeline.run({"text_embedder": {"text": query}})
|
||||
|
||||
print(result["retriever"]["documents"][0])
|
||||
|
||||
# Document(id=..., mimetype: 'text/plain',
|
||||
# text: 'My name is Wolfgang and I live in Berlin')
|
||||
```
|
||||
Reference in New Issue
Block a user