Skip to content

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

.

A-Vamshif11104a40680
diff --git a/deepteam/batcher/__init__.py b/deepteam/batcher/__init__.py
deleted file mode 100644
index 94fae473..00000000
--- a/deepteam/batcher/__init__.py
+++ /dev/null
@@ -1 +0,0 @@
-from .batcher import TraceBatchEvaluator
\ No newline at end of file
diff --git a/deepteam/trace_scanner/__init__.py b/deepteam/trace_scanner/__init__.py
new file mode 100644
index 00000000..1a7c4885
--- /dev/null
+++ b/deepteam/trace_scanner/__init__.py
@@ -0,0 +1 @@
+from .trace_scanner import TraceScanner
\ No newline at end of file
diff --git a/deepteam/batcher/schema.py b/deepteam/trace_scanner/schema.py
similarity index 95%
rename from deepteam/batcher/schema.py
rename to deepteam/trace_scanner/schema.py
index 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: str
parentUuid: Optional[str] = None
diff --git a/deepteam/batcher/batcher.py b/deepteam/trace_scanner/trace_scanner.py
similarity index 80%
rename from deepteam/batcher/batcher.py
rename to deepteam/trace_scanner/trace_scanner.py
index 5abf2232..9d5d10cc 100644
--- a/deepteam/batcher/batcher.py
+++ b/deepteam/trace_scanner/trace_scanner.py
@@ -4,11 +4,11 @@ from typing import Dict, List, Any
from deepeval.tracing.types import AgentSpan, LlmSpan, RetrieverSpan, ToolSpan, Trace, BaseSpan
from deepeval.models import DeepEvalBaseLLM
-from .schema import BatchFinding, BatchFindingsList, ExtractedSpan
+from .schema import BatchFinding, BatchFindingsList, SpanNode
from deepteam.utils import SPANS_CONTEXT_LIMIT
from 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_findings
async 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_findings
def _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 tokens
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:
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 and
f.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 findings
def _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.py
index 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 AttackEngine
from deepteam.test_case import RTTestCase
from deepteam.attacks.attack_simulator.schema import SyntheticDataList
from 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 TraceScanner
from .template import BiasTemplate
BiasLiteralType = 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 = findings
diff --git a/tests/test_core/test_batcher/test_batcher.py b/tests/test_core/test_batcher/test_batcher.py
index 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 pytest
from deepeval.metrics.utils import initialize_model
from deepeval.tracing.types import AgentSpan, LlmSpan, Trace, TraceSpanStatus
-from deepteam.batcher.batcher import TraceBatchEvaluator
+from deepteam.trace_scanner.trace_scanner import TraceScanner
from deepteam.vulnerabilities.bias.bias import Bias
from 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")