Gab-Chung commited on
Commit
e2d4168
·
verified ·
1 Parent(s): 7c91c74

added a currency converter tool

Browse files
Files changed (1) hide show
  1. app.py +32 -1
app.py CHANGED
@@ -4,6 +4,7 @@ import requests
4
  import pytz
5
  import yaml
6
  from tools.final_answer import FinalAnswerTool
 
7
 
8
  from Gradio_UI import GradioUI
9
 
@@ -18,6 +19,36 @@ 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.
@@ -56,7 +87,7 @@ with open("prompts.yaml", 'r') as stream:
56
 
57
  agent = CodeAgent(
58
  model=model,
59
- tools=[final_answer, get_current_time_in_timezone, image_generation_tool], ## add your tools here (don't remove final answer)
60
  max_steps=6,
61
  verbosity_level=1,
62
  grammar=None,
 
4
  import pytz
5
  import yaml
6
  from tools.final_answer import FinalAnswerTool
7
+ from forex_python.converter import CurrencyRates
8
 
9
  from Gradio_UI import GradioUI
10
 
 
19
  """
20
  return "What magic will you build ?"
21
 
22
+ @tool
23
+ def convert_currency(amount:float, from_currency:float, to_currency:float)-> float:
24
+ """
25
+ Convert the specified amount from one currency to another.
26
+
27
+ Parameters:
28
+ - amount (float): The amount of money to convert.
29
+ - from_currency (str): The currency code of the source currency (e.g., 'USD').
30
+ - to_currency (str): The currency code of the target currency (e.g., 'EUR').
31
+
32
+ Returns:
33
+ - float: The converted amount in the target currency.
34
+ """
35
+ # Initialize the CurrencyRates object
36
+ c = CurrencyRates()
37
+
38
+ try:
39
+ # Fetch the conversion rate
40
+ rate = c.get_rate(from_currency.upper(), to_currency.upper())
41
+ # Perform the conversion
42
+ converted_amount = amount * rate
43
+ return converted_amount
44
+
45
+ except RatesNotAvailableError:
46
+ return f"Error: Conversionrate not available for {from_currency} to {to_currency}."
47
+ except ValueError:
48
+ return f"Error: Invalid amount entered."
49
+ except Exception as e:
50
+ return f"An unexpected error occurred: {e}"
51
+
52
  @tool
53
  def get_current_time_in_timezone(timezone: str) -> str:
54
  """A tool that fetches the current local time in a specified timezone.
 
87
 
88
  agent = CodeAgent(
89
  model=model,
90
+ tools=[final_answer, get_current_time_in_timezone, convert_currency, image_generation_tool], ## add your tools here (don't remove final answer)
91
  max_steps=6,
92
  verbosity_level=1,
93
  grammar=None,