-
Notifications
You must be signed in to change notification settings - Fork 609
FEAT Add JailbreakV_28k dataset from HF #1098
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
Open
AdrGav941
wants to merge
17
commits into
Azure:main
Choose a base branch
from
AdrGav941:add__HF_jailbreakV_28K_dataset
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 4 commits
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
1ab7c61
Adding JailBreakV_28k dataset and tests
5d297d3
Fixes for handling splits
1afab68
pre-commit hooks
2b7b81f
removing unused params
8ec3baa
Adding image support handling for invalid image_path entries coming f…
115c1c2
Integration tests, ValueError for empty seed_prompts, comment cleanup
725418b
Adding threshold for prompts returned, associated tests and local ima…
625a16c
Merge branch 'main' into add__HF_jailbreakV_28K_dataset
AdrGav941 9d65116
Fixing sorting for dataset init imports
615b01d
Fixing merge conflicts
31f7c48
Removing integration tests with manual download approach
8a9fa3b
Cleaning up comments
8439969
Merge branch 'main' into add__HF_jailbreakV_28K_dataset
AdrGav941 5becda3
Adding excpetion for JailbrakV-28k dataset in integration presence ve…
c9b9dc3
Merge remote-tracking branch 'refs/remotes/adrgav941/add__HF_jailbrea…
c2d8540
Removing jailbreakV-28k from integration tests
5a9de48
Merge branch 'main' into add__HF_jailbreakV_28K_dataset
AdrGav941 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
Some comments aren't visible on the classic Files Changed page.
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
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,116 @@ | ||
| # Copyright (c) Microsoft Corporation. | ||
| # Licensed under the MIT license. | ||
|
|
||
| import logging | ||
| from typing import List, Literal, Optional | ||
|
|
||
| from datasets import load_dataset | ||
|
|
||
| from pyrit.models import SeedPrompt, SeedPromptDataset | ||
|
|
||
| logger = logging.getLogger(__name__) | ||
|
|
||
| HarmLiteral = Literal[ | ||
| "Unethical Behavior", | ||
| "Economic Harm", | ||
| "Hate Speech", | ||
| "Government Decision", | ||
| "Physical Harm", | ||
| "Fraud", | ||
| "Political Sensitivity", | ||
| "Malware", | ||
| "Illegal Activity", | ||
| "Bias", | ||
| "Violence", | ||
| "Animal Abuse", | ||
| "Tailored Unlicensed Advice", | ||
| "Privacy Violation", | ||
| "Health Consultation", | ||
| "Child Abuse Content", | ||
| ] | ||
|
|
||
|
|
||
| def fetch_jailbreakv_28k_dataset( | ||
| *, | ||
| data_home: Optional[str] = None, | ||
| split: Literal["JailBreakV_28K", "mini_JailBreakV_28K"] = "mini_JailBreakV_28K", | ||
| text_field: Literal["jailbreak_query", "redteam_query"] = "redteam_query", | ||
| harm_categories: Optional[List[HarmLiteral]] = None, | ||
| ) -> SeedPromptDataset: | ||
AdrGav941 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| """ | ||
| Fetch examples from the JailBreakV 28k Dataset with optional filtering and create a SeedPromptDataset. | ||
| Args: | ||
| data_home: Directory used as cache_dir in call to HF to store cached data. Defaults to None. | ||
| split (str): The split of the dataset to fetch. Defaults to "mini_JailBreakV_28K". | ||
| Options are "JailBreakV_28K" and "mini_JailBreakV_28K". | ||
| text_field (str): The field to use as the prompt text. Defaults to "redteam_query". | ||
| harm_categories: List of harm categories to filter the examples. | ||
| Defaults to None, which means all categories are included. | ||
| Otherwise, only prompts with at least one matching category are included. | ||
| Returns: | ||
| SeedPromptDataset: A SeedPromptDataset containing the filtered examples. | ||
| Note: | ||
| For more information and access to the original dataset and related materials, visit: | ||
| https://huggingface.co/datasets/JailbreakV-28K/JailBreakV-28k/blob/main/README.md \n | ||
| Related paper: https://arxiv.org/abs/2404.03027 \n | ||
| The dataset license: mit | ||
AdrGav941 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| Warning: | ||
| Due to the nature of these prompts, it may be advisable to consult your relevant legal | ||
| department before testing them with LLMs to ensure compliance and reduce potential risks. | ||
| """ | ||
|
|
||
| source = "JailbreakV-28K/JailBreakV-28k" | ||
|
|
||
| try: | ||
| logger.info(f"Loading JailBreakV-28k dataset from {source}") | ||
|
|
||
| # Normalize the harm categories to match pyrit harm category conventions | ||
| harm_categories_normalized = ( | ||
| None if not harm_categories else [_normalize_policy(policy) for policy in harm_categories] | ||
| ) | ||
|
|
||
| # Load the dataset from HuggingFace | ||
| data = load_dataset(source, "JailBreakV_28K", cache_dir=data_home) | ||
|
|
||
| dataset_split = data[split] | ||
|
|
||
| seed_prompts = [] | ||
|
|
||
| # Define common metadata that will be used across all seed prompts | ||
| common_metadata = { | ||
| "dataset_name": "JailbreakV-28K", | ||
| "authors": ["Weidi Luo", "Siyuan Ma", "Xiaogeng Liu", "Chaowei Xiao"], | ||
AdrGav941 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| "description": ( | ||
| "Benchmark for Assessing the Robustness of Large Language Models against Jailbreak Attacks. " | ||
| ), | ||
| "source": source, | ||
| "data_type": "text", | ||
AdrGav941 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| "name": "JailBreakV-28K", | ||
| } | ||
|
|
||
| for item in dataset_split: | ||
| policy = _normalize_policy(item.get("policy", "")) | ||
| # Skip if user requested policy filter and items policy does not match | ||
| if harm_categories_normalized and policy not in harm_categories_normalized: | ||
AdrGav941 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| continue | ||
| seed_prompt = SeedPrompt( | ||
| value=item.get(text_field, ""), | ||
| harm_categories=[policy], | ||
| **common_metadata, # type: ignore[arg-type] | ||
| ) | ||
| seed_prompts.append(seed_prompt) | ||
| seed_prompt_dataset = SeedPromptDataset(prompts=seed_prompts) | ||
| return seed_prompt_dataset | ||
|
|
||
| except Exception as e: | ||
| logger.error(f"Failed to load JailBreakV-28K dataset: {str(e)}") | ||
| raise Exception(f"Error loading JailBreakV-28K dataset: {str(e)}") | ||
|
|
||
|
|
||
| def _normalize_policy(policy: str) -> str: | ||
| """Create a machine-friendly variant alongside the human-readable policy.""" | ||
| return policy.strip().lower().replace(" ", "_").replace("-", "_") | ||
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,57 @@ | ||
| # Copyright (c) Microsoft Corporation. | ||
| # Licensed under the MIT license. | ||
|
|
||
| from unittest.mock import patch | ||
|
|
||
| import pytest | ||
|
|
||
| from pyrit.datasets.fetch_jailbreakv_28k_dataset import fetch_jailbreakv_28k_dataset | ||
| from pyrit.models import SeedPrompt, SeedPromptDataset | ||
|
|
||
|
|
||
| class TestFetchJailbreakv28kDataset: | ||
| """Test suite for the fetch_jailbreakv_28k_dataset function.""" | ||
|
|
||
| @pytest.mark.parametrize("text_field", [None, "jailbreak_query"]) | ||
| @pytest.mark.parametrize("harm_categories", [None, ["Economic Harm"]]) | ||
| @patch("pyrit.datasets.fetch_jailbreakv_28k_dataset.load_dataset") | ||
| def test_fetch_jailbreakv_28k_dataset_success(self, mock_load_dataset, text_field, harm_categories): | ||
| # Mock dataset response | ||
| mock_dataset = { | ||
| "mini_JailBreakV_28K": [ | ||
| { | ||
| "redteam_query": "test query 1", | ||
| "jailbreak_query": "jailbreak: test query 1", | ||
| "policy": "Economic Harm", | ||
| }, | ||
| { | ||
| "redteam_query": "test query 2", | ||
| "jailbreak_query": "jailbreak: test query 2", | ||
| "policy": "Government Decision", | ||
| }, | ||
| { | ||
| "redteam_query": "test query 3", | ||
| "jailbreak_query": "jailbreak: test query 3", | ||
| "policy": "Fraud", | ||
| }, | ||
| ] | ||
| } | ||
| mock_load_dataset.return_value = mock_dataset | ||
|
|
||
| # Call the function | ||
| result = fetch_jailbreakv_28k_dataset(text_field=text_field, harm_categories=harm_categories) | ||
|
|
||
| # Assertions | ||
| assert isinstance(result, SeedPromptDataset) | ||
| if harm_categories is None: | ||
| assert len(result.prompts) == 3 | ||
| elif harm_categories == ["Economic Harm"]: | ||
| assert len(result.prompts) == 1 | ||
| print(result.prompts) | ||
| assert result.prompts[0].harm_categories == ["economic_harm"] | ||
| assert all(isinstance(prompt, SeedPrompt) for prompt in result.prompts) | ||
| print(result.prompts) | ||
| if text_field == "jailbreak_query": | ||
| assert all("jailbreak" in prompt.value for prompt in result.prompts) | ||
| else: | ||
| assert all("jailbreak" not in prompt.value for prompt in result.prompts) |
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.