Skip to content

DeepTeam is a framework to red team LLMs and AI agents.

lint

A-Vamshi19929197c36e
diff --git a/deepteam/attacks/attack_simulator/attack_simulator.py b/deepteam/attacks/attack_simulator/attack_simulator.py
index 43c122ef..1f9aeef8 100644
--- a/deepteam/attacks/attack_simulator/attack_simulator.py
+++ b/deepteam/attacks/attack_simulator/attack_simulator.py
@@ -107,7 +107,6 @@ class AttackSimulator:
self.test_cases.extend(test_cases)
return test_cases
-
async def a_simulate(
self,
attacks_per_vulnerability_type: int,
@@ -180,7 +179,9 @@ class AttackSimulator:
if not attacks:
attack = BaselineAttack()
else:
- attack_weights = [attack.weight for attack in attacks]
+ attack_weights = [
+ attack.weight for attack in attacks
+ ]
attack = random.choices(
attacks, weights=attack_weights, k=1
)[0]
diff --git a/deepteam/frameworks/aegis/aegis.py b/deepteam/frameworks/aegis/aegis.py
index 84a3efe7..157b4c00 100644
--- a/deepteam/frameworks/aegis/aegis.py
+++ b/deepteam/frameworks/aegis/aegis.py
@@ -11,11 +11,12 @@ from deepteam.frameworks.aegis.types import AegisType
from deepteam.test_case import RTTestCase
from deepteam.metrics import HarmMetric
+
class Aegis(AISafetyFramework):
_DATASET = "nvidia/Aegis-AI-Content-Safety-Dataset-1.0"
def __init__(
- self,
+ self,
num_attacks: int = 15,
async_mode: bool = True,
verbose_mode: bool = False,
@@ -28,40 +29,52 @@ class Aegis(AISafetyFramework):
self.verbose_mode = verbose_mode
self.evaluation_model = evaluation_model
self._is_dataset = True
-
+
def simulate_attacks(self):
from datasets import load_dataset
dataset = load_dataset(self._DATASET, split="train")
- attacks = [row for row in dataset if row.get("text_type") == "user_message" and row.get("labels_0") != "Safe"]
+ attacks = [
+ row
+ for row in dataset
+ if row.get("text_type") == "user_message"
+ and row.get("labels_0") != "Safe"
+ ]
if len(attacks) < self.num_attacks:
- raise ValueError(f"Please set your 'num_attacks' to less than or equal to {len(attacks)}.")
+ raise ValueError(
+ f"Please set your 'num_attacks' to less than or equal to {len(attacks)}."
+ )
sampled = random.sample(attacks, k=self.num_attacks)
test_cases = [
RTTestCase(
input=attack["text"],
vulnerability=attack["labels_0"],
- vulnerability_type=AegisType.AEGIS
+ vulnerability_type=AegisType.AEGIS,
)
for attack in sampled
]
return test_cases
-
async def a_simulate_attacks(self):
from datasets import load_dataset
- dataset = await asyncio.to_thread(load_dataset, self._DATASET, split="train")
-
+ dataset = await asyncio.to_thread(
+ load_dataset, self._DATASET, split="train"
+ )
+
attacks = [
- row for row in dataset
- if row.get("text_type") == "user_message" and row.get("labels_0") != "Safe"
+ row
+ for row in dataset
+ if row.get("text_type") == "user_message"
+ and row.get("labels_0") != "Safe"
]
if len(attacks) < self.num_attacks:
- raise ValueError(f"Please set your 'num_attacks' to less than or equal to {len(attacks)}.")
+ raise ValueError(
+ f"Please set your 'num_attacks' to less than or equal to {len(attacks)}."
+ )
sampled = random.sample(attacks, k=self.num_attacks)
@@ -69,17 +82,15 @@ class Aegis(AISafetyFramework):
RTTestCase(
input=attack["text"],
vulnerability=attack["labels_0"],
- vulnerability_type=AegisType.AEGIS
+ vulnerability_type=AegisType.AEGIS,
)
for attack in sampled
]
return test_cases
-
+
def evaluate_test_cases(
- self,
- test_cases: List[RTTestCase],
- model_callback: CallbackType
+ self, test_cases: List[RTTestCase], model_callback: CallbackType
):
evaluated_test_cases = []
pbar = tqdm(
@@ -96,15 +107,13 @@ class Aegis(AISafetyFramework):
pbar.update(1)
pbar.close()
-
+
return evaluated_test_cases
-
+
async def a_evaluate_test_cases(
- self,
- test_cases: List[RTTestCase],
- model_callback: CallbackType
+ self, test_cases: List[RTTestCase], model_callback: CallbackType
):
-
+
pbar = tqdm(
total=len(test_cases),
desc=f"📝 Evaluating {len(test_cases)} test cases using {self.get_name()} risk categories",
@@ -116,9 +125,9 @@ class Aegis(AISafetyFramework):
await metric.a_measure(test_case)
test_case.score = metric.score
test_case.reason = metric.reason
-
+
pbar.update(1)
-
+
return test_case
tasks = [evaluate_test_case(tc) for tc in test_cases]
@@ -129,14 +138,13 @@ class Aegis(AISafetyFramework):
return evaluated_test_cases
-
def _get_metric(self, harm_category: str):
return HarmMetric(
harm_category=harm_category,
model=self.evaluation_model,
async_mode=self.async_mode,
- verbose_mode=self.verbose_mode
+ verbose_mode=self.verbose_mode,
)
-
+
def get_name(self):
return "Aegis"
diff --git a/deepteam/frameworks/aegis/types.py b/deepteam/frameworks/aegis/types.py
index 56440d9c..4cce5503 100644
--- a/deepteam/frameworks/aegis/types.py
+++ b/deepteam/frameworks/aegis/types.py
@@ -6,6 +6,4 @@ class AegisType(Enum):
AEGIS = "aegis_safety_dataset"
-AegisTypes = Literal[
- AegisType.AEGIS.value
-]
+AegisTypes = Literal[AegisType.AEGIS.value]
diff --git a/deepteam/red_teamer/red_teamer.py b/deepteam/red_teamer/red_teamer.py
index 7b3558dd..68cac0af 100644
--- a/deepteam/red_teamer/red_teamer.py
+++ b/deepteam/red_teamer/red_teamer.py
@@ -108,7 +108,11 @@ class RedTeamer:
if simulator_model is not None:
self.simulator_model = simulator_model
with capture_red_teamer_run(
- vulnerabilities=[v.get_name() for v in vulnerabilities] if vulnerabilities else [f"Framework: {framework.get_name()}"],
+ vulnerabilities=(
+ [v.get_name() for v in vulnerabilities]
+ if vulnerabilities
+ else [f"Framework: {framework.get_name()}"]
+ ),
attacks=[a.get_name() for a in attacks] if attacks else [],
):
# Generate attacks
@@ -232,7 +236,11 @@ class RedTeamer:
self.simulator_model = simulator_model
with capture_red_teamer_run(
- vulnerabilities=[v.get_name() for v in vulnerabilities] if vulnerabilities else [f"Framework: {framework.get_name()}"],
+ vulnerabilities=(
+ [v.get_name() for v in vulnerabilities]
+ if vulnerabilities
+ else [f"Framework: {framework.get_name()}"]
+ ),
attacks=[a.get_name() for a in attacks] if attacks else [],
):
# Generate attacks