Skip to content

FEAT: Add a HarmBench classifier scorer - #4237

Open
Siddhant Rajhans (siddhant-rajhans) wants to merge 5 commits into
microsoft:mainfrom
siddhant-rajhans:feat/harmbench-classifier-scorer
Open

Siddhant Rajhans (siddhant-rajhans) wants to merge 5 commits into
microsoft:mainfrom
siddhant-rajhans:feat/harmbench-classifier-scorer

Conversation

@siddhant-rajhans

@siddhant-rajhans Siddhant Rajhans (siddhant-rajhans) commented Nov 12, 2026 •

Copy link
Copy Markdown
Contributor

Description

PyRIT loads HarmBench's behaviors but has no scorer for HarmBench's attack-success classifier (Mazeika et al., 2024, arXiv:2402.04249), the fine-tuned cais/HarmBench-Llama-2-13b-cls that answers whether a response is an instance of a behavior. This adds HarmBenchScorer, which reproduces HarmBench's classification step.

  • HarmBenchScorer sends HarmBench's classifier prompt, standard or contextual, to a target serving the classifier and returns True when it answers "Yes", so it can serve as an attack's objective scorer. The objective is the behavior; a contextual behavior is read the way harmbench_dataset.py builds it. HarmBench's copyright behaviors are scored by hashing and are out of scope.
  • The bundled prompts are HarmBench's (MIT), including the Llama 2 [INST] wrapper the classifier was trained on, so the classifier is served from a completions endpoint (OpenAICompletionTarget), as with WildGuardScorer.
  • Like HarmBench, each response is clipped to 512 tokens of the classifier's tokenizer (pinned revision, loaded on first use; needs the huggingface extra), which also keeps long responses inside the classifier's 2,048-token context. clip_generations=False sends full responses.
  • The parser reads "yes" or "no" case-insensitively, as HarmBench does, and raises InvalidJsonException otherwise, so the judge retry applies. Judgments replay like LlamaGuard's.

With the real classifier on the 395 human-labelled responses in scorer_evals/objective, the scorer gives the same label as HarmBench's own pipeline (evaluate_completions.py clipping, then vllm.LLM.generate at temperature 0) for 394 of 395 responses; the one difference is between vLLM's offline and server modes on an identical request. Against the human labels, ObjectiveScorerEvaluator gives accuracy 0.878, precision 0.918, recall 0.822 and F1 0.867.

Copilot AI balanced review requested due to automatic review settings November 12, 2026 07:34

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.

Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota. Try requesting a review at a lower effort level.

HarmBench measures attack success with a fine-tuned classifier
(cais/HarmBench-Llama-2-13b-cls) that answers whether a generation is an
instance of a behavior. HarmBenchScorer sends HarmBench's own classifier
prompts (MIT), standard and contextual, to a target serving the
classifier and returns True when it answers yes. The objective is the
behavior; a contextual behavior is read the way PyRIT's HarmBench dataset
builds it. Like HarmBench, it clips each response to 512 tokens of the
classifier's pinned tokenizer before classification.

Signed-off-by: Siddhant Rajhans <51508945+siddhant-rajhans@users.noreply.github.com>
…ctives up front

- The parser no longer writes a harmbench_raw_output metadata key: the rationale already quotes the
  classifier's answer and nothing read the key, so there is no namespaced key left without an owning model.
- Reject a whitespace-only objective in HarmBenchScorer._validate_expectation, after the shared check,
  instead of in the piece hook, so it raises MissingConditionError before any classifier call.
@siddhant-rajhans

Copy link
Copy Markdown
Contributor Author

Updated ahead of review, to match what #4229's review asked for (c751703, after merging main):

  • The parser no longer writes the harmbench_raw_output metadata key. The rationale already quotes the classifier's answer and nothing read the key, so no namespaced key is left without an owning model.
  • A whitespace-only objective is now rejected in a _validate_expectation override that runs after the shared check, raising MissingConditionError before any classifier call. Previously the piece hook checked it, and the error surfaced as a RuntimeError.

ty 0.0.105, now pinned on main, renamed unresolved-attribute to missing-attribute
and reports an annotated assignment from an Any value as unsound. Use the new rule
name and narrow the tokenizer's decode() result with cast, as main does elsewhere.

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.

3 participants