Skip to content
KitploitKITPLOIT
工具博客
提交
工具博客
提交

黑客、渗透测试和网络安全工具,武装您的安全武器库!

Kitploit 是一个黑客、网络安全和渗透测试工具的目录。发现最新的项目更新,查找漏洞、分析系统、自动化测试并加强你的安全。

··订阅源·联系·隐私·© 2026 Kitploit

工具目录

分类

查看所有分类
Loading categories
batch_jailbreak — 此仓库包含论文“[Safety in Batches? Understanding and Mitigating Safety Failures in Batch Prompting]”的官方实现。 | Kitploit
工具/GitHubGitHub/96kihyun/batch_jailbreak
机器学习论文与研究学习与教育AI 安全对抗性攻击
GitHub96kihyun/batch_jailbreak

batch_jailbreak

此仓库包含论文“[Safety in Batches? Understanding and Mitigating Safety Failures in Batch Prompting]”的官方实现。

查看仓库
5天前尚未审核

最受欢迎

查看全部 →

发现我们社区最常用的工具。

探索所有工具

浏览我们的工具集合

查看所有工具 →
分享
网站

批量中的安全性?理解与缓解批量提示中的安全失效 官方仓库

Kihyun Kim, Hee-Seon Kim, Wonjun Lee, Changick Kim

韩国科学技术院 (KAIST)

arXiv

新闻

  • 2026.09 我们的论文已被 AACL-IJCNLP 2026 (Main) 接收!🎉
  • 2026.08 论文已在 arXiv 上发布!
  • 2026.08 代码已发布!

概述

批量提示是一种实用的推理策略,它将多个查询打包到单次调用中。我们表明,它在效用上的成功并不能延伸到安全性上:一个在单独提出时会被可靠拒绝的有害问题,当被嵌入到一批良性问题中时,可能会引出有害的回答。

本仓库提供了以下官方代码:

  1. 生成批量提示攻击数据
  2. 在开源模型(Phi / Llama / Qwen)和商业 API(GPT / Gemini / Claude)上运行推理
  3. 使用 LlamaGuard-4、WildGuard 和 Beaver-Dam 的集成来评估攻击成功率

准备工作

  1. 克隆本仓库。
root@kitploit:~
git clone https://github.com/96kihyun/batch_jailbreak.git
cd batch_jailbreak
  1. 环境配置
root@kitploit:~
conda create -n batch python=3.10 -y
conda activate batch
pip install -r requirements.txt

本地模型推理以及三个评判模型都需要 GPU。已在 Python 3.10 + PyTorch 2.4 + transformers==5.2 环境下测试。

注意:src/evaluate/llamaguard4/ 中附带了一份为 LlamaGuard-4 内置的 transformers 副本(Llama4ForConditionalGeneration)。它仅在 LlamaGuard 加载时被添加到 sys.path 中。

  1. 注册模型 / API 密钥

在 configs/models.yaml 中填写本地 HuggingFace 检查点路径(生成器 + 评判器),并在 configs/api.yaml 中设置 API 密钥(或通过环境变量 OPENAI_API_KEY / GEMINI_API_KEY / ANTHROPIC_API_KEY / HF_TOKEN)。

所需数据与模型

  • 良性填充内容:data/context_dataset/gsm8k.jsonl(每行包含一个 "question" 键)
  • 有害数据集(自动下载):JailbreakBench/JBB-Behaviors、walledai/StrongREJECT
  • 生成器:Phi-4 / Llama-3.1-8B-Instruct / Qwen3-8B
  • 评判器:Llama-Guard-4-12B、PKU-Alignment/beaver-dam-7b、allenai/wildguard

生成批量提示数据

默认:--harmful_dataset JBB,批量大小 n = 1..10,313 个样本。良性槽位由 GSM8K(single_math)填充。

root@kitploit:~
python -m src.utils.generate_batch_question_prompt

输出写入 data/batch_prompt/single_math/{n}/pos_{k}.jsonl。

常用参数

运行推理

开源模型(Phi / Llama / Qwen)

root@kitploit:~
python scripts/inference/run_inference_batch_prompt.py \
  --model_name qwen \
  --case single_math --n 9 --pos 2 \
  --harmful_dataset JBB

对 (model, case, batch_size, pos) 进行批量扫描:

root@kitploit:~
bash scripts/inference/run_inference_batch_prompt_eval.sh \
  --dataset jbb --gpu_id 0 \
  --models phi qwen --cases single_math \
  --batch_sizes 12 --pos_values 1 2

商业 API(GPT / Gemini / Claude)

root@kitploit:~
python scripts/inference/run_inference_api.py \
  --api gpt \
  --input_file data/batch_prompt_jbb/single_math/12/pos_2.jsonl \
  --model_name gpt --output_subdir batch_prompt_jbb/single_math/12/pos_2 \
  --harmful_dataset JBB --extract_pos 2

输出以 JSONL 格式保存在 result/inference/<model>/... 下,包含 {prompt, response} 对。

评估攻击成功率

每个回答由 LlamaGuard-4、WildGuard 和 Beaver-Dam 评分,然后通过多数投票进行组合:

root@kitploit:~
python scripts/eval/run_ensemble.py \
  --input_file result/inference/qwen/batch_prompt_jbb/single_math/12/qwen_single_math_n12_pos_2_response.jsonl \
  --output_dir result/evaluation/batch_prompt_jbb/qwen/single_math/12/pos_2

你也可以在与推理相同的循环中运行评估:

root@kitploit:~
bash scripts/inference/run_inference_batch_prompt_eval.sh \
  --dataset jbb --gpu_id 0 \
  --models qwen --cases single_math \
  --batch_sizes 12 --pos_values 2 \
  --conda_env batch

快速开始(冒烟测试)

root@kitploit:~
# 1. data
python -m src.utils.generate_batch_question_prompt \
  --harmful_dataset JBB --min_n 12 --max_n 12 --num_samples 100

# 2. inference
python scripts/inference/run_inference_batch_prompt.py \
  --model_name llama --case single_math --n 12 --pos 2 --harmful_dataset JBB

# 3. evaluation
python scripts/eval/run_ensemble.py \
  --input_file result/inference/llama/batch_prompt/llama_single_math_n12_pos_2_response.jsonl

引用

如果我们的工作对您的研究有帮助,请考虑引用我们的论文。

root@kitploit:~
@article{kim2026safety,
  title={Safety in Batches? Understanding and Mitigating Safety Failures in Batch Prompting},
  author={Kim, Kihyun and Kim, HeeSeon and Lee, Wonjun and Kim, Changick},
  journal={arXiv preprint arXiv:2608.02681},
  year={2026}
}

联系方式

如对我们的论文或代码有任何疑问,请发送邮件至 [email protected]。

下载工具
参数默认值说明
--harmful_datasetJBBJBB 或 StrongREJECT
--min_n / --max_n1 / 10批量大小 N 的闭区间范围
--num_samples313有害提示的数量
--seed42随机数种子
--output_dir./data/batch_prompt输出根目录