akanyaani commited on
Commit
871b748
·
verified ·
1 Parent(s): 9af3f83

Model card: one quick start with English, Hindi and Hinglish, tested code examples with outputs, precision section, Transformers code folded

Browse files
Files changed (1) hide show
  1. README.md +80 -31
README.md CHANGED
@@ -132,41 +132,40 @@ Mean task scores on MTEB. EmbeddingNandi-450M is evaluated with the task prompts
132
 
133
  ## Quick Start
134
 
135
- Install the `sentence-transformers` library (tested with sentence-transformers 6.1.0 and transformers 5.20.2):
136
 
137
  ```bash
138
- pip install -U sentence-transformers transformers
139
  ```
140
 
141
- Generate text embeddings. The model uses custom code, so pass `trust_remote_code=True`:
142
 
143
  ```python
144
  from sentence_transformers import SentenceTransformer
145
 
146
  model = SentenceTransformer("FrontiersMind/EmbeddingNandi-450M", trust_remote_code=True)
147
 
148
- query = "What causes the northern lights?"
149
- document = "The northern lights are caused by charged particles from the sun."
150
-
151
- query_emb = model.encode(query, prompt_name="SearchQuery")
152
- doc_emb = model.encode(document, prompt_name="Document")
153
- print(model.similarity(query_emb, doc_emb))
154
- ```
155
-
156
- For search, `encode_query` and `encode_document` add the query and document prompts for you:
157
-
158
- ```python
159
- queries = ["What is the capital of France?", "फ्रांस की राजधानी क्या है?"]
160
  documents = [
161
- "Paris is the capital and largest city of France.",
 
 
162
  "The Amazon is the largest rainforest in the world.",
163
- "पेरिस फ्रांस की राजधानी है।",
164
  ]
165
 
166
- query_embs = model.encode_query(queries) # "query" prompt, same as prompt_name="SearchQuery"
167
- doc_embs = model.encode_document(documents) # "document" prompt, same as prompt_name="Document"
168
- print(query_embs.shape, doc_embs.shape) # (2, 1024) (3, 1024)
 
 
169
  print(model.similarity(query_embs, doc_embs))
 
 
 
170
  ```
171
 
172
  ## Best Practices
@@ -188,6 +187,30 @@ There are two kinds of tasks:
188
  | Clustering | Symmetric | `Clustering` | `task: clustering \| query: {content}` | N/A |
189
  | Sentence similarity | Symmetric | `SentenceSimilarity` | `task: sentence similarity \| query: {content}` | N/A |
190
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
191
  `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.
192
 
193
  ### 2. Matryoshka Dimension Truncation
@@ -200,17 +223,16 @@ EmbeddingNandi-450M is trained with Matryoshka Representation Learning, so the 1
200
  Pass `truncate_dim` and `normalize_embeddings=True` to let Sentence Transformers do both:
201
 
202
  ```python
203
- query_embs = model.encode_query(
204
- queries,
205
- truncate_dim=256, # or 768, 512, 128
206
- normalize_embeddings=True,
207
- )
208
  ```
209
 
210
  Or set the size once when loading the model:
211
 
212
  ```python
213
- model = SentenceTransformer("FrontiersMind/EmbeddingNandi-450M", trust_remote_code=True, truncate_dim=256)
214
  ```
215
 
216
  ### 3. Long Inputs
@@ -221,9 +243,26 @@ The model takes inputs of up to 32,768 tokens, and `model.max_seq_length` is 327
221
  model.max_seq_length = 1024
222
  ```
223
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
224
  ## Usage with Transformers
225
 
226
- EmbeddingNandi-450M also loads with Hugging Face `transformers` 5.x 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.
 
 
 
227
 
228
  ```python
229
  import torch
@@ -239,7 +278,7 @@ QUERY_PROMPT = "task: search result | query: "
239
  DOCUMENT_PROMPT = "title: none | text: "
240
 
241
 
242
- def embed(texts, prompt, max_length=1024, dim=None):
243
  inputs = tokenizer(
244
  [prompt + text for text in texts],
245
  padding=True,
@@ -257,17 +296,27 @@ def embed(texts, prompt, max_length=1024, dim=None):
257
  return F.normalize(emb.float(), dim=-1)
258
 
259
 
260
- queries = ["What is the capital of France?", "फ्रांस की राजधानी क्या है?"]
 
 
 
 
261
  documents = [
262
- "Paris is the capital and largest city of France.",
 
 
263
  "The Amazon is the largest rainforest in the world.",
264
- "पेरिस फ्रांस की राजधानी है।",
265
  ]
266
 
267
  scores = embed(queries, QUERY_PROMPT) @ embed(documents, DOCUMENT_PROMPT).T # cosine similarity
268
  print(scores)
 
 
 
269
  ```
270
 
 
 
271
  ## Citation
272
 
273
  ```bibtex
 
132
 
133
  ## Quick Start
134
 
135
+ Install Sentence Transformers (tested with sentence-transformers 6.1.0 and transformers 5.20.2):
136
 
137
  ```bash
138
+ pip install -U "sentence-transformers>=6.1.0" "transformers>=5.20.2"
139
  ```
140
 
141
+ The model uses custom code, so pass `trust_remote_code=True`. For search, `encode_query` and `encode_document` add the query and document prompts for you. Queries and documents can be in English, an Indic script or romanized text, in any mix:
142
 
143
  ```python
144
  from sentence_transformers import SentenceTransformer
145
 
146
  model = SentenceTransformer("FrontiersMind/EmbeddingNandi-450M", trust_remote_code=True)
147
 
148
+ queries = [
149
+ "What causes the northern lights?",
150
+ "फ्रांस की राजधानी क्या है?",
151
+ "phone ki battery jaldi khatam kyun hoti hai",
152
+ ]
 
 
 
 
 
 
 
153
  documents = [
154
+ "The northern lights are caused by charged particles from the sun hitting the Earth's atmosphere.",
155
+ "पेरिस फ्रांस की राजधानी और सबसे बड़ा शहर है।",
156
+ "Phone batteries drain quickly when many apps run in the background and the screen is very bright.",
157
  "The Amazon is the largest rainforest in the world.",
 
158
  ]
159
 
160
+ query_embs = model.encode_query(queries)
161
+ doc_embs = model.encode_document(documents)
162
+ print(query_embs.shape, doc_embs.shape)
163
+ # (3, 1024) (4, 1024)
164
+
165
  print(model.similarity(query_embs, doc_embs))
166
+ # tensor([[0.7576, 0.0962, 0.1732, 0.1833],
167
+ # [0.1373, 0.8103, 0.0362, 0.2305],
168
+ # [0.1879, 0.0734, 0.5977, 0.0014]])
169
  ```
170
 
171
  ## Best Practices
 
187
  | Clustering | Symmetric | `Clustering` | `task: clustering \| query: {content}` | N/A |
188
  | Sentence similarity | Symmetric | `SentenceSimilarity` | `task: sentence similarity \| query: {content}` | N/A |
189
 
190
+ Pass the prompt name to `encode`. For code search, encode the queries with `CodeRetrieval` and the code with `Document`:
191
+
192
+ ```python
193
+ code_queries = ["reverse a string in python"]
194
+ code_docs = ["def reverse(s):\n return s[::-1]", "def add(a, b):\n return a + b"]
195
+
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.5560, 0.2400]])
200
+ ```
201
+
202
+ For a symmetric task, use the same prompt for every text:
203
+
204
+ ```python
205
+ texts = ["This phone is really good", "Yeh phone bahut accha hai", "My order was delivered late"]
206
+
207
+ embs = model.encode(texts, prompt_name="SentenceSimilarity")
208
+ print(model.similarity(embs, embs))
209
+ # tensor([[1.0000, 0.7550, 0.2029],
210
+ # [0.7550, 1.0000, 0.0294],
211
+ # [0.2029, 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.
215
 
216
  ### 2. Matryoshka Dimension Truncation
 
223
  Pass `truncate_dim` and `normalize_embeddings=True` to let Sentence Transformers do both:
224
 
225
  ```python
226
+ query_embs = model.encode_query(queries, truncate_dim=256, normalize_embeddings=True)
227
+ doc_embs = model.encode_document(documents, truncate_dim=256, normalize_embeddings=True)
228
+ print(query_embs.shape)
229
+ # (3, 256)
 
230
  ```
231
 
232
  Or set the size once when loading the model:
233
 
234
  ```python
235
+ model_256 = SentenceTransformer("FrontiersMind/EmbeddingNandi-450M", trust_remote_code=True, truncate_dim=256)
236
  ```
237
 
238
  ### 3. Long Inputs
 
243
  model.max_seq_length = 1024
244
  ```
245
 
246
+ ### 4. Numerical Precision
247
+
248
+ The weights are stored in float32. On a GPU you can load the model in half precision to halve memory and speed up inference. In our checks against float32, float16 embeddings stayed above 0.998 cosine similarity even for a 16K-token input. bfloat16 stayed above 0.999 up to 4K tokens but dropped to about 0.988 at 16K tokens, so use float16 or float32 for long documents. On CPU, float32 is the safe default.
249
+
250
+ ```python
251
+ import torch
252
+
253
+ model = SentenceTransformer(
254
+ "FrontiersMind/EmbeddingNandi-450M",
255
+ trust_remote_code=True,
256
+ model_kwargs={"dtype": torch.float16},
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>
266
 
267
  ```python
268
  import torch
 
278
  DOCUMENT_PROMPT = "title: none | text: "
279
 
280
 
281
+ def embed(texts, prompt, max_length=32768, dim=None):
282
  inputs = tokenizer(
283
  [prompt + text for text in texts],
284
  padding=True,
 
296
  return F.normalize(emb.float(), dim=-1)
297
 
298
 
299
+ queries = [
300
+ "What causes the northern lights?",
301
+ "फ्रांस की राजधानी क्या है?",
302
+ "phone ki battery jaldi khatam kyun hoti hai",
303
+ ]
304
  documents = [
305
+ "The northern lights are caused by charged particles from the sun hitting the Earth's atmosphere.",
306
+ "पेरिस फ्रांस की राजधानी और सबसे बड़ा शहर है।",
307
+ "Phone batteries drain quickly when many apps run in the background and the screen is very bright.",
308
  "The Amazon is the largest rainforest in the world.",
 
309
  ]
310
 
311
  scores = embed(queries, QUERY_PROMPT) @ embed(documents, DOCUMENT_PROMPT).T # cosine similarity
312
  print(scores)
313
+ # tensor([[0.7576, 0.0962, 0.1732, 0.1833],
314
+ # [0.1373, 0.8103, 0.0362, 0.2305],
315
+ # [0.1879, 0.0734, 0.5977, 0.0014]])
316
  ```
317
 
318
+ </details>
319
+
320
  ## Citation
321
 
322
  ```bibtex