使用 MLflow 评估 LLM 和智能体
MLflow 的评估和监控功能可帮助您在从开发到生产的整个生命周期中,系统地衡量、改进和维护 LLM 应用及 AI 智能体的质量。
试用 MLflow LLM 和智能体 Demo
了解面向 LLM 和 AI 智能体的 MLflow 的最快方法是试用 Demo。点击启动 Demo ↓
公开 Demo
访问 demo.mlflow.org,探索预装了样本数据的公开托管 MLflow 实例。
从 UI 启动
要启动 Demo,请点击 MLflow UI 顶部页面上的“Start Demo”按钮。

从 CLI 启动
或者,您可以使用 mlflow demo 命令从命令行启动 Demo。此选项不需要您运行 MLflow 服务器。
uvx mlflow demo
MLflow 评估能力的核心准则在于评估驱动开发(Evaluation-Driven Development)。这是一种应对构建高质量 LLM/智能体应用挑战的新兴实践。MLflow 是一个开源 AI 工程平台,旨在支持这一实践,并帮助您快速构建生产级质量的 AI 智能体和 LLM 应用。

关键能力
- 数据集管理
- 人工反馈
- LLM 即裁判 (LLM-as-a-Judge)
- 系统性评估
- 生产监控
通过自动化扩展质量评估
质量评估是构建高质量 LLM 应用和 AI 智能体的关键环节,但它往往非常耗时且需要专业人员参与。LLM 是实现质量评估自动化的强大工具。
MLflow 提供了多种内置的 LLM-as-a-Judge(以 LLM 为判别器) 评分器来帮助自动化该过程,同时也提供了一套灵活的工具集,方便您轻松构建自己的 LLM 判别器。

生产环境应用监控
理解并优化 LLM 应用和 AI 智能体的性能对于高效运营至关重要。MLflow Tracing 能够捕获每一环节的关键指标(如延迟和 Token 使用量)以及各种质量指标,帮助您识别瓶颈、监控效率并寻找优化机会。

运行评估
每次评估由三个组件定义
| 组件 | 示例 |
|---|---|
| 数据集 输入与预期(以及可选的预生成输出和追踪记录) | |
| 评分器 评估标准 | |
| 预测函数 为数据集生成输出 | |
以下示例展示了针对问答对数据集的简单评估。
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:
mlflow server --port 5000
您应该会在“Runs”(运行)选项卡下看到一个新创建的评估运行。点击运行名称即可查看评估结果。



