Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion doc/code/scoring/prompt_shield_scorer.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@
"\n",
"for score in scores:\n",
" prompt_text = memory.get_message_pieces(prompt_ids=[str(score.message_piece_id)])[0].original_value\n",
" print(f\"{score} : {prompt_text}\") # We can see that the attack was detected\n"
" print(f\"{score} : {prompt_text}\") # We can see that the attack was detected"
]
}
],
Expand Down
34 changes: 24 additions & 10 deletions doc/code/setup/pyrit_initializer.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,18 @@
"execution_count": null,
"id": "2",
"metadata": {},
"outputs": [],
"outputs": [
{
"data": {
"text/plain": [
"<__main__.CustomInitializer at 0x230d9e527b0>"
]
},
"execution_count": null,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"from pyrit.common.apply_defaults import set_default_value\n",
"from pyrit.prompt_target import OpenAIChatTarget\n",
Expand All @@ -50,12 +61,15 @@
" def execution_order(self) -> int:\n",
" return 2 # Lower numbers run first (default is 1)\n",
" \n",
" def initialize(self) -> None:\n",
" async def initialize_async(self) -> None:\n",
" set_default_value(class_type=OpenAIChatTarget, parameter_name=\"temperature\", value=0.9)\n",
"\n",
" @property\n",
" def description(self) -> str:\n",
" return \"Sets custom temperature for OpenAI targets\""
" return \"Sets custom temperature for OpenAI targets\"\n",
" \n",
"\n",
"CustomInitializer()"
]
},
{
Expand All @@ -80,11 +94,11 @@
"metadata": {},
"outputs": [],
"source": [
"from pyrit.setup import initialize_pyrit\n",
"from pyrit.setup import initialize_pyrit_async\n",
"from pyrit.setup.initializers import SimpleInitializer\n",
"\n",
"# Using built-in initializer\n",
"initialize_pyrit(\n",
"await initialize_pyrit_async( # type: ignore\n",
" memory_db_type=\"InMemory\",\n",
" initializers=[SimpleInitializer()]\n",
")"
Expand Down Expand Up @@ -117,7 +131,7 @@
"name": "stdout",
"output_type": "stream",
"text": [
"Created: C:\\Users\\rlundeen\\AppData\\Local\\Temp\\tmpgs282kvw\\custom_init.py\n"
"Created: C:\\Users\\rlundeen\\AppData\\Local\\Temp\\tmpa2k36reo\\custom_init.py\n"
]
}
],
Expand All @@ -126,14 +140,14 @@
"import shutil\n",
"import tempfile\n",
"\n",
"from pyrit.setup import initialize_pyrit\n",
"from pyrit.setup import initialize_pyrit_async\n",
"\n",
"temp_dir = tempfile.mkdtemp()\n",
"script_path = os.path.join(temp_dir, \"custom_init.py\")\n",
"\n",
"# This is the simple custom initializer from the \"Creating an Initializer\" section of this notebook\n",
"script_content = '''\n",
"from pyrit.setup.initializers.base import PyRITInitializer\n",
"from pyrit.setup.initializers.pyrit_initializer import PyRITInitializer\n",
"from pyrit.common.apply_defaults import set_default_value\n",
"from pyrit.prompt_target import OpenAIChatTarget\n",
"\n",
Expand All @@ -146,7 +160,7 @@
" def execution_order(self) -> int:\n",
" return 2 # Lower numbers run first (default is 1)\n",
" \n",
" def initialize(self) -> None:\n",
" async def initialize_async(self) -> None:\n",
" set_default_value(class_type=OpenAIChatTarget, parameter_name=\"temperature\", value=0.9)\n",
"\n",
" @property\n",
Expand All @@ -161,7 +175,7 @@
"print(f\"Created: {script_path}\")\n",
"\n",
"\n",
"initialize_pyrit(\n",
"await initialize_pyrit_async( # type: ignore\n",
" memory_db_type=\"InMemory\",\n",
" initialization_scripts=[temp_dir + \"/custom_init.py\"]\n",
")\n",
Expand Down
19 changes: 12 additions & 7 deletions doc/code/setup/pyrit_initializer.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
#
# ## Execution Order
#
# When `initialize_pyrit_async` is called:
# When `initialize_pyrit` is called:
# 1. Environment files are loaded (`.env`, `.env.local`)
# 2. Memory database is configured
# 3. All initializers are sorted by `execution_order` and executed
Expand All @@ -27,10 +27,11 @@
# %% [markdown]
# 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.

# %%
from pyrit.common.apply_defaults import set_default_value
from pyrit.prompt_target import OpenAIChatTarget
from pyrit.setup.initializers import PyRITInitializer

# %%
from pyrit.setup.initializers.pyrit_initializer import PyRITInitializer


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

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

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


CustomInitializer()

# %% [markdown]
# ## Built-in Initializers
#
Expand Down Expand Up @@ -91,7 +94,7 @@ def description(self) -> str:

# This is the simple custom initializer from the "Creating an Initializer" section of this notebook
script_content = """
from pyrit.setup.initializers import PyRITInitializer
from pyrit.setup.initializers.pyrit_initializer import PyRITInitializer
from pyrit.common.apply_defaults import set_default_value
from pyrit.prompt_target import OpenAIChatTarget

Expand All @@ -104,7 +107,7 @@ def name(self) -> str:
def execution_order(self) -> int:
return 2 # Lower numbers run first (default is 1)

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

@property
Expand All @@ -119,7 +122,9 @@ def description(self) -> str:
print(f"Created: {script_path}")


await initialize_pyrit_async(memory_db_type="InMemory", initialization_scripts=[temp_dir + "/custom_init.py"]) # type: ignore
await initialize_pyrit_async( # type: ignore
memory_db_type="InMemory", initialization_scripts=[temp_dir + "/custom_init.py"]
)


if os.path.exists(temp_dir):
Expand Down
2 changes: 1 addition & 1 deletion doc/code/targets/1_openai_chat_target.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@
"attack = PromptSendingAttack(objective_target=target)\n",
"\n",
"result = await attack.execute_async(objective=jailbreak_prompt) # type: ignore\n",
"await ConsoleAttackResultPrinter().print_conversation_async(result=result) # type: ignore\n"
Comment thread
romanlutz marked this conversation as resolved.
"await ConsoleAttackResultPrinter().print_conversation_async(result=result) # type: ignore"
]
},
{
Expand Down
2 changes: 1 addition & 1 deletion doc/code/targets/5_multi_modal_targets.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@
")\n",
"\n",
"result = await attack.execute_async(objective=objective) # type: ignore\n",
"await ConsoleAttackResultPrinter().print_result_async(result=result) # type: ignore\n"
"await ConsoleAttackResultPrinter().print_result_async(result=result) # type: ignore"
]
},
{
Expand Down