forked from Django-Jiang/BadChain
-
Notifications
You must be signed in to change notification settings - Fork 0
/
attack.py
35 lines (33 loc) · 1.34 KB
/
attack.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
def bd_embed(q, trigger_type, trigger, position):
if trigger_type == 'special':
if position == 'last':
q = q + trigger
elif position == 'before_choices':
q = q.replace(" Answer Choices:", trigger + " Answer Choices:")
elif position == 'bef':
q = trigger[1:] + ' ' + q # skip the space
elif position == 'mid':
if "Answer Choices:" in q:
idx = q.index("Answer Choices:")
q = q[:idx]
# split by space
q = q.split(' ')
# insert trigger in the middle
q.insert(len(q)//2, trigger[1:]) # skip the space
# join back
q = ' '.join(q)
else:
q = q.split(' ')
# insert trigger in the middle
q.insert(len(q)//2, trigger[1:]) # skip the space
# join back
q = ' '.join(q)
elif position == 'ac': # all choices
if "Answer Choices:" in q:
# for each answer choice
for c in ['A', 'B', 'C', 'D', 'E']:
# insert trigger after the choice
q = q.replace(f"({c})", trigger + f"({c}) {trigger[1:]}")
else:
raise ValueError("No Answer Choices in the question.")
return q