Text Ranking
sentence-transformers
Safetensors
Transformers
multilingual
t5gemma2
text2text-generation
reranker
encoder-decoder
FBNL
matryoshka
retrieval
RAG
Instructions to use KaLM-Embedding/KaLM-Reranker-V1-Nano-R2 with libraries, inference providers, notebooks, and local apps. Follow these links to get started.
- Libraries
- sentence-transformers
How to use KaLM-Embedding/KaLM-Reranker-V1-Nano-R2 with sentence-transformers:
from sentence_transformers import CrossEncoder model = CrossEncoder("KaLM-Embedding/KaLM-Reranker-V1-Nano-R2") query = "Which planet is known as the Red Planet?" passages = [ "Venus is often called Earth's twin because of its similar size and proximity.", "Mars, known for its reddish appearance, is often referred to as the Red Planet.", "Jupiter, the largest planet in our solar system, has a prominent red spot.", "Saturn, famous for its rings, is sometimes mistaken for the Red Planet." ] scores = model.predict([(query, passage) for passage in passages]) print(scores) - Transformers
How to use KaLM-Embedding/KaLM-Reranker-V1-Nano-R2 with Transformers:
# Load model directly from transformers import AutoProcessor, AutoModelForMultimodalLM processor = AutoProcessor.from_pretrained("KaLM-Embedding/KaLM-Reranker-V1-Nano-R2") model = AutoModelForMultimodalLM.from_pretrained("KaLM-Embedding/KaLM-Reranker-V1-Nano-R2", device_map="auto") - Notebooks
- Google Colab
- Kaggle
Update README.md
Browse files
README.md
CHANGED
|
@@ -340,6 +340,112 @@ rankings: [{'corpus_id': 0, 'score': 0.985496461391449}, {'corpus_id': 1, 'score
|
|
| 340 |
|
| 341 |
```
|
| 342 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 343 |
|
| 344 |
#### Ablation on multi-stage training
|
| 345 |
|
|
|
|
| 340 |
|
| 341 |
```
|
| 342 |
|
| 343 |
+
### Using vLLM
|
| 344 |
+
An experimental single-GPU adapter is available for offline
|
| 345 |
+
`LLM.classify()` reranking and optional FastAPI serving. It reuses the original
|
| 346 |
+
checkpoint without adding or modifying model weights.
|
| 347 |
+
|
| 348 |
+
The adapter has been validated with Python 3.12, vLLM 0.19.1, Transformers
|
| 349 |
+
5.6.2 and CUDA BF16:
|
| 350 |
+
|
| 351 |
+
```bash
|
| 352 |
+
conda create -n kalm-vllm python=3.12 -y
|
| 353 |
+
conda activate kalm-vllm
|
| 354 |
+
pip install "vllm==0.19.1" "transformers==5.6.2"
|
| 355 |
+
|
| 356 |
+
hf download KaLM-Embedding/KaLM-Reranker-V1-Nano-R2 \
|
| 357 |
+
--local-dir ./KaLM-Reranker-V1-Nano-R2
|
| 358 |
+
pip install ./KaLM-Reranker-V1-Nano-R2/vllm_support --no-deps
|
| 359 |
+
export VLLM_PLUGINS=kalm_t5gemma2
|
| 360 |
+
```
|
| 361 |
+
|
| 362 |
+
If you modify the `vllm_support` source code for some reason
|
| 363 |
+
(e.g. changing the model path in `vllm_support/src/kalm_t5gemma2_vllm_plugin/constants.py`),
|
| 364 |
+
please run `pip install ./KaLM-Reranker-V1-Nano-R2/vllm_support --no-deps --force-reinstall` to refresh the patch.
|
| 365 |
+
|
| 366 |
+
Offline Python:
|
| 367 |
+
|
| 368 |
+
```python
|
| 369 |
+
from kalm_t5gemma2_vllm_plugin import KaLMVLLMReranker
|
| 370 |
+
|
| 371 |
+
query = "What is the capital of China?"
|
| 372 |
+
documents = [
|
| 373 |
+
"The capital of China is Beijing.",
|
| 374 |
+
"Gravity attracts bodies toward one another.",
|
| 375 |
+
]
|
| 376 |
+
|
| 377 |
+
with KaLMVLLMReranker(
|
| 378 |
+
"KaLM-Embedding/KaLM-Reranker-V1-Nano-R2",
|
| 379 |
+
query_max_length=512,
|
| 380 |
+
document_max_length=1024,
|
| 381 |
+
encoder_chunk_size=4,
|
| 382 |
+
) as reranker:
|
| 383 |
+
print(reranker.rank(query, documents))
|
| 384 |
+
```
|
| 385 |
+
|
| 386 |
+
Offline CLI:
|
| 387 |
+
|
| 388 |
+
```bash
|
| 389 |
+
kalm-vllm-rerank --return-margin
|
| 390 |
+
```
|
| 391 |
+
|
| 392 |
+
To deploy the online service, install the HTTP dependencies and keep the
|
| 393 |
+
server running in the first terminal:
|
| 394 |
+
|
| 395 |
+
```bash
|
| 396 |
+
pip install "fastapi>=0.136,<0.137" "uvicorn>=0.46,<0.47"
|
| 397 |
+
export CUDA_VISIBLE_DEVICES=0
|
| 398 |
+
export VLLM_PLUGINS=kalm_t5gemma2
|
| 399 |
+
|
| 400 |
+
kalm-vllm-serve \
|
| 401 |
+
--host 0.0.0.0 \
|
| 402 |
+
--port 8000 \
|
| 403 |
+
--model KaLM-Embedding/KaLM-Reranker-V1-Nano-R2 \
|
| 404 |
+
--query-max-length 512 \
|
| 405 |
+
--document-max-length 1024 \
|
| 406 |
+
--encoder-chunk-size 4 \
|
| 407 |
+
--max-model-len 2048
|
| 408 |
+
```
|
| 409 |
+
|
| 410 |
+
In a second terminal, check the server:
|
| 411 |
+
|
| 412 |
+
```bash
|
| 413 |
+
conda activate kalm-vllm
|
| 414 |
+
kalm-vllm-client --base-url http://127.0.0.1:8000 --health
|
| 415 |
+
```
|
| 416 |
+
|
| 417 |
+
Use `/rerank` for one query and a list of documents. Results are sorted by
|
| 418 |
+
score:
|
| 419 |
+
|
| 420 |
+
```bash
|
| 421 |
+
kalm-vllm-client \
|
| 422 |
+
--base-url http://127.0.0.1:8000 \
|
| 423 |
+
--endpoint rerank \
|
| 424 |
+
--json-file ./KaLM-Reranker-V1-Nano-R2/vllm_support/examples/rerank_request.json \
|
| 425 |
+
--return-margin \
|
| 426 |
+
--top-k 10
|
| 427 |
+
```
|
| 428 |
+
|
| 429 |
+
Use `/score` to score a batch of independent query-document pairs. Results
|
| 430 |
+
preserve the input order and optional IDs:
|
| 431 |
+
|
| 432 |
+
```bash
|
| 433 |
+
kalm-vllm-client \
|
| 434 |
+
--base-url http://127.0.0.1:8000 \
|
| 435 |
+
--endpoint score \
|
| 436 |
+
--json-file ./KaLM-Reranker-V1-Nano-R2/vllm_support/examples/score_request.json \
|
| 437 |
+
--return-margin
|
| 438 |
+
```
|
| 439 |
+
|
| 440 |
+
The default output is `P(yes)`. Set `return_margin=true` to also receive
|
| 441 |
+
`yes_logit - no_logit`; the client flag `--return-margin` applies the same
|
| 442 |
+
setting to a JSON file request. The supported encoder chunk sizes are
|
| 443 |
+
`1, 2, 4, 8, 16, 32`, with `4` as the default.
|
| 444 |
+
|
| 445 |
+
This adapter uses vLLM's plugin, scheduling and pooling interfaces while the
|
| 446 |
+
T5Gemma2 semantic forward still runs through Transformers. It is not vLLM's
|
| 447 |
+
native HTTP `/score` implementation or a complete vLLM-native kernel port.
|
| 448 |
+
See [the complete installation, API and troubleshooting guide](./vllm_support/README.md).
|
| 449 |
|
| 450 |
#### Ablation on multi-stage training
|
| 451 |
|