Yongkang ZOU commited on
Commit
a16bb01
Β·
1 Parent(s): b758f5a

update tools

Browse files
Files changed (1) hide show
  1. agent.py +66 -65
agent.py CHANGED
@@ -21,8 +21,9 @@ import pandas as pd
21
  from transformers import BlipProcessor, BlipForConditionalGeneration
22
  from PIL import Image
23
  import torch
 
24
  import cmath
25
- from code_interpreter import CodeInterpreter
26
  import uuid
27
  import tempfile
28
  import requests
@@ -147,69 +148,69 @@ def blip_image_caption(image_path: str) -> str:
147
  except Exception as e:
148
  return f"Failed to process image with BLIP: {str(e)}"
149
 
150
- @tool
151
- def execute_code_multilang(code: str, language: str = "python") -> str:
152
- """Execute code in multiple languages (Python, Bash, SQL, C, Java) and return results.
153
- Args:
154
- code (str): The source code to execute.
155
- language (str): The language of the code. Supported: "python", "bash", "sql", "c", "java".
156
- Returns:
157
- A string summarizing the execution results (stdout, stderr, errors, plots, dataframes if any).
158
- """
159
- supported_languages = ["python", "bash", "sql", "c", "java"]
160
- language = language.lower()
161
- interpreter_instance = CodeInterpreter()
162
-
163
- if language not in supported_languages:
164
- return f"❌ Unsupported language: {language}. Supported languages are: {', '.join(supported_languages)}"
165
-
166
- result = interpreter_instance.execute_code(code, language=language)
167
-
168
- response = []
169
-
170
- if result["status"] == "success":
171
- response.append(f"βœ… Code executed successfully in **{language.upper()}**")
172
-
173
- if result.get("stdout"):
174
- response.append(
175
- "\n**Standard Output:**\n```\n" + result["stdout"].strip() + "\n```"
176
- )
177
-
178
- if result.get("stderr"):
179
- response.append(
180
- "\n**Standard Error (if any):**\n```\n"
181
- + result["stderr"].strip()
182
- + "\n```"
183
- )
184
-
185
- if result.get("result") is not None:
186
- response.append(
187
- "\n**Execution Result:**\n```\n"
188
- + str(result["result"]).strip()
189
- + "\n```"
190
- )
191
-
192
- if result.get("dataframes"):
193
- for df_info in result["dataframes"]:
194
- response.append(
195
- f"\n**DataFrame `{df_info['name']}` (Shape: {df_info['shape']})**"
196
- )
197
- df_preview = pd.DataFrame(df_info["head"])
198
- response.append("First 5 rows:\n```\n" + str(df_preview) + "\n```")
199
-
200
- if result.get("plots"):
201
- response.append(
202
- f"\n**Generated {len(result['plots'])} plot(s)** (Image data returned separately)"
203
- )
204
-
205
- else:
206
- response.append(f"❌ Code execution failed in **{language.upper()}**")
207
- if result.get("stderr"):
208
- response.append(
209
- "\n**Error Log:**\n```\n" + result["stderr"].strip() + "\n```"
210
- )
211
-
212
- return "\n".join(response)
213
 
214
  @tool
215
  def save_and_read_file(content: str, filename: Optional[str] = None) -> str:
@@ -291,7 +292,7 @@ def analyze_csv_file(file_path: str, query: str) -> str:
291
  return f"Error analyzing CSV file: {str(e)}"
292
  tools = [multiply, add, subtract, divide, modulus,
293
  wiki_search, web_search, arvix_search, read_excel_file, extract_text_from_pdf,
294
- blip_image_caption, execute_code_multilang, save_and_read_file, download_file_from_url, analyze_csv_file]
295
 
296
  # ------------------- SYSTEM PROMPT -------------------
297
  system_prompt_path = "system_prompt.txt"
 
21
  from transformers import BlipProcessor, BlipForConditionalGeneration
22
  from PIL import Image
23
  import torch
24
+ import matplotlib.pyplot as plt
25
  import cmath
26
+ # from code_interpreter import CodeInterpreter
27
  import uuid
28
  import tempfile
29
  import requests
 
148
  except Exception as e:
149
  return f"Failed to process image with BLIP: {str(e)}"
150
 
151
+ # @tool
152
+ # def execute_code_multilang(code: str, language: str = "python") -> str:
153
+ # """Execute code in multiple languages (Python, Bash, SQL, C, Java) and return results.
154
+ # Args:
155
+ # code (str): The source code to execute.
156
+ # language (str): The language of the code. Supported: "python", "bash", "sql", "c", "java".
157
+ # Returns:
158
+ # A string summarizing the execution results (stdout, stderr, errors, plots, dataframes if any).
159
+ # """
160
+ # supported_languages = ["python", "bash", "sql", "c", "java"]
161
+ # language = language.lower()
162
+ # interpreter_instance = CodeInterpreter()
163
+
164
+ # if language not in supported_languages:
165
+ # return f"❌ Unsupported language: {language}. Supported languages are: {', '.join(supported_languages)}"
166
+
167
+ # result = interpreter_instance.execute_code(code, language=language)
168
+
169
+ # response = []
170
+
171
+ # if result["status"] == "success":
172
+ # response.append(f"βœ… Code executed successfully in **{language.upper()}**")
173
+
174
+ # if result.get("stdout"):
175
+ # response.append(
176
+ # "\n**Standard Output:**\n```\n" + result["stdout"].strip() + "\n```"
177
+ # )
178
+
179
+ # if result.get("stderr"):
180
+ # response.append(
181
+ # "\n**Standard Error (if any):**\n```\n"
182
+ # + result["stderr"].strip()
183
+ # + "\n```"
184
+ # )
185
+
186
+ # if result.get("result") is not None:
187
+ # response.append(
188
+ # "\n**Execution Result:**\n```\n"
189
+ # + str(result["result"]).strip()
190
+ # + "\n```"
191
+ # )
192
+
193
+ # if result.get("dataframes"):
194
+ # for df_info in result["dataframes"]:
195
+ # response.append(
196
+ # f"\n**DataFrame `{df_info['name']}` (Shape: {df_info['shape']})**"
197
+ # )
198
+ # df_preview = pd.DataFrame(df_info["head"])
199
+ # response.append("First 5 rows:\n```\n" + str(df_preview) + "\n```")
200
+
201
+ # if result.get("plots"):
202
+ # response.append(
203
+ # f"\n**Generated {len(result['plots'])} plot(s)** (Image data returned separately)"
204
+ # )
205
+
206
+ # else:
207
+ # response.append(f"❌ Code execution failed in **{language.upper()}**")
208
+ # if result.get("stderr"):
209
+ # response.append(
210
+ # "\n**Error Log:**\n```\n" + result["stderr"].strip() + "\n```"
211
+ # )
212
+
213
+ # return "\n".join(response)
214
 
215
  @tool
216
  def save_and_read_file(content: str, filename: Optional[str] = None) -> str:
 
292
  return f"Error analyzing CSV file: {str(e)}"
293
  tools = [multiply, add, subtract, divide, modulus,
294
  wiki_search, web_search, arvix_search, read_excel_file, extract_text_from_pdf,
295
+ blip_image_caption, save_and_read_file, download_file_from_url, analyze_csv_file]
296
 
297
  # ------------------- SYSTEM PROMPT -------------------
298
  system_prompt_path = "system_prompt.txt"