Skip to content

Commit 8ef2817

Browse files
improve weather tool
1 parent 1f0b3f2 commit 8ef2817

File tree

2 files changed

+28
-13
lines changed

2 files changed

+28
-13
lines changed

app/tools/my_calendar.py

+6-5
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,12 @@ class CalendarEventTool(BaseTool):
1111
"""Tool for fetching calendar events."""
1212

1313
name: str = "calendar_events"
14-
description: str = "Use this tool to fetch upcoming calendar events. Tool have one optional parameter 'date'."
14+
description: str = "Use this tool to fetch upcoming calendar events. Tool have one optional argument 'date'."
1515
calendar: Calendar = None
1616

1717
def _run(
1818
self,
19-
day: str = "Today",
19+
date: str = "Today",
2020
run_manager: Optional[CallbackManagerForToolRun] = None,
2121
) -> str:
2222
self.calendar.get_events()
@@ -30,9 +30,10 @@ def _run(
3030
]
3131

3232
filter_date = None
33-
if day is not None and day != "":
34-
filter_date = dateparser.parse(day)
35-
filter_date_date = filter_date.strftime("%Y-%m-%d")
33+
if date is not None and date != "":
34+
filter_date = dateparser.parse(date)
35+
if filter_date is not None:
36+
filter_date_date = filter_date.strftime("%Y-%m-%d")
3637

3738
for event in events:
3839
event_date = event["start"].strftime("%Y-%m-%d")

app/tools/weather.py

+22-8
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@
33
from langchain_core.callbacks import CallbackManagerForToolRun
44
from langchain_core.tools import BaseTool
55
from app.config import Configuration
6-
import datetime
6+
from datetime import datetime
7+
import dateparser
78

89

910
class WeatherTool(BaseTool):
@@ -13,23 +14,32 @@ class WeatherTool(BaseTool):
1314
description: str = (
1415
"A wrapper around Weather Search. "
1516
"Useful for when you need to know current or upcoming weather. "
16-
"Input should be location."
17+
"Tool have one optional argument 'date'."
1718
)
18-
cache: str = ""
19+
cache: dict = {}
1920
config: Configuration = None
2021

2122
def _run(
2223
self,
23-
location: str,
24+
date: str,
2425
run_manager: Optional[CallbackManagerForToolRun] = None,
2526
) -> str:
2627
"""Use the tool."""
27-
if not self.cache:
28+
29+
now = datetime.now()
30+
filter_date = None
31+
filter_date_date = ""
32+
if date is not None and date != "":
33+
filter_date = dateparser.parse(date)
34+
if filter_date is not None:
35+
filter_date_date = filter_date.strftime("%Y-%m-%d")
36+
37+
if filter_date_date not in self.cache:
2838
location = self.config.prompt_replacements["location"]
2939
location = location.replace(",", "").replace(" ", "-")
3040
response = requests.get(f"https://wttr.in/{location}?format=j1")
3141

32-
current_time = datetime.datetime.now().strftime("%Y-%m-%d %H:%M")
42+
current_time = now.strftime("%Y-%m-%d %H:%M")
3343
weather_info = [f"Current time is {current_time}"]
3444
if response.status_code == 200:
3545
data = response.json()
@@ -52,12 +62,16 @@ def _run(
5262
weather_info.append(f"- Wind Speed (km/h): {current_condition['windspeedKmph']}")
5363
weather_info.append("# Forecast:")
5464
for current_condition in data['weather']:
65+
66+
if filter_date is not None and filter_date_date != current_condition['date']:
67+
continue
68+
5569
weather_info.append(f"- {current_condition['date']}")
5670
for description in current_condition['hourly']:
5771
time = str(description['time']).zfill(4)
5872
weather_info.append(
5973
f"-- {time[:2]}:{time[2:]}: {description['weatherDesc'][0]['value']}, {description['tempC']}°C, {description['chanceofrain']}% rain, {description['windspeedKmph']} km/h")
6074

61-
self.cache = "\n".join(weather_info)
75+
self.cache[filter_date_date] = "\n".join(weather_info)
6276

63-
return self.cache
77+
return self.cache[filter_date_date]

0 commit comments

Comments
 (0)