跳到主要内容

使用 MLflow 评估 LLM 和智能体

MLflow 的评估和监控功能可帮助您在从开发到生产的整个生命周期中,系统地衡量、改进和维护 LLM 应用及 AI 智能体的质量。


试用 MLflow LLM 和智能体 Demo

了解面向 LLM 和 AI 智能体的 MLflow 的最快方法是试用 Demo。点击启动 Demo ↓

公开 Demo

访问 demo.mlflow.org,探索预装了样本数据的公开托管 MLflow 实例。

从 UI 启动

要启动 Demo,请点击 MLflow UI 顶部页面上的“Start Demo”按钮。

MLflow GenAI Demo UI

从 CLI 启动

或者,您可以使用 mlflow demo 命令从命令行启动 Demo。此选项不需要您运行 MLflow 服务器。

bash
uvx mlflow demo

MLflow 评估能力的核心准则在于评估驱动开发(Evaluation-Driven Development)。这是一种应对构建高质量 LLM/智能体应用挑战的新兴实践。MLflow 是一个开源 AI 工程平台,旨在支持这一实践,并帮助您快速构建生产级质量的 AI 智能体和 LLM 应用。

Evaluation Driven Development

关键能力

创建并维护高质量数据集

在评估 LLM 应用或 AI 智能体之前,您需要测试数据。评估数据集提供了一个集中式存储库,用于大规模管理测试用例、基础事实(Ground Truth)预期和评估数据。

可以将评估数据集视为您的“测试数据库”——它是评估 AI 系统所需所有数据的单一事实来源。它们将临时测试转化为系统化的质量保证。

了解更多 →

Trace Dataset

运行评估

每次评估由三个组件定义

组件示例
数据集
输入与预期(以及可选的预生成输出和追踪记录)
[
{"inputs": {"question": "2+2"}, "expectations": {"answer": "4"}},
{"inputs": {"question": "2+3"}, "expectations": {"answer": "5"}}
]
评分器
评估标准
@scorer
def exact_match(expectations, outputs):
return expectations == outputs
预测函数
为数据集生成输出
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

以下示例展示了针对问答对数据集的简单评估。

python
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"),
],
)

查看结果

打开 MLflow UI 以查看评估结果。您可以使用以下命令启动 UI:

bash
mlflow server --port 5000

您应该会在“Runs”(运行)选项卡下看到一个新创建的评估运行。点击运行名称即可查看评估结果。

Evaluation Results

后续步骤