Evaluating LLMs in Practice: What to Measure and How to Interpret
Evaluating the quality of LLMs is a non-trivial task. Unlike classical machine learning, where metrics are clearly defined, generative models require a combination of automatic and manual methods. In this article, I will discuss which metrics to use depending on the task, how to calculate and interpret them, and how to avoid common pitfalls.
Why Metrics Are Needed and What Problems They Solve
Metrics allow: - Comparing models when selecting a base architecture; - Tracking regressions when updating the model or prompts; - Evaluating the overall quality of a RAG system, not just the generator; - Formalizing quality requirements in contracts and SLAs.
Without metrics, it is impossible to talk about the reproducibility of experiments. Even subjective statements like 'the model has improved' should be backed up with numbers.
Classical Metrics for Text Generation
For tasks with a reference answer, metrics based on n-grams and semantic similarity are applied.
| Metric | What it measures | When to use | Limitations |
|---|---|---|---|
| BLEU | Matching n-grams with the reference | Machine translation, short answers | Does not account for synonyms, penalizes reformulations |
| ROUGE | Overlap of n-grams and sequences | Summarization | Similar limitations |
| METEOR | Considers synonyms and stemming | Translation, summarization | Requires a synonym dictionary |
| BERTScore | Semantic similarity through embeddings | Any generative tasks | Depends on the embedding model |
Example of calculating BLEU in Python:
from nltk.translate.bleu_score import sentence_bleu
reference = ['the cat is on the mat'.split()]
candidate = 'the cat is on mat'.split()
score = sentence_bleu(reference, candidate)
print(score) # 0.71
It is important to remember: BLEU and ROUGE poorly correlate with human evaluation for long creative texts. They are almost useless for chatbots and assistants.
Metrics for RAG Systems
RAG systems are evaluated based on two groups: search quality and generation quality. For search, classical information retrieval metrics such as precision@k, recall@k, MRR, NDCG are used. For generation, specialized metrics from the RAGAS library are used.
Main RAGAS metrics: - Faithfulness — how well the answer corresponds to the found fragments (grounding). - Answer Relevancy — how relevant the answer is to the question. - Context Precision — how relevant the found fragments are. - Context Recall — how comprehensively the necessary fragments are found.
Example evaluation with RAGAS:
from ragas import evaluate
from ragas.metrics import faithfulness, answer_relevancy
dataset = {
"question": ["What is RAG?"] ,
"answer": ["RAG is an approach combining search and generation."] ,
"contexts": [["RAG (Retrieval-Augmented Generation) — a method that supplements generation with external data."]],
"ground_truth": ["RAG is an approach combining search and generation."]
}
result = evaluate(dataset, metrics=[faithfulness, answer_relevancy])
print(result)
For evaluating search, it is convenient to use the ranx library or write a custom function:
def precision_at_k(retrieved, relevant, k):
retrieved_k = retrieved[:k]
return len(set(retrieved_k) & set(relevant)) / k
LLM-as-a-Judge: Automatic Evaluation Without References
When reference answers are not available, an LLM judge is applied. This approach involves a strong model (e.g., GPT-4) evaluating responses based on given criteria. The method works well for evaluating usefulness, safety, and style.
Example prompt for an LLM judge:
Rate the assistant's response on a scale of 1-5 for usefulness.
Question: {question}
Answer: {answer}
Return only the number.
Important: - Use the same model for all evaluations; - Define clear criteria; - Check the consistency of the judge (e.g., through Cohen's Kappa).
LLM-as-a-Judge does not replace human evaluation but allows scaling experiments.
How to Choose Metrics for Your Scenario
| Scenario | Recommended Metrics |
|---|---|
| Text Classification | Accuracy, F1, Precision, Recall |
| Machine Translation | BLEU, METEOR, BERTScore |
| Summarization | ROUGE, BERTScore, faithfulness |
| RAG System | RAGAS (faithfulness, answer relevancy), precision@k |
| Chatbot | LLM-as-a-Judge, human eval |
| Code Generation | Pass@k, HumanEval |
Common Pitfalls in Evaluation
- Comparing models on different prompts. Prompts should be the same.
- Ignoring variance. Run the evaluation multiple times and calculate the mean and standard deviation.
- Using only one metric. Combine automatic and manual evaluations.
- Evaluating on too small a sample. At least 50-100 examples for statistical significance.
Conclusion
The right choice of metrics is the key to an objective evaluation of LLMs. Start by defining the task, then select metrics, automate the calculation, and regularly review them. Remember that metrics are a tool, not a goal. They should help make decisions, not just look good in a report.
For further reading, I recommend the metrics glossary, guide to reading benchmarks, and practice of evaluating RAG systems.
