在应用中使用提示
在 MLflow 提示注册表中创建并版本化提示后,下一步关键是将其集成到您的 GenAI 应用程序中。本页指导您如何加载提示、绑定变量、处理版本控制,并通过将提示版本链接到您记录的 MLflow 模型来确保完整的血缘。
从注册表加载提示
在应用程序代码中访问已注册提示的主要方式是使用 mlflow.genai.load_prompt()
函数。此函数从注册表中检索特定提示版本(或别名指向的版本)。
它使用特殊的 URI 格式:prompts:/<prompt_name>/<version_or_alias>
。
<prompt_name>
:注册表中提示的唯一名称。<version_or_alias>
:可以是特定的版本号(例如,1
、2
)或别名(例如,production
、latest-dev
)。
import mlflow
prompt_name = "my-sdk-prompt"
# Load by specific version (assuming version 1 exists)
mlflow.genai.load_prompt(name_or_uri=f"prompts:/{prompt_name}/1")
# Load by alias (assuming an alias 'staging' points to a version of a prompt)
mlflow.genai.load_prompt(name_or_uri=f"prompts:/{prompt_name}@staging")
使用变量格式化提示
加载提示对象(这是一个 Prompt
实例)后,您可以使用 prompt.format()
方法填充其模板变量。此方法接受关键字参数,其中键与提示模板中的变量名称匹配(不带 {{ }}
大括号)。
import mlflow
# define a prompt template
prompt_template = """\
You are an expert AI assistant. Answer the user's question with clarity, accuracy, and conciseness.
## Question:
{{question}}
## Guidelines:
- Keep responses factual and to the point.
- If relevant, provide examples or step-by-step instructions.
- If the question is ambiguous, clarify before answering.
Respond below:
"""
prompt = mlflow.genai.register_prompt(
name="ai_assistant_prompt",
template=prompt_template,
commit_message="Initial version of AI assistant",
)
question = "What is MLflow?"
response = (
client.chat.completions.create(
messages=[{"role": "user", "content": prompt.format(question=question)}],
model="gpt-4o-mini",
temperature=0.1,
max_tokens=2000,
)
.choices[0]
.message.content
)
与其他框架(LangChain、LlamaIndex)一起使用提示
MLflow 提示使用 {{variable}}
双大括号语法进行模板化。一些其他流行的框架,如 LangChain 和 LlamaIndex,通常期望单大括号语法(例如,{variable}
)。为了方便无缝集成,MLflow Prompt
对象提供了 prompt.to_single_brace_format()
方法。此方法返回转换为单大括号格式的提示模板字符串,可供这些框架使用。
import mlflow
from langchain_core.prompts import ChatPromptTemplate
from langchain_openai import ChatOpenAI
# Load registered prompt
prompt = mlflow.genai.load_prompt("prompts:/summarization-prompt/2")
# Create LangChain prompt object
langchain_prompt = ChatPromptTemplate.from_messages(
[
(
# IMPORTANT: Convert prompt template from double to single curly braces format
"system",
prompt.to_single_brace_format(),
),
("placeholder", "{messages}"),
]
)
# Define the LangChain chain
llm = ChatOpenAI()
chain = langchain_prompt | llm
# Invoke the chain
response = chain.invoke({"num_sentences": 1, "sentences": "This is a test sentence."})
print(response)
将提示链接到已记录的模型以获取完整血缘
为了实现完全的可重现性和可追溯性,记录应用程序(或模型)使用的特定提示版本至关重要。当您将 GenAI 应用程序记录为 MLflow 模型时(例如,使用 mlflow.pyfunc.log_model()
、mlflow.langchain.log_model()
等),您应该包含有关它使用的注册表中的提示信息。
MLflow 旨在促进这一点。当模型被记录时,并且该模型的代码通过 mlflow.genai.load_prompt()
使用 prompts:/
URI 加载提示时,MLflow 可以自动将这些提示依赖项记录为已记录模型的元数据的一部分。
此链接的好处
- 可重现性:了解模型版本使用的确切提示版本,可以精确地重现其行为。
- 调试:如果模型版本开始出现意外行为,您可以轻松检查是否是底层提示的变化(即使通过别名更新)造成的。
- 审计和治理:维护任何给定模型版本使用的提示的清晰记录。
- 影响分析:了解如果发现特定提示版本有问题,哪些模型可能会受到影响。