提示词注册表
MLflow Prompt Registry 是一个强大的工具,可简化您生成式 AI (GenAI) 应用程序中的提示工程和管理。它使您能够在整个组织中对提示进行版本控制、跟踪和重用,有助于保持一致性并改善提示开发中的协作。
主要优势
版本控制
通过受 Git 启发的基于提交的版本控制和带有差异高亮的并排比较来跟踪提示的演变。MLflow 中的提示版本是不可变的,为可复现性提供了强有力的保证。
别名
为提示构建健壮而灵活的部署管道,使您能够将提示版本与主应用程序代码隔离,并轻松执行 A/B 测试和回滚等任务。
血缘关系
与 MLflow 的现有功能(如模型跟踪和评估)无缝集成,以实现端到端的 GenAI 生命周期管理。
协作
通过集中式注册表在您的组织中共享提示,使团队能够建立在彼此工作的基础上。
开始使用
1. 创建提示
- UI
- Python

- 在终端中运行
mlflow ui以启动 MLflow UI。 - 在 MLflow UI 中导航到“提示”选项卡。
- 点击“创建提示”按钮。
- 填写提示详细信息,例如名称、提示模板文本和提交消息(可选)。
- 点击“创建”以注册提示。
提示模板文本可以包含 {{variable}} 格式的变量。在使用提示于 GenAI 应用程序时,这些变量可以用动态内容填充。MLflow 还提供了 to_single_brace_format() API,可将模板转换为单括号格式,以适应像 LangChain 或 LlamaIndex 这样需要单括号插值的框架。
要使用 Python API 创建新提示,请使用 mlflow.genai.register_prompt() API。
import mlflow
# Use double curly braces for variables in the template
initial_template = """\
Summarize content you are provided with in {{ num_sentences }} sentences.
Sentences: {{ sentences }}
"""
# Register a new prompt
prompt = mlflow.genai.register_prompt(
name="summarization-prompt",
template=initial_template,
# Optional: Provide a commit message to describe the changes
commit_message="Initial commit",
# Optional: Set tags applies to the prompt (across versions)
tags={
"author": "author@example.com",
"task": "summarization",
"language": "en",
},
)
# The prompt object contains information about the registered prompt
print(f"Created prompt '{prompt.name}' (version {prompt.version})")
这将创建一个具有指定模板文本和元数据的新提示。该提示现在可在 MLflow UI 中进行进一步管理。

2. 使用新版本更新提示
- UI
- Python

- 上一步将导向已创建的提示页面。(如果您关闭了页面,请导航到 MLflow UI 中的“提示”选项卡,然后点击提示名称。)
- 点击“创建提示版本”按钮。
- 弹出对话框已预填充现有提示文本。您可以根据需要修改提示。
- 点击“创建”以注册新版本。
要使用新版本更新现有提示,请使用具有现有提示名称的 mlflow.genai.register_prompt() API。
import mlflow
new_template = """\
You are an expert summarizer. Condense the following content into exactly {{ num_sentences }} clear and informative sentences that capture the key points.
Sentences: {{ sentences }}
Your summary should:
- Contain exactly {{ num_sentences }} sentences
- Include only the most important information
- Be written in a neutral, objective tone
- Maintain the same level of formality as the original text
"""
# Register a new version of an existing prompt
updated_prompt = mlflow.genai.register_prompt(
name="summarization-prompt", # Specify the existing prompt name
template=new_template,
commit_message="Improvement",
tags={
"author": "author@example.com",
},
)
3. 比较提示版本
一旦您拥有多个提示版本,就可以比较它们以了解版本之间的更改。要在 MLflow UI 中比较提示版本,请在提示详细信息页面的“比较”选项卡中进行。

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 Prompt Registry 中的核心实体。它表示一个可包含用于动态内容的变量的版本化模板文本。
Prompt 对象的关键属性
名称:提示的唯一标识符。模板:提示的内容,可以是- 包含
{{variable}}格式变量的文本字符串(文本提示) - 表示具有 'role' 和 'content' 键的聊天消息的字典列表(聊天提示)
- 包含
版本:表示提示修订版的顺序编号。提交消息:对提示版本中所做更改的描述,类似于 Git 提交消息。标签:可选的键值对,在提示版本级别分配,用于分类和过滤。例如,您可以为项目名称、语言等添加标签,这些标签适用于提示的所有版本。别名:对提示的可变命名引用。例如,您可以创建一个名为production的别名来引用您的生产系统中使用的版本。有关更多详细信息,请参阅 别名。is_text_prompt:一个布尔属性,指示提示是文本提示(True)还是聊天提示(False)。response_format:一个可选属性,包含预期的响应结构规范,可用于验证或结构化 LLM 调用的输出。
提示类型
文本提示
文本提示使用简单的字符串模板,其中变量用双花括号括起来。
text_template = "Hello {{ name }}, how are you today?"
聊天提示
聊天提示使用消息字典列表,每个字典都有 'role' 和 'content' 键。
chat_template = [
{"role": "system", "content": "You are a helpful {{ style }} assistant."},
{"role": "user", "content": "{{ question }}"},
]
响应格式
response_format 属性允许您指定 LLM 调用响应的预期结构。这可以是 Pydantic 模型类或定义架构的字典。
from pydantic import BaseModel
class SummaryResponse(BaseModel):
summary: str
key_points: list[str]
word_count: int
# Or as a dictionary
response_format_dict = {
"type": "object",
"properties": {
"summary": {"type": "string"},
"key_points": {"type": "array", "items": {"type": "string"}},
"word_count": {"type": "integer"},
},
}
管理提示和版本标签
MLflow 允许您在提示注册后修改和检查标签。标签可以应用于提示级别,也可以应用于单个提示版本。
import mlflow
# Prompt-level tag operations
mlflow.genai.set_prompt_tag("summarization-prompt", "language", "en")
mlflow.genai.get_prompt_tags("summarization-prompt")
mlflow.genai.delete_prompt_tag("summarization-prompt", "language")
# Prompt-version tag operations
mlflow.genai.set_prompt_version_tag("summarization-prompt", 1, "author", "alice")
mlflow.genai.load_prompt("prompts:/summarization-prompt/1").tags
mlflow.genai.delete_prompt_version_tag("summarization-prompt", 1, "author")
常见问题
问:如何删除提示版本?
答:您可以使用 MLflow UI 或 Python API 删除提示版本。
import mlflow
# Delete a prompt version
client = mlflow.MlflowClient()
client.delete_prompt_version("summarization-prompt", version=2)
为避免意外删除,通过 API 一次只能删除一个版本。如果您删除了提示的所有版本,提示本身也将被删除。
问:我可以更新现有提示版本的提示模板吗?
答:不可以,提示版本一旦创建即不可变。要更新提示,请创建一个包含所需更改的新版本。
问:如何动态加载提示的最新版本?
答:您可以使用 @latest 别名加载提示的最新版本。这是保留的别名名称,MLflow 将自动查找提示的可用最新版本。
prompt = mlflow.genai.load_prompt("prompts:/summarization-prompt@latest")
问:我可以使用像 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']
问:Prompt Registry 是否与 Prompt Engineering UI 集成?
答:Prompt Registry 和 Prompt Engineering UI 之间的直接集成即将推出。在此期间,您可以在 Prompt Engineering UI 中迭代提示模板,并通过手动复制提示模板将其最终版本注册到 Prompt Registry。