その他のカスタム エバリュエーター (プレビュー)

Important

この記事でプレビューとしてマークされているアイテムは、現在プレビュー段階です。 このプレビューはサービス レベル アグリーメント(SLA)なしで提供されており、Microsoft はこれを運用ワークロードでの使用に推奨していません。 特定の機能がサポートされていないか、機能が制限されている可能性があります。 詳細については、「 Microsoft Azure プレビューの追加使用条件」を参照してください。

重み付けされた多次元条件が必要な場合、または Foundry で開始ルーブリックを生成する必要がある場合は、 ルーブリック エバリュエーター を使用します。 プロンプト ベースのエバリュエーターは、1 つの LLM ジャッジ プロンプトのみが必要な場合、または既存のプロンプト エバリュエーターで構築する場合に、実行可能でシンプルな選択肢です。 独自のホスト型インフラストラクチャ、外部データ ソース、または既存の評価サービスを必要とする新しいカスタム コードには、 エンドポイント ベースのエバリュエーター を使用します。 コード ベースのエバリュエーターは、既存のスコアリング スクリプト、またはネットワーク アクセスを必要としない自己完結型のサンドボックスPython関数に制約されたシナリオに適合します。

カスタム エバリュエーターは、 組み込みのエバリュエーター カタログを超えるドメイン固有の品質メトリックを定義します。 プロンプト ベースのエバリュエーターとコード ベースのエバリュエーターは Foundry 評価サービス内で実行されるため、独自のエンドポイントをホストする必要はありません。 どちらもサンドボックス環境で実行されます。プロンプト ベースのエバリュエーターは、LLM が各項目について評価するジャッジ プロンプトを使用し、コードベースのエバリュエーターは、制限されたパッケージとネットワーク アクセスなしでPythonスコアリング関数を実行します。

Foundry ポータルまたは SDK からプロンプト ベースのエバリュエーターとコード ベースのエバリュエーターを作成および管理します。 ポータルでは、SDK コードを記述せずに両方の種類が作成されます。 エバリュエーターをプログラムで管理する場合、または既存の CI/CD ワークフローに作成を統合する場合は、SDK を使用します。

SDK の例の前提条件

この記事の SDK の例では、プロジェクト クライアントが作成されていることを前提としています。 SDK をインストールしてクライアントを 1 回セットアップした後、各エバリュエーターの種類の例全体で project_client と client を再利用します。

pip install "azure-ai-projects>=2.8.0" azure-identity
import os
from azure.identity import DefaultAzureCredential
from azure.ai.projects import AIProjectClient

# Foundry project endpoint
# Example:
# https://<account_name>.services.ai.azure.com/api/projects/<project_name>
endpoint = os.environ["FOUNDRY_PROJECT_ENDPOINT"]

# Model deployment name (required for prompt-based and code-based evaluators)
# Example: gpt-5-mini
model_deployment_name = os.environ["FOUNDRY_MODEL_NAME"]

credential = DefaultAzureCredential()

# Create the project client
project_client = AIProjectClient(endpoint=endpoint, credential=credential)

# Get the OpenAI client for evaluation API
client = project_client.get_openai_client()

プロンプトベースのエバリュエーター

Tip

重み付け、多次元条件、または Foundry でエージェントのコンテキストから開始ルーブリックを生成する場合は、 ルーブリック エバリュエーター を使用します。 1 つの焦点を絞ったジャッジ プロンプトまたは既存のプロンプト エバリュエーターを維持するには、プロンプト ベースのエバリュエーターを使用します。

プロンプト ベースのエバリュエーターは、LLM が各項目について評価するジャッジ プロンプト テンプレートを使用します。 テンプレート変数では、二重中かっこ ( {{query}} など) を使用し、入力データ フィールドにマップします。

プロンプト ベースのエバリュエーターでは、次の 3 つのスコアリング方法がサポートされています。

  • 序数: 定義した不連続スケールの整数スコア (例: 1 ~ 5)。 高い方が良いです。
  • 連続: 定義した範囲 (0.0 ~ 1.0 など) でのきめ細かい測定の浮動小数点スコア。 高い方が良いです。
  • バイナリ (true/false): しきい値ベースのチェックのブール結果。

エバリュエーターは、 result と reasonを持つ JSON オブジェクトを返す必要があります。 resultの型は、スコアリング メソッドと一致します。序数の整数、連続する場合は float、バイナリの場合はブール値です。

次のプロンプト例では、序数スコアリング (1 ~ 5) を使用して応答の使いやすさを評価します。

Friendliness assesses the warmth and approachability of the response.
Rate the friendliness of the response between one and five.
Use the following scale:

1 - Unfriendly or hostile
2 - Mostly unfriendly
3 - Neutral
4 - Mostly friendly
5 - Very friendly

Assign a rating based on the tone and demeanor of the response.

Response:
{{response}}

Output Format (JSON):
{
  "result": <integer from 1 to 5>,
  "reason": "<brief explanation for the score>"
}

ランタイム パラメーター

プロンプト ベースのエバリュエーターを作成するときは、初期化パラメーターとして deployment_name と threshold の両方が必要です。

評価にエバリュエーターを追加するときに、入力フィールドマッピングと共にパラメーターを指定します。

testing_criteria = [
    {
        "type": "azure_ai_evaluator",
        "name": "friendliness_evaluator",
        "evaluator_name": "friendliness_evaluator",
        "data_mapping": {
            "response": "{{item.response}}",
        },
        "initialization_parameters": {
            "deployment_name": model_deployment_name,
            "threshold": 3,
        },
    },
]

SDK を使用してプロンプト ベースのエバリュエーターを作成する

prompt_textフィールドにジャッジプロンプトを渡します。 プロンプトで必要な入力フィールドを宣言する data_schema と、スコアリング方法と範囲を記述する metrics を定義します。 init_parametersは、実行時にエバリュエーターが必要とするモデルのデプロイとしきい値を宣言します。

from azure.ai.projects.models import EvaluatorCategory, EvaluatorDefinitionType

prompt_evaluator = project_client.evaluators.create_version(
    name="friendliness_evaluator",
    evaluator_version={
        "name": "friendliness_evaluator",
        "categories": [EvaluatorCategory.QUALITY],
        "display_name": "Friendliness Evaluator",
        "description": "Evaluates the warmth and approachability of a response",
        "definition": {
            "type": EvaluatorDefinitionType.PROMPT,
            "prompt_text": (
                "Friendliness assesses the warmth and approachability "
                "of the response.\n"
                "Rate the friendliness of the response between one and five.\n"
                "Use the following scale:\n\n"
                "1 - Unfriendly or hostile\n"
                "2 - Mostly unfriendly\n"
                "3 - Neutral\n"
                "4 - Mostly friendly\n"
                "5 - Very friendly\n\n"
                "Assign a rating based on the tone and demeanor "
                "of the response.\n\n"
                "Response:\n{{response}}\n\n"
                "Output Format (JSON):\n"
                '{\n  "result": <integer from 1 to 5>,\n'
                '  "reason": "<brief explanation for the score>"\n}\n'
            ),
            "init_parameters": {
                "type": "object",
                "properties": {
                    "deployment_name": {"type": "string"},
                    "threshold": {"type": "number"},
                },
                "required": ["deployment_name", "threshold"],
            },
            "data_schema": {
                "type": "object",
                "properties": {
                    "response": {"type": "string"},
                },
                "required": ["response"],
            },
            "metrics": {
                "custom_prompt": {
                    "type": "ordinal",
                    "desirable_direction": "increase",
                    "min_value": 1,
                    "max_value": 5,
                }
            },
        },
    },
    # Custom evaluator definitions are in preview and require this opt-in header.
    headers={"Foundry-Features": "Evaluations=V1Preview"},
)

完全な例については、prompt ベースのエバリュエーター Python SDK サンプルを参照してください。

コードベースのエバリュエーター

コード ベースのエバリュエーターは、2 つの dict パラメーター (grade と sample) を受け取り、0.0 から 1.0 までの浮動小数点スコアを返す、item という名前のPython関数です (上位の方が適しています)。 実際には、すべてのデータは itemを介してアクセスされます。

  • データセットの評価: responseやground_truthなどの入力フィールドは、item.get("response")やitem.get("ground_truth")などのPython コードで取得できます。
  • モデルまたはエージェントターゲットの評価: 生成された応答テキストをフェッチするには、 item.get("sample", {}).get("output_text")を使用します。

Note

現在、モデルまたはエージェント ターゲットから生成された応答テキストには、 item.get("sample", {}).get("output_text")経由でアクセスされます。 このアクセス パターンは、今後の API 更新プログラムで変更される可能性があります。

次の例では、長さに基づいて応答をスコア付けし、50 ~ 500 文字の応答を優先します。

def grade(sample: dict, item: dict) -> float:
    """Score based on response length (prefer 50-500 chars)."""
    # For dataset evaluation, access fields directly from item:
    response = item.get("response", "")

    # For model/agent target evaluation, use item.get("sample") instead:
    # response = item.get("sample", {}).get("output_text", "")

    if not response:
        return 0.0

    length = len(response)
    if length < 50:
        return 0.2
    if length > 500:
        return 0.5
    return 1.0

Note

grade()関数が例外を発生させるかタイムアウトすると、サービスはそのアイテムの結果を0.0として記録し、評価レポートでエラーとしてマークします。 意味のあるスコアを返すことができる場合にのみ、予期される例外をキャッチします。

サポートされているパッケージと制限

コード ベースのエバリュエーターは、次の制約を持つサンドボックス Python環境で実行されます。

  • コード サイズは 256 KB 未満にする必要があります。
  • 実行は、評価呼び出しごとに 2 分に制限されます。
  • 実行時に使用できるネットワーク アクセスはありません。
  • メモリ制限は 2 GB、ディスク制限は 1 GB、CPU は 2 コアに制限されます。

次のサード パーティ製パッケージを使用できます。

Package Version
numpy 2.2.4
scipy 1.15.2
pandas 2.2.3
scikit-learn 1.6.1
rapidfuzz 3.10.1
sympy 1.13.3
jsonschema 4.23.0
pydantic 2.10.6
deepdiff 8.4.2
nltk 3.9.1
rouge-score 0.1.2
pyyaml 6.0.2

NLTK コーパス punkt、 stopwords、 wordnet、 omw-1.4、および names がプリロードされます。

スコアリング ロジックに、この一覧にないパッケージが必要な場合、ネットワーク アクセスが必要な場合、またはこれらの制限を超えている場合は、代わりに エンドポイント ベースのエバリュエーター を使用してください。

ランタイム パラメーター

pass_threshold および deployment_name は、コード ベースのエバリュエーターを作成するときに初期化パラメーターとして必要です。 コード ベースのエバリュエーターが LLM を呼び出さない場合でも、サービス API スキーマでは評価実行オーケストレーションに deployment_name が必要です。 プロジェクトから任意の有効なモデル 配置名を渡すことができます。

評価にエバリュエーターを追加するときにパラメーターを指定します。

testing_criteria = [
    {
        "type": "azure_ai_evaluator",
        "name": "response_length_scorer",
        "evaluator_name": "response_length_scorer",
        "initialization_parameters": {
            "deployment_name": model_deployment_name,
            "pass_threshold": 0.5,
        },
    },
]

SDK を使用してコード ベースのエバリュエーターを作成する

grade()関数を文字列として code_text フィールドに渡します。 関数が期待する入力フィールドを宣言する data_schema と、関数が返すスコアを記述する metrics を定義します。 コード ベースのエバリュエーターは、0.0 から 1.0 の範囲の continuous メトリック型を使用します。

from azure.ai.projects.models import EvaluatorCategory, EvaluatorDefinitionType

code_evaluator = project_client.evaluators.create_version(
    name="response_length_scorer",
    evaluator_version={
        "name": "response_length_scorer",
        "categories": [EvaluatorCategory.QUALITY],
        "display_name": "Response Length Scorer",
        "description": (
            "Scores responses based on length, preferring 50-500 characters"
        ),
        "definition": {
            "type": EvaluatorDefinitionType.CODE,
            "code_text": (
                'def grade(sample: dict, item: dict) -> float:\n'
                '    """Score based on response length."""\n'
                '    response = item.get("response", "")\n'
                '    if not response:\n'
                '        return 0.0\n'
                '    length = len(response)\n'
                '    if length < 50:\n'
                '        return 0.2\n'
                '    if length > 500:\n'
                '        return 0.5\n'
                '    return 1.0\n'
            ),
            "init_parameters": {
                "type": "object",
                "properties": {
                    "deployment_name": {"type": "string"},
                    "pass_threshold": {"type": "number"},
                },
                "required": ["deployment_name", "pass_threshold"],
            },
            "metrics": {
                "result": {
                    "type": "continuous",
                    "desirable_direction": "increase",
                    "min_value": 0.0,
                    "max_value": 1.0,
                }
            },
            "data_schema": {
                "type": "object",
                "required": ["item"],
                "properties": {
                    "item": {
                        "type": "object",
                        "properties": {
                            "response": {"type": "string"},
                        },
                    },
                },
            },
        },
    },
    # Custom evaluator definitions are in preview and require this opt-in header.
    headers={"Foundry-Features": "Evaluations=V1Preview"},
)

完全な例については、code ベースのエバリュエーター Python SDK サンプルを参照してください。

ポータルでプロンプト ベースまたはコード ベースのエバリュエーターを作成する

Foundry ポータルのカスタム エバリュエーター フローでは、SDK コードを使用せずに、プロンプト ベースのエバリュエーターとコード ベースのエバリュエーターが作成されます。 新しい LLM-as-judge 条件の場合は、 ルーブリック エバリュエーターを作成します。

  1. Foundry プロジェクトで、 Evaluation>Evaluator カタログに移動します。
  2. [カスタム エバリュエーター>作成] を選択します。
  3. 以下のフィールドに記入してください。
フィールド Description
名前 エバリュエーターの一意識別子 (たとえば、 response_length_scorer)。
表示名 エバリュエーター カタログに表示される人間が判読できる名前。
Description エバリュエーターが測定する内容の簡単な概要。
タイプ プロンプトベース または コードベース。 ジャッジ プロンプトとPython grade()関数のどちらを指定するかを決定します。
スコアリング メソッド プロンプト ベースのエバリュエーターは、カスタム範囲で序数、連続、またはバイナリ スコアリングを使用できます。 コード ベースのエバリュエーターは継続的 (0.0 ~ 1.0) を使用します。
プロンプトまたはコード プロンプト ベースの場合は、プロンプト エディターでジャッジ プロンプトを記述します。 コード ベースの場合は、コード エディターで grade() 関数を記述します。 例と要件については、この記事で前述したプロンプト ベースのエバリュエーターとコード ベースのエバリュエーターのセクションを参照してください。

ポータル評価でカスタム エバリュエーターを使用する

カスタム エバリュエーターを作成したら、ポータルから評価実行で使用します。

  1. Foundry プロジェクトで、[ 評価 ] に移動し、[ 作成] を選択します。
  2. 評価作成ウィザードに従います。 [ 条件 ] ステップで、[ エバリュエーターの追加] を選択します。
  3. エバリュエーター カタログからカスタム エバリュエーターを選択します。
  4. 必要な初期化パラメーターを指定します。 プロンプト ベースのエバリュエーターの場合は、 モデルのデプロイ と しきい値を指定します。 コード ベースのエバリュエーターの場合は、 モデルのデプロイ を指定し、 しきい値を渡します。
  5. ウィザードを完了し、評価の実行を開始します。

ポータルから評価を実行する詳細な手順については、「ポータル から評価を実行する」を参照してください。

会話レベルのカスタム ロジック

コード ベースのエバリュエーターでは、ターン レベルの評価のみがサポートされます。 会話レベルの評価実行では、それらをスキップします。 カスタム コードを使用して会話全体をスコア付けするには、 エンドポイント ベースのエバリュエーター (プレビュー) を使用します。このエバリュエーターは、実行で evaluation_levelconversationに設定されている場合に完全な会話を受け取ります。