- The vector database performs the initial similarity-based retrieval (fast approximate nearest neighbors on embeddings).
- The re-ranker refines those initial results by applying more accurate relevance criteria. Typical re-rankers use cross-attention or cross-encoder architectures that jointly consider the query and each candidate document to compute a more accurate relevance score than simple embedding similarity.
- Chunking prepares long documents into pieces (chunks) so they fit model context windows and produce focused embeddings.
- Tokenizers convert raw text into tokens appropriate for the embedding model or language model.
Quick flow (step-by-step)
- The user asks a question.
- The question is converted into an embedding that encodes its semantic meaning.
- The vector database is searched using that embedding to retrieve the top-N candidate documents or chunks (initial retrieval).
- The candidate documents are passed to a re-ranker which compares the original query against each candidate (typically using a cross-encoder or an LLM scoring method).
- The re-ranker scores and reorders the candidates to place the most relevant items at the top.
- The top-ranked documents are returned to the RAG pipeline (for example, used as context for a generative model).
Component responsibilities and when to use them
Why re-ranking improves RAG quality
- Embedding similarity methods are efficient but often approximate — they evaluate query-to-document similarity in a single vector space and can miss fine-grained signals.
- Re-rankers use models that jointly encode the query and document (cross-encoders) or use LLM-based scoring to capture contextual interactions, improving ordering and final relevance.
- Because re-rankers are more expensive, the typical pattern is: run a high-recall initial search (e.g., top 50–200) then re-rank to a much smaller set (e.g., top 3–10).
Example retrieval + re-ranking pseudocode
Practical notes, patterns, and tuning tips
- Typical pattern: perform a relatively high-recall initial search (top 50–200) and re-rank to a small curated set (top 3–10).
- Re-rankers add latency and cost; apply them only to the limited candidate set from the vector DB.
- Choose initial_k and final_k to balance recall (ensure the right documents are in the candidate set) and precision (final ordering).
- Hybrid strategies: combine lexical filters (BM25) with vector search to improve recall for rare or exact-match queries.
- If latency is critical, consider lightweight neural re-rankers or distilling a re-ranker for faster inference.
Common re-ranker approaches
- Cross-encoder models (e.g., BERT cross-encoders): jointly encode query and candidate text with cross-attention and output a scalar relevance score.
- LLM-based scoring: prompt an LLM to rate relevance or compute a scalar score via structured prompt engineering.
- Supervised neural rankers: fine-tune models on labeled relevance data for domain-specific improvements.
- Lightweight or distilled rankers: compressed models that approximate cross-encoder behavior with lower latency.
When to re-rank vs. when not to
- Use re-ranking when final answer quality and precision are important (e.g., legal or medical retrieval, high-stakes Q&A).
- Skip re-ranking when you need minimal latency and can tolerate lower precision (e.g., broad search UIs where approximate ordering is acceptable).
Re-rankers improve final result quality by using models that directly compare query and document text (rather than relying solely on vector similarity). Because they are costlier, they are typically run on a smaller candidate set returned by the vector database.
Links and references
- Retrieval-Augmented Generation (RAG) overview
- Cross-encoder ranking (example BERT cross-encoders)
- Vector databases and ANN search — FAISS, Annoy, Milvus
- Tokenization basics and why it matters