Skip to content

FEAT: Suggest a FloatScaleThresholdScorer threshold from human-labeled data - #4645

Merged
Richard Lundeen (richlundeen) merged 14 commits into
microsoft:mainfrom
Garyouki:feat/suggest-threshold
Dec 5, 2026
Merged

Richard Lundeen (richlundeen) merged 14 commits into
microsoft:mainfrom
Garyouki:feat/suggest-threshold

Conversation

@Garyouki

@Garyouki Garyouki commented Nov 25, 2026 •

Copy link
Copy Markdown
Contributor

Description

Closes #4569.

Right now FloatScaleThresholdScorer thresholds are picked by hand. This adds a small opt-in helper that suggests one from human-labeled data. It only suggests a value. Nothing changes unless you create a new scorer with it.

  • suggest_threshold(scores=, labels=): splits the data in half, tries each nonzero observed score as the threshold on one half, picks the best F1 (or the best recall above min_precision), then checks that value on the other half.
  • suggest_threshold_async(scorer=, labeled_dataset=): does the scoring for you and keeps the float behind each true/false verdict. The result keeps those scores and labels, so you can try min_precision or another split with suggest_threshold without scoring again. ScorerMetrics and the registry are untouched.
  • Scoring the same HumanLabeledDataset object again no longer fails: both evaluators now skip dataset messages already in memory. suggest_threshold_async shares the evaluator's gold-label rule, so entries need the same number of human labels in both, and it rejects a scorer that is not a FloatScaleThresholdScorer before any scoring call.
  • A docs page, doc/code/scoring/float_scale_threshold_metrics.py, linked from the scorer metrics, combining, and float-scale scorer pages. It places the helper as objective metrics for float-scale scorers, shows the effect on made-up scores, and then tunes the registered SelfAskScaleScorer (threshold 0.9) on bias.csv with a live model.

In the made-up example, moving from 0.9 to the suggested 0.33 takes held-out recall from 1% to 90%, at the cost of 17 false positives.

🤖 Generated with Claude Code

…d data

Add an opt-in helper that sweeps thresholds on human-labeled float scores,
picks one on a tuning split (max F1, or max recall at a precision floor),
and checks the frozen choice on a stratified holdout split.
suggest_threshold_async scores an OBJECTIVE dataset with a
FloatScaleThresholdScorer and keeps the float value behind each verdict.

Closes microsoft#4569.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Copilot AI balanced review requested due to automatic review settings November 25, 2026 20:16
@Garyouki

Garyouki commented Nov 25, 2026 •

Copy link
Copy Markdown
Contributor Author

Quick map for whoever reviews this:

  1. suggest_threshold is the actual logic. The rest is mostly plumbing.
  2. suggest_threshold_async reuses three private ObjectiveScorerEvaluator helpers so scoring matches evaluation exactly. If you'd rather have it as a public method on the evaluator, I'm happy to move it.
  3. Tests and the docs page. About 360 of the added lines are the executed .ipynb.
flowchart LR
    A[float scores + human labels] --> B{split by response}
    B -->|tuning half| C[try each observed score<br/>pick best F1]
    B -->|holdout half| D[check the picked value]
    C --> D --> E[suggestion + current threshold<br/>for comparison]
Loading

A few choices you might ask about:

  • Only observed scores are tried. A threshold between two of them gives the same result as the one above it.
  • With several trials, each one counts separately, since an attack scores once. All trials of a response go in the same half.
  • Ties go to the higher threshold, which means fewer false positives.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Changes recommended

The implementation can silently skip requested holdout validation and select a lower threshold on tied F1 scores due to floating-point rounding.

3 open issues
What changed in this PR

Adds opt-in threshold selection to PyRIT’s scorer-evaluation tools, addressing #4569 without changing existing scorer defaults or persisted metrics.

Changes:

  • Suggests thresholds using F1 or a precision floor, with held-out evaluation and current-threshold comparisons.
  • Supports human-labeled datasets and repeated scoring trials while retaining underlying float feedback.
  • Adds public exports, unit tests, and an offline documentation example.
File Description
tests/​unit/​score/​test_threshold_suggestion.py Tests metrics, selection, validation, splits, and async scoring.
pyrit/​score/​scorer_evaluation/​threshold_suggestion.py Implements threshold evaluation, selection, and result dataclasses.
pyrit/​score/​__init__.py Exposes the new helpers and dataclasses through lazy exports.
doc/​myst.yml Adds the threshold guide to documentation navigation.
doc/​code/​scoring/​9_choosing_a_threshold.py Demonstrates offline tuning and real-scorer usage.
doc/​code/​scoring/​9_choosing_a_threshold.ipynb Provides the paired notebook with example outputs.

Review effort: Balanced

- Reject an empty holdout split whenever holdout_fraction > 0.
- Compute F1 from integer counts so exact ties break toward the higher threshold.
- Use SelfAskScaleScorer.from_scale in the docs example.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@Garyouki

Copy link
Copy Markdown
Contributor Author

Sorry for the super long PR, mostly b/c of the ipynb.

Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
@richlundeen

Copy link
Copy Markdown
Contributor

review:6064382711

Checked all three previous review findings at 3f17114278652ec0fd4ba146bc753a47c7ae6601; the author's holdout, F1-tie and scorer-factory fixes are preserved. The repair also shares the evaluator trial loop and metric arithmetic, removes the extra evaluation-only public helper, validates tuning options before scoring, and corrects the real-scorer example's initialization and dataset. The scoped scorer/export suites and offline documentation checks pass. No live model calls were made; CI is a separate observation.

Verified on published commit 3f17114278652ec0fd4ba146bc753a47c7ae6601.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Automated review completed. No blocking concerns remain. This review records source coverage; merging is handled separately.

Move the page to a non-numbered name, explain where it fits next to scorer metrics, execute the real-scorer example against bias.csv, add reading guidance, and link it from the scorer metrics page.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
ThresholdSuggestion now keeps the scores and labels it tuned on, so callers can re-tune without scoring again, and leaves the long fields out of repr. The docs show the exact suggested threshold and warn against rounding it, list the judge's operating points, re-split the kept scores, and are linked from the combining and float-scale scorer pages. The regenerated notebook is stripped like the other docs notebooks.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The judge operating-points table now shows each threshold exactly, so a copied value matches the scorer's verdicts. ThresholdSuggestion copies the scores and labels it keeps, so later changes to the caller's arrays do not alter it.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
…uator

- Skip dataset messages already in memory, so the same HumanLabeledDataset can be scored again.
- Share the human-score matrix and majority vote with the evaluator, so ragged rater counts fail the same way before scoring.
- Check the scorer keeps a float on each verdict before any scoring call.
- Reject tuning data where every gold-true score is 0.0.
- Test that tuning ignores the held-out split.

Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
The capability check accepted a TrueFalseFallbackScorer of two threshold
scorers, which then failed on the missing threshold attribute.

Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
@richlundeen
Richard Lundeen (richlundeen) added this pull request to the merge queue Dec 5, 2026
Merged via the queue into microsoft:main with commit 98f2279 Dec 5, 2026
72 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

FEAT: Help choose a FloatScaleThresholdScorer threshold from human-labeled data

3 participants