Skip to content

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

model_callback can now use strings and DeepEvalBaseLLM

A-Vamshif74e427843b8
diff --git a/deepteam/red_teamer/red_teamer.py b/deepteam/red_teamer/red_teamer.py
index a6718cef..4ff32316 100644
--- a/deepteam/red_teamer/red_teamer.py
+++ b/deepteam/red_teamer/red_teamer.py
@@ -28,6 +28,7 @@ from deepteam.utils import (
add_pbar,
update_pbar,
)
+from deepteam.red_teamer.utils import resolve_model_callback
from deepteam.vulnerabilities import BaseVulnerability
from deepteam.vulnerabilities.types import VulnerabilityType
from deepteam.attacks.attack_simulator import AttackSimulator
@@ -70,7 +71,7 @@ class RedTeamer:
def red_team(
self,
- model_callback: CallbackType,
+ model_callback: Union[CallbackType, str, DeepEvalBaseLLM],
vulnerabilities: Optional[List[BaseVulnerability]] = None,
attacks: Optional[List[BaseAttack]] = None,
simulator_model: DeepEvalBaseLLM = None,
@@ -92,6 +93,9 @@ class RedTeamer:
raise ValueError(
"You can only pass either 'framework' or 'attacks' and 'vulnerabilities' at the same time"
)
+
+ if isinstance(model_callback, str) or isinstance(model_callback, DeepEvalBaseLLM):
+ model_callback = resolve_model_callback(model_callback, self.async_mode)
if self.async_mode:
validate_model_callback_signature(
@@ -142,10 +146,6 @@ class RedTeamer:
framework.load_dataset()
update_pbar(progress, task_id, advance_to_end=True)
- assert not inspect.iscoroutinefunction(
- 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
@@ -284,6 +284,9 @@ class RedTeamer:
_print_assessment: Optional[bool] = True,
_upload_to_confident: Optional[bool] = True,
):
+ if isinstance(model_callback, str) or isinstance(model_callback, DeepEvalBaseLLM):
+ model_callback = resolve_model_callback(model_callback, self.async_mode)
+
if not framework and not vulnerabilities:
raise ValueError(
"You must either provide a 'framework' or 'vulnerabilities'"
diff --git a/deepteam/red_teamer/utils.py b/deepteam/red_teamer/utils.py
index db85fee6..f8c35928 100644
--- a/deepteam/red_teamer/utils.py
+++ b/deepteam/red_teamer/utils.py
@@ -1,7 +1,28 @@
-from typing import List, Dict
+from typing import List, Dict, Union, Optional
-from deepteam.test_case import RTTestCase
+from deepteam.test_case import RTTestCase, RTTurn
from deepteam.vulnerabilities.types import VulnerabilityType
+from deepeval.models import (
+ DeepEvalBaseLLM,
+ GPTModel,
+ GeminiModel,
+ GrokModel,
+ KimiModel,
+ AnthropicModel,
+ DeepSeekModel,
+ OllamaModel
+)
+from deepeval.metrics.utils import initialize_model
+
+MODEL_PROVIDER_MAPPING = {
+ "openai": GPTModel,
+ "anthropic": AnthropicModel,
+ "google": GeminiModel,
+ "xai": GrokModel,
+ "moonshotai": KimiModel,
+ "deepseek": DeepSeekModel,
+ "ollama": OllamaModel
+}
def group_attacks_by_vulnerability_type(
@@ -25,3 +46,33 @@ def group_attacks_by_vulnerability_type(
].append(simulated_attack)
return vulnerability_type_to_attacks_map
+
+
+def resolve_model_callback(model_callback: Union[str, DeepEvalBaseLLM], async_mode: bool):
+ if isinstance(model_callback, str):
+ if "/" in model_callback:
+ provider, model = model_callback.split("/", 1)
+ if provider in MODEL_PROVIDER_MAPPING.keys():
+ model_class = MODEL_PROVIDER_MAPPING.get(provider)
+ model_callback = model_class(model)
+ else:
+ raise ValueError(
+ f"Invalid provider for model_callback {model_callback}, please pass a string with provider prefix separated by a '/'. Ex: 'openai/gpt-4.1'. Avaliable provider prefixes: {[key for key in MODEL_PROVIDER_MAPPING.keys()]}"
+ )
+ else:
+ raise ValueError(
+ f"Invalid string for model_callback {model_callback}, please pass a string with provider prefix separated by a '/'. Ex: 'openai/gpt-4.1'. Avaliable provider prefixes: {[key for key in MODEL_PROVIDER_MAPPING.keys()]}"
+ )
+
+ model_callback, _ = initialize_model(model_callback)
+
+ if not async_mode:
+ def new_model_callback(input: str, turns: Optional[List[RTTurn]] = None):
+ res, _ = model_callback.generate(input)
+ return res
+ else:
+ async def new_model_callback(input: str, turns: Optional[List[RTTurn]] = None):
+ res, _ = await model_callback.a_generate(input)
+ return res
+
+ return new_model_callback
diff --git a/deepteam/utils.py b/deepteam/utils.py
index 410d305d..594ed20c 100644
--- a/deepteam/utils.py
+++ b/deepteam/utils.py
@@ -26,11 +26,11 @@ def validate_model_callback_signature(
):
if async_mode and not inspect.iscoroutinefunction(model_callback):
raise ValueError(
- "`model_callback` must be async. `async_mode` has been set to True."
+ "`model_callback` must be an async callback function. `async_mode` has been set to True."
)
if not async_mode and inspect.iscoroutinefunction(model_callback):
raise ValueError(
- "`model_callback` must not be async. `async_mode` has been set to False."
+ "`model_callback` must be a sync callback function. `async_mode` has been set to False."
)