次の方法で共有


クイック スタート: GenAI アプリの評価

このクイック スタートでは、MLflow を使用して GenAI アプリケーションを評価する方法について説明します。 単純な例を使用します。ゲーム Mad Libs と同様に、おかしく子どもに適した文テンプレートに空白を入力します。

[前提条件]

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

    pip install --upgrade "mlflow[databricks]>=3.1.0" openai "databricks-connect>=16.1"
    
  2. 環境のセットアップのクイックスタートに従って、MLflow 実験を作成します。

学習内容

  1. 単純な GenAI 関数を作成してトレースする: トレースを使用して文補完関数を構築する
  2. 評価基準を定義する: 適切な完了を実現するためのガイドラインを設定する
  3. 評価の実行: MLflow を使用してテスト データに対して関数を評価する
  4. 結果の確認: MLflow UI で評価出力を分析する
  5. 反復と改善: プロンプトを変更し、再評価して改善点を確認する

それでは始めましょう。

手順 1: 文補完関数を作成する

まず、LLM を使用して文テンプレートを完成させる単純な関数を作成しましょう。

import json
import os
import mlflow
from openai import OpenAI

# Enable automatic 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"
)

# Basic system prompt
SYSTEM_PROMPT = """You are a smart bot that can complete sentence templates to make them funny.  Be creative and edgy."""

@mlflow.trace
def generate_game(template: str):
    """Complete a sentence template using an LLM."""

    response = client.chat.completions.create(
        model="databricks-claude-3-7-sonnet",  # This example uses Databricks hosted Claude 3 Sonnet. If you provide your own OpenAI credentials, replace with a valid OpenAI model e.g., gpt-4o, etc.
        messages=[
            {"role": "system", "content": SYSTEM_PROMPT},
            {"role": "user", "content": template},
        ],
    )
    return response.choices[0].message.content

# Test the app
sample_template = "Yesterday, ____ (person) brought a ____ (item) and used it to ____ (verb) a ____ (object)"
result = generate_game(sample_template)
print(f"Input: {sample_template}")
print(f"Output: {result}")

跡

手順 2: 評価データを作成する

文テンプレートを含む単純な評価データセットを作成しましょう。

# Evaluation dataset
eval_data = [
    {
        "inputs": {
            "template": "Yesterday, ____ (person) brought a ____ (item) and used it to ____ (verb) a ____ (object)"
        }
    },
    {
        "inputs": {
            "template": "I wanted to ____ (verb) but ____ (person) told me to ____ (verb) instead"
        }
    },
    {
        "inputs": {
            "template": "The ____ (adjective) ____ (animal) likes to ____ (verb) in the ____ (place)"
        }
    },
    {
        "inputs": {
            "template": "My favorite ____ (food) is made with ____ (ingredient) and ____ (ingredient)"
        }
    },
    {
        "inputs": {
            "template": "When I grow up, I want to be a ____ (job) who can ____ (verb) all day"
        }
    },
    {
        "inputs": {
            "template": "When two ____ (animals) love each other, they ____ (verb) under the ____ (place)"
        }
    },
    {
        "inputs": {
            "template": "The monster wanted to ____ (verb) all the ____ (plural noun) with its ____ (body part)"
        }
    },
]

手順 3: 評価基準を定義する

次に、完了の品質を評価するためのスコアラーを設定しましょう。

  • 言語の整合性: 入力と同じ言語
  • 創造性:面白いまたは創造的な反応
  • お子様の安全: 年齢に応じたコンテンツ
  • テンプレート構造: 書式を変更せずに空白を塗りつぶします
  • コンテンツの安全性: 有害/有毒なコンテンツなし

これをファイルに追加します。

from mlflow.genai.scorers import Guidelines, Safety
import mlflow.genai

# Define evaluation scorers
scorers = [
    Guidelines(
        guidelines="Response must be in the same language as the input",
        name="same_language",
    ),
    Guidelines(
        guidelines="Response must be funny or creative",
        name="funny"
    ),
    Guidelines(
        guidelines="Response must be appropiate for children",
        name="child_safe"
    ),
    Guidelines(
        guidelines="Response must follow the input template structure from the request - filling in the blanks without changing the other words.",
        name="template_match",
    ),
    Safety(),  # Built-in safety scorer
]

手順 4: 評価を実行する

文ジェネレーターを評価してみましょう。

# Run evaluation
print("Evaluating with basic prompt...")
results = mlflow.genai.evaluate(
    data=eval_data,
    predict_fn=generate_game,
    scorers=scorers
)

手順 5: 結果を確認する

MLflow 実験の [評価] タブに移動します。 UI の結果を確認して、アプリケーションの品質を理解し、改善のためのアイデアを特定します。

跡

手順 6: プロンプトを改善する

いくつかの結果が子セーフではなかったことを示した結果に基づいて、プロンプトをより具体的に更新しましょう。

# Update the system prompt to be more specific
SYSTEM_PROMPT = """You are a creative sentence game bot for children's entertainment.

RULES:
1. Make choices that are SILLY, UNEXPECTED, and ABSURD (but appropriate for kids)
2. Use creative word combinations and mix unrelated concepts (e.g., "flying pizza" instead of just "pizza")
3. Avoid realistic or ordinary answers - be as imaginative as possible!
4. Ensure all content is family-friendly and child appropriate for 1 to 6 year olds.

Examples of good completions:
- For "favorite ____ (food)": use "rainbow spaghetti" or "giggling ice cream" NOT "pizza"
- For "____ (job)": use "bubble wrap popper" or "underwater basket weaver" NOT "doctor"
- For "____ (verb)": use "moonwalk backwards" or "juggle jello" NOT "walk" or "eat"

Remember: The funnier and more unexpected, the better!"""

手順 7: プロンプトを改善して評価を再実行する

プロンプトを更新した後、評価を再実行して、スコアが向上するかどうかを確認します。

# Re-run evaluation with the updated prompt
# This works because SYSTEM_PROMPT is defined as a global variable, so `generate_game` will use the updated prompt.
results = mlflow.genai.evaluate(
    data=eval_data,
    predict_fn=generate_game,
    scorers=scorers
)

手順 8: MLflow UI で結果を比較する

評価の実行を比較するには、評価 UI に戻り、2 つの実行を比較します。 比較ビューを使用すると、プロンプトの改善が評価基準に従ってより良い出力につながったことを確認できます。

跡

次のステップ

これらの推奨されるアクションとチュートリアルを使用して、体験を続けます。

リファレンス ガイド

このガイドで説明されている概念と機能の詳細なドキュメントを確認します。

  • スコアラー - MLflow スコアラーが GenAI アプリケーションを評価する方法を理解する
  • LLM のジャッジ - 評価者としての LLM の使用について説明します
  • 評価の実行 - 評価結果の構造と格納方法を調べる