File size: 18,519 Bytes
8174855 |
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 |
import json
import re
import math
import cmath
from typing import Dict, List, Optional, Any
import requests
from dataclasses import dataclass
try:
import sympy as sp
import numpy as np
from scipy import optimize, integrate, stats
MATH_LIBS_AVAILABLE = True
except ImportError:
MATH_LIBS_AVAILABLE = False
print("Warning: Install sympy, numpy, scipy for enhanced math capabilities: pip install sympy numpy scipy")
@dataclass
class ToolCall:
tool: str
query: str
result: Optional[str] = None
error: Optional[str] = None
class MathEngine:
"""Free mathematical computation engine using SymPy, NumPy, SciPy."""
def __init__(self):
self.available = MATH_LIBS_AVAILABLE
def solve_equation(self, equation_str: str) -> str:
"""Solve mathematical equations."""
try:
# Parse and solve equation
if '=' in equation_str:
left, right = equation_str.split('=')
eq = sp.Eq(sp.sympify(left.strip()), sp.sympify(right.strip()))
x = sp.Symbol('x')
solutions = sp.solve(eq, x)
return f"Solutions: {solutions}"
else:
# Just evaluate expression
result = sp.sympify(equation_str)
simplified = sp.simplify(result)
return f"Result: {simplified}"
except Exception as e:
return f"Error solving equation: {str(e)}"
def calculus_operations(self, expression: str, operation: str, variable: str = 'x') -> str:
"""Perform calculus operations (derivative, integral, limit)."""
try:
expr = sp.sympify(expression)
var = sp.Symbol(variable)
if operation.lower() in ['derivative', 'diff', 'differentiate']:
result = sp.diff(expr, var)
return f"Derivative of {expression} with respect to {variable}: {result}"
elif operation.lower() in ['integral', 'integrate']:
result = sp.integrate(expr, var)
return f"Integral of {expression} with respect to {variable}: {result}"
elif operation.lower() in ['limit']:
result = sp.limit(expr, var, 0) # Default limit as x approaches 0
return f"Limit of {expression} as {variable} approaches 0: {result}"
else:
return f"Unknown calculus operation: {operation}"
except Exception as e:
return f"Error in calculus operation: {str(e)}"
def basic_math(self, expression: str) -> str:
"""Handle basic mathematical calculations."""
try:
# Handle common math functions
safe_expr = expression.lower()
# Replace common functions
replacements = {
'sin': 'math.sin',
'cos': 'math.cos',
'tan': 'math.tan',
'log': 'math.log',
'ln': 'math.log',
'sqrt': 'math.sqrt',
'pi': 'math.pi',
'e': 'math.e',
'^': '**' # Power operator
}
for old, new in replacements.items():
safe_expr = safe_expr.replace(old, new)
# Evaluate safely
result = eval(safe_expr, {"__builtins__": {}, "math": math, "cmath": cmath})
return f"Result: {result}"
except Exception as e:
return f"Error in calculation: {str(e)}"
def statistics_operations(self, data_str: str, operation: str) -> str:
"""Perform statistical calculations."""
try:
# Parse data
data = [float(x.strip()) for x in data_str.replace('[', '').replace(']', '').split(',')]
if operation.lower() in ['mean', 'average']:
result = np.mean(data)
return f"Mean of {data}: {result}"
elif operation.lower() in ['median']:
result = np.median(data)
return f"Median of {data}: {result}"
elif operation.lower() in ['std', 'standard deviation']:
result = np.std(data)
return f"Standard deviation of {data}: {result}"
elif operation.lower() in ['variance']:
result = np.var(data)
return f"Variance of {data}: {result}"
else:
return f"Unknown statistical operation: {operation}"
except Exception as e:
return f"Error in statistical calculation: {str(e)}"
def unit_conversion(self, value: float, from_unit: str, to_unit: str) -> str:
"""Convert between common units."""
try:
# Temperature conversions
if from_unit.lower() == 'celsius' and to_unit.lower() == 'fahrenheit':
result = (value * 9/5) + 32
return f"{value}°C = {result}°F"
elif from_unit.lower() == 'fahrenheit' and to_unit.lower() == 'celsius':
result = (value - 32) * 5/9
return f"{value}°F = {result}°C"
elif from_unit.lower() == 'celsius' and to_unit.lower() == 'kelvin':
result = value + 273.15
return f"{value}°C = {result}K"
# Length conversions
elif from_unit.lower() == 'meters' and to_unit.lower() == 'feet':
result = value * 3.28084
return f"{value}m = {result}ft"
elif from_unit.lower() == 'feet' and to_unit.lower() == 'meters':
result = value / 3.28084
return f"{value}ft = {result}m"
else:
return f"Unit conversion not implemented: {from_unit} to {to_unit}"
except Exception as e:
return f"Error in unit conversion: {str(e)}"
def query(self, question: str) -> Dict[str, Any]:
"""Main query interface for mathematical questions."""
if not self.available:
return {
'success': False,
'error': 'Mathematical libraries not available. Install with: pip install sympy numpy scipy'
}
try:
question_lower = question.lower().strip()
results = []
# Detect operation type and route accordingly
if any(word in question_lower for word in ['derivative', 'differentiate', 'diff']):
# Extract expression (simple heuristic)
expression = question_lower.split('of')[-1].strip()
if 'with respect to' in expression:
expr_part = expression.split('with respect to')[0].strip()
var_part = expression.split('with respect to')[1].strip()
result = self.calculus_operations(expr_part, 'derivative', var_part)
else:
result = self.calculus_operations(expression, 'derivative')
results.append({'title': 'Derivative', 'text': result})
elif any(word in question_lower for word in ['integral', 'integrate', 'antiderivative']):
expression = question_lower.split('of')[-1].strip()
if 'with respect to' in expression:
expr_part = expression.split('with respect to')[0].strip()
var_part = expression.split('with respect to')[1].strip()
result = self.calculus_operations(expr_part, 'integral', var_part)
else:
result = self.calculus_operations(expression, 'integral')
results.append({'title': 'Integral', 'text': result})
elif any(word in question_lower for word in ['solve', 'equation']):
# Extract equation
equation_part = question.split('solve')[-1].strip() if 'solve' in question_lower else question
result = self.solve_equation(equation_part)
results.append({'title': 'Equation Solution', 'text': result})
elif any(word in question_lower for word in ['mean', 'average', 'median', 'std', 'variance']):
# Statistical operations
for op in ['mean', 'average', 'median', 'standard deviation', 'variance']:
if op in question_lower:
data_part = question_lower.replace(op, '').replace('of', '').strip()
result = self.statistics_operations(data_part, op)
results.append({'title': f'Statistics - {op.title()}', 'text': result})
break
elif any(word in question_lower for word in ['convert', 'to fahrenheit', 'to celsius', 'to kelvin', 'to meters', 'to feet']):
# Unit conversion (simplified parsing)
words = question_lower.split()
try:
value = float(next(word for word in words if word.replace('.', '').isdigit()))
if 'celsius' in question_lower and 'fahrenheit' in question_lower:
result = self.unit_conversion(value, 'celsius', 'fahrenheit')
elif 'fahrenheit' in question_lower and 'celsius' in question_lower:
result = self.unit_conversion(value, 'fahrenheit', 'celsius')
else:
result = "Unit conversion not recognized"
results.append({'title': 'Unit Conversion', 'text': result})
except:
results.append({'title': 'Unit Conversion', 'text': 'Could not parse conversion request'})
else:
# Try basic mathematical evaluation
# Clean the question to extract mathematical expression
math_expr = question.lower()
for word in ['calculate', 'compute', 'evaluate', 'what is', 'find', 'test:', 'test']:
math_expr = math_expr.replace(word, '').strip()
# Remove punctuation that might interfere
import string
math_expr = math_expr.translate(str.maketrans('', '', '?!'))
result = self.basic_math(math_expr)
results.append({'title': 'Calculation', 'text': result})
if results:
return {
'success': True,
'results': results
}
else:
return {
'success': False,
'error': 'Could not process mathematical query'
}
except Exception as e:
return {
'success': False,
'error': f'Math engine error: {str(e)}'
}
class SerperAPI:
def __init__(self, api_key: str):
self.api_key = api_key
self.base_url = "https://google.serper.dev/search"
def search(self, query: str, num_results: int = 5) -> Dict[str, Any]:
"""Search the web using Serper API."""
try:
headers = {
'X-API-KEY': self.api_key,
'Content-Type': 'application/json'
}
payload = {
'q': query,
'num': num_results
}
response = requests.post(self.base_url, headers=headers, json=payload, timeout=10)
response.raise_for_status()
data = response.json()
results = []
# Extract organic results
if 'organic' in data:
for item in data['organic']:
results.append({
'title': item.get('title', ''),
'link': item.get('link', ''),
'snippet': item.get('snippet', ''),
'date': item.get('date', '')
})
# Extract knowledge graph if available
knowledge_graph = None
if 'knowledgeGraph' in data:
kg = data['knowledgeGraph']
knowledge_graph = {
'title': kg.get('title', ''),
'type': kg.get('type', ''),
'description': kg.get('description', ''),
'attributes': kg.get('attributes', {})
}
return {
'success': True,
'results': results,
'knowledge_graph': knowledge_graph,
'search_information': data.get('searchInformation', {})
}
except Exception as e:
return {
'success': False,
'error': f'Serper API error: {str(e)}'
}
class ToolOrchestrator:
def __init__(self, serper_api_key: Optional[str] = None):
self.math_engine = MathEngine()
self.serper = SerperAPI(serper_api_key) if serper_api_key else None
def should_use_math_engine(self, query: str) -> bool:
"""Determine if query should be routed to the math engine."""
math_indicators = [
# Mathematical operations
r'\b(?:calculate|solve|compute|evaluate|find)\b',
r'[+\-*/=()]',
r'\b(?:integral|derivative|limit|sum|product)\b',
r'\b(?:equation|formula|expression)\b',
# Scientific/mathematical terms
r'\b(?:physics|chemistry|biology|mathematics|calculus|algebra|geometry|trigonometry)\b',
r'\b(?:mass|energy|force|velocity|acceleration|temperature|pressure)\b',
r'\b(?:molecular|atomic|quantum|thermodynamic)\b',
# Units and constants
r'\b(?:kg|m/s|joule|newton|pascal|kelvin|celsius|fahrenheit)\b',
r'\b(?:pi|euler|planck|avogadro|boltzmann)\b',
# Numbers and mathematical notation
r'\d+\s*[\+\-\*/\^]\s*\d+',
r'\b(?:square root|log|ln|sin|cos|tan|exp)\b',
]
query_lower = query.lower()
return any(re.search(pattern, query_lower) for pattern in math_indicators)
def should_use_serper(self, query: str) -> bool:
"""Determine if query should be routed to Serper for web search."""
web_indicators = [
# Current events and time-sensitive info
r'\b(?:current|latest|recent|today|yesterday|this year|2024|2025)\b',
r'\b(?:news|breaking|update|announcement)\b',
# Factual queries
r'\b(?:when did|what is|who is|where is|how many|what happened)\b',
r'\b(?:price|cost|stock|market|weather|temperature)\b',
# Specific entities that might need current info
r'\b(?:company|corporation|startup|CEO|president|politician)\b',
r'\b(?:movie|film|song|album|book|game|app)\b',
# Location-based queries
r'\b(?:restaurant|hotel|store|hospital|university|airport)\b',
r'\b(?:near me|in [A-Z][a-z]+|located in)\b',
]
query_lower = query.lower()
return any(re.search(pattern, query_lower) for pattern in web_indicators)
def execute_tool_call(self, tool_call: ToolCall) -> ToolCall:
"""Execute a tool call and return the result."""
try:
if tool_call.tool == "math_engine" and self.math_engine:
result = self.math_engine.query(tool_call.query)
if result['success']:
# Format math engine results nicely
formatted_results = []
for r in result['results']:
formatted_results.append(f"{r['title']}: {r['text']}")
tool_call.result = "\n".join(formatted_results)
else:
tool_call.error = result['error']
elif tool_call.tool == "serper" and self.serper:
result = self.serper.search(tool_call.query)
if result['success']:
# Format Serper results nicely
formatted_results = []
# Add knowledge graph first if available
if result['knowledge_graph']:
kg = result['knowledge_graph']
formatted_results.append(f"**{kg['title']}**")
if kg['description']:
formatted_results.append(kg['description'])
formatted_results.append("")
# Add search results
for i, r in enumerate(result['results'][:3]): # Top 3 results
formatted_results.append(f"{i+1}. **{r['title']}**")
formatted_results.append(f" {r['snippet']}")
formatted_results.append("")
tool_call.result = "\n".join(formatted_results)
else:
tool_call.error = result['error']
else:
tool_call.error = f"Tool '{tool_call.tool}' not available or configured"
except Exception as e:
tool_call.error = f"Tool execution error: {str(e)}"
return tool_call
def route_query(self, query: str) -> Optional[ToolCall]:
"""Determine which tool to use for a query, if any."""
if self.should_use_math_engine(query):
return ToolCall(tool="math_engine", query=query)
elif self.should_use_serper(query):
return ToolCall(tool="serper", query=query)
else:
return None # Use direct generation
|