-
Notifications
You must be signed in to change notification settings - Fork 679
FEAT: CBT-Bench Dataset #1411
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
warisgill
wants to merge
4
commits into
Azure:main
Choose a base branch
from
warisgill:feat/cbt-bench-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.
+352
−17
Open
FEAT: CBT-Bench Dataset #1411
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
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
140 changes: 140 additions & 0 deletions
140
pyrit/datasets/seed_datasets/remote/cbt_bench_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,140 @@ | ||
| # Copyright (c) Microsoft Corporation. | ||
| # Licensed under the MIT license. | ||
|
|
||
| import logging | ||
| from typing import Any | ||
|
|
||
| from pyrit.datasets.seed_datasets.remote.remote_dataset_loader import ( | ||
| _RemoteDatasetLoader, | ||
| ) | ||
| from pyrit.models import SeedDataset, SeedPrompt | ||
|
|
||
| logger = logging.getLogger(__name__) | ||
|
|
||
|
|
||
| class _CBTBenchDataset(_RemoteDatasetLoader): | ||
| """ | ||
| Loader for the CBT-Bench dataset from HuggingFace. | ||
|
|
||
| CBT-Bench is a benchmark designed to evaluate the proficiency of Large Language Models | ||
| in assisting Cognitive Behavioral Therapy (CBT). The dataset contains psychotherapy case | ||
| scenarios with client situations, thoughts, and core belief classifications. | ||
|
|
||
| The dataset is organized into multiple configurations covering basic CBT knowledge, | ||
| cognitive model understanding, and therapeutic response generation. | ||
|
|
||
| References: | ||
| - https://huggingface.co/datasets/Psychotherapy-LLM/CBT-Bench | ||
| - https://arxiv.org/abs/2410.13218 | ||
| """ | ||
|
|
||
| def __init__( | ||
| self, | ||
| *, | ||
| source: str = "Psychotherapy-LLM/CBT-Bench", | ||
| config: str = "core_fine_seed", | ||
| split: str = "train", | ||
| ): | ||
| """ | ||
| Initialize the CBT-Bench dataset loader. | ||
|
|
||
| Args: | ||
| source: HuggingFace dataset identifier. Defaults to "Psychotherapy-LLM/CBT-Bench". | ||
| config: Dataset configuration/subset to load. Defaults to "core_fine_seed". | ||
| split: Dataset split to load. Defaults to "train". | ||
| """ | ||
| self.source = source | ||
| self.config = config | ||
| self.split = split | ||
|
|
||
| @property | ||
| def dataset_name(self) -> str: | ||
| """Return the dataset name.""" | ||
| return "cbt_bench" | ||
|
|
||
| async def fetch_dataset(self, *, cache: bool = True) -> SeedDataset: | ||
| """ | ||
| Fetch CBT-Bench dataset from HuggingFace and return as SeedDataset. | ||
|
|
||
| Args: | ||
| cache: Whether to cache the fetched dataset. Defaults to True. | ||
|
|
||
| Returns: | ||
| SeedDataset: A SeedDataset containing CBT-Bench examples. | ||
|
|
||
| Raises: | ||
| ValueError: If the dataset is empty after processing. | ||
| Exception: If the dataset cannot be loaded or processed. | ||
| """ | ||
| logger.info(f"Loading CBT-Bench dataset from {self.source} (config={self.config})") | ||
|
|
||
| data = await self._fetch_from_huggingface( | ||
| dataset_name=self.source, | ||
| config=self.config, | ||
| split=self.split, | ||
| cache=cache, | ||
| ) | ||
|
|
||
| authors = [ | ||
| "Mian Zhang", | ||
| "Xianjun Yang", | ||
| "Xinlu Zhang", | ||
| "Travis Labrum", | ||
| "Jamie C Chiu", | ||
| "Shaun M Eack", | ||
| "Fei Fang", | ||
| "William Yang Wang", | ||
| "Zhiyu Zoey Chen", | ||
| ] | ||
| description = ( | ||
| "CBT-Bench is a benchmark designed to evaluate the proficiency of Large Language Models " | ||
| "in assisting Cognitive Behavioral Therapy (CBT). The dataset covers basic CBT knowledge, " | ||
| "cognitive model understanding, and therapeutic response generation." | ||
| ) | ||
|
|
||
| seed_prompts = [] | ||
|
|
||
| for item in data: | ||
| situation = item.get("situation", "").strip() | ||
| thoughts = item.get("thoughts", "").strip() | ||
|
|
||
| # Combine situation and thoughts as the prompt value | ||
| if situation and thoughts: | ||
| value = f"Situation: {situation}\n\nThoughts: {thoughts}" | ||
| elif situation: | ||
| value = situation | ||
| elif thoughts: | ||
| value = thoughts | ||
| else: | ||
| logger.warning("[CBT-Bench] Skipping item with no situation or thoughts") | ||
| continue | ||
|
|
||
| # Extract core beliefs for metadata | ||
| core_beliefs = item.get("core_belief_fine_grained", []) | ||
|
|
||
| metadata: dict[str, Any] = { | ||
| "config": self.config, | ||
| } | ||
|
|
||
| if core_beliefs: | ||
| metadata["core_belief_fine_grained"] = core_beliefs | ||
|
|
||
| seed_prompt = SeedPrompt( | ||
| value=value, | ||
| data_type="text", | ||
| dataset_name=self.dataset_name, | ||
| harm_categories=["psycho-social harms"], | ||
| description=description, | ||
| source=f"https://huggingface.co/datasets/{self.source}", | ||
| authors=authors, | ||
| metadata=metadata, | ||
| ) | ||
|
|
||
| seed_prompts.append(seed_prompt) | ||
|
|
||
| if not seed_prompts: | ||
| raise ValueError("SeedDataset cannot be empty.") | ||
|
|
||
| logger.info(f"Successfully loaded {len(seed_prompts)} examples from CBT-Bench dataset") | ||
|
|
||
| return SeedDataset(seeds=seed_prompts, dataset_name=self.dataset_name) |
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why do we have personal workspace path here?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If you run in devcontainer it won't show but at the end of the day it does not matter