model2vec 0.3.8__tar.gz → 0.4.0__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.
Files changed (73) hide show
  1. {model2vec-0.3.8 → model2vec-0.4.0}/Makefile +1 -1
  2. model2vec-0.4.0/PKG-INFO +271 -0
  3. model2vec-0.4.0/README.md +194 -0
  4. model2vec-0.4.0/assets/images/speed_vs_mteb_score_v3.png +0 -0
  5. model2vec-0.4.0/docs/README.md +6 -0
  6. model2vec-0.4.0/docs/integrations.md +155 -0
  7. model2vec-0.4.0/docs/usage.md +202 -0
  8. model2vec-0.4.0/docs/what_is_model2vec.md +11 -0
  9. {model2vec-0.3.8 → model2vec-0.4.0}/model2vec/distill/distillation.py +93 -29
  10. {model2vec-0.3.8 → model2vec-0.4.0}/model2vec/hf_utils.py +4 -2
  11. model2vec-0.4.0/model2vec/inference/README.md +18 -0
  12. model2vec-0.4.0/model2vec/inference/__init__.py +10 -0
  13. model2vec-0.4.0/model2vec/inference/model.py +202 -0
  14. {model2vec-0.3.8 → model2vec-0.4.0}/model2vec/model.py +1 -1
  15. model2vec-0.4.0/model2vec/modelcards/classifier_template.md +49 -0
  16. {model2vec-0.3.8/model2vec → model2vec-0.4.0/model2vec/modelcards}/model_card_template.md +1 -1
  17. model2vec-0.4.0/model2vec/train/README.md +108 -0
  18. model2vec-0.4.0/model2vec/train/__init__.py +10 -0
  19. model2vec-0.4.0/model2vec/train/base.py +160 -0
  20. model2vec-0.4.0/model2vec/train/classifier.py +297 -0
  21. {model2vec-0.3.8 → model2vec-0.4.0}/model2vec/utils.py +1 -1
  22. {model2vec-0.3.8 → model2vec-0.4.0}/model2vec/version.py +1 -1
  23. model2vec-0.4.0/model2vec.egg-info/PKG-INFO +271 -0
  24. {model2vec-0.3.8 → model2vec-0.4.0}/model2vec.egg-info/SOURCES.txt +19 -2
  25. {model2vec-0.3.8 → model2vec-0.4.0}/model2vec.egg-info/requires.txt +10 -0
  26. {model2vec-0.3.8 → model2vec-0.4.0}/pyproject.toml +6 -2
  27. model2vec-0.4.0/results/README.md +132 -0
  28. model2vec-0.4.0/results/make_speed_vs_mteb_plot.py +184 -0
  29. {model2vec-0.3.8 → model2vec-0.4.0}/tests/conftest.py +27 -3
  30. {model2vec-0.3.8 → model2vec-0.4.0}/tests/test_distillation.py +49 -19
  31. model2vec-0.4.0/tests/test_inference.py +50 -0
  32. model2vec-0.4.0/tests/test_trainable.py +145 -0
  33. {model2vec-0.3.8 → model2vec-0.4.0}/tutorials/README.md +1 -0
  34. model2vec-0.4.0/tutorials/train_classifier.ipynb +806 -0
  35. {model2vec-0.3.8 → model2vec-0.4.0}/uv.lock +615 -15
  36. model2vec-0.3.8/PKG-INFO +0 -590
  37. model2vec-0.3.8/README.md +0 -521
  38. model2vec-0.3.8/model2vec.egg-info/PKG-INFO +0 -590
  39. model2vec-0.3.8/results/README.md +0 -67
  40. {model2vec-0.3.8 → model2vec-0.4.0}/.github/workflows/ci.yaml +0 -0
  41. {model2vec-0.3.8 → model2vec-0.4.0}/.gitignore +0 -0
  42. {model2vec-0.3.8 → model2vec-0.4.0}/.pre-commit-config.yaml +0 -0
  43. {model2vec-0.3.8 → model2vec-0.4.0}/LICENSE +0 -0
  44. {model2vec-0.3.8 → model2vec-0.4.0}/assets/images/logo.png +0 -0
  45. {model2vec-0.3.8 → model2vec-0.4.0}/assets/images/logo_v2.png +0 -0
  46. {model2vec-0.3.8 → model2vec-0.4.0}/assets/images/model2vec_model_diagram.png +0 -0
  47. {model2vec-0.3.8 → model2vec-0.4.0}/assets/images/model2vec_model_diagram_transparant_dark.png +0 -0
  48. {model2vec-0.3.8 → model2vec-0.4.0}/assets/images/model2vec_model_diagram_transparant_light.png +0 -0
  49. {model2vec-0.3.8 → model2vec-0.4.0}/assets/images/sentences_per_second_vs_average_score.png +0 -0
  50. {model2vec-0.3.8 → model2vec-0.4.0}/assets/images/speed_vs_accuracy.png +0 -0
  51. {model2vec-0.3.8 → model2vec-0.4.0}/assets/images/speed_vs_accuracy_v2.png +0 -0
  52. {model2vec-0.3.8 → model2vec-0.4.0}/assets/images/speed_vs_accuracy_v3.png +0 -0
  53. {model2vec-0.3.8 → model2vec-0.4.0}/assets/images/speed_vs_accuracy_v4.png +0 -0
  54. {model2vec-0.3.8 → model2vec-0.4.0}/assets/images/speed_vs_mteb_score.png +0 -0
  55. {model2vec-0.3.8 → model2vec-0.4.0}/assets/images/speed_vs_mteb_score_v2.png +0 -0
  56. {model2vec-0.3.8 → model2vec-0.4.0}/assets/images/tutorial_ezlo.png +0 -0
  57. {model2vec-0.3.8 → model2vec-0.4.0}/model2vec/__init__.py +0 -0
  58. {model2vec-0.3.8 → model2vec-0.4.0}/model2vec/distill/__init__.py +0 -0
  59. {model2vec-0.3.8 → model2vec-0.4.0}/model2vec/distill/inference.py +0 -0
  60. {model2vec-0.3.8 → model2vec-0.4.0}/model2vec/distill/tokenizer.py +0 -0
  61. {model2vec-0.3.8 → model2vec-0.4.0}/model2vec/distill/utils.py +0 -0
  62. {model2vec-0.3.8 → model2vec-0.4.0}/model2vec.egg-info/dependency_links.txt +0 -0
  63. {model2vec-0.3.8 → model2vec-0.4.0}/model2vec.egg-info/top_level.txt +0 -0
  64. {model2vec-0.3.8 → model2vec-0.4.0}/scripts/export_to_onnx.py +0 -0
  65. {model2vec-0.3.8 → model2vec-0.4.0}/setup.cfg +0 -0
  66. {model2vec-0.3.8 → model2vec-0.4.0}/tests/__init__.py +0 -0
  67. {model2vec-0.3.8 → model2vec-0.4.0}/tests/data/test_tokenizer/special_tokens_map.json +0 -0
  68. {model2vec-0.3.8 → model2vec-0.4.0}/tests/data/test_tokenizer/tokenizer.json +0 -0
  69. {model2vec-0.3.8 → model2vec-0.4.0}/tests/data/test_tokenizer/tokenizer_config.json +0 -0
  70. {model2vec-0.3.8 → model2vec-0.4.0}/tests/test_model.py +0 -0
  71. {model2vec-0.3.8 → model2vec-0.4.0}/tests/test_utils.py +0 -0
  72. {model2vec-0.3.8 → model2vec-0.4.0}/tutorials/recipe_search.ipynb +0 -0
  73. {model2vec-0.3.8 → model2vec-0.4.0}/tutorials/semantic_chunking.ipynb +0 -0
@@ -9,7 +9,7 @@ install:
9
9
  uv run pre-commit install
10
10
 
11
11
  install-no-pre-commit:
12
- uv pip install ".[dev,distill]"
12
+ uv pip install ".[dev,distill,inference,train]"
13
13
  uv pip install "torch<2.5.0"
14
14
 
15
15
  install-base:
@@ -0,0 +1,271 @@
1
+ Metadata-Version: 2.2
2
+ Name: model2vec
3
+ Version: 0.4.0
4
+ Summary: Fast State-of-the-Art Static Embeddings
5
+ Author-email: Stéphan Tulkens <stephantul@gmail.com>, Thomas van Dongen <thomas123@live.nl>
6
+ License: MIT License
7
+
8
+ Copyright (c) 2024 Thomas van Dongen
9
+
10
+ Permission is hereby granted, free of charge, to any person obtaining a copy
11
+ of this software and associated documentation files (the "Software"), to deal
12
+ in the Software without restriction, including without limitation the rights
13
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
14
+ copies of the Software, and to permit persons to whom the Software is
15
+ furnished to do so, subject to the following conditions:
16
+
17
+ The above copyright notice and this permission notice shall be included in all
18
+ copies or substantial portions of the Software.
19
+
20
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
21
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
22
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
23
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
24
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
25
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
26
+ SOFTWARE.
27
+
28
+ Project-URL: Homepage, https://github.com/MinishLab
29
+ Project-URL: Bug Reports, https://github.com/MinishLab/model2vec/issues
30
+ Project-URL: Source, https://github.com/MinishLab/model2vec
31
+ Classifier: Development Status :: 4 - Beta
32
+ Classifier: Intended Audience :: Developers
33
+ Classifier: Intended Audience :: Science/Research
34
+ Classifier: Topic :: Scientific/Engineering :: Artificial Intelligence
35
+ Classifier: Topic :: Software Development :: Libraries
36
+ Classifier: License :: OSI Approved :: MIT License
37
+ Classifier: Programming Language :: Python :: 3 :: Only
38
+ Classifier: Programming Language :: Python :: 3.9
39
+ Classifier: Programming Language :: Python :: 3.10
40
+ Classifier: Programming Language :: Python :: 3.11
41
+ Classifier: Programming Language :: Python :: 3.12
42
+ Classifier: Natural Language :: English
43
+ Requires-Python: >=3.9
44
+ Description-Content-Type: text/markdown
45
+ License-File: LICENSE
46
+ Requires-Dist: jinja2
47
+ Requires-Dist: joblib
48
+ Requires-Dist: numpy
49
+ Requires-Dist: rich
50
+ Requires-Dist: safetensors
51
+ Requires-Dist: setuptools
52
+ Requires-Dist: tokenizers>=0.20
53
+ Requires-Dist: tqdm
54
+ Provides-Extra: dev
55
+ Requires-Dist: black; extra == "dev"
56
+ Requires-Dist: ipython; extra == "dev"
57
+ Requires-Dist: mypy; extra == "dev"
58
+ Requires-Dist: pre-commit; extra == "dev"
59
+ Requires-Dist: pytest; extra == "dev"
60
+ Requires-Dist: pytest-cov; extra == "dev"
61
+ Requires-Dist: ruff; extra == "dev"
62
+ Provides-Extra: distill
63
+ Requires-Dist: torch; extra == "distill"
64
+ Requires-Dist: transformers; extra == "distill"
65
+ Requires-Dist: scikit-learn; extra == "distill"
66
+ Provides-Extra: onnx
67
+ Requires-Dist: onnx; extra == "onnx"
68
+ Requires-Dist: torch; extra == "onnx"
69
+ Provides-Extra: train
70
+ Requires-Dist: torch; extra == "train"
71
+ Requires-Dist: lightning; extra == "train"
72
+ Requires-Dist: scikit-learn; extra == "train"
73
+ Requires-Dist: skops; extra == "train"
74
+ Provides-Extra: inference
75
+ Requires-Dist: scikit-learn; extra == "inference"
76
+ Requires-Dist: skops; extra == "inference"
77
+
78
+
79
+ <div align="center">
80
+ <picture>
81
+ <img width="35%" alt="Model2Vec logo" src="assets/images/logo_v2.png">
82
+ </picture>
83
+ </a>
84
+ </div>
85
+
86
+ <div align="center">
87
+ <h2>Fast State-of-the-Art Static Embeddings</h2>
88
+ </div>
89
+
90
+ <div align="center">
91
+ <h2>
92
+ <a href="https://huggingface.co/minishlab"><strong>🤗 Models</strong></a> |
93
+ <a href="https://github.com/MinishLab/model2vec/tree/main/tutorials"><strong>📚 Tutorials</strong></a> |
94
+ <a href="https://minishlab.github.io/"><strong>🌐 Blog</strong></a> |
95
+ <a href="https://github.com/MinishLab/model2vec/blob/main/results/README.md"><strong>🏆 Results</strong></a> |
96
+ <a href="https://github.com/MinishLab/model2vec/blob/main/docs"><strong>📖 Docs</strong></a>
97
+ </h2>
98
+ </div>
99
+
100
+ <div align="center">
101
+ <h2>
102
+ <a href="https://pypi.org/project/model2vec/"><img src="https://img.shields.io/pypi/v/model2vec?color=%23007ec6&label=pypi%20package" alt="Package version"></a>
103
+ <a href="https://pypi.org/project/model2vec/"><img src="https://img.shields.io/pypi/pyversions/model2vec" alt="Supported Python versions"></a>
104
+ <a href="https://pepy.tech/project/model2vec">
105
+ <img src="https://static.pepy.tech/badge/model2vec" alt="Downloads">
106
+ </a>
107
+ <a href="https://app.codecov.io/gh/MinishLab/model2vec">
108
+ <img src="https://codecov.io/gh/MinishLab/model2vec/graph/badge.svg?token=21TWJ6B5ET" alt="Codecov">
109
+ </a>
110
+ </a>
111
+ <a href="https://github.com/MinishLab/model2vec/blob/main/LICENSE"><img src="https://img.shields.io/badge/license-MIT-green" alt="License - MIT"></a>
112
+
113
+ </h2>
114
+ </div>
115
+
116
+
117
+
118
+
119
+ Model2Vec is a technique to turn any sentence transformer into a really small static model, reducing model size by a factor up to 50 and making the models up to 500 times faster, with a small drop in performance. Our [best model](https://huggingface.co/minishlab/potion-base-8M) is the most performant static embedding model in the world. See our results [here](results/README.md), or dive in to see how it works.
120
+
121
+ <div align="center">
122
+ <h3>
123
+
124
+ [Quickstart](#quickstart) • [Updates & Announcements](#updates--announcements) • [Main Features](#main-features) • [Model List](#model-list)
125
+ </h3>
126
+ </div>
127
+
128
+ ## Quickstart
129
+
130
+ Install the lightweight base package with:
131
+
132
+ ```bash
133
+ pip install model2vec
134
+ ```
135
+
136
+ You can start using Model2Vec by loading 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, which you can use for any task, such as text classification, retrieval, clustering, or building a RAG system:
137
+ ```python
138
+ from model2vec import StaticModel
139
+
140
+ # Load a model from the HuggingFace hub (in this case the potion-base-8M model)
141
+ model = StaticModel.from_pretrained("minishlab/potion-base-8M")
142
+
143
+ # Make embeddings
144
+ embeddings = model.encode(["It's dangerous to go alone!", "It's a secret to everybody."])
145
+
146
+ # Make sequences of token embeddings
147
+ token_embeddings = model.encode_as_sequence(["It's dangerous to go alone!", "It's a secret to everybody."])
148
+ ```
149
+
150
+ Instead of using one of our models, you can also distill your own Model2Vec model from a Sentence Transformer model. First, install the `distillation` extras with:
151
+
152
+ ```bash
153
+ pip install model2vec[distill]
154
+ ```
155
+
156
+
157
+ Then, you can distill a model in ~30 seconds on a CPU with the following code snippet:
158
+
159
+ ```python
160
+ from model2vec.distill import distill
161
+
162
+ # Distill a Sentence Transformer model, in this case the BAAI/bge-base-en-v1.5 model
163
+ m2v_model = distill(model_name="BAAI/bge-base-en-v1.5", pca_dims=256)
164
+
165
+ # Save the model
166
+ m2v_model.save_pretrained("m2v_model")
167
+ ```
168
+
169
+ After distillation, you can also fine-tune your own classification models on top of the distilled model, or on a pre-trained model. First, make sure you install the `training` extras with:
170
+
171
+ ```bash
172
+ pip install model2vec[training]
173
+ ```
174
+
175
+ Then, you can fine-tune a model as follows:
176
+
177
+ ```python
178
+ import numpy as np
179
+ from datasets import load_dataset
180
+ from model2vec.train import StaticModelForClassification
181
+
182
+ # Initialize a classifier from a pre-trained model
183
+ classifier = StaticModelForClassification.from_pretrained(model_name="minishlab/potion-base-32M")
184
+
185
+ # Load a dataset
186
+ ds = load_dataset("setfit/subj")
187
+
188
+ # Train the classifier on text (X) and labels (y)
189
+ classifier.fit(ds["train"]["text"], ds["train"]["label"])
190
+
191
+ # Evaluate the classifier
192
+ predictions = classifier.predict(ds["test"]["text"])
193
+ accuracy = np.mean(np.array(predictions) == np.array(ds["test"]["label"])) * 100
194
+ ```
195
+
196
+ For advanced usage, please refer to our [usage documentation](https://github.com/MinishLab/model2vec/blob/main/docs/usage.md).
197
+
198
+ ## Updates & Announcements
199
+
200
+ - **12/02/2024**: We released **Model2Vec training**, allowing you to fine-tune your own classification models on top of Model2Vec models. Find out more in our [training documentation](https://github.com/MinishLab/model2vec/blob/main/model2vec/train/README.md) and [results](results/README.md#training-results).
201
+
202
+ - **30/01/2024**: We released two new models: [potion-base-32M](https://huggingface.co/minishlab/potion-base-32M) and [potion-retrieval-32M](https://huggingface.co/minishlab/potion-retrieval-32M). [potion-base-32M](https://huggingface.co/minishlab/potion-base-32M) is our most performant model to date, using a larger vocabulary and higher dimensions. [potion-retrieval-32M](https://huggingface.co/minishlab/potion-retrieval-32M) is a finetune of [potion-base-32M](https://huggingface.co/minishlab/potion-base-32M) that is optimized for retrieval tasks, and is the best performing static retrieval model currently available.
203
+
204
+ - **30/10/2024**: We released three new models: [potion-base-8M](https://huggingface.co/minishlab/potion-base-8M), [potion-base-4M](https://huggingface.co/minishlab/potion-base-4M), and [potion-base-2M](https://huggingface.co/minishlab/potion-base-2M). These models are trained using [Tokenlearn](https://github.com/MinishLab/tokenlearn). Find out more in our [blog post](https://minishlab.github.io/tokenlearn_blogpost/). NOTE: for users of any of our old English M2V models, we recommend switching to these new models as they [perform better on all tasks](https://github.com/MinishLab/model2vec/tree/main/results).
205
+
206
+ ## Main Features
207
+
208
+ - **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).
209
+ - **Small**: Model2Vec reduces the size of a Sentence Transformer model by a factor of up to 50. Our [best model](https://huggingface.co/minishlab/potion-base-8M) is just ~30 MB on disk, and our smallest model just ~8 MB (making it the smallest model on [MTEB](https://huggingface.co/spaces/mteb/leaderboard)!).
210
+ - **Lightweight Dependencies**: the base package's only major dependency is `numpy`.
211
+ - **Lightning-fast Inference**: up to 500 times faster on CPU than the original model.
212
+ - **Fast, Dataset-free Distillation**: distill your own model in 30 seconds on a CPU, without a dataset.
213
+ - **Fine-tuning**: fine-tune your own classification models on top of Model2Vec models.
214
+ - **Integrated in many popular libraries**: Model2Vec is integrated direclty into popular libraries such as [Sentence Transformers](https://github.com/UKPLab/sentence-transformers) and [LangChain](https://github.com/langchain-ai/langchain). For more information, see our [integrations documentation](https://github.com/MinishLab/model2vec/blob/main/docs/integrations.md).
215
+ - **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).
216
+
217
+ ## What is Model2Vec?
218
+
219
+ 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.
220
+
221
+ The core idea is to forward pass a vocabulary through a sentence transformer model, creating static embeddings for the indiviudal tokens. After this, there are a number of post-processing steps we do that results in our best models. For a more extensive deepdive, please refer to the following resources:
222
+ - Our initial [Model2Vec blog post](https://huggingface.co/blog/Pringled/model2vec). Note that, while this post gives a good overview of the core idea, we've made a number of substantial improvements since then.
223
+ - Our [Tokenlearn blog post](https://minishlab.github.io/tokenlearn_blogpost/). This post describes the Tokenlearn method we used to train our [potion models](https://huggingface.co/collections/minishlab/potion-6721e0abd4ea41881417f062).
224
+ - Our official [documentation](https://github.com/MinishLab/model2vec/blob/main/docs/what_is_model2vec.md). This document provides a high-level overview of how Model2Vec works.
225
+
226
+ ## Documentation
227
+
228
+ Our official documentation can be found [here](https://github.com/MinishLab/model2vec/blob/main/docs/README.md). This includes:
229
+ - [Usage documentation](https://github.com/MinishLab/model2vec/blob/main/docs/usage.md): provides a technical overview of how to use Model2Vec.
230
+ - [Integrations documentation](https://github.com/MinishLab/model2vec/blob/main/docs/integrations.md): provides examples of how to use Model2Vec in various downstream libraries.
231
+ - [Model2Vec technical documentation](https://github.com/MinishLab/model2vec/blob/main/docs/what_is_model2vec.md): provides a high-level overview of how Model2Vec works.
232
+
233
+
234
+ ## Model List
235
+
236
+ 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.
237
+
238
+
239
+
240
+ | Model | Language | Sentence Transformer | Params | Task |
241
+ |-----------------------------------------------------------------------|------------|-----------------------------------------------------------------|---------|-----------|
242
+ | [potion-base-32M](https://huggingface.co/minishlab/potion-base-32M) | English | [bge-base-en-v1.5](https://huggingface.co/BAAI/bge-base-en-v1.5) | 32.3M | General |
243
+ | [potion-base-8M](https://huggingface.co/minishlab/potion-base-8M) | English | [bge-base-en-v1.5](https://huggingface.co/BAAI/bge-base-en-v1.5) | 7.5M | General |
244
+ | [potion-base-4M](https://huggingface.co/minishlab/potion-base-4M) | English | [bge-base-en-v1.5](https://huggingface.co/BAAI/bge-base-en-v1.5) | 3.7M | General |
245
+ | [potion-base-2M](https://huggingface.co/minishlab/potion-base-2M) | English | [bge-base-en-v1.5](https://huggingface.co/BAAI/bge-base-en-v1.5) | 1.8M | General |
246
+ | [potion-retrieval-32M](https://huggingface.co/minishlab/potion-retrieval-32M) | English | [bge-base-en-v1.5](https://huggingface.co/BAAI/bge-base-en-v1.5) | 32.3M | Retrieval |
247
+ | [M2V_multilingual_output](https://huggingface.co/minishlab/M2V_multilingual_output) | Multilingual | [LaBSE](https://huggingface.co/sentence-transformers/LaBSE) | 471M | General |
248
+
249
+
250
+ ## Results
251
+
252
+ We have performed extensive experiments to evaluate the performance of Model2Vec models. The results are documented in the [results](results/README.md) folder. The results are presented in the following sections:
253
+ - [MTEB Results](results/README.md#mteb-results)
254
+ - [Training Results](results/README.md#training-results)
255
+ - [Ablations](results/README.md#ablations)
256
+
257
+ ## License
258
+
259
+ MIT
260
+
261
+ ## Citing
262
+
263
+ If you use Model2Vec in your research, please cite the following:
264
+ ```bibtex
265
+ @software{minishlab2024model2vec,
266
+ authors = {Stephan Tulkens and Thomas van Dongen},
267
+ title = {Model2Vec: Fast State-of-the-Art Static Embeddings},
268
+ year = {2024},
269
+ url = {https://github.com/MinishLab/model2vec}
270
+ }
271
+ ```
@@ -0,0 +1,194 @@
1
+
2
+ <div align="center">
3
+ <picture>
4
+ <img width="35%" alt="Model2Vec logo" src="assets/images/logo_v2.png">
5
+ </picture>
6
+ </a>
7
+ </div>
8
+
9
+ <div align="center">
10
+ <h2>Fast State-of-the-Art Static Embeddings</h2>
11
+ </div>
12
+
13
+ <div align="center">
14
+ <h2>
15
+ <a href="https://huggingface.co/minishlab"><strong>🤗 Models</strong></a> |
16
+ <a href="https://github.com/MinishLab/model2vec/tree/main/tutorials"><strong>📚 Tutorials</strong></a> |
17
+ <a href="https://minishlab.github.io/"><strong>🌐 Blog</strong></a> |
18
+ <a href="https://github.com/MinishLab/model2vec/blob/main/results/README.md"><strong>🏆 Results</strong></a> |
19
+ <a href="https://github.com/MinishLab/model2vec/blob/main/docs"><strong>📖 Docs</strong></a>
20
+ </h2>
21
+ </div>
22
+
23
+ <div align="center">
24
+ <h2>
25
+ <a href="https://pypi.org/project/model2vec/"><img src="https://img.shields.io/pypi/v/model2vec?color=%23007ec6&label=pypi%20package" alt="Package version"></a>
26
+ <a href="https://pypi.org/project/model2vec/"><img src="https://img.shields.io/pypi/pyversions/model2vec" alt="Supported Python versions"></a>
27
+ <a href="https://pepy.tech/project/model2vec">
28
+ <img src="https://static.pepy.tech/badge/model2vec" alt="Downloads">
29
+ </a>
30
+ <a href="https://app.codecov.io/gh/MinishLab/model2vec">
31
+ <img src="https://codecov.io/gh/MinishLab/model2vec/graph/badge.svg?token=21TWJ6B5ET" alt="Codecov">
32
+ </a>
33
+ </a>
34
+ <a href="https://github.com/MinishLab/model2vec/blob/main/LICENSE"><img src="https://img.shields.io/badge/license-MIT-green" alt="License - MIT"></a>
35
+
36
+ </h2>
37
+ </div>
38
+
39
+
40
+
41
+
42
+ Model2Vec is a technique to turn any sentence transformer into a really small static model, reducing model size by a factor up to 50 and making the models up to 500 times faster, with a small drop in performance. Our [best model](https://huggingface.co/minishlab/potion-base-8M) is the most performant static embedding model in the world. See our results [here](results/README.md), or dive in to see how it works.
43
+
44
+ <div align="center">
45
+ <h3>
46
+
47
+ [Quickstart](#quickstart) • [Updates & Announcements](#updates--announcements) • [Main Features](#main-features) • [Model List](#model-list)
48
+ </h3>
49
+ </div>
50
+
51
+ ## Quickstart
52
+
53
+ Install the lightweight base package with:
54
+
55
+ ```bash
56
+ pip install model2vec
57
+ ```
58
+
59
+ You can start using Model2Vec by loading 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, which you can use for any task, such as text classification, retrieval, clustering, or building a RAG system:
60
+ ```python
61
+ from model2vec import StaticModel
62
+
63
+ # Load a model from the HuggingFace hub (in this case the potion-base-8M model)
64
+ model = StaticModel.from_pretrained("minishlab/potion-base-8M")
65
+
66
+ # Make embeddings
67
+ embeddings = model.encode(["It's dangerous to go alone!", "It's a secret to everybody."])
68
+
69
+ # Make sequences of token embeddings
70
+ token_embeddings = model.encode_as_sequence(["It's dangerous to go alone!", "It's a secret to everybody."])
71
+ ```
72
+
73
+ Instead of using one of our models, you can also distill your own Model2Vec model from a Sentence Transformer model. First, install the `distillation` extras with:
74
+
75
+ ```bash
76
+ pip install model2vec[distill]
77
+ ```
78
+
79
+
80
+ Then, you can distill a model in ~30 seconds on a CPU with the following code snippet:
81
+
82
+ ```python
83
+ from model2vec.distill import distill
84
+
85
+ # Distill a Sentence Transformer model, in this case the BAAI/bge-base-en-v1.5 model
86
+ m2v_model = distill(model_name="BAAI/bge-base-en-v1.5", pca_dims=256)
87
+
88
+ # Save the model
89
+ m2v_model.save_pretrained("m2v_model")
90
+ ```
91
+
92
+ After distillation, you can also fine-tune your own classification models on top of the distilled model, or on a pre-trained model. First, make sure you install the `training` extras with:
93
+
94
+ ```bash
95
+ pip install model2vec[training]
96
+ ```
97
+
98
+ Then, you can fine-tune a model as follows:
99
+
100
+ ```python
101
+ import numpy as np
102
+ from datasets import load_dataset
103
+ from model2vec.train import StaticModelForClassification
104
+
105
+ # Initialize a classifier from a pre-trained model
106
+ classifier = StaticModelForClassification.from_pretrained(model_name="minishlab/potion-base-32M")
107
+
108
+ # Load a dataset
109
+ ds = load_dataset("setfit/subj")
110
+
111
+ # Train the classifier on text (X) and labels (y)
112
+ classifier.fit(ds["train"]["text"], ds["train"]["label"])
113
+
114
+ # Evaluate the classifier
115
+ predictions = classifier.predict(ds["test"]["text"])
116
+ accuracy = np.mean(np.array(predictions) == np.array(ds["test"]["label"])) * 100
117
+ ```
118
+
119
+ For advanced usage, please refer to our [usage documentation](https://github.com/MinishLab/model2vec/blob/main/docs/usage.md).
120
+
121
+ ## Updates & Announcements
122
+
123
+ - **12/02/2024**: We released **Model2Vec training**, allowing you to fine-tune your own classification models on top of Model2Vec models. Find out more in our [training documentation](https://github.com/MinishLab/model2vec/blob/main/model2vec/train/README.md) and [results](results/README.md#training-results).
124
+
125
+ - **30/01/2024**: We released two new models: [potion-base-32M](https://huggingface.co/minishlab/potion-base-32M) and [potion-retrieval-32M](https://huggingface.co/minishlab/potion-retrieval-32M). [potion-base-32M](https://huggingface.co/minishlab/potion-base-32M) is our most performant model to date, using a larger vocabulary and higher dimensions. [potion-retrieval-32M](https://huggingface.co/minishlab/potion-retrieval-32M) is a finetune of [potion-base-32M](https://huggingface.co/minishlab/potion-base-32M) that is optimized for retrieval tasks, and is the best performing static retrieval model currently available.
126
+
127
+ - **30/10/2024**: We released three new models: [potion-base-8M](https://huggingface.co/minishlab/potion-base-8M), [potion-base-4M](https://huggingface.co/minishlab/potion-base-4M), and [potion-base-2M](https://huggingface.co/minishlab/potion-base-2M). These models are trained using [Tokenlearn](https://github.com/MinishLab/tokenlearn). Find out more in our [blog post](https://minishlab.github.io/tokenlearn_blogpost/). NOTE: for users of any of our old English M2V models, we recommend switching to these new models as they [perform better on all tasks](https://github.com/MinishLab/model2vec/tree/main/results).
128
+
129
+ ## Main Features
130
+
131
+ - **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).
132
+ - **Small**: Model2Vec reduces the size of a Sentence Transformer model by a factor of up to 50. Our [best model](https://huggingface.co/minishlab/potion-base-8M) is just ~30 MB on disk, and our smallest model just ~8 MB (making it the smallest model on [MTEB](https://huggingface.co/spaces/mteb/leaderboard)!).
133
+ - **Lightweight Dependencies**: the base package's only major dependency is `numpy`.
134
+ - **Lightning-fast Inference**: up to 500 times faster on CPU than the original model.
135
+ - **Fast, Dataset-free Distillation**: distill your own model in 30 seconds on a CPU, without a dataset.
136
+ - **Fine-tuning**: fine-tune your own classification models on top of Model2Vec models.
137
+ - **Integrated in many popular libraries**: Model2Vec is integrated direclty into popular libraries such as [Sentence Transformers](https://github.com/UKPLab/sentence-transformers) and [LangChain](https://github.com/langchain-ai/langchain). For more information, see our [integrations documentation](https://github.com/MinishLab/model2vec/blob/main/docs/integrations.md).
138
+ - **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).
139
+
140
+ ## What is Model2Vec?
141
+
142
+ 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.
143
+
144
+ The core idea is to forward pass a vocabulary through a sentence transformer model, creating static embeddings for the indiviudal tokens. After this, there are a number of post-processing steps we do that results in our best models. For a more extensive deepdive, please refer to the following resources:
145
+ - Our initial [Model2Vec blog post](https://huggingface.co/blog/Pringled/model2vec). Note that, while this post gives a good overview of the core idea, we've made a number of substantial improvements since then.
146
+ - Our [Tokenlearn blog post](https://minishlab.github.io/tokenlearn_blogpost/). This post describes the Tokenlearn method we used to train our [potion models](https://huggingface.co/collections/minishlab/potion-6721e0abd4ea41881417f062).
147
+ - Our official [documentation](https://github.com/MinishLab/model2vec/blob/main/docs/what_is_model2vec.md). This document provides a high-level overview of how Model2Vec works.
148
+
149
+ ## Documentation
150
+
151
+ Our official documentation can be found [here](https://github.com/MinishLab/model2vec/blob/main/docs/README.md). This includes:
152
+ - [Usage documentation](https://github.com/MinishLab/model2vec/blob/main/docs/usage.md): provides a technical overview of how to use Model2Vec.
153
+ - [Integrations documentation](https://github.com/MinishLab/model2vec/blob/main/docs/integrations.md): provides examples of how to use Model2Vec in various downstream libraries.
154
+ - [Model2Vec technical documentation](https://github.com/MinishLab/model2vec/blob/main/docs/what_is_model2vec.md): provides a high-level overview of how Model2Vec works.
155
+
156
+
157
+ ## Model List
158
+
159
+ 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.
160
+
161
+
162
+
163
+ | Model | Language | Sentence Transformer | Params | Task |
164
+ |-----------------------------------------------------------------------|------------|-----------------------------------------------------------------|---------|-----------|
165
+ | [potion-base-32M](https://huggingface.co/minishlab/potion-base-32M) | English | [bge-base-en-v1.5](https://huggingface.co/BAAI/bge-base-en-v1.5) | 32.3M | General |
166
+ | [potion-base-8M](https://huggingface.co/minishlab/potion-base-8M) | English | [bge-base-en-v1.5](https://huggingface.co/BAAI/bge-base-en-v1.5) | 7.5M | General |
167
+ | [potion-base-4M](https://huggingface.co/minishlab/potion-base-4M) | English | [bge-base-en-v1.5](https://huggingface.co/BAAI/bge-base-en-v1.5) | 3.7M | General |
168
+ | [potion-base-2M](https://huggingface.co/minishlab/potion-base-2M) | English | [bge-base-en-v1.5](https://huggingface.co/BAAI/bge-base-en-v1.5) | 1.8M | General |
169
+ | [potion-retrieval-32M](https://huggingface.co/minishlab/potion-retrieval-32M) | English | [bge-base-en-v1.5](https://huggingface.co/BAAI/bge-base-en-v1.5) | 32.3M | Retrieval |
170
+ | [M2V_multilingual_output](https://huggingface.co/minishlab/M2V_multilingual_output) | Multilingual | [LaBSE](https://huggingface.co/sentence-transformers/LaBSE) | 471M | General |
171
+
172
+
173
+ ## Results
174
+
175
+ We have performed extensive experiments to evaluate the performance of Model2Vec models. The results are documented in the [results](results/README.md) folder. The results are presented in the following sections:
176
+ - [MTEB Results](results/README.md#mteb-results)
177
+ - [Training Results](results/README.md#training-results)
178
+ - [Ablations](results/README.md#ablations)
179
+
180
+ ## License
181
+
182
+ MIT
183
+
184
+ ## Citing
185
+
186
+ If you use Model2Vec in your research, please cite the following:
187
+ ```bibtex
188
+ @software{minishlab2024model2vec,
189
+ authors = {Stephan Tulkens and Thomas van Dongen},
190
+ title = {Model2Vec: Fast State-of-the-Art Static Embeddings},
191
+ year = {2024},
192
+ url = {https://github.com/MinishLab/model2vec}
193
+ }
194
+ ```
@@ -0,0 +1,6 @@
1
+ # Documentation
2
+
3
+ This directory contains the documentation for Model2Vec. The documentation is formatted in Markdown. The documentation is organized as follows:
4
+ - [usage.md](https://github.com/MinishLab/model2vec/blob/main/docs/usage.md): This document provides a technical overview of how to use Model2Vec.
5
+ - [integrations.md](https://github.com/MinishLab/model2vec/blob/main/docs/integrations.md): This document provides examples of how to use Model2Vec in various downstream libraries.
6
+ - [what_is_model2vec.md](https://github.com/MinishLab/model2vec/blob/main/docs/what_is_model2vec.md): This document provides a high-level overview of how Model2Vec works.
@@ -0,0 +1,155 @@
1
+
2
+ # Integrations
3
+
4
+ Model2Vec can be used in a variety of downstream libraries. This document provides examples of how to use Model2Vec in some of these libraries.
5
+
6
+ ## Table of Contents
7
+ - [Sentence Transformers](#sentence-transformers)
8
+ - [LangChain](#langchain)
9
+ - [Txtai](#txtai)
10
+ - [Chonkie](#chonkie)
11
+ - [Transformers.js](#transformersjs)
12
+
13
+ ## Sentence Transformers
14
+
15
+ Model2Vec can be used directly in [Sentence Transformers](https://github.com/UKPLab/sentence-transformers):
16
+
17
+ The following code snippet shows how to load a Model2Vec model into a Sentence Transformer model:
18
+ ```python
19
+ from sentence_transformers import SentenceTransformer
20
+
21
+ # Load a Model2Vec model from the Hub
22
+ model = SentenceTransformer("minishlab/potion-base-8M")
23
+ # Make embeddings
24
+ embeddings = model.encode(["It's dangerous to go alone!", "It's a secret to everybody."])
25
+ ```
26
+
27
+ The following code snippet shows how to distill a model directly into a Sentence Transformer model:
28
+
29
+ ```python
30
+ from sentence_transformers import SentenceTransformer
31
+ from sentence_transformers.models import StaticEmbedding
32
+
33
+ static_embedding = StaticEmbedding.from_distillation("BAAI/bge-base-en-v1.5", device="cpu", pca_dims=256)
34
+ model = SentenceTransformer(modules=[static_embedding])
35
+ embeddings = model.encode(["It's dangerous to go alone!", "It's a secret to everybody."])
36
+ ```
37
+
38
+ For more documentation, please refer to the [Sentence Transformers documentation](https://sbert.net/docs/package_reference/sentence_transformer/models.html#sentence_transformers.models.StaticEmbedding).
39
+
40
+
41
+ ## LangChain
42
+
43
+ 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`:
44
+
45
+ ```python
46
+ from langchain_community.embeddings import Model2vecEmbeddings
47
+ from langchain_community.vectorstores import FAISS
48
+ from langchain.schema import Document
49
+
50
+ # Initialize a Model2Vec embedder
51
+ embedder = Model2vecEmbeddings("minishlab/potion-base-8M")
52
+
53
+ # Create some example texts
54
+ texts = [
55
+ "Enduring Stew",
56
+ "Hearty Elixir",
57
+ "Mighty Mushroom Risotto",
58
+ "Spicy Meat Skewer",
59
+ "Fruit Salad",
60
+ ]
61
+
62
+ # Embed the texts
63
+ embeddings = embedder.embed_documents(texts)
64
+
65
+ # Or, create a vector store and query it
66
+ documents = [Document(page_content=text) for text in texts]
67
+ vector_store = FAISS.from_documents(documents, embedder)
68
+ query = "Risotto"
69
+ query_vector = embedder.embed_query(query)
70
+ retrieved_docs = vector_store.similarity_search_by_vector(query_vector, k=1)
71
+ ```
72
+
73
+ ## Txtai
74
+
75
+ 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]`:
76
+
77
+ ```python
78
+ from txtai import Embeddings
79
+
80
+ # Load a model2vec model
81
+ embeddings = Embeddings(path="minishlab/potion-base-8M", method="model2vec", backend="numpy")
82
+
83
+ # Create some example texts
84
+ texts = ["Enduring Stew", "Hearty Elixir", "Mighty Mushroom Risotto", "Spicy Meat Skewer", "Chilly Fruit Salad"]
85
+
86
+ # Create embeddings for downstream tasks
87
+ vectors = embeddings.batchtransform(texts)
88
+
89
+ # Or create a nearest-neighbors index and search it
90
+ embeddings.index(texts)
91
+ result = embeddings.search("Risotto", 1)
92
+ ```
93
+
94
+ ## Chonkie
95
+
96
+ 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:
97
+
98
+ ```python
99
+ from chonkie import SDPMChunker
100
+
101
+ # Create some example text to chunk
102
+ text = "It's dangerous to go alone! Take this."
103
+
104
+ # Initialize the SemanticChunker with a potion model
105
+ chunker = SDPMChunker(
106
+ embedding_model="minishlab/potion-base-8M",
107
+ similarity_threshold=0.3
108
+ )
109
+
110
+ # Chunk the text
111
+ chunks = chunker.chunk(text)
112
+ ```
113
+
114
+ ## Transformers.js
115
+
116
+ 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:
117
+
118
+ ```javascript
119
+ import { AutoModel, AutoTokenizer, Tensor } from '@huggingface/transformers';
120
+
121
+ const modelName = 'minishlab/potion-base-8M';
122
+
123
+ const modelConfig = {
124
+ config: { model_type: 'model2vec' },
125
+ dtype: 'fp32',
126
+ revision: 'refs/pr/1'
127
+ };
128
+ const tokenizerConfig = {
129
+ revision: 'refs/pr/2'
130
+ };
131
+
132
+ const model = await AutoModel.from_pretrained(modelName, modelConfig);
133
+ const tokenizer = await AutoTokenizer.from_pretrained(modelName, tokenizerConfig);
134
+
135
+ const texts = ['hello', 'hello world'];
136
+ const { input_ids } = await tokenizer(texts, { add_special_tokens: false, return_tensor: false });
137
+
138
+ const cumsum = arr => arr.reduce((acc, num, i) => [...acc, num + (acc[i - 1] || 0)], []);
139
+ const offsets = [0, ...cumsum(input_ids.slice(0, -1).map(x => x.length))];
140
+
141
+ const flattened_input_ids = input_ids.flat();
142
+ const modelInputs = {
143
+ input_ids: new Tensor('int64', flattened_input_ids, [flattened_input_ids.length]),
144
+ offsets: new Tensor('int64', offsets, [offsets.length])
145
+ };
146
+
147
+ const { embeddings } = await model(modelInputs);
148
+ console.log(embeddings.tolist()); // output matches python version
149
+ ```
150
+
151
+ Note that this requires that the Model2Vec has a `model.onnx` file and several required tokenizers file. To generate these for a model that does not have them yet, the following code snippet can be used:
152
+
153
+ ```bash
154
+ python scripts/export_to_onnx.py --model_path <path-to-a-model2vec-model> --save_path "<path-to-save-the-onnx-model>"
155
+ ```