Skip to content

Commit 6d710da

Browse files
committed
Fix redteam report
1 parent f475f13 commit 6d710da

File tree

3 files changed

+89
-5
lines changed

3 files changed

+89
-5
lines changed

aisploit/redteam/__init__.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
11
from .job import RedTeamJob
2+
from .report import RedTeamReport, RedTeamReportEntry
23
from .task import RedTeamTask, RedTeamEndTokenTask, RedTeamClassifierTask
34

45
__all__ = [
56
"RedTeamJob",
7+
"RedTeamReport",
8+
"RedTeamReportEntry",
69
"RedTeamTask",
710
"RedTeamEndTokenTask",
811
"RedTeamClassifierTask",

aisploit/redteam/report.py

Lines changed: 41 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
from typing import List, Optional
1+
from typing import Optional
22
from dataclasses import dataclass
33
from ..core import BaseReport, BasePromptValue, Score
44

@@ -12,18 +12,54 @@ class RedTeamReportEntry:
1212

1313

1414
class RedTeamReport(BaseReport[RedTeamReportEntry]):
15+
"""
16+
A report class for storing red team evaluation entries.
17+
"""
18+
1519
def __init__(self, *, run_id: str) -> None:
20+
"""
21+
Initialize the RedTeamReport instance.
22+
23+
Args:
24+
run_id (str): The ID of the run.
25+
"""
1626
super().__init__(run_id=run_id)
1727

1828
def add_entry(self, entry: RedTeamReportEntry):
29+
"""
30+
Add an entry to the report.
31+
32+
Args:
33+
entry (RedTeamReportEntry): The entry to add to the report.
34+
"""
1935
self._entries.append(entry)
2036

2137
@property
2238
def final_score(self) -> Optional[Score]:
23-
last_entry = self._entries[-1]
24-
if last_entry:
25-
return last_entry.score
26-
return None
39+
"""
40+
Get the final score of the report.
41+
42+
Returns:
43+
Optional[Score]: The final score of the report, or None if no entries exist.
44+
"""
45+
if len(self._entries) == 0:
46+
return None
47+
return self._entries[-1].score
48+
49+
@property
50+
def final_response(self) -> Optional[str]:
51+
"""
52+
Get the final response of the report.
53+
54+
Returns:
55+
Optional[str]: The final response of the report, or None if no entries exist.
56+
"""
57+
if len(self._entries) == 0:
58+
return None
59+
return self._entries[-1].response
2760

2861
def _ipython_display_(self):
62+
"""
63+
Display the report in IPython environments.
64+
"""
2965
print("TODO")

tests/redteam/test_report.py

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
import pytest
2+
3+
from aisploit.core import BaseReport, Score
4+
from aisploit.redteam import RedTeamReport, RedTeamReportEntry
5+
6+
7+
@pytest.fixture
8+
def red_team_report():
9+
return RedTeamReport(run_id="test_run")
10+
11+
12+
@pytest.fixture
13+
def red_team_report_entry():
14+
return RedTeamReportEntry(
15+
attempt=1,
16+
prompt="Test prompt",
17+
response="Test response",
18+
score=Score(flagged=True, value=0.8),
19+
)
20+
21+
22+
def test_red_team_report_init(red_team_report):
23+
assert isinstance(red_team_report, BaseReport)
24+
assert red_team_report.run_id == "test_run"
25+
assert len(red_team_report._entries) == 0
26+
27+
28+
def test_red_team_report_add_entry(red_team_report, red_team_report_entry):
29+
red_team_report.add_entry(red_team_report_entry)
30+
assert len(red_team_report._entries) == 1
31+
assert red_team_report._entries[0] == red_team_report_entry
32+
33+
34+
def test_red_team_report_final_score(red_team_report, red_team_report_entry):
35+
assert red_team_report.final_score is None
36+
37+
red_team_report.add_entry(red_team_report_entry)
38+
assert red_team_report.final_score == red_team_report_entry.score
39+
40+
41+
def test_red_team_report_final_response(red_team_report, red_team_report_entry):
42+
assert red_team_report.final_response is None
43+
44+
red_team_report.add_entry(red_team_report_entry)
45+
assert red_team_report.final_response == red_team_report_entry.response

0 commit comments

Comments
 (0)