3
3
from langchain_core .callbacks import CallbackManagerForToolRun
4
4
from langchain_core .tools import BaseTool
5
5
from app .config import Configuration
6
- import datetime
6
+ from datetime import datetime
7
+ import dateparser
7
8
8
9
9
10
class WeatherTool (BaseTool ):
@@ -13,23 +14,32 @@ class WeatherTool(BaseTool):
13
14
description : str = (
14
15
"A wrapper around Weather Search. "
15
16
"Useful for when you need to know current or upcoming weather. "
16
- "Input should be location ."
17
+ "Tool have one optional argument 'date' ."
17
18
)
18
- cache : str = ""
19
+ cache : dict = {}
19
20
config : Configuration = None
20
21
21
22
def _run (
22
23
self ,
23
- location : str ,
24
+ date : str ,
24
25
run_manager : Optional [CallbackManagerForToolRun ] = None ,
25
26
) -> str :
26
27
"""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 :
28
38
location = self .config .prompt_replacements ["location" ]
29
39
location = location .replace ("," , "" ).replace (" " , "-" )
30
40
response = requests .get (f"https://wttr.in/{ location } ?format=j1" )
31
41
32
- current_time = datetime . datetime . now () .strftime ("%Y-%m-%d %H:%M" )
42
+ current_time = now .strftime ("%Y-%m-%d %H:%M" )
33
43
weather_info = [f"Current time is { current_time } " ]
34
44
if response .status_code == 200 :
35
45
data = response .json ()
@@ -52,12 +62,16 @@ def _run(
52
62
weather_info .append (f"- Wind Speed (km/h): { current_condition ['windspeedKmph' ]} " )
53
63
weather_info .append ("# Forecast:" )
54
64
for current_condition in data ['weather' ]:
65
+
66
+ if filter_date is not None and filter_date_date != current_condition ['date' ]:
67
+ continue
68
+
55
69
weather_info .append (f"- { current_condition ['date' ]} " )
56
70
for description in current_condition ['hourly' ]:
57
71
time = str (description ['time' ]).zfill (4 )
58
72
weather_info .append (
59
73
f"-- { time [:2 ]} :{ time [2 :]} : { description ['weatherDesc' ][0 ]['value' ]} , { description ['tempC' ]} °C, { description ['chanceofrain' ]} % rain, { description ['windspeedKmph' ]} km/h" )
60
74
61
- self .cache = "\n " .join (weather_info )
75
+ self .cache [ filter_date_date ] = "\n " .join (weather_info )
62
76
63
- return self .cache
77
+ return self .cache [ filter_date_date ]
0 commit comments