跳到主要内容

在应用中使用提示

在 MLflow Prompt Registry 中创建提示词并进行版本控制后,下一步关键步骤是将它们集成到您的 GenAI 应用程序中。本页面将指导您如何加载提示词、绑定变量、处理版本控制,以及通过将提示词版本链接到您记录的 MLflow 模型来确保完整的沿袭。

从注册表中加载提示词

在应用程序代码中访问已注册提示词的主要方法是使用 mlflow.genai.load_prompt() 函数。 此函数从注册表中检索特定的提示词版本(或别名指向的版本)。

它使用一种特殊的 URI 格式:prompts:/<prompt_name>/<version_or_alias>

  • <prompt_name>:注册表中提示词的唯一名称。
  • <version_or_alias>:可以是特定的版本号(例如,12)或别名(例如,productionlatest-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 可以自动记录这些提示词依赖项,作为已记录模型的元数据的一部分。

此链接的好处

  • 可重复性:了解模型版本使用的确切提示词版本,使您可以精确地重现其行为。
  • 调试:如果模型版本开始出现异常行为,您可以轻松检查底层提示词的更改(即使通过别名更新)是否是原因。
  • 审计和治理:维护有关给定模型版本使用了哪些提示词的清晰记录。
  • 影响分析:了解如果发现特定提示词版本存在问题,哪些模型可能会受到影响。