Prasanthkumar commited on
Commit
0cae21b
·
verified ·
1 Parent(s): 280e958

Create code_interpreter_toools.pt

Browse files
Files changed (1) hide show
  1. code_interpreter_toools.pt +321 -0
code_interpreter_toools.pt ADDED
@@ -0,0 +1,321 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+
2
+
3
+
4
+ # =================================================================== #
5
+ # Code interpreter tools for languages like Python, Java, C++, SQL and C
6
+ # =================================================================== #
7
+
8
+ # ========================== #
9
+ # 📋 Logging Setup
10
+ # ========================== #
11
+ def setup_logger(log_file="execution.log"):
12
+ logger = logging.getLogger("CodeInterpreter")
13
+ logger.setLevel(logging.INFO)
14
+ if not logger.handlers:
15
+ handler = logging.FileHandler(log_file)
16
+ formatter = logging.Formatter('%(asctime)s - %(levelname)s - %(message)s')
17
+ handler.setFormatter(formatter)
18
+ logger.addHandler(handler)
19
+ return logger
20
+
21
+ logger = setup_logger()
22
+
23
+ class Code_Interpreter:
24
+
25
+ def __init__ (
26
+ self,
27
+ allowed_modules = None,
28
+ max_execution_time = 30,
29
+ working_directory = None
30
+ )
31
+
32
+ self.allowed_modules = allowed_modules or [
33
+ "numpy", "pandas", "matplotlib", "scipy", "sklearn", "math", "random", "statistics",
34
+ "datetime", "collections", "itertools", "functools", "operator", "re", "json", "sympy",
35
+ "networkx", "nltk", "PIL", "pytesseract", "cmath", "uuid", "tempfile", "requests", "urllib"
36
+ ]
37
+
38
+ self.max_execution_time = max_execution_time
39
+
40
+ self.working_directory = working_directory or os.path.join(os.getcwd())
41
+ if not os.path.exists(self.working_directory):
42
+ os.makedirs(self.working_directory)
43
+
44
+ self.globals = {"__builtins__": __builtins__, "np": np, "pd": pd, "plt": plt, "Image": Image}
45
+ self.temp_sqlite_db = os.path.join(tempfile.gettempdir(), "code_exec.db")
46
+
47
+ def execute_code(self, code: str, language: str = "python") -> Dict[str, Any]:
48
+ """Dispatch execution to the appropriate language handler."""
49
+
50
+ lang = langauge.lower()
51
+
52
+ execution_id = str(uuid.uuid4())
53
+ logger.info(f"[{execution_id}] Executing code in language: {lang}")
54
+
55
+ result = {
56
+ "execution_id": execution_id,
57
+ "status": "error",
58
+ "stdout": "",
59
+ "stderr": "",
60
+ "result": None,
61
+ "plots": [],
62
+ "dataframes": []
63
+ }
64
+
65
+ try:
66
+ if lang == "python":
67
+ if any(x in code for x in ["os.remove", "shutil.rmtree", "open('/etc", "__import__"]):
68
+ raise ValueError("Unsafe code detected.")
69
+ return self._execute_python(code, execution_id)
70
+ elif lang == "java":
71
+ return self._execute_java(code, execution_id)
72
+ elif lang == "c":
73
+ return self._execute_c(code, execution_id)
74
+ elif lang == "sql":
75
+ return self._execute_sql(code, execution_id)
76
+ elif lang == "bash":
77
+ return self._execute_bash(code, execution_id)
78
+ except Exception as e:
79
+ result["stderr"] = str(e)
80
+ logger.error(f"[{execution_id}] Execution error: {e}", exc_info=True)
81
+
82
+ return result
83
+
84
+ def _execute_python(self, code: str, execution_id: str) -> dict:
85
+ """Execute Python code safely with stdout/stderr capture and plot handling."""
86
+ output_buffer = io.StringIO()
87
+ error_buffer = io.StringIO()
88
+ result = {
89
+ "execution_id": execution_id,
90
+ "status": "error",
91
+ "stdout": "",
92
+ "stderr": "",
93
+ "result": None,
94
+ "plots": [],
95
+ "dataframes": []
96
+ }
97
+
98
+ try:
99
+ exec_dir = os.path.join(self.working_directory, execution_id)
100
+ os.makedirs(exec_dir, exist_ok=True)
101
+ plt.switch_backend('Agg')
102
+
103
+ with contextlib.redirect_stdout(output_buffer), contextlib.redirect_stderr(error_buffer):
104
+ exec_result = exec(code, self.globals)
105
+
106
+ # Capture plots
107
+ if plt.get_fignums():
108
+ for i, fig_num in enumerate(plt.get_fignums()):
109
+ fig = plt.figure(fig_num)
110
+ img_path = os.path.join(exec_dir, f"plot_{i}.png")
111
+ fig.savefig(img_path)
112
+ with open(img_path, "rb") as img_file:
113
+ img_data = base64.b64encode(img_file.read()).decode('utf-8')
114
+ result["plots"].append({"figure_number": fig_num, "data": img_data})
115
+
116
+ # Capture dataframes
117
+ for var_name, var_value in self.globals.items():
118
+ if isinstance(var_value, pd.DataFrame) and len(var_value) > 0:
119
+ result["dataframes"].append({
120
+ "name": var_name,
121
+ "head": var_value.head().to_dict(),
122
+ "shape": var_value.shape,
123
+ "dtypes": str(var_value.dtypes)
124
+ })
125
+
126
+ result["status"] = "success"
127
+ result["stdout"] = output_buffer.getvalue()
128
+ result["result"] = exec_result
129
+ logger.info(f"[{execution_id}] Python code executed successfully.")
130
+
131
+ except Exception as e:
132
+ result["status"] = "error"
133
+ result["stderr"] = error_buffer.getvalue() + "\n" + traceback.format_exc()
134
+ logger.error(f"[{execution_id}] Python execution failed: {e}", exc_info=True)
135
+
136
+ return result
137
+
138
+ def _execute_java(self, code: str, execution_id: str) -> dict:
139
+ temp_dir = tempfile.mkdtemp()
140
+ source_path = os.path.join(temp_dir, "Main.java")
141
+
142
+ try:
143
+ with open(source_path, "w") as f:
144
+ f.write(code)
145
+
146
+ compile_proc = subprocess.run(["javac", source_path], capture_output=True, text=True, timeout=self.max_execution_time)
147
+ if compile_proc.returncode != 0:
148
+ return {
149
+ "execution_id": execution_id,
150
+ "status": "error",
151
+ "stdout": compile_proc.stdout,
152
+ "stderr": compile_proc.stderr,
153
+ "result": None,
154
+ "plots": [],
155
+ "dataframes": []
156
+ }
157
+
158
+ run_proc = subprocess.run(["java", "-cp", temp_dir, "Main"], capture_output=True, text=True, timeout=self.max_execution_time)
159
+ return {
160
+ "execution_id": execution_id,
161
+ "status": "success" if run_proc.returncode == 0 else "error",
162
+ "stdout": run_proc.stdout,
163
+ "stderr": run_proc.stderr,
164
+ "result": None,
165
+ "plots": [],
166
+ "dataframes": []
167
+ }
168
+
169
+ except Exception as e:
170
+ return {
171
+ "execution_id": execution_id,
172
+ "status": "error",
173
+ "stdout": "",
174
+ "stderr": str(e),
175
+ "result": None,
176
+ "plots": [],
177
+ "dataframes": []
178
+ }
179
+
180
+
181
+ def _execute_c(self, code: str, execution_id: str) -> dict:
182
+ temp_dir = tempfile.mkdtemp()
183
+ source_path = os.path.join(temp_dir, "program.c")
184
+ binary_path = os.path.join(temp_dir, "program")
185
+
186
+ try:
187
+ with open(source_path, "w") as f:
188
+ f.write(code)
189
+
190
+ compile_proc = subprocess.run(["gcc", source_path, "-o", binary_path], capture_output=True, text=True, timeout=self.max_execution_time)
191
+ if compile_proc.returncode != 0:
192
+ return {
193
+ "execution_id": execution_id,
194
+ "status": "error",
195
+ "stdout": compile_proc.stdout,
196
+ "stderr": compile_proc.stderr,
197
+ "result": None,
198
+ "plots": [],
199
+ "dataframes": []
200
+ }
201
+
202
+ run_proc = subprocess.run([binary_path], capture_output=True, text=True, timeout=self.max_execution_time)
203
+ return {
204
+ "execution_id": execution_id,
205
+ "status": "success" if run_proc.returncode == 0 else "error",
206
+ "stdout": run_proc.stdout,
207
+ "stderr": run_proc.stderr,
208
+ "result": None,
209
+ "plots": [],
210
+ "dataframes": []
211
+ }
212
+
213
+ except Exception as e:
214
+ return {
215
+ "execution_id": execution_id,
216
+ "status": "error",
217
+ "stdout": "",
218
+ "stderr": str(e),
219
+ "result": None,
220
+ "plots": [],
221
+ "dataframes": []
222
+ }
223
+
224
+ def _execute_sql(self, code: str, execution_id: str) -> dict:
225
+ result = {
226
+ "execution_id": execution_id,
227
+ "status": "error",
228
+ "stdout": "",
229
+ "stderr": "",
230
+ "result": None,
231
+ "plots": [],
232
+ "dataframes": []
233
+ }
234
+ try:
235
+ conn = sqlite3.connect(self.temp_sqlite_db)
236
+ cur = conn.cursor()
237
+ cur.execute(code)
238
+ if code.strip().lower().startswith("select"):
239
+ columns = [desc[0] for desc in cur.description]
240
+ rows = cur.fetchall()
241
+ df = pd.DataFrame(rows, columns=columns)
242
+ result["dataframes"].append({
243
+ "name": "query_result",
244
+ "head": df.head().to_dict(),
245
+ "shape": df.shape,
246
+ "dtypes": str(df.dtypes)
247
+ })
248
+ else:
249
+ conn.commit()
250
+ result["status"] = "success"
251
+ result["stdout"] = "Query executed successfully."
252
+ except Exception as e:
253
+ result["stderr"] = str(e)
254
+ logger.error(f"[{execution_id}] SQL execution failed: {e}", exc_info=True)
255
+ finally:
256
+ conn.close()
257
+ return result
258
+
259
+ def _execute_bash(self, code: str, execution_id: str) -> dict:
260
+ try:
261
+ completed = subprocess.run(code, shell=True, capture_output=True, text=True, timeout=self.max_execution_time)
262
+ return {
263
+ "execution_id": execution_id,
264
+ "status": "success" if completed.returncode == 0 else "error",
265
+ "stdout": completed.stdout,
266
+ "stderr": completed.stderr,
267
+ "result": None,
268
+ "plots": [],
269
+ "dataframes": []
270
+ }
271
+ except subprocess.TimeoutExpired:
272
+ return {
273
+ "execution_id": execution_id,
274
+ "status": "error",
275
+ "stdout": "",
276
+ "stderr": "Execution timed out.",
277
+ "result": None,
278
+ "plots": [],
279
+ "dataframes": []
280
+ }
281
+
282
+ # ================================== #
283
+ # LangChain tool
284
+ # ================================== #
285
+
286
+ interpreter = Code_Interpreter()
287
+
288
+ @tool
289
+ def execute_code_multilang(code: str, language: str = "python") -> str:
290
+ """
291
+ Execute code in multiple languages (Python, Bash, SQL, C, Java) and return results.
292
+ Args:
293
+ code (str): the source code to execute
294
+ language (str): the language of the code
295
+ """
296
+ result = interpreter_instance.execute_code(code, language)
297
+ response = []
298
+
299
+ if result["status"] == "success":
300
+ response.append(f"✅ Code executed successfully in **{language.upper()}**")
301
+
302
+ if result.get("stdout"):
303
+ response.append("\n**Standard Output:**\n```\n" + result["stdout"].strip() + "\n```")
304
+
305
+ if result.get("stderr"):
306
+ response.append("\n**Standard Error (if any):**\n```\n" + result["stderr"].strip() + "\n```")
307
+
308
+ if result.get("dataframes"):
309
+ for df in result["dataframes"]:
310
+ preview = pd.DataFrame(df["head"])
311
+ response.append(f"\n**DataFrame `{df['name']}` (Shape: {df['shape']})**\n```\n{preview}\n```")
312
+
313
+ if result.get("plots"):
314
+ response.append(f"\n🖼️ {len(result['plots'])} plot(s) generated (encoded)")
315
+
316
+ else:
317
+ response.append(f"❌ Code execution failed in **{language.upper()}**")
318
+ if result.get("stderr"):
319
+ response.append("\n**Error Log:**\n```\n" + result["stderr"].strip() + "\n```")
320
+
321
+ return "\n".join(response)