跳到主要内容

提示词注册表

MLflow 提示词注册表(Prompt Registry)是一个功能强大的工具,可简化您生成式 AI (GenAI) 应用程序中的提示词工程和管理。它使您能够在组织内部对提示词进行版本控制、跟踪和重用,有助于保持一致性并改进提示词开发中的协作。

主要优势

版本控制

通过类似 Git 的基于提交的版本控制和带有差异高亮显示的并排比较来跟踪提示词的演变。MLflow 中的提示词版本是不可变的,为可复现性提供了强有力的保证。

别名

为提示词构建健壮而灵活的部署管道,使您能够将提示词版本与主应用程序代码隔离,并轻松执行 A/B 测试和回滚等任务。

血缘关系

与 MLflow 现有功能(如模型跟踪和评估)无缝集成,实现端到端的 GenAI 生命周期管理。

协作

通过集中式注册表在组织内部共享提示词,使团队能够基于彼此的工作进行构建。

开始使用

1. 创建提示词

Create Prompt UI

  1. 在您的终端中运行 mlflow ui 以启动 MLflow UI。
  2. 导航到 MLflow UI 中的提示词(Prompts)选项卡。
  3. 点击创建提示词(Create Prompt)按钮。
  4. 填写提示词详细信息,例如名称、提示词模板文本和提交消息(可选)。
  5. 点击创建(Create)以注册提示词。
注意

提示词模板文本可以包含 {{variable}} 格式的变量。在使用 GenAI 应用程序中的提示词时,这些变量可以填充动态内容。MLflow 还提供了 to_single_brace_format() API,用于将模板转换为单括号格式,以适应 LangChain 或 LlamaIndex 等需要单括号插值的框架。

这将使用指定的模板文本和元数据创建一个新的提示词。该提示词现在可在 MLflow UI 中进行进一步管理。

Registered Prompt in UI

2. 使用新版本更新提示词

Update Prompt UI

  1. 上一步将跳转到已创建的提示词页面。(如果您关闭了页面,请导航到 MLflow UI 中的提示词(Prompts)选项卡并点击提示词名称。)
  2. 点击创建提示词版本(Create prompt Version)按钮。
  3. 弹出的对话框已预填充现有提示词文本。根据需要修改提示词。
  4. 点击创建(Create)以注册新版本。

3. 比较提示词版本

当您有多个提示词版本时,可以比较它们以了解版本之间的更改。要在 MLflow UI 中比较提示词版本,请点击提示词详细信息页面中的比较(Compare)选项卡

Compare Prompt
Versions

4. 加载和使用提示词

要在您的 GenAI 应用程序中使用提示词,您可以使用 mlflow.genai.load_prompt() API 加载它,并使用提示词对象的 mlflow.entities.Prompt.format() 方法填充变量

import mlflow
import openai

target_text = """
MLflow is an open source platform for managing the end-to-end machine learning lifecycle.
It tackles four primary functions in the ML lifecycle: Tracking experiments, packaging ML
code for reuse, managing and deploying models, and providing a central model registry.
MLflow currently offers these functions as four components: MLflow Tracking,
MLflow Projects, MLflow Models, and MLflow Registry.
"""

# Load the prompt
prompt = mlflow.genai.load_prompt("prompts:/summarization-prompt/2")

# Use the prompt with an LLM
client = openai.OpenAI()
response = client.chat.completions.create(
messages=[
{
"role": "user",
"content": prompt.format(num_sentences=1, sentences=target_text),
}
],
model="gpt-4o-mini",
)

print(response.choices[0].message.content)

5. 搜索提示词

您可以通过名称、标签或其他注册表字段来发现提示词

import mlflow

# Fluent API: returns a flat list of all matching prompts
prompts = mlflow.genai.search_prompts(filter_string="task='summarization'")
print(f"Found {len(prompts)} prompts")

# For pagination control, use the client API:
from mlflow.tracking import MlflowClient

client = MlflowClient()
all_prompts = []
token = None
while True:
page = client.search_prompts(
filter_string="task='summarization'",
max_results=50,
page_token=token,
)
all_prompts.extend(page)
token = page.token
if not token:
break
print(f"Total prompts across pages: {len(all_prompts)}")

提示词对象

Prompt 对象是 MLflow 提示词注册表中的核心实体。它代表一个带版本控制的模板文本,其中可以包含用于动态内容的变量。

提示词对象的关键属性

  • 名称:提示词的唯一标识符。
  • 模板:提示词的文本,可以包含 {{variable}} 格式的变量。
  • 版本:表示提示词修订的连续数字。
  • 提交消息:描述提示词版本中所做更改的说明,类似于 Git 提交消息。
  • 标签:在提示词版本级别分配的可选键值对,用于分类和过滤。例如,您可以添加项目名称、语言等标签,这些标签适用于提示词的所有版本。
  • 别名:指向提示词的可变命名引用。例如,您可以创建一个名为 production 的别名来引用生产系统中使用的版本。更多详细信息请参见别名

常见问题

问:如何删除提示词版本?

答:您可以使用 MLflow UI 或 Python API 删除提示词版本。

import mlflow

# Delete a prompt version
mlflow.delete_prompt("summarization-prompt", version=2)

为避免意外删除,您只能通过 API 一次删除一个版本。如果您删除了某个提示词的所有版本,则该提示词本身将被删除。

问:我可以更新现有提示词版本的提示词模板吗?

答:不能,提示词版本一旦创建就不可变。要更新提示词,请创建包含所需更改的新版本。

问:我可以使用提示词模板与 LangChain 或 LlamaIndex 等框架一起使用吗?

答:是的,您可以从 MLflow 加载提示词并将其与任何框架一起使用。例如,以下示例演示了如何将 MLflow 中注册的提示词与 LangChain 一起使用。另请参阅使用 LangChain 记录提示词以获取更多详细信息。

import mlflow
from langchain.prompts import PromptTemplate

# Load prompt from MLflow
prompt = mlflow.genai.load_prompt("question_answering")

# Convert the prompt to single brace format for LangChain (MLflow uses double braces),
# using the `to_single_brace_format` method.
langchain_prompt = PromptTemplate.from_template(prompt.to_single_brace_format())
print(langchain_prompt.input_variables)
# Output: ['num_sentences', 'sentences']

问:提示词注册表与提示词工程 UI 集成了吗?

答:提示词注册表与提示词工程 UI 之间的直接集成即将推出。在此期间,您可以在提示词工程 UI 中迭代提示词模板,然后通过手动复制提示词模板在提示词注册表中注册最终版本。