在应用中使用提示
创建并版本化 Prompt 后,下一步是将它们集成到您的 GenAI 应用程序中。本文档将指导您如何加载 Prompt、绑定变量、处理版本控制,并通过将 Prompt 版本链接到已记录的 MLflow 模型来确保完整的溯源。
从注册表中加载 Prompt
在应用程序代码中访问已注册 Prompt 的主要方式是使用 mlflow.genai.load_prompt() 函数。此函数从注册表中检索特定的 Prompt 版本(或由别名指向的版本)。
它使用特殊的 URI 格式:prompts:/<prompt_name>/<version_or_alias>。
<prompt_name>:注册表中 Prompt 的唯一名称。<version_or_alias>:可以是特定版本号(例如1、2)或别名(例如production、latest-dev)。
import mlflow
prompt_name = "my-sdk-prompt"
# Load the latest version of the prompt
mlflow.genai.load_prompt(name_or_uri=f"prompts:/{prompt_name}@latest")
# 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 对象(即 Prompt 实例)后,您可以使用 prompt.format() 方法填充其模板变量。此方法接受关键字参数,其中键与 Prompt 模板中的变量名称匹配(不带 {{ }} 花括号)。
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
)
将 Prompt 与其他框架(LangChain、LlamaIndex)一起使用
MLflow Prompt 使用 {{variable}} 双花括号语法进行模板化。其他一些流行框架,如 LangChain 和 LlamaIndex,通常期望使用单花括号语法(例如 {variable})。为方便无缝集成,MLflow Prompt 对象提供了 prompt.to_single_brace_format() 方法。此方法返回已转换为单花括号格式的 Prompt 模板字符串,可供这些框架使用。
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)
将 Prompt 链接到已记录的模型以实现完整溯源
为实现完整的可复现性和可追溯性,至关重要的是记录您的应用程序(或模型)使用的具体 Prompt 版本。当您将 GenAI 应用程序记录为 MLflow Model(例如,使用 mlflow.pyfunc.log_model()、mlflow.langchain.log_model() 等)时,应包含有关其使用的注册表中 Prompt 的信息。
MLflow 的设计旨在促进这一点。当模型被记录,并且该模型的代码通过 mlflow.genai.load_prompt() 使用 prompts:/ URI 加载 Prompt 时,MLflow 可以在已记录模型的元数据中自动记录这些 Prompt 依赖项。
此链接的好处
- 可复现性:了解模型版本使用的确切 Prompt 版本,可以精确地复现其行为。
- 调试:如果模型版本开始出现意外行为,您可以轻松检查底层 Prompt 的更改(即使是通过别名更新的)是否是原因。
- 审计和治理:维护一份关于特定模型版本使用了哪些 Prompt 的清晰记录。
- 影响分析:了解如果某个 Prompt 版本出现问题,可能会影响哪些模型。