Spaces:
Sleeping
Sleeping
File size: 1,938 Bytes
686a009 | 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 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 | import os
from dotenv import load_dotenv
load_dotenv()
from langchain.tools import tool
from langchain_community.utilities.alpha_vantage import AlphaVantageAPIWrapper
from logger.decorators import log_entry
@tool
@log_entry
def multiply(a: int, b: int) -> int:
"""
Multiply two integers.
This tool takes two integer values and returns their product.
Args:
a (int): The first integer.
b (int): The second integer.
Returns:
int: The product of `a` and `b`.
"""
print('Entered into multiply().')
return a * b
@tool
@log_entry
def add(a: int, b: int) -> int:
"""
Add two integers.
This tool takes two integer values and returns their sum.
Args:
a (int): The first integer.
b (int): The second integer.
Returns:
int: The sum of `a` and `b`.
"""
print('Entered into add().')
return a + b
@tool
@log_entry
def currency_converter(from_curr: str, to_curr: str, value: float) -> float:
"""
Convert a currency value from one currency to another using real-time exchange rates.
This tool uses the AlphaVantage API to fetch real-time exchange rates and converts
the given value from `from_curr` to `to_curr`.
Args:
from_curr (str): The currency code to convert from (e.g., "USD").
to_curr (str): The currency code to convert to (e.g., "EUR").
value (float): The numeric amount in the `from_curr` currency.
Returns:
float: The equivalent amount in `to_curr` currency based on the current exchange rate.
"""
print('Entered into currency_converter().')
os.environ["ALPHAVANTAGE_API_KEY"] = os.getenv('ALPHAVANTAGE_API_KEY')
alpha_vantage = AlphaVantageAPIWrapper()
response = alpha_vantage._get_exchange_rate(from_curr, to_curr)
exchange_rate = response['Realtime Currency Exchange Rate']['5. Exchange Rate']
return value * float(exchange_rate)
|