プロンプトレジストリの例
このページでは、プロンプトレジストリの操作例を示します。
register_prompt()
APIリファレンス: mlflow.genai.register_prompt
Python
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
以下のコードは、特定のプロンプトバージョンを読み込むためのさまざまな方法を示しています。
Python
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 Catalogプロンプト レジストリの場合は、カタログとスキーマの両方を指定する必要があります。
Python
# 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 Catalog ではサポートされていません。
- 名前のパターン:
name LIKE '%pattern%' - タグフィルタリング:
tags.field = 'value' - 名前の完全一致:
name = 'specific.name' - カタログ + スキーマを超えたフィルターの組み合わせ
特定のプロンプトを見つけるには、返されたリストを使用してプログラムでフィルタリングします。
Python
# 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
Python
# 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
プロンプトの特定のバージョンを削除します。
Python
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 Catalogレジストリの場合、バージョンがまだ存在すると
delete_prompt()失敗します。 まずdelete_prompt_version()使用してすべてのバージョンを削除する必要があります。 - その他のレジストリ タイプの場合、
delete_prompt()バージョン チェックなしで正常に動作します。
APIリファレンス: MlflowClient.delete_prompt
Python
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")