smarttrip_ai / tools /currency_conversion_tool.py
swaraj
test_2
0fefb50
Raw
History Blame Contribute Delete
1.15 kB
import os
from utils.currency_converter import CurrencyConverter
from typing import List
from langchain.tools import tool
from dotenv import load_dotenv
class CurrencyConverterTool:
def __init__(self):
load_dotenv()
self.api_key = os.environ.get("EXCHANGE_RATE_API_KEY")
self.currency_service = CurrencyConverter(self.api_key)
self.currency_converter_tool_list = self._setup_tools()
def _setup_tools(self) -> List:
"""Setup all tools for the currency converter tool"""
@tool
def convert_currency(amount:float, from_currency:str, to_currency:str):
"""Convert amount from one currency to another"""
try:
return self.currency_service.convert(amount, from_currency, to_currency)
except Exception as exc:
return (
"Live currency conversion is currently unavailable "
f"({exc}). Continue with the user's provided currency and, "
"if needed, clearly label any conversion as an approximate estimate."
)
return [convert_currency]