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

プロンプトの作成と編集

備考

ベータ版

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

このガイドでは、MLflow Python SDK を使用して、MLflow プロンプト レジストリで新しいプロンプトを作成し、そのバージョンを管理する方法について説明します。このページのすべてのコードは、 サンプル ノートブックに含まれています。

前提 条件

  1. MLflow と必要なパッケージをインストールする

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

  3. Unity Catalog スキーマに対する CREATE FUNCTION 権限が必要です。プロンプトは、Unity Catalog に関数として格納されます。

注記

プロンプト レジストリを使用するには、Unity Catalog スキーマに対する CREATE FUNCTION 特権が必要です。Databricks 試用版アカウントを使用している場合は、Unity Catalog スキーマ に対する必要な権限workspace.default.

ステップ1.新しいプロンプトを作成する

プロンプトは、Python SDK を使用してプログラムで作成できます。

プロンプトは、 mlflow.genai.register_prompt()を使用してプログラムで作成します。プロンプトでは、テンプレート変数に二重中括弧構文 ({{variable}}) を使用します。

Python
import mlflow

# Replace with a Unity Catalog schema where you have CREATE FUNCTION privileges
uc_schema = "workspace.default"
# This table will be created in the UC schema specified in the previous line
prompt_name = "summarization_prompt"

# Define the prompt template with variables
initial_template = """\
Summarize content you are provided with in {{num_sentences}} sentences.

Content: {{content}}
"""

# Register a new prompt
prompt = mlflow.genai.register_prompt(
name=f"{uc_schema}.{prompt_name}",
template=initial_template,
# all parameters below are optional
commit_message="Initial version of summarization prompt",
tags={
"author": "data-science-team@company.com",
"use_case": "document_summarization",
"task": "summarization",
"language": "en",
"model_compatibility": "gpt-4"
}
)

print(f"Created prompt '{prompt.name}' (version {prompt.version})")

ステップ 2: アプリケーションでプロンプトを使用する

次の手順では、プロンプト テンプレートを使用する簡単なアプリケーションを作成します。

  1. レジストリからプロンプトを読み込みます。
Python
# Load a specific version using URI syntax
prompt = mlflow.genai.load_prompt(name_or_uri=f"prompts:/{uc_schema}.{prompt_name}/1")

# Alternative syntax without URI
prompt = mlflow.genai.load_prompt(name_or_uri=f"{uc_schema}.{prompt_name}", version="1")
  1. アプリケーションでプロンプトを使用します。
Python
import mlflow
from openai import OpenAI

# Enable MLflow's autologging to instrument your application with Tracing
mlflow.openai.autolog()

# Connect to a Databricks LLM via OpenAI using the same credentials as MLflow
# Alternatively, you can use your own OpenAI credentials here
mlflow_creds = mlflow.utils.databricks_utils.get_databricks_host_creds()
client = OpenAI(
api_key=mlflow_creds.token,
base_url=f"{mlflow_creds.host}/serving-endpoints"
)

# Use the trace decorator to capture the application's entry point
@mlflow.trace
def my_app(content: str, num_sentences: int):
# Format with variables
formatted_prompt = prompt.format(
content=content,
num_sentences=num_sentences
)

response = client.chat.completions.create(
model="databricks-claude-sonnet-4", # This example uses a Databricks hosted LLM - you can replace this with any AI Gateway or Model Serving endpoint. If you provide your own OpenAI credentials, replace with a valid OpenAI model e.g., gpt-4o, etc.
messages=[
{
"role": "system",
"content": "You are a helpful assistant.",
},
{
"role": "user",
"content": formatted_prompt,
},
],
)
return response.choices[0].message.content

result = my_app(content="This guide shows you how to integrate prompts from the MLflow Prompt Registry into your GenAI applications. You'll learn to load prompts, format them with dynamic data, and ensure complete lineage by linking prompt versions to your MLflow Models.", num_sentences=1)
print(result)

ステップ3.プロンプトの編集

プロンプト・バージョンは、作成後に不変です。プロンプトを編集するには、新しいバージョンを作成する必要があります。この Git のようなバージョン管理により、完全な履歴が保証され、ロールバックが可能になります。

既存のプロンプト名で mlflow.genai.register_prompt() を呼び出して、新しいバージョンを作成します。

Python
import mlflow

# Define the improved template
new_template = """\
You are an expert summarizer. Condense the following content into exactly {{ num_sentences }} clear and informative sentences that capture the key points.

Content: {{content}}

Your summary should:
- Contain exactly {{num_sentences}} sentences
- Include only the most important information
- Be written in a neutral, objective tone
- Maintain the same level of formality as the original text
"""

# Register a new version
updated_prompt = mlflow.genai.register_prompt(
name=f"{uc_schema}.{prompt_name}",
template=new_template,
commit_message="Added detailed instructions for better output quality",
tags={
"author": "data-science-team@company.com",
"improvement": "Added specific guidelines for summary quality"
}
)

print(f"Created version {updated_prompt.version} of '{updated_prompt.name}'")

ステップ4.新しいプロンプトを使用する

次のコードは、プロンプトの使用方法を示しています。

Python
# Load a specific version using URI syntax
prompt = mlflow.genai.load_prompt(name_or_uri=f"prompts:/{uc_schema}.{prompt_name}/2")

# Or load from specific version
prompt = mlflow.genai.load_prompt(name_or_uri=f"{uc_schema}.{prompt_name}", version="2")

ステップ5.プロンプトの検索と検出

Unity Catalog スキーマでプロンプトを検索するには、次のようにします。

Python
# REQUIRED format for Unity Catalog - specify catalog and schema
results = mlflow.genai.search_prompts("catalog = 'workspace' AND schema = 'default'")

# Using variables for your schema
catalog_name = uc_schema.split('.')[0] # 'workspace'
schema_name = uc_schema.split('.')[1] # 'default'
results = mlflow.genai.search_prompts(f"catalog = '{catalog_name}' AND schema = '{schema_name}'")

# Limit results
results = mlflow.genai.search_prompts(
filter_string=f"catalog = '{catalog_name}' AND schema = '{schema_name}'",
max_results=50
)

ノートブックの例

プロンプトの作成と編集のサンプル ノートブック

Open notebook in new tab

次のステップ