Overview
A sparse embedding implementation using the FastEmbed library for generating sparse text embeddings compatible with Qdrant vector store.
Description
FastEmbedSparse is a concrete implementation of the SparseEmbeddings abstract base class in the langchain-qdrant partner package. It wraps the fastembed.SparseTextEmbedding model to produce sparse vector representations of text, which are useful for hybrid search in Qdrant. The class supports configurable model names (defaulting to Qdrant/bm25), batch sizes, cache directories, threading, ONNX execution providers, and data-parallel encoding for large datasets.
Usage
Import this class when you need sparse text embeddings for hybrid (dense + sparse) vector search in Qdrant. Requires the fastembed or fastembed-gpu package to be installed.
Code Reference
Source Location
- Repository: Langchain_ai_Langchain
- File:
libs/partners/qdrant/langchain_qdrant/fastembed_sparse.py
- Lines: 1-84
Signature
class FastEmbedSparse(SparseEmbeddings):
"""An interface for sparse embedding models to use with Qdrant."""
def __init__(
self,
model_name: str = "Qdrant/bm25",
batch_size: int = 256,
cache_dir: str | None = None,
threads: int | None = None,
providers: Sequence[Any] | None = None,
parallel: int | None = None,
**kwargs: Any,
) -> None:
...
def embed_documents(self, texts: list[str]) -> list[SparseVector]:
...
def embed_query(self, text: str) -> SparseVector:
...
Import
from langchain_qdrant import FastEmbedSparse
I/O Contract
Inputs (Constructor Parameters)
| Name |
Type |
Required |
Description
|
| model_name |
str |
No |
The name of the sparse embedding model. Defaults to "Qdrant/bm25".
|
| batch_size |
int |
No |
Batch size for encoding. Defaults to 256.
|
| cache_dir |
None |
No |
Path to the model cache directory. Can also be set via FASTEMBED_CACHE_PATH env variable.
|
| threads |
None |
No |
Number of threads for the ONNX runtime session.
|
| providers |
None |
No |
List of ONNX execution providers.
|
| parallel |
None |
No |
Data-parallel encoding setting. >1 enables parallelism, 0 uses all cores, None uses default threading.
|
| **kwargs |
Any |
No |
Additional options passed to fastembed.SparseTextEmbedding.
|
embed_documents
Inputs
| Name |
Type |
Required |
Description
|
| texts |
list[str] |
Yes |
List of text documents to embed.
|
Outputs
| Name |
Type |
Description
|
| return |
list[SparseVector] |
List of sparse vectors, each containing indices and values lists.
|
embed_query
Inputs
| Name |
Type |
Required |
Description
|
| text |
str |
Yes |
Single query text to embed.
|
Outputs
| Name |
Type |
Description
|
| return |
SparseVector |
A sparse vector with indices and values lists.
|
Usage Examples
Basic Usage
from langchain_qdrant import FastEmbedSparse
# Initialize with default BM25 model
sparse_embeddings = FastEmbedSparse(model_name="Qdrant/bm25")
# Embed documents
docs = ["LangChain is a framework for LLM applications", "Qdrant is a vector database"]
sparse_vectors = sparse_embeddings.embed_documents(docs)
# Embed a query
query_vector = sparse_embeddings.embed_query("What is LangChain?")
print(query_vector.indices) # Non-zero dimension indices
print(query_vector.values) # Corresponding values
With Custom Configuration
from langchain_qdrant import FastEmbedSparse
sparse_embeddings = FastEmbedSparse(
model_name="Qdrant/bm25",
batch_size=128,
threads=4,
parallel=2,
cache_dir="/tmp/fastembed_cache",
)
vectors = sparse_embeddings.embed_documents(["document text"])
Related Pages
Page Connections
Double-click a node to navigate. Hold to expand connections.