BuddAI/skills/calculator.py
JamesTheGiblet f9fd27d228 Implement core skills: Code validation, model fine-tuning, and system diagnostics
- Added `ModelFineTuner` class for preparing training data and fine-tuning models based on user corrections.
- Introduced `CodeValidator` class to validate generated code against various hardware and style rules, including safety checks and function naming conventions.
- Developed skills for calculator operations, system information retrieval, weather fetching, and timer functionality.
- Implemented a self-diagnostic skill to run unit tests and report results.
- Created a dynamic skill loading mechanism to discover and register skills from the current directory.
- Added unit tests for skills to ensure functionality and reliability.
2026-01-06 22:04:37 +00:00

47 lines
No EOL
1.5 KiB
Python

import re
def meta():
"""
Defines the metadata for the Calculator skill.
"""
return {
"name": "Smart Calculator",
"description": "Performs basic arithmetic operations detected in the prompt.",
"triggers": ["calculate", "compute", "solve", "math", "+", "-", "*", "/"]
}
def run(payload):
"""
Executes the calculation logic.
Accepts a string prompt or a dictionary context.
"""
# Normalize input
prompt = payload if isinstance(payload, str) else payload.get("prompt", "")
# 1. Extract the mathematical expression
# Regex looks for sequences of numbers and operators
# Allowed: digits, whitespace, +, -, *, /, ., (, )
match = re.search(r'([\d\.\s\+\-\*\/\(\)]+)', prompt)
if not match:
return None # Fallback to LLM if no math found
expression = match.group(0).strip()
if not any(char.isdigit() for char in expression):
return None
# 2. Safety Check (Double verification)
allowed_chars = set("0123456789.+-*/() ")
if not set(expression).issubset(allowed_chars):
return "Calculation aborted: Invalid characters detected."
# 3. Execute
try:
# pylint: disable=eval-used
result = eval(expression, {"__builtins__": None}, {})
return f"🧮 Result: {expression} = {result}"
except ZeroDivisionError:
return "🧮 Error: Division by zero is not allowed."
except Exception as e:
return f"🧮 Calculation Error: {str(e)}"