Repository navigation
FEAT: Add a Nemotron Safety Guard scorer - #4626
Siddhant Rajhans (siddhant-rajhans) wants to merge 16 commits into
Conversation
… models Scores a prompt and response pair with nvidia/Llama-3.1-Nemotron-Safety-Guard-8B-v3 or nvidia/llama-3.1-nemoguard-8b-content-safety, using the prompt NeMo Guardrails sends to both (Apache-2.0, credited in THIRD_PARTY_NOTICES.txt). The label selects the User Safety or Response Safety verdict; the other verdict and the category names go into the score metadata. The prompt is the fixed user_prompt, the latest earlier user turn, or the objective, so labelled responses can be evaluated. Signed-off-by: Siddhant Rajhans <51508945+siddhant-rajhans@users.noreply.github.com>
…guard-scorer # Conflicts: # doc/bibliography.md
Main's CyberSecEval prompt injection scorer (microsoft#4440) and WildJailbreak loader (microsoft#4535) touched the same files as this branch: - THIRD_PARTY_NOTICES.txt: both notices are kept, main's CyberSecEval notice first. - doc/bibliography.md: the citation keys are the sorted union of both sides. - tests/unit/score/test_target_judge.py: both scorers are in the judge list. - doc/code/scoring/0_scoring.ipynb: main's notebook, with the scorer table printed again from the merged code, so it adds the NemotronSafetyGuardScorer row at main's column widths.
The guard's own resolver was the same code as the shared _AssistantContentMessageResolver, so use that one.
Main's MultiJail loader (microsoft#4510) added a citation key to doc/bibliography.md next to this branch's CultureGuard key; the keys are the sorted union of both sides. Signed-off-by: Siddhant Rajhans <51508945+siddhant-rajhans@users.noreply.github.com>
…guard-scorer # Conflicts: # doc/bibliography.md
…guard-scorer # Conflicts: # doc/bibliography.md
Integrate main 4dead80 and preserve both branches' bibliography citations. Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
Richard Lundeen (richlundeen)
left a comment
There was a problem hiding this comment.
Automated review completed. No blocking concerns remain. This review records source coverage; merging is handled separately.
Richard Lundeen (richlundeen)
left a comment
There was a problem hiding this comment.
Thanks for the careful work here, especially the parity check against NeMo Guardrails.
Our earlier automated review moved this scorer onto MessageMultiLabelTrueFalseScorer (649f572, 2c5f865, 16a7758) and then approved it. That was our mistake, and I'm sorry for the churn. We plan to remove the multi-label family in a separate PR. Please make these changes:
- Port it to float scale. Subclass
MessageFloatScaleScorer, asAzureContentFilterScorerandInsecureCodeScorerdo. Unsafe is 1.0 and safe is 0.0. A harm classifier belongs in float scale even with only two values:ScorerEvaluator.from_scorer(pyrit/score/scorer_evaluation/scorer_evaluator.py:171-172) sends it to harm metrics. Attacks that need a true/false objective can wrap it inFloatScaleThresholdScorer. A prompt-only request can stay undetermined withscore.set_value(None). - Return one score for the scored message. A scorer should get a message, which is the user turn and the response, and score that message. Here that is the guard's judgment of the response with the user turn you already resolve. The native verdicts, including the user-prompt verdict, can stay in metadata. Drop the multi-label base, the edits to
multi_label_true_false_scorer.pyandscorer_info.py, andTrueFalseScoreSelectorin the docs and tests. Move the notebook paragraph todoc/code/scoring/2_float_scale_scorers. - Map categories where you can. Map each Aegis 2.0 category to
HarmCategorywhere a clear match exists, and put the mapped names inscore_category. Keep the original Aegis names and the native verdicts in metadata, asNemotronSafetyGuardVerdictsdoes now. Names without a clear match stay in metadata only. The aliases behindstandardize_harm_categoriesalready map 17 of the 23 names.
Harm evaluation over pyrit/datasets/scorer_evals/harm/*.csv must keep working once categories are mapped. ScorerEvaluator._select_evaluation_score (scorer_evaluator.py:691-719) accepts a single score only when its score_category is empty or matches the CSV's harm_category. Otherwise it raises ValueError, and the evaluation stops. A violence.csv response flagged only as Criminal Planning/Confessions (COORDINATION_HARM) would do that. So would any mapped category on exploits.csv or info_integrity.csv, whose labels parse to OTHER, and the request's nemotron_safety_guard category if it stays on the score. Please choose the handling, and add a test where a harm evaluation scores a response flagged under a different category.
…guard-scorer # Conflicts: # THIRD_PARTY_NOTICES.txt # doc/bibliography.md
A safety guard gives one verdict for the whole response and names the categories it flagged. Scorer.SCORE_CATEGORIES_DESCRIBE_VERDICT (False by default) marks such a scorer, and ScorerEvaluator._select_evaluation_score then takes its lone score for any labeled harm instead of rejecting a score that names another category. Every other scorer keeps the current check, and several scores still need exactly one category match.
As asked in review: - NemotronSafetyGuardScorer is a MessageFloatScaleScorer in pyrit/score/float_scale: 1.0 when the guard rates the response unsafe and 0.0 when it rates it safe. A response with no text leaves the score undetermined. FloatScaleThresholdScorer gives a true/false view. - It returns one score for the scored message: the guard's judgment of the response, with the text of all its pieces, against the resolved user turn. Both native verdicts and the Aegis 2.0 names stay in the metadata through NemotronSafetyGuardVerdicts. - An unsafe response's score_category holds the HarmCategory names of the violated categories that have a clear match: the 17 the shared aliases map, and Sexual (minor) as SEXUALIZATION. The other five stay in the metadata only, and a safe response names no category, since the guard's one list can describe only the prompt. The scorer sets SCORE_CATEGORIES_DESCRIBE_VERDICT, so a harm evaluation takes its verdict whatever category a dataset is labeled with. - Drop the multi-label base and the edits to multi_label_true_false_scorer.py and scorer_info.py (and their test), NemotronSafetyGuardLabel, and TrueFalseScoreSelector in the docs and tests. The notebook paragraph moves to 2_float_scale_scorers, and the 0_scoring catalog lists the scorer as float_scale.
|
Thanks Richard Lundeen (@richlundeen), and no problem about the churn. I reworked it on top of your commits, with main merged in (d79e465).
Cross-category handling. The guard gives one verdict per response and its categories describe that verdict, so harm evaluation now takes the verdict whatever category the CSV is labeled with. Tests: the scorer, parser and evaluator tests and the score and models suites pass (11,651 passed), as does the pinned pre-commit. The PR description is updated to match. |
Description
PyRIT has scorers for LlamaGuard, ShieldGemma and WildGuard and loads NVIDIA's Aegis 2.0 dataset, but it has no scorer for the guard models trained on that taxonomy (#4524). This adds
NemotronSafetyGuardScorerfornvidia/Llama-3.1-Nemotron-Safety-Guard-8B-v3(multilingual, open weights) andnvidia/llama-3.1-nemoguard-8b-content-safety, the models behind NeMo Guardrails' content safety rails. Closes #4524.MessageFloatScaleScorer: its one score is the response verdict, 1.0 unsafe and 0.0 safe, and an unsafe response'sscore_categoryholds the matchedHarmCategorynames (the 17 the shared aliases map, plus Sexual (minor) asSEXUALIZATION). Both native verdicts and the Aegis names go into the score metadata throughNemotronSafetyGuardVerdictsinpyrit.models, preserving the metadata ownership agreed in the FEAT: Add a StrongREJECT rubric scorer #4229 review. A response with no text is undetermined.FloatScaleThresholdScorergives a true/false objective scorer.Scorer.SCORE_CATEGORIES_DESCRIBE_VERDICT(False by default, True here) letsScorerEvaluator._select_evaluation_scoretake the guard's lone score whatever category a harm dataset is labeled with, since its categories only describe its one verdict. Every other scorer keeps the current check.THIRD_PARTY_NOTICES.txt), sent as one user message to a chat target. Judgments replay like ShieldGemma's.user_prompt, else the latest earlier user turn of the scored conversation (its converted value, as in WildGuard), else the objective. The objective fallback lets an evaluation score labelled responses that have no stored user turn.doc/references.bibadds CultureGuard, and the scorer table in0_scoring.ipynblists the new scorer as float scale.With the real model (revision 8fdc246b, served by vLLM at temperature 0) on the 395 human-labelled responses in
scorer_evals/objective, with the objective as the user turn, the rendered requests are identical to what NeMo Guardrails sends for all 395, and the verdicts match a direct run parsed with NeMo Guardrails' own parser on 392 of 393. Against the human labels, the 393 verdicts give accuracy 0.824, precision 0.917, recall 0.698 and F1 0.793 (unsafe read as objective achieved). HarmBench's classifier gets accuracy 0.880 and recall 0.825 on the same responses, so this model is a precise harm detector more than an objective judge.Two defaults, from the questions in #4524, which richlundeen confirmed there:
InvalidJsonException, asWildGuardScorerdoes for "N/A", so the retries repeat the same answer and an evaluation stops at that response. NeMo Guardrails cannot parse such an answer either and blocks the message, so reading a missing verdict as unsafe would match it.