|
| 1 | +"""Correctness judges and three-axis aggregation for answer-mode eval.""" |
| 2 | + |
| 3 | +from __future__ import annotations |
| 4 | + |
| 5 | +import re |
| 6 | +from dataclasses import dataclass |
| 7 | +from typing import TYPE_CHECKING, Protocol, runtime_checkable |
| 8 | + |
| 9 | +if TYPE_CHECKING: |
| 10 | + from collections.abc import Sequence |
| 11 | + |
| 12 | + from mteb.agentic.interface import AnswerResult, ChatModel |
| 13 | + |
| 14 | + |
| 15 | +@runtime_checkable |
| 16 | +class Judge(Protocol): |
| 17 | + """Scores answer correctness in the range 0 to 1.""" |
| 18 | + |
| 19 | + def score(self, question: str, predicted: str, reference: str) -> float: |
| 20 | + """Grade a predicted answer against a reference answer.""" |
| 21 | + ... |
| 22 | + |
| 23 | + |
| 24 | +def _normalize(text: str) -> str: |
| 25 | + # Lowercase, drop articles and punctuation, collapse whitespace. |
| 26 | + text = text.lower() |
| 27 | + text = re.sub(r"\b(a|an|the)\b", " ", text) |
| 28 | + text = re.sub(r"[^a-z0-9 ]", " ", text) |
| 29 | + return " ".join(text.split()) |
| 30 | + |
| 31 | + |
| 32 | +class ExactMatchJudge: |
| 33 | + """Normalized exact match.""" |
| 34 | + |
| 35 | + def score(self, question: str, predicted: str, reference: str) -> float: # noqa: PLR6301 |
| 36 | + """Return 1.0 on a normalized exact match, else 0.0.""" |
| 37 | + return 1.0 if _normalize(predicted) == _normalize(reference) else 0.0 |
| 38 | + |
| 39 | + |
| 40 | +_JUDGE_PROMPT = ( |
| 41 | + "You grade a predicted answer against a reference answer. " |
| 42 | + "Reply with only YES if the prediction is correct, otherwise NO.\n\n" |
| 43 | + "Question: {question}\nReference: {reference}\nPrediction: {predicted}" |
| 44 | +) |
| 45 | + |
| 46 | + |
| 47 | +class LLMJudge: |
| 48 | + """Grades open ended answers with a ChatModel.""" |
| 49 | + |
| 50 | + def __init__(self, model: ChatModel) -> None: |
| 51 | + self.model = model |
| 52 | + |
| 53 | + def score(self, question: str, predicted: str, reference: str) -> float: |
| 54 | + """Return 1.0 if the judge model answers YES, else 0.0.""" |
| 55 | + prompt = _JUDGE_PROMPT.format( |
| 56 | + question=question, reference=reference, predicted=predicted |
| 57 | + ) |
| 58 | + out = self.model.generate([{"role": "user", "content": prompt}]) |
| 59 | + return 1.0 if out.text.strip().lower().startswith("yes") else 0.0 |
| 60 | + |
| 61 | + |
| 62 | +@dataclass |
| 63 | +class AggregateScores: |
| 64 | + """Three-axis summary over a question set: quality, cost, latency.""" |
| 65 | + |
| 66 | + accuracy: float |
| 67 | + mean_cost_usd: float | None |
| 68 | + total_cost_usd: float | None |
| 69 | + mean_latency_s: float | None |
| 70 | + mean_llm_calls: float |
| 71 | + n: int |
| 72 | + |
| 73 | + |
| 74 | +def aggregate( |
| 75 | + results: Sequence[AnswerResult], correctness: Sequence[float] |
| 76 | +) -> AggregateScores: |
| 77 | + """Reduce per-question results into the three reported axes.""" |
| 78 | + n = len(results) |
| 79 | + if n == 0: |
| 80 | + return AggregateScores(0.0, None, None, None, 0.0, 0) |
| 81 | + costs = [r.usage.cost_usd for r in results if r.usage.cost_usd is not None] |
| 82 | + latencies = [r.usage.latency_s for r in results if r.usage.latency_s is not None] |
| 83 | + total_cost = sum(costs) if costs else None |
| 84 | + mean_cost = sum(costs) / len(costs) if costs else None |
| 85 | + return AggregateScores( |
| 86 | + accuracy=sum(correctness) / n, |
| 87 | + mean_cost_usd=mean_cost, |
| 88 | + total_cost_usd=total_cost, |
| 89 | + mean_latency_s=(sum(latencies) / len(latencies)) if latencies else None, |
| 90 | + mean_llm_calls=sum(r.usage.num_llm_calls for r in results) / n, |
| 91 | + n=n, |
| 92 | + ) |
0 commit comments