Spaces:
Sleeping
Sleeping
File size: 963 Bytes
b9793ec |
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 |
from smolagents import Tool
class PythonCalcTool(Tool):
name = "python_calc"
description = (
"Executes Python code to compute numeric answers for calculation problems. "
"Safe, GAIA-friendly tool to solve math, physics, or distance/time problems."
)
inputs = {
"code": {"type": "string", "description": "Python code that sets a variable 'result' with the answer."}
}
output_type = "string"
def forward(self, code: str) -> str:
"""
Executes the code safely and returns the result as a string.
Code must set a variable 'result' which is returned.
"""
try:
local_vars = {}
exec(code, {}, local_vars)
# The agent should always set 'result'
answer = local_vars.get("result", None)
if answer is None:
return "NA"
return str(answer)
except Exception as e:
return "NA"
|