maxwellmogambi commited on
Commit
dba13be
·
verified ·
1 Parent(s): a3cbdc6

Update tools/unit_conversion.py

Browse files
Files changed (1) hide show
  1. tools/unit_conversion.py +30 -29
tools/unit_conversion.py CHANGED
@@ -1,33 +1,34 @@
1
- from smolagents import tool
 
2
 
3
- @tool
4
- def unit_conversion(value: float, from_unit: str, to_unit: str) -> str:
5
- """A tool for converting units like distance, weight, and temperature.
6
-
7
- Args:
8
- value: The numerical value to convert.
9
- from_unit: The unit of the input value (e.g., 'miles', 'kilograms', 'celsius').
10
- to_unit: The desired unit (e.g., 'kilometers', 'pounds', 'fahrenheit').
11
-
12
- Returns:
13
- A string describing the converted value.
14
- """
15
-
16
- conversion_factors = {
17
- ("miles", "kilometers"): 1.60934,
18
- ("kilometers", "miles"): 0.621371,
19
- ("kilograms", "pounds"): 2.20462,
20
- ("pounds", "kilograms"): 0.453592,
21
  }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
22
 
23
- # Temperature conversions
24
- if from_unit == "celsius" and to_unit == "fahrenheit":
25
- result = (value * 9/5) + 32
26
- elif from_unit == "fahrenheit" and to_unit == "celsius":
27
- result = (value - 32) * 5/9
28
- elif (from_unit, to_unit) in conversion_factors:
29
- result = value * conversion_factors[(from_unit, to_unit)]
30
- else:
31
- return f"Sorry, conversion from {from_unit} to {to_unit} is not supported."
32
 
33
- return f"{value} {from_unit} is {round(result, 2)} {to_unit}."
 
1
+ from typing import Any
2
+ from smolagents.tools import Tool
3
 
4
+ class UnitConversionTool(Tool):
5
+ name = "unit_conversion"
6
+ description = "Converts between units such as distance, weight, and temperature."
7
+ inputs = {
8
+ "value": {"type": "float", "description": "The numerical value to convert."},
9
+ "from_unit": {"type": "string", "description": "The unit of the input value (e.g., 'miles', 'kilograms', 'celsius')."},
10
+ "to_unit": {"type": "string", "description": "The desired unit (e.g., 'kilometers', 'pounds', 'fahrenheit')."}
 
 
 
 
 
 
 
 
 
 
 
11
  }
12
+ output_type = "string"
13
+
14
+ def forward(self, value: float, from_unit: str, to_unit: str) -> str:
15
+ """Performs unit conversion."""
16
+ conversion_factors = {
17
+ ("miles", "kilometers"): 1.60934,
18
+ ("kilometers", "miles"): 0.621371,
19
+ ("kilograms", "pounds"): 2.20462,
20
+ ("pounds", "kilograms"): 0.453592,
21
+ }
22
+
23
+ # Temperature conversions
24
+ if from_unit == "celsius" and to_unit == "fahrenheit":
25
+ result = (value * 9/5) + 32
26
+ elif from_unit == "fahrenheit" and to_unit == "celsius":
27
+ result = (value - 32) * 5/9
28
+ elif (from_unit, to_unit) in conversion_factors:
29
+ result = value * conversion_factors[(from_unit, to_unit)]
30
+ else:
31
+ return f"Sorry, conversion from {from_unit} to {to_unit} is not supported."
32
 
33
+ return f"{value} {from_unit} is {round(result, 2)} {to_unit}."
 
 
 
 
 
 
 
 
34