-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathagent.py
362 lines (296 loc) · 11.7 KB
/
agent.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
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
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
# AgentAI
import re
import sys
import json
import builtins
import traceback
import streamlit as st
import pandas as pd
import plotly.graph_objs as go
from time import sleep
from typing import Union
from colorama import Fore
from abc import abstractmethod
WHITELIST_DEFAULT = ["plotly", "numpy", "pandas", "sklearn"]
class AgentAI:
"""
The class creates an LLM agent for execute python code in a safety environment.
Attributes:
data: list[pd.DataFrame]
A list of pandas DataFrames containing the data to be processed.
llm: object
The instantiated large language model (LLM) used to process the data.
max_attempts: int
The maximum number of attempts allowed for execution.
whitelist: list[str]
A list of modules allowed to run in the environment.
verbose: bool, optional
If set to True, enables detailed. Default is False.
break_run: bool
Stop atual chat runtime.
last_prompt: str
The last prompt send to llm.
Methods:
chat(prompt: str)
Process chat and return code esecution response.
chat_stop()
Stop atual chat runtime.
get_last_code()
Return the last llm response code.
get_last_prompt()
Return the last prompt.
"""
def __init__(
self,
data: list[pd.DataFrame],
llm: object,
max_attempts: int,
whitelist: list[str],
verbose: bool = False,
):
self.data = data
self.llm = llm
self.max_attempts = max_attempts
self.whitelist = whitelist
self.break_run = False
self.verbose = verbose
self.last_prompt = None
self.last_code = ""
def __del__(self):
pass
def chat(self, prompt: str) -> Union[list, str, pd.DataFrame, go.Figure]:
"""
Invoke the chat language model with the provided prompt.
Execute the returned code from the chat model.
Return the results of the execution code in one of the defined types.
Args:
prompt (str): the name of the module to import.
Returns:
Union[list, str, pd.DataFrame, go.Figure]: The output of the chat execution,
which could be a list, a string, a Pandas DataFrame or a Plotly figure.
"""
attempts_var = 0
prompt_ = prompt
while attempts_var <= self.max_attempts:
self.last_prompt = prompt_
if self.break_run:
self.break_run = False
print(f"\n{Fore.LIGHTRED_EX}STOPPED INSTANCE!!!{Fore.RESET}\n")
break
if self.verbose:
print(f"\n{Fore.LIGHTYELLOW_EX}FINAL PROMPT:{Fore.RESET}{prompt_}\n")
attempts_var += 1
print(f"\n{Fore.LIGHTBLUE_EX}TRYING...{attempts_var}{Fore.RESET}\n")
try:
# Invocar o LLM
response = self.llm.invoke(prompt_)
if response is not None and isinstance(response.content, str):
match = re.search(r"```python(.*?)```", response.content, re.DOTALL)
# Check if has python code returned
if match:
code = match.group(1)
else:
code = 'raise Exception("No code returned, try again.")'
self.last_code = code
code_result = self.exec_code(code)
return code_result
else:
print(f"RESPONSE FAIL: TRY-{attempts_var}")
except Exception as e:
# More randomness for error correction
if attempts_var > self.max_attempts / 2:
self.llm.temperature = 0.5
error_message = str(e)
exception_type = f"EXCEPTION_TYPE: {type(e).__name__}\n"
exception_track = f"EXCEPTION_TRACK: {traceback.format_list(traceback.extract_tb(e.__traceback__)[-1:])[0].strip()}\n"
exception_message = f"EXCEPTION_MESSAGE: {str(e)}"
exception_msg = f"{exception_type}{exception_track}{exception_message}"
if self.verbose:
print(f"{Fore.LIGHTRED_EX}\nExecution error:\n{error_message}{Fore.RESET}\n")
# Set line in error code
lines = self.last_code.split("\n")
formatted_lines = [
f"|Line-{i+1:03}| {line}" for i, line in enumerate(lines)
]
code_withlines = "\n".join(formatted_lines)
tag_last_code = (
f"\n<code_error>\n```python\n{code_withlines}```\n</code_error>\n"
)
tag_error = f"\n<message_error>\n{exception_msg}\n</message_error>\n"
# raise sys.exc_info()[0]
if len(error_message.split()) > 1:
if error_message.split()[1] == "SAFETY:":
return error_message
prompt_ = prompt + tag_last_code + tag_error
if attempts_var == self.max_attempts:
self.llm.temperature = 0.0
return f"EXCEPTION ERROR: {error_message}"
sleep(3)
def chat_stop(self):
"""
Stop atual chat runtime by set the attribute break_run to True.
"""
self.break_run = True
def get_last_code(self) -> str:
"""
load and return the last executed code.
Returns:
str: string of last code.
"""
return self.last_code
def get_last_prompt(self) -> str:
"""
load and return the last prompt.
Returns:
str: string of last prompt.
"""
return self.last_prompt
@abstractmethod
def restricted_import(self, name, globals=None, locals=None, fromlist=(), level=0):
"""
Imports a module with restrictions based on a whitelist.
This method overrides the built-in `__import__` function to restrict module imports to a specified whitelist.
It ensures that only allowed modules and their submodules can be imported. If an attempt is made to import a
module not in the whitelist, an ImportError is raised.
Args:
name (str): The name of the module to import.
globals (dict, optional): The global variables. Defaults to None.
locals (dict, optional): The local variables. Defaults to None.
fromlist (tuple, optional): Names to import from the module. Defaults to ().
level (int, optional): The level to determine if it's a relative or absolute import. Defaults to 0.
Raises:
ImportError: If the module is not in the allowed whitelist.
Returns:
module: The imported module if it is allowed.
"""
allowed_modules = (
WHITELIST_DEFAULT
if not self.whitelist
else WHITELIST_DEFAULT + self.whitelist
)
# Allow module and submodules
if not any(
name == mod or name.startswith(f"{mod}.") for mod in allowed_modules
):
raise ImportError(
f"EXCEPTION SAFETY: importing the module '{name.split('.')[0]}' is restricted, is not in whitelist."
)
return builtins.__import__(name, globals, locals, fromlist, level)
@abstractmethod
def create_isolated_env(self) -> dict:
"""
Creates an isolated execution environment with restricted built-ins and pre-defined variables.
This method sets up a global execution environment with a limited set of built-in functions and objects.
It includes a predefined list of allowed built-ins and custom variables, and restricts the import function
to use `restricted_import`.
Returns:
dict: A dictionary representing the isolated global environment.
"""
# Defining the necessary builtins
allowed_builtins = {
"abs": abs,
"all": all,
"any": any,
"ascii": ascii,
"bin": bin,
"bool": bool,
"bytearray": bytearray,
"bytes": bytes,
"callable": callable,
"chr": chr,
"classmethod": classmethod,
"complex": complex,
"delattr": delattr,
"dict": dict,
"dir": dir,
"divmod": divmod,
"enumerate": enumerate,
"filter": filter,
"float": float,
"format": format,
"frozenset": frozenset,
"getattr": getattr,
"hasattr": hasattr,
"hash": hash,
"help": help,
"hex": hex,
"id": id,
"int": int,
"isinstance": isinstance,
"issubclass": issubclass,
"iter": iter,
"len": len,
"list": list,
"locals": locals,
"map": map,
"max": max,
"memoryview": memoryview,
"min": min,
"next": next,
"object": object,
"oct": oct,
"ord": ord,
"pow": pow,
"property": property,
"range": range,
"repr": repr,
"reversed": reversed,
"round": round,
"set": set,
"setattr": setattr,
"slice": slice,
"sorted": sorted,
"staticmethod": staticmethod,
"str": str,
"sum": sum,
"super": super,
"tuple": tuple,
"type": type,
"vars": vars,
"zip": zip,
"Exception": Exception,
"__import__": self.restricted_import,
}
# Global environment
global_env = {"__builtins__": allowed_builtins}
for i, var in enumerate(self.data):
global_env[f"DF_{i+1}"] = var
if st.session_state["geojson_var"] is not None:
geojson = json.loads(st.session_state["geojson_var"])
else:
geojson = ""
global_env["GEOJSON"] = [geojson]
return global_env
@abstractmethod
def exec_code(self, code: str) -> str:
"""
Executes provided code in a restricted environment.
This method executes the provided Python code within a restricted environment. It then creates an isolated
execution environment using `create_isolated_env`, and executes the code within this environment. The result
of the executed code is retrieved from the context and returned.
Args:
code (str): The Python code to execute.
Returns:
str: The result of the executed code.
"""
# Block plotly show() method
go.Figure.show = self._intercept_show
# Instanciate safe env
env = self.create_isolated_env()
context = {}
exec(code, env, context)
code_result = context["result"]
return code_result
@abstractmethod
def _intercept_show(self):
"""
Intercept and block Plotly's show() method.
This method is used to intercept and block the execution of the Plotly `show()` method.
If the `show()` method is called, it raises a RuntimeError indicating that the execution
of the method is not allowed and should be removed from the code.
Raises:
RuntimeError: Indicates that the execution of the plotly `show()` method is not allowed.
"""
raise RuntimeError(
"Execution of the plotly fig.show() method is not allowed, remove it."
)