创建和编辑提示
本指南将引导您完成在 MLflow 提示注册表中创建新提示并管理其版本的流程。
创建新提示
您可以通过两种主要方式在 MLflow 提示注册表中启动新提示:通过 MLflow UI 或使用 Python SDK 编程方式。
- UI
- Python
- 导航到 MLflow 实例中的“提示注册表”部分。
- 点击“创建提示”按钮(或类似按钮)。
- 填写提示详细信息,例如名称、提示模板文本和提交消息(可选)
要以编程方式创建新提示,请使用 mlflow.genai.register_prompt()
函数。这对于自动化提示创建或将提示作为大型脚本的一部分进行管理特别有用。
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})")
编辑现有提示(创建新版本)
一旦创建了提示版本,其模板和初始元数据是不可变的。编辑现有提示意味着创建该提示的新版本并包含您的更改。这种类似 Git 的行为确保了完整的历史记录,并允许您在需要时回滚到以前的版本。
- UI
- Python
- 导航到提示注册表中您希望编辑的特定提示。
- 选择您希望作为新版本基础的版本(通常是最新版本)。
- 寻找“编辑提示”或“创建新版本”按钮。
- 根据需要修改模板、更新元数据或更改标签。
- 提供一个新的提交消息,描述您为此新版本所做的更改。
要创建现有提示的新版本,您再次使用 mlflow.genai.register_prompt()
函数,但这次,您需要提供现有提示的 name
。MLflow 将自动增加版本号。
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 提示注册表中的提示版本是不可变的。一旦调用 mlflow.genai.register_prompt()
并创建了一个版本(或现有提示的新版本),该特定版本的模板、初始提交消息和初始元数据就无法更改。这种设计选择为可重现性和沿袭跟踪提供了强有力的保证。
如果您需要更改提示,您总是创建一个新版本。
比较提示版本
MLflow UI 提供了比较提示不同版本的工具。这通常包括并排的差异视图,允许您轻松查看版本之间模板文本、元数据或标签的更改。