Model2Vec
Model2Vec is a Python library designed for training and using state-of-the-art static embeddings for various NLP tasks like classification, clustering, and semantic search. Built on top of Hugging Face's `transformers` library, it aims for fast and efficient embedding generation. The current version is 0.8.1, and it maintains an active release cadence with updates typically occurring monthly or bi-monthly.
Warnings
- breaking The `v0.5.0` release included a significant 'rewrite backend' which likely introduced breaking changes to the API, particularly around model initialization and internal component access. Users upgrading from versions prior to 0.5.0 may need to refactor their code.
- gotcha Prior to `v0.8.1`, Windows users might encounter path-related issues when loading models or processing data due to non-POSIX path handling. This was addressed in v0.8.1.
- gotcha Model2Vec relies heavily on Hugging Face `transformers`. Mismatches or outdated versions of `transformers` can lead to issues with tokenizers, padding, or model loading. Version `v0.7.0` specifically included a fix for padding token recognition and an update to `transformers` usage.
Install
-
pip install model2vec
Imports
- Model2Vec
from model2vec import Model2Vec
Quickstart
from model2vec import Model2Vec
import os
# Load a pre-trained model. Specify 'device' for CPU/GPU.
# Example uses a dummy device for quickstart portability.
model = Model2Vec("minishlab/m2v_base", device=os.environ.get('MODEL2VEC_DEVICE', 'cpu'))
# Get embeddings for some text
sentences = [
"This is a test sentence for model2vec.",
"Another example sentence to demonstrate embedding."
]
embeddings = model.encode(sentences)
print(f"Embeddings shape: {embeddings.shape}")
# Expected output for base model: Embeddings shape: (2, 768)