MODULE 12/LESSON 1
📊 Enterprise LLMOps & Evaluation

LLM Evaluation & RAGAS Metrics

Mastering LLM Evaluation Frameworks: LLM-as-a-Judge, Ragas Triad Metrics, and DeepEval CI/CD Unit Testing

15 min📊 Diagram
You cannot optimize what you do not measure. Traditional software testing relies on exact string assertions (`assert result == "hello"`), which fails completely when evaluating nondeterministic LLM outputs. In enterprise AI engineering, evaluation is divided into two disciplines: offline evaluation (benchmarking candidate models/prompts against ground-truth datasets using Ragas/DeepEval) and online evaluation (real-time LLM-as-a-Judge scoring of live user traces).

Key Concepts

LLM-as-a-Judge Pattern

Using a highly capable model (GPT-4o or Claude 3.5 Sonnet) with structured rubrics to grade the output quality, toxicity, and correctness of smaller, cheaper production models.

The Ragas Evaluation Triad

Evaluating RAG pipelines across Faithfulness (checking hallucinations against retrieved context), Answer Relevance (checking if prompt was answered), and Context Precision (retrieval quality).

DeepEval CI/CD Integration

Writing PyTest assertions for LLM metrics (`assert_test(test_case, [faithfulness_metric])`) so that CI/CD pipelines fail automatically if accuracy degrades below production thresholds.

Reference-Free vs Ground-Truth Evals

Combining reference-free metrics (Ragas measuring context vs answer) with golden ground-truth test datasets to achieve 95%+ correlation with human expert judgment.

The Ragas RAG Evaluation Triad

The Ragas RAG Quality Evaluation Triad User Query User's Input Prompt Retrieved Context Vector Chunks from DB Generated Response LLM's Final Output Context Precision Retrieval Signal/Noise Answer Relevance Addresses User Intent? Faithfulness Metric Zero Hallucinations Check

1. Ragas Automated RAG Evaluation Script (Python)

Evaluating RAG quality by executing Faithfulness, Answer Relevance, and Context Precision metrics against your dataset using OpenAI as the evaluator model.
evals/ragas_evaluator.pypython
1from datasets import Dataset
2from ragas import evaluate
3from ragas.metrics import (
4    faithfulness,
5    answer_relevance,
6    context_precision,
7)
8
9# Prepare dataset with User Questions, LLM Answers, and Retrieved Contexts
10eval_dataset = Dataset.from_dict({
11    "question": ["What is the refund policy for enterprise accounts?"],
12    "contexts": [["Enterprise accounts are eligible for full refund within 30 days of purchase upon written notice."]],
13    "answer": ["Enterprise customers can get a full refund within 30 days by providing written notice."]
14})
15
16# Run Ragas Evaluation Suite
17results = evaluate(
18    dataset=eval_dataset,
19    metrics=[
20        faithfulness,
21        answer_relevance,
22        context_precision
23    ]
24)
25
26print(results)
27# Output: {'faithfulness': 1.0, 'answer_relevance': 0.96, 'context_precision': 1.0}

2. DeepEval PyTest Integration for CI/CD Pipelines

DeepEval allows unit testing LLM applications directly in PyTest. If the hallucination metric score falls below 0.8, the PyTest suite fails, preventing broken prompts from being merged into production.
tests/test_llm_quality.pypython
1import pytest
2from deepeval import assert_test
3from deepeval.metrics import HallucinationMetric
4from deepeval.test_case import LLMTestCase
5
6def test_hallucination():
7    context = ["Company X recorded $50M revenue in Q3 2024."]
8    actual_output = ["Company X recorded $50M revenue in Q3 2024."]
9    
10    test_case = LLMTestCase(
11        input="What was Q3 2024 revenue?",
12        actual_output=actual_output[0],
13        context=context
14    )
15    
16    # Define Hallucination threshold (Must be >= 0.8 to pass)
17    metric = HallucinationMetric(threshold=0.8)
18    
19    # PyTest assertion for LLM evaluation
20    assert_test(test_case, [metric])
💡
Senior Architect Insight: Don't rely solely on general LLM benchmarks (like MMLU or GSM8K) for your production app. Your users ask domain-specific questions. Build your own ground-truth 'golden dataset' (100-200 representative questions) and run Ragas/DeepEval on every prompt update.