File size: 2,747 Bytes
7236cee | 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 68 69 70 71 72 73 74 75 | from langchain_community.tools.yahoo_finance_news import YahooFinanceNewsTool
from langchain_community.utilities import OpenWeatherMapAPIWrapper
from langchain_community.utilities import WikipediaAPIWrapper
from langchain_community.tools import WikipediaQueryRun
from langchain_tavily.tavily_search import TavilySearch
from agents.coder.agent import coder_agent
from agents.utils.smart_home import change_device_states
from timezonefinder import TimezoneFinder
from langchain_core.tools import tool
from dotenv import load_dotenv
from geopy import Nominatim
import datetime
import pytz
import os
load_dotenv()
tf = TimezoneFinder()
web_search = TavilySearch(
max_results=5,
topic="general",
include_answer=True
)
yahoo = YahooFinanceNewsTool()
geolocator = Nominatim(user_agent="my_geocoder")
wikipedia = WikipediaQueryRun(api_wrapper=WikipediaAPIWrapper())
weekday_mapping = ("Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday")
@tool
def current_time(location: str = None):
"""Get the current time for a location or current position."""
try:
if location:
location_data = geolocator.geocode(location)
if location_data:
timezone = pytz.timezone(tf.timezone_at(lat=location_data.latitude, lng=location_data.longitude))
location_name = location.capitalize()
else:
return f"Could not find location: {location}"
else:
# Use environment location if available
lat = os.getenv('LATITUDE')
lon = os.getenv('LONGITUDE')
if lat and lon:
timezone = pytz.timezone(tf.timezone_at(lat=float(lat), lng=float(lon)))
location_name = os.getenv('LOCATION', 'Current Location')
else:
timezone = pytz.UTC
location_name = 'UTC'
current_dt = datetime.datetime.now(timezone)
weekday = weekday_mapping[current_dt.weekday()]
return f"Location: {location_name}; Current Date and Time: {current_dt.strftime('%Y-%m-%d %H:%M')}, {weekday}."
except Exception as e:
return f"Error getting current time: {str(e)}"
@tool
def weather(location: str = None):
"""Get the current weather for a location or current position."""
try:
weather_wrapper = OpenWeatherMapAPIWrapper(
openweathermap_api_key=os.getenv('OPENWEATHERMAP_API_KEY')
)
if not location:
location = os.getenv('LOCATION', 'Unknown')
return weather_wrapper.run(location=location)
except Exception as e:
return f"Error getting weather: {str(e)}"
tools = [yahoo, web_search, current_time, weather, change_device_states, coder_agent]
|