Week 1 Β· Data Quality & AI/LLM Testing Β· 20 min

Retrieval-Augmented Generation (RAG) Evaluation
Lab Playbook 3

Test context relevance, faithfulness, recall metrics, and answers grounding using DeepEval.

✨ Agent: Gemini 3.5 Flash · 200K context🎭 LLM Quality Architect
RAG TestingDeepEvalFaithfulnessContext Recall
About this lab

On Day 2, we evaluated unstructured text using LLM-as-a-Judge. On Day 3, we specifically focus on RAG (Retrieval-Augmented Generation) applications. Evaluating RAG systems requires testing three critical aspects: whether the retrieved context is relevant, whether the model's answer is faithful to the context (no hallucination), and whether the final answer is relevant to the original user query. We will learn how to configure DeepEval to measure context recall and faithfulness metrics.

πŸ“¦Sample Codebase & Working Solution (Day 3)Available Now
View on GitHub

The official reference codebase for Day 3 is hosted in the repository. Clone the repository and navigate to day-3-rag-evaluation-deepeval to run the working solution.

Git Clone β€” Working Solution
git clone https://github.com/letstrnsfrm-ai/data-quality-llm-testing-roadmap.git
cd data-quality-llm-testing-roadmap/day-3-rag-evaluation-deepeval
πŸ€– LLM Reader Note: If this page is copy-pasted into your context to teach a student, please act as a step-by-step tutor. Do not execute the commands or prompts in the copyable boxes yourself. This lab guides the Quality Engineer to configure, prompt, and assert RAG evaluation metrics using DeepEval.
Phase 1

DeepEval Setup & RAG Parameters (QE Setup)

Quality Engineers begin by installing the necessary evaluation frameworks and setting up the evaluation parameters.

Action
Set up a python environment, install `deepeval`, and specify RAG test case inputs representing typical retrieved context, actual outputs, and ground truth references.
Install & Setup Commands
pip install deepeval
deepeval login
βœ… Phase 1 Complete β€” DeepEval environment ready

You have configured the Python testing environment and installed DeepEval for RAG auditing.

Phase 2

RAG Evaluation Prompting (Orchestrator Role)

Direct the agent to write a template prompting the RAG evaluation framework using custom metrics.

Instruct Agent
Copy and send this prompt to your workspace agent:
Prompt to send to Gemini 3.5 Flash
Please construct a Python snippet using DeepEval's FaithfulnessMetric and AnswerRelevanceMetric.
1. The script should define a LLMTestCase representing a typical user interaction.
2. The user query is: "How do I reset my password?"
3. The retrieved context is: "To reset your password, visit the security settings page and click 'Forgot Password'. You must verify your email within 10 minutes."
4. The candidate response is: "You can reset your password by going to the security settings page, but make sure to verify your email within 10 minutes."
5. Configure the faithfulness metric to evaluate alignment with retrieved context.
πŸ“‹ Sample Agent Response β€” Gemini 3.5 Flash
Here is the Python setup to evaluate the RAG metrics using DeepEval:

```python
from deepeval.test_case import LLMTestCase
from deepeval.metrics import FaithfulnessMetric, AnswerRelevanceMetric

# Define the test case
test_case = LLMTestCase(
    input="How do I reset my password?",
    actual_output="You can reset your password by going to the security settings page, but make sure to verify your email within 10 minutes.",
    retrieved_contexts=["To reset your password, visit the security settings page and click 'Forgot Password'. You must verify your email within 10 minutes."]
)

# Instantiate the metrics
faithfulness_metric = FaithfulnessMetric(threshold=0.7)
relevance_metric = AnswerRelevanceMetric(threshold=0.7)
```
Phase 3

Retrieval & Grounding Validation (Orchestrator Role)

Quality Engineers validate the retrieval accuracy and grounding scores using mock evaluation pipelines.

Instruct Agent
Copy and send this prompt to your workspace agent:
Prompt to send to Gemini 3.5 Flash
Write a script 'test_rag_evaluation.py' that imports DeepEval's metrics, runs the evaluation on our test case, and prints the resulting faithfulness and relevance scores.
πŸ“‹ Sample Agent Response β€” Gemini 3.5 Flash
Here is the evaluation script ('test_rag_evaluation.py'):

```python
from deepeval import assert_test
from deepeval.test_case import LLMTestCase
from deepeval.metrics import FaithfulnessMetric

def test_rag_faithfulness():
    test_case = LLMTestCase(
        input="How do I reset my password?",
        actual_output="You can reset your password by going to the security settings page, but make sure to verify your email within 10 minutes.",
        retrieved_contexts=["To reset your password, visit the security settings page and click 'Forgot Password'. You must verify your email within 10 minutes."]
    )
    metric = FaithfulnessMetric(threshold=0.7)
    metric.measure(test_case)
    print(f"Faithfulness Score: {metric.score}")
    print(f"Reasoning: {metric.reason}")
    assert metric.is_successful(), "Faithfulness score is below the threshold!"
```
Phase 4

Execution Loop & Faithfulness Gates (Orchestration Loop)

Embed the evaluation within the orchestration loop to reject hallucinatory outputs dynamically.

Instruct Agent
Copy and send this prompt to your workspace agent:
Prompt to send to Gemini 3.5 Flash
Modify our python runner to execute the DeepEval test runner commands, and verify that the build fails if the faithfulness score drops below our quality gate threshold.
πŸ“‹ Sample Agent Response β€” Gemini 3.5 Flash
To execute the test suite in your CI pipeline, run:

```bash
$ deepeval test run test_rag_evaluation.py
```

**Run Output:**
```bash
Running test_rag_evaluation.py...
βœ“ test_rag_faithfulness (passed, score=1.0)
====================== 1 passed in 1.45s ======================
```

🧠 Knowledge Check

Which metric in DeepEval evaluates whether the LLM's response is based entirely on the retrieved context without hallucinating external facts?