Sub topic 11
Evaluating quality with datasets, scorers, and judges
This chapter assumes you have the answer() function from the RAG chapter, MLflow Tracing enabled, and a workspace where mlflow 3.x is installed. You should also know how to read a pass/fail table in a notebook.
Why spot checks are not enough
Generative output varies from run to run, so a few manual questions do not tell you whether a prompt change helped or hurt. Evaluation turns "it seems fine" into a repeatable measurement. You run a fixed set of questions through your app, score each answer, and compare the scores between versions.
Three parts make up an evaluation:
- Dataset: a list of test inputs, optionally with expectations such as facts a correct answer must contain.
- Scorer: a function that takes one input, output, and trace, and returns a judgment.
- Judge: a scorer that asks an LLM to make the judgment. Judges handle qualities that are hard to check with code, such as correctness or tone.
Build a small dataset
Each row has inputs, whose keys match the parameters of your prediction function. It can also have expectations. Start with 20 to 50 realistic questions, including some the documents cannot answer.
Run an evaluation
import mlflow
from mlflow.genai.scorers import Correctness, Guidelines, RelevanceToQuery, scorer
eval_data = [
{
"inputs": {"question": "What is a vector index?"},
"expectations": {
"expected_facts": ["A vector index stores embeddings for similarity search"]
},
},
{
"inputs": {"question": "What is the CEO's favorite color?"},
"expectations": {
"expected_facts": ["The documents do not contain this information"]
},
},
]
@scorer
def concise(outputs: str) -> bool:
# Code-based scorer: pass if the answer is under 120 words
return len(outputs.split()) < 120
def predict_fn(question: str) -> str:
return answer(question) # the RAG function from the previous chapter
results = mlflow.genai.evaluate(
data=eval_data,
predict_fn=predict_fn,
scorers=[
Correctness(),
RelevanceToQuery(),
Guidelines(
name="grounded_tone",
guidelines="The answer must be polite and must not invent facts.",
),
concise,
],
)
print(results.metrics)mlflow.genai.evaluate calls predict_fn for each row, captures a trace, and runs every scorer. Results appear in the MLflow experiment, where you can open the trace behind any low score.
Choosing scorers
| Scorer type | Example | Use it for | Cost |
|---|---|---|---|
| Code-based | `concise` | Length, format, required keywords | Free, deterministic |
| Built-in judge | `Correctness`, `RelevanceToQuery` | General quality checks | One LLM call per row |
| Guidelines judge | `Guidelines(...)` | Your own plain-language rules | One LLM call per row |
Correctness needs expected_facts or an expected response in expectations. RelevanceToQuery needs no labels, so it also works on production traffic.
Common errors
- Key mismatch: if
inputshasquestionbutpredict_fntakesquery, the call fails. Parameter names must match the keys. - Vague guidelines: a guideline such as "be good" gives noisy scores. State one checkable rule per
Guidelinesscorer. - Trusting judges blindly: a judge is a model and can be wrong. Read the judge's rationale on a sample of rows and compare it with your own view.
- Tiny or easy datasets: if every row passes, the dataset cannot show regressions. Add failures you have seen in real use.
Exercise
Your Correctness scorer returns an error for some rows but works for others. What is the most likely cause, and how do you fix it?
Show answer
Correctness compares the answer with ground truth, so rows missing expected_facts (or an expected response) in expectations cannot be scored. Add expectations to those rows, or use a scorer that needs no labels, such as RelevanceToQuery.