Text Ranking
sentence-transformers
Safetensors
Transformers
multilingual
t5gemma2
text2text-generation
reranker
encoder-decoder
FBNL
matryoshka
retrieval
RAG
cosyy commited on
Commit
378247e
·
verified ·
1 Parent(s): a3a1622

Update README.md

Browse files
Files changed (1) hide show
  1. README.md +69 -0
README.md CHANGED
@@ -158,6 +158,75 @@ On LMEB-Dialogue, a compact embedding model paired with our Nano reranker, which
158
  ![lmeb_emb](./assets/lmeb_emb.jpg)
159
 
160
  ## Usage
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
161
  ### Using transformers
162
  ```python
163
  import argparse
 
158
  ![lmeb_emb](./assets/lmeb_emb.jpg)
159
 
160
  ## Usage
161
+ ### Using Sentence Transformers
162
+
163
+ KaLM-Reranker-V1-Nano-R2 can be loaded as a modular Sentence Transformers
164
+ `CrossEncoder`. This integration requires `sentence-transformers>=5.6,<6`,
165
+ `transformers>=5.3,<6`, and the PyTorch backend. The repository contains custom
166
+ modeling code, so load only trusted revisions and pass `trust_remote_code=True`.
167
+
168
+ ```bash
169
+ pip install "sentence-transformers>=5.6,<6" "transformers>=5.3,<6"
170
+ ```
171
+
172
+ ```python
173
+ import torch
174
+ from sentence_transformers import CrossEncoder
175
+
176
+ model = CrossEncoder(
177
+ "KaLM-Embedding/KaLM-Reranker-V1-Nano-R2",
178
+ trust_remote_code=True,
179
+ device="cuda",
180
+ model_kwargs={"dtype": torch.bfloat16, "chunk_size": 4},
181
+ )
182
+
183
+ query = "What is the capital of China?"
184
+ documents = [
185
+ "The capital of China is Beijing.",
186
+ "Gravity attracts bodies toward one another.",
187
+ ]
188
+ pairs = [(query, document) for document in documents]
189
+
190
+ # The default output is P(yes).
191
+ scores = model.predict(pairs)
192
+ rankings = model.rank(query, documents, return_documents=True)
193
+
194
+ # CrossEncoder prompts are interpreted as KaLM task instructions.
195
+ instruction = "Given a web search query, retrieve passages that answer the query."
196
+ custom_scores = model.predict(pairs, prompt=instruction)
197
+ custom_rankings = model.rank(query, documents, prompt=instruction)
198
+
199
+ # Use Identity to return yes_logit - no_logit instead of P(yes).
200
+ margins = model.predict(pairs, activation_fn=torch.nn.Identity())
201
+
202
+ print(f"scores: {scores}")
203
+ print(f"rankings: {rankings}")
204
+ print(f"custom_scores: {custom_scores}")
205
+ print(f"custom_rankings: {custom_rankings}")
206
+ print(f"margins: {margins}")
207
+
208
+ '''
209
+ scores: [9.8549646e-01 1.7952797e-04]
210
+ rankings: [{'corpus_id': 0, 'score': 0.98549646, 'text': 'The capital of China is Beijing.'}, {'corpus_id': 1, 'score': 0.00017952797, 'text': 'Gravity attracts bodies toward one another.'}]
211
+ custom_scores: [9.7771388e-01 7.4846226e-05]
212
+ custom_rankings: [{'corpus_id': 0, 'score': 0.9777139}, {'corpus_id': 1, 'score': 7.4846226e-05}]
213
+ margins: [ 4.21875 -8.625 ]
214
+ '''
215
+
216
+ ```
217
+
218
+ Inputs must be ordered as `(query, document)`. By default, queries are
219
+ truncated to 512 tokens and documents to 1024 tokens. `chunk_size=4` performs a
220
+ mask-aware mean over each consecutive group of four encoder token states before
221
+ passing the compressed encoder output to the decoder. Set `chunk_size=None` to
222
+ disable compression, or change `model[0].chunk_size` after loading.
223
+
224
+ For CPU inference, use `device="cpu"` and
225
+ `model_kwargs={"dtype": torch.float32, "chunk_size": 4}`. Only the PyTorch
226
+ inference backend is currently supported; training, ONNX, and OpenVINO are not
227
+ included in this release.
228
+
229
+
230
  ### Using transformers
231
  ```python
232
  import argparse