Spaces:
Runtime error
Runtime error
Create PythonCalcTool.py
Browse files- tools/PythonCalcTool.py +28 -0
tools/PythonCalcTool.py
ADDED
|
@@ -0,0 +1,28 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from smolagents import Tool
|
| 2 |
+
|
| 3 |
+
class PythonCalcTool(Tool):
|
| 4 |
+
name = "python_calc"
|
| 5 |
+
description = (
|
| 6 |
+
"Executes Python code to compute numeric answers for calculation problems. "
|
| 7 |
+
"Safe, GAIA-friendly tool to solve math, physics, or distance/time problems."
|
| 8 |
+
)
|
| 9 |
+
inputs = {
|
| 10 |
+
"code": {"type": "string", "description": "Python code that sets a variable 'result' with the answer."}
|
| 11 |
+
}
|
| 12 |
+
output_type = "string"
|
| 13 |
+
|
| 14 |
+
def forward(self, code: str) -> str:
|
| 15 |
+
"""
|
| 16 |
+
Executes the code safely and returns the result as a string.
|
| 17 |
+
Code must set a variable 'result' which is returned.
|
| 18 |
+
"""
|
| 19 |
+
try:
|
| 20 |
+
local_vars = {}
|
| 21 |
+
exec(code, {}, local_vars)
|
| 22 |
+
# The agent should always set 'result'
|
| 23 |
+
answer = local_vars.get("result", None)
|
| 24 |
+
if answer is None:
|
| 25 |
+
return "NA"
|
| 26 |
+
return str(answer)
|
| 27 |
+
except Exception as e:
|
| 28 |
+
return "NA"
|