6500: MLOps Pipelines for RAG - Practice¶
Exercises¶
Exercise 1: Build RAG Evaluation Pipeline¶
Build a RAG evaluation harness around RAGAS: assemble a Dataset of question, generated answer, retrieved contexts, and ground truth through mock retriever and generator classes, then run faithfulness and answer relevancy scoring - noting both are LLM-judged metrics that require a judge API key.
Attempt it yourself first — the full worked answer is in the Answer Key at the end of this file.
Exercise 2: A/B Testing Framework¶
Implement an A/B testing class for two RAG systems: run both on a query set, score each answer with simple quality metrics, record a winner per query, and summarize win rates across the whole test.
Attempt it yourself first — the full worked answer is in the Answer Key at the end of this file.
Exercise 3: CI/CD for RAG Pipeline¶
Write a GitHub Actions workflow in YAML that runs on every push and pull request: sync dependencies from a committed lockfile with uv, run unit and integration test suites with coverage, execute a RAG evaluation script against a fixed eval set, and upload the coverage report to Codecov.
Attempt it yourself first — the full worked answer is in the Answer Key at the end of this file.
Exercise 4: Monitoring RAG Performance¶
Instrument a RAG pipeline with Prometheus: define a request counter, latency histograms for retrieval, generation, and the total, a retrieved-docs histogram, and gauges for faithfulness and relevancy, then wrap the pipeline so every request times its stages and records all metrics.
Attempt it yourself first — the full worked answer is in the Answer Key at the end of this file.
Exercise 5: Model Registry for RAG Components¶
Build an MLflow-backed registry for RAG components: log retriever and generator runs with params and metrics under registered model names, and provide a loader that reconstructs the pipeline from two pinned versions through their models:/ URIs.
Attempt it yourself first — the full worked answer is in the Answer Key at the end of this file.
Exercise 6: Canary Deployment¶
Implement a canary deployment router: send a configurable percentage of traffic to the canary model and the rest to production, tag every response with its source, and produce a metrics comparison - latency, quality, error rate - between the two models.
Attempt it yourself first — the full worked answer is in the Answer Key at the end of this file.
Exercise 7: Automated Retraining Pipeline¶
Write an automated retraining pipeline: compare current faithfulness and relevancy against their thresholds independently, fetch fresh data and retrain the affected component when a metric drops, log the new model, and deploy only when retraining actually occurred.
Attempt it yourself first — the full worked answer is in the Answer Key at the end of this file.
Answer Key¶
Full solutions for every exercise above. Work through your own attempt before comparing.
Exercise 1: Build RAG Evaluation Pipeline¶
from datasets import Dataset
from ragas import evaluate
from ragas.metrics import faithfulness, answer_relevancy
# NOTE: faithfulness and answer_relevancy are LLM-judged metrics -
# evaluate() falls back to a default OpenAI judge, so an
# OPENAI_API_KEY is required. The lowercase imports are the legacy
# style (supported through v0.3); newer versions prefer explicit
# classes like Faithfulness(llm=your_judge_llm).
def create_evaluation_dataset(queries, retriever, generator):
"""Create dataset for RAG evaluation."""
evaluation_data = []
for query in queries:
# Retrieve context
contexts = retriever.retrieve(query, top_k=3)
context_texts = [c["document"] for c in contexts]
# Generate answer
answer = generator.generate(query, context_texts)
# Simple ground truth (in practice, use human annotations)
ground_truth = f"Answer based on: {context_texts[0][:50]}..."
evaluation_data.append({
"question": query,
"answer": answer,
"contexts": context_texts,
"ground_truth": ground_truth,
})
return Dataset.from_list(evaluation_data)
# Mock retriever and generator for testing
class MockRetriever:
def retrieve(self, query, top_k=3):
return [{"document": f"Context for {query}"}] * top_k
class MockGenerator:
def generate(self, query, contexts):
return f"Answer to '{query}' based on provided context."
# Run evaluation
test_queries = [
"What is machine learning?",
"How do neural networks work?",
"Explain transformers architecture.",
]
print("Creating evaluation dataset...")
dataset = create_evaluation_dataset(
test_queries,
MockRetriever(),
MockGenerator()
)
print(f"Dataset created with {len(dataset)} examples")
print("\nRunning RAGAS evaluation...")
result = evaluate(
dataset,
metrics=[
faithfulness,
answer_relevancy,
]
)
print(f"\nEvaluation Results:")
for metric_name, score in result.items():
print(f" {metric_name}: {score:.3f}")
# Expected output:
# - Faithfulness score: 0.7-0.9 (higher is better)
# - Answer relevancy: 0.6-0.8 (higher is better)
# - Metrics measure RAG system quality
Exercise 2: A/B Testing Framework¶
class RAGABTest:
def __init__(self, system_a, system_b):
self.system_a = system_a
self.system_b = system_b
self.results = []
def run_test(self, queries):
"""Run A/B test on queries."""
for query in queries:
# Get answers from both systems
answer_a = self.system_a.query(query)
answer_b = self.system_b.query(query)
# Compare metrics
metrics_a = self.evaluate_answer(query, answer_a)
metrics_b = self.evaluate_answer(query, answer_b)
winner = "a" if metrics_a["score"] > metrics_b["score"] else "b"
self.results.append({
"query": query,
"system_a": metrics_a,
"system_b": metrics_b,
"winner": winner,
})
return self.results
def evaluate_answer(self, query, answer):
"""Evaluate answer quality."""
# Simple evaluation metrics
return {
"length": len(answer.get("text", "")),
"has_context": "context" in answer.get("text", "").lower(),
"latency": answer.get("latency", 0),
"score": len(answer.get("text", "")) / 100, # Simple score
}
def summarize_results(self):
"""Summarize A/B test results."""
wins_a = sum(1 for r in self.results if r["winner"] == "a")
wins_b = sum(1 for r in self.results if r["winner"] == "b")
return {
"system_a_wins": wins_a,
"system_b_wins": wins_b,
"win_rate_a": wins_a / len(self.results),
"win_rate_b": wins_b / len(self.results),
}
# Test
class MockRAGSystem:
def __init__(self, name):
self.name = name
def query(self, query):
return {"text": f"Answer from {self.name} for: {query}", "latency": 0.1}
ab_test = RAGABTest(
MockRAGSystem("SystemA"),
MockRAGSystem("SystemB")
)
results = ab_test.run_test(test_queries)
summary = ab_test.summarize_results()
print("A/B Test Results:")
print(f" System A wins: {summary['system_a_wins']}")
print(f" System B wins: {summary['system_b_wins']}")
print(f" System A win rate: {summary['win_rate_a']:.1%}")
# Expected output:
# - Statistical comparison of two RAG systems
# - Win rate indicates which system performs better
# - Use for validating model changes
Exercise 3: CI/CD for RAG Pipeline¶
# .github/workflows/rag-ci.yml
name: RAG Pipeline CI
on: [push, pull_request]
jobs:
test:
runs-on: ubuntu-latest
steps:
- uses: astral-sh/setup-uv@v10.2.0
- name: Install dependencies
# The repo commits pyproject.toml + uv.lock (uv init --bare + uv add).
run: uv sync --locked # dev group installs by default, so pytest rides along
- name: Unit tests
run: uv run pytest tests/unit/ --cov=rag_pipeline
- name: Integration tests
run: uv run pytest tests/integration/
- name: RAG evaluation
run: |
python scripts/evaluate_rag.py \
--retriever-config config/retriever.yaml \
--generator-config config/generator.yaml \
--eval-data data/eval_set.json
- name: Upload coverage
uses: codecov/codecov-action@v7
# Expected usage:
# - Runs on every push/PR
# - Unit tests verify code correctness
# - Integration tests verify end-to-end functionality
# - RAG evaluation monitors quality metrics
Exercise 4: Monitoring RAG Performance¶
from prometheus_client import Counter, Histogram, Gauge
class RAGMetrics:
def __init__(self):
# Define metrics
self.requests_total = Counter('rag_requests_total', 'Total RAG requests')
self.retrieval_latency = Histogram('rag_retrieval_latency_seconds', 'Retrieval latency')
self.generation_latency = Histogram('rag_generation_latency_seconds', 'Generation latency')
self.total_latency = Histogram('rag_total_latency_seconds', 'Total latency')
self.retrieved_docs = Histogram('rag_retrieved_docs', 'Documents retrieved')
self.faithfulness_score = Gauge('rag_faithfulness_score', 'Answer faithfulness')
self.relevancy_score = Gauge('rag_relevancy_score', 'Answer relevancy')
def record_request(self, retrieval_time, generation_time, num_docs, faithfulness, relevancy):
"""Record metrics for a request."""
self.requests_total.inc()
self.retrieval_latency.observe(retrieval_time)
self.generation_latency.observe(generation_time)
self.total_latency.observe(retrieval_time + generation_time)
self.retrieved_docs.observe(num_docs)
self.faithfulness_score.set(faithfulness)
self.relevancy_score.set(relevancy)
# Use in RAG pipeline
metrics = RAGMetrics()
def rag_pipeline_with_metrics(query, retriever, generator):
import time
start_retrieve = time.time()
contexts = retriever.retrieve(query, top_k=3)
retrieval_time = time.time() - start_retrieve
start_generate = time.time()
answer = generator.generate(query, contexts)
generation_time = time.time() - start_generate
# Evaluate quality (simplified)
faithfulness = 0.85 # In practice, calculate with RAGAS
relevancy = 0.78
# Record metrics
metrics.record_request(
retrieval_time,
generation_time,
len(contexts),
faithfulness,
relevancy,
)
return answer
# Test
print("Testing RAG pipeline with metrics...")
answer = rag_pipeline_with_metrics(
"What is RAG?",
MockRetriever(),
MockGenerator()
)
print(f"Answer: {answer}")
print("Metrics recorded to Prometheus")
# Expected output:
# - Metrics exposed at /metrics endpoint
# - Grafana dashboards visualize performance
# - Alerts on threshold violations
Exercise 5: Model Registry for RAG Components¶
import mlflow
class RAGModelRegistry:
def __init__(self, tracking_uri):
mlflow.set_tracking_uri(tracking_uri)
def log_retriever(self, retriever, config, metrics):
"""Log retriever model to registry."""
with mlflow.start_run(run_name="retriever"):
mlflow.log_params(config)
mlflow.log_metrics(metrics)
# Log model (simplified)
mlflow.sklearn.log_model(
retriever,
"retriever",
registered_model_name="rag_retriever",
)
def log_generator(self, generator, config, metrics):
"""Log generator model to registry."""
with mlflow.start_run(run_name="generator"):
mlflow.log_params(config)
mlflow.log_metrics(metrics)
mlflow.transformers.log_model(
generator,
"generator",
registered_model_name="rag_generator",
)
def load_rag_pipeline(self, retriever_version, generator_version):
"""Load RAG pipeline from registry."""
import mlflow
retriever_uri = f"models:/rag_retriever/{retriever_version}"
generator_uri = f"models:/rag_generator/{generator_version}"
retriever = mlflow.sklearn.load_model(retriever_uri)
generator = mlflow.transformers.load_model(generator_uri)
return {"retriever": retriever, "generator": generator}
# Test
registry = RAGModelRegistry("sqlite:///mlflow.db")
# NOTE: log_model needs real artifacts - mlflow.sklearn requires a
# scikit-learn estimator and mlflow.transformers a transformers model
# or pipeline. The mocks cannot be serialized with those flavors, so
# the calls stay commented here and run unchanged against real
# components.
print("Logging retriever model (skipped with mocks)...")
# registry.log_retriever(
# retriever, # a real sklearn-compatible retriever
# {"top_k": 3, "embedding_model": "miniLM"},
# {"precision": 0.85, "recall": 0.78},
# )
print("Logging generator model (skipped with mocks)...")
# registry.log_generator(
# generator, # a real transformers pipeline/model
# {"model": "gpt-4", "temperature": 0.7},
# {"latency": 0.5, "quality": 0.9},
# )
print("Registry demo complete - params/metrics logging and the")
print("models:/ URI loading shown above run with real artifacts")
# Expected output:
# - Models versioned and tracked
# - Easy rollback to previous versions
# - Metadata and metrics preserved
Exercise 6: Canary Deployment¶
import random
class CanaryDeployment:
def __init__(self, production_model, canary_model, canary_percentage=0.1):
self.production_model = production_model
self.canary_model = canary_model
self.canary_percentage = canary_percentage
def query(self, query):
"""Route query to production or canary."""
# Route based on percentage
if random.random() < self.canary_percentage:
response = self.canary_model.query(query)
source = "canary"
else:
response = self.production_model.query(query)
source = "production"
response["model_source"] = source
return response
def compare_metrics(self):
"""Compare metrics between production and canary."""
# Simulated metrics
production_metrics = {
"avg_latency": 0.45,
"avg_quality": 0.87,
"error_rate": 0.01,
}
canary_metrics = {
"avg_latency": 0.42,
"avg_quality": 0.91,
"error_rate": 0.008,
}
comparison = {
"latency": {
"production": production_metrics["avg_latency"],
"canary": canary_metrics["avg_latency"],
"diff": canary_metrics["avg_latency"] - production_metrics["avg_latency"],
},
"quality": {
"production": production_metrics["avg_quality"],
"canary": canary_metrics["avg_quality"],
"diff": canary_metrics["avg_quality"] - production_metrics["avg_quality"],
},
"error_rate": {
"production": production_metrics["error_rate"],
"canary": canary_metrics["error_rate"],
"diff": canary_metrics["error_rate"] - production_metrics["error_rate"],
},
}
return comparison
# Evaluate canary promotion
def evaluate_canary_promotion(canary_deployment, threshold=0.05):
"""Decide whether to promote canary to production."""
comparison = canary_deployment.compare_metrics()
# Promote if latency is not materially worse (threshold is in
# seconds), quality improves, and the error rate does not regress.
# Ignoring error_rate here would promote a canary that fails more
# often, as long as it is fast and fluent.
if (
comparison["latency"]["diff"] < threshold and
comparison["quality"]["diff"] > 0 and
comparison["error_rate"]["diff"] <= 0
):
return True, "Canary performs better - promote"
else:
return False, "Canary not ready - keep in canary"
# Test
canary = CanaryDeployment(
MockRAGSystem("production"),
MockRAGSystem("canary"),
canary_percentage=0.2
)
print(f"Routing queries: {canary.canary_percentage:.0%} to canary")
for i in range(10):
result = canary.query(f"Query {i}")
print(f" Query {i}: {result['model_source']}")
# Check promotion
should_promote, reason = evaluate_canary_promotion(canary)
print(f"\nPromote canary: {should_promote}")
print(f"Reason: {reason}")
# Expected output:
# - 20% of queries go to canary
# - Gradual rollout reduces risk
# - Promote if metrics improve
Exercise 7: Automated Retraining Pipeline¶
def retrain_pipeline():
"""Automated retraining pipeline for RAG components."""
# Check if retraining is needed
current_metrics = {
"faithfulness": 0.75,
"relevancy": 0.68,
}
threshold_metrics = {
"faithfulness": 0.80,
"relevancy": 0.75,
}
print("Checking if retraining is needed...")
needs_retrain = False
# Each metric is checked independently: a relevancy drop alone must
# also trigger retraining, not just a faithfulness drop.
if current_metrics["faithfulness"] < threshold_metrics["faithfulness"]:
print("Faithfulness below threshold - triggering retraining")
needs_retrain = True
# Fetch new data
new_data = [{"query": f"Query {i}", "context": f"Context {i}"} for i in range(100)]
# Retrain retriever
print("Retraining retriever...")
new_retriever = MockRetriever() # In practice, actual training
retriever_metrics = {"precision": 0.88, "recall": 0.82}
# Log new model
print("Logging new retriever model...")
# registry.log_retriever(new_retriever, config, retriever_metrics)
if current_metrics["relevancy"] < threshold_metrics["relevancy"]:
print("Relevancy below threshold - retraining generator")
needs_retrain = True
new_generator = MockGenerator()
generator_metrics = {"latency": 0.4, "quality": 0.92}
# registry.log_generator(new_generator, config, generator_metrics)
if needs_retrain:
print("Deploying new models...")
# deploy_new_models(...)
print("Retraining complete - new models deployed")
else:
print("Metrics within thresholds - no retraining needed")
# Test
print("Running automated retraining check...")
retrain_pipeline()
# Schedule with cron (commented example):
# 0 2 * * * python scripts/retrain_pipeline.py
# Expected output:
# - Automated quality monitoring
# - Triggers retraining when metrics degrade
# - Deploys new models automatically
# - Scheduled to run periodically