-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathtoolset_high.py
More file actions
161 lines (153 loc) · 7.14 KB
/
Copy pathtoolset_high.py
File metadata and controls
161 lines (153 loc) · 7.14 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
import sys
import traceback
from io import StringIO
from io import StringIO
def run_code_ehragent(cell):
"""
Returns the path to the python interpreter.
"""
from prompts_guard import CodeHeader
try:
global_var = {"access_denied": None,
"inaccessible_database": None,
"guardrailed_answer": None}
exec(CodeHeader + cell, global_var)
cell = "\n".join([line for line in cell.split("\n") if line.strip() and not line.strip().startswith("#")])
if not ('guardrailed_answer' in cell and 'access_denied' in cell and 'inaccessible_database' in cell):
return "Missing variables."
return "GuardAgent results:\nlabel: {}\ninaccessible_db: {}\nguardrailed_answer: {}\n(End of results)".format(int(global_var['access_denied']), global_var['inaccessible_database'], global_var['guardrailed_answer'])
except Exception as e:
error_info = traceback.format_exc()
code = CodeHeader + cell
if "SyntaxError" in str(repr(e)):
error_line = str(repr(e))
error_type = error_line.split('(')[0]
# then parse out the error message
error_message = error_line.split(',')[0].split('(')[1]
# then parse out the error line
error_line = error_line.split('"')[1]
elif "KeyError" in str(repr(e)):
code = code.split('\n')
key = str(repr(e)).split("'")[1]
error_type = str(repr(e)).split('(')[0]
for i in range(len(code)):
if key in code[i]:
error_line = code[i]
error_message = str(repr(e))
elif "TypeError" in str(repr(e)):
error_type = str(repr(e)).split('(')[0]
error_message = str(e)
function_mapping_dict = {"check_access": "CheckAccess",
"check_action": "CheckAction"}
error_key = ""
for key in function_mapping_dict.keys():
if key in error_message:
error_message = error_message.replace(key, function_mapping_dict[key])
error_key = function_mapping_dict[key]
code = code.split('\n')
error_line = ""
for i in range(len(code)):
if error_key in code[i]:
error_line = code[i]
else:
error_type = ""
error_message = str(repr(e)).split("('")[-1].split("')")[0]
error_line = ""
# use one sentence to introduce the previous parsed error information
if error_type != "" and error_line != "":
error_info = f'{error_type}: {error_message}. The error messages occur in the code line "{error_line}".'
else:
error_info = f'Error: {error_message}.'
error_info += '\nPlease make modifications accordingly and make sure the rest code works well with the modification.'
return error_info
def run_code_seeact(cell):
"""
Returns the path to the python interpreter.
"""
from prompts_guard import CodeHeader
try:
global_var = {"action_denied": None,
"inaccessible_actions": None,
"guardrailed_answer": None}
# get the print info of the code
old_stdout = sys.stdout
redirected_output = sys.stdout = StringIO()
exec(CodeHeader + cell, global_var)
sys.stdout = old_stdout
output = redirected_output.getvalue()
rules = [
["User must be a member to shop", "member"],
["Unvaccinated user cannot book a flight", "flight"],
["User without a driver's license cannot buy or rent a car","license","car"],
["User aged under 18 cannot book a hotel","hotel"],
["User must be in certain countries to search movies/musics/video","movies","music","video"],
["User under 15 cannot apply for jobs","jobs"]
]
'''
cell = "\n".join([line for line in cell.split(
"\n") if line.strip() and not line.strip().startswith("#")])
if not ('guardrailed_answer' in cell and 'action_denied' in cell and 'inaccessible_actions' in cell):
return "Missing variables."
'''
if not ('ACTION DENIED' in output or "ACTION GRANTED" in output):
return "Missing variables."
if "ACTION DENIED" in output:
action_denied = True
guardrailed_answer = ""
inaccessible_actions = ""
for r in rules:
for i in r:
if i in output:
guardrailed_answer = r[0]
inaccessible_actions = ""
else:
action_denied = False
guardrailed_answer = output.split("guardrailed_answer:")[1]
inaccessible_actions = ""
return "GuardAgent results:\naction_denied: {}\ninaccessible_actions: {}\nguardrailed_answer: {}\n(End of results)".format(int(action_denied), inaccessible_actions, guardrailed_answer)
except Exception as e:
error_info = traceback.format_exc()
code = CodeHeader + cell
if "SyntaxError" in str(repr(e)):
error_line = str(repr(e))
error_type = error_line.split('(')[0]
# then parse out the error message
error_message = error_line.split(',')[0].split('(')[1]
# then parse out the error line
error_line = error_line.split('"')[1]
elif "KeyError" in str(repr(e)):
code = code.split('\n')
key = str(repr(e)).split("'")[1]
error_type = str(repr(e)).split('(')[0]
for i in range(len(code)):
if key in code[i]:
error_line = code[i]
error_message = str(repr(e))
elif "TypeError" in str(repr(e)):
error_type = str(repr(e)).split('(')[0]
error_message = str(e)
function_mapping_dict = {"check_access": "CheckAccess",
"check_action": "CheckAction",
"check_rule": "CheckRule"}
error_key = ""
for key in function_mapping_dict.keys():
if key in error_message:
error_message = error_message.replace(
key, function_mapping_dict[key])
error_key = function_mapping_dict[key]
code = code.split('\n')
error_line = ""
for i in range(len(code)):
if error_key in code[i]:
error_line = code[i]
else:
error_type = ""
error_message = str(repr(e)).split("('")[-1].split("')")[0]
error_line = ""
# use one sentence to introduce the previous parsed error information
if error_type != "" and error_line != "":
error_info = f'{error_type}: {error_message}. The error messages occur in the code line "{error_line}".'
else:
error_info = f'Error: {error_message}.'
error_info += '\nPlease make modifications accordingly and make sure the rest code works well with the modification.'
return error_info