Skip to content

Commit bd6d2f3

Browse files
committed
feat(add state module):
1 parent dda8d13 commit bd6d2f3

File tree

2 files changed

+48
-47
lines changed

2 files changed

+48
-47
lines changed

agentic_security/probe_actor/fuzzer.py

Lines changed: 1 addition & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
from typing import Any
77

88
import httpx
9-
import pandas as pd
109
from skopt import Optimizer
1110
from skopt.space import Real
1211

@@ -15,6 +14,7 @@
1514
from agentic_security.primitives import Scan, ScanResult
1615
from agentic_security.probe_actor.cost_module import calculate_cost
1716
from agentic_security.probe_actor.refusal import refusal_heuristic
17+
from agentic_security.probe_actor.state import FuzzerState
1818
from agentic_security.probe_data import audio_generator, image_generator, msj_data
1919
from agentic_security.probe_data.data import prepare_prompts
2020

@@ -26,52 +26,6 @@
2626
FAILURE_RATE_THRESHOLD = 0.5
2727

2828

29-
class FuzzerState:
30-
"""Container for tracking scan results"""
31-
32-
def __init__(self):
33-
self.errors = []
34-
self.refusals = []
35-
self.outputs = []
36-
37-
def add_error(
38-
self,
39-
module_name: str,
40-
prompt: str,
41-
status_code: int | str,
42-
error_msg: str,
43-
):
44-
"""Add an error to the state"""
45-
self.errors.append((module_name, prompt, status_code, error_msg))
46-
47-
def add_refusal(
48-
self, module_name: str, prompt: str, status_code: int, response_text: str
49-
):
50-
"""Add a refusal to the state"""
51-
self.refusals.append((module_name, prompt, status_code, response_text))
52-
53-
def add_output(
54-
self, module_name: str, prompt: str, response_text: str, refused: bool
55-
):
56-
"""Add an output to the state"""
57-
self.outputs.append((module_name, prompt, response_text, refused))
58-
59-
def get_last_output(self, prompt: str) -> str | None:
60-
"""Get the last output for a given prompt"""
61-
for output in reversed(self.outputs):
62-
if output[1] == prompt:
63-
return output[2]
64-
return None
65-
66-
def export_failures(self, filename: str = "failures.csv"):
67-
"""Export failures to a CSV file"""
68-
failure_data = self.errors + self.refusals
69-
df = pd.DataFrame(
70-
failure_data, columns=["module", "prompt", "status_code", "content"]
71-
)
72-
df.to_csv(filename, index=False)
73-
74-
7529
async def generate_prompts(
7630
prompts: list[str] | AsyncGenerator,
7731
) -> AsyncGenerator[str, None]:
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
import pandas as pd
2+
3+
4+
class FuzzerState:
5+
"""Container for tracking scan results"""
6+
7+
def __init__(self):
8+
self.errors = []
9+
self.refusals = []
10+
self.outputs = []
11+
12+
def add_error(
13+
self,
14+
module_name: str,
15+
prompt: str,
16+
status_code: int | str,
17+
error_msg: str,
18+
):
19+
"""Add an error to the state"""
20+
self.errors.append((module_name, prompt, status_code, error_msg))
21+
22+
def add_refusal(
23+
self, module_name: str, prompt: str, status_code: int, response_text: str
24+
):
25+
"""Add a refusal to the state"""
26+
self.refusals.append((module_name, prompt, status_code, response_text))
27+
28+
def add_output(
29+
self, module_name: str, prompt: str, response_text: str, refused: bool
30+
):
31+
"""Add an output to the state"""
32+
self.outputs.append((module_name, prompt, response_text, refused))
33+
34+
def get_last_output(self, prompt: str) -> str | None:
35+
"""Get the last output for a given prompt"""
36+
for output in reversed(self.outputs):
37+
if output[1] == prompt:
38+
return output[2]
39+
return None
40+
41+
def export_failures(self, filename: str = "failures.csv"):
42+
"""Export failures to a CSV file"""
43+
failure_data = self.errors + self.refusals
44+
df = pd.DataFrame(
45+
failure_data, columns=["module", "prompt", "status_code", "content"]
46+
)
47+
df.to_csv(filename, index=False)

0 commit comments

Comments
 (0)