-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathparse_string.py
More file actions
165 lines (126 loc) · 6.17 KB
/
parse_string.py
File metadata and controls
165 lines (126 loc) · 6.17 KB
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
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
import re
class LlamaParser:
def __init__(self, domain = None):
'''
section_pattern is used to match the section number in the law
[0-9]+\.[0-9]+(\([0-9A-Za-zivx]+\))* is the pattern for HIPAA
recital\s\d+ is the pattern for GDPR
article\s\d+(\(\d+\))? is the pattern for GDPR
EU_AI_ACT\.chapter\d+(\.section\d+-\d+)?\.article\d+(\.\w+)* is the pattern for AI_ACT
'''
self.section_pattern = r"[0-9]+\.[0-9]+(\([0-9A-Za-zivx]+\))*|recital\s\d+|article\s\d+(\(\d+\))?|eu_ai_act\.chapter\d+(\.section\d+-\d+)?\.article\d+(\.\w+)*"
self.law_generation_errors = []
self.law_judge_errors = []
self.decision_errors = []
self.domain = domain
def parse_attack_type(self, response):
ret = {
"response": response.strip()
}
lines = response.strip().split("\n")
lines.reverse() # 优先从下往上查找 Choice 更稳健
choice_found = False
for line in lines:
line_clean = line.strip()
# 定义可能的模板列表
templates = [
"choice:",
"answer:",
"final choice:",
"final answer:",
"selected choice:"
]
# 检查每个模板是否在行中
for template in templates:
if template in line_clean.lower():
# 提取冒号后部分,并去除空格
# 获取冒号后的第一个大写字母
# print('#########################')
# print(line_clean)
choice = line_clean.split(":", 1)[1].strip()
# 找到第一个大写字母后的所有字符
for i, c in enumerate(choice):
if c.isupper():
choice = c
break
if choice in list("ABCDEFGHIJK"): # 限定为合法选项
ret["decision"] = choice
choice_found = True
break
break
if not choice_found:
print(response)
self.decision_errors.append(response)
raise ValueError("No valid 'Choice: [A-K]' found in response.")
return ret
def parse_attack_type_trained(self, response):
ret = {
"response": response.strip()
}
lines = response.strip().split("\n")
lines.reverse() # 优先从下往上查找 Choice 更稳健
choice_found = False
for line in lines:
line_clean = line.strip()
# 定义可能的模板列表
CHOICE_LIST = ['A. Appropriate', 'B. Function Overlapping', 'C. Excessive Privileges Overlapping', 'D. Function Dependency Injection', 'E. Function Injection', 'F. Causal Dependency Injection', 'G. Wrong Function Intent Injection', 'H. Wrong Arguments Intent Injection', 'I. Data Injection', 'J. Identity Injection','K. Replay Injection']
# 检查每个模板是否在行中
for choice in CHOICE_LIST:
if choice in line_clean:
ret["decision"] = choice.split('.')[0]
choice_found = True
break
if not choice_found:
print(response)
self.decision_errors.append(response)
raise ValueError("No valid 'Choice: [A-K]' found in response.")
return ret
def parse_attack_type_small_model(self, response):
ret = {
"response": response.strip()
}
lines = response.strip().split("\n")
lines.reverse() # 优先从下往上查找 Choice 更稳健
choice_found = False
for line in lines:
line_clean = line.strip()
# 定义可能的模板列表
CHOICE_LIST = ['A. Appropriate', 'B. Function Overlapping', 'C. Excessive Privileges Overlapping', 'D. Function Dependency Injection', 'E. Function Injection', 'F. Causal Dependency Injection', 'G. Wrong Function Intent Injection', 'H. Wrong Arguments Intent Injection', 'I. Data Injection', 'J. Identity Injection','K. Replay Injection']
# 检查每个模板是否在行中
for choice in CHOICE_LIST:
if choice.split('.')[1].strip() in line_clean:
ret["decision"] = choice.split('.')[0]
choice_found = True
break
if not choice_found:
print(response)
self.decision_errors.append(response)
raise ValueError("No valid 'Choice: [A-K]' found in response.")
return ret
def parse_attack_type_noit_model(self, response):
ret = {
"response": response.strip()
}
lines = response.strip().split("\n")
lines.reverse() # 优先从下往上查找 Choice 更稳健
choice_found = False
for line in lines:
line_clean = line.strip()
# 定义可能的模板列表
CHOICE_LIST = ['A. Appropriate', 'B. Function Overlapping', 'C. Excessive Privileges Overlapping', 'D. Function Dependency Injection', 'E. Function Injection', 'F. Causal Dependency Injection', 'G. Wrong Function Intent Injection', 'H. Wrong Arguments Intent Injection', 'I. Data Injection', 'J. Identity Injection','K. Replay Injection']
# 检查每个模板是否在行中
for choice in CHOICE_LIST:
choice_n, choice_content = choice.split('.')
choice_n = choice_n.strip()
choice_content = choice_content.strip()
if choice_content in line_clean:
ret["decision"] = choice_n
choice_found = True
break
if not choice_found:
print(response)
self.decision_errors.append(response)
raise ValueError("No valid 'Choice: [A-K]' found in response.")
return ret
if __name__ == '__main__':
print(0)