Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add section on sparse embeddings to Fastembed page #225

Merged
merged 1 commit into from
Apr 26, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
70 changes: 55 additions & 15 deletions integrations/fastembed.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,11 @@ layout: integration
name: FastEmbed
description: Use the FastEmbed embedding models
authors:
- name: deepset
socials:
github: deepset-ai
twitter: deepset_ai
linkedin: deepset-ai
- name: Nicola Procopio
socials:
github: nickprock
Expand All @@ -24,14 +29,9 @@ toc: true
## Overview
[FastEmbed](https://qdrant.github.io/fastembed/) is a lightweight, fast, Python library built for embedding generation.

1. Light & Fast
* Quantized model weights
* ONNX Runtime for inference via Optimum

2. Accuracy/Recall
* Better than OpenAI Ada-002
* Default is Flag Embedding, which is top of the [MTEB leaderboard](https://huggingface.co/spaces/mteb/leaderboard)
* List of [supported models](https://qdrant.github.io/fastembed/examples/Supported_Models/) - including multilingual models
- Light and fast: quantized model weights; ONNX Runtime for inference via Optimum.
- Performant embedding models: list of [supported models](https://qdrant.github.io/fastembed/examples/Supported_Models/) - including multilingual models.
- Support for sparse embedding models.


## Installation
Expand All @@ -42,14 +42,13 @@ pip install fastembed-haystack

## Usage
### Components

You can use Fastembed models with two components: [FastembedTextEmbedder](https://github.com/deepset-ai/haystack-core-integrations/blob/main/integrations/fastembed/src/haystack_integrations/components/embedders/fastembed/fastembed_text_embedder.py) and [FastembedDocumentEmbedder](https://github.com/deepset-ai/haystack-core-integrations/blob/main/integrations/fastembed/src/haystack_integrations/components/embedders/fastembed/fastembed_document_embedder.py).

To create semantic embeddings for documents, use `FastembedDocumentEmbedder` in your indexing pipeline. For generating embeddings for queries, use `FastembedTextEmbedder`.
The `fastembed-haystack` integrations provides the following components:
- `FastembedTextEmbedder`: creates a dense embedding for text (used in query/RAG pipelines).
- `FastembedDocumentEmbedder`: enriches documents with dense embeddings (used in indexing pipelines).
- `FastembedSparseTextEmbedder`: creates a sparse embedding for text (used in query/RAG pipelines).
- `FastembedSparseDocumentEmbedder`: enriches documents with sparse embeddings (used in indexing pipelines).

### Example

Below is the example indexing pipeline with `InMemoryDocumentStore`, `InMemoryEmbeddingRetriever`, `FastembedTextEmbedder` and `FastembedDocumentEmbedder`:
### Example with dense embeddings

```python
from haystack import Document, Pipeline
Expand Down Expand Up @@ -81,6 +80,47 @@ query = "Who supports fastembed?"
result = query_pipeline.run({"text_embedder": {"text": query}})
```

### Example with sparse embeddings
Currently, Sparse Embedding retrieval is only supported by `QdrantDocumentStore`.
You can install the package as follows:
```bash
pip install qdrant-haystack
```

```python
from haystack import Document, Pipeline
from haystack_integrations.components.retrievers.qdrant import QdrantSparseEmbeddingRetriever
from haystack_integrations.document_stores.qdrant import QdrantDocumentStore
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()
sparse_document_embedder.warm_up()
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", "retriever.query_sparse_embedding")

query = "Who supports fastembed?"

result = query_pipeline.run({"sparse_text_embedder": {"text": query}})
```

### License

`fastembed-haystack` is distributed under the terms of the [Apache-2.0](https://spdx.org/licenses/Apache-2.0.html) license.