Sentence Similarity
sentence-transformers
Safetensors
nandi_encoder
embeddings
feature-extraction
mteb
retrieval
multilingual
indic
long-context
frontiersmind
nandi
custom_code
Instructions to use FrontiersMind/EmbeddingNandi-450M with libraries, inference providers, notebooks, and local apps. Follow these links to get started.
- Libraries
- sentence-transformers
How to use FrontiersMind/EmbeddingNandi-450M with sentence-transformers:
from sentence_transformers import SentenceTransformer model = SentenceTransformer("FrontiersMind/EmbeddingNandi-450M", trust_remote_code=True) sentences = [ "task: sentence similarity | query: How do I return a damaged product?", "task: sentence similarity | query: खराब सामान वापस कैसे करें?", "task: sentence similarity | query: damaged product wapas kaise kare?", "task: sentence similarity | query: What is the weather like today?" ] embeddings = model.encode(sentences) similarities = model.similarity(embeddings, embeddings) print(similarities.shape) # [4, 4] - Notebooks
- Google Colab
- Kaggle
Model card: bfloat16 is the default precision, outputs refreshed
Browse files
README.md
CHANGED
|
@@ -163,9 +163,9 @@ print(query_embs.shape, doc_embs.shape)
|
|
| 163 |
# (3, 1024) (4, 1024)
|
| 164 |
|
| 165 |
print(model.similarity(query_embs, doc_embs))
|
| 166 |
-
# tensor([[0.
|
| 167 |
-
# [0.
|
| 168 |
-
# [0.
|
| 169 |
```
|
| 170 |
|
| 171 |
## Best Practices
|
|
@@ -196,7 +196,7 @@ code_docs = ["def reverse(s):\n return s[::-1]", "def add(a, b):\n return
|
|
| 196 |
q = model.encode(code_queries, prompt_name="CodeRetrieval")
|
| 197 |
d = model.encode(code_docs, prompt_name="Document")
|
| 198 |
print(model.similarity(q, d))
|
| 199 |
-
# tensor([[0.
|
| 200 |
```
|
| 201 |
|
| 202 |
For a symmetric task, use the same prompt for every text:
|
|
@@ -206,9 +206,9 @@ texts = ["This phone is really good", "Yeh phone bahut accha hai", "My order was
|
|
| 206 |
|
| 207 |
embs = model.encode(texts, prompt_name="SentenceSimilarity")
|
| 208 |
print(model.similarity(embs, embs))
|
| 209 |
-
# tensor([[1.0000, 0.
|
| 210 |
-
# [0.
|
| 211 |
-
# [0.
|
| 212 |
```
|
| 213 |
|
| 214 |
`encode_query` uses the `query` prompt (same text as `SearchQuery`) and `encode_document` uses the `document` prompt (same text as `Document`). The model also holds prompts keyed by MTEB task type (for example `Retrieval-query`, `Retrieval-document`, `STS`, `BitextMining`), so MTEB picks the right prompt for each task. `model.prompts` lists all of them.
|
|
@@ -245,7 +245,7 @@ model.max_seq_length = 1024
|
|
| 245 |
|
| 246 |
### 4. Numerical Precision
|
| 247 |
|
| 248 |
-
The weights are stored in
|
| 249 |
|
| 250 |
```python
|
| 251 |
import torch
|
|
@@ -253,13 +253,13 @@ import torch
|
|
| 253 |
model = SentenceTransformer(
|
| 254 |
"FrontiersMind/EmbeddingNandi-450M",
|
| 255 |
trust_remote_code=True,
|
| 256 |
-
model_kwargs={"dtype": torch.
|
| 257 |
)
|
| 258 |
```
|
| 259 |
|
| 260 |
## Usage with Transformers
|
| 261 |
|
| 262 |
-
EmbeddingNandi-450M also loads with Hugging Face `transformers` using `trust_remote_code=True` for both the tokenizer and the model. Add the task prompt in front of each text, take the mean over all tokens (prompt tokens included) and L2 normalize. This gives the same embeddings as Sentence Transformers.
|
| 263 |
|
| 264 |
<details>
|
| 265 |
<summary>Show the code</summary>
|
|
@@ -310,9 +310,9 @@ documents = [
|
|
| 310 |
|
| 311 |
scores = embed(queries, QUERY_PROMPT) @ embed(documents, DOCUMENT_PROMPT).T # cosine similarity
|
| 312 |
print(scores)
|
| 313 |
-
# tensor([[0.
|
| 314 |
-
# [0.
|
| 315 |
-
# [0.
|
| 316 |
```
|
| 317 |
|
| 318 |
</details>
|
|
|
|
| 163 |
# (3, 1024) (4, 1024)
|
| 164 |
|
| 165 |
print(model.similarity(query_embs, doc_embs))
|
| 166 |
+
# tensor([[0.7564, 0.0961, 0.1738, 0.1838],
|
| 167 |
+
# [0.1359, 0.8079, 0.0362, 0.2289],
|
| 168 |
+
# [0.1872, 0.0741, 0.5996, 0.0011]])
|
| 169 |
```
|
| 170 |
|
| 171 |
## Best Practices
|
|
|
|
| 196 |
q = model.encode(code_queries, prompt_name="CodeRetrieval")
|
| 197 |
d = model.encode(code_docs, prompt_name="Document")
|
| 198 |
print(model.similarity(q, d))
|
| 199 |
+
# tensor([[0.5554, 0.2409]])
|
| 200 |
```
|
| 201 |
|
| 202 |
For a symmetric task, use the same prompt for every text:
|
|
|
|
| 206 |
|
| 207 |
embs = model.encode(texts, prompt_name="SentenceSimilarity")
|
| 208 |
print(model.similarity(embs, embs))
|
| 209 |
+
# tensor([[1.0000, 0.7536, 0.2031],
|
| 210 |
+
# [0.7536, 1.0000, 0.0294],
|
| 211 |
+
# [0.2031, 0.0294, 1.0000]])
|
| 212 |
```
|
| 213 |
|
| 214 |
`encode_query` uses the `query` prompt (same text as `SearchQuery`) and `encode_document` uses the `document` prompt (same text as `Document`). The model also holds prompts keyed by MTEB task type (for example `Retrieval-query`, `Retrieval-document`, `STS`, `BitextMining`), so MTEB picks the right prompt for each task. `model.prompts` lists all of them.
|
|
|
|
| 245 |
|
| 246 |
### 4. Numerical Precision
|
| 247 |
|
| 248 |
+
The weights are stored in `bfloat16` and the model loads in `bfloat16` by default. This is the precision used for the benchmark results above, and it works well for inputs up to a few thousand tokens. For very long inputs, load the model in `float32` (or `float16` on GPU) for more accurate embeddings: on a 16K-token input, `float32` and `float16` stay above 0.998 cosine similarity to the original `float32` model, while `bfloat16` is about 0.988.
|
| 249 |
|
| 250 |
```python
|
| 251 |
import torch
|
|
|
|
| 253 |
model = SentenceTransformer(
|
| 254 |
"FrontiersMind/EmbeddingNandi-450M",
|
| 255 |
trust_remote_code=True,
|
| 256 |
+
model_kwargs={"dtype": torch.float32}, # or torch.float16 on GPU
|
| 257 |
)
|
| 258 |
```
|
| 259 |
|
| 260 |
## Usage with Transformers
|
| 261 |
|
| 262 |
+
EmbeddingNandi-450M also loads with Hugging Face `transformers` using `trust_remote_code=True` for both the tokenizer and the model. Add the task prompt in front of each text, take the mean over all tokens (prompt tokens included) and L2 normalize. This gives the same embeddings as Sentence Transformers, up to tiny `bfloat16` rounding differences.
|
| 263 |
|
| 264 |
<details>
|
| 265 |
<summary>Show the code</summary>
|
|
|
|
| 310 |
|
| 311 |
scores = embed(queries, QUERY_PROMPT) @ embed(documents, DOCUMENT_PROMPT).T # cosine similarity
|
| 312 |
print(scores)
|
| 313 |
+
# tensor([[0.7565, 0.0961, 0.1738, 0.1837],
|
| 314 |
+
# [0.1358, 0.8079, 0.0361, 0.2288],
|
| 315 |
+
# [0.1873, 0.0740, 0.5995, 0.0010]])
|
| 316 |
```
|
| 317 |
|
| 318 |
</details>
|