Skip to content

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

rich.progress utils added

A-Vamshiaebe0dfdf586
diff --git a/deepteam/utils.py b/deepteam/utils.py
index bbe15fa4..d3b27037 100644
--- a/deepteam/utils.py
+++ b/deepteam/utils.py
@@ -1,5 +1,13 @@
+import time
import inspect
-from typing import Callable, List
+from typing import Optional, List, Iterable, AsyncIterable, Any, Callable
+from rich.progress import (
+ Progress,
+ SpinnerColumn,
+ TextColumn,
+ BarColumn,
+ TimeElapsedColumn,
+)
from deepteam.test_case import RTTurn
@@ -28,3 +36,62 @@ def format_turns(turns: List[RTTurn]):
formatted_turns += "End of conversation. \n"
return formatted_turns
+
+def create_progress() -> Progress:
+ return Progress(
+ SpinnerColumn(),
+ TextColumn("[progress.description]{task.description}"),
+ BarColumn(),
+ TimeElapsedColumn(),
+ )
+
+def add_pbar(progress: Optional[Progress], description: str, total: Optional[int] = None) -> Optional[int]:
+ if progress is None:
+ return None
+ return progress.add_task(description, total=total)
+
+def update_pbar(
+ progress: Optional[Progress],
+ pbar_id: Optional[int],
+ advance: int = 1,
+ advance_to_end: bool = False,
+ remove: bool = True,
+ total: Optional[int] = None,
+):
+ if progress is None or pbar_id is None:
+ return
+ task = next((t for t in progress.tasks if t.id == pbar_id), None)
+ if task is None:
+ return
+ if advance_to_end:
+ advance = task.remaining
+ progress.update(pbar_id, advance=advance, total=total)
+ task = next((t for t in progress.tasks if t.id == pbar_id), None)
+ if task is not None and task.finished and remove:
+ progress.remove_task(pbar_id)
+
+def remove_pbars(progress: Optional[Progress], pbar_ids: List[int], cascade: bool = True):
+ if progress is None:
+ return
+ for pbar_id in pbar_ids:
+ if cascade:
+ time.sleep(0.1)
+ task = next((t for t in progress.tasks if t.id == pbar_id), None)
+ if task is not None:
+ progress.remove_task(pbar_id)
+
+def rich_track(iterable: Iterable[Any], progress: Optional[Progress], description: str) -> Iterable[Any]:
+ if progress is None:
+ for item in iterable:
+ yield item
+ return
+ try:
+ total = len(iterable)
+ except Exception:
+ total = None
+ task_id = add_pbar(progress, description, total)
+ for item in iterable:
+ yield item
+ update_pbar(progress, task_id)
+ update_pbar(progress, task_id, advance_to_end=True)
+