#123Cross-Encoder Rerank Top-kEasyLLMsML System DesignAsked atCohere · Anthropic · OpenAI
Cross-Encoder Rerank Top-k
Background
After bi-encoder retrieval returns the top- (say 50 candidates), a cross-encoder reranker scores each (query, candidate) pair more accurately. The pipeline then takes the top- by rerank score - typically 5-10 chunks for RAG. This is the simple selection step, not the cross-encoder itself.
Problem statement
Implement rerank_topk(candidates, rerank_scores, k). Reorder candidates by the parallel rerank_scores (descending) and return the top-.
Input
candidates- sequence of items (any type).rerank_scores- parallel array of floats.k-int, cutoff.
Output
listof the top- candidates in rerank order.
Examples
Input: candidates=["a","b","c"], rerank_scores=[0.1, 0.9, 0.5], k=2
Output: ["b", "c"]
Constraints
- Lengths must match; ValueError otherwise.
- Tie-break by lower original index (stable sort).
- may exceed
len(candidates).
Notes
- Two-stage retrieval. Bi-encoder for speed at scale, cross-encoder for quality on the shortlist. Top-k after rerank is what enters the LLM context.
Python
Loading...
▶ Run executes the 3 visible sample tests below in your browser. Submit runs the full suite — including hidden tests — on the server for an official verdict.
- •Example: top-2 by rerank score
- •Reference: k exceeds length -> full reordered list
- •Sample: strictly descending with negative scores