File size: 1,100 Bytes
9e5b98a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7a4f0f5
9e5b98a
7a4f0f5
9e5b98a
 
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
from smolagents import Tool

class UnitConversionTool(Tool):
    name = "unit_converter"
    description = "Converts values between units (e.g., kilometers to miles)."
    inputs = {
        "value": {"type": "number", "description": "Value to convert"},
        "from_unit": {"type": "string", "description": "Unit to convert from (e.g., 'km', 'kg')"},
        "to_unit": {"type": "string", "description": "Unit to convert to (e.g., 'miles', 'lbs')"}
    }
    output_type = "number"

    def forward(self, value: float, from_unit: str, to_unit: str):
        conversions = {
            ("km", "miles"): 0.621371,
            ("miles", "km"): 1.60934,
            ("kg", "lbs"): 2.20462,
            ("lbs", "kg"): 0.453592,
            ("C", "F"): lambda x: x * 9/5 + 32,
            ("F", "C"): lambda x: (x - 32) * 5/9
        }
        conversion = conversions.get((from_unit, to_unit))
        if callable(conversion):
            return conversion(float(value))
        elif conversion:
            return float(value) * conversion
        else:
            return "Conversion not supported."