|
| 1 | +# (C) 2026 GoodData Corporation |
| 2 | +"""Evaluator for dashboard_summary: rubric-based LLM-as-judge scoring. |
| 3 | +
|
| 4 | +Summaries are free text, so we do not match strings. Instead, `expected_output` |
| 5 | +is a rubric of checkable criteria: |
| 6 | +
|
| 7 | + { |
| 8 | + "must_include": ["...facts a good summary must contain..."], |
| 9 | + "must_not_include": ["...things a good summary must avoid (hallucinations)..."], |
| 10 | + "rubric": ["...soft quality dimensions..."] |
| 11 | + } |
| 12 | +
|
| 13 | +Each criterion is scored independently by the judge (True/False), so the |
| 14 | +runner's `quality_score` becomes the fraction of satisfied criteria. The item |
| 15 | +*passes* only when every `must_include` is satisfied and no `must_not_include` |
| 16 | +is violated; `rubric` items contribute to quality but do not gate pass/fail. |
| 17 | +
|
| 18 | +As a fallback, a non-dict `expected_output` is treated as a single rubric |
| 19 | +criterion (same behaviour as `general_question`). |
| 20 | +""" |
| 21 | + |
| 22 | +from typing import Any |
| 23 | + |
| 24 | +from gooddata_eval.core.evaluators._llm_judge import LLMJudge |
| 25 | +from gooddata_eval.core.evaluators._text_utils import extract_text |
| 26 | +from gooddata_eval.core.evaluators.base import ItemEvaluation |
| 27 | +from gooddata_eval.core.models import ChatResult, DatasetItem |
| 28 | + |
| 29 | +_POSITIVE_STEPS = [ |
| 30 | + "Read the INPUT (the user's request) and the EXPECTED OUTPUT (one criterion the summary must satisfy).", |
| 31 | + "Read the ACTUAL OUTPUT (the generated summary).", |
| 32 | + "Score 1 if the actual output clearly satisfies the criterion (allow paraphrasing and reasonable numeric tolerance).", |
| 33 | + "Score 0 if the criterion is missing, contradicted, or only partially addressed.", |
| 34 | +] |
| 35 | + |
| 36 | +# For must_not_include we ask the judge a plain presence question and invert the |
| 37 | +# result in code. Scoring "does the summary AVOID X?" via a field labelled |
| 38 | +# EXPECTED OUTPUT is unreliable: the model reads the forbidden behaviour as |
| 39 | +# desired and flips the verdict. Detecting presence (no negation, no |
| 40 | +# contradictory label) is far more robust. |
| 41 | +_VIOLATION_STEPS = [ |
| 42 | + "Read the CHARACTERISTIC described in EXPECTED OUTPUT.", |
| 43 | + "Read the ACTUAL OUTPUT (the generated summary).", |
| 44 | + "Score 1 if the actual output clearly exhibits the described characteristic.", |
| 45 | + "Score 0 if it does not exhibit it.", |
| 46 | +] |
| 47 | + |
| 48 | + |
| 49 | +class DashboardSummaryEvaluator: |
| 50 | + test_kind = "dashboard_summary" |
| 51 | + |
| 52 | + def __init__(self): |
| 53 | + self._positive_judge = LLMJudge(evaluation_steps=_POSITIVE_STEPS) |
| 54 | + self._violation_judge = LLMJudge(evaluation_steps=_VIOLATION_STEPS) |
| 55 | + |
| 56 | + @staticmethod |
| 57 | + def _criteria(expected_output: Any) -> tuple[list[str], list[str], list[str]]: |
| 58 | + if isinstance(expected_output, dict): |
| 59 | + must_include = [str(c) for c in expected_output.get("must_include", [])] |
| 60 | + must_not_include = [str(c) for c in expected_output.get("must_not_include", [])] |
| 61 | + rubric = [str(c) for c in expected_output.get("rubric", [])] |
| 62 | + if must_include or must_not_include or rubric: |
| 63 | + return must_include, must_not_include, rubric |
| 64 | + # Fallback: treat the whole expected_output as a single gating criterion |
| 65 | + # (same pass/fail semantics as general_question). |
| 66 | + return [str(expected_output)], [], [] |
| 67 | + |
| 68 | + def evaluate(self, item: DatasetItem, chat_result: ChatResult) -> ItemEvaluation: |
| 69 | + actual = extract_text(chat_result) |
| 70 | + must_include, must_not_include, rubric = self._criteria(item.expected_output) |
| 71 | + |
| 72 | + detail: dict[str, Any] = {"actual_output": actual} |
| 73 | + passed = True |
| 74 | + |
| 75 | + for i, criterion in enumerate(must_include): |
| 76 | + ok, reason = self._positive_judge.score(item.question, criterion, actual) |
| 77 | + detail[f"include_{i}"] = ok |
| 78 | + detail[f"include_{i}_reason"] = reason |
| 79 | + passed = passed and ok |
| 80 | + |
| 81 | + for i, criterion in enumerate(must_not_include): |
| 82 | + violated, reason = self._violation_judge.score(item.question, criterion, actual) |
| 83 | + ok = not violated # True == characteristic absent == correctly avoided |
| 84 | + detail[f"exclude_{i}"] = ok |
| 85 | + detail[f"exclude_{i}_reason"] = reason |
| 86 | + passed = passed and ok |
| 87 | + |
| 88 | + for i, criterion in enumerate(rubric): |
| 89 | + ok, reason = self._positive_judge.score(item.question, criterion, actual) |
| 90 | + detail[f"rubric_{i}"] = ok |
| 91 | + detail[f"rubric_{i}_reason"] = reason |
| 92 | + |
| 93 | + bool_checks = [v for v in detail.values() if isinstance(v, bool)] |
| 94 | + quality = sum(1 for v in bool_checks if v) / len(bool_checks) if bool_checks else 0.0 |
| 95 | + |
| 96 | + return ItemEvaluation(passed=passed, rank_key=(int(passed), quality), detail=detail) |
0 commit comments