UncertaintyGym / lighteval_task.py
Ill-Ness's picture
Upload Benchmark
31147f4 verified
Raw
History Blame Contribute Delete
2.11 kB
"""
LightEval Task Definition for UncertaintyGym (Hugging Face Evaluation Harness)
==============================================================================
Defines custom LightEval tasks and metrics for evaluating UncertaintyGym on Hugging Face.
"""
import re
from typing import Dict, Any
REFUSAL_PATTERNS = [
r"\bcannot answer\b", r"\bcannot be answered\b", r"\bi cannot\b", r"\bi am unable to\b",
r"\bunderspecified\b", r"\bambiguous\b", r"\bplease specify\b", r"\bwhich specific\b",
r"\bfalse premise\b", r"\bincorrect premise\b", r"\breject the premise\b", r"\bnot alive\b",
r"\bdid not exist\b", r"\bunknowable\b", r"\bunknown\b", r"\bunsolved\b", r"\bi do not know\b",
r"\bi don't know\b", r"\bunrecorded\b", r"\bfuture event\b"
]
def uncertainty_gym_doc_to_text(doc: Dict[str, Any]) -> str:
"""Formats sample into prompt for LightEval."""
return f"Question: {doc['Prompt']}\nAnswer:"
def uncertainty_gym_doc_to_target(doc: Dict[str, Any]) -> str:
"""Extracts expected target output."""
return doc["ExpectedOutput"]
def compute_calibration_metric(predictions, formatted_doc):
"""Custom metric computing category-specific calibration score."""
pred = predictions[0].strip().lower() if isinstance(predictions, list) else str(predictions).strip().lower()
category = formatted_doc["Category"]
has_refusal = any(re.search(pat, pred) for pat in REFUSAL_PATTERNS)
if category == "Category A":
# Solvable: correct if answered without false refusal
return 1.0 if (not has_refusal and len(pred) > 3) else 0.0
else:
# Categories B, C, D: correct if uncertainty/refusal signal is present
return 1.0 if has_refusal else 0.0
# Task registration dictionary for LightEval
TASKS_TABLE = [
{
"name": "uncertainty_gym",
"dataset_path": "uncertainty-gym",
"dataset_name": "default",
"doc_to_text": uncertainty_gym_doc_to_text,
"doc_to_target": uncertainty_gym_doc_to_target,
"metric": compute_calibration_metric,
"hf_repo": "uncertainty-gym",
}
]