unit_conversion / tool.py
peterbonnesoeur's picture
Quick fix
7a4f0f5
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."