Overview
AutoDANGenerator evolves a population of jailbreak prompts against a target
model using a simplified hierarchical genetic algorithm based on Liu et al.,
“AutoDAN: Generating Stealthy Jailbreak Prompts on Aligned Large Language
Models” (arXiv 2310.15140).
The earlier autodan_genetic.py attack shipped four hardcoded templates and
was misrepresented as a genetic algorithm. This generator implements the real
loop: scored fitness, sentence-level crossover, word- and sentence-level
mutation, and elite selection with early termination.
Algorithm Steps
- Initialization. Build a population of
population_sizecandidates by cycling through a seed pool (DEFAULT_SEED_TEMPLATESby default) and inlining the harmfultargetinto each template. - Fitness scoring. For each candidate, send to
target_provider, askjudge_providerto rate compliance 1-10, subtract a fluency penalty approximated by character-level Shannon entropy. - Selection. Sort by fitness; keep the top
elite_ksurvivors. - Crossover. Split parents on sentence boundaries (
[.!?]\s+). Swap a random contiguous span of sentences between two parents to produce a child. - Mutation. With probability
mutation_rate, apply either word-level synonym substitution (offline static dictionary) or sentence-level paraphrase viaattacker_provider. - Termination. Stop when best raw judge score reaches
success_thresholdormax_generationsis exhausted.
Parameters
The fluency penalty is a Shannon-entropy proxy for the negative-log-perplexity
score used in the paper. Natural English settles near 0.3, near-uniform random
text approaches 1.4. The proxy keeps the loop fully offline-capable.
Example Usage
best_prompt, best_score, generations,
and history (one record per generation).
Backward Compatibility
The legacy attack id"autodan-genetic" still resolves to
ai_blackteam.attacks.autodan_genetic.AutoDANGenetic, but the class now
delegates to AutoDANGenerator when a target_provider is supplied. Static
batch runs without providers fall back to the seed templates so existing
pipelines keep working.
Reference Paper
- Liu, X., Xu, N., Lu, K., Xiao, C. AutoDAN: Generating Stealthy Jailbreak Prompts on Aligned Large Language Models. arXiv 2310.15140 (2024).