メインコンテンツまでスキップ

プロンプトレジストリの例

このページでは、プロンプト レジストリ操作の例を示します。

レジスタプロンプト()

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"
}
)

ロードプロンプト()

APIリファレンス: mlflow.genai.load_prompt

次の例では、URI形式を使用して特定のバージョンをロードします。

Python
# Load specific version using URI format
v2 = mlflow.genai.load_prompt(name_or_uri="prompts:/mycatalog.myschema.qa_prompt/2")

# Load specific version using name + version parameter
v3 = mlflow.genai.load_prompt(name_or_uri="mycatalog.myschema.qa_prompt", version=3)

# Load with allow_missing flag (returns None if not found)
optional_prompt = mlflow.genai.load_prompt(
name_or_uri="mycatalog.myschema.qa_prompt",
version=3,
allow_missing=True
)

# Use the prompt
if optional_prompt is not None:
response = llm.invoke(optional_prompt.format(
question="What is MLflow?",
context="MLflow is an open source platform..."
))

次の例では、URI形式を使用してエイリアスでロードします。

Python

# Load by alias using URI
prod = mlflow.genai.load_prompt(name_or_uri="prompts:/mycatalog.myschema.qa_prompt@production")

# Load with allow_missing flag (returns None if not found)
optional_prompt = mlflow.genai.load_prompt(
name_or_uri="mycatalog.myschema.qa_prompt",
version=3,
allow_missing=True
)

# Use the prompt
if optional_prompt is not None:
response = llm.invoke(optional_prompt.format(
question="What is MLflow?",
context="MLflow is an open source platform..."
))

検索プロンプト()

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()

削除プロンプトバージョン()

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")

削除プロンプト()

プロンプト バージョンを削除したら、プロンプトを削除できます。

important
  • 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")