-
Notifications
You must be signed in to change notification settings - Fork 763
FEAT: Add CategoricalHarmfulQA (CatQA) dataset loader #1749
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
romanlutz
merged 7 commits into
microsoft:main
from
romanlutz:romanlutz/categorical-harmfulqa-review
May 22, 2026
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
75ef816
Add CategoricalHarmfulQA (CatQA) dataset loader
romanlutz b61b348
Regenerate loading datasets notebook with CatQA in the list
romanlutz 7d42478
Rename empty-category CatQA test for clarity
romanlutz 01060d3
Convert CatQA loader to SeedObjective
romanlutz 7c9c928
Merge remote-tracking branch 'origin/main' into romanlutz/categorical…
romanlutz ef362d2
Refresh loading datasets notebook after main merge
romanlutz d5a27fa
Merge remote-tracking branch 'origin/main' into romanlutz/categorical…
romanlutz 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
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
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
131 changes: 131 additions & 0 deletions
131
pyrit/datasets/seed_datasets/remote/categorical_harmful_qa_dataset.py
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,131 @@ | ||
| # Copyright (c) Microsoft Corporation. | ||
| # Licensed under the MIT license. | ||
|
|
||
| import logging | ||
| from typing import Literal | ||
|
|
||
| from pyrit.datasets.seed_datasets.remote.remote_dataset_loader import ( | ||
| _RemoteDatasetLoader, | ||
| ) | ||
| from pyrit.models import SeedDataset, SeedObjective | ||
|
|
||
| logger = logging.getLogger(__name__) | ||
|
|
||
|
|
||
| class _CategoricalHarmfulQADataset(_RemoteDatasetLoader): | ||
| """ | ||
| Loader for the CategoricalHarmfulQA (CatQA) dataset from HuggingFace. | ||
|
|
||
| CatQA contains 550 harmful questions hand-authored against the combined list of | ||
| prohibited use cases from OpenAI's usage policies and Meta's Llama2 acceptable | ||
| use policy. Questions are organized across 11 main harm categories, each split | ||
| into 5 sub-categories with 10 questions per sub-category. The dataset is | ||
| available in English ("en"), Chinese ("zh"), and Vietnamese ("vi") splits; | ||
| translations were produced by an unaligned LLM and refined by human annotators. | ||
|
|
||
| References: | ||
| - https://huggingface.co/datasets/declare-lab/CategoricalHarmfulQA | ||
| - [@bhardwaj2024homer] | ||
| License: Apache 2.0 | ||
|
|
||
| Warning: This dataset contains harmful questions designed to evaluate LLM safety, | ||
| including content related to child abuse, weapons, and other deeply harmful topics. | ||
| """ | ||
|
|
||
| HF_DATASET_NAME: str = "declare-lab/CategoricalHarmfulQA" | ||
|
|
||
| # Metadata | ||
| harm_categories: list[str] = [ | ||
| "adult content", | ||
| "child abuse", | ||
| "economic harm", | ||
| "fraud/deception", | ||
| "hate/harass/violence", | ||
| "illegal activity", | ||
| "malware viruses", | ||
| "physical harm", | ||
| "political campaigning", | ||
| "privacy violation activity", | ||
| "tailored financial advice", | ||
| ] | ||
| modalities: list[str] = ["text"] | ||
| size: str = "large" # 550 questions per language split | ||
| tags: set[str] = {"safety", "multilingual"} | ||
|
|
||
| def __init__( | ||
| self, | ||
| *, | ||
| language: Literal["en", "zh", "vi"] = "en", | ||
| ) -> None: | ||
| """ | ||
| Initialize the CategoricalHarmfulQA dataset loader. | ||
|
|
||
| Args: | ||
| language (Literal["en", "zh", "vi"]): Language split to load. | ||
| "en" (English), "zh" (Chinese), or "vi" (Vietnamese). Defaults to "en". | ||
| """ | ||
| self.language: Literal["en", "zh", "vi"] = language | ||
|
|
||
| @property | ||
| def dataset_name(self) -> str: | ||
| """Return the dataset name.""" | ||
| return "categorical_harmful_qa" | ||
|
|
||
| async def fetch_dataset_async(self, *, cache: bool = True) -> SeedDataset: | ||
| """ | ||
| Fetch CategoricalHarmfulQA dataset from HuggingFace and return as SeedDataset. | ||
|
|
||
| The Category field is exposed via ``harm_categories``; the Subcategory and | ||
| the language code are recorded in the per-objective ``metadata``. | ||
|
|
||
| Args: | ||
| cache (bool): Whether to cache the fetched dataset. Defaults to True. | ||
|
|
||
| Returns: | ||
| SeedDataset: A SeedDataset containing the CategoricalHarmfulQA objectives. | ||
| """ | ||
| logger.info(f"Loading CategoricalHarmfulQA dataset from {self.HF_DATASET_NAME} (language={self.language})") | ||
|
|
||
| data = await self._fetch_from_huggingface( | ||
| dataset_name=self.HF_DATASET_NAME, | ||
| split=self.language, | ||
| cache=cache, | ||
| ) | ||
|
|
||
| authors = [ | ||
| "Rishabh Bhardwaj", | ||
| "Do Duc Anh", | ||
| "Soujanya Poria", | ||
| ] | ||
| description = ( | ||
| "CategoricalHarmfulQA (CatQA) contains 550 hand-authored harmful questions " | ||
| "organized into 11 main harm categories and 5 sub-categories each, framed " | ||
| "against OpenAI's prohibited use cases and Meta's Llama2 acceptable use policy. " | ||
| "Introduced in 'Language Models are Homer Simpson! Safety Re-Alignment of " | ||
| "Fine-tuned Language Models through Task Arithmetic' (2024). Available in " | ||
| "English, Chinese, and Vietnamese." | ||
| ) | ||
| source_url = f"https://huggingface.co/datasets/{self.HF_DATASET_NAME}" | ||
| groups = ["DeCLaRe Lab, Singapore University of Technology and Design"] | ||
|
|
||
| seed_objectives = [ | ||
| SeedObjective( | ||
| value=item["Question"], | ||
| name="CategoricalHarmfulQA", | ||
| dataset_name=self.dataset_name, | ||
| harm_categories=[item["Category"]] if item.get("Category") else [], | ||
| description=description, | ||
| source=source_url, | ||
| authors=authors, | ||
| groups=groups, | ||
| metadata={ | ||
| "language": self.language, | ||
| **({"subcategory": subcategory} if (subcategory := item.get("Subcategory")) else {}), | ||
| }, | ||
| ) | ||
| for item in data | ||
| ] | ||
|
|
||
| logger.info(f"Successfully loaded {len(seed_objectives)} objectives from CategoricalHarmfulQA dataset") | ||
|
|
||
| return SeedDataset(seeds=seed_objectives, dataset_name=self.dataset_name) | ||
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.