プロンプトの最適化チュートリアル
このチュートリアルの例では、分類タスクに GEPA と GPT-OSS 20B を使用して、MLflow プロンプト最適化によって このクエリを分類する 単純なプロンプトを最適化します。
依存関係をインストールする
Python
%pip install --upgrade mlflow databricks-sdk dspy openai
dbutils.library.restartPython()
これを正常に実行するには、 Databricks基盤モデルAPIsにアクセスできることを確認してください。
Python
import mlflow
import openai
from mlflow.genai.optimize import GepaPromptOptimizer
from mlflow.genai.scorers import Correctness
from databricks.sdk import WorkspaceClient
w = WorkspaceClient()
# Change the catalog and schema to your catalog and schema
catalog = ""
schema = ""
prompt_registry_name = "qa"
prompt_location = f"{catalog}.{schema}.{prompt_registry_name}"
openai_client = w.serving_endpoints.get_open_ai_client()
# Register initial prompt
prompt = mlflow.genai.register_prompt(
name=prompt_location,
template="classify this: {{query}}",
)
# Define your prediction function
def predict_fn(query: str) -> str:
prompt = mlflow.genai.load_prompt(f"prompts:/{prompt_location}/1")
completion = openai_client.chat.completions.create(
model="databricks-gpt-oss-20b",
# load prompt template using PromptVersion.format()
messages=[{"role": "user", "content": prompt.format(question=query)}],
)
return completion.choices[0].message.content
関数をテストする
必要最低限のプロンプトでモデルが入力をどれだけ正確に分類できるか観察します。正確ではありますが、探しているタスクやユースケースには適合しません。
Python
from IPython.display import Markdown
output = predict_fn("The emergence of HIV as a chronic condition means that people living with HIV are required to take more responsibility for the self-management of their condition , including making physical , emotional and social adjustments.")
Markdown(output[1]['text'])
データに基づいて最適化する
予想される応答と事実を含むデータを提供することで、ユースケースに適した方法でモデルの動作と出力を最適化できます。
この場合、モデルには 5 つの単語の中から 1 つの単語を出力するようにする必要があります。それ以上の説明はなしで、その単語のみを出力する必要があります。
Python
# Training data with inputs and expected outputs
dataset = [
{
"inputs": {"query": "The emergence of HIV as a chronic condition means that people living with HIV are required to take more responsibility for the self-management of their condition , including making physical , emotional and social adjustments."},
"outputs": {"response": "BACKGROUND"},
"expectations": {"expected_facts": ["Classification label must be 'CONCLUSIONS', 'RESULTS', 'METHODS', 'OBJECTIVE', 'BACKGROUND'"]}
},
{
"inputs": {"query": "This paper describes the design and evaluation of Positive Outlook , an online program aiming to enhance the self-management skills of gay men living with HIV ."},
"outputs": {"response": "BACKGROUND"},
"expectations": {"expected_facts": ["Classification label must be 'CONCLUSIONS', 'RESULTS', 'METHODS', 'OBJECTIVE', 'BACKGROUND'"]}
},
{
"inputs": {"query": "This study is designed as a randomised controlled trial in which men living with HIV in Australia will be assigned to either an intervention group or usual care control group ."},
"outputs": {"response": "METHODS"},
"expectations": {"expected_facts": ["Classification label must be 'CONCLUSIONS', 'RESULTS', 'METHODS', 'OBJECTIVE', 'BACKGROUND'"]}
},
{
"inputs": {"query": "The intervention group will participate in the online group program ` Positive Outlook ' ."},
"outputs": {"response": "METHODS"},
"expectations": {"expected_facts": ["Classification label must be 'CONCLUSIONS', 'RESULTS', 'METHODS', 'OBJECTIVE', 'BACKGROUND'"]}
},
{
"inputs": {"query": "The program is based on self-efficacy theory and uses a self-management approach to enhance skills , confidence and abilities to manage the psychosocial issues associated with HIV in daily life ."},
"outputs": {"response": "METHODS"},
"expectations": {"expected_facts": ["Classification label must be 'CONCLUSIONS', 'RESULTS', 'METHODS', 'OBJECTIVE', 'BACKGROUND'"]}
},
{
"inputs": {"query": "Participants will access the program for a minimum of 90 minutes per week over seven weeks ."},
"outputs": {"response": "METHODS"},
"expectations": {"expected_facts": ["Classification label must be 'CONCLUSIONS', 'RESULTS', 'METHODS', 'OBJECTIVE', 'BACKGROUND'"]}
}
]
# Optimize the prompt
result = mlflow.genai.optimize_prompts(
predict_fn=predict_fn,
train_data=dataset,
prompt_uris=[prompt.uri],
optimizer=GepaPromptOptimizer(reflection_model="databricks:/databricks-claude-sonnet-4-5"),
scorers=[Correctness(model="databricks:/databricks-gpt-5")],
)
# Use the optimized prompt
optimized_prompt = result.optimized_prompts[0]
print(f"Optimized template: {optimized_prompt.template}")
プロンプトを確認する
MLflowエクスペリメントへのリンクを開いて次のステップを完了すると、エクスペリメントにプロンプトが表示されます。
- エクスペリメント タイプが GenAI アプリとエージェントに設定されていることを確認してください
- プロンプトタブに移動する
- 右上の スキーマの選択を クリックし、上で設定したのと同じスキーマを入力するとプロンプトが表示されます。
新しいプロンプトをロードして再度テストします
プロンプトがどのように見えるかを確認し、それを予測関数に読み込んで、モデルのパフォーマンスがどのように異なるかを確認します。
Python
from IPython.display import Markdown
prompt = mlflow.genai.load_prompt(f"prompts:/{prompt_location}/34")
Markdown(prompt.template)
Python
from IPython.display import Markdown
def predict_fn(query: str) -> str:
prompt = mlflow.genai.load_prompt(f"prompts:/{prompt_location}/34")
completion = openai_client.chat.completions.create(
model="databricks-gpt-oss-20b",
# load prompt template using PromptVersion.format()
messages=[{"role": "user", "content": prompt.format(query=query)}],
)
return completion.choices[0].message.content
output = predict_fn("The emergence of HIV as a chronic condition means that people living with HIV are required to take more responsibility for the self-management of their condition , including making physical , emotional and social adjustments.")
Markdown(output[1]['text'])
サンプルノートブック
以下は、MLflow GenAI Prompt Optimization with GEPA を使用してプロンプトを最適化し、GPT-OSS 20B を使用して分類タスクを示す実行可能なノートブックです。