pyrit.scenarios.AtomicAttack#
- class AtomicAttack(*, atomic_attack_name: str, attack: AttackStrategy, objectives: List[str], prepended_conversations: List[List[Message]] | None = None, seed_groups: List[SeedGroup] | None = None, custom_prompts: List[str] | None = None, memory_labels: Dict[str, str] | None = None, **attack_execute_params: Any)[source]#
Bases:
objectRepresents a single atomic attack test combining an attack strategy and dataset.
An AtomicAttack is an executable unit that executes a configured attack against all objectives in a dataset. Multiple AtomicAttacks can be grouped together into larger test scenarios for comprehensive security testing and evaluation.
The AtomicAttack automatically detects whether the attack is single-turn or multi-turn and calls the appropriate executor method. For single-turn attacks, you can provide seed_groups. For multi-turn attacks, you can provide custom_prompts.
Example
>>> from pyrit.scenarios import AtomicAttack >>> from pyrit.attacks import PromptAttack >>> from pyrit.prompt_target import OpenAIChatTarget >>> >>> target = OpenAIChatTarget() >>> attack = PromptAttack(objective_target=target) >>> objectives = ["how to make a bomb", "how to hack a system"] >>> >>> atomic_attack = AtomicAttack( ... attack=attack, ... objectives=objectives, ... memory_labels={"test": "run1"} ... ) >>> results = await atomic_attack.run_async(max_concurrency=5) >>> >>> # With prepended conversations >>> from pyrit.models import Message >>> conversation = [Message(...)] >>> atomic_attack = AtomicAttack( ... attack=attack, ... objectives=objectives, ... prepended_conversations=[conversation] ... ) >>> results = await atomic_attack.run_async(max_concurrency=5) >>> >>> # Single-turn attack with seeds >>> from pyrit.models import SeedGroup >>> seeds = [SeedGroup(...), SeedGroup(...)] >>> atomic_attack = AtomicAttack( ... attack=single_turn_attack, ... objectives=objectives, ... seed_groups=seeds ... ) >>> results = await atomic_attack.run_async(max_concurrency=3) >>> >>> # Multi-turn attack with custom prompts >>> custom_prompts = ["Tell me about chemistry", "Explain system administration"] >>> atomic_attack = AtomicAttack( ... attack=multi_turn_attack, ... objectives=objectives, ... custom_prompts=custom_prompts ... ) >>> results = await atomic_attack.run_async(max_concurrency=3)
- __init__(*, atomic_attack_name: str, attack: AttackStrategy, objectives: List[str], prepended_conversations: List[List[Message]] | None = None, seed_groups: List[SeedGroup] | None = None, custom_prompts: List[str] | None = None, memory_labels: Dict[str, str] | None = None, **attack_execute_params: Any) None[source]#
Initialize an atomic attack with an attack strategy and dataset parameters.
- Parameters:
atomic_attack_name (str) – Used to group an AtomicAttack with related attacks for a strategy.
attack (AttackStrategy) – The configured attack strategy to execute.
objectives (List[str]) – List of attack objectives to test against.
prepended_conversations (Optional[List[List[Message]]]) – Optional list of conversation histories to prepend to each attack execution. This will be used for all objectives.
seed_groups (Optional[List[SeedGroup]]) – List of seed groups for single-turn attacks. Only valid for single-turn attacks.
custom_prompts (Optional[List[str]]) – List of custom prompts for multi-turn attacks. Only valid for multi-turn attacks.
memory_labels (Optional[Dict[str, str]]) – Additional labels to apply to prompts. These labels help track and categorize the atomic attack in memory.
**attack_execute_params (Any) – Additional parameters to pass to the attack execution method (e.g., batch_size).
- Raises:
ValueError – If objectives list is empty.
TypeError – If seed_groups is provided for multi-turn attacks or custom_prompts is provided for single-turn attacks.
Methods
__init__(*, atomic_attack_name, attack, ...)Initialize an atomic attack with an attack strategy and dataset parameters.
run_async(*[, max_concurrency])Execute the atomic attack against all objectives in the dataset.
- async run_async(*, max_concurrency: int = 1) AtomicAttackResult[source]#
Execute the atomic attack against all objectives in the dataset.
This method uses AttackExecutor to run the configured attack against all objectives from the dataset. It automatically detects whether to use single-turn or multi-turn execution based on the attack’s context type.
- Parameters:
max_concurrency (int) – Maximum number of concurrent attack executions. Defaults to 1 for sequential execution.
- Returns:
Result containing list of attack results, one for each objective.
- Return type:
- Raises:
ValueError – If the attack execution fails.