FastEmbed Documentation

repository·main·Indexed 25 days ago

https://github.com/qdrant/fastembed

A lightweight, high-performance Python library for generating text, image, and multimodal embeddings using the ONNX Runtime. FastEmbed supports dense, sparse, and late interaction (ColBERT) embeddings, as well as document reranking via TextCrossEncoder. It is designed for efficiency in serverless environments and provides optional GPU acceleration via the fastembed-gpu package, including support for multi-GPU setups and integration with the qdrant-client.

Tokens
12.6K
Snippets
42
Records
71
Agent score
84%

What's inside fastembed

  1. Run FastEmbed on GPU

    main

    To use GPU acceleration, install fastembed-gpu and specify the CUDAExecutionProvider in the providers argument when initializing a model.

    pip install fastembed-gpu
    from fastembed import TextEmbedding
    
    embedding_model = TextEmbedding(
        model_name="BAAI/bge-small-en-v1.5", 
        providers=["CUDAExecutionProvider"]
    )
  2. Install Qdrant Client with FastEmbed support

    main

    To use FastEmbed directly within the Qdrant client for seamless embedding generation during ingestion and querying, install the qdrant-client with the [fastembed] extra.

    Note: If you are using zsh, you may need to wrap the installation command in quotes.

    pip install qdrant-client[fastembed]
    # or for zsh users:
    pip install 'qdrant-client[fastembed]'
  3. Install FastEmbed

    main

    Install the lightweight FastEmbed library using pip. You can choose between the standard CPU version or the GPU-supported version.

    CPU version:

    pip install fastembed

    GPU version:

    pip install fastembed-gpu
  4. Use FastEmbed with Qdrant Client

    main

    You can integrate FastEmbed directly into the qdrant-client workflow. First, install the integration: pip install qdrant-client[fastembed] (or [fastembed-gpu]). This allows you to pass a model_name directly into models.Document and use client.upload_collection with documents to automate embedding generation.

    from qdrant_client import QdrantClient, models
    
    client = QdrantClient("localhost", port=6333)
    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
  5. Optimize Binary Quantization accuracy with Rescoring and Oversampling

    main

    Binary Quantization can significantly improve retrieval latency (up to 20x), but may impact accuracy. To mitigate this, use the following strategy:

    1. Oversampling: Retrieve more candidates than the requested limit using the binary index. For example, if limit=10 and oversampling=5.0, Qdrant retrieves 50 candidates.
    2. Rescoring: Set rescore=True in QuantizationSearchParams. This tells Qdrant to take the candidates found via binary search and re-rank them using their original high-precision vector values. This combination typically yields higher accuracy than binary search alone.
  6. Generate text embeddings with TextEmbedding

    main

    Use the TextEmbedding class to embed a list of strings. The .embed() method returns a generator of vectors, which is memory-efficient for large datasets. You can convert this generator to a list or a NumPy array.

    import numpy as np
    from fastembed import TextEmbedding
    
    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 triggers model download and initialization
    embedding_model = TextEmbedding()
    
    # Returns a generator
    embeddings_generator = embedding_model.embed(documents)
    
    # Convert to list
    embeddings_list = list(embeddings_generator)
    
    # Convert to numpy array
    embeddings_array = np.array(list(embedding_model.embed(documents)))
  7. Format input texts for retrieval models

    main

    When using models for retrieval tasks (like BGE), input texts should follow specific prefixing conventions to ensure optimal performance.

    • For retrieval tasks, prefix each input text with query: or passage: .
    • For non-retrieval tasks, you can simply use the query: prefix.
    • These prefixes should be applied even when working with non-English languages.
  8. Export and optimize a model to ONNX format

    main

    You can export a Hugging Face model to ONNX and apply optimizations using optimum. This process involves exporting the model, applying an optimization configuration (e.g., O4), and saving the optimized model and tokenizer to a directory.

    Steps:

    1. Load the model using ORTModelForFeatureExtraction.from_pretrained(model_id, export=True).
    2. Use ORTOptimizer with an AutoOptimizationConfig (like O4()) to optimize the model.
    3. Save the optimized model and tokenizer to a local directory.
    from optimum.onnxruntime import AutoOptimizationConfig, ORTModelForFeatureExtraction, ORTOptimizer
    from transformers import AutoTokenizer
    from pathlib import Path
    
    model_id = "BAAI/bge-small-en-v1.5"
    save_dir = Path(f"fast-{model_id.split('/')[1]}")
    save_dir.mkdir(parents=True, exist_ok=True)
    
    tokenizer = AutoTokenizer.from_pretrained(model_id)
    model = ORTModelForFeatureExtraction.from_pretrained(model_id, export=True)
    
    # Optimization
    optimization_config = AutoOptimizationConfig.O4()
    optimizer = ORTOptimizer.from_pretrained(model)
    optimizer.optimize(
        save_dir=save_dir, 
        optimization_config=optimization_config, 
        use_external_data_format=True
    )
    
    # Save results
    model = ORTModelForFeatureExtraction.from_pretrained(save_dir)
    tokenizer.save_pretrained(save_dir)
  9. Format documents for retrieval tasks

    main

    When performing retrieval tasks (queries vs. passages) using the default model, you should prepend specific labels to your strings to optimize performance:

    • Queries: Prepend query: to the beginning of the string.
    • Passages: Prepend passage: to the beginning of the string.
  10. Install FastEmbed with GPU support

    main

    To use GPU acceleration, install the fastembed-gpu package.

    CRITICAL: fastembed-gpu and fastembed cannot coexist in the same environment. Similarly, onnxruntime-gpu and onnxruntime cannot coexist. If you have the standard version installed, you must uninstall it before installing the GPU version.

    !pip install fastembed-gpu