Text Ranking
Transformers
Safetensors
sentence-transformers
qwen3
text-generation
cross-encoder
reranker
Instructions to use ContextualAI/ctxl-rerank-v2-instruct-multilingual-2b with libraries, inference providers, notebooks, and local apps. Follow these links to get started.
- Libraries
- Transformers
How to use ContextualAI/ctxl-rerank-v2-instruct-multilingual-2b with Transformers:
# Load model directly from transformers import AutoTokenizer, AutoModelForCausalLM tokenizer = AutoTokenizer.from_pretrained("ContextualAI/ctxl-rerank-v2-instruct-multilingual-2b") model = AutoModelForCausalLM.from_pretrained("ContextualAI/ctxl-rerank-v2-instruct-multilingual-2b", device_map="auto") - sentence-transformers
How to use ContextualAI/ctxl-rerank-v2-instruct-multilingual-2b with sentence-transformers:
from sentence_transformers import CrossEncoder model = CrossEncoder("ContextualAI/ctxl-rerank-v2-instruct-multilingual-2b") 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) - Notebooks
- Google Colab
- Kaggle
Update README.md
Browse files
README.md
CHANGED
|
@@ -4,40 +4,92 @@ license: cc-by-nc-sa-4.0
|
|
| 4 |
pipeline_tag: text-ranking
|
| 5 |
---
|
| 6 |
|
|
|
|
|
|
|
| 7 |
# Contextual AI Reranker v2 2B
|
| 8 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 9 |
## Highlights
|
| 10 |
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
-
|
| 14 |
-
-
|
| 15 |
-
-
|
|
|
|
| 16 |
- Real-world use cases
|
| 17 |
|
| 18 |
<p align="center">
|
| 19 |
<img src="main_benchmark.png" width="1200"/>
|
| 20 |
<p>
|
| 21 |
|
| 22 |
-
For
|
| 23 |
|
| 24 |
## Overview
|
| 25 |
|
| 26 |
-
- Model Type: Text Reranking
|
| 27 |
-
- Supported Languages: 100+
|
| 28 |
-
-
|
| 29 |
-
- Context Length: up to 32K
|
| 30 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 31 |
|
| 32 |
## Quickstart
|
| 33 |
|
| 34 |
-
###
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 35 |
|
| 36 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 37 |
|
| 38 |
```python
|
| 39 |
import os
|
| 40 |
-
os.environ['VLLM_USE_V1'] = '0' # v1 engine doesn
|
| 41 |
|
| 42 |
import torch
|
| 43 |
from vllm import LLM, SamplingParams
|
|
@@ -97,12 +149,26 @@ def infer_w_vllm(model_path: str, query: str, instruction: str, documents: list[
|
|
| 97 |
print(f"Instruction: {instruction}")
|
| 98 |
for score, doc_id, doc in results:
|
| 99 |
print(f"Score: {score:.4f} | Doc: {doc}")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 100 |
```
|
| 101 |
|
| 102 |
|
| 103 |
-
### Transformers Usage
|
| 104 |
|
| 105 |
-
Requires transformers>=4.51.0 for BF16. Not supported for NVFP4.
|
| 106 |
|
| 107 |
```python
|
| 108 |
import torch
|
|
@@ -157,6 +223,20 @@ def infer_w_hf(model_path: str, query: str, instruction: str, documents: list[st
|
|
| 157 |
print(f"Instruction: {instruction}")
|
| 158 |
for score, doc_id, doc in results:
|
| 159 |
print(f"Score: {score:.4f} | Doc: {doc}")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 160 |
```
|
| 161 |
|
| 162 |
## Citation
|
|
@@ -166,7 +246,7 @@ If you use this model, please cite:
|
|
| 166 |
```bibtex
|
| 167 |
@misc{ctxl_rerank_v2_instruct_multilingual,
|
| 168 |
title={Contextual AI Reranker v2},
|
| 169 |
-
author={
|
| 170 |
year={2025},
|
| 171 |
url={https://contextual.ai/blog/rerank-v2},
|
| 172 |
}
|
|
@@ -178,4 +258,4 @@ Creative Commons Attribution Non Commercial Share Alike 4.0 (cc-by-nc-sa-4.0)
|
|
| 178 |
|
| 179 |
## Contact
|
| 180 |
|
| 181 |
-
For questions or issues, please open an issue on the model repository
|
|
|
|
| 4 |
pipeline_tag: text-ranking
|
| 5 |
---
|
| 6 |
|
| 7 |
+
<div align="center">
|
| 8 |
+
|
| 9 |
# Contextual AI Reranker v2 2B
|
| 10 |
|
| 11 |
+
<img src="Contextual_AI_Brand_Mark_Dark.png" width="10%" alt="Contextual_AI"/>
|
| 12 |
+
|
| 13 |
+
[](https://contextual.ai/blog/rerank-v2)
|
| 14 |
+
[](https://huggingface.co/collections/ContextualAI/contextual-ai-reranker-v2)
|
| 15 |
+
|
| 16 |
+
</div>
|
| 17 |
+
|
| 18 |
+
<hr>
|
| 19 |
+
|
| 20 |
## Highlights
|
| 21 |
|
| 22 |
+
Contextual AI's reranker is the **first instruction-following reranker** capable of handling retrieval conflicts and ranking with custom instructions (e.g., prioritizing recent information). It achieves state-of-the-art performance on BEIR and sits on the cost/performance Pareto frontier across:
|
| 23 |
+
|
| 24 |
+
- Instruction following
|
| 25 |
+
- Question answering
|
| 26 |
+
- Multilinguality (100+ languages)
|
| 27 |
+
- Product search & recommendation
|
| 28 |
- Real-world use cases
|
| 29 |
|
| 30 |
<p align="center">
|
| 31 |
<img src="main_benchmark.png" width="1200"/>
|
| 32 |
<p>
|
| 33 |
|
| 34 |
+
For detailed benchmarks, see our [blog post](https://contextual.ai/blog/rerank-v2).
|
| 35 |
|
| 36 |
## Overview
|
| 37 |
|
| 38 |
+
- **Model Type**: Text Reranking
|
| 39 |
+
- **Supported Languages**: 100+
|
| 40 |
+
- **Parameters**: 2B
|
| 41 |
+
- **Context Length**: up to 32K
|
| 42 |
+
|
| 43 |
+
## When to Use This Model
|
| 44 |
+
|
| 45 |
+
Use this reranker when you need to:
|
| 46 |
+
- Re-rank retrieved documents with custom instructions
|
| 47 |
+
- Handle conflicting information in retrieval results
|
| 48 |
+
- Prioritize documents by recency or other criteria
|
| 49 |
+
- Support multilingual search (100+ languages)
|
| 50 |
+
- Process long contexts (up to 32K tokens)
|
| 51 |
|
| 52 |
## Quickstart
|
| 53 |
|
| 54 |
+
### Basic Usage
|
| 55 |
+
|
| 56 |
+
```python
|
| 57 |
+
# Choose vLLM (recommended for production) or Transformers (simpler setup)
|
| 58 |
+
# See full implementation in sections below
|
| 59 |
+
|
| 60 |
+
model_path = "ContextualAI/ctxl-rerank-v2-instruct-multilingual-2b"
|
| 61 |
+
|
| 62 |
+
query = "What are the health benefits of exercise?"
|
| 63 |
+
instruction = "Prioritize recent medical research"
|
| 64 |
+
documents = [
|
| 65 |
+
"Regular exercise reduces risk of heart disease and improves mental health.",
|
| 66 |
+
"A 2024 study shows exercise enhances cognitive function in older adults.",
|
| 67 |
+
"Ancient Greeks valued physical fitness for military training."
|
| 68 |
+
]
|
| 69 |
|
| 70 |
+
# Using vLLM (see full code below):
|
| 71 |
+
infer_w_vllm(model_path, query, instruction, documents)
|
| 72 |
+
|
| 73 |
+
# OR using Transformers (see full code below):
|
| 74 |
+
infer_w_hf(model_path, query, instruction, documents)
|
| 75 |
+
```
|
| 76 |
+
|
| 77 |
+
**Expected Output:**
|
| 78 |
+
```
|
| 79 |
+
Query: What are the health benefits of exercise?
|
| 80 |
+
Instruction: Prioritize recent medical research
|
| 81 |
+
Score: 0.8542 | Doc: A 2024 study shows exercise enhances cognitive function in older adults.
|
| 82 |
+
Score: 0.7891 | Doc: Regular exercise reduces risk of heart disease and improves mental health.
|
| 83 |
+
Score: 0.4123 | Doc: Ancient Greeks valued physical fitness for military training.
|
| 84 |
+
```
|
| 85 |
+
|
| 86 |
+
### vLLM Usage (Recommended for Production)
|
| 87 |
+
|
| 88 |
+
Requires `vllm==0.10.0` for NVFP4 or `vllm>=0.8.5` for BF16.
|
| 89 |
|
| 90 |
```python
|
| 91 |
import os
|
| 92 |
+
os.environ['VLLM_USE_V1'] = '0' # v1 engine doesn't support logits processor yet
|
| 93 |
|
| 94 |
import torch
|
| 95 |
from vllm import LLM, SamplingParams
|
|
|
|
| 149 |
print(f"Instruction: {instruction}")
|
| 150 |
for score, doc_id, doc in results:
|
| 151 |
print(f"Score: {score:.4f} | Doc: {doc}")
|
| 152 |
+
|
| 153 |
+
|
| 154 |
+
# Example usage
|
| 155 |
+
if __name__ == "__main__":
|
| 156 |
+
model_path = "ContextualAI/ctxl-rerank-v2-instruct-multilingual-2b"
|
| 157 |
+
query = "What are the health benefits of exercise?"
|
| 158 |
+
instruction = "Prioritize recent medical research"
|
| 159 |
+
documents = [
|
| 160 |
+
"Regular exercise reduces risk of heart disease and improves mental health.",
|
| 161 |
+
"A 2024 study shows exercise enhances cognitive function in older adults.",
|
| 162 |
+
"Ancient Greeks valued physical fitness for military training."
|
| 163 |
+
]
|
| 164 |
+
|
| 165 |
+
infer_w_vllm(model_path, query, instruction, documents)
|
| 166 |
```
|
| 167 |
|
| 168 |
|
| 169 |
+
### Transformers Usage (Simpler Setup)
|
| 170 |
|
| 171 |
+
Requires `transformers>=4.51.0` for BF16. Not supported for NVFP4.
|
| 172 |
|
| 173 |
```python
|
| 174 |
import torch
|
|
|
|
| 223 |
print(f"Instruction: {instruction}")
|
| 224 |
for score, doc_id, doc in results:
|
| 225 |
print(f"Score: {score:.4f} | Doc: {doc}")
|
| 226 |
+
|
| 227 |
+
|
| 228 |
+
# Example usage
|
| 229 |
+
if __name__ == "__main__":
|
| 230 |
+
model_path = "ContextualAI/ctxl-rerank-v2-instruct-multilingual-2b"
|
| 231 |
+
query = "What are the health benefits of exercise?"
|
| 232 |
+
instruction = "Prioritize recent medical research"
|
| 233 |
+
documents = [
|
| 234 |
+
"Regular exercise reduces risk of heart disease and improves mental health.",
|
| 235 |
+
"A 2024 study shows exercise enhances cognitive function in older adults.",
|
| 236 |
+
"Ancient Greeks valued physical fitness for military training."
|
| 237 |
+
]
|
| 238 |
+
|
| 239 |
+
infer_w_hf(model_path, query, instruction, documents)
|
| 240 |
```
|
| 241 |
|
| 242 |
## Citation
|
|
|
|
| 246 |
```bibtex
|
| 247 |
@misc{ctxl_rerank_v2_instruct_multilingual,
|
| 248 |
title={Contextual AI Reranker v2},
|
| 249 |
+
author={Halal, George and Agrawal, Sheshansh},
|
| 250 |
year={2025},
|
| 251 |
url={https://contextual.ai/blog/rerank-v2},
|
| 252 |
}
|
|
|
|
| 258 |
|
| 259 |
## Contact
|
| 260 |
|
| 261 |
+
For questions or issues, please open an issue on the model repository.
|