model2vec 0.3.3__tar.gz → 0.3.5__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- {model2vec-0.3.3 → model2vec-0.3.5}/Makefile +1 -0
- {model2vec-0.3.3 → model2vec-0.3.5}/PKG-INFO +171 -134
- {model2vec-0.3.3 → model2vec-0.3.5}/README.md +167 -131
- {model2vec-0.3.3 → model2vec-0.3.5}/model2vec/distill/distillation.py +54 -17
- {model2vec-0.3.3 → model2vec-0.3.5}/model2vec/distill/tokenizer.py +4 -2
- {model2vec-0.3.3 → model2vec-0.3.5}/model2vec/model.py +58 -19
- {model2vec-0.3.3 → model2vec-0.3.5}/model2vec/utils.py +42 -2
- {model2vec-0.3.3 → model2vec-0.3.5}/model2vec/version.py +1 -1
- {model2vec-0.3.3 → model2vec-0.3.5}/model2vec.egg-info/PKG-INFO +171 -134
- {model2vec-0.3.3 → model2vec-0.3.5}/model2vec.egg-info/SOURCES.txt +1 -0
- {model2vec-0.3.3 → model2vec-0.3.5}/model2vec.egg-info/requires.txt +2 -1
- {model2vec-0.3.3 → model2vec-0.3.5}/pyproject.toml +2 -2
- {model2vec-0.3.3 → model2vec-0.3.5}/tests/test_distillation.py +60 -0
- {model2vec-0.3.3 → model2vec-0.3.5}/tests/test_model.py +23 -1
- model2vec-0.3.5/tutorials/README.md +16 -0
- model2vec-0.3.5/tutorials/semantic_chunking.ipynb +261 -0
- {model2vec-0.3.3 → model2vec-0.3.5}/uv.lock +578 -464
- model2vec-0.3.3/tutorials/README.md +0 -15
- {model2vec-0.3.3 → model2vec-0.3.5}/.github/workflows/ci.yaml +0 -0
- {model2vec-0.3.3 → model2vec-0.3.5}/.gitignore +0 -0
- {model2vec-0.3.3 → model2vec-0.3.5}/.pre-commit-config.yaml +0 -0
- {model2vec-0.3.3 → model2vec-0.3.5}/LICENSE +0 -0
- {model2vec-0.3.3 → model2vec-0.3.5}/assets/images/logo.png +0 -0
- {model2vec-0.3.3 → model2vec-0.3.5}/assets/images/logo_v2.png +0 -0
- {model2vec-0.3.3 → model2vec-0.3.5}/assets/images/model2vec_model_diagram.png +0 -0
- {model2vec-0.3.3 → model2vec-0.3.5}/assets/images/model2vec_model_diagram_transparant_dark.png +0 -0
- {model2vec-0.3.3 → model2vec-0.3.5}/assets/images/model2vec_model_diagram_transparant_light.png +0 -0
- {model2vec-0.3.3 → model2vec-0.3.5}/assets/images/sentences_per_second_vs_average_score.png +0 -0
- {model2vec-0.3.3 → model2vec-0.3.5}/assets/images/speed_vs_accuracy.png +0 -0
- {model2vec-0.3.3 → model2vec-0.3.5}/assets/images/speed_vs_accuracy_v2.png +0 -0
- {model2vec-0.3.3 → model2vec-0.3.5}/assets/images/speed_vs_accuracy_v3.png +0 -0
- {model2vec-0.3.3 → model2vec-0.3.5}/assets/images/speed_vs_accuracy_v4.png +0 -0
- {model2vec-0.3.3 → model2vec-0.3.5}/assets/images/speed_vs_mteb_score.png +0 -0
- {model2vec-0.3.3 → model2vec-0.3.5}/assets/images/speed_vs_mteb_score_v2.png +0 -0
- {model2vec-0.3.3 → model2vec-0.3.5}/assets/images/tutorial_ezlo.png +0 -0
- {model2vec-0.3.3 → model2vec-0.3.5}/model2vec/__init__.py +0 -0
- {model2vec-0.3.3 → model2vec-0.3.5}/model2vec/distill/__init__.py +0 -0
- {model2vec-0.3.3 → model2vec-0.3.5}/model2vec/distill/inference.py +0 -0
- {model2vec-0.3.3 → model2vec-0.3.5}/model2vec/distill/utils.py +0 -0
- {model2vec-0.3.3 → model2vec-0.3.5}/model2vec/hf_utils.py +0 -0
- {model2vec-0.3.3 → model2vec-0.3.5}/model2vec/model_card_template.md +0 -0
- {model2vec-0.3.3 → model2vec-0.3.5}/model2vec.egg-info/dependency_links.txt +0 -0
- {model2vec-0.3.3 → model2vec-0.3.5}/model2vec.egg-info/top_level.txt +0 -0
- {model2vec-0.3.3 → model2vec-0.3.5}/results/README.md +0 -0
- {model2vec-0.3.3 → model2vec-0.3.5}/scripts/export_to_onnx.py +0 -0
- {model2vec-0.3.3 → model2vec-0.3.5}/setup.cfg +0 -0
- {model2vec-0.3.3 → model2vec-0.3.5}/tests/__init__.py +0 -0
- {model2vec-0.3.3 → model2vec-0.3.5}/tests/conftest.py +0 -0
- {model2vec-0.3.3 → model2vec-0.3.5}/tests/data/test_tokenizer/special_tokens_map.json +0 -0
- {model2vec-0.3.3 → model2vec-0.3.5}/tests/data/test_tokenizer/tokenizer.json +0 -0
- {model2vec-0.3.3 → model2vec-0.3.5}/tests/data/test_tokenizer/tokenizer_config.json +0 -0
- {model2vec-0.3.3 → model2vec-0.3.5}/tests/test_utils.py +0 -0
- {model2vec-0.3.3 → model2vec-0.3.5}/tutorials/recipe_search.ipynb +0 -0
- {model2vec-0.3.3 → model2vec-0.3.5}/tutorials/semantic_deduplication.ipynb +0 -0
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
Metadata-Version: 2.
|
|
1
|
+
Metadata-Version: 2.2
|
|
2
2
|
Name: model2vec
|
|
3
|
-
Version: 0.3.
|
|
3
|
+
Version: 0.3.5
|
|
4
4
|
Summary: The Fastest State-of-the-Art Static Embeddings in the World
|
|
5
5
|
Author-email: Stéphan Tulkens <stephantul@gmail.com>, Thomas van Dongen <thomas123@live.nl>
|
|
6
6
|
License: MIT License
|
|
@@ -44,6 +44,7 @@ Requires-Python: >=3.9
|
|
|
44
44
|
Description-Content-Type: text/markdown
|
|
45
45
|
License-File: LICENSE
|
|
46
46
|
Requires-Dist: jinja2
|
|
47
|
+
Requires-Dist: joblib
|
|
47
48
|
Requires-Dist: numpy
|
|
48
49
|
Requires-Dist: rich
|
|
49
50
|
Requires-Dist: safetensors
|
|
@@ -56,7 +57,7 @@ Requires-Dist: ipython; extra == "dev"
|
|
|
56
57
|
Requires-Dist: mypy; extra == "dev"
|
|
57
58
|
Requires-Dist: pre-commit; extra == "dev"
|
|
58
59
|
Requires-Dist: pytest; extra == "dev"
|
|
59
|
-
Requires-Dist: pytest-
|
|
60
|
+
Requires-Dist: pytest-cov; extra == "dev"
|
|
60
61
|
Requires-Dist: ruff; extra == "dev"
|
|
61
62
|
Provides-Extra: distill
|
|
62
63
|
Requires-Dist: torch; extra == "distill"
|
|
@@ -82,7 +83,7 @@ Requires-Dist: torch; extra == "onnx"
|
|
|
82
83
|
<h2>
|
|
83
84
|
<a href="https://huggingface.co/minishlab"><strong>🤗 Models</strong></a> |
|
|
84
85
|
<a href="https://github.com/MinishLab/model2vec/tree/main/tutorials"><strong>📚 Tutorials</strong></a> |
|
|
85
|
-
<a href="https://
|
|
86
|
+
<a href="https://minishlab.github.io/"><strong>🌐 Website</strong></a> |
|
|
86
87
|
<a href="https://github.com/MinishLab/model2vec/blob/main/results/README.md"><strong>🏆 Results</strong></a>
|
|
87
88
|
</h2>
|
|
88
89
|
</div>
|
|
@@ -119,35 +120,33 @@ Model2Vec is a technique to turn any sentence transformer into a really small st
|
|
|
119
120
|
- [Main Features](#main-features)
|
|
120
121
|
- [What is Model2Vec?](#what-is-model2vec)
|
|
121
122
|
- [Usage](#usage)
|
|
122
|
-
- [Distillation](#distillation)
|
|
123
123
|
- [Inference](#inference)
|
|
124
|
+
- [Distillation](#distillation)
|
|
124
125
|
- [Evaluation](#evaluation)
|
|
125
|
-
|
|
126
|
+
- [Integrations](#integrations)
|
|
126
127
|
- [Model List](#model-list)
|
|
127
128
|
- [Results](#results)
|
|
128
|
-
- [Related Work](#related-work)
|
|
129
|
-
- [Citing](#citing)
|
|
130
129
|
|
|
131
130
|
## Quickstart
|
|
132
131
|
|
|
133
|
-
Install the package
|
|
132
|
+
Install the package with:
|
|
133
|
+
|
|
134
134
|
```bash
|
|
135
|
-
pip install model2vec
|
|
135
|
+
pip install model2vec
|
|
136
136
|
```
|
|
137
137
|
|
|
138
|
-
|
|
138
|
+
This will install the base inference package, which only depends on `numpy` and a few other minor dependencies. If you want to distill your own models, you can install the distillation extras with:
|
|
139
139
|
|
|
140
140
|
```bash
|
|
141
|
-
pip install model2vec
|
|
141
|
+
pip install model2vec[distill]
|
|
142
142
|
```
|
|
143
143
|
|
|
144
|
-
The easiest way to get started with Model2Vec is to
|
|
144
|
+
The easiest way to get started with Model2Vec is to load one of our [flagship models from the HuggingFace hub](https://huggingface.co/collections/minishlab/potion-6721e0abd4ea41881417f062). These models are pre-trained and ready to use. The following code snippet shows how to load a model and make embeddings:
|
|
145
145
|
```python
|
|
146
146
|
from model2vec import StaticModel
|
|
147
147
|
|
|
148
148
|
# Load a model from the HuggingFace hub (in this case the potion-base-8M model)
|
|
149
|
-
|
|
150
|
-
model = StaticModel.from_pretrained(model_name)
|
|
149
|
+
model = StaticModel.from_pretrained("minishlab/potion-base-8M")
|
|
151
150
|
|
|
152
151
|
# Make embeddings
|
|
153
152
|
embeddings = model.encode(["It's dangerous to go alone!", "It's a secret to everybody."])
|
|
@@ -158,82 +157,89 @@ token_embeddings = model.encode_as_sequence(["It's dangerous to go alone!", "It'
|
|
|
158
157
|
|
|
159
158
|
And that's it. You can use the model to classify texts, to cluster, or to build a RAG system.
|
|
160
159
|
|
|
161
|
-
Instead of using one of our models, you can distill your own Model2Vec model from a Sentence Transformer model. The following code snippet shows how to distill a model:
|
|
160
|
+
Instead of using one of our models, you can also distill your own Model2Vec model from a Sentence Transformer model. The following code snippet shows how to distill a model:
|
|
162
161
|
```python
|
|
163
162
|
from model2vec.distill import distill
|
|
164
163
|
|
|
165
|
-
#
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
# Distill the model
|
|
169
|
-
m2v_model = distill(model_name=model_name, pca_dims=256)
|
|
164
|
+
# Distill a Sentence Transformer model, in this case the BAAI/bge-base-en-v1.5 model
|
|
165
|
+
m2v_model = distill(model_name="BAAI/bge-base-en-v1.5", pca_dims=256)
|
|
170
166
|
|
|
171
167
|
# Save the model
|
|
172
168
|
m2v_model.save_pretrained("m2v_model")
|
|
173
169
|
```
|
|
174
170
|
|
|
175
|
-
Distillation is really fast
|
|
171
|
+
Distillation is really fast and only takes 30 seconds on CPU. Best of all, distillation requires no training data.
|
|
176
172
|
|
|
177
|
-
|
|
178
|
-
```python
|
|
179
|
-
from sentence_transformers import SentenceTransformer
|
|
180
|
-
from sentence_transformers.models import StaticEmbedding
|
|
173
|
+
For advanced usage, such as using Model2Vec in the [Sentence Transformers library](https://github.com/UKPLab/sentence-transformers), please refer to the [Usage](#usage) sections.
|
|
181
174
|
|
|
182
|
-
# Initialize a StaticEmbedding module
|
|
183
|
-
static_embedding = StaticEmbedding.from_model2vec("minishlab/potion-base-8M")
|
|
184
|
-
model = SentenceTransformer(modules=[static_embedding])
|
|
185
|
-
embeddings = model.encode(["It's dangerous to go alone!", "It's a secret to everybody."])
|
|
186
|
-
```
|
|
187
|
-
|
|
188
|
-
Or you can distill a model directly into a Sentence Transformer model:
|
|
189
|
-
```python
|
|
190
|
-
from sentence_transformers import SentenceTransformer
|
|
191
|
-
from sentence_transformers.models import StaticEmbedding
|
|
192
|
-
|
|
193
|
-
static_embedding = StaticEmbedding.from_distillation("BAAI/bge-base-en-v1.5", device="cpu", pca_dims=256)
|
|
194
|
-
model = SentenceTransformer(modules=[static_embedding])
|
|
195
|
-
embeddings = model.encode(["It's dangerous to go alone!", "It's a secret to everybody."])
|
|
196
|
-
```
|
|
197
|
-
For more documentation, please refer to the [Sentence Transformers documentation](https://sbert.net/docs/package_reference/sentence_transformer/models.html#sentence_transformers.models.StaticEmbedding).
|
|
198
175
|
|
|
199
176
|
## Main Features
|
|
200
177
|
|
|
201
|
-
Model2Vec
|
|
202
|
-
|
|
203
|
-
- **
|
|
204
|
-
- **
|
|
205
|
-
- **Fast
|
|
206
|
-
- **
|
|
207
|
-
- **No data needed**: Distillation happens directly on the token level, so no dataset is needed.
|
|
208
|
-
- **Simple to use**: An easy to use interface for distilling and inferencing.
|
|
209
|
-
- **Integrated into Sentence Transformers**: Model2Vec can be used directly in [Sentence Transformers](https://github.com/UKPLab/sentence-transformers).
|
|
210
|
-
- **Bring your own model**: Can be applied to any Sentence Transformer model.
|
|
211
|
-
- **Bring your own vocabulary**: Can be applied to any vocabulary, allowing you to use your own domain-specific vocabulary. Need biomedical? Just get a medical dictionary, a biomedical model, and inference it.
|
|
212
|
-
- **Multi-lingual**: Use any language. Need a French model? [Pick one](https://huggingface.co/models?library=sentence-transformers&language=fr&sort=trending). Need multilingual? [Here you go](https://huggingface.co/sentence-transformers/LaBSE).
|
|
178
|
+
- **State-of-the-Art Performance**: Model2Vec models outperform any other static embeddings (such as GLoVe and BPEmb) by a large margin, as can be seen in our [results](results/README.md).
|
|
179
|
+
- **Small**: Model2Vec reduces the size of a Sentence Transformer model by a factor of 15, from 120M params, down to 7.5M (30 MB on disk, making it the smallest model on [MTEB](https://huggingface.co/spaces/mteb/leaderboard)!).
|
|
180
|
+
- **Lightweight Dependencies**: the base package's only major dependency is `numpy`.
|
|
181
|
+
- **Lightning-fast Inference**: up to 500 times faster on CPU than the original model. Go green or go home.
|
|
182
|
+
- **Fast, Dataset-free Distillation**: distill your own model in 30 seconds on a CPU, without a dataset. All you need is a model and (optionally) a custom vocabulary.
|
|
183
|
+
- **Integrated in many popular libraries**: Model2Vec can be used directly in popular libraries such as [Sentence Transformers](https://github.com/UKPLab/sentence-transformers), [LangChain](https://github.com/langchain-ai/langchain), [txtai](https://github.com/neuml/txtai), and [Chonkie](https://github.com/bhavnicksm/chonkie). See the [Integrations](#integrations) section for more information.
|
|
213
184
|
- **Tightly integrated with HuggingFace hub**: easily share and load models from the HuggingFace hub, using the familiar `from_pretrained` and `push_to_hub`. Our own models can be found [here](https://huggingface.co/minishlab). Feel free to share your own.
|
|
214
|
-
- **Easy Evaluation**: evaluate your models on MTEB and some of our own tasks to measure the performance of the distilled model. Model2Vec models work out of the box on [MTEB](https://huggingface.co/spaces/mteb/leaderboard).
|
|
215
185
|
|
|
216
186
|
## What is Model2Vec?
|
|
217
187
|
|
|
218
|
-
Model2vec creates a small, fast, and powerful model that outperforms other static embedding models by a large margin on all tasks we could find, while being much faster to create than traditional static embedding models such as GloVe. Like BPEmb, it can create subword embeddings, but with much better performance.
|
|
188
|
+
Model2vec creates a small, fast, and powerful model that outperforms other static embedding models by a large margin on all tasks we could find, while being much faster to create than traditional static embedding models such as GloVe. Like BPEmb, it can create subword embeddings, but with much better performance. Distillation doesn't need _any_ data, just a vocabulary and a model.
|
|
219
189
|
|
|
220
|
-
|
|
190
|
+
The base model2vec technique works by passing a vocabulary through a sentence transformer model, then reducing the dimensionality of the resulting embeddings using PCA, and finally weighting the embeddings using zipf weighting. During inference, we simply take the mean of all token embeddings occurring in a sentence.
|
|
221
191
|
|
|
222
|
-
|
|
223
|
-
- **
|
|
224
|
-
- **
|
|
225
|
-
- **
|
|
192
|
+
Our [potion models](https://huggingface.co/collections/minishlab/potion-6721e0abd4ea41881417f062) are pre-trained using [tokenlearn](https://github.com/MinishLab/tokenlearn), a technique to pre-train model2vec distillation models. These models are created with the following steps:
|
|
193
|
+
- **Distillation**: We distill a Model2Vec model from a Sentence Transformer model, using the method described above.
|
|
194
|
+
- **Sentence Transformer inference**: We use the Sentence Transformer model to create mean embeddings for a large number of texts from a corpus.
|
|
195
|
+
- **Training**: We train a model to minimize the cosine distance between the mean embeddings generated by the Sentence Transformer model and the mean embeddings generated by the Model2Vec model.
|
|
196
|
+
- **Post-training re-regularization**: We re-regularize the trained embeddings by first performing PCA, and then weighting the embeddings using `smooth inverse frequency (SIF)` weighting using the following formula: `w = 1e-3 / (1e-3 + proba)`. Here, `proba` is the probability of the token in the corpus we used for training.
|
|
226
197
|
|
|
227
|
-
For a technical deepdive into Model2Vec, please refer to our [blog post](https://huggingface.co/blog/Pringled/model2vec).
|
|
228
198
|
|
|
229
|
-
|
|
199
|
+
For a much more extensive deepdive, please refer to our [Model2Vec blog post](https://huggingface.co/blog/Pringled/model2vec) and our [Tokenlearn blog post](https://minishlab.github.io/tokenlearn_blogpost/).
|
|
230
200
|
|
|
231
|
-
|
|
201
|
+
## Usage
|
|
232
202
|
|
|
203
|
+
### Inference
|
|
233
204
|
|
|
234
|
-
|
|
205
|
+
<details>
|
|
206
|
+
<summary> Inference with a pretrained model </summary>
|
|
207
|
+
<br>
|
|
208
|
+
|
|
209
|
+
Inference works as follows. The example shows one of our own models, but you can also just load a local one, or another one from the hub.
|
|
210
|
+
```python
|
|
211
|
+
from model2vec import StaticModel
|
|
212
|
+
|
|
213
|
+
# Load a model from the Hub. You can optionally pass a token when loading a private model
|
|
214
|
+
model = StaticModel.from_pretrained(model_name="minishlab/potion-base-8M", token=None)
|
|
215
|
+
|
|
216
|
+
# Make embeddings
|
|
217
|
+
embeddings = model.encode(["It's dangerous to go alone!", "It's a secret to everybody."])
|
|
218
|
+
|
|
219
|
+
# Make sequences of token embeddings
|
|
220
|
+
token_embeddings = model.encode_as_sequence(["It's dangerous to go alone!", "It's a secret to everybody."])
|
|
221
|
+
```
|
|
222
|
+
</details>
|
|
235
223
|
|
|
236
224
|
|
|
225
|
+
<details>
|
|
226
|
+
<summary> Inference with the Sentence Transformers library </summary>
|
|
227
|
+
<br>
|
|
228
|
+
|
|
229
|
+
The following code snippet shows how to use a Model2Vec model in the [Sentence Transformers](https://github.com/UKPLab/sentence-transformers) library. This is useful if you want to use the model in a Sentence Transformers pipeline.
|
|
230
|
+
|
|
231
|
+
```python
|
|
232
|
+
from sentence_transformers import SentenceTransformer
|
|
233
|
+
from sentence_transformers.models import StaticEmbedding
|
|
234
|
+
|
|
235
|
+
# Initialize a StaticEmbedding module
|
|
236
|
+
static_embedding = StaticEmbedding.from_model2vec("minishlab/potion-base-8M")
|
|
237
|
+
model = SentenceTransformer(modules=[static_embedding])
|
|
238
|
+
embeddings = model.encode(["It's dangerous to go alone!", "It's a secret to everybody."])
|
|
239
|
+
```
|
|
240
|
+
|
|
241
|
+
</details>
|
|
242
|
+
|
|
237
243
|
### Distillation
|
|
238
244
|
|
|
239
245
|
<details>
|
|
@@ -244,11 +250,8 @@ The following code can be used to distill a model from a Sentence Transformer. A
|
|
|
244
250
|
```python
|
|
245
251
|
from model2vec.distill import distill
|
|
246
252
|
|
|
247
|
-
#
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
# Distill the model
|
|
251
|
-
m2v_model = distill(model_name=model_name, pca_dims=256)
|
|
253
|
+
# Distill a Sentence Transformer model
|
|
254
|
+
m2v_model = distill(model_name="BAAI/bge-base-en-v1.5", pca_dims=256)
|
|
252
255
|
|
|
253
256
|
# Save the model
|
|
254
257
|
m2v_model.save_pretrained("m2v_model")
|
|
@@ -308,11 +311,9 @@ from model2vec.distill import distill
|
|
|
308
311
|
|
|
309
312
|
# Load a vocabulary as a list of strings
|
|
310
313
|
vocabulary = ["word1", "word2", "word3"]
|
|
311
|
-
# Choose a Sentence Transformer model
|
|
312
|
-
model_name = "BAAI/bge-base-en-v1.5"
|
|
313
314
|
|
|
314
|
-
# Distill
|
|
315
|
-
m2v_model = distill(model_name=
|
|
315
|
+
# Distill a Sentence Transformer model with the custom vocabulary
|
|
316
|
+
m2v_model = distill(model_name="BAAI/bge-base-en-v1.5", vocabulary=vocabulary)
|
|
316
317
|
|
|
317
318
|
# Save the model
|
|
318
319
|
m2v_model.save_pretrained("m2v_model")
|
|
@@ -331,48 +332,6 @@ m2v_model = distill(model_name=model_name, vocabulary=vocabulary, use_subword=Fa
|
|
|
331
332
|
|
|
332
333
|
</details>
|
|
333
334
|
|
|
334
|
-
### Inference
|
|
335
|
-
|
|
336
|
-
<details>
|
|
337
|
-
<summary> Inference using pretrained model </summary>
|
|
338
|
-
<br>
|
|
339
|
-
|
|
340
|
-
Inference works as follows. The example shows one of our own models, but you can also just load a local one, or another one from the hub.
|
|
341
|
-
```python
|
|
342
|
-
from model2vec import StaticModel
|
|
343
|
-
|
|
344
|
-
# Load a model from the HuggingFace hub, or a local one.
|
|
345
|
-
model_name = "minishlab/potion-base-8M"
|
|
346
|
-
# You can optionally pass a token if you're loading a private model
|
|
347
|
-
model = StaticModel.from_pretrained(model_name, token=None)
|
|
348
|
-
|
|
349
|
-
# Make embeddings
|
|
350
|
-
embeddings = model.encode(["It's dangerous to go alone!", "It's a secret to everybody."])
|
|
351
|
-
|
|
352
|
-
# Make sequences of token embeddings
|
|
353
|
-
token_embeddings = model.encode_as_sequence(["It's dangerous to go alone!", "It's a secret to everybody."])
|
|
354
|
-
```
|
|
355
|
-
</details>
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
<details>
|
|
359
|
-
<summary> Inference using the Sentence Transformers library </summary>
|
|
360
|
-
<br>
|
|
361
|
-
|
|
362
|
-
The following code snippet shows how to use a Model2Vec model in the [Sentence Transformers](https://github.com/UKPLab/sentence-transformers) library. This is useful if you want to use the model in a Sentence Transformers pipeline.
|
|
363
|
-
|
|
364
|
-
```python
|
|
365
|
-
from sentence_transformers import SentenceTransformer
|
|
366
|
-
from sentence_transformers.models import StaticEmbedding
|
|
367
|
-
|
|
368
|
-
# Initialize a StaticEmbedding module
|
|
369
|
-
static_embedding = StaticEmbedding.from_model2vec("minishlab/potion-base-8M")
|
|
370
|
-
model = SentenceTransformer(modules=[static_embedding])
|
|
371
|
-
embeddings = model.encode(["It's dangerous to go alone!", "It's a secret to everybody."])
|
|
372
|
-
```
|
|
373
|
-
|
|
374
|
-
</details>
|
|
375
|
-
|
|
376
335
|
|
|
377
336
|
### Evaluation
|
|
378
337
|
|
|
@@ -425,9 +384,10 @@ print(make_leaderboard(task_scores))
|
|
|
425
384
|
```
|
|
426
385
|
</details>
|
|
427
386
|
|
|
428
|
-
|
|
387
|
+
## Integrations
|
|
429
388
|
<details>
|
|
430
389
|
<summary> Sentence Transformers </summary>
|
|
390
|
+
<a name="sentence-transformers"></a>
|
|
431
391
|
<br>
|
|
432
392
|
|
|
433
393
|
Model2Vec can be used directly in [Sentence Transformers](https://github.com/UKPLab/sentence-transformers) using the `StaticEmbedding` module.
|
|
@@ -454,12 +414,97 @@ model = SentenceTransformer(modules=[static_embedding])
|
|
|
454
414
|
embeddings = model.encode(["It's dangerous to go alone!", "It's a secret to everybody."])
|
|
455
415
|
```
|
|
456
416
|
|
|
417
|
+
For more documentation, please refer to the [Sentence Transformers documentation](https://sbert.net/docs/package_reference/sentence_transformer/models.html#sentence_transformers.models.StaticEmbedding).
|
|
418
|
+
|
|
457
419
|
</details>
|
|
458
420
|
|
|
421
|
+
<details>
|
|
422
|
+
<summary> LangChain </summary>
|
|
423
|
+
<br>
|
|
424
|
+
|
|
425
|
+
Model2Vec can be used in [LangChain](https://github.com/langchain-ai/langchain) using the `langchain-community` package. For more information, see the [LangChain Model2Vec docs](https://python.langchain.com/docs/integrations/text_embedding/model2vec/). The following code snippet shows how to use Model2Vec in LangChain after installing the `langchain-community` package with `pip install langchain-community`:
|
|
426
|
+
|
|
427
|
+
```python
|
|
428
|
+
from langchain_community.embeddings import Model2vecEmbeddings
|
|
429
|
+
from langchain_community.vectorstores import FAISS
|
|
430
|
+
from langchain.schema import Document
|
|
431
|
+
|
|
432
|
+
# Initialize a Model2Vec embedder
|
|
433
|
+
embedder = Model2vecEmbeddings("minishlab/potion-base-8M")
|
|
434
|
+
|
|
435
|
+
# Create some example texts
|
|
436
|
+
texts = [
|
|
437
|
+
"Enduring Stew",
|
|
438
|
+
"Hearty Elixir",
|
|
439
|
+
"Mighty Mushroom Risotto",
|
|
440
|
+
"Spicy Meat Skewer",
|
|
441
|
+
"Fruit Salad",
|
|
442
|
+
]
|
|
443
|
+
|
|
444
|
+
# Embed the texts
|
|
445
|
+
embeddings = embedder.embed_documents(texts)
|
|
446
|
+
|
|
447
|
+
# Or, create a vector store and query it
|
|
448
|
+
documents = [Document(page_content=text) for text in texts]
|
|
449
|
+
vector_store = FAISS.from_documents(documents, embedder)
|
|
450
|
+
query = "Risotto"
|
|
451
|
+
query_vector = embedder.embed_query(query)
|
|
452
|
+
retrieved_docs = vector_store.similarity_search_by_vector(query_vector, k=1)
|
|
453
|
+
```
|
|
454
|
+
</details>
|
|
459
455
|
|
|
460
456
|
<details>
|
|
461
|
-
<summary>
|
|
457
|
+
<summary> Txtai </summary>
|
|
458
|
+
<br>
|
|
459
|
+
|
|
460
|
+
Model2Vec can be used in [txtai](https://github.com/neuml/txtai) for text embeddings, nearest-neighbors search, and any of the other functionalities that txtai offers. The following code snippet shows how to use Model2Vec in txtai after installing the `txtai` package (including the `vectors` dependency) with `pip install txtai[vectors]`:
|
|
461
|
+
|
|
462
|
+
```python
|
|
463
|
+
from txtai import Embeddings
|
|
464
|
+
|
|
465
|
+
# Load a model2vec model
|
|
466
|
+
embeddings = Embeddings(path="minishlab/potion-base-8M", method="model2vec", backend="numpy")
|
|
467
|
+
|
|
468
|
+
# Create some example texts
|
|
469
|
+
texts = ["Enduring Stew", "Hearty Elixir", "Mighty Mushroom Risotto", "Spicy Meat Skewer", "Chilly Fruit Salad"]
|
|
470
|
+
|
|
471
|
+
# Create embeddings for downstream tasks
|
|
472
|
+
vectors = embeddings.batchtransform(texts)
|
|
473
|
+
|
|
474
|
+
# Or create a nearest-neighbors index and search it
|
|
475
|
+
embeddings.index(texts)
|
|
476
|
+
result = embeddings.search("Risotto", 1)
|
|
477
|
+
```
|
|
478
|
+
|
|
479
|
+
</details>
|
|
480
|
+
|
|
481
|
+
<details>
|
|
482
|
+
<summary> Chonkie </summary>
|
|
483
|
+
<br>
|
|
484
|
+
|
|
485
|
+
Model2Vec is the default model for semantic chunking in [Chonkie](https://github.com/bhavnicksm/chonkie). To use Model2Vec for semantic chunking in Chonkie, simply install Chonkie with `pip install chonkie[semantic]` and use one of the `potion` models in the `SemanticChunker` class. The following code snippet shows how to use Model2Vec in Chonkie:
|
|
486
|
+
|
|
487
|
+
```python
|
|
488
|
+
from chonkie import SDPMChunker
|
|
489
|
+
|
|
490
|
+
# Create some example text to chunk
|
|
491
|
+
text = "It's dangerous to go alone! Take this."
|
|
462
492
|
|
|
493
|
+
# Initialize the SemanticChunker with a potion model
|
|
494
|
+
chunker = SDPMChunker(
|
|
495
|
+
embedding_model="minishlab/potion-base-8M",
|
|
496
|
+
similarity_threshold=0.3
|
|
497
|
+
)
|
|
498
|
+
|
|
499
|
+
# Chunk the text
|
|
500
|
+
chunks = chunker.chunk(text)
|
|
501
|
+
```
|
|
502
|
+
|
|
503
|
+
</details>
|
|
504
|
+
|
|
505
|
+
|
|
506
|
+
<details>
|
|
507
|
+
<summary> Transformers.js </summary>
|
|
463
508
|
<br>
|
|
464
509
|
|
|
465
510
|
To use a Model2Vec model in [transformers.js](https://github.com/huggingface/transformers.js), the following code snippet can be used as a starting point:
|
|
@@ -513,12 +558,13 @@ python scripts/export_to_onnx.py --model_path <path-to-a-model2vec-model> --save
|
|
|
513
558
|
We provide a number of models that can be used out of the box. These models are available on the [HuggingFace hub](https://huggingface.co/collections/minishlab/model2vec-base-models-66fd9dd9b7c3b3c0f25ca90e) and can be loaded using the `from_pretrained` method. The models are listed below.
|
|
514
559
|
|
|
515
560
|
|
|
516
|
-
| Model | Language | Vocab | Sentence Transformer | Tokenizer Type | Params | Tokenlearn
|
|
517
|
-
|
|
518
|
-
| [potion-base-8M](https://huggingface.co/minishlab/potion-base-8M) | English | Output | [bge-base-en-v1.5](https://huggingface.co/BAAI/bge-base-en-v1.5) | Subword | 7.5M |
|
|
519
|
-
| [potion-base-4M](https://huggingface.co/minishlab/potion-base-4M) | English | Output | [bge-base-en-v1.5](https://huggingface.co/BAAI/bge-base-en-v1.5) | Subword | 3.7M
|
|
520
|
-
| [potion-base-2M](https://huggingface.co/minishlab/potion-base-2M) | English | Output | [bge-base-en-v1.5](https://huggingface.co/BAAI/bge-base-en-v1.5) | Subword | 1.8M
|
|
521
|
-
| [M2V_multilingual_output](https://huggingface.co/minishlab/M2V_multilingual_output) | Multilingual | Output | [LaBSE](https://huggingface.co/sentence-transformers/LaBSE) | Subword | 471M |
|
|
561
|
+
| Model | Language | Vocab | Sentence Transformer | Tokenizer Type | Params | Tokenlearn |
|
|
562
|
+
|-----------------------------------------------------------------------|-------------|------------------|-----------------------------------------------------------------|----------------|---------|-------------------|
|
|
563
|
+
| [potion-base-8M](https://huggingface.co/minishlab/potion-base-8M) | English | Output | [bge-base-en-v1.5](https://huggingface.co/BAAI/bge-base-en-v1.5) | Subword | 7.5M | <div align="center">✅</div> |
|
|
564
|
+
| [potion-base-4M](https://huggingface.co/minishlab/potion-base-4M) | English | Output | [bge-base-en-v1.5](https://huggingface.co/BAAI/bge-base-en-v1.5) | Subword | 3.7M | <div align="center">✅</div> |
|
|
565
|
+
| [potion-base-2M](https://huggingface.co/minishlab/potion-base-2M) | English | Output | [bge-base-en-v1.5](https://huggingface.co/BAAI/bge-base-en-v1.5) | Subword | 1.8M | <div align="center">✅</div> |
|
|
566
|
+
| [M2V_multilingual_output](https://huggingface.co/minishlab/M2V_multilingual_output) | Multilingual | Output | [LaBSE](https://huggingface.co/sentence-transformers/LaBSE) | Subword | 471M | <div align="center">❌</div> |
|
|
567
|
+
|
|
522
568
|
|
|
523
569
|
|
|
524
570
|
## Results
|
|
@@ -527,15 +573,6 @@ We have performed extensive experiments to evaluate the performance of Model2Vec
|
|
|
527
573
|
- [MTEB Results](results/README.md#mteb-results)
|
|
528
574
|
- [Ablations](results/README.md#ablations)
|
|
529
575
|
|
|
530
|
-
## Related work
|
|
531
|
-
|
|
532
|
-
If you are interested in fast small models, also consider looking at these techniques:
|
|
533
|
-
* [BPEmb](https://bpemb.h-its.org/): GLoVE embeddings trained on BPE-encoded Wikipedias. Huge inspiration to this project, multilingual, very fast. If you don't find a sentence transformer in the language you need, check this out.
|
|
534
|
-
* [fast-sentence-transformers](https://github.com/davidberenstein1957/fast-sentence-transformers): distillation using Model2Vec comes at a cost. If that cost is too steep for you, and you have access to a GPU, this package is for you. It automates the quantization and optimization of sentence transformers without loss of performance.
|
|
535
|
-
* [wordllama](https://github.com/dleemiller/WordLlama): Uses the _input_ embeddings of a LLama2 model and then performs contrastive learning on these embeddings. We think this is a bit overfit on MTEB, as the model is trained on MTEB datasets, and only evaluated on MTEB. Fun fact: this was invented at the same time as Model2Vec.
|
|
536
|
-
|
|
537
|
-
If you find other related work, please let us know.
|
|
538
|
-
|
|
539
576
|
## License
|
|
540
577
|
|
|
541
578
|
MIT
|
|
@@ -546,7 +583,7 @@ If you use Model2Vec in your research, please cite the following:
|
|
|
546
583
|
```bibtex
|
|
547
584
|
@software{minishlab2024model2vec,
|
|
548
585
|
authors = {Stephan Tulkens, Thomas van Dongen},
|
|
549
|
-
title = {Model2Vec:
|
|
586
|
+
title = {Model2Vec: The Fastest State-of-the-Art Static Embeddings in the World},
|
|
550
587
|
year = {2024},
|
|
551
588
|
url = {https://github.com/MinishLab/model2vec},
|
|
552
589
|
}
|