Skip to content

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

attack simulator and red teamer updated to use rich.progress

A-Vamshi13c1532675f0
diff --git a/deepteam/attacks/attack_simulator/attack_simulator.py b/deepteam/attacks/attack_simulator/attack_simulator.py
index d62142a8..a981ba43 100644
--- a/deepteam/attacks/attack_simulator/attack_simulator.py
+++ b/deepteam/attacks/attack_simulator/attack_simulator.py
@@ -1,6 +1,5 @@
import random
import asyncio
-from tqdm import tqdm
from pydantic import BaseModel
from typing import List, Optional, Union
import inspect
@@ -10,6 +9,7 @@ from deepeval.metrics.utils import initialize_model
from deepeval.metrics.utils import initialize_model
from deepteam.attacks import BaseAttack
+from deepteam.utils import create_progress, rich_track, add_pbar, update_pbar
from deepteam.vulnerabilities import BaseVulnerability
from deepteam.attacks.multi_turn.types import CallbackType
from deepteam.errors import ModelRefusalError
@@ -60,30 +60,33 @@ class AttackSimulator:
num_vulnerability_types = sum(
len(v.get_types()) for v in vulnerabilities
)
- pbar = tqdm(
- vulnerabilities,
- desc=f"πŸ’₯ Generating {num_vulnerability_types * attacks_per_vulnerability_type} attacks (for {num_vulnerability_types} vulnerability types across {len(vulnerabilities)} vulnerability(s))",
- )
- for vulnerability in pbar:
- if simulator_model is not None:
- vulnerability.simulator_model = simulator_model
- test_cases.extend(
- self.simulate_baseline_attacks(
- attacks_per_vulnerability_type=attacks_per_vulnerability_type,
- vulnerability=vulnerability,
- ignore_errors=ignore_errors,
- metadata=metadata,
+
+ progress = create_progress()
+ with progress:
+ for vulnerability in rich_track(
+ iterable=vulnerabilities,
+ progress=progress,
+ description=f"πŸ’₯ Generating {num_vulnerability_types * attacks_per_vulnerability_type} attacks (for {num_vulnerability_types} vulnerability types across {len(vulnerabilities)} vulnerability(s))"
+ ):
+ if simulator_model is not None:
+ vulnerability.simulator_model = simulator_model
+ test_cases.extend(
+ self.simulate_baseline_attacks(
+ attacks_per_vulnerability_type=attacks_per_vulnerability_type,
+ vulnerability=vulnerability,
+ ignore_errors=ignore_errors,
+ metadata=metadata,
+ )
)
- )
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))",
- )
attack_weights = [attack.weight for attack in attacks]
- for test_case in pbar:
+ for test_case in rich_track(
+ iterable=test_cases,
+ progress=progress,
+ description=f"✨ Simulating {num_vulnerability_types * attacks_per_vulnerability_type} attacks (using {len(attacks)} method(s))",
+ ):
# Randomly sample an enhancement based on the distribution
sampled_attack = random.choices(
attacks, weights=attack_weights, k=1
@@ -114,70 +117,74 @@ class AttackSimulator:
num_vulnerability_types = sum(
len(v.get_types()) for v in vulnerabilities
)
- pbar = tqdm(
- vulnerabilities,
- desc=f"πŸ’₯ Generating {num_vulnerability_types * attacks_per_vulnerability_type} attacks (for {num_vulnerability_types} vulnerability types across {len(vulnerabilities)} vulnerability(s))",
- )
-
- async def throttled_simulate_baseline_attack(vulnerability):
- if simulator_model is not None:
- vulnerability.simulator_model = simulator_model
- async with semaphore: # Throttling applied here
- result = await self.a_simulate_baseline_attacks(
- attacks_per_vulnerability_type=attacks_per_vulnerability_type,
- vulnerability=vulnerability,
- ignore_errors=ignore_errors,
- metadata=metadata,
- )
- pbar.update(1)
- return result
-
- simulate_tasks = [
- asyncio.create_task(
- throttled_simulate_baseline_attack(vulnerability)
- )
- for vulnerability in vulnerabilities
- ]
+ progress = create_progress()
+ with progress:
- attack_results = await asyncio.gather(*simulate_tasks)
- for result in attack_results:
- test_cases.extend(result)
- pbar.close()
-
- 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))",
+ task_id_vuln = add_pbar(
+ progress,
+ description=f"πŸ’₯ Generating {num_vulnerability_types * attacks_per_vulnerability_type} attacks (for {num_vulnerability_types} vulnerability types across {len(vulnerabilities)} vulnerability(s))",
+ total=len(vulnerabilities)
)
-
- async def throttled_attack_method(
- test_case: RTTestCase,
- ):
+
+ async def throttled_simulate_baseline_attack(vulnerability):
+ if simulator_model is not None:
+ vulnerability.simulator_model = simulator_model
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=test_case,
+ result = await self.a_simulate_baseline_attacks(
+ attacks_per_vulnerability_type=attacks_per_vulnerability_type,
+ vulnerability=vulnerability,
ignore_errors=ignore_errors,
+ metadata=metadata,
)
- pbar.update(1)
+ update_pbar(progress, task_id_vuln)
+ return result
- await asyncio.gather(
- *[
- asyncio.create_task(throttled_attack_method(test_case))
- for test_case in test_cases
- ]
- )
- pbar.close()
+ simulate_tasks = [
+ asyncio.create_task(
+ throttled_simulate_baseline_attack(vulnerability)
+ )
+ for vulnerability in vulnerabilities
+ ]
+
+ attack_results = await asyncio.gather(*simulate_tasks)
+ for result in attack_results:
+ test_cases.extend(result)
+
+ if attacks is not None:
+ # Enhance attacks by sampling from the provided distribution
+ task_id_attack = add_pbar(
+ progress,
+ description=f"✨ Simulating {num_vulnerability_types * attacks_per_vulnerability_type} attacks (using {len(attacks)} method(s))",
+ total=len(test_cases)
+ )
+
+
+ async def throttled_attack_method(
+ test_case: 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=test_case,
+ ignore_errors=ignore_errors,
+ )
+ update_pbar(progress, task_id_attack)
+
+ await asyncio.gather(
+ *[
+ asyncio.create_task(throttled_attack_method(test_case))
+ for test_case in test_cases
+ ]
+ )
self.test_cases.extend(test_cases)
return test_cases
diff --git a/deepteam/red_teamer/red_teamer.py b/deepteam/red_teamer/red_teamer.py
index 89a0cf6c..12ea5158 100644
--- a/deepteam/red_teamer/red_teamer.py
+++ b/deepteam/red_teamer/red_teamer.py
@@ -1,5 +1,4 @@
import asyncio
-from tqdm import tqdm
from typing import Dict, List, Optional, Union
from rich.console import Console
from rich.table import Table
@@ -19,7 +18,7 @@ from deepteam.frameworks.frameworks import AISafetyFramework
from deepteam.frameworks.risk_category import RiskCategory
from deepteam.telemetry import capture_red_teamer_run
from deepteam.attacks import BaseAttack
-from deepteam.utils import validate_model_callback_signature
+from deepteam.utils import validate_model_callback_signature, create_progress, rich_track, add_pbar, update_pbar
from deepteam.vulnerabilities import BaseVulnerability
from deepteam.vulnerabilities.types import VulnerabilityType
from deepteam.attacks.attack_simulator import AttackSimulator
@@ -85,19 +84,21 @@ class RedTeamer:
reuse_simulated_test_cases=reuse_simulated_test_cases,
metadata=metadata,
attacks_per_vulnerability_type=attacks_per_vulnerability_type,
+ _print_assessment=False,
)
results = {}
-
- pbar = tqdm(
- total=len(framework.risk_categories),
- desc=f"⏳ Running red-teaming for {framework.get_name()} Framework",
- )
- for risk_category in framework.risk_categories:
- framework_assessment = assess_risk_category(risk_category)
- results[risk_category.name] = framework_assessment
- pbar.update(1)
- pbar.close()
+ progress = create_progress()
+ with progress:
+ task_id = add_pbar(
+ progress,
+ description=f"⏳ Running red-teaming for {framework.get_name()} Framework",
+ total=len(framework.risk_categories),
+ )
+ for risk_category in framework.risk_categories:
+ framework_assessment = assess_risk_category(risk_category)
+ results[risk_category.name] = framework_assessment
+ update_pbar(progress, task_id)
return results
@@ -128,6 +129,7 @@ class RedTeamer:
reuse_simulated_test_cases=reuse_simulated_test_cases,
metadata=metadata,
attacks_per_vulnerability_type=attacks_per_vulnerability_type,
+ _print_assessment=False,
)
tasks = {
@@ -137,18 +139,19 @@ class RedTeamer:
results = {}
- pbar = tqdm(
- total=len(tasks),
- desc=f"⏳ Running red-teaming for {framework.get_name()} Framework",
- )
-
- for task in asyncio.as_completed(tasks):
- result = await task
- name = tasks[task]
- results[name] = result
- pbar.update(1)
+ progress = create_progress()
+ with progress:
+ task_id = add_pbar(
+ progress,
+ description=f"⏳ Running red-teaming for {framework.get_name()} Framework",
+ total=len(framework.risk_categories),
+ )
+ for task in asyncio.as_completed(tasks):
+ result = await task
+ name = tasks[task]
+ results[name] = result
+ update_pbar(progress, task_id)
- pbar.close()
return results
def red_team(
@@ -163,6 +166,7 @@ class RedTeamer:
ignore_errors: bool = False,
reuse_simulated_test_cases: bool = False,
metadata: Optional[dict] = None,
+ _print_assessment: Optional[bool] = True,
):
if not framework and not vulnerabilities:
raise ValueError(
@@ -208,21 +212,20 @@ class RedTeamer:
ignore_errors=ignore_errors,
reuse_simulated_test_cases=reuse_simulated_test_cases,
metadata=metadata,
+ _print_assessment=_print_assessment
)
)
else:
- if framework:
- if framework._has_dataset:
- pbar = tqdm(
- range(framework.num_attacks),
- desc=f"πŸ’₯ Fetching {framework.num_attacks} attacks from {framework.get_name()} Dataset",
+ if framework and framework._has_dataset:
+ progress = create_progress()
+ with progress:
+ task_id = add_pbar(
+ progress,
+ description=f"πŸ’₯ Fetching {framework.num_attacks} attacks from {framework.get_name()} Dataset",
+ total=framework.num_attacks,
)
framework.load_dataset()
- pbar.update(framework.num_attacks)
- pbar.close()
- else:
- attacks = framework.attacks
- vulnerabilities = framework.vulnerabilities
+ update_pbar(progress, task_id, advance_to_end=True)
assert not inspect.iscoroutinefunction(
model_callback
@@ -289,14 +292,16 @@ class RedTeamer:
)
if framework and framework._has_dataset:
- pbar = tqdm(
- total=len(simulated_test_cases),
- desc=f"πŸ“ Evaluating {len(simulated_test_cases)} test cases using {framework.get_name()} risk categories",
- )
- red_teaming_test_cases = framework.assess(
- model_callback, pbar, ignore_errors
- )
- pbar.close()
+ progress = create_progress()
+ with progress:
+ task_id = add_pbar(
+ progress,
+ description=f"πŸ“ Evaluating {len(simulated_test_cases)} test cases using {framework.get_name()} risk categories",
+ total=len(simulated_test_cases),
+ )
+ red_teaming_test_cases = framework.assess(
+ model_callback, progress, task_id, ignore_errors
+ )
else:
total_attacks = sum(
len(test_cases)
@@ -305,29 +310,30 @@ class RedTeamer:
num_vulnerability_types = sum(
len(v.get_types()) for v in vulnerabilities
)
- pbar = tqdm(
- total=total_attacks,
- desc=f"πŸ“ Evaluating {num_vulnerability_types} vulnerability types across {len(vulnerabilities)} vulnerability(s)",
- )
+ progress = create_progress()
+ with progress:
+ task_id = add_pbar(
+ progress,
+ description=f"πŸ“ Evaluating {num_vulnerability_types} vulnerability types across {len(vulnerabilities)} vulnerability(s)",
+ total=total_attacks,
+ )
- red_teaming_test_cases: List[RTTestCase] = []
+ red_teaming_test_cases: List[RTTestCase] = []
- for (
- vulnerability_type,
- test_cases,
- ) in vulnerability_type_to_attacks_map.items():
- rt_test_cases = self._evaluate_vulnerability_type(
- model_callback,
- vulnerabilities,
+ for (
vulnerability_type,
test_cases,
- ignore_errors=ignore_errors,
- )
- red_teaming_test_cases.extend(rt_test_cases)
-
- pbar.update(len(test_cases))
+ ) in vulnerability_type_to_attacks_map.items():
+ rt_test_cases = self._evaluate_vulnerability_type(
+ model_callback,
+ vulnerabilities,
+ vulnerability_type,
+ test_cases,
+ ignore_errors=ignore_errors,
+ )
+ red_teaming_test_cases.extend(rt_test_cases)
- pbar.close()
+ update_pbar(progress, task_id, advance=len(test_cases))
self.risk_assessment = RiskAssessment(
overview=construct_risk_assessment_overview(
@@ -336,7 +342,9 @@ class RedTeamer:
test_cases=red_teaming_test_cases,
)
self.test_cases = red_teaming_test_cases
- self._print_risk_assessment()
+
+ if _print_assessment:
+ self._print_risk_assessment(self.risk_assessment)
return self.risk_assessment
@@ -352,6 +360,7 @@ class RedTeamer:
ignore_errors: bool = False,
reuse_simulated_test_cases: bool = False,
metadata: Optional[dict] = None,
+ _print_assessment: Optional[bool] = True,
):
if not framework and not vulnerabilities:
raise ValueError(
@@ -381,16 +390,15 @@ class RedTeamer:
if framework:
if framework._has_dataset:
- pbar = tqdm(
- range(framework.num_attacks),
- desc=f"πŸ’₯ Fetching {framework.num_attacks} attacks from {framework.get_name()} Dataset",
- )
- framework.load_dataset()
- pbar.update(framework.num_attacks)
- pbar.close()
- else:
- attacks = framework.attacks
- vulnerabilities = framework.vulnerabilities
+ progress = create_progress()
+ with progress:
+ task_id = add_pbar(
+ progress,
+ description=f"πŸ’₯ Fetching {framework.num_attacks} attacks from {framework.get_name()} Dataset",
+ total=framework.num_attacks,
+ )
+ framework.load_dataset()
+ update_pbar(progress, task_id, advance_to_end=True)
if evaluation_model is not None:
self.evaluation_model = evaluation_model
@@ -454,14 +462,16 @@ class RedTeamer:
)
if framework and framework._has_dataset:
- pbar = tqdm(
- total=len(simulated_test_cases),
- desc=f"πŸ“ Evaluating {len(simulated_test_cases)} test cases using {framework.get_name()} risk categories",
- )
- red_teaming_test_cases = await framework.a_assess(
- model_callback, pbar, ignore_errors
- )
- pbar.close()
+ progress = create_progress()
+ with progress:
+ task_id = add_pbar(
+ progress,
+ description=f"πŸ“ Evaluating {len(simulated_test_cases)} test cases using {framework.get_name()} risk categories",
+ total=len(simulated_test_cases),
+ )
+ red_teaming_test_cases = await framework.a_assess(
+ model_callback, progress, task_id, ignore_errors
+ )
else:
semaphore = asyncio.Semaphore(self.max_concurrent)
total_attacks = sum(
@@ -471,36 +481,38 @@ class RedTeamer:
num_vulnerability_types = sum(
len(v.get_types()) for v in vulnerabilities
)
- pbar = tqdm(
- total=total_attacks,
- desc=f"πŸ“ Evaluating {num_vulnerability_types} vulnerability types across {len(vulnerabilities)} vulnerability(s)",
- )
-
- red_teaming_test_cases: List[RTTestCase] = []
+ progress = create_progress()
+ with progress:
+ task_id = add_pbar(
+ progress,
+ description=f"πŸ“ Evaluating {num_vulnerability_types} vulnerability types across {len(vulnerabilities)} vulnerability(s)",
+ total=total_attacks,
+ )
- async def throttled_evaluate_vulnerability_type(
- vulnerability_type, attacks
- ):
- async with semaphore:
- test_cases = await self._a_evaluate_vulnerability_type(
- model_callback,
- vulnerabilities,
- vulnerability_type,
- attacks,
- ignore_errors=ignore_errors,
- )
- red_teaming_test_cases.extend(test_cases)
- pbar.update(len(attacks))
+ red_teaming_test_cases: List[RTTestCase] = []
- # Create a list of tasks for evaluating each vulnerability, with throttling
- tasks = [
- throttled_evaluate_vulnerability_type(
+ async def throttled_evaluate_vulnerability_type(
vulnerability_type, attacks
- )
- for vulnerability_type, attacks in vulnerability_type_to_attacks_map.items()
- ]
- await asyncio.gather(*tasks)
- pbar.close()
+ ):
+ async with semaphore:
+ test_cases = await self._a_evaluate_vulnerability_type(
+ model_callback,
+ vulnerabilities,
+ vulnerability_type,
+ attacks,
+ ignore_errors=ignore_errors,
+ )
+ red_teaming_test_cases.extend(test_cases)
+ update_pbar(progress, task_id, advance=len(attacks))
+
+ # Create a list of tasks for evaluating each vulnerability, with throttling
+ tasks = [
+ throttled_evaluate_vulnerability_type(
+ vulnerability_type, attacks
+ )
+ for vulnerability_type, attacks in vulnerability_type_to_attacks_map.items()
+ ]
+ await asyncio.gather(*tasks)
self.risk_assessment = RiskAssessment(
overview=construct_risk_assessment_overview(
@@ -509,7 +521,10 @@ class RedTeamer:
test_cases=red_teaming_test_cases,
)
self.test_cases = red_teaming_test_cases
- self._print_risk_assessment()
+
+ if _print_assessment:
+ self._print_risk_assessment(self.risk_assessment)
+
return self.risk_assessment
def _attack(
@@ -711,8 +726,8 @@ class RedTeamer:
)
return red_teaming_test_cases
- def _print_risk_assessment(self):
- if self.risk_assessment is None:
+ def _print_risk_assessment(self, risk_assessment=None):
+ if risk_assessment is None:
return
console = Console()
@@ -746,7 +761,7 @@ class RedTeamer:
table.add_column("Status", justify="center", width=10)
# Add rows
- for case in self.risk_assessment.test_cases:
+ for case in risk_assessment.test_cases:
status = (
"Passed"
if case.score and case.score > 0
@@ -789,13 +804,13 @@ class RedTeamer:
console.print("\n" + "=" * 80)
console.print(
- f"[bold magenta]πŸ” DeepTeam Risk Assessment[/bold magenta] ({self.risk_assessment.overview.errored} errored)"
+ f"[bold magenta]πŸ” DeepTeam Risk Assessment[/bold magenta] ({risk_assessment.overview.errored} errored)"
)
console.print("=" * 80)
# Sort vulnerability type results by pass rate in descending order
sorted_vulnerability_results = sorted(
- self.risk_assessment.overview.vulnerability_type_results,
+ risk_assessment.overview.vulnerability_type_results,
key=lambda x: x.pass_rate,
reverse=True,
)
@@ -821,7 +836,7 @@ class RedTeamer:
# Sort attack method results by pass rate in descending order
sorted_attack_method_results = sorted(
- self.risk_assessment.overview.attack_method_results,
+ risk_assessment.overview.attack_method_results,
key=lambda x: x.pass_rate,
reverse=True,
)
@@ -851,6 +866,11 @@ class RedTeamer:
console.print("=" * 80 + "\n")
def _print_framework_overview_table(self, framework_results: dict):
+
+ for category_name in sorted(framework_results.keys()):
+ assessment = framework_results[category_name]
+ self._print_risk_assessment(assessment)
+
console = Console()
console.print("\n" + "=" * 80)