Text Ranking
sentence-transformers
Safetensors
Transformers
multilingual
t5gemma2
text2text-generation
reranker
encoder-decoder
FBNL
Retrieval
RAG
Instructions to use KaLM-Embedding/KaLM-Reranker-V1-Nano 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 with sentence-transformers:
from sentence_transformers import CrossEncoder model = CrossEncoder("KaLM-Embedding/KaLM-Reranker-V1-Nano") 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 with Transformers:
# Load model directly from transformers import AutoProcessor, AutoModelForMultimodalLM processor = AutoProcessor.from_pretrained("KaLM-Embedding/KaLM-Reranker-V1-Nano") model = AutoModelForMultimodalLM.from_pretrained("KaLM-Embedding/KaLM-Reranker-V1-Nano", device_map="auto") - Notebooks
- Google Colab
- Kaggle
Update README.md
Browse files
README.md
CHANGED
|
@@ -92,6 +92,59 @@ On LMEB, reranking models demonstrate a clear advantage, with even the 0.27B Nan
|
|
| 92 |

|
| 93 |
|
| 94 |
# Usage
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 95 |
## Using transformers
|
| 96 |
```python
|
| 97 |
import argparse
|
|
|
|
| 92 |

|
| 93 |
|
| 94 |
# Usage
|
| 95 |
+
## Using Sentence Transformers
|
| 96 |
+
|
| 97 |
+
KaLM-Reranker-V1-Nano can be loaded as a modular Sentence Transformers
|
| 98 |
+
`CrossEncoder`. This integration requires `sentence-transformers>=5.6,<6`,
|
| 99 |
+
`transformers>=5.3,<6`, and the PyTorch backend. The repository contains custom
|
| 100 |
+
modeling code, so load only trusted revisions and pass `trust_remote_code=True`.
|
| 101 |
+
|
| 102 |
+
```bash
|
| 103 |
+
pip install "sentence-transformers>=5.6,<6" "transformers>=5.3,<6"
|
| 104 |
+
```
|
| 105 |
+
|
| 106 |
+
```python
|
| 107 |
+
import torch
|
| 108 |
+
from sentence_transformers import CrossEncoder
|
| 109 |
+
|
| 110 |
+
model = CrossEncoder(
|
| 111 |
+
"KaLM-Embedding/KaLM-Reranker-V1-Nano",
|
| 112 |
+
trust_remote_code=True,
|
| 113 |
+
device="cuda",
|
| 114 |
+
model_kwargs={"dtype": torch.bfloat16, "chunk_size": 4},
|
| 115 |
+
)
|
| 116 |
+
|
| 117 |
+
query = "What is the capital of China?"
|
| 118 |
+
documents = [
|
| 119 |
+
"The capital of China is Beijing.",
|
| 120 |
+
"Gravity attracts bodies toward one another.",
|
| 121 |
+
]
|
| 122 |
+
pairs = [(query, document) for document in documents]
|
| 123 |
+
|
| 124 |
+
# The default output is P(yes).
|
| 125 |
+
scores = model.predict(pairs)
|
| 126 |
+
rankings = model.rank(query, documents, return_documents=True)
|
| 127 |
+
|
| 128 |
+
# CrossEncoder prompts are interpreted as KaLM task instructions.
|
| 129 |
+
instruction = "Given a web search query, retrieve passages that answer the query."
|
| 130 |
+
custom_scores = model.predict(pairs, prompt=instruction)
|
| 131 |
+
custom_rankings = model.rank(query, documents, prompt=instruction)
|
| 132 |
+
|
| 133 |
+
# Use Identity to return yes_logit - no_logit instead of P(yes).
|
| 134 |
+
margins = model.predict(pairs, activation_fn=torch.nn.Identity())
|
| 135 |
+
```
|
| 136 |
+
|
| 137 |
+
Inputs must be ordered as `(query, document)`. By default, queries are
|
| 138 |
+
truncated to 512 tokens and documents to 1024 tokens. `chunk_size=4` performs a
|
| 139 |
+
mask-aware mean over each consecutive group of four encoder token states before
|
| 140 |
+
passing the compressed encoder output to the decoder. Set `chunk_size=None` to
|
| 141 |
+
disable compression, or change `model[0].chunk_size` after loading.
|
| 142 |
+
|
| 143 |
+
For CPU inference, use `device="cpu"` and
|
| 144 |
+
`model_kwargs={"dtype": torch.float32, "chunk_size": 4}`. Only the PyTorch
|
| 145 |
+
inference backend is currently supported; training, ONNX, and OpenVINO are not
|
| 146 |
+
included in this release.
|
| 147 |
+
|
| 148 |
## Using transformers
|
| 149 |
```python
|
| 150 |
import argparse
|