Text Ranking
sentence-transformers
Safetensors
Transformers
multilingual
t5gemma2
text2text-generation
reranker
encoder-decoder
FBNL
Retrieval
RAG
cosyy commited on
Commit
1763a45
·
verified ·
1 Parent(s): 465d1b4

Update README.md

Browse files
Files changed (1) hide show
  1. README.md +53 -0
README.md CHANGED
@@ -92,6 +92,59 @@ On LMEB, reranking models demonstrate a clear advantage, with even the 0.27B Nan
92
  ![lmeb_emb](./assets/lmeb_emb.jpg)
93
 
94
  # Usage
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
95
  ## Using transformers
96
  ```python
97
  import argparse
 
92
  ![lmeb_emb](./assets/lmeb_emb.jpg)
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