campusdesk-ai / src /evaluation /metrics.py
Estarcode's picture
Upload folder using huggingface_hub
2ab2b76 verified
Raw History Blame Contribute Delete
3.68 kB
"""Exact rubric metric recomputation over per-case end-to-end evaluation records."""
from typing import Dict, List
MARKING_RULE = """
Marking Rules (Human-Verified Per-Case Judgements):
1. evidence_recall_at_3 (denominator = 20 answerable/partly answerable prompts):
|required_passage_ids ∩ top3_passage_ids| / |required_passage_ids|, averaged across the 20 prompts.
2. supported_answer (denominator = 20 answerable/partly answerable prompts):
1 iff ALL material claims for the supported portion are factually correct, complete, and backed by valid cited passage IDs.
3. handling_unknowns (denominator = 8 partly or fully unanswerable prompts):
1 iff the reply explicitly identifies the missing information without guessing AND (for partial cases) answers the supported portion accurately.
4. cross_language_consistency (denominator = 12 bilingual pairs):
1 iff BOTH English and Nigerian Pidgin replies preserve all expected facts, dates, amounts, and unknown-handling (two matching wrong answers earn 0).
""".strip()
def compute_aggregates(records: List[Dict]) -> Dict:
answerable_or_partial = [
r for r in records if r["category"] in {"direct", "two_source", "partly_answerable"}
]
unknown_cases = [
r for r in records if r["category"] in {"partly_answerable", "unanswerable"}
]
assert len(answerable_or_partial) == 20, (
f"Expected 20 answerable/partial prompts, found {len(answerable_or_partial)}"
)
assert len(unknown_cases) == 8, (
f"Expected 8 partial/unanswerable prompts, found {len(unknown_cases)}"
)
recalls = []
for r in answerable_or_partial:
req = set(r["required_passage_ids"])
got = set(r["top3_passage_ids"])
recalls.append(len(req & got) / len(req))
supported_count = sum(1 for r in answerable_or_partial if r["human_judgement"]["supported_answer"])
unknown_count = sum(1 for r in unknown_cases if r["human_judgement"]["handling_unknowns_ok"])
by_pair: Dict[str, List[Dict]] = {}
for r in records:
by_pair.setdefault(r["pair_id"], []).append(r)
assert len(by_pair) == 12, f"Expected 12 pairs, found {len(by_pair)}"
consistent_pairs = 0
pair_breakdown = {}
for pid, items in sorted(by_pair.items()):
assert len(items) == 2, f"Pair {pid} missing a language variant"
both_ok = all(item["human_judgement"]["facts_and_dates_accurate"] for item in items)
if both_ok:
consistent_pairs += 1
pair_breakdown[pid] = {
"category": items[0]["category"],
"pair_consistent_and_accurate": both_ok,
}
return {
"marking_rule": MARKING_RULE,
"metrics": {
"evidence_recall_at_3": {
"score": round(sum(recalls) / len(recalls), 4),
"denominator": len(answerable_or_partial),
"per_prompt_recalls": [round(x, 4) for x in recalls],
},
"supported_answers": {
"count": supported_count,
"denominator": len(answerable_or_partial),
"rate": round(supported_count / len(answerable_or_partial), 4),
},
"handling_unknowns": {
"count": unknown_count,
"denominator": len(unknown_cases),
"rate": round(unknown_count / len(unknown_cases), 4),
},
"consistency_across_languages": {
"count": consistent_pairs,
"denominator": len(by_pair),
"rate": round(consistent_pairs / len(by_pair), 4),
},
},
"pair_breakdown": pair_breakdown,
}