Skip to content

Commit fd27727

Browse files
authored
FIX: initializer doc fix (#1241)
1 parent 1d5ad84 commit fd27727

5 files changed

Lines changed: 39 additions & 20 deletions

File tree

doc/code/scoring/prompt_shield_scorer.ipynb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,7 @@
148148
"\n",
149149
"for score in scores:\n",
150150
" prompt_text = memory.get_message_pieces(prompt_ids=[str(score.message_piece_id)])[0].original_value\n",
151-
" print(f\"{score} : {prompt_text}\") # We can see that the attack was detected\n"
151+
" print(f\"{score} : {prompt_text}\") # We can see that the attack was detected"
152152
]
153153
}
154154
],

doc/code/setup/pyrit_initializer.ipynb

Lines changed: 24 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,18 @@
3434
"execution_count": null,
3535
"id": "2",
3636
"metadata": {},
37-
"outputs": [],
37+
"outputs": [
38+
{
39+
"data": {
40+
"text/plain": [
41+
"<__main__.CustomInitializer at 0x230d9e527b0>"
42+
]
43+
},
44+
"execution_count": null,
45+
"metadata": {},
46+
"output_type": "execute_result"
47+
}
48+
],
3849
"source": [
3950
"from pyrit.common.apply_defaults import set_default_value\n",
4051
"from pyrit.prompt_target import OpenAIChatTarget\n",
@@ -50,12 +61,15 @@
5061
" def execution_order(self) -> int:\n",
5162
" return 2 # Lower numbers run first (default is 1)\n",
5263
" \n",
53-
" def initialize(self) -> None:\n",
64+
" async def initialize_async(self) -> None:\n",
5465
" set_default_value(class_type=OpenAIChatTarget, parameter_name=\"temperature\", value=0.9)\n",
5566
"\n",
5667
" @property\n",
5768
" def description(self) -> str:\n",
58-
" return \"Sets custom temperature for OpenAI targets\""
69+
" return \"Sets custom temperature for OpenAI targets\"\n",
70+
" \n",
71+
"\n",
72+
"CustomInitializer()"
5973
]
6074
},
6175
{
@@ -80,11 +94,11 @@
8094
"metadata": {},
8195
"outputs": [],
8296
"source": [
83-
"from pyrit.setup import initialize_pyrit\n",
97+
"from pyrit.setup import initialize_pyrit_async\n",
8498
"from pyrit.setup.initializers import SimpleInitializer\n",
8599
"\n",
86100
"# Using built-in initializer\n",
87-
"initialize_pyrit(\n",
101+
"await initialize_pyrit_async( # type: ignore\n",
88102
" memory_db_type=\"InMemory\",\n",
89103
" initializers=[SimpleInitializer()]\n",
90104
")"
@@ -117,7 +131,7 @@
117131
"name": "stdout",
118132
"output_type": "stream",
119133
"text": [
120-
"Created: C:\\Users\\rlundeen\\AppData\\Local\\Temp\\tmpgs282kvw\\custom_init.py\n"
134+
"Created: C:\\Users\\rlundeen\\AppData\\Local\\Temp\\tmpa2k36reo\\custom_init.py\n"
121135
]
122136
}
123137
],
@@ -126,14 +140,14 @@
126140
"import shutil\n",
127141
"import tempfile\n",
128142
"\n",
129-
"from pyrit.setup import initialize_pyrit\n",
143+
"from pyrit.setup import initialize_pyrit_async\n",
130144
"\n",
131145
"temp_dir = tempfile.mkdtemp()\n",
132146
"script_path = os.path.join(temp_dir, \"custom_init.py\")\n",
133147
"\n",
134148
"# This is the simple custom initializer from the \"Creating an Initializer\" section of this notebook\n",
135149
"script_content = '''\n",
136-
"from pyrit.setup.initializers.base import PyRITInitializer\n",
150+
"from pyrit.setup.initializers.pyrit_initializer import PyRITInitializer\n",
137151
"from pyrit.common.apply_defaults import set_default_value\n",
138152
"from pyrit.prompt_target import OpenAIChatTarget\n",
139153
"\n",
@@ -146,7 +160,7 @@
146160
" def execution_order(self) -> int:\n",
147161
" return 2 # Lower numbers run first (default is 1)\n",
148162
" \n",
149-
" def initialize(self) -> None:\n",
163+
" async def initialize_async(self) -> None:\n",
150164
" set_default_value(class_type=OpenAIChatTarget, parameter_name=\"temperature\", value=0.9)\n",
151165
"\n",
152166
" @property\n",
@@ -161,7 +175,7 @@
161175
"print(f\"Created: {script_path}\")\n",
162176
"\n",
163177
"\n",
164-
"initialize_pyrit(\n",
178+
"await initialize_pyrit_async( # type: ignore\n",
165179
" memory_db_type=\"InMemory\",\n",
166180
" initialization_scripts=[temp_dir + \"/custom_init.py\"]\n",
167181
")\n",

doc/code/setup/pyrit_initializer.py

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
#
1818
# ## Execution Order
1919
#
20-
# When `initialize_pyrit_async` is called:
20+
# When `initialize_pyrit` is called:
2121
# 1. Environment files are loaded (`.env`, `.env.local`)
2222
# 2. Memory database is configured
2323
# 3. All initializers are sorted by `execution_order` and executed
@@ -27,10 +27,11 @@
2727
# %% [markdown]
2828
# The following is a minimal `PyRITInitializer` class. It doesn't need much! In this case, it sets the default value for temperature for all OpenAIChatTargets to .9.
2929

30-
# %%
3130
from pyrit.common.apply_defaults import set_default_value
3231
from pyrit.prompt_target import OpenAIChatTarget
33-
from pyrit.setup.initializers import PyRITInitializer
32+
33+
# %%
34+
from pyrit.setup.initializers.pyrit_initializer import PyRITInitializer
3435

3536

3637
class CustomInitializer(PyRITInitializer):
@@ -42,14 +43,16 @@ def name(self) -> str:
4243
def execution_order(self) -> int:
4344
return 2 # Lower numbers run first (default is 1)
4445

45-
def initialize(self) -> None:
46+
async def initialize_async(self) -> None:
4647
set_default_value(class_type=OpenAIChatTarget, parameter_name="temperature", value=0.9)
4748

4849
@property
4950
def description(self) -> str:
5051
return "Sets custom temperature for OpenAI targets"
5152

5253

54+
CustomInitializer()
55+
5356
# %% [markdown]
5457
# ## Built-in Initializers
5558
#
@@ -91,7 +94,7 @@ def description(self) -> str:
9194

9295
# This is the simple custom initializer from the "Creating an Initializer" section of this notebook
9396
script_content = """
94-
from pyrit.setup.initializers import PyRITInitializer
97+
from pyrit.setup.initializers.pyrit_initializer import PyRITInitializer
9598
from pyrit.common.apply_defaults import set_default_value
9699
from pyrit.prompt_target import OpenAIChatTarget
97100
@@ -104,7 +107,7 @@ def name(self) -> str:
104107
def execution_order(self) -> int:
105108
return 2 # Lower numbers run first (default is 1)
106109
107-
def initialize(self) -> None:
110+
async def initialize_async(self) -> None:
108111
set_default_value(class_type=OpenAIChatTarget, parameter_name="temperature", value=0.9)
109112
110113
@property
@@ -119,7 +122,9 @@ def description(self) -> str:
119122
print(f"Created: {script_path}")
120123

121124

122-
await initialize_pyrit_async(memory_db_type="InMemory", initialization_scripts=[temp_dir + "/custom_init.py"]) # type: ignore
125+
await initialize_pyrit_async( # type: ignore
126+
memory_db_type="InMemory", initialization_scripts=[temp_dir + "/custom_init.py"]
127+
)
123128

124129

125130
if os.path.exists(temp_dir):

doc/code/targets/1_openai_chat_target.ipynb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@
8181
"attack = PromptSendingAttack(objective_target=target)\n",
8282
"\n",
8383
"result = await attack.execute_async(objective=jailbreak_prompt) # type: ignore\n",
84-
"await ConsoleAttackResultPrinter().print_conversation_async(result=result) # type: ignore\n"
84+
"await ConsoleAttackResultPrinter().print_conversation_async(result=result) # type: ignore"
8585
]
8686
},
8787
{

doc/code/targets/5_multi_modal_targets.ipynb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,7 @@
150150
")\n",
151151
"\n",
152152
"result = await attack.execute_async(objective=objective) # type: ignore\n",
153-
"await ConsoleAttackResultPrinter().print_result_async(result=result) # type: ignore\n"
153+
"await ConsoleAttackResultPrinter().print_result_async(result=result) # type: ignore"
154154
]
155155
},
156156
{

0 commit comments

Comments
 (0)