提示工程 UI(实验性)
从 MLflow 2.7 开始,MLflow Tracking UI 提供了一流的提示工程体验。无需编写代码,您就可以尝试来自 MLflow AI Gateway 的多种 LLM、参数配置和提示,以构建各种模型,用于问答、文档摘要等。使用嵌入式评估 UI,您还可以评估一组输入上的多个模型,并比较响应以选择最佳模型。使用提示工程 UI 创建的每个模型都以 MLflow Model 格式存储,可用于批量或实时推理。所有配置(提示模板、LLM 选择、参数等)都被追踪为 MLflow Runs。
快速入门
以下指南将帮助您开始使用 MLflow 的提示工程 UI。
步骤 1:创建 MLflow AI Gateway 补全或聊天端点
要使用提示工程 UI,您需要创建一个或多个 MLflow AI Gateway 补全或聊天端点。请遵循 MLflow AI Gateway 快速入门指南,在不到五分钟内轻松创建端点。如果您已经有权访问类型为 llm/v1/completions
或 llm/v1/chat
的 MLflow AI Gateway 端点,则可以跳过此步骤。
mlflow gateway start --config-path config.yaml --port 7000
步骤 2:将 MLflow AI Gateway 连接到 MLflow Tracking Server
提示工程 UI 还需要 MLflow AI Gateway 和 MLflow Tracking Server 之间的连接。要将 MLflow AI Gateway 与 MLflow Tracking Server 连接,只需在服务器运行的环境中设置 MLFLOW_DEPLOYMENTS_TARGET
环境变量并重新启动服务器即可。例如,如果 MLflow AI Gateway 在 http://localhost:7000
运行,您可以在本地机器上的 shell 中使用 mlflow server 命令启动 MLflow Tracking Server 并将其连接到 MLflow AI Gateway,如下所示
export MLFLOW_DEPLOYMENTS_TARGET="http://127.0.0.1:7000"
mlflow server --port 5000
步骤 3:创建或查找 MLflow Experiment
接下来,在 MLflow UI 中打开一个现有的 MLflow Experiment,或创建一个新的 Experiment。
步骤 4:使用提示工程创建一个 Run
打开 Experiment 后,点击 New Run(新建运行)按钮并选择 using Prompt Engineering(使用提示工程)。这将打开提示工程游乐场,您可以在其中尝试不同的 LLM、参数和提示。


步骤 5:选择您的端点并评估示例提示
接下来,点击 Select endpoint(选择端点)下拉菜单,选择您在步骤 1 中创建的 MLflow AI Gateway 补全端点。然后,点击 Evaluate(评估)按钮,测试一个生成产品广告的示例提示工程用例。
MLflow 会将指定的 stock_type 输入变量值 - "books"
- 嵌入到指定的 prompt template(提示模板)中,并将其发送到与 MLflow AI Gateway 端点关联的 LLM,同时使用配置的 temperature(温度)(当前为 0.01
)和 max_tokens(最大令牌数)(当前为 1000)。LLM 的响应将出现在 Output(输出)部分。
步骤 6:尝试您选择的提示
将上一步的提示模板替换为您选择的提示模板。提示可以定义多个变量。例如,您可以使用以下提示模板来指示 LLM 回答关于 MLflow 文档的问题
Read the following article from the MLflow documentation that appears between triple
backticks. Then, answer the question about the documentation that appears between triple quotes.
Include relevant links and code examples in your answer.
```{{article}}```
"""
{{question}}
"""
然后,填写输入变量。例如,在 MLflow 文档用例中,可以将 article 输入变量设置为 https://mlflow.org.cn/docs/latest/tracking.html#logging-data-to-runs 的内容,并将 question 输入变量设置为 "How do I create a new MLflow Run using the Python API?"
(如何使用 Python API 创建新的 MLflow Run?)。
最后,点击 Evaluate(评估)按钮查看新的输出。您也可以尝试选择一个较大的 temperature(温度)值,观察 LLM 输出的变化。
步骤 7:将您选择的 LLM、提示模板和参数作为 MLflow Run 捕获
一旦您对所选的提示模板和参数感到满意,点击 Create Run(创建运行)按钮,将此信息以及您选择的 LLM 存储为 MLflow Run。这将创建一个新的 Run,并将提示模板、参数和选择的 LLM 存储为 Run 参数。它还将自动创建一个包含此信息的 MLflow Model,可用于批量或实时推理。
-
要查看此信息,点击 Run 名称打开 Run(运行)页面
-
您还可以通过打开 Table(表格)视图标签页来查看参数并将其与其他配置进行比较
-
创建 Run 后,MLflow 将打开 Evaluation(评估)标签页,您可以在其中查看最新的游乐场输入和输出,并尝试更多输入
步骤 8:尝试新的输入
要测试您选择的 LLM、提示模板和参数在新输入上的行为
- 点击 Add Row(添加行)按钮并填写提示模板输入变量的值。例如,在 MLflow 文档用例中,您可以尝试询问与 MLflow 无关的问题,看看 LLM 如何响应。这对于确保应用程序对不相关的输入具有鲁棒性非常重要。


-
然后,点击 Evaluate(评估)按钮查看输出。
-
最后,点击 Save(保存)按钮存储新的输入和输出。
步骤 9:调整您的提示模板并创建新的 Run
在尝试更多输入时,您可能会发现一些场景,您的 LLM 选择、提示模板和参数的表现不如您期望的好。例如,在 MLflow 文档用例中,即使答案没有出现在指定的文章中,LLM 仍然尝试回答有关 MLflow Projects 的不相关问题。
-
为了提高性能,通过上下文菜单选择 Duplicate run(复制运行)选项来创建新的 Run。例如,在 MLflow 文档用例中,在提示模板中添加以下文本有助于提高对不相关问题的鲁棒性
If the question does not relate to the article, respond exactly with the phrase
"I do not know how to answer that question." Do not include any additional text in your
response. -
然后,从提示工程游乐场中调整提示模板(和/或 LLM 选择和参数),评估一个输入,并点击 Create Run(创建运行)按钮创建新的 Run。
步骤 10:在新提示模板上评估先前的输入
现在您已经对提示模板进行了调整,重要的是要确保新模板在先前的输入上表现良好,并将输出与旧配置进行比较。
-
从 Evaluation(评估)选项卡中,点击新 Run 旁边的 Evaluate all(评估所有)按钮,以评估所有先前的输入。
-
点击 Save(保存)按钮存储结果。
步骤 11:以编程方式加载评估数据
MLflow 提示工程 UI 和评估 UI 生成的所有输入和输出都作为 artifacts 存储在 MLflow Runs 中。可以使用 mlflow.load_table()
API 以编程方式访问它们,如下所示
import mlflow
mlflow.set_experiment("/Path/to/your/prompt/engineering/experiment")
# Load input and output data across all Runs (configurations) as a Pandas DataFrame
inputs_outputs_pdf = mlflow.load_table(
# All inputs and outputs created from the MLflow UI are stored in an artifact called
# "eval_results_table.json"
artifact_file="eval_results_table.json",
# Include the run ID as a column in the table to distinguish inputs and outputs
# produced by different runs
extra_columns=["run_id"],
)
# Optionally convert the Pandas DataFrame to Spark where it can be stored as a Delta
# table or joined with existing Delta tables
inputs_outputs_sdf = spark.createDataFrame(inputs_outputs_pdf)
步骤 12:以编程方式生成预测
找到表现良好的 LLM、提示模板和参数配置后,您可以在您选择的 Python 环境中使用相应的 MLflow Model 生成预测,或者 部署它以提供实时服务。
-
要在 notebook 中加载 MLflow Model 以进行批量推理,点击 Run 的名称打开 Run Page(运行页面),并在 Artifact Viewer(artifacts 查看器)中选择 model 目录。然后,复制 Predict on a Pandas DataFrame(在 Pandas DataFrame 上预测)部分的前几行代码,并在您选择的 Python 环境中运行它们,例如
import mlflow
logged_model = "runs:/8451075c46964f82b85fe16c3d2b7ea0/model"
# Load model as a PyFuncModel.
loaded_model = mlflow.pyfunc.load_model(logged_model) -
然后,要生成预测,调用
predict()
方法并传入输入变量的字典。例如article_text = """
An MLflow Project is a format for packaging data science code in a reusable and reproducible way.
The MLflow Projects component includes an API and command-line tools for running projects, which
also integrate with the Tracking component to automatically record the parameters and git commit
of your source code for reproducibility.
This article describes the format of an MLflow Project and how to run an MLflow project remotely
using the MLflow CLI, which makes it easy to vertically scale your data science code.
"""
question = "What is an MLflow project?"
loaded_model.predict({"article": article_text, "question": question})有关使用 MLflow 部署实时服务的更多信息,请参阅下面的说明。
步骤 13:对模型的输出执行基于指标的评估
如果您想评估模型在特定指标上的性能,MLflow 提供了 mlflow.evaluate()
API。让我们在文本摘要的某些预定义指标上评估我们的模型
import mlflow
import pandas as pd
logged_model = "runs:/840a5c43f3fb46f2a2059b761557c1d0/model"
article_text = """
An MLflow Project is a format for packaging data science code in a reusable and reproducible way.
The MLflow Projects component includes an API and command-line tools for running projects, which
also integrate with the Tracking component to automatically record the parameters and git commit
of your source code for reproducibility.
This article describes the format of an MLflow Project and how to run an MLflow project remotely
using the MLflow CLI, which makes it easy to vertically scale your data science code.
"""
question = "What is an MLflow project?"
data = pd.DataFrame(
{
"article": [article_text],
"question": [question],
"ground_truth": [
article_text
], # used for certain evaluation metrics, such as ROUGE score
}
)
with mlflow.start_run():
results = mlflow.evaluate(
model=logged_model,
data=data,
targets="ground_truth",
model_type="text-summarization",
)
eval_table = results.tables["eval_results_table"]
print(f"See evaluation table below: \n{eval_table}")
评估结果也可以在 MLflow Evaluation UI 中查看
mlflow.evaluate()
API 还支持自定义指标、静态数据集评估等等。如需更深入的指南,请参阅MLflow LLM 评估。
部署用于实时服务
找到表现良好的 LLM、提示模板和参数配置后,您可以如下部署相应的 MLflow Model 用于实时服务
-
将您的模型注册到 MLflow Model Registry。以下示例将从快速入门创建的 MLflow Model 注册为名为
"mlflow_docs_qa_model"
的 Registered Model 的版本 1。mlflow.register_model(
model_uri="runs:/8451075c46964f82b85fe16c3d2b7ea0/model",
name="mlflow_docs_qa_model",
) -
在您将运行 MLflow Model Server 的环境中定义以下环境变量,例如您本地机器上的 shell
MLFLOW_DEPLOYMENTS_TARGET
:MLflow AI Gateway 的 URL
-
使用 mlflow models serve 命令启动 MLflow Model Server。例如,在本地机器的 shell 中运行以下命令将在端口 8000 上提供模型服务
mlflow models serve --model-uri models:/mlflow_docs_qa_model/1 --port 8000
-
服务器启动后,可以通过 REST API 调用进行查询。例如
input='
{
"dataframe_records": [
{
"article": "An MLflow Project is a format for packaging data science code...",
"question": "What is an MLflow Project?"
}
]
}'
echo $input | curl \
-s \
-X POST \
https://localhost:8000/invocations
-H 'Content-Type: application/json' \
-d @-其中
article
和question
替换为您提示模板中的输入变量。