提示词注册表
MLflow 提示词注册表是一个强大的工具,可简化您生成式 AI (GenAI) 应用程序中的提示词工程和管理。它使您能够在整个组织中对提示词进行版本控制、跟踪和重用,有助于保持一致性并改进提示词开发中的协作。
主要优势
版本控制
通过受 Git 启发的基于提交的版本控制和带差异突出显示的并排比较,跟踪提示词的演变。MLflow 中的提示词版本是不可变的,为可重现性提供了强有力的保证。
别名
构建稳健而灵活的提示词部署管道,允许您将提示词版本与主应用程序代码隔离,并轻松执行 A/B 测试和回滚等任务。
沿袭
与 MLflow 的现有功能无缝集成,例如模型跟踪和评估,以实现端到端 GenAI 生命周期管理。
协作
通过集中式注册表在整个组织中共享提示词,使团队能够在彼此的工作基础上进行构建。
开始使用
1. 创建提示词
- 用户界面
- 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. 使用新版本更新提示词
- 用户界面
- 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 提示词注册表中的核心实体。它表示一个版本化模板文本,其中可以包含用于动态内容的变量。
提示词对象的关键属性
名称
:提示词的唯一标识符。模板
:提示词的内容,可以是- 一个包含带
{{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 UI 或 Python API 删除提示词版本
import mlflow
# Delete a prompt version
client = mlflow.MlflowClient()
client.delete_prompt_version("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 中迭代提示词模板,并通过手动复制提示词模板在提示词注册表中注册最终版本。