Skip to content

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

.

penguine2fd3d99310c2
diff --git a/deepteam/__init__.py b/deepteam/__init__.py
index 5c7d9cb6..d8fd9bd9 100644
--- a/deepteam/__init__.py
+++ b/deepteam/__init__.py
@@ -9,10 +9,12 @@ from deepteam.red_team import red_team
from deepteam.guardrails import (
Guardrails,
)
+from deepteam._version import __version__
__all__ = [
"red_team",
"Guardrails",
+ "__version__",
]
diff --git a/deepteam/attacks/base_attack.py b/deepteam/attacks/base_attack.py
index df84f43c..595a6e7a 100644
--- a/deepteam/attacks/base_attack.py
+++ b/deepteam/attacks/base_attack.py
@@ -4,6 +4,7 @@ from abc import ABC, abstractmethod
class BaseAttack(ABC):
weight: int = 1
multi_turn: bool = False
+ name: str
def enhance(self, attack: str, *args, **kwargs) -> str:
pass
diff --git a/deepteam/attacks/multi_turn/bad_likert_judge/bad_likert_judge.py b/deepteam/attacks/multi_turn/bad_likert_judge/bad_likert_judge.py
index 8f6bd0e2..b66c4a6e 100644
--- a/deepteam/attacks/multi_turn/bad_likert_judge/bad_likert_judge.py
+++ b/deepteam/attacks/multi_turn/bad_likert_judge/bad_likert_judge.py
@@ -28,6 +28,8 @@ from deepteam.attacks.single_turn import BaseSingleTurnAttack
class BadLikertJudge(BaseMultiTurnAttack):
+ name = "Bad Likert Judge"
+
def __init__(
self,
weight: int = 1,
@@ -469,7 +471,7 @@ class BadLikertJudge(BaseMultiTurnAttack):
return result
def get_name(self) -> str:
- return "Bad Likert Judge"
+ return self.name
@classmethod
def get_supported_categories(cls) -> list[str]:
diff --git a/deepteam/attacks/multi_turn/constants.py b/deepteam/attacks/multi_turn/constants.py
new file mode 100644
index 00000000..4ebd1175
--- /dev/null
+++ b/deepteam/attacks/multi_turn/constants.py
@@ -0,0 +1,23 @@
+from typing import Dict
+
+from .base_multi_turn_attack import BaseMultiTurnAttack
+from .crescendo_jailbreaking import CrescendoJailbreaking
+from .linear_jailbreaking import LinearJailbreaking
+from .tree_jailbreaking import TreeJailbreaking
+from .sequential_break import SequentialJailbreak
+from .bad_likert_judge import BadLikertJudge
+
+MULTI_TURN_ATTACK_CLASSES_MAP: Dict[str, BaseMultiTurnAttack] = {
+ v.name: v
+ for v in [
+ CrescendoJailbreaking,
+ LinearJailbreaking,
+ TreeJailbreaking,
+ SequentialJailbreak,
+ BadLikertJudge,
+ ]
+}
+
+MULTI_TURN_ATTACK_NAMES = sorted(
+ v.name for v in MULTI_TURN_ATTACK_CLASSES_MAP.values()
+)
\ No newline at end of file
diff --git a/deepteam/attacks/multi_turn/crescendo_jailbreaking/crescendo_jailbreaking.py b/deepteam/attacks/multi_turn/crescendo_jailbreaking/crescendo_jailbreaking.py
index ea50a55f..b8ca38a1 100644
--- a/deepteam/attacks/multi_turn/crescendo_jailbreaking/crescendo_jailbreaking.py
+++ b/deepteam/attacks/multi_turn/crescendo_jailbreaking/crescendo_jailbreaking.py
@@ -53,6 +53,7 @@ class MemorySystem:
class CrescendoJailbreaking(BaseMultiTurnAttack):
+ name = "Crescendo Jailbreaking"
def __init__(
self,
@@ -788,4 +789,4 @@ class CrescendoJailbreaking(BaseMultiTurnAttack):
return eval_response.value, eval_response.metadata
def get_name(self) -> str:
- return "Crescendo Jailbreaking"
+ return self.name
diff --git a/deepteam/attacks/multi_turn/linear_jailbreaking/linear_jailbreaking.py b/deepteam/attacks/multi_turn/linear_jailbreaking/linear_jailbreaking.py
index 77ae33b7..eddc302d 100644
--- a/deepteam/attacks/multi_turn/linear_jailbreaking/linear_jailbreaking.py
+++ b/deepteam/attacks/multi_turn/linear_jailbreaking/linear_jailbreaking.py
@@ -32,6 +32,8 @@ from deepteam.vulnerabilities import BaseVulnerability
class LinearJailbreaking(BaseMultiTurnAttack):
+ name = "Linear Jailbreaking"
+
def __init__(
self,
weight: int = 1,
@@ -435,4 +437,4 @@ class LinearJailbreaking(BaseMultiTurnAttack):
return result
def get_name(self) -> str:
- return "Linear Jailbreaking"
+ return self.name
diff --git a/deepteam/attacks/multi_turn/sequential_break/sequential_break.py b/deepteam/attacks/multi_turn/sequential_break/sequential_break.py
index 57673bc9..790f6ae0 100644
--- a/deepteam/attacks/multi_turn/sequential_break/sequential_break.py
+++ b/deepteam/attacks/multi_turn/sequential_break/sequential_break.py
@@ -33,6 +33,8 @@ from deepteam.attacks.single_turn import BaseSingleTurnAttack
class SequentialJailbreak(BaseMultiTurnAttack):
+ name = "Sequential Jailbreak"
+
def __init__(
self,
weight: int = 1,
@@ -607,4 +609,4 @@ class SequentialJailbreak(BaseMultiTurnAttack):
return result
def get_name(self) -> str:
- return "Sequential Jailbreak"
+ return self.name
diff --git a/deepteam/attacks/multi_turn/tree_jailbreaking/tree_jailbreaking.py b/deepteam/attacks/multi_turn/tree_jailbreaking/tree_jailbreaking.py
index adf5dab2..35d503db 100644
--- a/deepteam/attacks/multi_turn/tree_jailbreaking/tree_jailbreaking.py
+++ b/deepteam/attacks/multi_turn/tree_jailbreaking/tree_jailbreaking.py
@@ -53,6 +53,7 @@ class TreeNode:
class TreeJailbreaking(BaseMultiTurnAttack):
+ name = "Tree Jailbreaking"
def __init__(
self,
@@ -728,4 +729,4 @@ class TreeJailbreaking(BaseMultiTurnAttack):
return max(MIN_BRANCHES, min(MAX_BRANCHES, branches))
def get_name(self) -> str:
- return "Tree Jailbreaking"
+ return self.name
diff --git a/deepteam/attacks/single_turn/base64/base64.py b/deepteam/attacks/single_turn/base64/base64.py
index a0d5ec89..07d88d6c 100644
--- a/deepteam/attacks/single_turn/base64/base64.py
+++ b/deepteam/attacks/single_turn/base64/base64.py
@@ -3,6 +3,8 @@ from deepteam.attacks.single_turn import BaseSingleTurnAttack
class Base64(BaseSingleTurnAttack):
+ name = "Base64"
+
def __init__(self, weight: int = 1):
self.weight = weight
@@ -11,4 +13,4 @@ class Base64(BaseSingleTurnAttack):
return base64.b64encode(attack.encode()).decode()
def get_name(self) -> str:
- return "Base64"
+ return self.name
diff --git a/deepteam/attacks/single_turn/constants.py b/deepteam/attacks/single_turn/constants.py
new file mode 100644
index 00000000..29a5008c
--- /dev/null
+++ b/deepteam/attacks/single_turn/constants.py
@@ -0,0 +1,47 @@
+from typing import Dict
+
+from .base_single_turn_attack import BaseSingleTurnAttack
+from .base64 import Base64
+from .gray_box import GrayBox
+from .leetspeak import Leetspeak
+from .math_problem import MathProblem
+from .multilingual import Multilingual
+from .prompt_injection import PromptInjection
+from .prompt_probing import PromptProbing
+from .roleplay import Roleplay
+from .rot13 import ROT13
+from .system_override import SystemOverride
+
+
+from .system_override import SystemOverride
+from .permission_escalation.permission_escalation import PermissionEscalation
+from .goal_redirection.goal_redirection import GoalRedirection
+from .semantic_manipulation.semantic_manipulation import LinguisticConfusion
+from .input_bypass.input_bypass import InputBypass
+from .context_poisoning.context_poisoning import ContextPoisoning
+
+
+SINGLE_TURN_ATTACK_CLASSES_MAP: Dict[str, BaseSingleTurnAttack] = {
+ v.name: v
+ for v in [
+ Base64,
+ GrayBox,
+ Leetspeak,
+ MathProblem,
+ Multilingual,
+ PromptInjection,
+ PromptProbing,
+ Roleplay,
+ ROT13,
+ SystemOverride,
+ PermissionEscalation,
+ LinguisticConfusion,
+ InputBypass,
+ ContextPoisoning,
+ GoalRedirection,
+ ]
+}
+
+SINGLE_TURN_ATTACK_NAMES = sorted(
+ v.name for v in SINGLE_TURN_ATTACK_CLASSES_MAP.values()
+)
\ No newline at end of file
diff --git a/deepteam/attacks/single_turn/context_poisoning/context_poisoning.py b/deepteam/attacks/single_turn/context_poisoning/context_poisoning.py
index 58f90322..f5155ee6 100644
--- a/deepteam/attacks/single_turn/context_poisoning/context_poisoning.py
+++ b/deepteam/attacks/single_turn/context_poisoning/context_poisoning.py
@@ -7,6 +7,8 @@ from deepteam.attacks.single_turn.context_poisoning.template import (
class ContextPoisoning(BaseSingleTurnAttack):
+ name = "Context Poisoning"
+
def __init__(self, weight: int = 3, max_retries: int = 3):
self.weight = weight # Higher weight since memory poisoning can have persistent effects
self.max_retries = max_retries
@@ -48,4 +50,4 @@ class ContextPoisoning(BaseSingleTurnAttack):
return self.enhance(attack)
def get_name(self) -> str:
- return "Context Poisoning"
+ return self.name
diff --git a/deepteam/attacks/single_turn/goal_redirection/goal_redirection.py b/deepteam/attacks/single_turn/goal_redirection/goal_redirection.py
index 8e0d5b80..b8da22e8 100644
--- a/deepteam/attacks/single_turn/goal_redirection/goal_redirection.py
+++ b/deepteam/attacks/single_turn/goal_redirection/goal_redirection.py
@@ -7,6 +7,8 @@ from deepteam.attacks.single_turn.goal_redirection.template import (
class GoalRedirection(BaseSingleTurnAttack):
+ name = "Goal Redirection"
+
def __init__(self, weight: int = 1, max_retries: int = 3):
self.weight = weight
self.max_retries = max_retries
@@ -48,4 +50,4 @@ class GoalRedirection(BaseSingleTurnAttack):
return self.enhance(attack)
def get_name(self) -> str:
- return "Goal Redirection"
+ return self.name
diff --git a/deepteam/attacks/single_turn/gray_box/gray_box.py b/deepteam/attacks/single_turn/gray_box/gray_box.py
index fd105f7f..a32e3805 100644
--- a/deepteam/attacks/single_turn/gray_box/gray_box.py
+++ b/deepteam/attacks/single_turn/gray_box/gray_box.py
@@ -19,6 +19,7 @@ from deepteam.attacks.attack_simulator.utils import (
class GrayBox(BaseSingleTurnAttack):
+ name = "Gray Box"
def __init__(self, weight: int = 1, max_retries: int = 5):
self.weight = weight
@@ -154,4 +155,4 @@ class GrayBox(BaseSingleTurnAttack):
return attack
def get_name(self) -> str:
- return "Gray Box"
+ return self.name
diff --git a/deepteam/attacks/single_turn/input_bypass/input_bypass.py b/deepteam/attacks/single_turn/input_bypass/input_bypass.py
index 1a026942..0c338567 100644
--- a/deepteam/attacks/single_turn/input_bypass/input_bypass.py
+++ b/deepteam/attacks/single_turn/input_bypass/input_bypass.py
@@ -7,6 +7,8 @@ from deepteam.attacks.single_turn.input_bypass.template import (
class InputBypass(BaseSingleTurnAttack):
+ name = "Input Bypass"
+
def __init__(self, weight: int = 2, max_retries: int = 3):
self.weight = weight # Higher weight since this targets validation vulnerabilities
self.max_retries = max_retries
@@ -44,4 +46,4 @@ class InputBypass(BaseSingleTurnAttack):
return self.enhance(attack)
def get_name(self) -> str:
- return "Input Bypass"
+ return self.name
diff --git a/deepteam/attacks/single_turn/leetspeak/leetspeak.py b/deepteam/attacks/single_turn/leetspeak/leetspeak.py
index 32164825..d1a38221 100644
--- a/deepteam/attacks/single_turn/leetspeak/leetspeak.py
+++ b/deepteam/attacks/single_turn/leetspeak/leetspeak.py
@@ -2,6 +2,8 @@ from deepteam.attacks.single_turn import BaseSingleTurnAttack
class Leetspeak(BaseSingleTurnAttack):
+ name = "Leetspeak"
+
def __init__(self, weight: int = 1):
self.weight = weight
@@ -29,4 +31,4 @@ class Leetspeak(BaseSingleTurnAttack):
return self.enhance(attack)
def get_name(self) -> str:
- return "Leetspeak"
+ return self.name
diff --git a/deepteam/attacks/single_turn/math_problem/math_problem.py b/deepteam/attacks/single_turn/math_problem/math_problem.py
index 7729b07a..1242878a 100644
--- a/deepteam/attacks/single_turn/math_problem/math_problem.py
+++ b/deepteam/attacks/single_turn/math_problem/math_problem.py
@@ -20,6 +20,8 @@ from deepteam.attacks.attack_simulator.utils import (
class MathProblem(BaseSingleTurnAttack):
+ name = "Math Problem"
+
def __init__(
self,
weight: int = 1,
@@ -168,4 +170,4 @@ class MathProblem(BaseSingleTurnAttack):
)
def get_name(self) -> str:
- return "Math Problem"
+ return self.name
diff --git a/deepteam/attacks/single_turn/multilingual/multilingual.py b/deepteam/attacks/single_turn/multilingual/multilingual.py
index 0f566d7a..92e5f76d 100644
--- a/deepteam/attacks/single_turn/multilingual/multilingual.py
+++ b/deepteam/attacks/single_turn/multilingual/multilingual.py
@@ -21,6 +21,8 @@ from deepteam.attacks.attack_simulator.utils import (
class Multilingual(BaseSingleTurnAttack):
+ name = "Multilingual"
+
def __init__(self, weight: int = 1, max_retries: int = 5):
self.weight = weight
self.max_retries = max_retries
@@ -153,4 +155,4 @@ class Multilingual(BaseSingleTurnAttack):
return attack
def get_name(self) -> str:
- return "Multilingual"
+ return self.name
diff --git a/deepteam/attacks/single_turn/permission_escalation/permission_escalation.py b/deepteam/attacks/single_turn/permission_escalation/permission_escalation.py
index f27a6ffe..db466183 100644
--- a/deepteam/attacks/single_turn/permission_escalation/permission_escalation.py
+++ b/deepteam/attacks/single_turn/permission_escalation/permission_escalation.py
@@ -7,6 +7,8 @@ from deepteam.attacks.single_turn.permission_escalation.template import (
class PermissionEscalation(BaseSingleTurnAttack):
+ name = "Permission Escalation"
+
def __init__(self, weight: int = 1, max_retries: int = 3):
self.weight = weight
self.max_retries = max_retries
@@ -48,4 +50,4 @@ class PermissionEscalation(BaseSingleTurnAttack):
return self.enhance(attack)
def get_name(self) -> str:
- return "Permission Escalation"
+ return self.name
diff --git a/deepteam/attacks/single_turn/prompt_injection/prompt_injection.py b/deepteam/attacks/single_turn/prompt_injection/prompt_injection.py
index b25aa73e..6018de15 100644
--- a/deepteam/attacks/single_turn/prompt_injection/prompt_injection.py
+++ b/deepteam/attacks/single_turn/prompt_injection/prompt_injection.py
@@ -7,6 +7,8 @@ from deepteam.attacks.single_turn.prompt_injection.template import (
class PromptInjection(BaseSingleTurnAttack):
+ name = "Prompt Injection"
+
def __init__(self, weight: int = 1):
self.weight = weight
@@ -22,4 +24,4 @@ class PromptInjection(BaseSingleTurnAttack):
return self.enhance(attack)
def get_name(self) -> str:
- return "Prompt Injection"
+ return self.name
diff --git a/deepteam/attacks/single_turn/prompt_probing/prompt_probing.py b/deepteam/attacks/single_turn/prompt_probing/prompt_probing.py
index 5a97d62c..074d7ddb 100644
--- a/deepteam/attacks/single_turn/prompt_probing/prompt_probing.py
+++ b/deepteam/attacks/single_turn/prompt_probing/prompt_probing.py
@@ -20,6 +20,7 @@ from deepteam.attacks.attack_simulator.utils import (
class PromptProbing(BaseSingleTurnAttack):
+ name = "Prompt Probing"
def __init__(self, weight: int = 1, max_retries: int = 3):
self.weight = weight
@@ -153,4 +154,4 @@ class PromptProbing(BaseSingleTurnAttack):
return attack
def get_name(self) -> str:
- return "Prompt Probing"
+ return self.name
diff --git a/deepteam/attacks/single_turn/roleplay/roleplay.py b/deepteam/attacks/single_turn/roleplay/roleplay.py
index d8340a9a..a0a2d565 100644
--- a/deepteam/attacks/single_turn/roleplay/roleplay.py
+++ b/deepteam/attacks/single_turn/roleplay/roleplay.py
@@ -21,6 +21,7 @@ from deepteam.attacks.attack_simulator.utils import (
class Roleplay(BaseSingleTurnAttack):
+ name = "Roleplay"
def __init__(
self,
@@ -156,4 +157,4 @@ class Roleplay(BaseSingleTurnAttack):
return attack
def get_name(self) -> str:
- return "Roleplay"
+ return self.name
diff --git a/deepteam/attacks/single_turn/rot13/rot13.py b/deepteam/attacks/single_turn/rot13/rot13.py
index 85c0077d..0d8af76d 100644
--- a/deepteam/attacks/single_turn/rot13/rot13.py
+++ b/deepteam/attacks/single_turn/rot13/rot13.py
@@ -2,6 +2,8 @@ from deepteam.attacks.single_turn import BaseSingleTurnAttack
class ROT13(BaseSingleTurnAttack):
+ name = "ROT-13"
+
def __init__(self, weight: int = 1):
self.weight = weight
@@ -18,4 +20,4 @@ class ROT13(BaseSingleTurnAttack):
return self.enhance(attack)
def get_name(self) -> str:
- return "ROT-13"
+ return self.name
diff --git a/deepteam/attacks/single_turn/semantic_manipulation/semantic_manipulation.py b/deepteam/attacks/single_turn/semantic_manipulation/semantic_manipulation.py
index 034c1460..c24c3f27 100644
--- a/deepteam/attacks/single_turn/semantic_manipulation/semantic_manipulation.py
+++ b/deepteam/attacks/single_turn/semantic_manipulation/semantic_manipulation.py
@@ -7,6 +7,8 @@ from deepteam.attacks.single_turn.semantic_manipulation.template import (
class LinguisticConfusion(BaseSingleTurnAttack):
+ name = "Linguistic Confusion"
+
def __init__(self, weight: int = 1, max_retries: int = 3):
self.weight = weight
self.max_retries = max_retries
@@ -43,4 +45,4 @@ class LinguisticConfusion(BaseSingleTurnAttack):
return self.enhance(attack)
def get_name(self) -> str:
- return "Linguistic Confusion"
+ return self.name
diff --git a/deepteam/attacks/single_turn/system_override/system_override.py b/deepteam/attacks/single_turn/system_override/system_override.py
index bd4f2709..62426f8a 100644
--- a/deepteam/attacks/single_turn/system_override/system_override.py
+++ b/deepteam/attacks/single_turn/system_override/system_override.py
@@ -7,6 +7,8 @@ from deepteam.attacks.single_turn.system_override.template import (
class SystemOverride(BaseSingleTurnAttack):
+ name = "System Override"
+
def __init__(self, weight: int = 1, max_retries: int = 3):
self.weight = weight
self.max_retries = max_retries
@@ -48,4 +50,4 @@ class SystemOverride(BaseSingleTurnAttack):
return self.enhance(attack)
def get_name(self) -> str:
- return "System Override"
+ return self.name
diff --git a/deepteam/confident/__init__.py b/deepteam/confident/__init__.py
new file mode 100644
index 00000000..e69de29b
diff --git a/deepteam/confident/api.py b/deepteam/confident/api.py
new file mode 100644
index 00000000..18709d92
--- /dev/null
+++ b/deepteam/confident/api.py
@@ -0,0 +1,177 @@
+import logging
+from typing import Optional, Any, Union, Tuple
+import aiohttp
+import requests
+from enum import Enum
+from tenacity import (
+ retry,
+ wait_exponential_jitter,
+ retry_if_exception_type,
+)
+
+
+import deepteam
+from deepeval.confident.types import ApiResponse, ConfidentApiError
+from deepeval.confident.api import (
+ get_confident_api_key,
+ CONFIDENT_API_KEY_ENV_VAR,
+ get_base_api_url,
+ retryable_exceptions,
+ log_retry_error,
+ HttpMethods,
+)
+
+
+class Endpoints(Enum):
+ RISK_ASSESSMENT_ENDPOINT = "/v1/risk-assessments"
+
+
+class Api:
+ def __init__(self, api_key: Optional[str] = None):
+ if api_key is None:
+ api_key = get_confident_api_key()
+
+ if not api_key:
+ raise ValueError(
+ f"No Confident API key found. Please run `deepteam login` or set the {CONFIDENT_API_KEY_ENV_VAR} environment variable in the CLI."
+ )
+
+ self.api_key = api_key
+ self._headers = {
+ "Content-Type": "application/json",
+ "CONFIDENT_API_KEY": api_key,
+ "X-DeepTeam-Version": deepteam.__version__,
+ }
+ self.base_api_url = get_base_api_url()
+
+ @staticmethod
+ @retry(
+ wait=wait_exponential_jitter(initial=1, exp_base=2, jitter=2, max=10),
+ retry=retry_if_exception_type(retryable_exceptions),
+ after=log_retry_error,
+ )
+ def _http_request(
+ method: str, url: str, headers=None, json=None, params=None
+ ):
+ session = requests.Session()
+ return session.request(
+ method=method,
+ url=url,
+ headers=headers,
+ json=json,
+ params=params,
+ verify=True, # SSL verification is always enabled
+ )
+
+ def _handle_response(
+ self, response_data: Union[dict, Any]
+ ) -> Tuple[Any, Optional[str]]:
+ if not isinstance(response_data, dict):
+ return response_data, None
+
+ try:
+ api_response = ApiResponse(**response_data)
+ except Exception:
+ return response_data, None
+
+ if api_response.deprecated:
+ deprecation_msg = "You are using a deprecated API endpoint. Please update your deepteam version."
+ if api_response.link:
+ deprecation_msg += f" See: {api_response.link}"
+ logging.warning(deprecation_msg)
+
+ if not api_response.success:
+ error_message = api_response.error or "Request failed"
+ raise ConfidentApiError(error_message, api_response.link)
+
+ return api_response.data, api_response.link
+
+ def send_request(
+ self,
+ method: HttpMethods,
+ endpoint: Endpoints,
+ body=None,
+ params=None,
+ url_params=None,
+ ) -> Tuple[Any, Optional[str]]:
+ url = f"{self.base_api_url}{endpoint.value}"
+
+ # Replace URL parameters if provided
+ if url_params:
+ for key, value in url_params.items():
+ placeholder = f":{key}"
+ if placeholder in url:
+ url = url.replace(placeholder, str(value))
+
+ res = self._http_request(
+ method=method.value,
+ url=url,
+ headers=self._headers,
+ json=body,
+ params=params,
+ )
+
+ if res.status_code == 200:
+ try:
+ response_data = res.json()
+ return self._handle_response(response_data)
+ except ValueError:
+ return res.text, None
+ else:
+ try:
+ error_data = res.json()
+ return self._handle_response(error_data)
+ except (ValueError, ConfidentApiError) as e:
+ if isinstance(e, ConfidentApiError):
+ raise e
+ error_message = (
+ error_data.get("error", res.text)
+ if "error_data" in locals()
+ else res.text
+ )
+ raise Exception(error_message)
+
+ async def a_send_request(
+ self,
+ method: HttpMethods,
+ endpoint: Endpoints,
+ body=None,
+ params=None,
+ url_params=None,
+ ) -> Tuple[Any, Optional[str]]:
+ url = f"{self.base_api_url}{endpoint.value}"
+
+ if url_params:
+ for key, value in url_params.items():
+ placeholder = f":{key}"
+ if placeholder in url:
+ url = url.replace(placeholder, str(value))
+
+ async with aiohttp.ClientSession() as session:
+ async with session.request(
+ method=method.value,
+ url=url,
+ headers=self._headers,
+ json=body,
+ params=params,
+ ssl=True, # SSL verification enabled
+ ) as res:
+ if res.status == 200:
+ try:
+ response_data = await res.json()
+ return self._handle_response(response_data)
+ except aiohttp.ContentTypeError:
+ return await res.text(), None
+ else:
+ try:
+ error_data = await res.json()
+ return self._handle_response(error_data)
+ except (aiohttp.ContentTypeError, ConfidentApiError) as e:
+ if isinstance(e, ConfidentApiError):
+ raise e
+ error_message = (
+ error_data.get("error", await res.text())
+ if "error_data" in locals()
+ else await res.text()
+ )
+ raise Exception(error_message)
diff --git a/deepteam/red_teamer/red_teamer.py b/deepteam/red_teamer/red_teamer.py
index 251bfd91..26b13527 100644
--- a/deepteam/red_teamer/red_teamer.py
+++ b/deepteam/red_teamer/red_teamer.py
@@ -1,15 +1,20 @@
import asyncio
+import time
from typing import Dict, List, Optional, Union
+import webbrowser
from rich.console import Console
from rich.table import Table
import inspect
from rich import box
from enum import Enum
from collections import defaultdict
+from deepeval.confident.api import HttpMethods, is_confident
from deepeval.models import DeepEvalBaseLLM
from deepeval.metrics.utils import initialize_model
from deepeval.dataset.golden import Golden
+from deepteam.confident.api import Api, Endpoints
+from deepteam.red_teamer.api import map_risk_assessment_to_api
from deepteam.test_case import RTTestCase
from deepeval.utils import get_or_create_event_loop
@@ -34,6 +39,8 @@ from deepteam.red_teamer.risk_assessment import (
)
from deepteam.risks import getRiskCategory
+console = Console()
+
class RedTeamer:
risk_assessment: Optional[RiskAssessment] = None
@@ -191,6 +198,7 @@ class RedTeamer:
reuse_simulated_test_cases: bool = False,
metadata: Optional[dict] = None,
_print_assessment: Optional[bool] = True,
+ _upload_to_confident: Optional[bool] = True,
):
if not framework and not vulnerabilities:
raise ValueError(
@@ -255,6 +263,7 @@ class RedTeamer:
model_callback
), "`model_callback` needs to be sync. `async_mode` has been set to False."
+ start_time = time.time()
if evaluation_model is not None:
self.evaluation_model = evaluation_model
if simulator_model is not None:
@@ -363,7 +372,8 @@ class RedTeamer:
self.risk_assessment = RiskAssessment(
overview=construct_risk_assessment_overview(
- red_teaming_test_cases=red_teaming_test_cases
+ red_teaming_test_cases=red_teaming_test_cases,
+ run_duration=time.time() - start_time,
),
test_cases=red_teaming_test_cases,
)
@@ -371,6 +381,8 @@ class RedTeamer:
if _print_assessment:
self._print_risk_assessment(self.risk_assessment)
+ if _upload_to_confident:
+ self._post_risk_assessment()
return self.risk_assessment
@@ -387,6 +399,7 @@ class RedTeamer:
reuse_simulated_test_cases: bool = False,
metadata: Optional[dict] = None,
_print_assessment: Optional[bool] = True,
+ _upload_to_confident: Optional[bool] = True,
):
if not framework and not vulnerabilities:
raise ValueError(
@@ -429,6 +442,7 @@ class RedTeamer:
framework.load_dataset()
update_pbar(progress, task_id, advance_to_end=True)
+ start_time = time.time()
if evaluation_model is not None:
self.evaluation_model = evaluation_model
if simulator_model is not None:
@@ -547,7 +561,8 @@ class RedTeamer:
self.risk_assessment = RiskAssessment(
overview=construct_risk_assessment_overview(
- red_teaming_test_cases=red_teaming_test_cases
+ red_teaming_test_cases=red_teaming_test_cases,
+ run_duration=time.time() - start_time,
),
test_cases=red_teaming_test_cases,
)
@@ -555,6 +570,8 @@ class RedTeamer:
if _print_assessment:
self._print_risk_assessment(self.risk_assessment)
+ if _upload_to_confident:
+ self._post_risk_assessment()
return self.risk_assessment
@@ -896,6 +913,50 @@ class RedTeamer:
console.print("[bold magenta]LLM red teaming complete.[/bold magenta]")
console.print("=" * 80 + "\n")
+ def _post_risk_assessment(self):
+ if not is_confident():
+ passing = 0
+ failing = 0
+ for tc in self.risk_assessment.test_cases:
+ if tc and tc.score is not None:
+ if tc.score > 0:
+ passing += 1
+ else:
+ failing += 1
+ pass_rate = round((passing / (passing + failing)) * 100, 2)
+
+ console.print(
+ f"\n\n[rgb(5,245,141)]✓[/rgb(5,245,141)] Risk Assessment completed 🎉! (time taken: {round(self.risk_assessment.overview.run_duration, 2)}s)\n"
+ f"» Test Results ({len(self.risk_assessment.test_cases)} total tests):\n",
+ f" » Pass Rate: {pass_rate}% | Passed: [bold green]{passing}[/bold green] | Failed: [bold red]{failing}[/bold red]\n\n",
+ "=" * 80,
+ "\n\n» Want to share risk assessments with your team, or a place for your test cases to live? ❤️ 🏡\n"
+ " » Run [bold]'deepteam login'[/bold] to analyze and save testing results on [rgb(106,0,255)]Confident AI[/rgb(106,0,255)].\n\n",
+ )
+ return
+
+ api = Api()
+ api_risk_assessment = map_risk_assessment_to_api(self.risk_assessment)
+ try:
+ body = api_risk_assessment.model_dump(
+ by_alias=True, exclude_none=True
+ )
+ except AttributeError:
+ # Pydantic version below 2.0
+ body = api_risk_assessment.dict(by_alias=True, exclude_none=True)
+
+ data, link = api.send_request(
+ method=HttpMethods.POST,
+ endpoint=Endpoints.RISK_ASSESSMENT_ENDPOINT,
+ body=body,
+ )
+
+ console.print(
+ "[rgb(5,245,141)]✓[/rgb(5,245,141)] Done 🎉! View risk assessment on Confident AI:"
+ f"[link={link}]{link}[/link]"
+ )
+ webbrowser.open(link)
+
def _print_framework_overview_table(self, framework_results: dict):
for category_name in sorted(framework_results.keys()):
diff --git a/deepteam/red_teamer/risk_assessment.py b/deepteam/red_teamer/risk_assessment.py
index d6addb88..37e84c9c 100644
--- a/deepteam/red_teamer/risk_assessment.py
+++ b/deepteam/red_teamer/risk_assessment.py
@@ -97,6 +97,7 @@ class RedTeamingOverview(BaseModel):
vulnerability_type_results: List[VulnerabilityTypeResult]
attack_method_results: List[AttackMethodResult]
errored: int
+ run_duration: float
def to_df(self):
import pandas as pd
@@ -176,7 +177,7 @@ class RiskAssessment(BaseModel):
def construct_risk_assessment_overview(
- red_teaming_test_cases: List[RTTestCase],
+ red_teaming_test_cases: List[RTTestCase], run_duration: float
) -> RedTeamingOverview:
# Group test cases by vulnerability type
vulnerability_type_to_cases: Dict[
@@ -253,4 +254,5 @@ def construct_risk_assessment_overview(
vulnerability_type_results=vulnerability_type_results,
attack_method_results=attack_method_results,
errored=errored,
+ run_duration=run_duration,
)
diff --git a/kk.py b/kk.py
index ae647d77..4870cf32 100644
--- a/kk.py
+++ b/kk.py
@@ -18,19 +18,24 @@ async def your_callback(input: str, turns: List[RTTurn]) -> str:
return "I'm sorry but I can't answer this: " + input
+import deepteam
+
+print(deepteam.__version__)
+
print(os.environ.get("CONFIDENT_API_KEY"))
risk_assessment = red_team(
- # attacks=[
- # LinearJailbreaking(),
- # CrescendoJailbreaking(),
- # TreeJailbreaking(),
- # SequentialJailbreak(),
- # BadLikertJudge(),
- # ],
- # vulnerabilities=[Bias(), Bias()],
+ attacks=[
+ LinearJailbreaking(),
+ # CrescendoJailbreaking(),
+ Roleplay(),
+ # TreeJailbreaking(),
+ # SequentialJailbreak(),
+ # BadLikertJudge(),
+ ],
+ vulnerabilities=[Bias()],
model_callback=your_callback,
- framework=OWASPTop10(),
+ # framework=OWASPTop10(),
)
# from deepteam.vulnerabilities.constants import (