The RAGAS Framework: Measuring RAG with Math

The RAGAS Framework: Measuring RAG with Math

Master the industry standard for RAG evaluation. Learn how to use RAGAS to calculate Faithfulness, Answer Relevancy, and Context Recall.

The RAGAS Framework: Professional Evaluation

In the previous lesson, we established that measuring RAG is hard. RAGAS (Retrieval-Augmented Generation Assessment) is the library that makes it easy. It provides a mathematical framework to evaluate the various components of your pipeline without requiring vast amounts of human-labeled data.

In this lesson, we will deconstruct the "RAGAS Triad" and learn how to run an automated evaluation report on your vector database performance.


1. What is RAGAS?

RAGAS is an open-source framework that helps you evaluate RAG pipelines at the component level. It uses "Reference-free" evaluation in many cases, meaning it can judge an answer even if you don't have a "Perfect Human Answer" to compare it against.

It does this by using a "Judge LLM" (like GPT-4) to cross-check the claims in your answer against the facts in your retrieved chunks.


2. The Four Primary Metrics

RAGAS breaks evaluation down into four core scores:

1. Faithfulness (Is it a lie?)

  • Measures if the answer is derived only from the retrieved context.
  • High Score = Zero Hallucinations.

2. Answer Relevancy (Is it helpful?)

  • Measures how well the answer addresses the user's specific question.
  • High Score = The bot isn't "rambling" about irrelevant topics.

3. Context Precision (Was the search good?)

  • Measures if the Rank #1 document was actually the most useful one.
  • High Score = Your vector search is well-tuned.

4. Context Recall (Did we miss anything?)

  • Measures if the retrieved context contains all the facts required to answer the question.
  • High Score = Your chunking and retrieval depth are sufficient.

3. The Mathematics of "Faithfulness"

How does an AI calculate "Faithfulness"?

  1. RAGAS identifies all individual "Claims" in the bot's answer.
    • Claim 1: "The company was founded in 1999."
    • Claim 2: "It has 500 employees."
  2. It then searches the Retrieved Context for evidence for each claim.
  3. Score = (Supported Claims) / (Total Claims).
graph TD
    A[AI Answer] --> B[Claim 1]
    A --> C[Claim 2]
    B --> D{Found in Context?}
    C --> E{Found in Context?}
    D -- Yes --> F[1.0]
    E -- No --> G[0.0]
    F & G --> H[Faithfulness: 0.5]

4. Python Implementation: Running a RAGAS Test

To use RAGAS, you prepare a dataset of your bot's outputs and run the evaluator.

from ragas import evaluate
from ragas.metrics import faithfulness, answer_relevancy, context_recall, context_precision
from datasets import Dataset

# 1. Prepare your test data from a real RAG run
data_samples = {
    'question': ["What is the refund policy?", "How do I contact support?"],
    'answer': ["You can refund in 30 days.", "Email support@acme.com"],
    'contexts': [
        ["Our policy allows for 30-day returns if the item is unused."],
        ["Support is available via email at support@acme.com from 9 to 5."]
    ],
    'ground_truth': ["30 days refund", "support@acme.com"]
}

dataset = Dataset.from_dict(data_samples)

# 2. Run the Evaluation
# Note: This will use your OpenAI API key to run a Judge LLM
score = evaluate(
    dataset,
    metrics=[
        faithfulness,
        answer_relevancy,
        context_recall,
        context_precision
    ]
)

# 3. Print the report
df = score.to_pandas()
print(df[['faithfulness', 'answer_relevancy']])

5. Identifying the "Root Cause" of Failure

Using RAGAS allows you to fix the specific part of your system that is broken:

  • Low Context Recall: Your k is too low, or your embedding model is weak. Fix the Vector DB.
  • Low Faithfulness: Your system prompt is too "creative," or the LLM is choosing to ignore the context. Fix the Prompt.
  • Low Answer Relevancy: The retrieved context is too noisy. Fix the Chunking.

6. Integrating RAGAS into CI/CD

In a professional AI team, you don't wait for a user to complain. You run RAGAS as part of your Continuous Integration (GitHub Actions). If a new code change drops the "Faithfulness" score from 0.95 to 0.80, the build Fails, and you prevent a hallucinating bot from reaching production.


Summary and Key Takeaways

RAGAS turns "Product Reviews" into "Engineering Specs."

  1. RAGAS Triad: Focus on Faithfulness, Relevancy, and Context.
  2. Component-level Debugging: Know exactly which part of your pipeline is failing.
  3. Reference-free Evaluation: Use AI to grade AI, saving thousands of hours of human labor.
  4. Data-Driven Decisions: Stop arguing about "vibe" and start looking at the 0-to-1 scores.

In the next lesson, we will look at Building a test dataset, learning how to automatically generate "Golden Questions" from your documents so you can run RAGAS even on day one.


Exercise: Score Interpretation

You run a RAGAS test on a new "Medical Bot." Results:

  • Faithfulness: 1.0 (100%)
  • Context Recall: 0.2 (20%)
  • Answer Relevancy: 0.9 (90%)
  1. Is the bot hallucinating?
  2. If a doctor asks a complex question, will the bot provide a complete answer?
  3. What is the First thing you should change in your pipeline? (e.g., Increase k? Change chunking? Change the LLM?)

Congratulations on completing Module 11 Lesson 2! You're now measuring success.

Subscribe to our newsletter

Get the latest posts delivered right to your inbox.

Subscribe on LinkedIn