pyrit.executor.attack.AttackStrategy#
- class AttackStrategy(*, objective_target: ~pyrit.prompt_target.common.prompt_target.PromptTarget, context_type: type[~pyrit.executor.attack.core.attack_strategy.AttackStrategyContextT], params_type: type[~pyrit.executor.attack.core.attack_parameters.AttackParamsT] = <class 'pyrit.executor.attack.core.attack_parameters.AttackParameters'>, logger: ~logging.Logger = <Logger ai-red-team (INFO)>)[source]#
Bases:
Strategy[AttackStrategyContextT,AttackStrategyResultT],Identifiable,ABCAbstract base class for attack strategies. Defines the interface for executing attacks and handling results.
- __init__(*, objective_target: ~pyrit.prompt_target.common.prompt_target.PromptTarget, context_type: type[~pyrit.executor.attack.core.attack_strategy.AttackStrategyContextT], params_type: type[~pyrit.executor.attack.core.attack_parameters.AttackParamsT] = <class 'pyrit.executor.attack.core.attack_parameters.AttackParameters'>, logger: ~logging.Logger = <Logger ai-red-team (INFO)>)[source]#
Initialize the attack strategy with a specific context type and logger.
- Parameters:
objective_target (PromptTarget) – The target system to attack.
context_type (type[AttackStrategyContextT]) – The type of context this strategy operates on.
params_type (Type[AttackParamsT]) – The type of parameters this strategy accepts. Defaults to AttackParameters. Use AttackParameters.excluding() to create a params type that rejects certain fields.
logger (logging.Logger) – Logger instance for logging events.
Methods
__init__(*, objective_target, context_type)Initialize the attack strategy with a specific context type and logger.
Execute the attack strategy asynchronously with the provided parameters.
execute_with_context_async(*, context)Execute strategy with complete lifecycle management.
Get the attack scoring configuration used by this strategy.
get_identifier()Get the component's identifier, building it lazily on first access.
Get the objective target for this attack strategy.
Get request converter configurations used by this strategy.
Attributes
Get the parameters type for this attack strategy.
- async execute_async(*, objective: str, next_message: Message | None = None, prepended_conversation: list[Message] | None = None, memory_labels: dict[str, str] | None = None, **kwargs: Any) AttackStrategyResultT[source]#
- async execute_async(**kwargs: Any) AttackStrategyResultT
Execute the attack strategy asynchronously with the provided parameters.
This method provides a stable contract for all attacks. The signature includes all standard parameters (objective, next_message, prepended_conversation, memory_labels). Attacks that don’t accept certain parameters will raise ValueError if those parameters are provided.
- Parameters:
objective (str) – The objective of the attack.
next_message (Optional[Message]) – Message to send to the target.
prepended_conversation (Optional[List[Message]]) – Conversation to prepend.
memory_labels (Optional[Dict[str, str]]) – Memory labels for the attack context.
**kwargs – Additional context-specific parameters (conversation_id, system_prompt, etc.).
- Returns:
The result of the attack execution.
- Return type:
AttackStrategyResultT
- Raises:
ValueError – If required parameters are missing or if unsupported parameters are provided.
- get_attack_scoring_config() AttackScoringConfig | None[source]#
Get the attack scoring configuration used by this strategy.
- Returns:
The scoring configuration, or None if not applicable.
- Return type:
Optional[AttackScoringConfig]
Note
Subclasses that use scoring should override this method to return their scoring configuration. The default implementation returns None.
- get_objective_target() PromptTarget[source]#
Get the objective target for this attack strategy.
- Returns:
The target system being attacked.
- Return type:
- get_request_converters() list[Any][source]#
Get request converter configurations used by this strategy.
- Returns:
The list of request PromptConverterConfiguration objects.
- Return type:
list[Any]
- property params_type: type[AttackParameters]#
Get the parameters type for this attack strategy.
- Returns:
The parameters type this strategy accepts.
- Return type:
Type[AttackParameters]