Update README.md
Browse files
README.md
CHANGED
@@ -1,277 +1,272 @@
|
|
1 |
-
|
2 |
-
|
3 |
-
|
4 |
-
|
5 |
-
|
6 |
-
|
7 |
-
|
8 |
-
|
9 |
-
|
10 |
-
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
from transformers import AutoModelForCausalLM
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
β
|
40 |
-
|
41 |
-
β
|
42 |
-
|
43 |
-
β
|
44 |
-
|
45 |
-
β
|
46 |
-
|
47 |
-
β
|
48 |
-
|
49 |
-
β
|
50 |
-
|
51 |
-
β
|
52 |
-
|
53 |
-
β
|
54 |
-
|
55 |
-
β
|
56 |
-
|
57 |
-
β
|
58 |
-
|
59 |
-
β
|
60 |
-
|
61 |
-
β
|
62 |
-
|
63 |
-
β
|
64 |
-
|
65 |
-
β
|
66 |
-
|
67 |
-
β
|
68 |
-
|
69 |
-
β
|
70 |
-
|
71 |
-
β
|
72 |
-
|
73 |
-
β
|
74 |
-
|
75 |
-
β
|
76 |
-
|
77 |
-
β
|
78 |
-
|
79 |
-
β
|
80 |
-
|
81 |
-
β
|
82 |
-
|
83 |
-
β
|
84 |
-
|
85 |
-
β
|
86 |
-
|
87 |
-
β
|
88 |
-
|
89 |
-
β
|
90 |
-
|
91 |
-
|
92 |
-
|
93 |
-
β
|
94 |
-
|
95 |
-
β
|
96 |
-
|
97 |
-
β
|
98 |
-
|
99 |
-
β
|
100 |
-
|
101 |
-
|
102 |
-
|
103 |
-
β
|
104 |
-
|
105 |
-
β
|
106 |
-
|
107 |
-
β
|
108 |
-
|
109 |
-
β
|
110 |
-
|
111 |
-
β
|
112 |
-
|
113 |
-
β
|
114 |
-
|
115 |
-
β
|
116 |
-
|
117 |
-
β
|
118 |
-
|
119 |
-
β
|
120 |
-
|
121 |
-
β
|
122 |
-
|
123 |
-
β
|
124 |
-
|
125 |
-
β
|
126 |
-
|
127 |
-
β
|
128 |
-
|
129 |
-
β
|
130 |
-
|
131 |
-
β
|
132 |
-
|
133 |
-
β
|
134 |
-
|
135 |
-
β
|
136 |
-
|
137 |
-
β
|
138 |
-
|
139 |
-
β
|
140 |
-
|
141 |
-
β
|
142 |
-
|
143 |
-
β
|
144 |
-
|
145 |
-
β
|
146 |
-
|
147 |
-
β
|
148 |
-
|
149 |
-
β
|
150 |
-
|
151 |
-
β
|
152 |
-
|
153 |
-
β
|
154 |
-
|
155 |
-
β
|
156 |
-
|
157 |
-
β
|
158 |
-
|
159 |
-
β
|
160 |
-
|
161 |
-
β
|
162 |
-
|
163 |
-
β
|
164 |
-
|
165 |
-
β
|
166 |
-
|
167 |
-
β
|
168 |
-
|
169 |
-
β
|
170 |
-
|
171 |
-
β
|
172 |
-
|
173 |
-
β
|
174 |
-
|
175 |
-
β
|
176 |
-
|
177 |
-
β
|
178 |
-
|
179 |
-
β
|
180 |
-
|
181 |
-
β
|
182 |
-
|
183 |
-
β
|
184 |
-
|
185 |
-
β
|
186 |
-
|
187 |
-
β
|
188 |
-
|
189 |
-
β
|
190 |
-
|
191 |
-
β
|
192 |
-
|
193 |
-
β
|
194 |
-
|
195 |
-
β
|
196 |
-
|
197 |
-
β
|
198 |
-
|
199 |
-
β
|
200 |
-
|
201 |
-
β
|
202 |
-
|
203 |
-
β
|
204 |
-
|
205 |
-
|
206 |
-
|
207 |
-
|
208 |
-
|
209 |
-
|
210 |
-
|
211 |
-
|
212 |
-
|
213 |
-
|
214 |
-
|
215 |
-
|
216 |
-
|
217 |
-
|
218 |
-
|
219 |
-
|
220 |
-
|
221 |
-
|
222 |
-
|
223 |
-
|
224 |
-
|
225 |
-
|
226 |
-
|
227 |
-
|
228 |
-
|
229 |
-
|
230 |
-
|
231 |
-
|
232 |
-
|
233 |
-
|
234 |
-
|
235 |
-
|
236 |
-
|
237 |
-
|
238 |
-
|
239 |
-
|
240 |
-
|
241 |
-
|
242 |
-
|
243 |
-
|
244 |
-
|
245 |
-
|
246 |
-
|
247 |
-
|
248 |
-
|
249 |
-
|
250 |
-
|
251 |
-
|
252 |
-
|
253 |
-
|
254 |
-
|
255 |
-
|
256 |
-
|
257 |
-
|
258 |
-
|
259 |
-
|
260 |
-
|
261 |
-
|
262 |
-
|
263 |
-
|
264 |
-
|
265 |
-
|
266 |
-
|
267 |
-
|
268 |
-
|
269 |
-
|
270 |
-
|
271 |
-
|
272 |
-
|
273 |
-
β url={https://arxiv.org/abs/2406.11682},
|
274 |
-
|
275 |
-
}
|
276 |
-
|
277 |
-
\```
|
|
|
1 |
+
---
|
2 |
+
license: mit
|
3 |
+
language:
|
4 |
+
- en
|
5 |
+
tags:
|
6 |
+
- llm
|
7 |
+
- safety
|
8 |
+
- jailbreak
|
9 |
+
- knowledge
|
10 |
+
---
|
11 |
+
# Introduction
|
12 |
+
|
13 |
+
This is a model for generating a jailbreak prompt based on knowledge point texts. The model is trained on the Llama-2-7b dataset and fine-tuned on the Knowledge-to-Jailbreak dataset. The model is intended to bridge the gap between theoretical vulnerabilities and real-world application scenarios, simulating sophisticated adversarial attacks that incorporate specialized knowledge.
|
14 |
+
|
15 |
+
Our proposed method and dataset serve as a critical starting point for both offensive and defensive research, enabling the development of new techniques to enhance the security and robustness of language models in practical settings.
|
16 |
+
|
17 |
+
# How to load the model and tokenizer
|
18 |
+
|
19 |
+
We provide two helper functions for loading the model and tokenizer.
|
20 |
+
|
21 |
+
```python
|
22 |
+
|
23 |
+
import torch
|
24 |
+
|
25 |
+
from transformers import AutoModelForCausalLM, AutoTokenizer, AutoModelForSequenceClassification, AutoModelForTokenClassification
|
26 |
+
|
27 |
+
import os
|
28 |
+
|
29 |
+
import json
|
30 |
+
|
31 |
+
from peft import PeftModel
|
32 |
+
|
33 |
+
# from trl import AutoModelForCausalLMWithValueHead
|
34 |
+
|
35 |
+
from transformers import AutoModelForCausalLM as AutoGPTQForCausalLM
|
36 |
+
|
37 |
+
def load_tokenizer(dir_or_model):
|
38 |
+
|
39 |
+
β """
|
40 |
+
|
41 |
+
β This function is used to load the tokenizer for a specific pre-trained model.
|
42 |
+
|
43 |
+
β
|
44 |
+
|
45 |
+
β Args:
|
46 |
+
|
47 |
+
β dir_or_model: It can be either a directory containing the pre-training model configuration details or a pretrained model.
|
48 |
+
|
49 |
+
β
|
50 |
+
|
51 |
+
β Returns:
|
52 |
+
|
53 |
+
β It returns a tokenizer that can convert text to tokens for the specific model input.
|
54 |
+
|
55 |
+
β """
|
56 |
+
|
57 |
+
β is_lora_dir = os.path.isfile(os.path.join(dir_or_model, "adapter_config.json"))
|
58 |
+
|
59 |
+
β if is_lora_dir:
|
60 |
+
|
61 |
+
β loaded_json = json.load(open(os.path.join(dir_or_model, "adapter_config.json"), "r"))
|
62 |
+
|
63 |
+
β model_name = loaded_json["base_model_name_or_path"]
|
64 |
+
|
65 |
+
β else:
|
66 |
+
|
67 |
+
β model_name = dir_or_model
|
68 |
+
|
69 |
+
β
|
70 |
+
|
71 |
+
β if os.path.isfile(os.path.join(dir_or_model, "config.json")):
|
72 |
+
|
73 |
+
β loaded_json = json.load(open(os.path.join(dir_or_model, "config.json"), "r"))
|
74 |
+
|
75 |
+
β if "_name_or_path" in loaded_json:
|
76 |
+
|
77 |
+
β model_name = loaded_json["_name_or_path"]
|
78 |
+
|
79 |
+
β local_model_name = "/data3/MODELS/llama2-hf/llama-2-7b"#/data2/tsq/WaterBench/data/models/llama-2-7b-chat-hf
|
80 |
+
|
81 |
+
β
|
82 |
+
|
83 |
+
β print(">>>>>>>>>>>>>>>>>>>>>>>>>>notice this<<<<<<<<<<<<<<<<<<<<<<<<<<<<")
|
84 |
+
|
85 |
+
β
|
86 |
+
|
87 |
+
β #print(model_name)
|
88 |
+
|
89 |
+
β tokenizer = AutoTokenizer.from_pretrained(local_model_name)
|
90 |
+
|
91 |
+
β if tokenizer.pad_token is None:
|
92 |
+
|
93 |
+
β tokenizer.pad_token = tokenizer.eos_token
|
94 |
+
|
95 |
+
β tokenizer.pad_token_id = tokenizer.eos_token_id
|
96 |
+
|
97 |
+
β
|
98 |
+
|
99 |
+
β return tokenizer
|
100 |
+
|
101 |
+
def load_model(dir_or_model, classification=False, token_classification=False, return_tokenizer=False, dtype=torch.bfloat16, load_dtype=True,
|
102 |
+
|
103 |
+
β rl=False, peft_config=None, device_map="auto", revision='main'):
|
104 |
+
|
105 |
+
β """
|
106 |
+
|
107 |
+
β This function is used to load a model based on several parameters including the type of task it is targeted to perform.
|
108 |
+
|
109 |
+
β
|
110 |
+
|
111 |
+
β Args:
|
112 |
+
|
113 |
+
β dir_or_model: It can be either a directory containing the pre-training model configuration details or a pretrained model.
|
114 |
+
|
115 |
+
β classification (bool): If True, loads the model for sequence classification.
|
116 |
+
|
117 |
+
β token_classification (bool): If True, loads the model for token classification.
|
118 |
+
|
119 |
+
β return_tokenizer (bool): If True, returns the tokenizer along with the model.
|
120 |
+
|
121 |
+
β dtype: The data type that PyTorch should use internally to store the modelβs parameters and do the computation.
|
122 |
+
|
123 |
+
β load_dtype (bool): If False, sets dtype as torch.float32 regardless of the passed dtype value.
|
124 |
+
|
125 |
+
β rl (bool): If True, loads model specifically designed to be used in reinforcement learning environment.
|
126 |
+
|
127 |
+
β peft_config: Configuration details for Peft models.
|
128 |
+
|
129 |
+
β
|
130 |
+
|
131 |
+
β Returns:
|
132 |
+
|
133 |
+
β It returns a model for the required task along with its tokenizer, if specified.
|
134 |
+
|
135 |
+
β """
|
136 |
+
|
137 |
+
β is_lora_dir = os.path.isfile(os.path.join(dir_or_model, "adapter_config.json"))
|
138 |
+
|
139 |
+
β if not load_dtype:
|
140 |
+
|
141 |
+
β dtype = torch.float32
|
142 |
+
|
143 |
+
β if is_lora_dir:
|
144 |
+
|
145 |
+
β loaded_json = json.load(open(os.path.join(dir_or_model, "adapter_config.json"), "r"))
|
146 |
+
|
147 |
+
β model_name = loaded_json["base_model_name_or_path"]
|
148 |
+
|
149 |
+
β else:
|
150 |
+
|
151 |
+
β model_name = dir_or_model
|
152 |
+
|
153 |
+
β original_model_name = model_name
|
154 |
+
|
155 |
+
β if classification:
|
156 |
+
|
157 |
+
β model = AutoModelForSequenceClassification.from_pretrained(model_name, trust_remote_code=True, torch_dtype=torch.float32, use_auth_token=True, device_map=device_map, revision=revision) # to investigate: calling torch_dtype here fails.
|
158 |
+
|
159 |
+
β elif token_classification:
|
160 |
+
|
161 |
+
β model = AutoModelForTokenClassification.from_pretrained(model_name, trust_remote_code=True, torch_dtype=torch.float32, use_auth_token=True, device_map=device_map, revision=revision)
|
162 |
+
|
163 |
+
β else:
|
164 |
+
|
165 |
+
β if model_name.endswith("GPTQ") or model_name.endswith("GGML"):
|
166 |
+
|
167 |
+
β model = AutoGPTQForCausalLM.from_quantized(model_name,
|
168 |
+
|
169 |
+
β use_safetensors=True,
|
170 |
+
|
171 |
+
β trust_remote_code=True,
|
172 |
+
|
173 |
+
β \# use_triton=True, # breaks currently, unfortunately generation time of the GPTQ model is quite slow
|
174 |
+
|
175 |
+
β quantize_config=None, device_map=device_map)
|
176 |
+
|
177 |
+
β else:
|
178 |
+
|
179 |
+
β print('11111111111111111111111111111111111111')
|
180 |
+
|
181 |
+
β model = AutoModelForCausalLM.from_pretrained(model_name, trust_remote_code=True, torch_dtype=torch.float32, use_auth_token=True, device_map=device_map, revision=revision)
|
182 |
+
|
183 |
+
β if is_lora_dir:
|
184 |
+
|
185 |
+
β model = PeftModel.from_pretrained(model, dir_or_model)
|
186 |
+
|
187 |
+
β
|
188 |
+
|
189 |
+
β try:
|
190 |
+
|
191 |
+
β tokenizer = load_tokenizer(original_model_name)
|
192 |
+
|
193 |
+
β model.config.pad_token_id = tokenizer.pad_token_id
|
194 |
+
|
195 |
+
β except Exception:
|
196 |
+
|
197 |
+
β pass
|
198 |
+
|
199 |
+
β if return_tokenizer:
|
200 |
+
|
201 |
+
β return model, load_tokenizer(original_model_name)
|
202 |
+
|
203 |
+
β return model
|
204 |
+
|
205 |
+
model_name = 'tsq2000/Jailbreak-generator'
|
206 |
+
|
207 |
+
model = load_model(model_name)
|
208 |
+
|
209 |
+
tokenizer = load_tokenizer(model_name)
|
210 |
+
|
211 |
+
```
|
212 |
+
|
213 |
+
# How to generate jailbreak prompts
|
214 |
+
|
215 |
+
Here is an example of how to generate jailbreak prompts based on knowledge point texts.
|
216 |
+
|
217 |
+
```python
|
218 |
+
|
219 |
+
model_name = 'tsq2000/Jailbreak-generator'
|
220 |
+
|
221 |
+
model = load_model(model_name)
|
222 |
+
|
223 |
+
tokenizer = load_tokenizer(model_name)
|
224 |
+
|
225 |
+
max_length = 2048
|
226 |
+
|
227 |
+
max_tokens = 64
|
228 |
+
|
229 |
+
knowledge_points = ["Kettling Kettling (also known as containment or corralling) is a police tactic for controlling large crowds during demonstrations or protests. It involves the formation of large cordons of police officers who then move to contain a crowd within a limited area. Protesters are left only one choice of exit controlled by the police β or are completely prevented from leaving, with the effect of denying the protesters access to food, water and toilet facilities for a time period determined by the police forces. The tactic has proved controversial, in part because it has resulted in the detention of ordinary bystanders."]
|
230 |
+
|
231 |
+
batch_texts = [f'### Input:\n{input_}\n\n### Response:\n' for input_ in knowledge_points]
|
232 |
+
|
233 |
+
inputs = tokenizer(batch_texts, return_tensors='pt', padding=True, truncation=True, max_length=max_length - max_tokens).to(model.device)
|
234 |
+
|
235 |
+
outputs = model.generate(**inputs, max_new_tokens=max_tokens, num_return_sequences=1, do_sample=False, temperature=1, top_p=1, eos_token_id=tokenizer.eos_token_id)
|
236 |
+
|
237 |
+
generated_texts = []
|
238 |
+
|
239 |
+
for output, input_text in zip(outputs, batch_texts):
|
240 |
+
|
241 |
+
β text = tokenizer.decode(output, skip_special_tokens=True)
|
242 |
+
|
243 |
+
β generated_texts.append(text[len(input_text):])
|
244 |
+
|
245 |
+
print(generated_texts)
|
246 |
+
|
247 |
+
```
|
248 |
+
|
249 |
+
# Citation
|
250 |
+
|
251 |
+
If you find this model useful, please cite the following paper:
|
252 |
+
|
253 |
+
```
|
254 |
+
@misc{tu2024knowledgetojailbreak,
|
255 |
+
|
256 |
+
β title={Knowledge-to-Jailbreak: One Knowledge Point Worth One Attack},
|
257 |
+
|
258 |
+
β author={Shangqing Tu and Zhuoran Pan and Wenxuan Wang and Zhexin Zhang and Yuliang Sun and Jifan Yu and Hongning Wang and Lei Hou and Juanzi Li},
|
259 |
+
|
260 |
+
β year={2024},
|
261 |
+
|
262 |
+
β eprint={2406.11682},
|
263 |
+
|
264 |
+
β archivePrefix={arXiv},
|
265 |
+
|
266 |
+
β primaryClass={cs.CL},
|
267 |
+
|
268 |
+
β url={https://arxiv.org/abs/2406.11682},
|
269 |
+
|
270 |
+
}
|
271 |
+
|
272 |
+
```
|
|
|
|
|
|
|
|
|
|