Skip to content

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

Open
Garyouki wants to merge 3 commits into
microsoft:mainfrom
Garyouki:feat/suggest-threshold
Open

Garyouki wants to merge 3 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 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. ScorerMetrics and the registry are untouched.
  • A short docs page that runs offline: doc/code/scoring/9_choosing_a_threshold.py.

In the docs 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: Claude Opus 5.5 <noreply@anthropic.com>

This branch has not been deployed

No deployments
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

2 participants