Prasanthkumar commited on
Commit
1736506
·
verified ·
1 Parent(s): e81e5a5

Delete code_interpreter_tools.py

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