From 9943b0ebfd4566857add33e3f33997d2b70271ea Mon Sep 17 00:00:00 2001 From: anakin87 Date: Fri, 26 Apr 2024 15:05:41 +0200 Subject: [PATCH] add section on sparse embeddings to Fastembed page --- integrations/fastembed.md | 70 ++++++++++++++++++++++++++++++--------- 1 file changed, 55 insertions(+), 15 deletions(-) diff --git a/integrations/fastembed.md b/integrations/fastembed.md index 8823ea36..c8571642 100644 --- a/integrations/fastembed.md +++ b/integrations/fastembed.md @@ -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 @@ -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 @@ -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 @@ -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.