Running Red Teams
deepeval allows you to safety/penetration test LLM systems in a simple Python script. Bringing together vulnerabilities, attacks and your model callback, simply create a Python file and:
- Import your selected vulnerabilities.
- Import your chosen attacks.
- Define your model callback.
- Start red teaming.
The code looks like this:
from deepeval.red_team import red_team
from deepeval.red_team.vulnerabilities import Bias
from deepeval.red_team.attacks.single_turn import PromptInjection
from deepeval.red_team.test_case import RTTurn
async def model_callback(input: str, turns: list[RTTurn] = None) -> str:
# Replace this with your actual LLM application
return RTTurn(role="assistant", content="Your agent's response here...")
bias = Bias(types=["race"])
prompt_injection = PromptInjection()
risk_assessment = red_team(model_callback=model_callback, vulnerabilities=[bias], attacks=[prompt_injection])There is ONE mandatory and THIRTEEN optional arguments when calling the red_team() function:
model_callback: a callback of typeCallable[[str], str]that wraps around the target LLM system you wish to red team.- [Optional]
vulnerabilities: a list of typeBaseVulnerabilitys that determines the weaknesses to detect for. - [Optional]
attacks: a list of typeBaseAttacks that determines the methods that will be simulated to expose the definedvulnerabilities. - [Optional]
framework: an object of typeRedTeamingFrameworkthat contains bothvulnerabilitiesandattacksin it. - [Optional]
simulator_model: a string specifying which of OpenAI's GPT models to use, OR any custom LLM model of typeDeepEvalBaseLLMfor simulating attacks. Defaulted to"gpt-3.5-turbo-0125". - [Optional]
evaluation_model: a string specifying which of OpenAI's GPT models to use, OR any custom LLM model of typeDeepEvalBaseLLMfor evaluation. Defaulted to"gpt-4o". - [Optional]
attacks_per_vulnerability_type: an int that determines the number of attacks to be simulated per vulnerability type. Defaulted to1. - [Optional]
run_all_attacks: a boolean which when set toTrue, runs every attack you passed in against every simulated attack instead of sampling one at random per test case. Defaulted toFalse. See Running all attacks. - [Optional]
exposure: aLevelofLOW,MEDIUM, orHIGHdescribing how reachable your LLM application is to an attacker. Feeds the CVSS score of each test case. Defaulted toLevel.MEDIUM. - [Optional]
ignore_errors: a boolean which when set toTrue, ignores all exceptions raised during red teaming. Defaulted toFalse. - [Optional]
async_mode: a boolean specifying whether to enable async mode. Defaulted toTrue. - [Optional]
max_concurrent: an integer that determines the maximum number of coroutines that can be ran in parallel. You can decrease this value if your models are running into rate limit errors. Defaulted to10. - [Optional]
target_purpose: a string specifying your target LLM application's intended purpose. This affects the passing and failing of simulated attacks that are evaluated. Defaulted toNone. - [Optional]
attack_engine: an optionalAttackEngineused to refine baseline simulated attacks before they are sent to your target. Forwarded to the internalRedTeamer. Defaulted toNone(a default engine is created per run). See Attack engine.
Don't forget to save the results (or at least print it):
...
print(risk_assessment)
risk_assessment.save(to="./red-team-results/")The red_team() function is a quick and easy way to red team LLM systems in a stateless manner. If you wish to take advantage of more advanced features such as adversarial input caching to avoid simulating different attacks over and over again across different iterations of your LLM system, you should use deepeval's RedTeamer.
Running all attacks
By default, each simulated attack is enhanced by one attack drawn at random from the attacks you passed in, weighted by each attack's weight. That keeps a run cheap, but it means two runs of the same configuration can exercise different attacks — and it means an attack you passed in might never run at all.
Set run_all_attacks=True to run every attack against every simulated attack instead:
from deepeval.red_team import red_team
from deepeval.red_team.vulnerabilities import Bias
from deepeval.red_team.attacks.single_turn import PromptInjection, ROT13, Base64
risk_assessment = red_team(
model_callback=model_callback,
vulnerabilities=[Bias(types=["race"])],
attacks=[PromptInjection(), ROT13(), Base64()],
run_all_attacks=True,
)The number of test cases becomes vulnerability_types × attacks_per_vulnerability_type × attacks. The example above produces 3 test cases (one vulnerability type × 1 attack per type × 3 attacks) rather than 1.
Using a RedTeamer
deepeval offers a powerful RedTeamer that can scan LLM applications for safety risks and vulnerabilities. The RedTeamer has a red_team() method and is EXACTLY THE SAME as the standalone red_team() function, but using the RedTeamer would give you:
- Better control over your LLM system's safety testing lifecycle, allows reusing simulated attacks in the past.
- Better control over which models to use for simulating attacks and evaluating LLM outputs.
Create your red teamer
To use the RedTeamer, instantiate a RedTeamer instance.
from deepeval.red_team.red_teamer import RedTeamer
red_teamer = RedTeamer()There are SEVEN optional parameters when creating a RedTeamer:
- [Optional]
target_purpose: a string specifying your target LLM application's intended purpose. This affects the passing and failing of simulated attacks that are evaluated. Defaulted toNone. - [Optional]
simulator_model: a string specifying which of OpenAI's GPT models to use, OR any custom LLM model of typeDeepEvalBaseLLMfor simulating attacks. Defaulted to"gpt-3.5-turbo-0125". - [Optional]
evaluation_model: a string specifying which of OpenAI's GPT models to use, OR any custom LLM model of typeDeepEvalBaseLLMfor evaluation. Defaulted to"gpt-4o". - [Optional]
async_mode: a boolean specifying whether to enable async mode. Defaulted toTrue. - [Optional]
max_concurrent: an integer that determines the maximum number of coroutines that can be ran in parallel. You can decrease this value if your models are running into rate limit errors. Defaulted to10. - [Optional]
attack_engine: an optionalAttackEngineused as the default refinement engine for runs started from thisRedTeamerwhen you do not pass a differentattack_engineintored_team(). Defaulted toNone. See Attack engine. - [Optional]
exposure: aLevelofLOW,MEDIUM, orHIGHdescribing how reachable your LLM application is to an attacker. This is set once perRedTeamersince it describes the deployment rather than any single attack, and feeds the CVSS score of every test case. Defaulted toLevel.MEDIUM. See CVSS score.
Run your red team
Once you've set up your RedTeamer, and defined your target model and list of vulnerabilities, you can begin scanning your LLM application immediately using its red_team method, which is almost the same as the standalone red_team() function.
from deepeval.red_team.vulnerabilities import Bias
from deepeval.red_team.attacks.single_turn import PromptInjection, ROT13
from deepeval.red_team.red_teamer import RedTeamer
from deepeval.red_team.test_case import RTTurn
async def model_callback(input: str, turns: list[RTTurn] = None) -> str:
# Replace this with your actual LLM application
return RTTurn(role="assistant", content="Your LLM's response here...")
red_teamer = RedTeamer()
risk_assessment = red_teamer.red_team(
model_callback=model_callback,
vulnerabilities=[Bias(types=["race"])],
attacks=[PromptInjection(weight=2), ROT13(weight=1)],
)
print(risk_assessment.overall)There is ONE mandatory and ELEVEN optional arguments when calling the red_team() method:
model_callback: a callback of typeCallable[[str], str]that wraps around the target LLM system you wish to red team.- [Optional]
framework: an object of typeRedTeamingFrameworkthat contains bothvulnerabilitiesandattacksin it. - [Optional]
simulator_model: a string specifying which of OpenAI's GPT models to use, OR any custom LLM model of typeDeepEvalBaseLLMfor simulating attacks. Defaulted to"gpt-3.5-turbo-0125". - [Optional]
evaluation_model: a string specifying which of OpenAI's GPT models to use, OR any custom LLM model of typeDeepEvalBaseLLMfor evaluation. Defaulted to"gpt-4o". - [Optional]
attacks_per_vulnerability_type: an int that determines the number of attacks to be simulated per vulnerability type. Defaulted to1. - [Optional]
run_all_attacks: a boolean which when set toTrue, runs every attack against every simulated attack instead of sampling one at random per test case. Defaulted toFalse. See running all attacks above. - [Optional]
ignore_errors: a boolean which when set toTrue, ignores all exceptions raised during red teaming. Defaulted toFalse. - [Optional]
reuse_previous_attacks: a boolean which when set toTrue, will reuse the previously simulated attacks from the lastred_team()method run. These attacks can only be reused if they exist (i.e. if you have already ranred_team()at least once). Defaulted toFalse. - [Optional]
async_mode: a boolean specifying whether to enable async mode. Defaulted toTrue. - [Optional]
max_concurrent: an integer that determines the maximum number of coroutines that can be ran in parallel. You can decrease this value if your models are running into rate limit errors. Defaulted to10. - [Optional]
target_purpose: a string specifying your target LLM application's intended purpose. This affects the passing and failing of simulated attacks that are evaluated. Defaulted toNone. - [Optional]
attack_engine: an optionalAttackEnginefor this run’s attack refinement (overrides theRedTeamerdefault for this call when provided). Defaulted toNone. See Attack engine.
You'll notice that the RedTeamer since it is stateful, allows you to reuse_previous_attacks, which is not possible by the standalone red_team() function.
...
risk_assessment = red_teamer.red_team(model_callback=model_callback, reuse_previous_attacks=True)