创建和编辑提示
本指南将引导您完成在 MLflow Prompt Registry 中创建新提示及其版本管理的过程。
创建新提示
您可以通过两种主要方式在 MLflow Prompt Registry 中启动一个新提示:通过 MLflow UI 或使用 Python SDK 以编程方式。
- UI
- Python
- 在您的 MLflow 实例中导航到“提示注册表”(Prompt Registry)部分。
- 点击“创建提示”(Create Prompt)(或类似)按钮。
- 填写提示的详细信息,例如名称、提示模板文本和提交消息(可选)

要以编程方式创建新提示,请使用 mlflow.genai.register_prompt() 函数。这对于自动化提示创建或将提示管理作为更大脚本的一部分特别有用。
python
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 }}
"""
# Chat Style template
initial_template = [
{
"role": "system",
"content": "Summarize content you are provided with in {{ num_sentences }} sentences.",
},
{"role": "user", "content": "Sentences: {{ sentences }}"},
]
# Optional Response Format
from pydantic import BaseModel, Field
class ResponseFormat(BaseModel):
summary: str = Field(..., description="Summary of the content")
# Register a new prompt
prompt = mlflow.genai.register_prompt(
name="summarization-prompt",
template=initial_template,
# Optional: Provide Response Format to get structured output
response_format=ResponseFormat,
# 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})")
编辑现有提示(创建新版本)
一旦创建了提示版本,其模板和初始元数据就是不可变的。编辑现有提示意味着创建该提示的新版本来包含您的更改。这种类似于 Git 的行为确保了完整的历史记录,并允许您在需要时回滚到之前的版本。
- UI
- Python
- 在提示注册表中导航到您希望编辑的特定提示。
- 选择您想要基于其创建新版本的版本(通常是最新版本)。
- 查找“编辑提示”(Edit Prompt)或“创建新版本”(Create New Version)按钮。
- 根据需要修改模板、更新元数据或更改标签。
- 提供一条新的提交消息来描述您为这个新版本所做的更改。

要创建现有提示的新版本,您仍然使用 mlflow.genai.register_prompt() 函数,但这次您需要提供现有提示的name。MLflow 将自动递增版本号。
python
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",
},
)
理解不可变性
重要的是要记住,MLflow Prompt Registry 中的提示版本是不可变的。一旦调用了 mlflow.genai.register_prompt() 并创建了一个版本(或创建了现有提示的新版本),该特定版本的模板、初始提交消息和初始元数据就无法更改。这种设计选择为可重现性和血缘跟踪提供了强大的保证。
如果您需要更改提示,您总是会创建一个新版本。
比较提示版本
MLflow UI 提供了比较不同提示版本的方法。这通常包括一个并排的差异视图,允许您轻松地查看不同版本之间的模板文本、元数据或标签发生了什么变化。
