File size: 2,900 Bytes
9608ecf
 
cd5abb0
cf7c892
9608ecf
b617c72
 
 
 
 
 
a0ae9e2
9608ecf
b617c72
 
9608ecf
 
b617c72
 
9608ecf
 
b617c72
 
9608ecf
 
b617c72
 
9608ecf
 
 
cd5abb0
b617c72
 
 
 
cd5abb0
 
b617c72
 
cd5abb0
 
b617c72
 
 
 
 
 
 
 
 
cd5abb0
 
b617c72
 
 
 
 
 
cd5abb0
b617c72
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
from typing import Any, Optional
from smolagents.tools import Tool

class MathOperationsTool(Tool):
    name = "math_operations"
    description = "Performs mathematical operations like multiplication, addition, subtraction, division, modulus, power, and square root"
    inputs = {
        'operation': {'type': 'string', 'description': 'The operation to perform: "multiply", "add", "subtract", "divide", "modulus", "power", "square_root"'},
        'a': {'type': 'number', 'description': 'First value'},
        'b': {'type': 'number', 'description': 'Second value (not used for square_root)', 'nullable': True}
    }
    output_type = "number"
    
    def multiply(self, a: float, b: float) -> float:
        """Multiply two numbers."""
        return a * b
    
    def add(self, a: float, b: float) -> float:
        """Add two numbers."""
        return a + b
    
    def subtract(self, a: float, b: float) -> float:
        """Subtract two numbers."""
        return a - b
    
    def divide(self, a: float, b: float) -> float:
        """Divide two numbers."""
        if b == 0:
            raise ValueError("Cannot divide by zero.")
        return a / b
    
    def modulus(self, a: float, b: float) -> float:
        """Get the modulus of two numbers."""
        if b == 0:
            raise ValueError("Cannot calculate modulus with zero.")
        return a % b
    
    def power(self, a: float, b: float) -> float:
        """Raise a number to the power of another number."""
        return a ** b
    
    def square_root(self, a: float) -> float:
        """Get the square root of a number."""
        if a < 0:
            raise ValueError("Cannot get square root of negative number.")
        return a ** 0.5

    def forward(self, operation: str, a: float, b: Optional[float] = None) -> float:
        """
        Execute the requested mathematical operation.
        
        Args:
            operation: The operation to perform (multiply, add, subtract, divide, modulus, power, square_root)
            a: First number
            b: Second number (not required for square_root)
        
        Returns:
            float: Result of the mathematical operation
        """
        operations = {
            'multiply': self.multiply,
            'add': self.add,
            'subtract': self.subtract,
            'divide': self.divide,
            'modulus': self.modulus,
            'power': self.power,
            'square_root': self.square_root
        }
        
        if operation not in operations:
            raise ValueError(f"Invalid operation. Must be one of: {', '.join(operations.keys())}")
        
        if operation == 'square_root':
            return operations[operation](a)
        
        if b is None:
            raise ValueError(f"Second number (b) is required for operation: {operation}")
        
        return operations[operation](a, b)