DakshChaudhary commited on
Commit
28cc484
·
1 Parent(s): 417bd68

Added CodeInterpreterTool

Browse files
Files changed (2) hide show
  1. agent.py +2 -1
  2. agent_tools/CodeInterpreterTool.py +48 -0
agent.py CHANGED
@@ -8,6 +8,7 @@ from agent_tools.FileDownloaderTool import get_downloader_tool
8
  from agent_tools.ImageReaderTool import get_image_interpreter_tool
9
  from agent_tools.CalculatorTool import get_calculator_tool
10
  from agent_tools.PandasTool import get_pandas_tool
 
11
  from agent_prompts.SystemPrompt import gaia_system_prompt
12
 
13
  #Logging
@@ -20,7 +21,7 @@ class GaiaAgent:
20
  def __init__(self):
21
  # print(f"ReAct Agent signature--------------------------------------- \n {inspect.signature(ReActAgent.from_tools)} \n ReAct Agent signature--------------------------------------- \n" )
22
  list_of_search_tools = web_search_tools()
23
- list_of_other_tools = [get_downloader_tool(), get_image_interpreter_tool(), get_calculator_tool(), get_pandas_tool()]
24
 
25
  self.llm = get_language_model()
26
  self.tools = list_of_search_tools + list_of_other_tools
 
8
  from agent_tools.ImageReaderTool import get_image_interpreter_tool
9
  from agent_tools.CalculatorTool import get_calculator_tool
10
  from agent_tools.PandasTool import get_pandas_tool
11
+ from agent_tools.CodeInterpreterTool import get_code_interpreter_tool
12
  from agent_prompts.SystemPrompt import gaia_system_prompt
13
 
14
  #Logging
 
21
  def __init__(self):
22
  # print(f"ReAct Agent signature--------------------------------------- \n {inspect.signature(ReActAgent.from_tools)} \n ReAct Agent signature--------------------------------------- \n" )
23
  list_of_search_tools = web_search_tools()
24
+ list_of_other_tools = [get_downloader_tool(), get_image_interpreter_tool(), get_calculator_tool(), get_pandas_tool(), get_code_interpreter_tool()]
25
 
26
  self.llm = get_language_model()
27
  self.tools = list_of_search_tools + list_of_other_tools
agent_tools/CodeInterpreterTool.py ADDED
@@ -0,0 +1,48 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import io
2
+ import contextlib
3
+ from llama_index.core.tools import FunctionTool
4
+
5
+ def execute_python_code(file_path: str) -> str:
6
+ """
7
+ Executes Python code from a given file path and captures its output.
8
+ Args:
9
+ file_path (str): The local path to the .py file to execute.
10
+ """
11
+ print(f"Executing Python code from: {file_path}")
12
+
13
+ # Use io.StringIO to create an in-memory text buffer to capture output
14
+ string_io = io.StringIO()
15
+
16
+ try:
17
+ with open(file_path, 'r', encoding='utf-8') as f:
18
+ code = f.read()
19
+
20
+ # Use contextlib.redirect_stdout to temporarily redirect all print() output to our in-memory buffer.
21
+ with contextlib.redirect_stdout(string_io):
22
+ exec(code, {})
23
+
24
+ # Get the content that was "printed" from the buffer
25
+ output = string_io.getvalue()
26
+
27
+ if not output.strip():
28
+ # Fallback for scripts that don't print but end with an expression
29
+ try:
30
+ lines = code.strip().split('\n')
31
+ last_line = lines[-1]
32
+ # Safely evaluate the last line if it's a simple expression
33
+ output = str(eval(last_line, {"__builtins__": None}, {}))
34
+ except:
35
+ output = "Code executed successfully with no print output."
36
+
37
+ return f"Successfully executed. Output:\n---\n{output.strip()}\n---"
38
+
39
+ except Exception as e:
40
+ return f"Error executing Python code: {e}"
41
+
42
+ def get_code_interpreter_tool() -> FunctionTool:
43
+ """Initializes and returns the Python Code Interpreter tool."""
44
+ return FunctionTool.from_defaults(
45
+ fn=execute_python_code,
46
+ name="python_code_interpreter",
47
+ description="A tool that can execute Python code from a local file and return its output. It takes a 'file_path' as input. Use this to find the output of a provided .py script."
48
+ )