Module 3 · Advanced retrieval
Re-ranking with cross-encoders
6 min read
After hybrid search, almost all of Prism's right passages are in the top five results — but not always first (MRR 0.82). Yet the model pays more attention to the first passages, and reducing k to save context cuts off those ranked low. Re-ranking rereads each candidate together with the question and re-orders them finely. On Prism, it is the technique that takes MRR from 0.82 to 0.97.
Bi-encoder and cross-encoder
An embedding model is a bi-encoder: it encodes the question and each passage separately, then compares two vectors. That is what makes it fast — passages are embedded once and for all — but it compares two frozen summaries, without ever seeing the question and the passage together.
A cross-encoder takes the (question, passage) pair as input and directly produces a relevance score. It sees fine-grained interactions: negation, a precise reference, a passage on the same topic that does not answer the question. It is much more accurate, but it has to run for every pair, on every query: impossible over the whole corpus.
Hence the universal two-stage pattern:
question ─► recherche hybride (rapide) ─► 20 à 100 candidats
─► cross-encodeur (lent, précis) ─► 5 meilleurs
(Question → fast hybrid search → 20 to 100 candidates → slow, accurate cross-encoder → 5 best.)
In Prism
The course's re-ranker is a multilingual cross-encoder run locally with FastEmbed:
class CrossEncoderReranker:
def __init__(self, model: str = config.RERANK_MODEL) -> None:
from fastembed.rerank.cross_encoder import TextCrossEncoder
self.name = model
self._model = TextCrossEncoder(model, cache_dir=str(config.CACHE_DIR / "models"))
def rerank(self, query: str, hits: list[Hit], k: int = 5) -> list[Hit]:
scores = list(self._model.rerank(query, [h.chunk.indexed_text() for h in hits]))
ranked = sorted(zip(hits, scores), key=lambda x: x[1], reverse=True)[:k]
return [Hit(h.chunk, float(s)) for h, s in ranked]
And its use:
configs["Hybride + re-ranking"] = lambda q: reranker.rerank(q, hybrid.search(q, 20), 10)
The results
| Configuration | Recall@5 | MRR | Latency per query (CPU) |
|---|---|---|---|
| Dense | 0.90 | 0.82 | < 1 ms (embeddings cached) |
| Hybrid (RRF) | 0.99 | 0.82 | < 1 ms |
| Dense + re-ranking | 1.00 | 0.97 | ≈ 720 ms |
| Hybrid + re-ranking | 1.00 | 0.97 | ≈ 720 ms |
Two findings. Re-ranking puts the right article first in almost every case, including for English questions. And it costs: about 0.7 seconds per query for 20 candidates on a laptop CPU — the search itself is negligible by comparison.
How many candidates?
The number of candidates sets the quality/latency trade-off:
| Candidates re-ranked | Recall@5 | MRR | Latency per query (CPU) |
|---|---|---|---|
| 5 | 0.97 | 0.99 | ≈ 160 ms |
| 10 | 1.00 | 0.97 | ≈ 340 ms |
| 20 | 1.00 | 0.97 | ≈ 750 ms |
| 40 | 1.00 | 0.97 | ≈ 840 ms |
Latency grows with the number of candidates (the last row grows less, because the model processes pairs in batches). With 5 candidates, the re-ranker cannot rescue what the search ranked sixth: recall drops. On this small corpus, 10 candidates are enough; on a real corpus, 50 to 100 are often re-ranked, which requires a GPU or a re-ranking service to stay under a second.
The re-ranker's score, a useful signal
A cross-encoder's scores are more interpretable than cosine similarities. On Prism, out-of-scope questions all get a negative best score (from −1.0 to −2.0), while most covered questions get a positive score:
| Question | Best dense score | Best re-ranker score |
|---|---|---|
| What is the warranty period of the Halo lamp? | 0.82 | 1.86 |
| How much does express delivery cost? | 0.82 | 0.61 |
| How long does a refund take after a return? | 0.74 | −0.76 |
| Do you sell 3D printers? (out of scope) | 0.27 | −1.27 |
| What are the opening hours of your Paris store? (out of scope) | 0.56 | −1.76 |
Dense similarity separates nothing: 0.56 for an out-of-scope question, 0.51 for a covered one. The re-ranker's score separates better, without being perfect (−0.76 for a covered question). A threshold, tuned on your evaluation set, lets you detect questions with no answer in the database — failure point #1 — and answer "I don't know" or hand over to a human. That threshold is specific to the model: it must be recalibrated with every change.
Re-ranking options
| Option | Advantages | Drawbacks |
|---|---|---|
| Local cross-encoder (FastEmbed, Sentence Transformers) | Free, data stays with you | High latency without a GPU |
| Re-ranking API (Cohere, Voyage AI, Jina…) | Fast, very good models | Cost per query, data transmitted |
| LLM as re-ranker | Very flexible (business criteria in the prompt) | Slowest and most expensive |
| Late interaction (ColBERT) | Between bi- and cross-encoder: per-token vectors, precomputed | Much heavier storage |
Choose a multilingual model if your corpus or your questions are not in English: an English-only re-ranker can degrade results in French instead of improving them.
Key takeaways
- A bi-encoder compares two vectors computed separately; a cross-encoder reads the pair, more accurate but too slow for the whole corpus.
- Pattern: broad (hybrid) retrieval, re-rank the candidates, keep the best.
- On Prism: MRR from 0.82 to 0.97, for about 0.7 s per query on CPU with 20 candidates.
- The number of candidates sets the trade-off: too few limits recall, too many increases latency.
- The re-ranker's score helps detect out-of-scope questions, with a threshold calibrated on your data.
Practice
- Measure re-ranking latency with 10, 20 and 50 candidates on your machine.
- Choose a re-ranker score threshold that rejects Prism's 5 out-of-scope questions: how many covered questions does it wrongly reject?
- Replace the re-ranker with
OverlapReranker(the fake model): what happens to recall and MRR?