MLflow 中的提示词管理
什么是 MLflow 提示词注册表?
MLflow 提示词注册表是一个功能强大的工具,它能简化生成式 AI (GenAI) 应用中的提示词工程和管理。它使你能够在整个组织中对提示词进行版本控制、跟踪和重用,从而有助于保持一致性并改善提示词开发的协作。
- 可重用性 - 将提示词存储在集中式注册表中并在多个应用中重用它们。
- 版本控制 - 使用类似 Git 的基于提交的版本控制来跟踪提示词的演进,并通过差异突出显示对提示词版本进行并排比较。
- 别名 - 使用别名为提示词构建稳健而灵活的部署管道,使你能够将提示词版本与主应用代码隔离开来,并轻松执行 A/B 测试和回滚等任务。
- 血缘 - 与 MLflow 现有功能(如模型跟踪和评估)无缝集成,实现端到端的 GenAI 生命周期管理。
- 协作 - 通过集中式注册表在整个组织中共享提示词,使团队能够在他人的工作基础上进行构建。
入门
1. 创建提示词
- 用户界面 (UI)
- Python
- 在终端中运行
mlflow ui
以启动 MLflow 用户界面。 - 导航到 MLflow 用户界面中的提示词选项卡。
- 点击创建提示词按钮。
- 填写提示词详细信息,例如名称、提示词模板文本和提交消息(可选)。
- 点击创建以注册提示词。
提示词模板文本可以包含 {{variable}}
格式的变量。在使用这些提示词的 GenAI 应用中,可以使用动态内容填充这些变量。MLflow 还提供了一个实用方法,可以将模板转换为 LangChain 或 LlamaIndex 等框架使用的单括号格式。
要使用 Python API 创建新的提示词,请使用 mlflow.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.register_prompt(
name="summarization-prompt",
template=initial_template,
# Optional: Provide a commit message to describe the changes
commit_message="Initial commit",
# Optional: Specify any additional metadata about the prompt version
version_metadata={
"author": "author@example.com",
},
# Optional: Set tags applies to the prompt (across versions)
tags={
"task": "summarization",
"language": "en",
},
)
# The prompt object contains information about the registered prompt
print(f"Created prompt '{prompt.name}' (version {prompt.version})")
这将创建一个新的提示词,其中包含指定的模板文本和元数据。该提示词现在可以在 MLflow 用户界面中进行进一步管理。
2. 使用新版本更新提示词
- 用户界面 (UI)
- Python
- 上一步将引导你进入已创建的提示词页面。(如果你关闭了该页面,请导航到 MLflow 用户界面中的提示词选项卡,然后点击提示词名称。)
- 点击创建提示词版本按钮。
- 弹出的对话框会预填充现有提示词文本。根据需要修改提示词。
- 点击创建以注册新版本。
要使用新版本更新现有提示词,请使用 mlflow.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.register_prompt(
name="summarization-prompt", # Specify the existing prompt name
template=new_template,
commit_message="Improvement",
version_metadata={
"author": "author@example.com",
},
)
3. 比较提示词版本
一旦你有了提示词的多个版本,就可以比较它们以了解版本之间的变化。要在 MLflow 用户界面中比较提示词版本,请在提示词详细信息页面中点击比较选项卡
4. 加载和使用提示词
要在 GenAI 应用中使用提示词,可以使用 mlflow.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.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)
提示词对象
Prompt
对象是 MLflow 提示词注册表中的核心实体。它表示一个带版本控制的模板文本,可以包含用于动态内容的变量。
提示词对象的关键属性
Name
: 提示词的唯一标识符。Template
: 提示词的文本,可以包含{{variable}}
格式的变量。Version
: 表示提示词修订的序列号。Commit Message
: 对提示词版本中所做更改的描述,类似于 Git 提交消息。Version Metadata
: 用于向提示词版本添加元数据的可选键值对。例如,你可以使用此字段跟踪提示词版本的作者。Tags
: 在提示词级别(跨版本)分配的用于分类和过滤的可选键值对。例如,你可以添加项目名称、语言等标签,这些标签适用于提示词的所有版本。Alias
: 对提示词的可变命名引用。例如,你可以创建一个名为production
的别名,以引用在生产系统中使用的版本。有关更多详细信息,请参阅别名。
常见问题 (FAQ)
问:如何删除提示词版本?
答:你可以使用 MLflow 用户界面或 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.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']
问:提示词注册表与提示词工程用户界面集成了吗?
答:提示词注册表与提示词工程用户界面的直接集成即将推出。在此期间,你可以在提示词工程用户界面中迭代提示词模板,并通过手动复制提示词模板将最终版本注册到提示词注册表中。