Profile
Back to NewsBack
GitHub Trending 7 min
Reader Mode
qdrant/fastembed: Fast, Accurate, Lightweight Python library to make State of the Art Embedding

qdrant/fastembed: Fast, Accurate, Lightweight Python library to make State of the Art Embedding

⚡️ What is FastEmbed?

FastEmbed is a lightweight, fast, Python library built for embedding generation. We support popular text models. Please open a GitHub issue if you want us to add a new model.

The default text embedding (TextEmbedding) model is Flag Embedding, presented in the MTEB leaderboard. It supports "query" and "passage" prefixes for the input text. Here is an example for Retrieval Embedding Generation and how to use FastEmbed with Qdrant.

📈 Why FastEmbed?

  1. Light: FastEmbed is a lightweight library with few external dependencies. We don't require a GPU and don't download GBs of PyTorch dependencies, and instead use the ONNX Runtime. This makes it a great candidate for serverless runtimes like AWS Lambda.
  1. Fast: FastEmbed is designed for speed. We use the ONNX Runtime, which is faster than PyTorch. We also use data parallelism for encoding large datasets.
  1. Accurate: FastEmbed is better than OpenAI Ada-002. We also support an ever-expanding set of models, including a few multilingual models.

🚀 Installation

To install the FastEmbed library, pip works best. You can install it with or without GPU support:

pip install fastembed

or with GPU support

pip install fastembed-gpu

📖 Quickstart

from fastembed import TextEmbedding

Example list of documents

documents: list[str] = [ "This is built to be faster and lighter than other embedding libraries e.g. Transformers, Sentence-Transformers, etc.", "fastembed is supported by and maintained by Qdrant.", ]

This will trigger the model download and initialization

embedding_model = TextEmbedding() print("The model BAAI/bge-small-en-v1.5 is ready to use.")

embeddings_generator = embedding_model.embed(documents) # reminder this is a generator embeddings_list = list(embedding_model.embed(documents)) # you can also convert the generator to a list, and that to a numpy array len(embeddings_list[0]) # Vector of 384 dimensions

Fastembed supports a variety of models for different tasks and modalities. The list of all the available models can be found here

🎒 Dense text embeddings

from fastembed import TextEmbedding

model = TextEmbedding(model_name="BAAI/bge-small-en-v1.5") embeddings = list(model.embed(documents))

[

array([-0.1115, 0.0097, 0.0052, 0.0195, ...], dtype=float32),

array([-0.1019, 0.0635, -0.0332, 0.0522, ...], dtype=float32)

]

Dense text embedding can also be extended with models which are not in the list of supported models.

from fastembed import TextEmbedding
from fastembed.common.model_description import PoolingType, ModelSource

TextEmbedding.add_custom_model( model="intfloat/multilingual-e5-small", pooling=PoolingType.MEAN, normalization=True, sources=ModelSource(hf="intfloat/multilingual-e5-small"), # can be used with an url to load files from a private storage dim=384, model_file="onnx/model.onnx", # can be used to load an already supported model with another optimization or quantization, e.g. onnx/model_O4.onnx ) model = TextEmbedding(model_name="intfloat/multilingual-e5-small") embeddings = list(model.embed(documents))

🔱 Sparse text embeddings

  • SPLADE++
from fastembed import SparseTextEmbedding

model = SparseTextEmbedding(model_name="prithivida/Splade_PP_en_v1") embeddings = list(model.embed(documents))

[

SparseEmbedding(indices=[ 17, 123, 919, ... ], values=[0.71, 0.22, 0.39, ...]),

SparseEmbedding(indices=[ 38, 12, 91, ... ], values=[0.11, 0.22, 0.39, ...])

]

🦥 Late interaction models (aka ColBERT)

from fastembed import LateInteractionTextEmbedding

model = LateInteractionTextEmbedding(model_name="colbert-ir/colbertv2.0") embeddings = list(model.embed(documents))

[

array([

[-0.1115, 0.0097, 0.0052, 0.0195, ...],

[-0.1019, 0.0635, -0.0332, 0.0522, ...],

]),

array([

[-0.9019, 0.0335, -0.0032, 0.0991, ...],

[-0.2115, 0.8097, 0.1052, 0.0195, ...],

]),

]

🖼️ Image embeddings

from fastembed import ImageEmbedding

images = [ "./path/to/image1.jpg", "./path/to/image2.jpg", ]

model = ImageEmbedding(model_name="Qdrant/clip-ViT-B-32-vision") embeddings = list(model.embed(images))

[

array([-0.1115, 0.0097, 0.0052, 0.0195, ...], dtype=float32),

array([-0.1019, 0.0635, -0.0332, 0.0522, ...], dtype=float32)

]

Late interaction multimodal models (ColPali)

from fastembed import LateInteractionMultimodalEmbedding

doc_images = [ "./path/to/qdrant_pdf_doc_1_screenshot.jpg", "./path/to/colpali_pdf_doc_2_screenshot.jpg", ]

query = "What is Qdrant?"

model = LateInteractionMultimodalEmbedding(model_name="Qdrant/colpali-v1.3-fp16") doc_images_embeddings = list(model.embed_image(doc_images))

shape (2, 1030, 128)

[array([[-0.03354, -0.0209, ..., -0.1558, -0.0768]], dtype=float16)]

query_embedding = model.embed_text(query)

shape (1, 20, 128)

[array([[-0.002182, 0.1476, ..., -0.02208, 0.1683]], dtype=float16)]

🔄 Rerankers

from fastembed.rerank.cross_encoder import TextCrossEncoder

query = "Who is maintaining Qdrant?" documents: list[str] = [ "This is built to be faster and lighter than other embedding libraries e.g. Transformers, Sentence-Transformers, etc.", "fastembed is supported by and maintained by Qdrant.", ] encoder = TextCrossEncoder(model_name="Xenova/ms-marco-MiniLM-L-6-v2") scores = list(encoder.rerank(query, documents))

[-11.48061752319336, 5.472434997558594]

Text cross encoders can also be extended with models which are not in the list of supported models.

from fastembed.rerank.cross_encoder import TextCrossEncoder 
from fastembed.common.model_description import ModelSource

TextCrossEncoder.add_custom_model( model="Xenova/ms-marco-MiniLM-L-4-v2", model_file="onnx/model.onnx", sources=ModelSource(hf="Xenova/ms-marco-MiniLM-L-4-v2"), ) model = TextCrossEncoder(model_name="Xenova/ms-marco-MiniLM-L-4-v2") scores = list(model.rerank_pairs( [("What is AI?", "Artificial intelligence is ..."), ("What is ML?", "Machine learning is ..."),] ))

⚡️ FastEmbed on a GPU

FastEmbed supports running on GPU devices. It requires installation of the fastembed-gpu package.

pip install fastembed-gpu

Check our example for detailed instructions: CUDA 12 and 13, installing CUDA and cuDNN from pip, and troubleshooting of the common issues.

from fastembed import TextEmbedding

embedding_model = TextEmbedding( model_name="BAAI/bge-small-en-v1.5", providers=["CUDAExecutionProvider"] ) print("The model BAAI/bge-small-en-v1.5 is ready to use on a GPU.")

On Apple Silicon

FastEmbed can run TextEmbedding models on Apple Silicon GPUs through CoreML, with no extra installation:

from fastembed import TextEmbedding

embedding_model = TextEmbedding("BAAI/bge-small-en-v1.5", providers=["CoreMLExecutionProvider"])

the models that support CoreML

print([model["model"] for model in TextEmbedding.list_supported_models() if model["coreml"]])

Other TextEmbedding models print a warning, since CoreML may run them slower than the CPU. Sparse, late interaction, image, and reranking models have no CoreML support.

Creating the model compiles it for the GPU the first time, which can take a few minutes for larger models, and loads the compiled model from the cache after that. With lazy_load=True, this happens when the model is first used instead, or when you call warmup().

CoreML runs batches of 16 texts padded to a fixed number of tokens, and FastEmbed compiles a version of the model for each of these lengths: 64, 256, and 512 tokens by default. Each length takes its own memory, load time, and disk space. For BAAI/bge-base-en-v1.5 on an M2 Max, each one adds about 0.45 GB of memory, 30 seconds of load time, and 1.5 GB of disk space. More lengths waste less work on padding, which speeds up texts of mixed lengths, while fewer lengths save memory. Set them with coreml_lengths: for example, coreml_lengths=(64, 128, 256, 384, 512) makes texts of mixed lengths about 6% faster and takes 0.9 GB more memory.

Fewer than 4 texts at a time, such as single queries, run on the CPU, and so do texts longer than the largest length.

The compiled models are stored in cache_dir. By default it's a temporary directory that macOS can clear, which means compiling again, so pass a permanent cache_dir or set FASTEMBED_CACHE_PATH.

Usage with Qdrant

Installation with Qdrant Client in Python:

pip install qdrant-client[fastembed]

or

pip install qdrant-client[fastembed-gpu]

You might have to use quotes ``pip install 'qdrant-client[fastembed]'`` on zsh.

from qdrant_client import QdrantClient, models

Initialize the client

client = QdrantClient("localhost", port=6333) # For production

client = QdrantClient(":memory:") # For experimentation

model_name = "sentence-transformers/all-MiniLM-L6-v2" payload = [ {"document": "Qdrant has Langchain integrations", "source": "Langchain-docs", }, {"document": "Qdrant also has Llama Index integrations", "source": "LlamaIndex-docs"}, ] docs = [models.Document(text=data["document"], model=model_name) for data in payload] ids = [42, 2]

client.create_collection( "demo_collection", vectors_config=models.VectorParams( size=client.get_embedding_size(model_name), distance=models.Distance.COSINE) )

client.upload_collection( collection_name="demo_collection", vectors=docs, ids=ids, payload=payload, )

search_result = client.query_points( collection_name="demo_collection", query=models.Document(text="This is a query document", model=model_name) ).points print(search_result)

Chat with me