DeepTeam is a framework to red team LLMs and AI agents.
Updated red_team to run without attacks
A-Vamshi50feb8adb582
diff --git a/deepteam/attacks/attack_simulator/attack_simulator.py b/deepteam/attacks/attack_simulator/attack_simulator.pyindex e29970c1..2ae134ed 100644--- a/deepteam/attacks/attack_simulator/attack_simulator.py+++ b/deepteam/attacks/attack_simulator/attack_simulator.py@@ -50,8 +50,8 @@ class AttackSimulator:self,attacks_per_vulnerability_type: int,vulnerabilities: List[BaseVulnerability],- attacks: List[BaseAttack],ignore_errors: bool,+ attacks: Optional[List[BaseAttack]] = None,simulator_model: DeepEvalBaseLLM = None,metadata: Optional[dict] = None,) -> List[RTTestCase]:@@ -75,28 +75,27 @@ class AttackSimulator:metadata=metadata,))- # Enhance attacks by sampling from the provided distribution- enhanced_test_cases: List[RTTestCase] = []- pbar = tqdm(- test_cases,- desc=f"✨ Simulating {num_vulnerability_types * attacks_per_vulnerability_type} attacks (using {len(attacks)} method(s))",- )- attack_weights = [attack.weight for attack in attacks]-- for baseline_attack in pbar:- # Randomly sample an enhancement based on the distribution- sampled_attack = random.choices(- attacks, weights=attack_weights, k=1- )[0]- test_case = self.enhance_attack(- attack=sampled_attack,- test_case=baseline_attack,- ignore_errors=ignore_errors,+ if attacks is not None:+ # Enhance attacks by sampling from the provided distribution+ pbar = tqdm(+ test_cases,+ desc=f"✨ Simulating {num_vulnerability_types * attacks_per_vulnerability_type} attacks (using {len(attacks)} method(s))",)- enhanced_test_cases.append(test_case)+ attack_weights = [attack.weight for attack in attacks]++ for baseline_attack in pbar:+ # Randomly sample an enhancement based on the distribution+ sampled_attack = random.choices(+ attacks, weights=attack_weights, k=1+ )[0]+ self.enhance_attack(+ attack=sampled_attack,+ test_case=baseline_attack,+ ignore_errors=ignore_errors,+ )- self.test_cases.extend(enhanced_test_cases)- return enhanced_test_cases+ self.test_cases.extend(test_cases)+ return test_casesasync def a_simulate(self,@@ -145,46 +144,43 @@ class AttackSimulator:test_cases.extend(result)pbar.close()- # Enhance attacks by sampling from the provided distribution- enhanced_test_cases: List[RTTestCase] = []- pbar = tqdm(- total=len(test_cases),- desc=f"✨ Simulating {num_vulnerability_types * attacks_per_vulnerability_type} attacks (using {len(attacks)} method(s))",- )-- async def throttled_attack_method(- baseline_attack: RTTestCase,- ):- async with semaphore: # Throttling applied here- # Randomly sample an enhancement based on the distribution- if not attacks:- attack = BaselineAttack()- else:- attack_weights = [attack.weight for attack in attacks]- attack = random.choices(- attacks, weights=attack_weights, k=1- )[0]+ if attacks is not None:+ # Enhance attacks by sampling from the provided distribution+ pbar = tqdm(+ total=len(test_cases),+ desc=f"✨ Simulating {num_vulnerability_types * attacks_per_vulnerability_type} attacks (using {len(attacks)} method(s))",+ )- result = await self.a_enhance_attack(- attack=attack,- test_case=baseline_attack,- ignore_errors=ignore_errors,- )- pbar.update(1)- return result+ async def throttled_attack_method(+ baseline_attack: RTTestCase,+ ):+ async with semaphore: # Throttling applied here+ # Randomly sample an enhancement based on the distribution+ if not attacks:+ attack = BaselineAttack()+ else:+ attack_weights = [attack.weight for attack in attacks]+ attack = random.choices(+ attacks, weights=attack_weights, k=1+ )[0]++ await self.a_enhance_attack(+ attack=attack,+ test_case=baseline_attack,+ ignore_errors=ignore_errors,+ )+ pbar.update(1)- enhanced_test_cases.extend(await asyncio.gather(*[asyncio.create_task(throttled_attack_method(test_case))for test_case in test_cases])- )pbar.close()- self.test_cases.extend(enhanced_test_cases)- return enhanced_test_cases+ self.test_cases.extend(test_cases)+ return test_cases##################################################### Simulating Base (Unenhanced) Attacks #########diff --git a/deepteam/red_teamer/red_teamer.py b/deepteam/red_teamer/red_teamer.pyindex 0b588be3..ea41e25a 100644--- a/deepteam/red_teamer/red_teamer.py+++ b/deepteam/red_teamer/red_teamer.py@@ -73,9 +73,9 @@ class RedTeamer:vulnerabilities = framework.vulnerabilitiesattacks = framework.attackselse:- if not vulnerabilities or not attacks:+ if not vulnerabilities:raise ValueError(- "You must either provide a 'framework' or 'vulnerabilities' and 'attacks."+ "You must either provide a 'framework' or 'vulnerabilities'")if self.async_mode:@@ -108,7 +108,7 @@ class RedTeamer:self.simulator_model = simulator_modelwith capture_red_teamer_run(vulnerabilities=[v.get_name() for v in vulnerabilities],- attacks=[a.get_name() for a in attacks],+ attacks=[a.get_name() for a in attacks] if attacks else [],):# Generate attacksif (@@ -213,9 +213,9 @@ class RedTeamer:vulnerabilities = framework.vulnerabilitiesattacks = framework.attackselse:- if not vulnerabilities or not attacks:+ if not vulnerabilities:raise ValueError(- "You must either provide a 'framework' or 'vulnerabilities' and 'attacks."+ "You must either provide a 'framework' or 'vulnerabilities'")if evaluation_model is not None:@@ -225,7 +225,7 @@ class RedTeamer:with capture_red_teamer_run(vulnerabilities=[v.get_name() for v in vulnerabilities],- attacks=[a.get_name() for a in attacks],+ attacks=[a.get_name() for a in attacks] if attacks else [],):# Generate attacksif (diff --git a/deepteam/red_teamer/risk_assessment.py b/deepteam/red_teamer/risk_assessment.pyindex 05b97948..d6addb88 100644--- a/deepteam/red_teamer/risk_assessment.py+++ b/deepteam/red_teamer/risk_assessment.py@@ -187,7 +187,7 @@ def construct_risk_assessment_overview(errored = 0for test_case in red_teaming_test_cases:- if not test_case.attack_method or test_case.error:+ if test_case.error:errored += 1continue@@ -199,9 +199,10 @@ def construct_risk_assessment_overview()# Group by attack method- if test_case.attack_method not in attack_method_to_cases:- attack_method_to_cases[test_case.attack_method] = []- attack_method_to_cases[test_case.attack_method].append(test_case)+ if test_case.attack_method is not None:+ if test_case.attack_method not in attack_method_to_cases:+ attack_method_to_cases[test_case.attack_method] = []+ attack_method_to_cases[test_case.attack_method].append(test_case)vulnerability_type_results = []attack_method_results = []@@ -228,24 +229,25 @@ def construct_risk_assessment_overview()# Stats per attack method- for attack_method, test_cases in attack_method_to_cases.items():- passing = sum(- 1 for tc in test_cases if tc.score is not None and tc.score > 0- )- errored = sum(1 for tc in test_cases if tc.error is not None)- failing = len(test_cases) - passing - errored- valid_cases = len(test_cases) - errored- pass_rate = (passing / valid_cases) if valid_cases > 0 else 0.0-- attack_method_results.append(- AttackMethodResult(- attack_method=attack_method,- pass_rate=pass_rate,- passing=passing,- failing=failing,- errored=errored,+ if attack_method_to_cases is not None:+ for attack_method, test_cases in attack_method_to_cases.items():+ passing = sum(+ 1 for tc in test_cases if tc.score is not None and tc.score > 0+ )+ errored = sum(1 for tc in test_cases if tc.error is not None)+ failing = len(test_cases) - passing - errored+ valid_cases = len(test_cases) - errored+ pass_rate = (passing / valid_cases) if valid_cases > 0 else 0.0++ attack_method_results.append(+ AttackMethodResult(+ attack_method=attack_method,+ pass_rate=pass_rate,+ passing=passing,+ failing=failing,+ errored=errored,+ ))- )return RedTeamingOverview(vulnerability_type_results=vulnerability_type_results,