-
Notifications
You must be signed in to change notification settings - Fork 746
DOC: Add scenario doc pages for all 7 undocumented scenarios #1558
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
varunj-msft
merged 5 commits into
microsoft:main
from
varunj-msft:varunj-msft/7933-Migrate-cookbooks-to-scenarios
Apr 3, 2026
Merged
Changes from 1 commit
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
69d3ca8
PR 1: Scenario Doc Pages
varunj-msft 9c63319
Updates to scenario notebooks and scripts
varunj-msft 7fabec3
Updates
varunj-msft f4a58f5
Final Updates
varunj-msft 158b95b
Merge branch 'main' into varunj-msft/7933-Migrate-cookbooks-to-scenarios
varunj-msft File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,104 @@ | ||
| # --- | ||
| # jupyter: | ||
| # jupytext: | ||
| # text_representation: | ||
| # extension: .py | ||
| # format_name: percent | ||
| # format_version: '1.3' | ||
| # jupytext_version: 1.19.1 | ||
| # kernelspec: | ||
| # display_name: pyrit-dev | ||
| # language: python | ||
| # name: pyrit-dev | ||
| # --- | ||
|
|
||
| # %% [markdown] | ||
| # # 2. Content Harms Scenario | ||
| # | ||
| # The `ContentHarms` scenario tests whether a target model can be induced to generate harmful content across | ||
| # seven harm categories: hate, fairness, violence, sexual, harassment, misinformation, and leakage. It combines | ||
| # single-turn attacks (PromptSending, RolePlay) with multi-turn techniques (ManyShot, TAP) to provide broad | ||
| # coverage of content safety risks. | ||
| # | ||
| # ## Available Strategies | ||
| # | ||
| # Each strategy targets a specific harm category with its own dataset: | ||
| # | ||
| # | Strategy | CLI Value | Description | | ||
| # |----------|-----------|-------------| | ||
| # | ALL | `all` | Aggregate — runs all 7 harm categories | | ||
| # | Hate | `hate` | Tests for hateful content generation | | ||
| # | Fairness | `fairness` | Tests for unfair or biased content | | ||
| # | Violence | `violence` | Tests for violent content generation | | ||
| # | Sexual | `sexual` | Tests for sexual content generation | | ||
| # | Harassment | `harassment` | Tests for harassing content generation | | ||
| # | Misinformation | `misinformation` | Tests for misinformation generation | | ||
| # | Leakage | `leakage` | Tests for data leakage in content | | ||
| # | ||
| # ## Setup | ||
|
|
||
| # %% | ||
| from pyrit.prompt_target import OpenAIChatTarget | ||
| from pyrit.scenario import DatasetConfiguration | ||
| from pyrit.scenario.printer.console_printer import ConsoleScenarioResultPrinter | ||
| from pyrit.scenario.scenarios.airt import ContentHarms, ContentHarmsStrategy | ||
| from pyrit.setup import IN_MEMORY, initialize_pyrit_async | ||
| from pyrit.setup.initializers import LoadDefaultDatasets | ||
|
|
||
| await initialize_pyrit_async(memory_db_type=IN_MEMORY, initializers=[LoadDefaultDatasets()]) # type: ignore | ||
|
|
||
| objective_target = OpenAIChatTarget() | ||
| printer = ConsoleScenarioResultPrinter() | ||
|
|
||
| # %% [markdown] | ||
| # ## Running via CLI | ||
| # | ||
| # The simplest way to run this scenario is with `pyrit_scan`. To test a single harm category quickly: | ||
| # | ||
| # ```bash | ||
| # pyrit_scan airt.content_harms \ | ||
| # --initializers target load_default_datasets \ | ||
| # --target openai_chat \ | ||
| # --strategies hate \ | ||
| # --max-dataset-size 1 | ||
| # ``` | ||
| # | ||
| # To run all harm categories: | ||
| # | ||
| # ```bash | ||
| # pyrit_scan airt.content_harms \ | ||
| # --initializers target load_default_datasets \ | ||
| # --target openai_chat \ | ||
| # --max-dataset-size 2 | ||
| # ``` | ||
| # | ||
| # ## Programmatic Usage | ||
| # | ||
| # For more control, you can configure and run the scenario programmatically. Here we run only the `hate` | ||
| # strategy with a minimal dataset. Note that each strategy runs **four** attack types (PromptSending, | ||
| # RolePlay, ManyShot, TAP) plus a baseline, so even a single strategy produces multiple atomic attacks. | ||
|
|
||
| # %% | ||
| dataset_config = DatasetConfiguration(dataset_names=["airt_hate"], max_dataset_size=1) | ||
|
|
||
| scenario = ContentHarms() | ||
| await scenario.initialize_async( # type: ignore | ||
| objective_target=objective_target, | ||
| scenario_strategies=[ContentHarmsStrategy.Hate], | ||
| dataset_config=dataset_config, | ||
| ) | ||
|
|
||
| print(f"Scenario: {scenario.name}") | ||
| print(f"Atomic attacks: {scenario.atomic_attack_count}") | ||
|
|
||
| # %% | ||
| scenario_result = await scenario.run_async() # type: ignore | ||
|
|
||
| # %% [markdown] | ||
| # ## Interpreting Results | ||
| # | ||
| # The `ScenarioResult` contains aggregated outcomes from all atomic attacks. Use the printer to see a | ||
| # summary of success rates and strategy effectiveness. | ||
|
|
||
| # %% | ||
| await printer.print_summary_async(scenario_result) # type: ignore | ||
|
varunj-msft marked this conversation as resolved.
|
||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.