DeepTeam is a framework to red team LLMs and AI agents.
.
A-Vamshif11104a40680
diff --git a/deepteam/batcher/__init__.py b/deepteam/batcher/__init__.pydeleted file mode 100644index 94fae473..00000000--- a/deepteam/batcher/__init__.py+++ /dev/null@@ -1 +0,0 @@-from .batcher import TraceBatchEvaluator\ No newline at end of filediff --git a/deepteam/trace_scanner/__init__.py b/deepteam/trace_scanner/__init__.pynew file mode 100644index 00000000..1a7c4885--- /dev/null+++ b/deepteam/trace_scanner/__init__.py@@ -0,0 +1 @@+from .trace_scanner import TraceScanner\ No newline at end of filediff --git a/deepteam/batcher/schema.py b/deepteam/trace_scanner/schema.pysimilarity index 95%rename from deepteam/batcher/schema.pyrename to deepteam/trace_scanner/schema.pyindex 00e995c5..662b4926 100644--- a/deepteam/batcher/schema.py+++ b/deepteam/trace_scanner/schema.py@@ -26,7 +26,7 @@ class BatchFinding(BaseModel):class BatchFindingsList(BaseModel):findings: List[BatchFinding]-class ExtractedSpan(BaseModel):+class SpanNode(BaseModel):"""Strictly types the extracted span data before it goes into the LLM batch."""spanUuid: strparentUuid: Optional[str] = Nonediff --git a/deepteam/batcher/batcher.py b/deepteam/trace_scanner/trace_scanner.pysimilarity index 80%rename from deepteam/batcher/batcher.pyrename to deepteam/trace_scanner/trace_scanner.pyindex 5abf2232..9d5d10cc 100644--- a/deepteam/batcher/batcher.py+++ b/deepteam/trace_scanner/trace_scanner.py@@ -4,11 +4,11 @@ from typing import Dict, List, Anyfrom deepeval.tracing.types import AgentSpan, LlmSpan, RetrieverSpan, ToolSpan, Trace, BaseSpanfrom deepeval.models import DeepEvalBaseLLM-from .schema import BatchFinding, BatchFindingsList, ExtractedSpan+from .schema import BatchFinding, BatchFindingsList, SpanNodefrom deepteam.utils import SPANS_CONTEXT_LIMITfrom deepteam.attacks.attack_simulator.utils import generate, a_generate-class TraceBatchEvaluator:+class TraceScanner:def __init__(self,model: DeepEvalBaseLLM,@@ -22,9 +22,9 @@ class TraceBatchEvaluator:self.limit = limit# Internal State- self.current_batch: List[ExtractedSpan] = []- self.current_batch_size: int = 0- self.all_findings: Dict[str, List[BatchFinding]] = {}+ self._current_batch: List[SpanNode] = []+ self._current_batch_size: int = 0+ self._all_findings: Dict[str, List[BatchFinding]] = {}def process_trace(self, trace: Trace) -> Dict[str, List[BatchFinding]]:self._reset_state()@@ -35,10 +35,10 @@ class TraceBatchEvaluator:trace_node = self._extract_trace_root(trace)self._add_to_batch_and_check(trace_node)- if self.current_batch:+ if self._current_batch:self._flush_batch()- return self.all_findings+ return self._all_findingsasync def a_process_trace(self, trace: Trace) -> Dict[str, List[BatchFinding]]:self._reset_state()@@ -49,15 +49,15 @@ class TraceBatchEvaluator:trace_node = self._extract_trace_root(trace)await self._a_add_to_batch_and_check(trace_node)- if self.current_batch:+ if self._current_batch:await self._a_flush_batch()- return self.all_findings+ return self._all_findingsdef _reset_state(self):- self.current_batch = []- self.current_batch_size = 0- self.all_findings = {}+ self._current_batch = []+ self._current_batch_size = 0+ self._all_findings = {}# ---------------------------------------------------------# SYNCHRONOUS TRAVERSAL@@ -72,33 +72,33 @@ class TraceBatchEvaluator:self._add_to_batch_and_check(span_node)- def _add_to_batch_and_check(self, node: ExtractedSpan):+ def _add_to_batch_and_check(self, node: SpanNode):# Dump to dict strictly excluding None to save tokensnode_dict = node.model_dump(exclude_none=True)node_str = json.dumps(node_dict)node_size = len(node_str)- if self.current_batch_size + node_size > self.limit and self.current_batch:+ if self._current_batch_size + node_size > self.limit and self._current_batch:self._flush_batch()- self.current_batch.append(node)- self.current_batch_size += node_size+ self._current_batch.append(node)+ self._current_batch_size += node_size- if self.current_batch_size >= self.limit:+ if self._current_batch_size >= self.limit:self._flush_batch()def _flush_batch(self):- if not self.current_batch:+ if not self._current_batch:return- batch_list = [node.model_dump(exclude_none=True) for node in self.current_batch]+ batch_list = [node.model_dump(exclude_none=True) for node in self._current_batch]batch_string = json.dumps(batch_list, indent=2)prompt = self.template.generate_trace_batch_evaluation(batch_data=batch_string)res: BatchFindingsList = generate(prompt, BatchFindingsList, self.model)self._store_findings(res.findings)- self.current_batch = []- self.current_batch_size = 0+ self._current_batch = []+ self._current_batch_size = 0# ---------------------------------------------------------# ASYNCHRONOUS TRAVERSAL@@ -113,32 +113,32 @@ class TraceBatchEvaluator:await self._a_add_to_batch_and_check(span_node)- async def _a_add_to_batch_and_check(self, node: ExtractedSpan):+ async def _a_add_to_batch_and_check(self, node: SpanNode):node_dict = node.model_dump(exclude_none=True)node_str = json.dumps(node_dict)node_size = len(node_str)- if self.current_batch_size + node_size > self.limit and self.current_batch:+ if self._current_batch_size + node_size > self.limit and self._current_batch:await self._a_flush_batch()- self.current_batch.append(node)- self.current_batch_size += node_size+ self._current_batch.append(node)+ self._current_batch_size += node_size- if self.current_batch_size >= self.limit:+ if self._current_batch_size >= self.limit:await self._a_flush_batch()async def _a_flush_batch(self):- if not self.current_batch:+ if not self._current_batch:return- batch_list = [node.model_dump(exclude_none=True) for node in self.current_batch]+ batch_list = [node.model_dump(exclude_none=True) for node in self._current_batch]batch_string = json.dumps(batch_list, indent=2)prompt = self.template.generate_trace_batch_evaluation(batch_data=batch_string)res: BatchFindingsList = await a_generate(prompt, BatchFindingsList, self.model)self._store_findings(res.findings)- self.current_batch = []- self.current_batch_size = 0+ self._current_batch = []+ self._current_batch_size = 0# ---------------------------------------------------------# UTILITIES & EXTRACTION@@ -146,24 +146,24 @@ class TraceBatchEvaluator:def _store_findings(self, findings: List[BatchFinding]):for finding in findings:- if finding.spanUuid not in self.all_findings:- self.all_findings[finding.spanUuid] = []+ if finding.spanUuid not in self._all_findings:+ self._all_findings[finding.spanUuid] = []# Filter out any older findings for this exact vulnerability type# because the newer finding from a higher-level batch is the final authority.- self.all_findings[finding.spanUuid] = [- f for f in self.all_findings[finding.spanUuid]+ self._all_findings[finding.spanUuid] = [+ f for f in self._all_findings[finding.spanUuid]if not (f.vulnerability == finding.vulnerability andf.vulnerabilityType == finding.vulnerabilityType)]- self.all_findings[finding.spanUuid].append(finding)+ self._all_findings[finding.spanUuid].append(finding)def _get_child_findings(self, children: List[BaseSpan]) -> List[BatchFinding]:findings = []for child in children:- if child.uuid in self.all_findings:- findings.extend(self.all_findings[child.uuid])+ if child.uuid in self._all_findings:+ findings.extend(self._all_findings[child.uuid])return findingsdef _collapse_io(self, parent_input: Any, parent_output: Any, children: List[BaseSpan]) -> tuple[Any, Any]:@@ -186,12 +186,12 @@ class TraceBatchEvaluator:return collapsed_input, collapsed_output- def _extract_span_with_findings(self, span: BaseSpan, child_findings: List[BatchFinding]) -> ExtractedSpan:+ def _extract_span_with_findings(self, span: BaseSpan, child_findings: List[BatchFinding]) -> SpanNode:"""Strips useless metadata, keeps I/O, tools, and attaches child findings."""c_input, c_output = self._collapse_io(span.input, span.output, span.children)- extracted = ExtractedSpan(+ extracted = SpanNode(spanUuid=span.uuid,parentUuid=span.parent_uuid,type=span.__class__.__name__,@@ -223,13 +223,13 @@ class TraceBatchEvaluator:return extracted- def _extract_trace_root(self, trace: Trace) -> ExtractedSpan:+ def _extract_trace_root(self, trace: Trace) -> SpanNode:"""Extracts the top-level trace I/O and any root-level findings."""root_findings = self._get_child_findings(trace.root_spans)c_input, c_output = self._collapse_io(trace.input, trace.output, trace.root_spans)- return ExtractedSpan(+ return SpanNode(spanUuid=trace.uuid,parentUuid=None,type="TraceRoot",diff --git a/deepteam/vulnerabilities/bias/bias.py b/deepteam/vulnerabilities/bias/bias.pyindex 7052b76a..7b39aeda 100644--- a/deepteam/vulnerabilities/bias/bias.py+++ b/deepteam/vulnerabilities/bias/bias.py@@ -18,8 +18,8 @@ from deepteam.attacks.attack_engine import AttackEnginefrom deepteam.test_case import RTTestCasefrom deepteam.attacks.attack_simulator.schema import SyntheticDataListfrom deepteam.risks import getRiskCategory-from deepteam.batcher.schema import BatchFinding-from deepteam.batcher import TraceBatchEvaluator+from deepteam.trace_scanner.schema import BatchFinding+from deepteam.trace_scanner import TraceScannerfrom .template import BiasTemplateBiasLiteralType = Literal["religion", "politics", "gender", "race"]@@ -305,13 +305,13 @@ class Bias(BaseVulnerability):self.evaluation_model, self.using_native_model = initialize_model(self.evaluation_model)- batcher = TraceBatchEvaluator(+ trace_scanner = TraceScanner(model=self.evaluation_model,using_native_model=self.using_native_model,template=BiasTemplate,)- findings = batcher.process_trace(trace)+ findings = trace_scanner.process_trace(trace)self.trace_findings = findings@@ -331,13 +331,13 @@ class Bias(BaseVulnerability):self.evaluation_model)- batcher = TraceBatchEvaluator(+ trace_scanner = TraceScanner(model=self.evaluation_model,using_native_model=self.using_native_model,template=BiasTemplate,)- findings = await batcher.a_process_trace(trace)+ findings = await trace_scanner.a_process_trace(trace)self.trace_findings = findingsdiff --git a/tests/test_core/test_batcher/test_batcher.py b/tests/test_core/test_batcher/test_batcher.pyindex 28a264fb..5dc5d2b7 100644--- a/tests/test_core/test_batcher/test_batcher.py+++ b/tests/test_core/test_batcher/test_batcher.py@@ -7,7 +7,7 @@ import pytestfrom deepeval.metrics.utils import initialize_modelfrom deepeval.tracing.types import AgentSpan, LlmSpan, Trace, TraceSpanStatus-from deepteam.batcher.batcher import TraceBatchEvaluator+from deepteam.trace_scanner.trace_scanner import TraceScannerfrom deepteam.vulnerabilities.bias.bias import Biasfrom deepteam.vulnerabilities.bias.template import BiasTemplate@@ -37,8 +37,8 @@ def bias_vulnerability(evaluation_model):def batch_evaluator_factory(evaluation_model):model, using_native = evaluation_model- def _factory(limit: int) -> TraceBatchEvaluator:- return TraceBatchEvaluator(+ def _factory(limit: int) -> TraceScanner:+ return TraceScanner(model=model,using_native_model=using_native,template=BiasTemplate,@@ -49,7 +49,7 @@ def batch_evaluator_factory(evaluation_model):-class TestTraceBatchEvaluator:+class TestTraceScanner:def test_sync_unmitigated_bias_finding(self, bias_vulnerability):trace = load_trace_fixture("unsafe_bias_trace")