Setup • Usage • View Results • Data Pipeline
This repository contains the implementation of JARGON, a jailbreak attack that leverages domain‑specific contexts to bypass LLM safety mechanisms. By embedding harmful requests within legitimate academic, technical domains, JARGON exposes significant vulnerabilities in current alignment techniques. The framework supports multi‑round conversational attacks, prompt optimization, belief state tracking, and automatic extraction of harmful knowledge.
Authors: Ki Sen Hung, Xi Yang, Chang Liu, Haoran Li, Kejiang Chen, Changxuan Fan, Tsun On Kwok, Weiming Zhang, Xiaomeng Li, Yangqiu Song
git clone https://github.com/JerryHung1103/JARGON.git
cd JARGONconda env create -f environment.yml
conda activate JARGONCreate a .env file in the project root (or copy from .env.example) with the following variables:
HUGGINGFACE_HUB_TOKEN=your_hf_token_here
# Attacker model
ATTACKER_API_KEY=your_api_key_here
ATTACKER_BASE_URL=your_base_url_here
# Optimizer model
OPTIMIZER_API_KEY=your_api_key_here
OPTIMIZER_BASE_URL=your_base_url_here
# Judge model
JUDGE_API_KEY=your_api_key_here
JUDGE_BASE_URL=your_base_url_here
# Summary helper model
SUMMARY_API_KEY=your_api_key_here
SUMMARY_BASE_URL=your_base_url_here
# Refusal checker model
REFUSAL_CHECKER_API_KEY=your_api_key_here
REFUSAL_CHECKER_BASE_URL=your_base_url_here
# Target model
TARGET_API_KEY=your_api_key_here
TARGET_BASE_URL=your_base_url_here
# Knowledge extractor (for purification)
KNOWLEDGE_EXTRACTOR_API_KEY=your_api_key_here
KNOWLEDGE_EXTRACTOR_BASE_URL=your_base_url_here
# Safeguard (optional, only if enable_safeguard=True)
SAFEGUARD_API_KEY=your_api_key_here
SAFEGUARD_BASE_URL=your_base_url_here💡Note: All BASE_URL values should point to the API endpoint of your chosen LLM provider (e.g., OpenAI‑compatible endpoint). You can use the same API key for multiple roles if your provider allows.
The main settings are controlled through config.yml. Below are some example and explanations:
jailbreak_setting:
mode: 'Attack'
# Mode can be 'Attack' or 'Distillation'
# - 'Attack': Uses LLM judge to determine successful jailbreaks
# - 'Distillation': No judge is used. Attack ends when max_rounds_per_attack is reached.
# NOTE: Distillation mode cannot be used with early_stop enabled.
max_retries: 3
# Number of full retry attempts per test case (outer loop)
max_trials_per_retry: 2
# Number of trials (conversations) within each retry
max_rounds_per_attack: 4
# Maximum number of attack rounds (turns) in one conversation/trial
example_injection_threshold: 2
# After how many retries will successful jailbreak examples be injected into the attacker prompt.
# Set to a low number to enable few-shot learning earlier.
optimization_variants: 8
# Number of prompt variants generated during each optimization step.
# Higher = more diversity, but higher API cost.
diverse_attack_ratio: 0.5
# Ratio of variants that use diversity/role-playing techniques (0.0 ~ 1.0)
# 0.5 means 50% diverse variants, 50% standard variants.
early_stop: False
# If True, stops the attack immediately once a successful jailbreak is found.
# Greatly saves time and API cost in 'Attack' mode.
# Cannot be used together with 'Distillation' mode.
record_trajectories: False
# If True, saves full attack trajectories (both harmful and safe) into separate folders.
# Useful for analysis or collecting data for fine-tuning refusal models.
num_workers: 5
# Number of parallel worker processes.
# Adjust according to your CPU cores and API rate limits.
enable_safeguard: False
# Enables dynamic safeguard simulation on the target model (for testing robustness against defenses).
# ...
target_model:
model: "deepseek-chat"
base_url: "${TARGET_BASE_URL}"
api_key: "${TARGET_API_KEY}"
generate_kwargs:
temperature: 0.0
attacker:
model: "deepseek-chat"
base_url: "${ATTACKER_BASE_URL}"
api_key: "${ATTACKER_API_KEY}"
generate_kwargs:
temperature: 0.3
# ... other models follow the same patternTo prepare a new research paper (e.g., as context for the Jargon attack), use the paper_content_extractor.py script. It processes a PDF file, extracts the core academic content (removing PDF artifacts, headers, footers, etc.), and generates a structured JSON summary containing the title, abstract, methodology, and full cleaned text.
python paper_content_extractor.py --pdf_file_path path_to_paper.pdfRun the attacks:
python main.pyWhen you run an attack, the following components execute in a coordinated loop across retries, trials, and conversation rounds:
| Component | Role |
|---|---|
Jailbreak Engine (jailbreak_engine.py) |
|
| Attacker |
|
Prompt Optimizer (agents/optimizer.py) |
|
| Target Model |
|
Evaluator (agents/evaluator.py) |
|
Knowledge Extractor (knowledge_extractor_generate) |
|
Refusal Checker (agents/refusal_checker_utils.py) |
|
| Judge |
|
Safeguard (agents/safeguard.py) – optional (enabled via enable_safeguard) |
|
| Results & Caching |
|
After running the attack, You can calculate key metrics using the built-in script:
python metrics.py --result_json_file_path your_results.json
When record_trajectories: True, the system saves harmful and safe trajectories separately.
Use process_data.py to clean harmful trajectories and generate training data for fine-tuning.
python process_data.py \
--harmful_trajectories_path your_harmful_trajectories_.json \
--safe_trajectories_path your_safe_trajectories.json
It removes and refine the harmful elements from the last assistant response in harmful trajectories
Merges the cleaned responses into the safe data then saves the result to ./training_data/