Skip to content

Lesson 16 ยท Evaluation

Evaluate LLM Apps

Evaluate LLM applications with mlflow.genai.evaluate: build test records, connect your app, choose scorers, manage datasets and read the results.

An LLM app can answer the same question in many valid ways, so you cannot always compare its output with one exact answer. The GenAI evaluation in MLflow runs your app on a set of test cases and scores the results. For the concepts, see evaluating LLM apps.

The three main inputs

mlflow.genai.evaluate takes:

  • data: the test cases.
  • predict_fn: your app, as a function.
  • scorers: the checks to apply.

Each record has inputs, and often expectations (the ground truth, or notes about what a good answer needs):

Python
data = [
    {
        "inputs": {"question": "How long is the return window?"},
        "expectations": {"expected_response": "30 days"},
    },
    {
        "inputs": {"question": "Do you ship to islands?"},
        "expectations": {"expected_response": "Yes, with extra delivery time."},
    },
]

Records can also carry outputs or traces that you created earlier, if you want to score answers you already have. The key names inside expectations matter to some built-in judges, so check the documentation of each judge you use.

Run an evaluation

Python
import mlflow
from mlflow.genai.scorers import Correctness, Guidelines

def predict_fn(question: str) -> str:
    return my_app(question)        # your real application

results = mlflow.genai.evaluate(
    data=data,
    predict_fn=predict_fn,
    scorers=[
        Correctness(),
        Guidelines(name="polite", guidelines="The answer must be polite."),
    ],
)

The parameter names of predict_fn match the keys inside inputs. The results appear as an evaluation run in the experiment, with scores and traces for each row.

Some scorers call a language model to judge answers. You can choose one with the form Correctness(model="<provider>:/<model-name>"), and you can set a default with the MLFLOW_GENAI_JUDGE_DEFAULT_MODEL environment variable. Judge calls need provider access and cost money, and your data is sent to that provider. Confirm that this is allowed before you evaluate private data.

The loop, step by step

The stand-in below has the same shape: data, a predict function and scorers. The "app" is a lookup table, not a language model.

Python Stand-in: an evaluation loop over test cases

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

One answer fails the fact check. The total tells you how often something fails, and the row tells you what failed. Always read the failing rows.

Evaluation datasets

MLflow can store test cases as an evaluation dataset attached to experiments. It needs a SQL backend, not the old file store.

Python
from mlflow.genai.datasets import create_dataset

dataset = create_dataset(name="support-questions", experiment_id=["0"])
dataset = dataset.merge_records(data)

You can also merge traces found with mlflow.search_traces, which turns real questions into test cases. Remove personal data and add expected answers before you reuse them.

Build a useful test set

  1. Start with real, common questions.
  2. Add edge cases: unclear, hostile and out-of-scope requests.
  3. Include cases where the right answer is "I don't know".
  4. Add every important production failure as a new case.
  5. Keep the set versioned and review it.

A high pass rate on a tiny or easy test set proves little. Model outputs also vary between calls. Run important evaluations more than once before you draw conclusions.

Compare versions

Keep the data and scorers fixed, change one thing (a prompt or a model), and evaluate again. Then compare the runs side by side. Check cost and latency too: a slightly better answer may not be worth a big increase in either.

Write expectations that survive wording changes

An exact reference answer is fragile, because a good reply can use other words. Consider expectations that describe facts the answer must contain, or rules it must follow. For example: "mentions the 30 day limit", "does not promise a refund", "asks for the order number if it is missing". These rules are easier for a person to check and easier to give to a judge.

Score several qualities

One score cannot describe an answer. A useful set for a support bot is:

Quality How you might check it
Correct facts Reference facts or a correctness judge
Grounded in context A groundedness judge on traces
Follows tone and policy rules A guidelines judge or code rules
Admits when it does not know Cases with no answer in the notes
Length and format Code scorer
Speed and cost Values from traces and usage data

Do not merge everything into one average too early. A bot could improve its tone while getting more facts wrong, and an average might hide the trade.

Keep evaluation honest

  • Do not write test cases while looking at the answers of the current version, or you will write tests that it already passes.
  • Hold back some cases that you do not use while tuning prompts.
  • Check that judge calls and app calls use the intended models.
  • Record the prompt version, model and retrieval settings with each evaluation run, as parameters or tags.

Read results like a detective

For each failure, ask: did the app lack information, did it ignore information, or did the scorer judge wrongly? Each cause needs a different fix. Count the causes over many failures. If most failures come from missing information, better retrieval helps more than prompt changes. If many failures are scorer errors, fix the scorer first, because you are optimizing against a broken measure.

A common mistake

Teams sometimes celebrate a higher score without asking what changed. If the data, the scorers or the judge model changed too, the two numbers cannot be compared. Re-run the old version under the new conditions before you claim progress. This costs a little extra computing, and it protects you from shipping a change that only looks better. Keep a short note with each comparison that lists exactly what was held fixed.

Check your understanding

Quick check ยท 3 questions

of 3 answered You scored / 3 ยท perfect!

  1. 1. Which three main things does mlflow.genai.evaluate use?

    You supply test cases, your app as predict_fn, and scoring functions.
  2. 2. Why read individual failing rows?

    The details show the cause, which you need in order to fix it.
  3. 3. What should you consider before using an LLM judge on private data?

    Judge calls send content to a model provider, which has privacy and cost implications.

Summary

  • GenAI evaluation combines test data, a predict function and scorers.
  • Some judges call a language model, which affects cost and privacy.
  • Evaluation datasets keep test cases in one place.
  • Change one thing at a time and read the failures.

Next: Scorers and Judges

Your progress is saved in this browser. No account needed.