|
|
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) |
|
|
|
|
|
answer = local_vars.get("result", None) |
|
|
if answer is None: |
|
|
return "NA" |
|
|
return str(answer) |
|
|
except Exception as e: |
|
|
return "NA" |
|
|
|