-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
27 changed files
with
350 additions
and
143 deletions.
There are no files selected for viewing
This file contains 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 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 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 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 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 |
---|---|---|
@@ -1,7 +1,12 @@ | ||
from .dataset import Dataset, JailbreakDataset, Prompt | ||
from .dataset import Dataset | ||
from .prompt import PromptDataset, Prompt, JailbreakPromptDataset | ||
from .sample import SampleDataset, Sample | ||
|
||
__all__ = [ | ||
"Dataset", | ||
"JailbreakDataset", | ||
"PromptDataset", | ||
"Prompt", | ||
"JailbreakPromptDataset", | ||
"SampleDataset", | ||
"Sample", | ||
] |
This file contains 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 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,58 @@ | ||
import os | ||
from pathlib import Path | ||
from typing import Sequence, Optional | ||
from dataclasses import dataclass | ||
|
||
from .dataset import Dataset, YamlDeserializable | ||
|
||
|
||
@dataclass | ||
class Prompt(YamlDeserializable): | ||
"""A prompt configuration.""" | ||
|
||
name: str | ||
skip: bool | ||
source: str | ||
language: str | ||
tags: Sequence[str] | ||
parameters: Sequence[str] | ||
template: str | ||
|
||
|
||
class PromptDataset(Dataset[Prompt]): | ||
"""Dataset for prompts.""" | ||
|
||
def __init__(self, prompts: Sequence[Prompt]) -> None: | ||
"""Initialize the PromptDataset with a sequence of prompts. | ||
Args: | ||
prompts (Sequence[Prompt]): The prompts to initialize the dataset with. | ||
""" | ||
self._prompts = prompts | ||
|
||
@classmethod | ||
def load_from_directory( | ||
cls, path: Path, tags_filter: Optional[Sequence[str]] = None | ||
) -> "PromptDataset": | ||
"""Create a JailbreakDataset instance by loading prompts from a directory. | ||
Args: | ||
path (Path): The path to the directory containing prompt YAML files. | ||
tags_filter (Sequence[str], optional): Tags to filter prompts. Defaults to None. | ||
Returns: | ||
JailbreakDataset: A dataset containing prompts loaded from the directory. | ||
""" | ||
prompts = [] | ||
for file_name in os.listdir(path): | ||
prompt = Prompt.from_yaml_file(path / file_name) | ||
if not prompt.skip and ( | ||
not tags_filter or set(prompt.tags).intersection(tags_filter) | ||
): | ||
prompts.append(prompt) | ||
return cls(prompts) | ||
|
||
|
||
JailbreakPromptDataset = PromptDataset.load_from_directory( | ||
Path(__file__, "..", "prompts").resolve(), tags_filter=["jailbreak"] | ||
) |
File renamed without changes.
File renamed without changes.
File renamed without changes.
This file contains 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,51 @@ | ||
import os | ||
from typing import Sequence, Optional | ||
from pathlib import Path | ||
from dataclasses import dataclass | ||
|
||
from .dataset import Dataset, YamlDeserializable | ||
|
||
|
||
@dataclass | ||
class Sample(YamlDeserializable): | ||
"""A sample configuration.""" | ||
|
||
skip: bool | ||
input: str | ||
output: str | ||
language: str | ||
tags: Sequence[str] | ||
|
||
|
||
class SampleDataset(Dataset[Sample]): | ||
"""Dataset for samples.""" | ||
|
||
def __init__(self, samples: Sequence[Sample]) -> None: | ||
"""Initialize the SampleDataset with a sequence of samples. | ||
Args: | ||
samples (Sequence[Sample]): The samples to initialize the dataset with. | ||
""" | ||
self._samples = samples | ||
|
||
@classmethod | ||
def load_from_directory( | ||
cls, path: Path, tags_filter: Optional[Sequence[str]] = None | ||
) -> "SampleDataset": | ||
"""Create a SampleDataset instance by loading samples from a directory. | ||
Args: | ||
path (Path): The path to the directory containing sample YAML files. | ||
tags_filter (Optional[Sequence[str]], optional): Tags to filter samples. Defaults to None. | ||
Returns: | ||
SampleDataset: A dataset containing samples loaded from the directory. | ||
""" | ||
samples = [] | ||
for file_name in os.listdir(path): | ||
sample = Sample.from_yaml_file(path / file_name) | ||
if not sample.skip and ( | ||
not tags_filter or set(sample.tags).intersection(tags_filter) | ||
): | ||
samples.append(sample) | ||
return cls(samples) |
This file contains 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 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 was deleted.
Oops, something went wrong.
This file contains 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 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 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 |
---|---|---|
@@ -1,3 +1,7 @@ | ||
from .many_shot_plugin import ManyShotPlugin | ||
from .prompt_injection_plugin import PromptInjectionPlugin | ||
|
||
__all__ = ["PromptInjectionPlugin"] | ||
__all__ = [ | ||
"ManyShotPlugin", | ||
"PromptInjectionPlugin", | ||
] |
Oops, something went wrong.