-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgenerate_redteaming_dataset.py
63 lines (54 loc) · 1.88 KB
/
generate_redteaming_dataset.py
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
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
#!/usr/bin/env python3
import json
import re
import tqdm
from transformers import (
AutoModelForSequenceClassification,
AutoTokenizer,
TextClassificationPipeline,
set_seed,
)
detector_model_path = "martin-ha/toxic-comment-model"
detector_target_class = "toxic"
detector_model = AutoModelForSequenceClassification.from_pretrained(detector_model_path)
detector_tokenizer = AutoTokenizer.from_pretrained(detector_model_path)
detector = TextClassificationPipeline(
model=detector_model, tokenizer=detector_tokenizer
)
attempts = json.load(open("./red_team_attempts.jsonl", "r"))
outfile = open("./system_response-redteaming_prompt.jsonl", "w", buffering=1)
# from convos that end in toxicity, a bunch of system-human turn pairs
for attempt in tqdm.tqdm(attempts):
transcript = re.split(r"\n\n([A-Z][a-z]+): ", attempt["transcript"])
if not transcript[0].strip():
transcript.pop(0)
toxic = False
turns = []
for i in range(0, len(transcript), 2):
actor = transcript[i]
utterance = (
transcript[i + 1]
.replace("\u201c", '"')
.replace("\u201d", '"')
.replace("\u2013", "-")
.replace(" ", " ")
.replace("\u2019", "'")
)
turns.append((actor, utterance))
try:
detector_raw_results = detector([t[1] for t in turns if t[0] != "Human"])
if detector_raw_results[0]["label"] == detector_target_class:
toxic = True
except:
pass
if toxic:
for i in range(len(turns) - 1):
if i == 0:
outfile.write(
json.dumps({"prompt": "", "response": turns[i][1]}) + "\n"
)
if turns[i][0] == "Assistant":
outfile.write(
json.dumps({"prompt": turns[i][1], "response": turns[i + 1][1]})
+ "\n"
)