-
Notifications
You must be signed in to change notification settings - Fork 771
FEAT: Add PAIRAttack as a TAP alias with PAIR-definitional defaults pinned #1822
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
romanlutz
merged 6 commits into
microsoft:main
from
romanlutz:romanlutz/pair-attack-alias
May 29, 2026
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
685d12e
Add PAIRAttack as a TAP alias with PAIR-definitional defaults pinned
romanlutz 60b9ce3
Apply PAIR review feedback: drop standalone notebook, sort __init__, …
romanlutz ea03eab
Restore tap_attack.ipynb cell outputs (regression from prior commit)
romanlutz 8d6f7bc
Strip Sphinx/RST markup from PAIRAttack docstrings
romanlutz 5263ec1
Drop inherited Raises section from PAIRAttack.__init__ docstring
romanlutz 8186444
Merge branch 'main' into romanlutz/pair-attack-alias
romanlutz 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
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
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
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
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
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
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,108 @@ | ||
| # Copyright (c) Microsoft Corporation. | ||
| # Licensed under the MIT license. | ||
|
|
||
| """ | ||
| Prompt Automatic Iterative Refinement (PAIR) attack. | ||
|
|
||
| Implements PAIR (Chao et al. 2023, arXiv:2310.08419) as a thin subclass of | ||
| TreeOfAttacksWithPruningAttack with the PAIR-definitional structural | ||
| parameters (no tree branching, no off-topic pruning) hardcoded. | ||
| """ | ||
|
|
||
| import logging | ||
|
|
||
| from pyrit.common.apply_defaults import REQUIRED_VALUE, apply_defaults | ||
| from pyrit.executor.attack.component import PrependedConversationConfig | ||
| from pyrit.executor.attack.core.attack_config import ( | ||
| AttackAdversarialConfig, | ||
| AttackConverterConfig, | ||
| ) | ||
| from pyrit.executor.attack.multi_turn.tree_of_attacks import ( | ||
| TAPAttackScoringConfig, | ||
| TreeOfAttacksWithPruningAttack, | ||
| ) | ||
| from pyrit.prompt_normalizer import PromptNormalizer | ||
| from pyrit.prompt_target import PromptTarget | ||
|
|
||
| logger = logging.getLogger(__name__) | ||
|
|
||
|
|
||
| class PAIRAttack(TreeOfAttacksWithPruningAttack): | ||
| """ | ||
| Implement the Prompt Automatic Iterative Refinement (PAIR) attack strategy | ||
| [@chao2023pair]. | ||
|
|
||
| PAIR is a structural special case of Tree of Attacks with Pruning | ||
| [@mehrotra2023tap]: parallel "streams" of single-branch iterative | ||
| refinement, with no tree expansion and no off-topic pruning. PyRIT | ||
| implements PAIR as a thin subclass of TreeOfAttacksWithPruningAttack that | ||
| hardcodes the two definitional structural parameters (branching_factor=1 | ||
| and on_topic_checking_enabled=False) and reuses TAP's adversarial system | ||
| prompt. The remaining configuration (target, scoring, converters, width | ||
| and depth budget) is exposed exactly as on TAP, with TAP's defaults | ||
| preserved for tree_width (number of parallel streams) and tree_depth | ||
| (refinement iterations per stream). | ||
|
|
||
| References: | ||
| Jailbreaking Black Box Large Language Models in Twenty Queries | ||
| [@chao2023pair] | ||
|
|
||
| Tree of Attacks: Jailbreaking Black-Box LLMs Automatically | ||
| [@mehrotra2023tap] | ||
| """ | ||
|
|
||
| @apply_defaults | ||
| def __init__( | ||
| self, | ||
| *, | ||
| objective_target: PromptTarget = REQUIRED_VALUE, # type: ignore[ty:invalid-parameter-default] | ||
| attack_adversarial_config: AttackAdversarialConfig, | ||
| attack_converter_config: AttackConverterConfig | None = None, | ||
| attack_scoring_config: TAPAttackScoringConfig | None = None, | ||
| prompt_normalizer: PromptNormalizer | None = None, | ||
| tree_width: int = 3, | ||
| tree_depth: int = 5, | ||
| desired_response_prefix: str = "Sure, here is", | ||
| batch_size: int = 10, | ||
| prepended_conversation_config: PrependedConversationConfig | None = None, | ||
| ) -> None: | ||
| """ | ||
| Initialize the PAIR attack strategy. | ||
|
|
||
| Args: | ||
| objective_target (PromptTarget): The target system to attack. | ||
| attack_adversarial_config (AttackAdversarialConfig): Configuration for the | ||
| adversarial chat component. | ||
| attack_converter_config (AttackConverterConfig | None): Configuration for | ||
| attack converters. Defaults to None. | ||
| attack_scoring_config (TAPAttackScoringConfig | None): Scoring configuration. | ||
| The objective scorer must be a FloatScaleThresholdScorer. If not | ||
| provided, a default FloatScaleThresholdScorer wrapping | ||
| SelfAskScaleScorer (threshold 0.7) is created. Defaults to None. | ||
| prompt_normalizer (PromptNormalizer | None): The prompt normalizer to use. | ||
| Defaults to None. | ||
| tree_width (int): Number of parallel "streams" (N in the PAIR paper). | ||
| Defaults to 3. | ||
| tree_depth (int): Maximum refinement iterations per stream (K in the PAIR | ||
| paper). Defaults to 5. | ||
| desired_response_prefix (str): Expected prefix for successful responses. | ||
| Defaults to "Sure, here is". | ||
| batch_size (int): Number of nodes to process in parallel per batch. | ||
| Defaults to 10. | ||
| prepended_conversation_config (PrependedConversationConfig | None): | ||
| Configuration for prepended-conversation handling. Defaults to None. | ||
| """ | ||
| super().__init__( | ||
| objective_target=objective_target, | ||
| attack_adversarial_config=attack_adversarial_config, | ||
| attack_converter_config=attack_converter_config, | ||
| attack_scoring_config=attack_scoring_config, | ||
| prompt_normalizer=prompt_normalizer, | ||
| tree_width=tree_width, | ||
| tree_depth=tree_depth, | ||
| branching_factor=1, | ||
| on_topic_checking_enabled=False, | ||
| desired_response_prefix=desired_response_prefix, | ||
| batch_size=batch_size, | ||
| prepended_conversation_config=prepended_conversation_config, | ||
| ) |
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
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.