Naive RAG pipelines hit a ceiling quickly. This lesson covers techniques to push retrieval quality significantly higher: hybrid search, reranking, query expansion, and evaluation.
The basic embed-and-retrieve pipeline from Lesson 08 has predictable failure modes:
The 80/20 rule of RAG: Getting a basic pipeline working takes 20% of the effort. Getting it to work well on edge cases takes the remaining 80%.
Combine the strengths of two retrieval methods: BM25 (keyword/lexical) and dense retrieval (semantic embeddings).
Merge the two ranked lists using RRF: score(d) = sum(1 / (k + rank_i(d))) where k=60 is standard. Documents that rank high in both lists get the highest fused score.
After initial retrieval (which must be fast), use a more powerful model to reorder the top candidates for precision.
from sentence_transformers import CrossEncoder
reranker = CrossEncoder("cross-encoder/ms-marco-MiniLM-L-6-v2")
pairs = [(query, doc) for doc in candidate_docs]
scores = reranker.predict(pairs)
# Sort by score descending to get the best matches first
Improve recall by reformulating the query before retrieval.
Use an LLM to generate 2–3 alternative phrasings of the original query. Retrieve with all phrasings and merge results.
# Ask Claude to rephrase the query
expansions = client.messages.create(
model="claude-haiku-4-5", max_tokens=200,
messages=[{"role": "user", "content":
f"Generate 3 alternative phrasings of this search query. "
f"Return only the 3 queries, one per line.\n\nQuery: {query}"}]
).content[0].text.strip().split("\n")
Instead of embedding the question, ask the LLM to generate a hypothetical answer, then embed that. The hypothesis is closer in embedding space to the actual documents than the question is.
Trade-off: Both techniques add an LLM call before retrieval, increasing latency and cost. Use them when recall is more important than speed.
How do you know if your RAG pipeline is actually good? RAGAS (Retrieval Augmented Generation Assessment) defines four key metrics:
| Metric | Measures | How |
|---|---|---|
| Faithfulness | Is the answer grounded in the retrieved context? | LLM checks each claim against context |
| Answer Relevancy | Does the answer address the question? | Generate questions from the answer; compare to original |
| Context Precision | Are the retrieved docs actually relevant? | LLM rates each retrieved chunk |
| Context Recall | Did we retrieve all the needed information? | Compare retrieved context to ground truth answer |
LLM-as-judge: All RAGAS metrics use an LLM to evaluate. This is cost-effective and correlates well with human judgement for most use cases.
Live demonstration: upgrade the Lesson 08 pipeline with hybrid search and reranking.