File size: 1,208 Bytes
d75dae7 c921206 d75dae7 c921206 d75dae7 |
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 |
import subprocess
import sys
from smolagents import Tool
class PyCodeInterpreterTool(Tool):
name = "py_code_interpreter"
description = """
Executes file with python code.
Strongly use only for answering to the questions from GAIA benchmark.
Use default python_interpreter for other cases.
"""
inputs = {
"file_path": {
"type": "string",
"description": "The file with python code to execute.",
}
}
output_type = "string"
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
def forward(self, file_path: str) -> str:
cmd = f"python {file_path}".split()
try:
output = subprocess.check_output(cmd, stderr=subprocess.STDOUT, timeout=60.0)
output_text = output.decode(sys.stdout.encoding).strip()
except subprocess.TimeoutExpired:
output_text = "Error: code execution timed out."
except subprocess.CalledProcessError as e:
output_text = e.output.decode(sys.stdout.encoding).strip()
except subprocess.SubprocessError as ex:
output_text = f"Error: {str(ex)}\n"
return output_text |