次の方法で共有


カスタム エバリュエーター

組み込みエバリュエーターを使用すると、何も設定せずにアプリケーションの世代の評価をすぐに開始できます。 しかし、特定の評価ニーズに対応するために、コードベースまたはプロンプトベースの独自のエバリュエーターを構築することが必要な場合があります。

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

場合によっては、特定の評価メトリックに大規模言語モデルが必要ないことがあります。 このような場合、コードベースのエバリュエーターを使用すると、関数または呼び出し可能なクラスに基づいてメメトリックを柔軟に定義できます。 独自のコードベースのエバリュエータを構築できます。そのためには、たとえば、ディレクトリ answer_length.py の下の answer_len/ で回答の長さを計算する単純な Python クラスを作成します。

コード ベースのエバリュエーターの例: 回答の長さ

class AnswerLengthEvaluator:
    def __init__(self):
        pass
    # A class is made a callable my implementing the special method __call__
    def __call__(self, *, answer: str, **kwargs):
        return {"answer_length": len(answer)}

次に、呼び出し可能なクラスをインポートし、データ行に対してエバリュエータを実行します。

from answer_len.answer_length import AnswerLengthEvaluator

answer_length_evaluator = AnswerLengthEvaluator()
answer_length = answer_length_evaluator(answer="What is the speed of light?")

コード ベースのエバリュエーター出力: 応答の長さ

{"answer_length":27}

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

独自のプロンプトベースの大規模言語モデル エバリュエータまたは AI 支援アノテーターを構築するために、Prompty ファイルに基づいてカスタム エバリュエータを作成できます。 Prompty は、プロンプト テンプレートを開発するためのファイルであり、拡張子 .prompty が付きます。 Prompty アセットは、フロント マターが変更されたマークダウン ファイルです。 フロント マターは YAML 形式であり、Prompty のモデル構成と予期される入力を定義する多くのメタデータ フィールドが含まれています。 応答の使いやすさを測定するカスタム エバリュエータ FriendlinessEvaluator を作成してみましょう。

プロンプト ベースのエバリュエーターの例: 親しみやすさエバリュエーター

まず、使いやすさメトリックとその採点ルーブリックの定義を記述する friendliness.prompty ファイルを作成します。

---
name: Friendliness Evaluator
description: Friendliness Evaluator to measure warmth and approachability of answers.
model:
  api: chat
  configuration:
    type: azure_openai
    azure_endpoint: ${env:AZURE_OPENAI_ENDPOINT}
    azure_deployment: gpt-4o-mini
  parameters:
    model:
    temperature: 0.1
inputs:
  response:
    type: string
outputs:
  score:
    type: int
  explanation:
    type: string
---

system:
Friendliness assesses the warmth and approachability of the answer. Rate the friendliness of the response between one to five stars using the following scale:

One star: the answer is unfriendly or hostile

Two stars: the answer is mostly unfriendly

Three stars: the answer is neutral

Four stars: the answer is mostly friendly

Five stars: the answer is very friendly

Please assign a rating between 1 and 5 based on the tone and demeanor of the response.

**Example 1**
generated_query: I just don't feel like helping you! Your questions are getting very annoying.
output:
{"score": 1, "reason": "The response is not warm and is resisting to be providing helpful information."}
**Example 2**
generated_query: I'm sorry this watch is not working for you. Very happy to assist you with a replacement.
output:
{"score": 5, "reason": "The response is warm and empathetic, offering a resolution with care."}


**Here the actual conversation to be scored:**
generated_query: {{response}}
output:

次に、Prompty ファイルを読み込み、json 形式で出力を処理するクラス FriendlinessEvaluator を作成します。

import os
import json
import sys
from promptflow.client import load_flow


class FriendlinessEvaluator:
    def __init__(self, model_config):
        current_dir = os.path.dirname(__file__)
        prompty_path = os.path.join(current_dir, "friendliness.prompty")
        self._flow = load_flow(source=prompty_path, model={"configuration": model_config})

    def __call__(self, *, response: str, **kwargs):
        llm_response = self._flow(response=response)
        try:
            response = json.loads(llm_response)
        except Exception as ex:
            response = llm_response
        return response

これで、独自の Prompty ベースのエバリュエーターを作成し、データ行で実行できます。

from friendliness.friend import FriendlinessEvaluator

friendliness_eval = FriendlinessEvaluator(model_config)

friendliness_score = friendliness_eval(response="I will not apologize for my behavior!")

プロンプト ベースのエバリュエーターの出力: 親しみやすさエバリュエーター

{
    'score': 1, 
    'reason': 'The response is hostile and unapologetic, lacking warmth or approachability.'
}