提示注册表示例

本页显示提示注册表操作的示例。

register_prompt()

API 参考:mlflow.genai.register_prompt

prompt = mlflow.genai.register_prompt(
    name="mycatalog.myschema.summarization",
    template="""Summarize the following text in {{num_sentences}} sentences:

Text: {{content}}

Focus on: {{focus_areas}}""",
    commit_message="Added focus areas parameter",
    tags={
        "tested_with": "gpt-4",
        "avg_latency_ms": "1200",
        "team": "content",
        "project": "summarization-v2"
    }
)

load_prompt()

API 参考:mlflow.genai.load_prompt

以下代码显示了加载特定提示版本的不同方法。

import mlflow
from databricks.sdk import WorkspaceClient

model = "databricks-claude-sonnet-4-5"
llm = WorkspaceClient().serving_endpoints.get_open_ai_client()

# Load specific version using URI format
mlflow.genai.load_prompt(name_or_uri="prompts:/docs.default.customer_support/1")

# Load specific version using name + version parameter
mlflow.genai.load_prompt(
  name_or_uri="docs.default.customer_support",
  version=3,
  # allow optional parameters to be missing when constructing the prompt
  allow_missing=True,
)

# Use the prompt
prompt = mlflow.genai.load_prompt(name_or_uri="prompts:/docs.default.customer_support/1")
formatted_prompt = prompt.format(question="How do I reset my password?")

response = llm.chat.completions.create(
    model=model,
    messages=[{"role": "user", "content": formatted_prompt}],
)

print(f"\nResponse using version {prompt.version}:")
print(response.choices[0].message.content)

search_prompts()

API 参考:mlflow.genai.search_prompts

Unity Catalog 要求

对于 Unity 目录提示注册表,必须同时指定目录和架构:

# REQUIRED format - list all prompts in a catalog.schema
results = mlflow.genai.search_prompts("catalog = 'mycatalog' AND schema = 'myschema'")

# This is the ONLY supported filter format
results = mlflow.genai.search_prompts("catalog = 'rohit' AND schema = 'default'")

局限性

Unity 目录中不支持以下筛选器:

  • 名称模式: name LIKE '%pattern%'
  • 标签过滤:tags.field = 'value'
  • 确切的名称匹配: name = 'specific.name'
  • 超越目录和架构的组合筛选器

若要查找特定提示,请使用返回的列表并以编程方式进行筛选:

# Get all prompts in schema
all_prompts = mlflow.genai.search_prompts("catalog = 'mycatalog' AND schema = 'myschema'")

# Filter programmatically
customer_prompts = [p for p in all_prompts if 'customer' in p.name.lower()]
tagged_prompts = [p for p in all_prompts if p.tags.get('team') == 'support']

set_prompt_alias()

API 参考:mlflow.genai.set_prompt_alias

# Promote version 3 to production
mlflow.genai.set_prompt_alias(
    name="mycatalog.myschema.chat_assistant",
    alias="production",
    version=3
)

# Set up staging for testing
mlflow.genai.set_prompt_alias(
    name="mycatalog.myschema.chat_assistant",
    alias="staging",
    version=4
)

delete_prompt() 和delete_prompt_version()

delete_prompt_version()

API 参考:MlflowClient.delete_prompt_version

删除提示的特定版本:

from mlflow import MlflowClient

client = MlflowClient()

# Delete specific versions first (required for Unity Catalog)
client.delete_prompt_version("mycatalog.myschema.chat_assistant", "1")
client.delete_prompt_version("mycatalog.myschema.chat_assistant", "2")
client.delete_prompt_version("mycatalog.myschema.chat_assistant", "3")

# Then delete the entire prompt
client.delete_prompt("mycatalog.myschema.chat_assistant")

# For convenience with Unity Catalog, you can also search and delete all versions:
search_response = client.search_prompt_versions("mycatalog.myschema.chat_assistant")
for version in search_response.prompt_versions:
    client.delete_prompt_version("mycatalog.myschema.chat_assistant", str(version.version))

client.delete_prompt("mycatalog.myschema.chat_assistant")

delete_prompt()

删除提示版本后,可以删除提示。

重要

  • 对于 Unity 目录注册表,如果任何版本仍然存在, delete_prompt() 则失败。 必须先使用 delete_prompt_version() 删除所有版本。
  • 对于其他注册表类型, delete_prompt() 通常无需版本检查。

API 参考:MlflowClient.delete_prompt

from mlflow import MlflowClient

client = MlflowClient()

# Delete specific versions first (required for Unity Catalog)
client.delete_prompt_version("mycatalog.myschema.chat_assistant", "1")
client.delete_prompt_version("mycatalog.myschema.chat_assistant", "2")
client.delete_prompt_version("mycatalog.myschema.chat_assistant", "3")

# Then delete the entire prompt
client.delete_prompt("mycatalog.myschema.chat_assistant")

# For convenience with Unity Catalog, you can also search and delete all versions:
search_response = client.search_prompt_versions("mycatalog.myschema.chat_assistant")
for version in search_response.prompt_versions:
    client.delete_prompt_version("mycatalog.myschema.chat_assistant", str(version.version))

client.delete_prompt("mycatalog.myschema.chat_assistant")