Model2Vec is a library for static models: dense embeddings, sparse retrieval and classifiers that look up a vector per token instead of running a transformer. They're up to 30,000 times faster than transformer-based embeddings on a CPU, and small enough to ship anywhere. Our dense models are the strongest static embedding models in the world, and our sparse model is a drop-in replacement for BM25 that outperforms it on most retrieval benchmarks. See our results or read our docs.
Install the base package with:
pip install model2vecLoad one of our pre-trained models from the HuggingFace hub and make embeddings, which you can use for retrieval, classification, clustering or RAG:
from model2vec import StaticModel
model = StaticModel.from_pretrained("minishlab/blink-8M")
embeddings = model.encode(["It's dangerous to go alone!", "It's a secret to everybody."])Sparse models can be stored in any inverted index, just like BM25, but need no corpus statistics. Install the sparse extra with:
pip install model2vec[sparse]Then encode documents and queries, and score them with a dot product:
from model2vec.sparse import SparseStaticModel
model = SparseStaticModel.from_pretrained("minishlab/shimmer")
documents = model.encode_document(["It's dangerous to go alone!", "It's a secret to everybody."])
queries = model.encode_query(["Is it safe to go alone?"])
scores = queries @ documents.TYou can fine-tune a pre-trained model for classification, similarity, regression or pair similarity (for example query-document retrieval). Install the train extra with:
pip install model2vec[train]Then train a classifier as follows:
from datasets import load_dataset
from model2vec.train import StaticModelForClassification
# Initialize a classifier from a pre-trained model
classifier = StaticModelForClassification.from_pretrained(path="minishlab/blink-8M")
# Load a dataset. Both single and multi-label classification datasets are supported
ds = load_dataset("setfit/subj")
# Train the classifier on text (X) and labels (y)
classifier.fit(ds["train"]["text"], ds["train"]["label"])
# Evaluate the classifier
classification_report = classifier.evaluate(ds["test"]["text"], ds["test"]["label"])Trained models run without torch. You can also distill your own static model from any Sentence Transformer, see our distillation docs.
- Dense and sparse models: dense embeddings for semantic search, classification and clustering, and sparse models for inverted-index retrieval as a replacement for BM25.
- Multilingual and code: blink-multilingual-64M reaches 99% of the performance of LaBSE on MMTEB, and blink-code-16M outperforms BM25 on code retrieval (CoIR). See our results.
- Small: our best dense model has 7.7M parameters and is ~30 MB on disk. Our sparse model, shimmer, has 76k parameters and is under 1 MB.
- Fast on CPU: encoding is a token lookup and a sum, done in Rust by model2vec-rs.
- Lightweight dependencies: the base package needs no
torchortransformers, justnumpy,tokenizersand the Rust backend. - Fine-tuning: fine-tune models for classification, similarity, regression or pair similarity, and run them without
torch. A fine-tuned classifier scores within 1 point of a fully fine-tuned all-MiniLM-L6-v2 over 26 datasets, while running about 300 times faster (see our training results). - Integrations: load models directly in Sentence Transformers and LangChain, and share them on the HuggingFace hub with
from_pretrainedandpush_to_hub. See our integrations docs.
Our official documentation can be found here. This includes in-depth documentation on inference, training, distillation and integrations.
For details on the Rust backend and which dtypes to use, see internals.
These models are available on the HuggingFace hub and can be loaded with from_pretrained.
| Model | Language | Base model | Params | Task |
|---|---|---|---|---|
| blink-8M | English | bge-base-en-v1.5 | 7.7M | General |
| blink-multilingual-64M | Multilingual | bge-m3 | 64M | General |
| blink-code-16M | Code | CodeRankEmbed | 16M | Code |
| shimmer | English | blink-8M | 76k | Sparse retrieval |
We have performed extensive experiments to evaluate the performance of Model2Vec models. The results are documented in the results folder:
Thanks to Greptile for providing free access to their AI code review platform.
MIT
If you use Model2Vec in your research, please cite the following:
@software{minishlab2024model2vec,
author = {Stephan Tulkens and {van Dongen}, Thomas},
title = {Model2Vec: Fast State-of-the-Art Static Embeddings},
year = {2024},
publisher = {Zenodo},
doi = {10.5281/zenodo.17270888},
url = {https://github.com/MinishLab/model2vec},
license = {MIT}
}