Skip to content

Commit 204017a

Browse files
authored
FEAT Breaking: Updating CLI to use scenarios (microsoft#1154)
1 parent 4e90933 commit 204017a

72 files changed

Lines changed: 2988 additions & 2323 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

.env_example

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,10 @@ OPEN_ROUTER_CLAUDE_MODEL="anthropic/claude-3.7-sonnet"
4646
OLLAMA_CHAT_ENDPOINT="http://127.0.0.1:11434/v1/chat/completions"
4747
OLLAMA_MODEL="llama2"
4848

49+
DEFAULT_OPENAI_FRONTEND_ENDPOINT = ${AZURE_OPENAI_GPT4O_AAD_ENDPOINT}
50+
DEFAULT_OPENAI_FRONTEND_KEY = ${AZURE_OPENAI_GPT4O_AAD_KEY}
51+
DEFAULT_OPENAI_FRONTEND_MODEL = "gpt-4o"
52+
4953
OPENAI_CHAT_ENDPOINT=${PLATFORM_OPENAI_CHAT_ENDPOINT}
5054
OPENAI_CHAT_KEY=${PLATFORM_OPENAI_CHAT_API_KEY}
5155
OPENAI_CHAT_MODEL=${PLATFORM_OPENAI_CHAT_GPT4O_MODEL}

doc/_toc.yml

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -126,9 +126,7 @@ chapters:
126126
sections:
127127
- file: code/auxiliary_attacks/1_gcg_azure_ml
128128
- file: code/scenarios/scenarios
129-
- file: code/scanner/0_scanner
130-
sections:
131-
- file: code/scanner/1_configuration
129+
- file: code/front_end/0_cli
132130
- file: deployment/README
133131
sections:
134132
- file: deployment/deploy_hf_model_aml

doc/api.rst

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -543,10 +543,10 @@ API Reference
543543
AtomicAttack
544544
AtomicAttackResult
545545
EncodingScenario
546-
FoundryAttackStrategy
546+
FoundryStrategy
547547
FoundryScenario
548548
Scenario
549-
ScenarioAttackStrategy
549+
ScenarioStrategy
550550
ScenarioIdentifier
551551
ScenarioResult
552552

doc/code/front_end/0_cli.ipynb

Lines changed: 507 additions & 0 deletions
Large diffs are not rendered by default.

doc/code/front_end/0_cli.py

Lines changed: 148 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,148 @@
1+
# ---
2+
# jupyter:
3+
# jupytext:
4+
# text_representation:
5+
# extension: .py
6+
# format_name: percent
7+
# format_version: '1.3'
8+
# jupytext_version: 1.17.3
9+
# ---
10+
11+
# %% [markdown]
12+
# # The PyRIT CLI
13+
#
14+
# The PyRIT cli tool that allows you to run automated security testing and red teaming attacks against AI systems using [scenarios](../scenarios/scenarios.ipynb) for strategies and [configuration](../setup/0_configuration.ipynb).
15+
#
16+
# Note in this doc the ! prefaces all commands in the terminal so we can run in a Jupyter Notebook.
17+
#
18+
# ## Quick Start
19+
#
20+
# For help:
21+
22+
# %%
23+
# !pyrit_scan --help
24+
25+
# %% [markdown]
26+
# ### Discovery
27+
#
28+
# List all available scenarios:
29+
30+
# %%
31+
# !pyrit_scan --list-scenarios
32+
33+
# %% [markdown]
34+
# **Tip**: You can also discover user-defined scenarios by providing initialization scripts:
35+
#
36+
# ```shell
37+
# pyrit_scan --list-scenarios --initialization-scripts ./my_custom_initializer.py
38+
# ```
39+
#
40+
# This will load your custom scenario definitions and include them in the list.
41+
#
42+
# ## Initializers
43+
#
44+
# PyRITInitializers are how you can configure the CLI scanner. PyRIT includes several built-in initializers you can use with the `--initializers` flag.
45+
#
46+
# The `--list-initializers` command shows all available initializers. Initializers are referenced by their filename (e.g., `objective_target`, `objective_list`, `simple`) regardless of which subdirectory they're in.
47+
#
48+
# List the available initializers using the --list-initializers flag.
49+
50+
# %%
51+
# !pyrit_scan --list-initializers
52+
53+
# %% [markdown]
54+
# ### Running Scenarios
55+
#
56+
# You need a single scenario to run, you need two things:
57+
#
58+
# 1. A Scenario. Many are defined in `pyrit.scenarios.scenarios`. But you can also define your own in initialization_scripts.
59+
# 2. Initializers (which can be supplied via `--initializers` or `--initialization-scripts`). Scenarios often don't need many arguments, but they can be configured in different ways. And at the very least, most need an `objective_target` (the thing you're running a scan against).
60+
#
61+
# Basic usage will look something like:
62+
#
63+
# ```shell
64+
# pyrit_scan <scenario> --initializers <initializer1> <initializer2>
65+
# ```
66+
#
67+
# Or concretely:
68+
#
69+
# ```shell
70+
# !pyrit_scan foundry_scenario --initializers simple openai_objective_target
71+
# ```
72+
#
73+
# Example with a basic configuration that runs the Foundry scenario against the objective target defined in `openai_objective_target` (which just is an OpenAIChatTarget with `DEFAULT_OPENAI_FRONTEND_ENDPOINT` and `DEFAULT_OPENAI_FRONTEND_KEY`).
74+
75+
# %%
76+
# !pyrit_scan foundry_scenario --initializers openai_objective_target
77+
78+
# %% [markdown]
79+
# Or with all options and multiple initializers:
80+
#
81+
# ```shell
82+
# pyrit_scan foundry_scenario --database InMemory --initializers simple objective_target objective_list
83+
# ```
84+
#
85+
# You can also use custom initialization scripts by passing file paths. It is relative to your current working directory, but to avoid confusion, full paths are always better:
86+
#
87+
# ```shell
88+
# pyrit_scan encoding_scenario --initialization-scripts ./my_custom_config.py
89+
# ```
90+
#
91+
# #### Using Custom Scenarios
92+
#
93+
# You can define your own scenarios in initialization scripts. The CLI will automatically discover any `Scenario` subclasses and make them available:
94+
#
95+
# ```python
96+
# # my_custom_scenarios.py
97+
# from pyrit.scenarios import Scenario
98+
# from pyrit.common.apply_defaults import apply_defaults
99+
#
100+
# @apply_defaults
101+
# class MyCustomScenario(Scenario):
102+
# """My custom scenario that does XYZ."""
103+
#
104+
# def __init__(self, objective_target=None):
105+
# super().__init__(name="My Custom Scenario", version="1.0")
106+
# self.objective_target = objective_target
107+
# # ... your initialization code
108+
#
109+
# async def initialize_async(self):
110+
# # Load your atomic attacks
111+
# pass
112+
#
113+
# # ... implement other required methods
114+
# ```
115+
#
116+
# Then discover and run it:
117+
#
118+
# ```shell
119+
# # List to see it's available
120+
# pyrit_scan --list-scenarios --initialization-scripts ./my_custom_scenarios.py
121+
#
122+
# # Run it
123+
# pyrit_scan my_custom_scenario --initialization-scripts ./my_custom_scenarios.py
124+
# ```
125+
#
126+
# The scenario name is automatically converted from the class name (e.g., `MyCustomScenario` becomes `my_custom_scenario`).
127+
#
128+
#
129+
# ## When to Use the Scanner
130+
#
131+
# The scanner is ideal for:
132+
#
133+
# - **Automated testing pipelines**: CI/CD integration for continuous security testing
134+
# - **Batch testing**: Running multiple attack scenarios against various targets
135+
# - **Repeatable tests**: Standardized testing with consistent configurations
136+
# - **Team collaboration**: Shareable configuration files for consistent testing approaches
137+
# - **Quick testing**: Fast execution without writing Python code
138+
#
139+
#
140+
# ## Complete Documentation
141+
#
142+
# For comprehensive documentation about initialization files and setting defaults see:
143+
#
144+
# - **Configuration**: See [configuration](../setup/0_configuration.ipynb)
145+
# - **Setting Default Values**: See [default values](../setup/default_values.md)
146+
# - **Writing Initializers**: See [Initializers](../setup/pyrit_initializer.ipynb)
147+
#
148+
# Or visit the [PyRIT documentation website](https://azure.github.io/PyRIT/)

doc/code/scanner/0_scanner.md

Lines changed: 0 additions & 97 deletions
This file was deleted.

0 commit comments

Comments
 (0)