-
Notifications
You must be signed in to change notification settings - Fork 580
FEAT: Adding Harm Categories to Prompt Request Pieces #1116
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
jbolor21
merged 21 commits into
Azure:main
from
jbolor21:users/bjagdagdorj/harm_categories
Oct 10, 2025
Merged
Changes from 15 commits
Commits
Show all changes
21 commits
Select commit
Hold shift + click to select a range
a6f4565
initial commit adding changes to include harm_categories in prompt re…
4e0deda
Merge remote-tracking branch 'origin/main' into users/bjagdagdorj/har…
2d6afd1
adding in query to find harm_categories in attack results
d797592
adding notebook example
7dc7ab8
fixing toc
128f1f4
beginnning to fix unit tests
0fde92d
fixed seed prompt unit test
95bb2b4
fixed seed prompt unit test
07824f7
remove OR, fix unit tests, pre-commit
53a622e
adding example into cookbook notebook and small precommit
317d460
added new unit tests
ef65259
addressing feedback adding unit tests
66d96f6
rename harm categories
1147f59
Merge remote-tracking branch 'origin/main' into users/bjagdagdorj/har…
c80bbcf
minor edits, precommit
d34867c
pre-commit
2aca793
addressed feedback
87d53dc
merge conflict
c989bab
fixed unit test
8e80f28
minor changes for comment feedback
cc9ef39
minor feedback
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
Large diffs are not rendered by default.
Oops, something went wrong.
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,128 @@ | ||
# --- | ||
# jupyter: | ||
# jupytext: | ||
# text_representation: | ||
# extension: .py | ||
# format_name: percent | ||
# format_version: '1.3' | ||
# jupytext_version: 1.17.2 | ||
# --- | ||
|
||
# %% [markdown] | ||
# # Querying by Harm Categories | ||
# | ||
# This notebook shows how you can query attack results by harm category, as this data is not duplicated into the attack results. Instead we can use SQL queries to do this filtering. | ||
|
||
# %% [markdown] | ||
# ## Import Seed Prompt Dataset | ||
# | ||
# First we import a dataset which has individual prompts with differnt harm categories as an example. | ||
jbolor21 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
||
# %% | ||
import pathlib | ||
|
||
from pyrit.common.initialization import initialize_pyrit | ||
from pyrit.common.path import DATASETS_PATH | ||
from pyrit.memory.central_memory import CentralMemory | ||
from pyrit.models import SeedPromptDataset | ||
|
||
initialize_pyrit(memory_db_type="InMemory") | ||
|
||
memory = CentralMemory.get_memory_instance() | ||
|
||
seed_prompts = SeedPromptDataset.from_yaml_file(pathlib.Path(DATASETS_PATH) / "seed_prompts" / "illegal.prompt") | ||
|
||
print(f"Dataset name: {seed_prompts.dataset_name}") | ||
print(f"Number of prompts in dataset: {len(seed_prompts.prompts)}") | ||
print() | ||
|
||
await memory.add_seed_prompts_to_memory_async(prompts=seed_prompts.prompts, added_by="bolor") # type: ignore | ||
for i, prompt in enumerate(seed_prompts.prompts): | ||
print(f"Prompt {i+1}: {prompt.value}, Harm Categories: {prompt.harm_categories}") | ||
|
||
# %% [markdown] | ||
# ## Send to target | ||
# | ||
# We use prompt sending attack to create our attack results | ||
jbolor21 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
||
# %% | ||
from pyrit.executor.attack import ConsoleAttackResultPrinter, PromptSendingAttack | ||
from pyrit.prompt_target import OpenAIChatTarget | ||
|
||
# Create a real OpenAI target | ||
target = OpenAIChatTarget() | ||
|
||
# Create the attack with the OpenAI target | ||
attack = PromptSendingAttack(objective_target=target) | ||
|
||
# Get our seed prompt groups with harm categories | ||
groups = memory.get_seed_prompt_groups() | ||
print(f"Total groups: {len(groups)}") | ||
jbolor21 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
||
# Configure this to load the prompts loaded in the previous step. | ||
# In the last section, they were in the illegal.prompt file (which has a configured name of "2025_06_pyrit_illegal_example") | ||
prompt_groups = memory.get_seed_prompt_groups(dataset_name="2025_06_pyrit_illegal_example") | ||
print(f"Found {len(prompt_groups)} prompt groups for dataset") | ||
|
||
for i, group in enumerate(prompt_groups): | ||
prompt_text = group.prompts[0].value | ||
|
||
results = await attack.execute_async(objective=prompt_text, seed_prompt_group=group) # type: ignore | ||
|
||
print(f"Attack completed - Conversation ID: {results.conversation_id}") | ||
await ConsoleAttackResultPrinter().print_conversation_async(result=results) # type: ignore | ||
|
||
# %% [markdown] | ||
# ## Query by harm category | ||
# Now you can query your attack results by harm_category! | ||
jbolor21 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
||
# %% [markdown] | ||
# ### Single harm category: | ||
# | ||
# Query by a single harm category, as example here we query for `illegal` | ||
jbolor21 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
||
# %% | ||
from pyrit.analytics.analyze_results import analyze_results | ||
|
||
all_attack_results = memory.get_attack_results() | ||
|
||
# Demonstrating how to query attack results by harm category | ||
print("=== Querying Attack Results by Harm Category ===") | ||
print() | ||
|
||
# First, let's see all attack results to understand what we have | ||
print(f"Overall attack analytics:") | ||
print(f"Total attack results in memory: {len(all_attack_results)}") | ||
|
||
overall_analytics = analyze_results(list(all_attack_results)) | ||
|
||
print(f" Success rate: {overall_analytics['Attack success rate']}") | ||
print(f" Successes: {overall_analytics['Successes']}") | ||
print(f" Failures: {overall_analytics['Failures']}") | ||
print(f" Undetermined: {overall_analytics['Undetermined']}") | ||
print() | ||
|
||
# Example 1: Query for a single harm category | ||
jbolor21 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
print("1. Query for single harm category 'illegal':") | ||
illegal_attacks = memory.get_attack_results(targeted_harm_categories=["illegal"]) | ||
print(f" Found {len(illegal_attacks)} attack results with 'illegal' category") | ||
jbolor21 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
||
if illegal_attacks: | ||
for i, attack_result in enumerate(illegal_attacks): # Show first 2 | ||
jbolor21 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
print(f" Attack {i+1}: {attack_result.objective}") | ||
print(f" Conversation ID: {attack_result.conversation_id}") | ||
print(f" Outcome: {attack_result.outcome}") | ||
print() | ||
|
||
# %% [markdown] | ||
# ### Query by multiple harm categories | ||
jbolor21 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
||
# %% | ||
# Example 2: Query for multiple harm categories | ||
print("3. Query for multiple harm categories 'illegal' and 'violence':") | ||
jbolor21 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
multiple_groups = memory.get_attack_results(targeted_harm_categories=["illegal", "violence"]) | ||
|
||
for i, attack_result in enumerate(multiple_groups): | ||
print(f" Attack {i+1}: {attack_result.objective}...") | ||
print(f" Conversation ID: {attack_result.conversation_id}") | ||
print() |
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.