Skip to main content

Evaluating LLMs/Agents with MLflow

Modern GenAI Evaluation

This documentation covers MLflow's GenAI evaluation system which uses:

  • mlflow.genai.evaluate() for evaluation
  • Scorer objects for metrics
  • Built-in and custom LLM judges

Note: This system is separate from the classic ML evaluation system that uses mlflow.evaluate() and EvaluationMetric. The two systems serve different purposes and are not interoperable.

MLflow's evaluation and monitoring capabilities help you systematically measure, improve, and maintain the quality of your GenAI applications throughout their lifecycle from development through production.

Prompt Evaluation

A core tenet of MLflow's evaluation capabilities is Evaluation-Driven Development. This is an emerging practice to tackle the challenge of building high-quality LLM/Agentic applications. MLflow is an end-to-end platform that is designed to support this practice and help you deploy AI applications with confidence.

Evaluation Driven Development

Key Capabilities

Create and maintain a High-Quality Dataset

Before you can evaluate your GenAI application, you need test data. Evaluation Datasets provide a centralized repository for managing test cases, ground truth expectations, and evaluation data at scale.

Think of Evaluation Datasets as your "test database" - a single source of truth for all the data needed to evaluate your AI systems. They transform ad-hoc testing into systematic quality assurance.

Learn more →

Trace Dataset

Running an Evaluation

Each evaluation is defined by three components:

ComponentExample
Dataset
Inputs & expectations (and optionally pre-generated outputs and traces)
[
{"inputs": {"question": "2+2"}, "expectations": {"answer": "4"}},
{"inputs": {"question": "2+3"}, "expectations": {"answer": "5"}}
]
Scorer
Evaluation criteria
@scorer
def exact_match(expectations, outputs):
return expectations == outputs
Predict Function
Generates outputs for the dataset
def predict_fn(question: str) -> str:
response = client.chat.completions.create(
model="gpt-4o-mini",
messages=[{"role": "user", "content": question}]
)
return response.choices[0].message.content

The following example shows a simple evaluation of a dataset of questions and expected answers.

import os
import openai
import mlflow
from mlflow.genai.scorers import Correctness, Guidelines

client = openai.OpenAI(api_key=os.getenv("OPENAI_API_KEY"))

# 1. Define a simple QA dataset
dataset = [
{
"inputs": {"question": "Can MLflow manage prompts?"},
"expectations": {"expected_response": "Yes!"},
},
{
"inputs": {"question": "Can MLflow create a taco for my lunch?"},
"expectations": {
"expected_response": "No, unfortunately, MLflow is not a taco maker."
},
},
]


# 2. Define a prediction function to generate responses
def predict_fn(question: str) -> str:
response = client.chat.completions.create(
model="gpt-4o-mini", messages=[{"role": "user", "content": question}]
)
return response.choices[0].message.content


# 3.Run the evaluation
results = mlflow.genai.evaluate(
data=dataset,
predict_fn=predict_fn,
scorers=[
# Built-in LLM judge
Correctness(),
# Custom criteria using LLM judge
Guidelines(name="is_english", guidelines="The answer must be in English"),
],
)

Review the results

Open the MLflow UI to review the evaluation results. If you are using OSS MLflow, you can use the following command to start the UI:

mlflow ui --port 5000

If you are using cloud-based MLflow, open the experiment page in the platform. You should see a new evaluation run is created under the "Runs" tab. Click on the run name to view the evaluation results.

Evaluation Results

Next Steps