RAG Optimization · Module 04
Re-ranking
What to do with the retrieved passages before the model sees them: cross-encoder re-ranking, MMR for diversity, compression and ordering.
Học xong bạn làm được
- Explain why a cross-encoder ranks better than a bi-encoder
- Apply Maximal Marginal Relevance to diversify results
- Compress retrieved context
- Order passages to avoid the lost-in-the-middle problem
- Decide when LLM re-ranking is worth its cost
Bài học
- Re-ranking: Cross-Encoder
The top-K list from retrieval is not good enough to feed the LLM directly - embeddings trade semantic accuracy for speed, and keyword matches bring noise. Re-ranking reads a small candidate set carefully. A bi-encoder encodes question and document separately; a cross-encoder reads them together with full self-attention - accurate but slow, so it is used in a funnel.
- Maximal Marginal Relevance (MMR)
The most similar documents are not always the best set - five near-identical paragraphs waste the context window. MMR balances relevance to the question with diversity from what is already selected, controlled by lambda (usually 0.5). Use a cross-encoder for precise answers to hard questions, MMR for broad answers covering many aspects.
- Additional Post-Retrieval Techniques: Contextual Compression and Lost in the Middle
Contextual compression sends the LLM only the relevant parts of retrieved documents, by extracting sentences or by summarising with a small model. LLMs attend most to the beginning and end of the context and lose the middle (Liu et al., 2023); U-shape reordering, fewer documents and compression before stuffing mitigate it.
- LLM-Based Re-ranking
An LLM can be used as the re-ranker, instead of or as well as a cross-encoder. It follows complex instructions, can explain its ranking and needs no specially trained model, but it costs much more per document and is slower, so it suits small candidate sets of the top 10 to 20.
- Assignment: Post-Retrieval Processing
Re-ranking with Cross-Encoder and Maximal Marginal Relevance