Spaces:
Sleeping
Sleeping
File size: 15,938 Bytes
225a75e |
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 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 |
#!/usr/bin/env python3
"""
Calculator Tool for GAIA Agent System
Handles mathematical calculations, unit conversions, and statistical operations
"""
import re
import math
import statistics
import logging
from typing import Dict, List, Optional, Any, Union
from dataclasses import dataclass
from tools import BaseTool
logger = logging.getLogger(__name__)
@dataclass
class CalculationResult:
"""Container for calculation results"""
expression: str
result: Union[float, int, str]
result_type: str
steps: List[str]
units: Optional[str] = None
def to_dict(self) -> Dict[str, Any]:
return {
"expression": self.expression,
"result": self.result,
"result_type": self.result_type,
"steps": self.steps,
"units": self.units
}
class CalculatorTool(BaseTool):
"""
Calculator tool for mathematical operations
Supports basic math, advanced functions, statistics, and unit conversions
"""
def __init__(self):
super().__init__("calculator")
# Safe mathematical functions
self.safe_functions = {
# Basic functions
'abs': abs, 'round': round, 'min': min, 'max': max,
'sum': sum, 'len': len,
# Math module functions
'sin': math.sin, 'cos': math.cos, 'tan': math.tan,
'asin': math.asin, 'acos': math.acos, 'atan': math.atan,
'sinh': math.sinh, 'cosh': math.cosh, 'tanh': math.tanh,
'exp': math.exp, 'log': math.log, 'log10': math.log10,
'sqrt': math.sqrt, 'pow': pow, 'ceil': math.ceil, 'floor': math.floor,
'factorial': math.factorial, 'gcd': math.gcd,
# Constants
'pi': math.pi, 'e': math.e,
# Statistics functions
'mean': statistics.mean, 'median': statistics.median,
'mode': statistics.mode, 'stdev': statistics.stdev,
'variance': statistics.variance
}
# Unit conversion factors (to base units)
self.unit_conversions = {
# Length (to meters)
'length': {
'mm': 0.001, 'cm': 0.01, 'dm': 0.1, 'm': 1,
'km': 1000, 'in': 0.0254, 'ft': 0.3048,
'yd': 0.9144, 'mi': 1609.344
},
# Weight (to grams)
'weight': {
'mg': 0.001, 'g': 1, 'kg': 1000,
'oz': 28.3495, 'lb': 453.592, 'ton': 1000000
},
# Temperature (special handling)
'temperature': {
'celsius': 'celsius', 'fahrenheit': 'fahrenheit',
'kelvin': 'kelvin', 'c': 'celsius', 'f': 'fahrenheit', 'k': 'kelvin'
},
# Time (to seconds)
'time': {
's': 1, 'min': 60, 'h': 3600, 'hr': 3600,
'day': 86400, 'week': 604800, 'month': 2629746, 'year': 31556952
},
# Area (to square meters)
'area': {
'mm2': 0.000001, 'cm2': 0.0001, 'm2': 1,
'km2': 1000000, 'in2': 0.00064516, 'ft2': 0.092903
},
# Volume (to liters)
'volume': {
'ml': 0.001, 'l': 1, 'gal': 3.78541, 'qt': 0.946353,
'pt': 0.473176, 'cup': 0.236588, 'fl_oz': 0.0295735
}
}
def _execute_impl(self, input_data: Any, **kwargs) -> Dict[str, Any]:
"""
Execute calculator operations based on input type
Args:
input_data: Can be:
- str: Mathematical expression
- dict: {"expression": str, "operation": str, "data": list, "units": dict}
"""
if isinstance(input_data, str):
return self._evaluate_expression(input_data)
elif isinstance(input_data, dict):
operation = input_data.get("operation", "evaluate")
if operation == "evaluate":
expression = input_data.get("expression", "")
return self._evaluate_expression(expression)
elif operation == "statistics":
data = input_data.get("data", [])
return self._calculate_statistics(data)
elif operation == "convert":
value = input_data.get("value", 0)
from_unit = input_data.get("from_unit", "")
to_unit = input_data.get("to_unit", "")
return self._convert_units(value, from_unit, to_unit)
else:
raise ValueError(f"Unknown operation: {operation}")
else:
raise ValueError(f"Unsupported input type: {type(input_data)}")
def _evaluate_expression(self, expression: str) -> Dict[str, Any]:
"""
Safely evaluate a mathematical expression
"""
try:
# Clean the expression
original_expression = expression
expression = self._clean_expression(expression)
steps = [f"Original: {original_expression}", f"Cleaned: {expression}"]
# Check for unit conversion patterns
unit_match = re.search(r'(\d+\.?\d*)\s*(\w+)\s+to\s+(\w+)', expression)
if unit_match:
value, from_unit, to_unit = unit_match.groups()
return self._convert_units(float(value), from_unit, to_unit)
# Replace common mathematical expressions
expression = self._replace_math_expressions(expression)
steps.append(f"With functions: {expression}")
# Validate expression safety
if not self._is_safe_expression(expression):
raise ValueError("Expression contains unsafe operations")
# Create safe evaluation environment
safe_dict = {
"__builtins__": {},
**self.safe_functions
}
# Evaluate the expression
result = eval(expression, safe_dict)
# Determine result type and format
if isinstance(result, (int, float)):
if result == int(result):
result = int(result)
result_type = "integer"
else:
result = round(result, 10) # Avoid floating point errors
result_type = "float"
else:
result_type = type(result).__name__
calc_result = CalculationResult(
expression=original_expression,
result=result,
result_type=result_type,
steps=steps
)
return {
"success": True,
"calculation": calc_result.to_dict(),
"message": f"Successfully evaluated: {result}"
}
except Exception as e:
return {
"success": False,
"expression": expression,
"message": f"Calculation failed: {str(e)}",
"error_type": type(e).__name__
}
def _clean_expression(self, expression: str) -> str:
"""Clean and normalize mathematical expression"""
# Remove extra whitespace
expression = re.sub(r'\s+', ' ', expression.strip())
# Replace common text with symbols
replacements = {
' plus ': '+', ' minus ': '-', ' times ': '*', ' multiply ': '*',
' divided by ': '/', ' divide ': '/', ' power ': '**', ' to the power of ': '**'
}
for text, symbol in replacements.items():
expression = expression.replace(text, symbol)
# Handle percentage
expression = re.sub(r'(\d+\.?\d*)%', r'(\1/100)', expression)
return expression
def _replace_math_expressions(self, expression: str) -> str:
"""Replace mathematical function names with proper calls"""
# Handle square root
expression = re.sub(r'sqrt\s*\(([^)]+)\)', r'sqrt(\1)', expression)
expression = re.sub(r'square root of (\d+\.?\d*)', r'sqrt(\1)', expression)
# Handle logarithms
expression = re.sub(r'log\s*\(([^)]+)\)', r'log(\1)', expression)
expression = re.sub(r'ln\s*\(([^)]+)\)', r'log(\1)', expression)
# Handle trigonometric functions
trig_functions = ['sin', 'cos', 'tan', 'asin', 'acos', 'atan']
for func in trig_functions:
expression = re.sub(f'{func}\\s*\\(([^)]+)\\)', f'{func}(\\1)', expression)
return expression
def _is_safe_expression(self, expression: str) -> bool:
"""Check if expression is safe to evaluate"""
# Forbidden patterns
forbidden_patterns = [
r'__.*__', # Dunder methods
r'import\s', # Import statements
r'exec\s*\(', # Exec function
r'eval\s*\(', # Eval function
r'open\s*\(', # File operations
r'file\s*\(', # File operations
r'input\s*\(', # Input function
r'raw_input\s*\(', # Raw input
]
for pattern in forbidden_patterns:
if re.search(pattern, expression, re.IGNORECASE):
return False
return True
def _calculate_statistics(self, data: List[float]) -> Dict[str, Any]:
"""Calculate statistical measures for a dataset"""
try:
if not data:
raise ValueError("Empty dataset provided")
data = [float(x) for x in data] # Ensure all values are numeric
stats = {
"count": len(data),
"sum": sum(data),
"mean": statistics.mean(data),
"median": statistics.median(data),
"min": min(data),
"max": max(data),
"range": max(data) - min(data)
}
# Add standard deviation and variance if enough data points
if len(data) > 1:
stats["stdev"] = statistics.stdev(data)
stats["variance"] = statistics.variance(data)
# Add mode if applicable
try:
stats["mode"] = statistics.mode(data)
except statistics.StatisticsError:
stats["mode"] = "No unique mode"
return {
"success": True,
"statistics": stats,
"data": data,
"message": f"Calculated statistics for {len(data)} data points"
}
except Exception as e:
return {
"success": False,
"message": f"Statistics calculation failed: {str(e)}",
"error_type": type(e).__name__
}
def _convert_units(self, value: float, from_unit: str, to_unit: str) -> Dict[str, Any]:
"""Convert between different units"""
try:
from_unit = from_unit.lower()
to_unit = to_unit.lower()
# Find the unit type
unit_type = None
for utype, units in self.unit_conversions.items():
if from_unit in units and to_unit in units:
unit_type = utype
break
if not unit_type:
raise ValueError(f"Cannot convert between {from_unit} and {to_unit}")
# Special handling for temperature
if unit_type == 'temperature':
result = self._convert_temperature(value, from_unit, to_unit)
else:
# Standard unit conversion
from_factor = self.unit_conversions[unit_type][from_unit]
to_factor = self.unit_conversions[unit_type][to_unit]
result = value * from_factor / to_factor
# Round to reasonable precision
if result == int(result):
result = int(result)
else:
result = round(result, 6)
conversion_result = CalculationResult(
expression=f"{value} {from_unit} to {to_unit}",
result=result,
result_type="conversion",
steps=[
f"Convert {value} {from_unit} to {to_unit}",
f"Result: {result} {to_unit}"
],
units=to_unit
)
return {
"success": True,
"conversion": conversion_result.to_dict(),
"message": f"Converted {value} {from_unit} = {result} {to_unit}"
}
except Exception as e:
return {
"success": False,
"message": f"Unit conversion failed: {str(e)}",
"error_type": type(e).__name__
}
def _convert_temperature(self, value: float, from_unit: str, to_unit: str) -> float:
"""Convert temperature between Celsius, Fahrenheit, and Kelvin"""
# Normalize unit names
unit_map = {'c': 'celsius', 'f': 'fahrenheit', 'k': 'kelvin'}
from_unit = unit_map.get(from_unit, from_unit)
to_unit = unit_map.get(to_unit, to_unit)
# Convert to Celsius first
if from_unit == 'fahrenheit':
celsius = (value - 32) * 5/9
elif from_unit == 'kelvin':
celsius = value - 273.15
else: # Already Celsius
celsius = value
# Convert from Celsius to target unit
if to_unit == 'fahrenheit':
return celsius * 9/5 + 32
elif to_unit == 'kelvin':
return celsius + 273.15
else: # Stay in Celsius
return celsius
def test_calculator_tool():
"""Test the calculator tool with various operations"""
tool = CalculatorTool()
# Test cases
test_cases = [
"2 + 3 * 4",
"sqrt(16) + 2^3",
"sin(pi/2) + cos(0)",
{"operation": "statistics", "data": [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]},
{"operation": "convert", "value": 100, "from_unit": "cm", "to_unit": "m"},
{"operation": "convert", "value": 32, "from_unit": "f", "to_unit": "c"},
"10 factorial",
"mean([1, 2, 3, 4, 5])",
"15% of 200"
]
print("🧪 Testing Calculator Tool...")
for i, test_case in enumerate(test_cases, 1):
print(f"\n--- Test {i}: {test_case} ---")
try:
result = tool.execute(test_case)
if result.success:
if 'calculation' in result.result:
calc = result.result['calculation']
print(f"✅ Result: {calc['result']} ({calc['result_type']})")
elif 'statistics' in result.result:
stats = result.result['statistics']
print(f"✅ Mean: {stats['mean']}, Median: {stats['median']}, StDev: {stats.get('stdev', 'N/A')}")
elif 'conversion' in result.result:
conv = result.result['conversion']
print(f"✅ Conversion: {conv['result']} {conv['units']}")
print(f" Message: {result.result.get('message', 'No message')}")
else:
print(f"❌ Error: {result.result.get('message', 'Unknown error')}")
print(f" Execution time: {result.execution_time:.3f}s")
except Exception as e:
print(f"❌ Exception: {str(e)}")
if __name__ == "__main__":
# Test when run directly
test_calculator_tool() |