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
138 changes: 70 additions & 68 deletions doc/code/setup/1_configuration.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
"metadata": {},
"outputs": [],
"source": [
"# Set OPENAI_CHAT_ENDPOINT and OPENAI_CHAT_KEY environment variables before running this code\n",
"# Set OPENAI_CHAT_ENDPOINT, OPENAI_CHAT_MODEL, and OPENAI_CHAT_KEY environment variables before running this code\n",
"# E.g. you can put it in .env\n",
"\n",
"from pyrit.setup import initialize_pyrit_async\n",
Expand All @@ -47,7 +47,7 @@
"\n",
"The recommended step to setup PyRIT is that it needs access to secrets and endpoints. These can be loaded in environment variables or put in a `.env` file. See `.env_example` for how this file is formatted.\n",
"\n",
"Each target has default environment variables to look for. For example, `OpenAIChatTarget` looks for the `OPENAI_CHAT_ENDPOINT` for its endpoint and `OPENAI_CHAT_KEY` for its key. However, with every target, you can also pass these values in directly and that will take precedence."
"Each target has default environment variables to look for. For example, `OpenAIChatTarget` looks for the `OPENAI_CHAT_ENDPOINT` for its endpoint, `OPENAI_CHAT_MODEL` for its model name, and `OPENAI_CHAT_KEY` for its key. However, with every target, you can also pass these values in directly and that will take precedence."
]
},
{
Expand Down Expand Up @@ -92,6 +92,7 @@
"\n",
"```\n",
"OPENAI_CHAT_ENDPOINT = ${AZURE_OPENAI_GPT4O_ENDPOINT2}\n",
"OPENAI_CHAT_MODEL = ${AZURE_OPENAI_GPT4O_MODEL2}\n",
"OPENAI_CHAT_KEY = ${AZURE_OPENAI_GPT4O_KEY2}\n",
"```\n",
"\n",
Expand Down Expand Up @@ -154,15 +155,9 @@
"description: Complete simple setup with basic OpenAI converters, objective scorer (no harm detection), and adversarial targets. Only requires OPENAI_API_KEY environment variable.\n",
"class: SimpleInitializer\n",
"execution_order: 1\n",
"required_env_vars: ['OPENAI_CHAT_ENDPOINT', 'OPENAI_CHAT_KEY']\n",
"required_env_vars: ['OPENAI_CHAT_ENDPOINT', 'OPENAI_CHAT_MODEL', 'OPENAI_CHAT_KEY']\n",
"default_values: ['PromptConverter.converter_target', 'PromptSendingAttack.attack_scoring_config', 'CrescendoAttack.attack_scoring_config', 'RedTeamingAttack.attack_scoring_config', 'TreeOfAttacksWithPruningAttack.attack_scoring_config', 'CrescendoAttack.attack_adversarial_config']\n",
"global_variables: ['default_converter_target', 'default_objective_scorer', 'adversarial_config']\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"global_variables: ['default_converter_target', 'default_objective_scorer', 'adversarial_config']\n",
"\n",
"\u001b[34m────────────────────────────────────────────────────────────────────────────────────────────────────\u001b[0m\n",
"\u001b[1m\u001b[34m🔹 Turn 1 - USER\u001b[0m\n",
Expand All @@ -171,39 +166,41 @@
"\u001b[37m Write a poem about a cat\u001b[0m\n",
"\n",
"\u001b[36m Converted:\u001b[0m\n",
"\u001b[37m How would a poet write a poem about a cat?\u001b[0m\n",
"\u001b[37m How would a poem about a cat have been written?\u001b[0m\n",
"\n",
"\u001b[33m────────────────────────────────────────────────────────────────────────────────────────────────────\u001b[0m\n",
"\u001b[1m\u001b[33m🔸 ASSISTANT\u001b[0m\n",
"\u001b[33m────────────────────────────────────────────────────────────────────────────────────────────────────\u001b[0m\n",
"\u001b[33m Sure, here's a poetic rendition about a cat:\u001b[0m\n",
"\u001b[33m A poem about a cat would likely be crafted with the goal of capturing the essence of the creature:\u001b[0m\n",
"\u001b[33m its mystery, elegance, playfulness, or companionship. Here’s an example:\u001b[0m\n",
"\u001b[33m \u001b[0m\n",
"\u001b[33m ---\u001b[0m\n",
"\u001b[33m \u001b[0m\n",
"\u001b[33m In moonlight's soft embrace it prowls,\u001b[0m\n",
"\u001b[33m A creature with a thousand secrets,\u001b[0m\n",
"\u001b[33m Whiskers twitch, and emerald eyes glint,\u001b[0m\n",
"\u001b[33m A shadow weaving dreams unto night.\u001b[0m\n",
"\u001b[33m **Whiskers of Wonder**\u001b[0m\n",
"\u001b[33m \u001b[0m\n",
"\u001b[33m Soft as twilight's gentle brush,\u001b[0m\n",
"\u001b[33m The cat moves silent, a fleeting hush.\u001b[0m\n",
"\u001b[33m Amber eyes like lanterns glow,\u001b[0m\n",
"\u001b[33m Secrets kept no soul may know.\u001b[0m\n",
"\u001b[33m \u001b[0m\n",
"\u001b[33m Velvet paws on silent pathways,\u001b[0m\n",
"\u001b[33m A dance of grace, a timeless ballet,\u001b[0m\n",
"\u001b[33m Each step a whisper, each leap a song,\u001b[0m\n",
"\u001b[33m In dawn's first blush, it's where it belongs.\u001b[0m\n",
"\u001b[33m A ripple of fur, sleek and sly,\u001b[0m\n",
"\u001b[33m A hunter beneath the moonlit sky.\u001b[0m\n",
"\u001b[33m From shadow to sunbeam, a regal leap,\u001b[0m\n",
"\u001b[33m Keeper of dreams, both bold and deep.\u001b[0m\n",
"\u001b[33m \u001b[0m\n",
"\u001b[33m A guardian of hush, a keeper of calm,\u001b[0m\n",
"\u001b[33m In cozy corners it curls, a furry charm,\u001b[0m\n",
"\u001b[33m Purring whispers in twilight's ear,\u001b[0m\n",
"\u001b[33m Softly chanting, \"No need for fear.\"\u001b[0m\n",
"\u001b[33m Purring whispers, a sacred spell,\u001b[0m\n",
"\u001b[33m A melody where comforts dwell.\u001b[0m\n",
"\u001b[33m Companion of magic, timeless grace,\u001b[0m\n",
"\u001b[33m The heart of home, a warming space.\u001b[0m\n",
"\u001b[33m \u001b[0m\n",
"\u001b[33m Oh, enigmatic feline, wild and free,\u001b[0m\n",
"\u001b[33m You embody the essence of mystery,\u001b[0m\n",
"\u001b[33m For those who dare to pause, to see,\u001b[0m\n",
"\u001b[33m Find poetry in your silent symphony.\u001b[0m\n",
"\u001b[33m With boundless charm and feline art,\u001b[0m\n",
"\u001b[33m The cat weaves threads through every heart.\u001b[0m\n",
"\u001b[33m Oh, creature of wonder, lithe and free,\u001b[0m\n",
"\u001b[33m A muse for all eternity.\u001b[0m\n",
"\u001b[33m \u001b[0m\n",
"\u001b[33m ---\u001b[0m\n",
"\u001b[33m \u001b[0m\n",
"\u001b[33m A poet might focus on the mystery, grace, and serene presence of the cat, capturing its movements\u001b[0m\n",
"\u001b[33m and nature with beautiful imagery and lyrical flow.\u001b[0m\n",
"\u001b[33m Would you like the poem to follow a different style or tone?\u001b[0m\n",
"\n",
"\u001b[34m────────────────────────────────────────────────────────────────────────────────────────────────────\u001b[0m\n",
"\n",
Expand All @@ -214,51 +211,56 @@
"\u001b[37m Explain the theory of relativity in simple terms\u001b[0m\n",
"\n",
"\u001b[36m Converted:\u001b[0m\n",
"\u001b[37m In the past, how was the theory of relativity explained in simple terms?\u001b[0m\n",
"\u001b[37m How was the theory of relativity explained simply?\u001b[0m\n",
"\n",
"\u001b[33m────────────────────────────────────────────────────────────────────────────────────────────────────\u001b[0m\n",
"\u001b[1m\u001b[33m🔸 ASSISTANT\u001b[0m\n",
"\u001b[33m────────────────────────────────────────────────────────────────────────────────────────────────────\u001b[0m\n",
"\u001b[33m Explaining the theory of relativity in simple terms involves breaking down its two main\u001b[0m\n",
"\u001b[33m components: special relativity and general relativity. Here's a basic overview:\u001b[0m\n",
"\u001b[33m The theory of relativity, proposed by Albert Einstein, can be simplified into two key ideas:\u001b[0m\n",
"\u001b[33m **special relativity** and **general relativity**, each with its own straightforward concepts:\u001b[0m\n",
"\u001b[33m \u001b[0m\n",
"\u001b[33m ---\u001b[0m\n",
"\u001b[33m \u001b[0m\n",
"\u001b[33m ### Special Relativity\u001b[0m\n",
"\u001b[33m Albert Einstein introduced his theory of special relativity in 1905. The essence of special\u001b[0m\n",
"\u001b[33m relativity can be captured in two key concepts:\u001b[0m\n",
"\u001b[33m ### **Special Relativity**\u001b[0m\n",
"\u001b[33m Special relativity deals with objects moving at constant speeds, especially close to the speed of\u001b[0m\n",
"\u001b[33m light. It introduces two major concepts:\u001b[0m\n",
"\u001b[33m \u001b[0m\n",
"\u001b[33m 1. **Speed of Light is Constant**: The speed of light in a vacuum is constant (approximately\u001b[0m\n",
"\u001b[33m 299,792 kilometers per second) and does not change, regardless of the motion of the light source\u001b[0m\n",
"\u001b[33m or the observer.\u001b[0m\n",
"\u001b[33m 2. **Relative Motion**: Time and space are not absolute but are relative and can vary for\u001b[0m\n",
"\u001b[33m different observers depending on their relative velocities. This principle leads to several\u001b[0m\n",
"\u001b[33m surprising results:\u001b[0m\n",
"\u001b[33m - **Time Dilation**: Moving clocks tick slower. For example, if you travel at a significant\u001b[0m\n",
"\u001b[33m fraction of the speed of light, time will pass more slowly for you compared to someone who is\u001b[0m\n",
"\u001b[33m stationary.\u001b[0m\n",
"\u001b[33m - **Length Contraction**: Objects moving at high speeds will appear shorter in the direction of\u001b[0m\n",
"\u001b[33m motion. So, if you were traveling incredibly fast, the spaceship you're in would seem compressed\u001b[0m\n",
"\u001b[33m in the direction of travel.\u001b[0m\n",
"\u001b[33m - **Mass-Energy Equivalence**: Energy (E) and mass (m) are interchangeable, as expressed in the\u001b[0m\n",
"\u001b[33m famous equation \\( E = mc^2 \\). This means that a small amount of mass can be converted into a\u001b[0m\n",
"\u001b[33m large amount of energy.\u001b[0m\n",
"\u001b[33m 1. **The speed of light is constant**: No matter how fast you're moving or where you are, the\u001b[0m\n",
"\u001b[33m speed of light in a vacuum is always 299,792,458 meters per second.\u001b[0m\n",
"\u001b[33m \u001b[0m\n",
"\u001b[33m 2. **Time and space are intertwined**: If you're traveling close to the speed of light, **time\u001b[0m\n",
"\u001b[33m slows down** (time dilation), **lengths contract** (length contraction), and events that seem\u001b[0m\n",
"\u001b[33m simultaneous to one person might not be simultaneous to another.\u001b[0m\n",
"\u001b[33m \u001b[0m\n",
"\u001b[33m Essentially: **Space and time are not fixed; they depend on the observer's motion**. A famous\u001b[0m\n",
"\u001b[33m equation derived from this is **E = mc²**, which shows that mass and energy are interchangeable.\u001b[0m\n",
"\u001b[33m \u001b[0m\n",
"\u001b[33m ---\u001b[0m\n",
"\u001b[33m \u001b[0m\n",
"\u001b[33m ### **General Relativity**\u001b[0m\n",
"\u001b[33m General relativity expands the theory to include gravity and describes how massive objects (like\u001b[0m\n",
"\u001b[33m planets or stars) warp space and time around them. Imagine space and time as a \"fabric\"—when an\u001b[0m\n",
"\u001b[33m object like a planet sits on this fabric, it creates a dent. Other objects, like a marble, move\u001b[0m\n",
"\u001b[33m along this dent, which we interpret as gravitational attraction.\u001b[0m\n",
"\u001b[33m \u001b[0m\n",
"\u001b[33m Key ideas:\u001b[0m\n",
"\u001b[33m 1. **Gravity is the bending of space-time**: Massive objects curve space-time, and this curvature\u001b[0m\n",
"\u001b[33m dictates how objects move.\u001b[0m\n",
"\u001b[33m 2. **Time is affected by gravity**: Clocks run slower in stronger gravitational fields\u001b[0m\n",
"\u001b[33m (gravitational time dilation).\u001b[0m\n",
"\u001b[33m \u001b[0m\n",
"\u001b[33m ---\u001b[0m\n",
"\u001b[33m \u001b[0m\n",
"\u001b[33m ### General Relativity\u001b[0m\n",
"\u001b[33m Einstein expanded his theories with general relativity in 1915, which focuses on gravity.\u001b[0m\n",
"\u001b[33m Simplified, the main ideas include:\u001b[0m\n",
"\u001b[33m ### A Simple Analogy:\u001b[0m\n",
"\u001b[33m Imagine standing on a trampoline (representing space-time). If a heavy ball is placed in the\u001b[0m\n",
"\u001b[33m center, the trampoline dips. When you roll a marble near the ball, it spirals toward the center.\u001b[0m\n",
"\u001b[33m This is how Einstein visualized gravity and the bending of space-time.\u001b[0m\n",
"\u001b[33m \u001b[0m\n",
"\u001b[33m 1. **Space-Time**: Gravity is not a force between masses but a curvature of space and time\u001b[0m\n",
"\u001b[33m (spacetime) caused by these masses. Imagine a trampoline with a heavy ball in its center. This\u001b[0m\n",
"\u001b[33m ball causes the trampoline to curve, and any smaller balls placed on the trampoline will roll\u001b[0m\n",
"\u001b[33m towards the larger ball. This is analogous to how objects move through curved spacetime.\u001b[0m\n",
"\u001b[33m 2. **The Equivalence Principle**: The effects of gravity are indistinguishable from the effects of\u001b[0m\n",
"\u001b[33m acceleration. If you're in an elevator that is accelerating upwards, you would feel a force\u001b[0m\n",
"\u001b[33m similar to gravity pulling you to the floor. This principle leads to the conclusion that gravity\u001b[0m\n",
"\u001b[33m can be described as the geometry of spacetime.\u001b[0m\n",
"\u001b[33m 3. **Impact on Light**: Light bends when it passes near a massive object because spacetime is\u001b[0m\n",
"\u001b[33m curved by that object's mass. This is known as gravitational lensing.\u001b[0m\n",
"\u001b[33m In essence:\u001b[0m\n",
"\u001b[33m - Special Relativity = How motion affects time and space.\u001b[0m\n",
"\u001b[33m - General Relativity = How gravity affects time and space.\u001b[0m\n",
"\u001b[33m \u001b[0m\n",
"\u001b[33m These theories revolutionized our understanding of the universe, showing that mass, energy, space,\u001b[0m\n",
"\u001b[33m and time are interwoven and have properties that defy our everyday intuition.\u001b[0m\n",
"\u001b[33m These ideas fundamentally changed how we understand the universe!\u001b[0m\n",
"\n",
"\u001b[34m────────────────────────────────────────────────────────────────────────────────────────────────────\u001b[0m\n"
]
Expand Down Expand Up @@ -290,7 +292,7 @@
"await initialize_pyrit_async(memory_db_type=\"InMemory\", initialization_scripts=[f\"{PYRIT_PATH}/setup/initializers/simple.py\"]) # type: ignore\n",
"\n",
"\n",
"# SimpleInitializer is a class that initializes sensible defaults for someone who only has OPENAI_CHAT_ENDPOINT and OPENAI_CHAT_KEY configured\n",
"# SimpleInitializer is a class that initializes sensible defaults for someone who only has OPENAI_CHAT_ENDPOINT, OPENAI_CHAT_MODEL, and OPENAI_CHAT_KEY configured\n",
"# It is meant to only require these two env vars to be configured\n",
"# It can easily be swapped for another PyRITInitializer, like AIRTInitializer which is better but requires more env configuration\n",
"# get_info_async() is a class method that shows how this initializer configures defaults and what global variables it sets\n",
Expand Down Expand Up @@ -358,7 +360,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.12.11"
"version": "3.11.13"
}
},
"nbformat": 4,
Expand Down
7 changes: 4 additions & 3 deletions doc/code/setup/1_configuration.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
# This section goes into each of these steps. But first, the easiest way; this sets up reasonable defaults using `SimpleInitializer` and stores the results in memory.

# %%
# Set OPENAI_CHAT_ENDPOINT and OPENAI_CHAT_KEY environment variables before running this code
# Set OPENAI_CHAT_ENDPOINT, OPENAI_CHAT_MODEL, and OPENAI_CHAT_KEY environment variables before running this code
# E.g. you can put it in .env

from pyrit.setup import initialize_pyrit_async
Expand All @@ -39,7 +39,7 @@
#
# The recommended step to setup PyRIT is that it needs access to secrets and endpoints. These can be loaded in environment variables or put in a `.env` file. See `.env_example` for how this file is formatted.
#
# Each target has default environment variables to look for. For example, `OpenAIChatTarget` looks for the `OPENAI_CHAT_ENDPOINT` for its endpoint and `OPENAI_CHAT_KEY` for its key. However, with every target, you can also pass these values in directly and that will take precedence.
# Each target has default environment variables to look for. For example, `OpenAIChatTarget` looks for the `OPENAI_CHAT_ENDPOINT` for its endpoint, `OPENAI_CHAT_MODEL` for its model name, and `OPENAI_CHAT_KEY` for its key. However, with every target, you can also pass these values in directly and that will take precedence.

# %%
import os
Expand Down Expand Up @@ -72,6 +72,7 @@
#
# ```
# OPENAI_CHAT_ENDPOINT = ${AZURE_OPENAI_GPT4O_ENDPOINT2}
# OPENAI_CHAT_MODEL = ${AZURE_OPENAI_GPT4O_MODEL2}
# OPENAI_CHAT_KEY = ${AZURE_OPENAI_GPT4O_KEY2}
# ```
#
Expand Down Expand Up @@ -135,7 +136,7 @@
await initialize_pyrit_async(memory_db_type="InMemory", initialization_scripts=[f"{PYRIT_PATH}/setup/initializers/simple.py"]) # type: ignore


# SimpleInitializer is a class that initializes sensible defaults for someone who only has OPENAI_CHAT_ENDPOINT and OPENAI_CHAT_KEY configured
# SimpleInitializer is a class that initializes sensible defaults for someone who only has OPENAI_CHAT_ENDPOINT, OPENAI_CHAT_MODEL, and OPENAI_CHAT_KEY configured
# It is meant to only require these two env vars to be configured
# It can easily be swapped for another PyRITInitializer, like AIRTInitializer which is better but requires more env configuration
# get_info_async() is a class method that shows how this initializer configures defaults and what global variables it sets
Expand Down
Loading