DeepTeam is a framework to red team LLMs and AI agents.
.
sid-murali1e4555a97d65
diff --git a/README.md b/README.mdindex 523f31b1..66c49d7c 100644--- a/README.md+++ b/README.md@@ -189,32 +189,126 @@ deepteam set-gemini --google-api-key "key"## Config Example```yaml+# Red teaming models (separate from target)+models:+ simulator: gpt-3.5-turbo-0125+ evaluation: gpt-4o++# Target system configurationtarget:+ purpose: "A helpful AI assistant"++ # Option 1: Simple model specification (for testing foundational models)model: gpt-3.5-turbo- simulator_model: gpt-3.5-turbo- evaluation_model: gpt-4o++ # Option 2: Custom DeepEval model (for LLM applications)+ # model:+ # provider: custom+ # file: "my_custom_model.py"+ # class: "MyCustomLLM"++# System configuration+system_config:+ max_concurrent: 10+ attacks_per_vulnerability_type: 3+ run_async: true+ ignore_errors: falsedefault_vulnerabilities:- name: "Bias"types: ["race", "gender"]+ - name: "Toxicity"+ types: ["profanity", "insults"]attacks:- name: "Prompt Injection"```-**Available Providers:** `openai`, `anthropic`, `gemini`, `azure`, `local`, `ollama`+**Key Changes:**+- **`models`**: Simulator and evaluation models moved out of target (they're not part of the target system)+- **`target`**: Now supports both simple `model` specs and flexible custom DeepEval model specifications for custom LLM applications+- **`system_config`**: System configuration for red teaming execution++**CLI Overrides:**+The `-c` and `-a` CLI options override YAML config values:+```bash+# Override max_concurrent and attacks_per_vuln from CLI+deepteam run config.yaml -c 20 -a 5+```++**Target Configuration Options:**++For simple model testing:+```yaml+target:+ model: gpt-4o+ purpose: "A helpful AI assistant"+```++For custom LLM applications with DeepEval models:+```yaml+target:+ model:+ provider: custom+ file: "my_custom_model.py"+ class: "MyCustomLLM"+ purpose: "A customer service chatbot"+```++**Available Providers:** `openai`, `anthropic`, `gemini`, `azure`, `local`, `ollama`, `custom`**Model Format:**```yaml# Simple format-model: gpt-4o+simulator: gpt-4o# With provider-model:+simulator:provider: anthropicmodel: claude-3-5-sonnet-20241022```+## Custom Model Requirements++When creating custom models for target testing, you **MUST**:++1. **Inherit from `DeepEvalBaseLLM`**+2. **Implement `get_model_name()`** - return a string model name+3. **Implement `load_model()`** - return the model object (usually `self`)+4. **Implement `generate(prompt: str) -> str`** - synchronous generation+5. **Implement `a_generate(prompt: str) -> str`** - asynchronous generation++**Example Custom Model:**+```python+import requests+import json+import asyncio+from deepeval.models import DeepEvalBaseLLM++class MyCustomLLM(DeepEvalBaseLLM):+ def __init__(self):+ self.api_url = "https://your-api.com/chat"+ self.api_key = "your-api-key"++ def get_model_name(self):+ return "My Custom LLM"++ def load_model(self):+ return self++ def generate(self, prompt: str) -> str:+ response = requests.post(+ self.api_url,+ headers={"Authorization": f"Bearer {self.api_key}"},+ json={"message": prompt}+ )+ return response.json()["response"]++ async def a_generate(self, prompt: str) -> str:+ loop = asyncio.get_event_loop()+ return await loop.run_in_executor(None, self.generate, prompt)+```+<br /># Roadmapdiff --git a/deepteam/cli/main.py b/deepteam/cli/main.pyindex 844a7646..870a4731 100644--- a/deepteam/cli/main.py+++ b/deepteam/cli/main.py@@ -1,5 +1,9 @@import yamlimport typer+import importlib.util+import sys+import os+from typing import Callable, Awaitablefrom . import configfrom .model_callback import load_model@@ -117,6 +121,29 @@ def _build_attack(cfg: dict):return cls(**kwargs)+def _load_callback_from_file(file_path: str, function_name: str) -> Callable[[str], Awaitable[str]]:+ """Load a callback function from a Python file."""+ if not os.path.exists(file_path):+ raise FileNotFoundError(f"Target callback file not found: {file_path}")++ spec = importlib.util.spec_from_file_location("target_module", file_path)+ if spec is None or spec.loader is None:+ raise ImportError(f"Could not load module from {file_path}")++ module = importlib.util.module_from_spec(spec)+ sys.modules["target_module"] = module+ spec.loader.exec_module(module)++ if not hasattr(module, function_name):+ raise AttributeError(f"Function '{function_name}' not found in {file_path}")++ callback = getattr(module, function_name)+ if not callable(callback):+ raise TypeError(f"'{function_name}' in {file_path} is not callable")++ return callback++def _load_config(path: str):with open(path, "r") as f:return yaml.safe_load(f)@@ -132,24 +159,28 @@ def run(cfg = _load_config(config_file)config.apply_env()- target = cfg.get("target", {})-- # Load simulator and evaluation models using load_model- simulator_model_spec = target.get("simulator_model", "gpt-3.5-turbo-0125")- evaluation_model_spec = target.get("evaluation_model", "gpt-4o")+ # Parse red teaming models (moved out of target section)+ models_cfg = cfg.get("models", {})+ simulator_model_spec = models_cfg.get("simulator", "gpt-3.5-turbo-0125")+ evaluation_model_spec = models_cfg.get("evaluation", "gpt-4o")simulator_model = load_model(simulator_model_spec)evaluation_model = load_model(evaluation_model_spec)- # Use CLI options to override config values- final_max_concurrent = max_concurrent if max_concurrent is not None else cfg.get("options", {}).get("max_concurrent", 10)- final_attacks_per_vuln = attacks_per_vuln if attacks_per_vuln is not None else cfg.get("options", {}).get("attacks_per_vulnerability_type", 1)+ # Parse system configuration (renamed from options)+ system_config = cfg.get("system_config", {})+ final_max_concurrent = max_concurrent if max_concurrent is not None else system_config.get("max_concurrent", 10)+ final_attacks_per_vuln = attacks_per_vuln if attacks_per_vuln is not None else system_config.get("attacks_per_vulnerability_type", 1)++ # Parse target configuration+ target_cfg = cfg.get("target", {})+ target_purpose = target_cfg.get("purpose", "")red_teamer = RedTeamer(simulator_model=simulator_model,evaluation_model=evaluation_model,- target_purpose=target.get("purpose", ""),- async_mode=cfg.get("options", {}).get("run_async", True),+ target_purpose=target_purpose,+ async_mode=system_config.get("run_async", True),max_concurrent=final_max_concurrent,)@@ -159,28 +190,40 @@ def run(attacks = [_build_attack(a) for a in cfg.get("attacks", [])]- # Load the target model for the model callback- target_model_spec = target.get("model")- if not target_model_spec:- # Fallback to a simple model specification if not provided- target_model_spec = "gpt-3.5-turbo"+ # Load target model callback - support both model specs and custom callbacks+ model_callback = None- target_model = load_model(target_model_spec)-- # Create the async model callback- async def model_callback(input: str) -> str:- response = await target_model.a_generate(input)- # Ensure we return a string, handle different response types- if isinstance(response, tuple):- return str(response[0]) if response else "Empty response"- return str(response)+ if "callback" in target_cfg:+ # Load custom callback from file+ callback_cfg = target_cfg["callback"]+ file_path = callback_cfg.get("file")+ function_name = callback_cfg.get("function", "model_callback")++ if not file_path:+ raise ValueError("Target callback configuration missing 'file' field")++ model_callback = _load_callback_from_file(file_path, function_name)++ elif "model" in target_cfg:+ # Use model specification for simple cases+ target_model_spec = target_cfg["model"]+ target_model = load_model(target_model_spec)++ async def model_callback(input: str) -> str:+ response = await target_model.a_generate(input)+ # Ensure we return a string, handle different response types+ if isinstance(response, tuple):+ return str(response[0]) if response else "Empty response"+ return str(response)+ else:+ raise ValueError("Target configuration must specify either 'model' or 'callback'")risk = red_teamer.red_team(model_callback=model_callback,vulnerabilities=vulnerabilities,attacks=attacks,attacks_per_vulnerability_type=final_attacks_per_vuln,- ignore_errors=cfg.get("options", {}).get("ignore_errors", False),+ ignore_errors=system_config.get("ignore_errors", False),)red_teamer._print_risk_assessment()diff --git a/deepteam/cli/model_callback.py b/deepteam/cli/model_callback.pyindex 2263987d..925439c9 100644--- a/deepteam/cli/model_callback.py+++ b/deepteam/cli/model_callback.py@@ -1,6 +1,9 @@from __future__ import annotationsfrom typing import Any, Dict, Union, Optional+import importlib.util+import sys+import osfrom deepeval.models import (GPTModel,@@ -15,6 +18,29 @@ from deepeval.models import (from deepeval.metrics.utils import initialize_model+def _load_custom_model_from_file(file_path: str, class_name: str) -> DeepEvalBaseLLM:+ """Load a custom DeepEval model class from a Python file."""+ if not os.path.exists(file_path):+ raise FileNotFoundError(f"Custom model file not found: {file_path}")++ spec = importlib.util.spec_from_file_location("custom_model_module", file_path)+ if spec is None or spec.loader is None:+ raise ImportError(f"Could not load module from {file_path}")++ module = importlib.util.module_from_spec(spec)+ sys.modules["custom_model_module"] = module+ spec.loader.exec_module(module)++ if not hasattr(module, class_name):+ raise AttributeError(f"Class '{class_name}' not found in {file_path}")++ model_class = getattr(module, class_name)+ if not issubclass(model_class, DeepEvalBaseLLM):+ raise TypeError(f"'{class_name}' in {file_path} must inherit from DeepEvalBaseLLM")++ return model_class()++def load_model(spec: Union[str, Dict[str, Any], None]) -> DeepEvalBaseLLM:"""Construct a DeepEval model instance from a YAML spec or string."""if spec is None or isinstance(spec, str):@@ -80,4 +106,11 @@ def load_model(spec: Union[str, Dict[str, Any], None]) -> DeepEvalBaseLLM:temperature=temperature,)+ if provider == "custom":+ file_path = spec.get("file")+ class_name = spec.get("class")+ if not file_path or not class_name:+ raise ValueError("Custom provider requires 'file' and 'class' fields")+ return _load_custom_model_from_file(file_path, class_name)+raise ValueError(f"Unknown provider: {provider}")\ No newline at end of filediff --git a/examples/example_config.yaml b/examples/example_config.yamlnew file mode 100644index 00000000..0b06a151--- /dev/null+++ b/examples/example_config.yaml@@ -0,0 +1,75 @@+# Example DeepTeam Configuration+# This demonstrates the new YAML structure with improved organization++# Red teaming models (use standard models)+models:+ simulator: gpt-3.5-turbo-0125+ evaluation: gpt-4o++ # Alternative: With provider specification+ # simulator:+ # provider: anthropic+ # model: claude-3-5-sonnet-20241022+ # temperature: 0.7+ # evaluation:+ # provider: openai+ # model: gpt-4o+ # temperature: 0.1++# Target system configuration (uses custom model)+target:+ purpose: "A helpful AI assistant"++ # Option 1: Simple model specification (for testing foundational models directly)+ # model: gpt-3.5-turbo++ # Option 1b: With provider specification+ # model:+ # provider: anthropic+ # model: claude-3-haiku-20240307+ # temperature: 0.5++ # Option 2: Custom DeepEval model+ model:+ provider: custom+ file: "tests/test_callback.py"+ class: "CustomFireworksLLM"++# System configuration (renamed from options/simulation)+system_config:+ max_concurrent: 10 # Maximum concurrent operations+ attacks_per_vulnerability_type: 3 # Number of attacks per vulnerability type+ run_async: true # Whether to run operations asynchronously+ ignore_errors: false # Whether to continue on errors++# Vulnerabilities to test+default_vulnerabilities:+ - name: "Bias"+ types: ["race", "gender"]++ - name: "Toxicity"+ types: ["profanity", "insults"]++# Custom vulnerability types+custom_vulnerabilities:+ - name: "CustomVulnerability"+ custom_name: "Business Logic"+ types: ["access_control", "privilege_escalation"]++# Attack methods+attacks:+ - name: "Prompt Injection"+ weight: 1.0++ - name: "Leetspeak"+ weight: 0.8++ - name: "ROT-13"+ weight: 0.6++ - name: "Base64"+ weight: 0.7++ - name: "Roleplay"+ weight: 0.9+\ No newline at end of filediff --git a/examples/sample.yaml b/examples/sample.yamldeleted file mode 100644index 06fb9013..00000000--- a/examples/sample.yaml+++ /dev/null@@ -1,35 +0,0 @@-name: Run DeepTeam Penetration Testing--target:- purpose: "To test the model's ability to generate code"- model:- provider: openai- model: gpt-3.5-turbo- simulator_model:- provider: openai- model: gpt-3.5-turbo- evaluation_model:- provider: openai- model: gpt-4o--default_vulnerabilities:- - name: "Bias"- types: ["race"]--custom_vulnerabilities:- - name: "CustomVulnerability"- custom_name: "API Security"- types: ["endpoint_security"]- prompt:--attacks:- - name: "Prompt Injection"- weight: 1- - name: "ROT-13"- weight: 2--options:- attacks_per_vulnerability_type: 2- ignore_errors: false- run_async: true- max_concurrent: 5\ No newline at end of filediff --git a/tests/test_callback.py b/tests/test_callback.pynew file mode 100644index 00000000..6ba1c0b5--- /dev/null+++ b/tests/test_callback.py@@ -0,0 +1,59 @@+import requests+import json+import asyncio+from deepeval.models import DeepEvalBaseLLM+++class CustomFireworksLLM(DeepEvalBaseLLM):+ def __init__(self, model_name="accounts/fireworks/models/llama4-scout-instruct-basic"):+ self.api_url = "https://api.fireworks.ai/inference/v1/chat/completions"+ self.api_token = "fw_3ZN7ZzUGBGry228zT9RcVdww"+ self.model_name = model_name++ def get_model_name(self):+ return f"Fireworks {self.model_name}"++ def load_model(self):+ # For API-based models, we return self since there's no local model to load+ return self++ def generate(self, prompt: str) -> str:+ payload = {+ "model": self.model_name,+ "messages": [+ {+ "role": "system",+ "content": "You are a helpful AI assistant."+ },+ {+ "role": "user",+ "content": prompt+ }+ ],+ "max_tokens": 16874,+ "temperature": 1,+ "top_p": 1,+ "top_k": 50,+ "repetition_penalty": 1,+ "n": 1,+ "ignore_eos": False,+ "stop": None,+ "stream": False,+ }+ headers = {+ "Accept": "application/json",+ "Content-Type": "application/json",+ "Authorization": f"Bearer {self.api_token}"+ }++ response = requests.post(self.api_url, headers=headers, data=json.dumps(payload))+ if response.status_code == 200:+ return response.json()['choices'][0]['message']['content'].strip()+ else:+ print(f"Error: {response.status_code}, {response.text}")+ return ""++ async def a_generate(self, prompt: str) -> str:+ # Async version - runs the API call in a thread to avoid blocking+ loop = asyncio.get_event_loop()+ return await loop.run_in_executor(None, self.generate, prompt)\ No newline at end of filediff --git a/tests/test_example_config.py b/tests/test_example_config.pynew file mode 100644index 00000000..04323546--- /dev/null+++ b/tests/test_example_config.py@@ -0,0 +1,69 @@+import os+import sys+import asyncio+import yaml+import pytest++# Add the parent directory to the path to import deepteam+sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__))))++from deepteam.cli.main import _load_config, _load_callback_from_file+++def test_example_config_loads():+ """Test that the example config loads correctly"""+ config_path = os.path.join(os.path.dirname(__file__), "..", "examples", "example_config.yaml")+ config = _load_config(config_path)++ # Check basic structure+ assert "models" in config+ assert "target" in config+ assert "system_config" in config+ assert "default_vulnerabilities" in config+ assert "attacks" in config++ # Check models+ assert config["models"]["simulator"] == "gpt-3.5-turbo-0125"+ assert config["models"]["evaluation"] == "gpt-4o"++ # Check target+ assert "callback" in config["target"]+ assert config["target"]["callback"]["file"] == "target_callback.py"+ assert config["target"]["callback"]["function"] == "model_callback"++ # Check system config+ assert config["system_config"]["max_concurrent"] == 10+ assert config["system_config"]["attacks_per_vulnerability_type"] == 3+++def test_target_callback_loads():+ """Test that the target callback can be loaded"""+ callback_path = os.path.join(os.path.dirname(__file__), "..", "examples", "target_callback.py")++ # Test loading the callback function+ callback = _load_callback_from_file(callback_path, "model_callback")++ # Verify it's callable and async+ assert callable(callback)+ assert asyncio.iscoroutinefunction(callback)+++async def test_target_callback_execution():+ """Test that the target callback can execute (mock test)"""+ callback_path = os.path.join(os.path.dirname(__file__), "..", "examples", "target_callback.py")+ callback = _load_callback_from_file(callback_path, "model_callback")++ # Note: This would make an actual API call - in real tests you'd mock this+ # For now, just verify it's an async function that takes a string+ import inspect+ sig = inspect.signature(callback)+ assert len(sig.parameters) == 1+ assert "input" in sig.parameters+ assert sig.parameters["input"].annotation == str+++if __name__ == "__main__":+ test_example_config_loads()+ test_target_callback_loads()+ print("✅ All tests passed!")\ No newline at end of file