Skip to content

Commit 55dfddb

Browse files
bashirpartoviBashir Partovi
andauthored
FEAT Restructuring Attacks (microsoft#1059)
Co-authored-by: Bashir Partovi <bpartovi@microsoft.com>
1 parent c6937e7 commit 55dfddb

126 files changed

Lines changed: 4638 additions & 8712 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.

doc/api.rst

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ API Reference
1717
:py:mod:`pyrit.attacks`
1818
============================
1919

20-
.. automodule:: pyrit.attacks
20+
.. automodule:: pyrit.executor.attack
2121
:no-members:
2222
:no-inherited-members:
2323

@@ -31,8 +31,6 @@ API Reference
3131
AttackExecutor
3232
AttackScoringConfig
3333
AttackStrategy
34-
AttackStrategyLogAdapter
35-
ContextT
3634
ContextComplianceAttack
3735
ConversationSession
3836
CrescendoAttack
@@ -47,9 +45,6 @@ API Reference
4745
TAPAttack
4846
TAPAttackContext
4947
TAPAttackResult
50-
FuzzerAttack
51-
FuzzerAttackContext
52-
FuzzerAttackResult
5348
TreeOfAttacksWithPruningAttack
5449
SkeletonKeyAttack
5550
ConsoleAttackResultPrinter

doc/code/attacks/1_prompt_sending_attack.ipynb

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -48,8 +48,8 @@
4848
}
4949
],
5050
"source": [
51-
"from pyrit.attacks import ConsoleAttackResultPrinter, PromptSendingAttack\n",
5251
"from pyrit.common import IN_MEMORY, initialize_pyrit\n",
52+
"from pyrit.executor.attack import ConsoleAttackResultPrinter, PromptSendingAttack\n",
5353
"from pyrit.prompt_target import OpenAIChatTarget\n",
5454
"\n",
5555
"initialize_pyrit(memory_db_type=IN_MEMORY)\n",
@@ -206,13 +206,13 @@
206206
"source": [
207207
"import pathlib\n",
208208
"\n",
209-
"from pyrit.attacks import (\n",
209+
"from pyrit.common.path import DATASETS_PATH\n",
210+
"from pyrit.executor.attack import (\n",
210211
" AttackConverterConfig,\n",
211212
" AttackExecutor,\n",
212213
" ConsoleAttackResultPrinter,\n",
213214
" PromptSendingAttack,\n",
214215
")\n",
215-
"from pyrit.common.path import DATASETS_PATH\n",
216216
"from pyrit.models import SeedPromptDataset\n",
217217
"from pyrit.prompt_converter import Base64Converter\n",
218218
"from pyrit.prompt_normalizer import PromptConverterConfiguration\n",
@@ -329,7 +329,7 @@
329329
"source": [
330330
"import pathlib\n",
331331
"\n",
332-
"from pyrit.attacks import (\n",
332+
"from pyrit.executor.attack import (\n",
333333
" ConsoleAttackResultPrinter,\n",
334334
" PromptSendingAttack,\n",
335335
" SingleTurnAttackContext,\n",
@@ -432,7 +432,7 @@
432432
"source": [
433433
"from azure.ai.contentsafety.models import TextCategory\n",
434434
"\n",
435-
"from pyrit.attacks import AttackScoringConfig, PromptSendingAttack\n",
435+
"from pyrit.executor.attack import AttackScoringConfig, PromptSendingAttack\n",
436436
"from pyrit.prompt_target import OpenAIChatTarget\n",
437437
"from pyrit.score import (\n",
438438
" AzureContentFilterScorer,\n",
@@ -623,8 +623,8 @@
623623
}
624624
],
625625
"source": [
626-
"from pyrit.attacks import AttackExecutor, PromptSendingAttack\n",
627626
"from pyrit.datasets import TextJailBreak\n",
627+
"from pyrit.executor.attack import AttackExecutor, PromptSendingAttack\n",
628628
"from pyrit.models.prompt_request_response import PromptRequestResponse\n",
629629
"from pyrit.prompt_target import OpenAIChatTarget\n",
630630
"\n",
@@ -725,8 +725,8 @@
725725
"source": [
726726
"import uuid\n",
727727
"\n",
728-
"from pyrit.attacks import PromptSendingAttack\n",
729728
"from pyrit.common import IN_MEMORY, initialize_pyrit\n",
729+
"from pyrit.executor.attack import PromptSendingAttack\n",
730730
"from pyrit.models import SeedPrompt, SeedPromptGroup\n",
731731
"from pyrit.prompt_target import OpenAIChatTarget\n",
732732
"\n",

doc/code/attacks/1_prompt_sending_attack.py

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,10 @@
3131
# > It is required to manually set the memory instance using `initialize_pyrit`. For details, see the [Memory Configuration Guide](../memory/0_memory.md).
3232
#
3333

34-
# %%
35-
from pyrit.attacks import ConsoleAttackResultPrinter, PromptSendingAttack
3634
from pyrit.common import IN_MEMORY, initialize_pyrit
35+
36+
# %%
37+
from pyrit.executor.attack import ConsoleAttackResultPrinter, PromptSendingAttack
3738
from pyrit.prompt_target import OpenAIChatTarget
3839

3940
initialize_pyrit(memory_db_type=IN_MEMORY)
@@ -57,13 +58,13 @@
5758
# %%
5859
import pathlib
5960

60-
from pyrit.attacks import (
61+
from pyrit.common.path import DATASETS_PATH
62+
from pyrit.executor.attack import (
6163
AttackConverterConfig,
6264
AttackExecutor,
6365
ConsoleAttackResultPrinter,
6466
PromptSendingAttack,
6567
)
66-
from pyrit.common.path import DATASETS_PATH
6768
from pyrit.models import SeedPromptDataset
6869
from pyrit.prompt_converter import Base64Converter
6970
from pyrit.prompt_normalizer import PromptConverterConfiguration
@@ -99,7 +100,7 @@
99100
# %%
100101
import pathlib
101102

102-
from pyrit.attacks import (
103+
from pyrit.executor.attack import (
103104
ConsoleAttackResultPrinter,
104105
PromptSendingAttack,
105106
SingleTurnAttackContext,
@@ -135,7 +136,7 @@
135136
# %%
136137
from azure.ai.contentsafety.models import TextCategory
137138

138-
from pyrit.attacks import AttackScoringConfig, PromptSendingAttack
139+
from pyrit.executor.attack import AttackScoringConfig, PromptSendingAttack
139140
from pyrit.prompt_target import OpenAIChatTarget
140141
from pyrit.score import (
141142
AzureContentFilterScorer,
@@ -169,13 +170,14 @@
169170
result = await attack.execute_async(objective=objective) # type: ignore
170171
await printer.print_conversation_async(result=result, include_auxiliary_scores=True) # type: ignore
171172

173+
from pyrit.datasets import TextJailBreak
174+
172175
# %% [markdown]
173176
# ## Prepending Conversations
174177
#
175178
# If you prepend all or part of a conversation with `PromptSendingAttack`, that is also supported. You can call `set_prepended_conversation` to customize the beginning part of any message. For example, you could use this to do a multi-turn conversation. Below sets the system prompt for many messages.
176179
# %%
177-
from pyrit.attacks import AttackExecutor, PromptSendingAttack
178-
from pyrit.datasets import TextJailBreak
180+
from pyrit.executor.attack import AttackExecutor, PromptSendingAttack
179181
from pyrit.models.prompt_request_response import PromptRequestResponse
180182
from pyrit.prompt_target import OpenAIChatTarget
181183

@@ -207,8 +209,8 @@
207209
# %%
208210
import uuid
209211

210-
from pyrit.attacks import PromptSendingAttack
211212
from pyrit.common import IN_MEMORY, initialize_pyrit
213+
from pyrit.executor.attack import PromptSendingAttack
212214
from pyrit.models import SeedPrompt, SeedPromptGroup
213215
from pyrit.prompt_target import OpenAIChatTarget
214216

doc/code/auxiliary_attacks/0_auxiliary_attacks.ipynb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -65,12 +65,12 @@
6565
}
6666
],
6767
"source": [
68-
"from pyrit.attacks import (\n",
68+
"from pyrit.common import IN_MEMORY, initialize_pyrit\n",
69+
"from pyrit.executor.attack import (\n",
6970
" AttackScoringConfig,\n",
7071
" ConsoleAttackResultPrinter,\n",
7172
" PromptSendingAttack,\n",
7273
")\n",
73-
"from pyrit.common import IN_MEMORY, initialize_pyrit\n",
7474
"from pyrit.prompt_target import AzureMLChatTarget, OpenAIChatTarget\n",
7575
"from pyrit.score import SelfAskRefusalScorer, TrueFalseInverterScorer\n",
7676
"\n",
@@ -188,7 +188,7 @@
188188
}
189189
],
190190
"source": [
191-
"from pyrit.attacks import AttackConverterConfig\n",
191+
"from pyrit.executor.attack import AttackConverterConfig\n",
192192
"from pyrit.prompt_converter import SuffixAppendConverter\n",
193193
"from pyrit.prompt_normalizer import PromptConverterConfiguration\n",
194194
"\n",

doc/code/auxiliary_attacks/0_auxiliary_attacks.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
# extension: .py
77
# format_name: percent
88
# format_version: '1.3'
9-
# jupytext_version: 1.17.0
9+
# jupytext_version: 1.17.2
1010
# kernelspec:
1111
# display_name: pyrit-dev
1212
# language: python
@@ -29,13 +29,14 @@
2929
#
3030
# First, we send a harmful prompt to Phi-3-mini without a GCG suffix. If the environment variables `PHI3_MINI_ENDPOINT` and `PHI3_MINI_KEY` are not set in your .env file, the target will default to the model with `AZURE_ML_MANAGED_ENDPOINT` and `AZURE_ML_MANAGED_KEY`.
3131

32+
from pyrit.common import IN_MEMORY, initialize_pyrit
33+
3234
# %%
33-
from pyrit.attacks import (
35+
from pyrit.executor.attack import (
3436
AttackScoringConfig,
3537
ConsoleAttackResultPrinter,
3638
PromptSendingAttack,
3739
)
38-
from pyrit.common import IN_MEMORY, initialize_pyrit
3940
from pyrit.prompt_target import AzureMLChatTarget, OpenAIChatTarget
4041
from pyrit.score import SelfAskRefusalScorer, TrueFalseInverterScorer
4142

@@ -58,7 +59,7 @@
5859
# Next, let's apply a GCG suffix trained on Phi-3-mini to the base prompt using the `SuffixAppendConverter`.
5960

6061
# %%
61-
from pyrit.attacks import AttackConverterConfig
62+
from pyrit.executor.attack import AttackConverterConfig
6263
from pyrit.prompt_converter import SuffixAppendConverter
6364
from pyrit.prompt_normalizer import PromptConverterConfiguration
6465

doc/code/converters/2_using_converters.ipynb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -58,12 +58,12 @@
5858
}
5959
],
6060
"source": [
61-
"from pyrit.attacks import (\n",
61+
"from pyrit.common import IN_MEMORY, initialize_pyrit\n",
62+
"from pyrit.executor.attack import (\n",
6263
" AttackConverterConfig,\n",
6364
" ConsoleAttackResultPrinter,\n",
6465
" PromptSendingAttack,\n",
6566
")\n",
66-
"from pyrit.common import IN_MEMORY, initialize_pyrit\n",
6767
"from pyrit.prompt_converter import StringJoinConverter, VariationConverter\n",
6868
"from pyrit.prompt_normalizer import PromptConverterConfiguration\n",
6969
"from pyrit.prompt_target import OpenAIChatTarget, TextTarget\n",
@@ -91,7 +91,7 @@
9191
"result = await attack.execute_async(objective=objective) # type: ignore\n",
9292
"\n",
9393
"printer = ConsoleAttackResultPrinter()\n",
94-
"await printer.print_conversation_async(result=result) # type: ignore"
94+
"await printer.print_conversation_async(result=result) # type: ignore"
9595
]
9696
}
9797
],

doc/code/converters/2_using_converters.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
# extension: .py
66
# format_name: percent
77
# format_version: '1.3'
8-
# jupytext_version: 1.17.0
8+
# jupytext_version: 1.17.2
99
# kernelspec:
1010
# display_name: pyrit-dev
1111
# language: python
@@ -26,13 +26,14 @@
2626
# In this example the converters are stacked. First a variation is found using `VariationConverter`, and then `StringJoinConverter` is used to add a dash between letters. Remember that order matters. If `StringJoinConverter` came first, we would be asking the LLM to make variations of the prompt:
2727
# "t-e-l-l- - m-e- -h-o-w- -t-o- -c-u-t- -d-o-w-n - a- -t-r-e-e"
2828

29+
from pyrit.common import IN_MEMORY, initialize_pyrit
30+
2931
# %%
30-
from pyrit.attacks import (
32+
from pyrit.executor.attack import (
3133
AttackConverterConfig,
3234
ConsoleAttackResultPrinter,
3335
PromptSendingAttack,
3436
)
35-
from pyrit.common import IN_MEMORY, initialize_pyrit
3637
from pyrit.prompt_converter import StringJoinConverter, VariationConverter
3738
from pyrit.prompt_normalizer import PromptConverterConfiguration
3839
from pyrit.prompt_target import OpenAIChatTarget, TextTarget

doc/code/converters/5_selectively_converting.ipynb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,12 +51,12 @@
5151
}
5252
],
5353
"source": [
54-
"from pyrit.attacks import (\n",
54+
"from pyrit.common import IN_MEMORY, initialize_pyrit\n",
55+
"from pyrit.executor.attack import (\n",
5556
" AttackConverterConfig,\n",
5657
" ConsoleAttackResultPrinter,\n",
5758
" PromptSendingAttack,\n",
5859
")\n",
59-
"from pyrit.common import IN_MEMORY, initialize_pyrit\n",
6060
"from pyrit.prompt_converter import Base64Converter\n",
6161
"from pyrit.prompt_normalizer import PromptConverterConfiguration\n",
6262
"from pyrit.prompt_target import TextTarget\n",

doc/code/converters/5_selectively_converting.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
# extension: .py
66
# format_name: percent
77
# format_version: '1.3'
8-
# jupytext_version: 1.17.0
8+
# jupytext_version: 1.17.2
99
# kernelspec:
1010
# display_name: pyrit-dev
1111
# language: python
@@ -17,13 +17,14 @@
1717
#
1818
# You can selectively convert strings from text converters using most orchestrators or the `convert_tokens_async` function. This function uses a `start_token` and `end_token` to determine where to do the converting (by default these are the unicode characters ⟪ and ⟫). Here is an example that uses `PromptSendingAttack` to convert pieces of the text to base64.
1919

20+
from pyrit.common import IN_MEMORY, initialize_pyrit
21+
2022
# %%
21-
from pyrit.attacks import (
23+
from pyrit.executor.attack import (
2224
AttackConverterConfig,
2325
ConsoleAttackResultPrinter,
2426
PromptSendingAttack,
2527
)
26-
from pyrit.common import IN_MEMORY, initialize_pyrit
2728
from pyrit.prompt_converter import Base64Converter
2829
from pyrit.prompt_normalizer import PromptConverterConfiguration
2930
from pyrit.prompt_target import TextTarget

doc/code/converters/ansi_attack_converter.ipynb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -257,13 +257,13 @@
257257
}
258258
],
259259
"source": [
260-
"from pyrit.attacks import (\n",
260+
"from pyrit.common import IN_MEMORY, initialize_pyrit\n",
261+
"from pyrit.executor.attack import (\n",
261262
" AttackConverterConfig,\n",
262263
" AttackExecutor,\n",
263264
" ConsoleAttackResultPrinter,\n",
264265
" PromptSendingAttack,\n",
265266
")\n",
266-
"from pyrit.common import IN_MEMORY, initialize_pyrit\n",
267267
"from pyrit.prompt_converter import AnsiAttackConverter\n",
268268
"from pyrit.prompt_normalizer import PromptConverterConfiguration\n",
269269
"from pyrit.prompt_target import OpenAIChatTarget\n",

0 commit comments

Comments
 (0)