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

プロンプト レジストリ参照

備考

ベータ版

この機能は ベータ版です。

概要

MLflow プロンプト レジストリは、プロンプト テンプレートをライフサイクル全体にわたって管理するための一元化されたリポジトリです。これにより、チームは次のことが可能になります。

  • Git のようなバージョン管理、コミット メッセージ、ロールバック機能を備えた バージョン管理と追跡プロンプト
  • A/Bテストや段階的なロールアウトのために、変更可能な参照(例:「本番運用」、「ステージング」)を使用した エイリアスで安全にデプロイ
  • コードを変更せずに共同作業を行うには 、エンジニア以外のユーザーが UI を使用してプロンプトを変更できるようにします
  • LangChain、LlamaIndex、その他の生成AIフレームワークを含む 任意のフレームワークと統合
  • Unity Catalog の統合によるアクセス制御と監査証跡の ガバナンスの維持
  • プロンプトをエクスペリメントと評価結果にリンクすることで リネージを追跡

前提 条件

  1. Unity Catalog をサポートする MLflow をインストールします。

    Bash
    pip install --upgrade "mlflow[databricks]>=3.1.0"
  2. MLflow エクスペリメントを作成するには、環境のセットアップに関するクイックスタートに従ってください。

  3. Unity Catalog スキーマへのアクセス CREATE FUNCTION

    • なぜでしょうか。プロンプトは、関数として UC に格納されます
注記

プロンプト レジストリを使用するには、 CREATE FUNCTION アクセス許可を持つ Unity Catalog スキーマが必要です。Databricks 試用版アカウントを使用している場合は、Unity Catalog スキーマ workspace.defaultに対する CREATE TABLE 権限があります。

クイックスタート

プロンプト レジストリを使用するための基本的なワークフローを次に示します。

Python
import mlflow

# Register a prompt template
prompt = mlflow.genai.register_prompt(
name="mycatalog.myschema.customer_support",
template="You are a helpful assistant. Answer this question: {{question}}",
commit_message="Initial customer support prompt"
)
print(f"Created version {prompt.version}") # "Created version 1"

# Set a production alias
mlflow.genai.set_prompt_alias(
name="mycatalog.myschema.customer_support",
alias="production",
version=1
)

# Load and use the prompt in your application
prompt = mlflow.genai.load_prompt(name_or_uri="prompts:/mycatalog.myschema.customer_support@production")
response = llm.invoke(prompt.render(question="How do I reset my password?"))

コアコンセプト

SDK の概要

プロンプト レジストリには、次の 6 つの主要な操作があります。

関数

目的

register_prompt()

新しいプロンプトを作成するか、新しいバージョンを追加する

load_prompt()

特定のプロンプトバージョンまたはエイリアスを取得する

search_prompts()

名前、タグ、またはメタデータによるプロンプトの検索

set_prompt_alias()

エイリアス ポインターを作成または更新する

delete_prompt_alias()

エイリアスの削除 (バージョンは残る)

delete_prompt()

プロンプト全体または特定のバージョンを削除する

register_prompt()

新しいプロンプトを作成するか、既存のプロンプトに新しいバージョンを追加します。

Python
mlflow.genai.register_prompt(
name: str,
template: str,
commit_message: str,
tags: dict = None
) -> PromptVersion

パラメーター

パラメーター

Type

必須

説明

name

str

Yes

Unity Catalog 名 (catalog.schema.prompt_name)

template

str

Yes

プレースホルダーの {{variables}} を含むプロンプト テンプレート

commit_message

str

No

変更の説明 (git コミット メッセージなど)

tags

dict

No

このバージョンのキーと値のタグ

収益

PromptVersion 以下を含むオブジェクト:

  • name: プロンプト名
  • version: バージョン番号 (自動インクリメント)
  • template: プロンプトテンプレート
  • commit_message: コミット メッセージ
  • creation_timestamp: バージョンが作成された日時

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

バージョン番号またはエイリアスでプロンプトを取得します。

Python
mlflow.genai.load_prompt(
name_or_uri: str,
version: Optional[Union[str, int]] = None,
allow_missing: bool = False,
) -> Optional[PromptVersion]

パラメーター

パラメーター

Type

必須

説明

name_or_uri

str

Yes

プロンプトの名前、または「prompts:/name/version」形式のURI

version

Optional[Union[str, int]]

No

プロンプトのバージョン (name を使用する場合は必須、URI を使用する場合は許可されません)

allow_missing

bool

No

True の場合、指定したプロンプトが見つからない場合は Exception を発生させる代わりに None を返します

URI の形式

Python
# Load specific version
prompt = mlflow.genai.load_prompt(name_or_uri="prompts:/mycatalog.myschema.chat_prompt/3")

# Load by alias
prompt = mlflow.genai.load_prompt(name_or_uri="prompts:/mycatalog.myschema.chat_prompt@production")

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 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.render(
question="What is MLflow?",
context="MLflow is an open source platform..."
))

search_prompts()

Unity Catalogのプロンプトをカタログおよびスキーマ別にリストします。

Python
mlflow.genai.search_prompts(
filter_string: str,
max_results: int = None
) -> PagedList[Prompt]

パラメーター

パラメーター

Type

必須

説明

filter_string

str

Yes

カタログと スキーマを指定する必要があります。 "catalog = 'name' AND schema = 'name'"

max_results

int

No

返される最大プロンプト数

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

特定のバージョンを指すエイリアスを作成または更新します。

Python
mlflow.genai.set_prompt_alias(
name: str,
alias: str,
version: int
) -> None

パラメーター

パラメーター

Type

必須

説明

name

str

Yes

プロンプトの名前Unity Catalog

alias

str

Yes

エイリアス名(例:「本番運用」、「ステージング」)

version

int

Yes

エイリアスを指すバージョン番号

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

基になるバージョンに影響を与えずにエイリアスを削除します。

Python
mlflow.genai.delete_prompt_alias(
name: str,
alias: str
) -> None

パラメーター

パラメーター

Type

必須

説明

name

str

Yes

プロンプトの名前Unity Catalog

alias

str

Yes

削除するエイリアス名

delete_prompt() と delete_prompt_version()

プロンプトを完全に削除するか、特定のバージョンを削除する。

delete_prompt_version()

プロンプトの特定のバージョンを削除します。

Python
from mlflow import MlflowClient

client = MlflowClient()
client.delete_prompt_version(name: str, version: str) -> None

パラメーター

パラメーター

Type

必須

説明

name

str

Yes

プロンプトの名前Unity Catalog

version

str

Yes

削除するバージョン (文字列として)

delete_prompt()

レジストリからプロンプト全体を削除します。

Python
from mlflow import MlflowClient

client = MlflowClient()
client.delete_prompt(name: str) -> None

パラメータ

パラメーター

Type

必須

説明

name

str

Yes

プロンプトの名前Unity Catalog

重要な注意事項:

  • Unity Catalog レジストリの場合、バージョンがまだ存在する場合、 delete_prompt() は失敗します。すべてのバージョンは、最初に delete_prompt_version()を使用して削除する必要があります。
  • 他のレジストリの種類の場合、 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")

データモデル

プロンプト レジストリ データ モデル

プロンプト レジストリは、Git に似たモデルに従います。

  • プロンプト : Unity Catalog の名前付きエンティティ
  • バージョン : 自動インクリメント番号を持つ不変のスナップショット
  • エイリアス : 特定のバージョンへの変更可能なポインタ
  • タグ : バージョン固有のキーと値のペア

一般的なパターン

テンプレート変数

変数には二重中括弧構文を使用します。

Python
# Single variable
simple_prompt = mlflow.genai.register_prompt(
name="mycatalog.myschema.greeting",
template="Hello {{name}}, how can I help you today?",
commit_message="Simple greeting"
)

# Multiple variables with formatting
complex_prompt = mlflow.genai.register_prompt(
name="mycatalog.myschema.analysis",
template="""Analyze the following {{data_type}} data:


{{data}}

Consider these factors:

{{factors}}

Output format: {{output_format}}""",
commit_message="Multi-variable analysis template"
)

# Use the prompt
rendered = complex_prompt.render(
data_type="sales",
data="Q1: $1.2M, Q2: $1.5M...",
factors="seasonality, market trends",
output_format="bullet points"
)

バージョン管理ワークフロー

Python
# Development workflow
def develop_prompt(base_name: str, changes: str):
"""Iterate on prompts during development"""

# Register new version
new_version = mlflow.genai.register_prompt(
name=base_name,
template=changes,
commit_message=f"Dev iteration: {datetime.now()}"
)

# Update dev alias
mlflow.genai.set_prompt_alias(
name=base_name,
alias="dev",
version=new_version.version
)

return new_version

# Promotion workflow
def promote_prompt(name: str, from_env: str, to_env: str):
"""Promote prompt from one environment to another"""

# Get current version in source environment
source = mlflow.genai.load_prompt(f"prompts:/{name}@{from_env}")

# Point target environment to same version
mlflow.genai.set_prompt_alias(
name=name,
alias=to_env,
version=source.version
)

print(f"Promoted {name} v{source.version} from {from_env} to {to_env}")

エイリアス戦略

Python
# Standard environment aliases
ENVIRONMENT_ALIASES = ["dev", "staging", "production"]

# Feature branch aliases
def create_feature_alias(prompt_name: str, feature: str, version: int):
"""Create alias for feature development"""
mlflow.genai.set_prompt_alias(
name=prompt_name,
alias=f"feature-{feature}",
version=version
)

# Regional aliases
REGIONAL_ALIASES = {
"us": "production-us",
"eu": "production-eu",
"asia": "production-asia"
}

# Rollback-ready aliases
def safe_production_update(name: str, new_version: int):
"""Update production with rollback capability"""
try:
# Save current production
current = mlflow.genai.load_prompt(f"prompts:/{name}@production")
mlflow.genai.set_prompt_alias(name, "production-previous", current.version)
except:
pass # No current production

# Update production
mlflow.genai.set_prompt_alias(name, "production", new_version)

フレームワークの統合

LangChain

Python
from langchain_core.prompts import ChatPromptTemplate

# Load from registry
mlflow_prompt = mlflow.genai.load_prompt("prompts:/mycatalog.myschema.chat@production")

# Convert to LangChain format
langchain_template = mlflow_prompt.to_single_brace_format()
chat_prompt = ChatPromptTemplate.from_template(langchain_template)

# Use in chain
chain = chat_prompt | llm | output_parser

LlamaIndex

Python
from llama_index.core import PromptTemplate

# Load and convert
mlflow_prompt = mlflow.genai.load_prompt("prompts:/mycatalog.myschema.rag@production")
llama_template = PromptTemplate(mlflow_prompt.to_single_brace_format())

# Use in query engine
query_engine.update_prompts({"response_synthesizer:text_qa_template": llama_template})

次のステップ