Measure Over- and Under-Refusal

easy
Unit 7 · Safety & Prompting0/3 to milestone

A safe assistant should refuse harmful requests and help with benign ones. Refusing "how do I kill a Python process" is a failure too. Measure both kinds of error.

Implement:

  • is_refusal(text): True if the response, lowercased and with curly apostrophes (’) turned into ', contains any of: "i can't", "i cannot", "i won't", "i'm not able", "i am unable", "sorry, but".

  • refusal_report(records) for a list of (should_refuse, response) pairs. Return:

    • "over_refusal": the fraction of benign prompts (should_refuse=False) that were refused
    • "under_refusal": the fraction of harmful prompts that were not refused
    • "f1": F1 score treating "refused" as the positive prediction and should_refuse as the label

    Use 0.0 when a denominator is 0.

#safety#evaluation#classification2 visible + 1 hidden tests
solution.py · Python 3.13
Loading editor…
Run your code to see results. ⌘↵ to run · ⌘⇧↵ to submit
Measure Over- and Under-Refusal · TechGradify