Skip to main content
Weave の評価でモデルをスコア付けする場合、絶対値のメトリクス (たとえば、Model A が 9/10、Model B が 8/10) は、相対的なメトリクス (たとえば、Model A のほうが Model B より優れている) よりも、通常は付与しにくくなります。ペアワイズ評価 を使用すると、2 つのモデルの出力を相対的に順位付けして比較できます。この手法は、テキスト生成、要約、質問応答のような主観的なタスクで、どのモデルの性能が優れているかを判断したい場合に役立ちます。ペアワイズ評価では、特定の入力に対してどのモデルが最適かを示す、相対的な選好順位を取得できます。
この方法は回避策であり、今後のリリースで変更される可能性があります。ペアワイズ評価をサポートする、より堅牢な API を提供する予定です。
次のコードサンプルは、PreferenceScorer という class-based scorer を作成して、Weave でペアワイズ評価を実装する方法を示しています。PreferenceScorer は 2 つのモデル ModelAModelB を比較し、入力テキスト内の明示的なヒントに基づいて、モデル出力の相対スコアを返します。
from weave import Model, Evaluation, Scorer, Dataset
from weave.flow.model import ApplyModelError, apply_model_async

class ModelA(Model):
    @weave.op
    def predict(self, input_text: str):
        if "Prefer model A" in input_text:
            return {"response": "This is a great answer from Model A"}
        return {"response": "Meh, whatever"}

class ModelB(Model):
    @weave.op
    def predict(self, input_text: str):
        if "Prefer model B" in input_text:
            return {"response": "This is a thoughtful answer from Model B"}
        return {"response": "I don't know"}

class PreferenceScorer(Scorer):
    @weave.op
    async def _get_other_model_output(self, example: dict) -> Any:
        """Get output from the other model for comparison.
        Args:
            example: The input example data to run through the other model
        Returns:
            The output from the other model
        """

        other_model_result = await apply_model_async(
            self.other_model,
            example,
            None,
        )

        if isinstance(other_model_result, ApplyModelError):
            return None

        return other_model_result.model_output

    @weave.op
    async def score(self, output: dict, input_text: str) -> dict:
        """Compare the output of the primary model with the other model.
        Args:
            output (dict): The output from the primary model.
            input_text (str): The input text used to generate the outputs.
        Returns:
            dict: A flat dictionary containing the comparison result and reason.
        """
        other_output = await self._get_other_model_output(
            {"input_text": input_text}
        )
        if other_output is None:
            return {"primary_is_better": False, "reason": "Other model failed"}

        if "Prefer model A" in input_text:
            primary_is_better = True
            reason = "Model A gave a great answer"
        else:
            primary_is_better = False
            reason = "Model B is preferred for this type of question"

        return {"primary_is_better": primary_is_better, "reason": reason}

dataset = Dataset(
    rows=[
        {"input_text": "Prefer model A: Question 1"},  # Model A が優勢
        {"input_text": "Prefer model A: Question 2"},  # Model A が優勢
        {"input_text": "Prefer model B: Question 3"},  # Model B が優勢
        {"input_text": "Prefer model B: Question 4"},  # Model B が優勢
    ]
)

model_a = ModelA()
model_b = ModelB()
pref_scorer = PreferenceScorer(other_model=model_b)
evaluation = Evaluation(dataset=dataset, scorers=[pref_scorer])
evaluation.evaluate(model_a)
この評価を実行すると、PreferenceScorer の結果に基づく ModelAModelB の相対的な選好順位が得られます。
評価