Ha2Ha commited on
Commit
c27e688
·
verified ·
1 Parent(s): ae7a494

Upload app.py

Browse files

Add three tools such as calculator, get_weekday_of_february_28 and currency_converter

Files changed (1) hide show
  1. app.py +75 -2
app.py CHANGED
@@ -1,5 +1,5 @@
1
  from smolagents import CodeAgent,DuckDuckGoSearchTool, HfApiModel,load_tool,tool
2
- import datetime
3
  import requests
4
  import pytz
5
  import yaml
@@ -18,6 +18,79 @@ def my_custom_tool(arg1:str, arg2:int)-> str: #it's import to specify the return
18
  """
19
  return "What magic will you build ?"
20
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
21
  @tool
22
  def get_current_time_in_timezone(timezone: str) -> str:
23
  """A tool that fetches the current local time in a specified timezone.
@@ -55,7 +128,7 @@ with open("prompts.yaml", 'r') as stream:
55
 
56
  agent = CodeAgent(
57
  model=model,
58
- tools=[final_answer], ## add your tools here (don't remove final answer)
59
  max_steps=6,
60
  verbosity_level=1,
61
  grammar=None,
 
1
  from smolagents import CodeAgent,DuckDuckGoSearchTool, HfApiModel,load_tool,tool
2
+ from datetime import datetime
3
  import requests
4
  import pytz
5
  import yaml
 
18
  """
19
  return "What magic will you build ?"
20
 
21
+ @tool
22
+ def calculator(a: int, b: int) -> int:
23
+ """Multiply two integers.
24
+ Args:
25
+ a: the integer
26
+ b: the integer
27
+ Returns:
28
+ int: The multiplication of a and b as an int
29
+ """
30
+ return(a*b)
31
+
32
+ @tool
33
+ def get_weekday_of_february_28(start_year: int) -> str:
34
+ """Finds the weekdays for February 28th in the next three leap years after a given year.
35
+ Args:
36
+ start_year: A four-digit year greater than 2024.
37
+ Returns:
38
+ str: A formatted string listing each leap year to the name of the weekday on February 28th.
39
+ """
40
+ if start_year <= 2024:
41
+ raise ValueError("The starting year must be greater than 2024.")
42
+
43
+ leap_years = []
44
+ year = start_year + 1 # Start checking from the year
45
+
46
+ while len(leap_years) < 3:
47
+ # Leap year condition: divisible by 4 and (not divisible by 100 unless also divisible by 400)
48
+ if (year % 4 == 0 and year % 100 != 0) or (year % 400 == 0):
49
+ leap_years.append(year)
50
+ year += 1
51
+
52
+ #Get the weekdays for February 28th in those leap years
53
+ weekdays = "\n".join(f"{year}: {datetime(year, 2, 28).strftime('%A')}" for year in leap_years)
54
+
55
+ return weekdays
56
+
57
+ @tool
58
+ def currency_converter(amount: float, from_currency: str, to_currency: str) -> str:
59
+ """Converts an amount from one currency to another using live exchange rates.
60
+
61
+ Args:
62
+ amount: The amount of money to convert (float).
63
+ from_currency: The currency code (str) to convert from (e.g., 'USD').
64
+ to_currency: The currency code (str) to convert to (e.g., 'EUR').
65
+
66
+ Returns:
67
+ str: A formatted string displaying the converted amount.
68
+
69
+ Example:
70
+ currency_converter(100, 'USD', 'EUR')
71
+ -> "100 USD is approximately 92.50 EUR"
72
+ """
73
+
74
+ api_url = f"https://open.er-api.com/v6/latest/{from_currency.upper()}"
75
+
76
+ try:
77
+ response = requests.get(api_url)
78
+ data = response.json()
79
+
80
+ if "rates" not in data:
81
+ return "Error fetching exchange rates. Please check the currency codes."
82
+
83
+ if to_currency.upper() not in data["rates"]:
84
+ return f"Currency {to_currency.upper()} not supported."
85
+
86
+ exchange_rate = data["rates"][to_currency.upper()]
87
+ converted_amount = round(amount * exchange_rate, 2)
88
+
89
+ return f"{amount} {from_currency.upper()} is approximately {converted_amount} {to_currency.upper()}"
90
+
91
+ except requests.RequestException:
92
+ return "Error connecting to the currency exchange API. Please try again later."
93
+
94
  @tool
95
  def get_current_time_in_timezone(timezone: str) -> str:
96
  """A tool that fetches the current local time in a specified timezone.
 
128
 
129
  agent = CodeAgent(
130
  model=model,
131
+ tools=[final_answer,calculator,get_weekday_of_february_28,currency_converter], ## add your tools here (don't remove final answer)
132
  max_steps=6,
133
  verbosity_level=1,
134
  grammar=None,