Spaces:
Sleeping
Sleeping
File size: 2,006 Bytes
bdc5edd | 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 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 | """Basic computation tools: calculator and Python code execution."""
import os
import subprocess
import tempfile
from langchain_core.tools import tool
@tool
def calculator(a: float, b: float, type: str) -> float:
"""Performs mathematical calculations, addition, subtraction, multiplication, division, modulus.
Args:
a (float): first float number
b (float): second float number
type (str): the type of calculation to perform, can be addition, subtraction, multiplication, division, modulus
"""
if type == "addition":
return a + b
elif type == "subtraction":
return a - b
elif type == "multiplication":
return a * b
elif type == "division":
if b == 0:
raise ValueError("Cannot divide by zero.")
return a / b
elif type == "modulus":
return a % b
else:
raise TypeError(f"{type} is not an option for type, choose one of addition, subtraction, multiplication, division, modulus")
@tool
def python_eval(code: str) -> str:
"""
Execute a Python code snippet and return its stdout output.
Use this when a question asks what a script outputs, or when computation requires running code.
Args:
code: Python source code to execute.
Returns:
The stdout output of the code, or an error/timeout message.
"""
try:
with tempfile.NamedTemporaryFile(mode='w', suffix='.py', delete=False) as f:
f.write(code)
tmp_path = f.name
result = subprocess.run(
['python3', tmp_path],
capture_output=True, text=True, timeout=30
)
os.unlink(tmp_path)
if result.returncode == 0:
return f"Output:\n{result.stdout}"
return f"[python_eval] exit {result.returncode}:\n{result.stderr}"
except subprocess.TimeoutExpired:
return "[python_eval] execution timed out (30s limit)"
except Exception as e:
return f"[python_eval] failed: {e}"
|