-
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
45 changed files
with
1,405 additions
and
756 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 was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
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 |
---|---|---|
@@ -1,3 +1,3 @@ | ||
from .pipeline_prompt_injection_identifier import PipelinePromptIjectionIdentifier | ||
from .pipeline_prompt_injection_identifier import PipelinePromptInjectionIdentifier | ||
|
||
__all__ = ["PipelinePromptIjectionIdentifier"] | ||
__all__ = ["PipelinePromptInjectionIdentifier"] |
5 changes: 2 additions & 3 deletions
5
aisploit/classifier/huggingface/hub_prompt_injection_identifier.py
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,5 +1,4 @@ | ||
from aisploit.core import BaseTextClassification, Score | ||
from ...core import BaseClassifier | ||
|
||
|
||
class HubPromptIjectionIdentifier(BaseTextClassification): | ||
class HubPromptIjectionIdentifier(BaseClassifier): | ||
pass |
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,9 @@ | ||
from .base64 import Base64Converter | ||
from .keyboard_typo import KeyboardTypoConverter, KEYBOARD_NEIGHBORS_QWERTY, KEYBOARD_NEIGHBORS_QWERTZ | ||
|
||
__all__ = [ | ||
"Base64Converter", | ||
"KeyboardTypoConverter", | ||
"KEYBOARD_NEIGHBORS_QWERTY", | ||
"KEYBOARD_NEIGHBORS_QWERTZ" | ||
] |
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,7 @@ | ||
import base64 | ||
|
||
from ..core import BaseConverter | ||
|
||
class Base64Converter(BaseConverter): | ||
def convert(self, prompts: list[str]) -> list[str]: | ||
return [base64.b64encode(prompt.encode("utf-8")).decode("utf-8") for prompt in prompts] |
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,96 @@ | ||
import random | ||
|
||
from ..core import BaseConverter | ||
|
||
KEYBOARD_NEIGHBORS_QWERTZ = { | ||
'q': ['w', 'a', 's'], | ||
'w': ['q', 'e', 's', 'd'], | ||
'e': ['w', 'r', 'd', 'f'], | ||
'r': ['e', 't', 'f', 'g'], | ||
't': ['r', 'z', 'g', 'h'], | ||
'z': ['t', 'u', 'h', 'j'], | ||
'u': ['z', 'i', 'j', 'k'], | ||
'i': ['u', 'o', 'k', 'l'], | ||
'o': ['i', 'p', 'l'], | ||
|
||
'a': ['q', 's', 'y'], | ||
's': ['a', 'w', 'e', 'd', 'x', 'y'], | ||
'd': ['s', 'e', 'r', 'f', 'c', 'x'], | ||
'f': ['d', 'r', 't', 'g', 'v', 'c'], | ||
'g': ['f', 't', 'z', 'h', 'b', 'v'], | ||
'h': ['g', 'z', 'u', 'j', 'n', 'b'], | ||
'j': ['h', 'u', 'i', 'k', 'm', 'n'], | ||
'k': ['j', 'i', 'o', 'l', 'm'], | ||
'l': ['k', 'o', 'p'], | ||
|
||
'y': ['a', 's', 'x'], | ||
'x': ['z', 's', 'd', 'c'], | ||
'c': ['x', 'd', 'f', 'v'], | ||
'v': ['c', 'f', 'g', 'b'], | ||
'b': ['v', 'g', 'h', 'n'], | ||
'n': ['b', 'h', 'j', 'm'], | ||
'm': ['n', 'j', 'k'], | ||
} | ||
|
||
KEYBOARD_NEIGHBORS_QWERTY = { | ||
'q': ['w', 'a', 's'], | ||
'w': ['q', 'a', 's', 'd', 'e'], | ||
'e': ['w', 's', 'd', 'f', 'r'], | ||
'r': ['e', 'd', 'f', 'g', 't'], | ||
't': ['r', 'f', 'g', 'h', 'y'], | ||
'y': ['t', 'g', 'h', 'j', 'u'], | ||
'u': ['y', 'h', 'j', 'k', 'i'], | ||
'i': ['u', 'j', 'k', 'l', 'o'], | ||
'o': ['i', 'k', 'l', 'p'], | ||
'p': ['o', 'l'], | ||
|
||
'a': ['q', 'w', 's', 'z'], | ||
's': ['q', 'w', 'e', 'a', 'd', 'z', 'x'], | ||
'd': ['w', 'e', 'r', 's', 'f', 'x', 'c'], | ||
'f': ['e', 'r', 't', 'd', 'g', 'c', 'v'], | ||
'g': ['r', 't', 'y', 'f', 'h', 'v', 'b'], | ||
'h': ['t', 'y', 'u', 'g', 'j', 'b', 'n'], | ||
'j': ['y', 'u', 'i', 'h', 'k', 'n', 'm'], | ||
'k': ['u', 'i', 'o', 'j', 'l', 'm'], | ||
'l': ['i', 'o', 'p', 'k'], | ||
|
||
'z': ['a', 's', 'x'], | ||
'x': ['z', 's', 'd', 'c'], | ||
'c': ['x', 'd', 'f', 'v'], | ||
'v': ['c', 'f', 'g', 'b'], | ||
'b': ['v', 'g', 'h', 'n'], | ||
'n': ['b', 'h', 'j', 'm'], | ||
'm': ['n', 'j', 'k'], | ||
} | ||
|
||
class KeyboardTypoConverter(BaseConverter): | ||
def __init__( | ||
self, | ||
*, | ||
keyboard_neighbors=KEYBOARD_NEIGHBORS_QWERTY, | ||
typo_probability=0.1, | ||
random_state=None, | ||
) -> None: | ||
self._keyboard_neighbors = keyboard_neighbors | ||
self._typo_probability = typo_probability | ||
if random_state is not None: | ||
random.seed(random_state) | ||
|
||
def convert(self, prompts: list[str]) -> list[str]: | ||
typoPrompts = [] | ||
for prompt in prompts: | ||
typoPrompt = "" | ||
for char in prompt: | ||
if random.random() < self._typo_probability and char.lower() in self._keyboard_neighbors: | ||
# Replace the character with a random neighboring key | ||
neighbor_keys = self._keyboard_neighbors[char.lower()] | ||
typo_char = random.choice(neighbor_keys) | ||
# Preserve the original case | ||
if char.isupper(): | ||
typo_char = typo_char.upper() | ||
char = typo_char | ||
typoPrompt += char | ||
|
||
typoPrompts.append(typoPrompt) | ||
|
||
return typoPrompts |
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,13 +1,23 @@ | ||
from .callbacks import BaseCallbackHandler, Callbacks, CallbackManager | ||
from .classifier import BaseClassifier, Score | ||
from .converter import BaseConverter | ||
from .job import BaseJob | ||
from .model import BaseLLM, BaseChatModel, BaseModel, BaseEmbeddings | ||
from .score import Score, BaseTextClassification | ||
from .target import BaseTarget | ||
from .vectorstore import BaseVectorStore | ||
|
||
__all__ = [ | ||
"BaseCallbackHandler", | ||
"Callbacks", | ||
"CallbackManager", | ||
"BaseClassifier", | ||
"Score", | ||
"BaseConverter", | ||
"BaseJob", | ||
"BaseLLM", | ||
"BaseChatModel", | ||
"BaseModel", | ||
"BaseEmbeddings", | ||
"Score", | ||
"BaseTextClassification", | ||
"BaseTarget", | ||
"BaseVectorStore", | ||
] |
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,29 @@ | ||
from typing import List | ||
|
||
|
||
class BaseCallbackHandler: | ||
def on_redteam_attempt(self, attempt: int, prompt: str): | ||
pass | ||
|
||
def on_redteam_attempt_response(self, attempt: int, response: str): | ||
pass | ||
|
||
Callbacks = List[BaseCallbackHandler] | ||
|
||
class CallbackManager: | ||
def __init__( | ||
self, | ||
*, | ||
id: str, | ||
callbacks: List[BaseCallbackHandler] = [], | ||
) -> None: | ||
self.id = id | ||
self._callbacks = callbacks | ||
|
||
def on_redteam_attempt(self, attempt: int, prompt: str): | ||
for cb in self._callbacks: | ||
cb.on_redteam_attempt(attempt, prompt) | ||
|
||
def on_redteam_attempt_response(self, attempt: int, response: str): | ||
for cb in self._callbacks: | ||
cb.on_redteam_attempt_response(attempt, response) |
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,6 @@ | ||
from abc import ABC, abstractmethod | ||
|
||
class BaseConverter(ABC): | ||
@abstractmethod | ||
def convert(self, prompts: list[str]) -> list[str]: | ||
pass |
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,5 @@ | ||
from abc import ABC | ||
|
||
class BaseJob(ABC): | ||
def __init__(self, *, verbose=False) -> None: | ||
self.verbose = verbose |
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,14 +1,18 @@ | ||
from abc import abstractmethod | ||
from typing import Union | ||
from langchain_core.language_models import LanguageModelInput | ||
from langchain_core.messages import BaseMessage | ||
from langchain_core.runnables import Runnable | ||
from langchain_core.embeddings import Embeddings | ||
|
||
class BaseLLM(Runnable[LanguageModelInput, str]): | ||
pass | ||
|
||
BaseLLM = Runnable[LanguageModelInput, str] | ||
|
||
BaseChatModel = Runnable[LanguageModelInput, BaseMessage] | ||
class BaseChatModel(Runnable[LanguageModelInput, BaseMessage]): | ||
@abstractmethod | ||
def supports_functions(self) -> bool: | ||
pass | ||
|
||
BaseModel = Union[BaseLLM, BaseChatModel] | ||
|
||
BaseEmbeddings = Embeddings | ||
BaseEmbeddings = Embeddings |
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,6 @@ | ||
from abc import ABC, abstractmethod | ||
|
||
class BaseTarget(ABC): | ||
@abstractmethod | ||
def send_prompt(self, prompt: str) -> str: | ||
pass |
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,5 @@ | ||
from .dataset import Dataset | ||
|
||
__all__ = [ | ||
"Dataset", | ||
] |
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,3 @@ | ||
class Dataset(): | ||
def __init__(self) -> None: | ||
pass |
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
File renamed without changes.
Oops, something went wrong.