by thetestingacademy
Evaluate RAG pipelines with Ragas, measuring faithfulness, answer relevancy, context precision and recall, building golden datasets, and wiring threshold gates into CI for retrieval regressions.
npx @qaskills/cli add ragas-rag-evaluationAuto-detects your AI agent and installs the skill. Works with Claude Code, Cursor, Copilot, and more.
You are an expert AI quality engineer specializing in Ragas. When the user asks you to evaluate, debug, or regression-test a RAG (retrieval-augmented generation) pipeline, follow these instructions.
pip install ragas datasets
export OPENAI_API_KEY=sk-... # judge + embeddings (other providers configurable)
| Metric | Judges | Question it answers |
|---|---|---|
| faithfulness | Generator | Is every claim in the answer supported by the retrieved contexts? |
| answer_relevancy | Generator | Does the answer actually address the question? |
| context_precision | Retriever | Are the relevant chunks ranked above irrelevant ones? |
| context_recall | Retriever | Did retrieval fetch everything needed to answer? |
| answer_correctness | End to end | Does the answer match ground truth (factually + semantically)? |
Diagnosis table: low faithfulness with high context_recall means the generator ignores or contradicts good context (fix prompting). Low context_recall means retrieval misses content (fix chunking, embeddings, top_k). Low context_precision with high recall means noisy retrieval (fix reranking).
from ragas import evaluate, EvaluationDataset
from ragas.metrics import (
Faithfulness, AnswerRelevancy, LLMContextPrecisionWithReference, LLMContextRecall,
)
# 1. Run YOUR pipeline over the golden questions, capturing all four fields
rows = []
for item in load_golden("evals/golden_v2.jsonl"):
result = rag_pipeline.query(item["question"])
rows.append({
"user_input": item["question"],
"response": result.answer,
"retrieved_contexts": [c.text for c in result.chunks],
"reference": item["ground_truth"],
})
dataset = EvaluationDataset.from_list(rows)
# 2. Score
report = evaluate(
dataset,
metrics=[Faithfulness(), AnswerRelevancy(), LLMContextPrecisionWithReference(), LLMContextRecall()],
)
print(report) # aggregate scores
df = report.to_pandas() # per-row scores for failure triage
df[df["faithfulness"] < 0.7].to_json("faithfulness_failures.json", orient="records")
# evals/test_rag_gate.py (pytest wrapper around ragas)
import pytest
THRESHOLDS = {
"faithfulness": 0.85,
"answer_relevancy": 0.80,
"llm_context_precision_with_reference": 0.75,
"context_recall": 0.80,
}
def test_rag_quality_gate(ragas_report): # fixture runs evaluate() once
scores = ragas_report._repr_dict if hasattr(ragas_report, "_repr_dict") else dict(ragas_report)
failures = {m: s for m, s in scores.items() if m in THRESHOLDS and s < THRESHOLDS[m]}
assert not failures, f"RAG gate failed: {failures}"
Gate policy: PR runs use a 25-question stratified sample (mix of easy, hard, adversarial, out-of-scope questions); nightly runs the full set and writes scores to a tracked JSON so trends are diffable in git.
Ragas also ships a TestsetGenerator that synthesizes question/ground-truth pairs from your documents; use it to bootstrap breadth, then human-review before it enters the golden set.
For any change (chunk size, embedding model, top_k, reranker, prompt, generator model):
- name: Install QA Skills
run: npx @qaskills/cli add ragas-rag-evaluation12 of 29 agents supported
Build AI agents that write, run, and fix tests. Playwright, LLM evals, and CI in one live cohort.
Use code AITESTER at checkout