Appearance
Module 4.5: The RAG Triad & Evaluation Engineering β
Curriculum Alignment:
docs/plan/04_phase4_context_engineering_agentic_rag.md
Topic Scope: The RAG Triad (Faithfulness, Context Precision, Answer Relevance), Ragas, DeepEval, Offline Pytest Suites
Level: Advanced AI Engineering / Architecture
1. Moving Beyond "Vibe Checks": Scientific Evaluation β
In early RAG experiments, developers typically test a handful of queries manually, look at the answer, and conclude "it looks good". In production, this "vibe check" approach is disastrous:
- A slight change to chunk size or embedding model can silently degrade retrieval precision across thousands of edge cases.
- Prompt tweaks intended to improve formatting can trigger subtle hallucinations that evade manual inspection.
To engineer reliable RAG systems, we apply quantitative metric frameworks (Ragas and DeepEval) anchored on The RAG Triad:
text
ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
β THE RAG TRIAD EVALUATION FRAMEWORK β
ββββββββββββββββββββββ¬βββββββββββββββββββββ¬βββββββββββββββββββββββββββββββ€
β METRIC β EVALUATION TARGET β FORMULA / CONCEPT β
ββββββββββββββββββββββΌβββββββββββββββββββββΌβββββββββββββββββββββββββββββββ€
β 1. Context β Retriever Quality β What % of retrieved chunks β
β Precision β (Signal vs Noise) β are actually relevant? β
ββββββββββββββββββββββΌβββββββββββββββββββββΌβββββββββββββββββββββββββββββββ€
β 2. Answer β Generator Groundingβ Is every claim in the answer β
β Faithfulness β (Zero Hallucinationβ directly supported by the β
β β Rate) β retrieved context? β
ββββββββββββββββββββββΌβββββββββββββββββββββΌβββββββββββββββββββββββββββββββ€
β 3. Answer β Generator Utility β Does the answer directly and β
β Relevance β (Query Alignment) β completely address the user'sβ
β β β question? β
ββββββββββββββββββββββ΄βββββββββββββββββββββ΄βββββββββββββββββββββββββββββββ2. Mathematical Formulations of the Triad β
1. Answer Faithfulness (Grounding Score) β
Evaluates whether the generator stayed strictly within the facts present in the retrieved context:
A score of
2. Context Precision β
Measures whether the relevant information was ranked at the very top of the retrieved chunks (rewarding high rank order to mitigate "Lost in the Middle"):
3. Answer Relevance β
Measures whether the answer answered what was actually asked. Evaluated by prompting an evaluator model to generate questions from the generated answer and computing semantic cosine similarity between the generated questions and the original user question:
3. Automated Offline Evaluation with pytest and Ragas β
In production CI/CD pipelines, RAG systems must be guarded by automated regression test suites. Before merging a new chunking strategy, re-ranker, or system prompt, automated tests score the pipeline against a versioned gold-standard dataset:
python
"""
Automated RAG Triad Evaluation Suite using Pytest & Ragas
Validates that pipeline changes do not regress Faithfulness below 0.90.
"""
import pytest
from pydantic import BaseModel
from ragas import evaluate
from ragas.metrics import faithfulness, answer_relevance, context_precision
from datasets import Dataset
# Versioned Gold-Standard Evaluation Dataset
EVAL_DATASET = {
"question": [
"What is the maximum allowed replication lag for the payment database?",
"How is disaster recovery failover initiated in us-west-2?"
],
"ground_truth": [
"The maximum allowed replication lag is 500 milliseconds before alerting.",
"Disaster recovery failover is initiated via the runbook script 'dr-failover.sh' with senior SRE authorization."
]
}
def execute_rag_pipeline(question: str) -> dict[str, Any]:
"""Runs the production RAG pipeline and returns answer + contexts."""
# Simulated return from production pipeline
return {
"answer": "The maximum allowed replication lag is 500ms before P1 alerts fire.",
"contexts": [
"Section 4.2 Database SLAs: The maximum allowed replication lag for the payment cluster is 500 milliseconds."
]
}
@pytest.mark.rag_eval
def test_rag_pipeline_quality_standards():
"""CI/CD Gate: Verifies that RAG pipeline meets minimum production thresholds."""
questions = EVAL_DATASET["question"]
ground_truths = EVAL_DATASET["ground_truth"]
answers = []
contexts = []
for q in questions:
res = execute_rag_pipeline(q)
answers.append(res["answer"])
contexts.append(res["contexts"])
dataset = Dataset.from_dict({
"question": questions,
"answer": answers,
"contexts": contexts,
"ground_truth": ground_truths
})
# Run quantitative evaluation across the RAG Triad
score = evaluate(
dataset=dataset,
metrics=[faithfulness, answer_relevance, context_precision]
)
# Assert Strict Production Quality Gates
assert score["faithfulness"] >= 0.90, f"Faithfulness regressed: {score['faithfulness']}"
assert score["answer_relevance"] >= 0.85, f"Relevance regressed: {score['answer_relevance']}"
assert score["context_precision"] >= 0.80, f"Precision regressed: {score['context_precision']}"Conceptual Mindmap: RAG Triad & Evaluation β
4. Curated Reading & Canonical References β
| Resource | Canonical Reference & Link | Specific Focus Areas |
|---|---|---|
| Primary Curriculum Book | AI Agents and Applications (Google Drive) | Part 4, Chapter 14, Section 14.3.4 (pp. 374β375): "Evaluation of AI agents and applications" β benchmarking agent accuracy and regression testing. |
| Foundational Metric Paper | Ragas: Automated Evaluation of Retrieval Augmented Generation (Es et al., 2023) | Mathematical formulations for Faithfulness, Answer Relevance, and Context Precision. |
| Evaluation Framework | Ragas GitHub Repository | Test set synthesis, metric definitions, and LangSmith/LangFuse integration. |
5. Active Recall (Module 4.5 Flashcards) β
RAG EvaluationClick or press Space to flip βΊ
What does Answer Faithfulness measure, and why is it the #1 safety metric in enterprise RAG?
RAG Evaluation β’ AnswerClick to flip back β»
Faithfulness measures the proportion of claims made in the generated answer that can be directly verified against the retrieved context passages. A faithfulness score < 1.0 indicates that the model hallucinated facts or brought in unverified external training priors.
π‘ Architect Takeaway: Enforce a strict >= 0.90 Faithfulness threshold before releasing RAG pipelines to production.
CI/CD TestingClick or press Space to flip βΊ
How do automated offline pytest suites prevent RAG regression during model upgrades?
CI/CD Testing β’ AnswerClick to flip back β»
By running a versioned gold-standard dataset of representative questions and ground-truth answers through the pipeline, scoring results with Ragas, and failing the CI build if Faithfulness, Precision, or Relevance drop below baseline thresholds.
π‘ Architect Takeaway: Automated regression tests ensure that prompt tweaks or model swaps never silently break factual accuracy.
6. Hands-on Engineering Exercises β
Exercise 4.5: The Ragas Metric Benchmark Drill β
- Goal: Build the foundation for your Advanced RAG Dashboard deliverable.
- Task: Create a curated test set of 5 questions with verified ground-truth answers.
- Verification:
- Run the evaluation script using Ragas.
- Generate a Markdown evaluation summary reporting Faithfulness, Answer Relevance, and Context Precision scores.
- Demonstrate that the pipeline achieves
Faithfulness.