Skip to content

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

RTTestCase testing

A-Vamshica3e0190a667
diff --git a/deepteam/metrics/bias/bias.py b/deepteam/metrics/bias/bias.py
index 83dd2e45..6e621463 100644
--- a/deepteam/metrics/bias/bias.py
+++ b/deepteam/metrics/bias/bias.py
@@ -1,9 +1,6 @@
from typing import Optional, Tuple, Union
-from deepeval.test_case import (
- LLMTestCase,
- ConversationalTestCase,
-)
+from deepteam.test_case import RTTestCase
from deepeval.utils import get_or_create_event_loop
from deepeval.metrics.utils import (
construct_verbose_logs,
@@ -20,7 +17,7 @@ from deepteam.metrics.bias.template import (
BiasTemplate,
)
from deepteam.metrics.bias.schema import *
-
+from deepteam.utils import format_turns
class BiasMetric(BaseRedTeamingMetric):
def __init__(
@@ -41,11 +38,8 @@ class BiasMetric(BaseRedTeamingMetric):
self.system_prompt = ""
def measure(
- self, test_case: Union[LLMTestCase, ConversationalTestCase]
+ self, test_case: RTTestCase
) -> float:
- if isinstance(test_case, ConversationalTestCase):
- test_case = check_conversational_test_case_params(test_case, self)
- check_llm_test_case_params(test_case, self._required_params, self)
self.evaluation_cost = 0 if self.using_native_model else None
with metric_progress_indicator(self, _show_indicator=False):
@@ -67,12 +61,9 @@ class BiasMetric(BaseRedTeamingMetric):
async def a_measure(
self,
- test_case: Union[LLMTestCase, ConversationalTestCase],
+ test_case: RTTestCase,
_show_indicator: bool = False,
) -> float:
- if isinstance(test_case, ConversationalTestCase):
- test_case = check_conversational_test_case_params(test_case, self)
- check_llm_test_case_params(test_case, self._required_params, self)
self.evaluation_cost = 0 if self.using_native_model else None
with metric_progress_indicator(
@@ -125,10 +116,16 @@ class BiasMetric(BaseRedTeamingMetric):
return data["purpose"]
async def _a_evaluate(
- self, test_case: LLMTestCase
+ self, test_case: RTTestCase
) -> Tuple[Union[int, float], str]:
+
+ if test_case.turns is not None:
+ actual_output = format_turns(test_case.turns)
+ else:
+ actual_output = "AI'S ACTUAL OUTPUT TO EVALUATE: \n" + test_case.actual_output + "\n"
+
prompt = BiasTemplate.generate_evaluation_results(
- actual_output=test_case.actual_output,
+ actual_output=actual_output,
purpose=self.purpose,
)
if self.using_native_model:
@@ -146,9 +143,15 @@ class BiasMetric(BaseRedTeamingMetric):
data = trimAndLoadJson(res, self)
return data["score"], data["reason"]
- def evaluate(self, test_case: LLMTestCase) -> Tuple[Union[int, float], str]:
+ def evaluate(self, test_case: RTTestCase) -> Tuple[Union[int, float], str]:
+
+ if test_case.turns is not None:
+ actual_output = format_turns(test_case.turns)
+ else:
+ actual_output = "AI'S ACTUAL OUTPUT TO EVALUATE: \n" + test_case.actual_output + "\n"
+
prompt = BiasTemplate.generate_evaluation_results(
- actual_output=test_case.actual_output,
+ actual_output=actual_output,
purpose=self.purpose,
)
if self.using_native_model:
diff --git a/deepteam/metrics/bias/template.py b/deepteam/metrics/bias/template.py
index bb7359c0..62dfa19f 100644
--- a/deepteam/metrics/bias/template.py
+++ b/deepteam/metrics/bias/template.py
@@ -15,7 +15,6 @@ class BiasTemplate:
AI'S INTENDED PURPOSE:
{purpose}
- AI'S ACTUAL OUTPUT TO EVALUATE:
{actual_output}
EVALUATION CRITERIA - Scrutinize the output for any manifestation of the following biases.
diff --git a/deepteam/red_team.py b/deepteam/red_team.py
index 8dffc2b8..ab83df38 100644
--- a/deepteam/red_team.py
+++ b/deepteam/red_team.py
@@ -14,12 +14,12 @@ def red_team(
framework: Optional[AISafetyFramework] = None,
attacks_per_vulnerability_type: int = 1,
ignore_errors: bool = False,
- run_async: bool = True,
+ async_mode: bool = True,
max_concurrent: int = 10,
target_purpose: Optional[str] = None,
):
red_teamer = RedTeamer(
- async_mode=run_async,
+ async_mode=async_mode,
max_concurrent=max_concurrent,
target_purpose=target_purpose,
)
diff --git a/deepteam/red_teamer/red_teamer.py b/deepteam/red_teamer/red_teamer.py
index b13ecc7e..d42bfe89 100644
--- a/deepteam/red_teamer/red_teamer.py
+++ b/deepteam/red_teamer/red_teamer.py
@@ -10,7 +10,7 @@ from rich import box
from deepeval.models import DeepEvalBaseLLM
from deepeval.metrics.utils import initialize_model
from deepeval.dataset.golden import Golden
-from deepeval.test_case import LLMTestCase, ConversationalTestCase, Turn
+from deepteam.test_case import RTTestCase
from deepeval.utils import get_or_create_event_loop
from deepteam.frameworks.frameworks import AISafetyFramework
@@ -65,7 +65,6 @@ class RedTeamer:
framework: Optional[AISafetyFramework] = None,
attacks_per_vulnerability_type: int = 1,
ignore_errors: bool = False,
- async_mode: bool = True,
reuse_simulated_attacks: bool = False,
metadata: Optional[dict] = None,
):
@@ -78,8 +77,6 @@ class RedTeamer:
"You must either provide a 'framework' or 'vulnerabilities'."
)
- self.async_mode = async_mode
-
if self.async_mode:
validate_model_callback_signature(
model_callback=model_callback,
@@ -338,9 +335,9 @@ class RedTeamer:
if simulated_attack.error is not None:
red_teaming_test_case.error = simulated_attack.error
- test_case = LLMTestCase(
+ test_case = RTTestCase(
input=simulated_attack.input,
- actual_output=str(simulated_attack.turn_history),
+ turns=simulated_attack.turn_history,
)
try:
@@ -384,7 +381,7 @@ class RedTeamer:
return red_teaming_test_case
else:
raise
- test_case = LLMTestCase(
+ test_case = RTTestCase(
input=simulated_attack.input,
actual_output=actual_output,
)
@@ -435,9 +432,9 @@ class RedTeamer:
if simulated_attack.error is not None:
red_teaming_test_case.error = simulated_attack.error
- test_case = LLMTestCase(
+ test_case = RTTestCase(
input=simulated_attack.input,
- actual_output=str(simulated_attack.turn_history),
+ turns=simulated_attack.turn_history,
)
try:
@@ -481,7 +478,7 @@ class RedTeamer:
return red_teaming_test_case
else:
raise
- test_case = LLMTestCase(
+ test_case = RTTestCase(
input=simulated_attack.input,
actual_output=actual_output,
)
diff --git a/deepteam/test_case/__init__.py b/deepteam/test_case/__init__.py
index e69de29b..4822859d 100644
--- a/deepteam/test_case/__init__.py
+++ b/deepteam/test_case/__init__.py
@@ -0,0 +1 @@
+from .test_case import RTTestCase, RTTurn
\ No newline at end of file
diff --git a/deepteam/test_case/test_case.py b/deepteam/test_case/test_case.py
index e8d8375f..cb7cd96e 100644
--- a/deepteam/test_case/test_case.py
+++ b/deepteam/test_case/test_case.py
@@ -1,9 +1,54 @@
+from typing import List, Literal, Union, Optional
+from enum import Enum
+from pydantic import model_validator
from deepeval.test_case import LLMTestCase, Turn
class RTTurn(Turn):
- pass
+ role: Literal['user', 'assistant']
+ content: str
class RTTestCase(LLMTestCase):
- pass
+ input: str
+ actual_output: Optional[str] = None
+ turns: Optional[List[RTTurn]] = None
+ metadata: Optional[dict] = None
+ attack_method: Optional[str] = None
+ error: Optional[str] = None
+
+ @model_validator(mode="before")
+ def validate_input(cls, data):
+ input = data.get("input")
+ actual_output = data.get("actual_output")
+ turns = data.get("turns")
+ attack_method = data.get("attack_method")
+ error = data.get("error")
+
+ if input is not None:
+ if not isinstance(input, str):
+ raise TypeError("'input' must be a string")
+
+ if actual_output is not None:
+ if not isinstance(actual_output, str):
+ raise TypeError("'actual_output' must be a string")
+
+ if turns is not None:
+ if not isinstance(turns, list):
+ if not all(isinstance(turn, RTTurn) for turn in turns):
+ raise TypeError("'turns' must be a list of 'RTTurn'")
+
+ if attack_method is not None:
+ if not isinstance(attack_method, str):
+ raise TypeError("'attack_method' must be a string")
+
+ if error is not None:
+ if not isinstance(error, str):
+ raise TypeError("'error' must be a string")
+
+ if ((actual_output is not None) and (turns is not None)):
+ raise AttributeError(
+ "An 'RTTestCase' cannot contain both 'actual_output' and 'turns' at the same time"
+ )
+
+ return data
\ No newline at end of file
diff --git a/deepteam/utils.py b/deepteam/utils.py
index bcd23907..38f2d363 100644
--- a/deepteam/utils.py
+++ b/deepteam/utils.py
@@ -1,5 +1,6 @@
import inspect
-from typing import Callable
+from typing import Callable, List
+from deepteam.test_case import RTTurn
def validate_model_callback_signature(
@@ -14,3 +15,15 @@ def validate_model_callback_signature(
raise ValueError(
"`model_callback` must not be async. `async_mode` has been set to False."
)
+
+def format_turns(turns: List[RTTurn]):
+ if not turns:
+ raise ValueError("There are no 'turns' to format.")
+
+ formatted_turns = "Full Conversation To Evaluate: \n"
+ for turn in turns:
+ formatted_turns += f"Role: {turn.role} \n"
+ formatted_turns += f"Content: {turn.content} \n\n"
+ formatted_turns += "End of conversation. \n"
+
+ return formatted_turns
\ No newline at end of file