AI Engineer Portal
Your personal operating system for career transition.
Exercise
Build a multi-stage RAG pipeline with query routing
Build a Multi-Stage RAG Pipeline with Query Routing
Not all queries are the same. "What is our refund policy?" is a simple factual lookup. "Compare our Q3 2023 and Q4 2023 revenue performance" requires retrieving from multiple documents and synthesizing across them. Using the same retrieval pipeline for both wastes resources and produces worse answers for synthesis queries.
Query routing classifies incoming queries and directs them to specialized retrieval strategies optimized for each query type.
What you are building
Create a RoutedRAGPipeline with:
- A query classifier — injectable function that returns a
QueryTypeenum:FACTUAL,SYNTHESIS, orOUT_OF_SCOPE. - Factual retrieval — retrieves top-3 chunks for precision-optimized answering.
- Synthesis retrieval — retrieves top-8 chunks, groups by
sourcemetadata field, deduplicates within each source, returns a multi-source context map for comparative answering. - Out-of-scope handling — returns a canned response without any retrieval or LLM calls.
- Context formatting — factual uses a numbered list with source labels; synthesis uses per-source block headers (
### Source: {name}). - Returns a
RoutedResponsewith: answer, query_type, routing_reason, chunks_used, and latency breakdown.
Constraints
- Injectable classifier and retrieve functions.
QueryTypeas a Python Enum. Type-hint everything.
Retrieval / hard / Step 15 of 15
Retrieval quality and ranking
Do not trust a single score blindly. Combine ranking logic with metadata and think about why a candidate deserves to rise.
This is the end of the current mini-sequence.
- - Ranking rule is explainable
- - Supports future iteration without rewriting everything
- - Feels tied to product trust rather than pure math trivia
- - Did I account for both relevance and metadata?
- - Would I be able to explain the ranking rule in an interview?
- - Does the code make future tuning easy?
Practice
Generate a variation
Generate a new exercise variation to deepen understanding or practice a related concept.
Attempt history
Recent submissions
Before you submit, decide what a strong answer should make obvious to the reviewer.
No attempts yet.