zucchetti-llm-course

Lab 04: Query Router for Multi-Source RAG

Lecture: 5, Advanced RAG and Intelligent Routing Duration: 30 minutes Difficulty: Intermediate

Objectives

Prerequisites

Requirement How to Check
HR policy documents Read from lab_07_evaluation/data/policies/
Dependencies pip install openai chromadb sentence-transformers rich python-dotenv
LLM access Ollama or DeepSeek API key

Quick Start

# Create the employee database (auto-runs if missing)
cd lab_04_query_router
python setup_db.py

# Run the lab
python lab_04_query_router.py

Files

File Description
lab_04_query_router.py Main router script with TODO section
setup_db.py Creates the SQLite employee database
solutions.py Embedding-based classification solution
data/employees.db 50 Italian employee records

Tasks

Task 1: Understand the Data Sources

Two sources are available:

Task 2: Implement Query Classification

Replace the keyword-based classify_query() with embedding-based classification.

Task 3: Test with Mixed Queries

Watch how the router handles SQL vs. policy questions. Notice error handling when SQL generation fails.

Task 4: Interactive Testing

Try ambiguous questions like β€œCan a new employee work remotely?” β€” does it route correctly?

Expected Output

Q1: How many employees work in Engineering?
  Route: sql
  SQL: SELECT COUNT(*) FROM employees WHERE department = 'Engineering'
  Answer: There are 12 employees in the Engineering department.

Q4: What is the remote work policy?
  Route: vector
  Answer: According to the Remote Work Policy, eligible employees may
  work remotely up to 3 days per week...

πŸ†˜ Troubleshooting

Issue Solution
Policy docs not found Check that lab_07_evaluation/data/policies/ is in your clone
SQL errors The script auto-retries with error context
Wrong routing Improve the classify_query() function β€” see solutions.py