Implementation:Liu00222 Open Prompt Injection create attacker
Appearance
| Knowledge Sources | |
|---|---|
| Domains | Prompt_Injection, Adversarial_ML |
| Last Updated | 2026-02-14 15:00 GMT |
Overview
Concrete factory function for creating prompt injection attacker instances provided by the OpenPromptInjection attackers module.
Description
The create_attacker function dispatches to the correct Attacker subclass (NaiveAttacker, EscapeCharAttacker, IgnoreAttacker, FakeCompAttacker, CombineAttacker) based on the attack strategy string. Each attacker implements an `.inject(clean_data, idx, target_task)` method that produces attacked prompts.
Usage
Import this function when setting up an experiment to create the attacker that will modify clean data prompts during the attack execution phase.
Code Reference
Source Location
- Repository: Open-Prompt-Injection
- File: OpenPromptInjection/attackers/__init__.py
- Lines: L7-29
Signature
def create_attacker(attack_strategy, task):
"""
Factory function to create an attacker.
Args:
attack_strategy (str): One of 'naive', 'escape', 'ignore',
'fake_comp', 'combine'.
task (InjectedTask): The injected task instance
(must have for_injection() == True).
Returns:
Attacker: Subclass instance with .inject(clean_data, idx, target_task) method.
"""
Import
import OpenPromptInjection as PI
# or
from OpenPromptInjection import create_attacker
I/O Contract
Inputs
| Name | Type | Required | Description |
|---|---|---|---|
| attack_strategy | str | Yes | One of `'naive'`, `'escape'`, `'ignore'`, `'fake_comp'`, `'combine'` |
| task | InjectedTask | Yes | Injected task instance (must have `for_injection() == True`) |
Outputs
| Name | Type | Description |
|---|---|---|
| attacker | Attacker | Instance with `.inject(clean_data, idx, target_task) -> str` and `.task` attribute |
Usage Examples
Creating a Combined Attacker
import OpenPromptInjection as PI
from OpenPromptInjection.utils import open_config
inject_config = open_config("configs/task_configs/hsol_config.json")
inject_task = PI.create_task(inject_config, data_num=100, for_injection=True)
attacker = PI.create_attacker("combine", inject_task)
# Use the attacker to inject into clean data
attacked_prompt = attacker.inject("The movie was great.", idx=0, target_task="sst2")
print(attacked_prompt)
Related Pages
Implements Principle
Uses Heuristic
Page Connections
Double-click a node to navigate. Hold to expand connections.
Principle
Implementation
Heuristic
Environment