Attacks API¶
The attacks module provides the framework for creating and applying adversarial attacks to prompts.
Base Classes¶
BaseAttack¶
- class hivetracered.attacks.base_attack.BaseAttack[source]¶
Bases:
ABCAbstract base class for all attack implementations. Defines the standard interface for applying attacks to prompts in both synchronous and asynchronous contexts.
- DETERMINISTIC = False¶
- __or__(other)[source]¶
Allows using the | operator to compose attacks: attack1 | attack2 means attack1(attack2(prompt)).
- Parameters:
other – Another BaseAttack instance to compose with this one
- Returns:
A ComposedAttack instance that applies attacks in sequence
- abstractmethod get_description()[source]¶
Get the description of the attack.
- Return type:
- Returns:
A description of what the attack does
- abstractmethod get_name()[source]¶
Get the name of the attack.
- Return type:
- Returns:
The name of the attack
- get_params()[source]¶
Get the parameters of the attack.
- Returns:
A dictionary containing the attack’s parameters
TemplateAttack¶
- class hivetracered.attacks.template_attack.TemplateAttack(template='{prompt}', name=None, description=None)[source]¶
Bases:
BaseAttackA base class for template-based attacks. Allows creating new attacks by defining a template string with a ‘{prompt}’ placeholder where the original prompt will be inserted.
- DETERMINISTIC = True¶
- __init__(template='{prompt}', name=None, description=None)[source]¶
Initialize the template attack with a specific template string.
- apply(prompt)[source]¶
Apply the template attack to the given prompt.
- Parameters:
prompt (
str|list[dict[str,str]]) – A string or list of messages to apply the attack to. If the prompt is a list, the template will be applied to the last message.- Return type:
- Returns:
The transformed prompt with the template applied
- Raises:
ValueError – If the prompt is invalid or the last message is not a human message
- get_description()[source]¶
Get the description of the attack.
- Return type:
- Returns:
The custom description if provided, otherwise a default description based on the template
- get_name()[source]¶
Get the name of the attack.
- Return type:
- Returns:
The custom name if provided, otherwise the class name
- get_params()[source]¶
Get the parameters of the attack.
- Returns:
A dictionary containing the attack’s parameters
AlgoAttack¶
ModelAttack¶
IterativeAttack¶
ComposedAttack¶
Single-Turn Attack Types¶
Iterative Attacks¶
Iterative attacks (PAIR, TAP) that optimise a single attack prompt across an internal refinement loop:
Roleplay Attacks¶
Persuasion Attacks¶
Token Smuggling Attacks¶
Context Switching Attacks¶
In-Context Learning Attacks¶
Task Deflection Attacks¶
Text Structure Modification Attacks¶
Output Formatting Attacks¶
Irrelevant Information Attacks¶
Simple Instructions Attacks¶
Multi-Turn Attack Types¶
Conversational Attacks¶
Conversational attacks (Crescendo) that drive a multi-turn dialogue with the target across many rounds within a single invocation:
See Also¶
Attack Types Reference - Attack reference
Creating Custom Attacks - Usage and custom attacks
Crescendo Attack - Crescendo attack reference