Bird
Raised Fist0
Prompt Engineering / GenAIml~8 mins

RAG evaluation metrics in Prompt Engineering / GenAI - Model Metrics & Evaluation

Choose your learning style10 modes available

Start learning this pattern below

Jump into concepts and practice - no test required

or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Metrics & Evaluation - RAG evaluation metrics
Which metric matters for RAG evaluation and WHY

RAG (Retrieval-Augmented Generation) models combine searching for relevant information and generating answers. To check how well they work, we need metrics that measure both parts:

  • Retrieval quality: How good is the search? We use Recall@k to see if the right documents are found in the top k results.
  • Generation quality: How good is the answer? We use BLEU, ROUGE, or METEOR to compare the generated answer to a correct answer.
  • End-to-end quality: How useful is the final answer? We use Exact Match (EM) and F1 score on the answer text to check correctness and overlap.

These metrics together tell us if the model finds the right info and uses it well to answer.

Confusion matrix or equivalent visualization

For RAG, a confusion matrix is less direct because it's about retrieval and generation. But for retrieval, we can think like this:

    Retrieved Relevant Docs (TP) | Retrieved Irrelevant Docs (FP)
    ----------------------------|-----------------------------
    Not Retrieved Relevant Docs (FN) | Not Retrieved Irrelevant Docs (TN)
    

Example for retrieval of 5 docs where 3 are relevant:

    TP = 2 (relevant docs found)
    FP = 1 (irrelevant doc found)
    FN = 1 (relevant doc missed)
    TN = N/A (irrelevant docs not retrieved)
    

Recall@k = TP / (TP + FN) = 2 / (2 + 1) = 0.67

Precision vs Recall tradeoff with examples

In retrieval, Recall means finding as many relevant documents as possible. Precision means most retrieved documents are relevant.

For RAG:

  • If recall is low, the model misses important info, so answers may be wrong or incomplete.
  • If precision is low, the model uses many irrelevant documents, confusing the answer.

Example: A medical RAG system should have high recall to not miss any important facts, even if some irrelevant info is included.

Example: A legal RAG system might prefer higher precision to avoid wrong citations, even if some relevant docs are missed.

What "good" vs "bad" metric values look like for RAG
  • Recall@5: Good > 0.8 means most relevant docs found in top 5; Bad < 0.5 means many relevant docs missed.
  • BLEU/ROUGE: Good > 0.5 means generated answers closely match references; Bad < 0.2 means poor answer quality.
  • Exact Match (EM): Good > 0.7 means many answers exactly match; Bad < 0.4 means many answers are wrong or incomplete.
  • F1 score: Good > 0.7 means good overlap of answer words; Bad < 0.4 means poor overlap.

Good values depend on task difficulty and data quality but these ranges help spot strong vs weak models.

Common pitfalls in RAG evaluation metrics
  • Ignoring retrieval quality: Only checking generated text can hide poor retrieval, leading to wrong answers.
  • Overfitting to reference answers: Metrics like BLEU may penalize valid but different answers.
  • Data leakage: If retrieval index contains test answers, metrics will be unrealistically high.
  • Confusing precision and recall: For retrieval, recall is often more important to find all relevant info.
  • Using accuracy alone: Accuracy on answer correctness can be misleading if dataset is imbalanced.
Self-check question

Your RAG model has 90% Exact Match but only 40% Recall@5 on retrieval. Is it good for production? Why or why not?

Answer: No, it is not good. The model's retrieval misses many relevant documents (low recall), so it may not have enough info to answer well in many cases. High Exact Match might be from easy questions or memorized answers, but poor retrieval limits real usefulness.

Key Result
RAG evaluation needs both retrieval recall and generation quality metrics to ensure relevant info is found and answers are accurate.

Practice

(1/5)
1. What does RAG evaluation metrics primarily measure in a retrieval-augmented generation system?
easy
A. Both the quality of generated answers and the relevance of retrieved documents
B. Only the speed of document retrieval
C. The size of the training dataset
D. The number of layers in the neural network

Solution

  1. Step 1: Understand RAG system components

    RAG combines document retrieval and answer generation, so evaluation must cover both parts.
  2. Step 2: Identify what metrics measure

    Metrics check answer quality (like accuracy) and retrieval quality (like precision).
  3. Final Answer:

    Both the quality of generated answers and the relevance of retrieved documents -> Option A
  4. Quick Check:

    RAG metrics = answer + retrieval quality [OK]
Hint: RAG means check both answer and retrieval quality [OK]
Common Mistakes:
  • Thinking RAG only measures answer quality
  • Confusing retrieval speed with quality
  • Ignoring document relevance in evaluation
2. Which of the following is a common metric used to evaluate the retrieval part of a RAG system?
easy
A. Mean squared error
B. BLEU score
C. Cross-entropy loss
D. Retrieval precision

Solution

  1. Step 1: Identify retrieval metrics

    Retrieval precision measures how many retrieved documents are relevant.
  2. Step 2: Match metric to retrieval

    BLEU is for text generation, cross-entropy and MSE are loss functions, not retrieval metrics.
  3. Final Answer:

    Retrieval precision -> Option D
  4. Quick Check:

    Retrieval metric = precision [OK]
Hint: Precision measures retrieval relevance, not BLEU or loss [OK]
Common Mistakes:
  • Choosing BLEU which is for generation
  • Confusing loss functions with evaluation metrics
  • Ignoring retrieval-specific metrics
3. Consider this Python snippet evaluating a RAG model's answer quality using F1 score:
from sklearn.metrics import f1_score
true_answers = ["cat", "dog", "bird"]
pred_answers = ["cat", "dog", "cat"]
f1 = f1_score(true_answers, pred_answers, average='macro')
print(round(f1, 2))
What will be the output?
medium
A. Error due to string inputs
B. 0.75
C. 0.56
D. 1.00

Solution

  1. Step 1: Verify f1_score handles strings

    sklearn's f1_score supports string labels directly via internal encoding.
  2. Step 2: Compute macro F1

    Classes: 'bird', 'cat', 'dog'
    • 'bird': F1 = 0 (TP=0, predicted 0 times)
    • 'cat': prec=1/2=0.5, rec=1/1=1, F1=2×0.5×1/(0.5+1)=0.67
    • 'dog': F1=1
    Macro F1 = (0 + 0.67 + 1)/3 ≈ 0.5556, round(0.56, 2) = 0.56
  3. Final Answer:

    0.56 -> Option C
  4. Quick Check:

    macro F1 = (0 + 0.67 + 1)/3 = 0.56 [OK]
Hint: f1_score works on strings; macro F1=(0+0.67+1)/3=0.56 [OK]
Common Mistakes:
  • Computing micro F1 or accuracy (0.67)
  • Expecting error due to strings
  • Wrong per-class calculation (0.75)
4. You have this code snippet to compute retrieval precision but it gives wrong results:
retrieved_docs = ["doc1", "doc2", "doc3"]
relevant_docs = ["doc2", "doc4"]
precision = len(set(retrieved_docs) & set(relevant_docs)) / len(relevant_docs)
print(round(precision, 2))
What is the bug and how to fix it?
medium
A. Divide by len(retrieved_docs) instead of len(relevant_docs)
B. Use union instead of intersection in numerator
C. Convert lists to tuples before set operations
D. No bug, code is correct

Solution

  1. Step 1: Understand precision formula

    Precision = relevant retrieved / total retrieved, so denominator must be retrieved docs count.
  2. Step 2: Identify denominator mistake

    Code divides by len(relevant_docs), which is recall formula denominator.
  3. Step 3: Fix denominator

    Change denominator to len(retrieved_docs) to compute precision correctly.
  4. Final Answer:

    Divide by len(retrieved_docs) instead of len(relevant_docs) -> Option A
  5. Quick Check:

    Precision denominator = retrieved docs count [OK]
Hint: Precision divides by retrieved docs count, not relevant docs [OK]
Common Mistakes:
  • Mixing precision with recall formula
  • Using union instead of intersection
  • Ignoring set conversion issues
5. You want to evaluate a RAG model combining answer F1 score and retrieval precision into a single metric. Which approach is best to fairly combine these metrics?
hard
A. Add F1 score and retrieval precision directly
B. Calculate the harmonic mean of F1 score and retrieval precision
C. Use only the higher of the two scores
D. Multiply F1 score by retrieval precision without normalization

Solution

  1. Step 1: Understand metric combination needs

    Combining metrics requires balancing both scores fairly, avoiding dominance by one.
  2. Step 2: Evaluate combination methods

    Harmonic mean balances low and high values well; addition or multiplication can skew results.
  3. Step 3: Choose harmonic mean

    Harmonic mean is common for combining precision and recall, so it suits combining F1 and retrieval precision.
  4. Final Answer:

    Calculate the harmonic mean of F1 score and retrieval precision -> Option B
  5. Quick Check:

    Harmonic mean balances combined metrics [OK]
Hint: Use harmonic mean to balance combined metrics fairly [OK]
Common Mistakes:
  • Adding metrics without normalization
  • Ignoring metric scale differences
  • Choosing max score only