#123Cross-Encoder Rerank Top-kEasy

Cross-Encoder Rerank Top-k

Background

After bi-encoder retrieval returns the top-NN (say 50 candidates), a cross-encoder reranker scores each (query, candidate) pair more accurately. The pipeline then takes the top-kk 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-kk.

Input

  • candidates - sequence of items (any type).
  • rerank_scores - parallel array of floats.
  • k - int, cutoff.

Output

  • list of the top-kk 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).
  • kk 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