| 5 | |
| 6 | |
| 7 | def build_tool(config) -> Tool: |
| 8 | tool = Tool( |
| 9 | "Weather Info", |
| 10 | "Look up weather information", |
| 11 | name_for_model="Weather", |
| 12 | description_for_model="Plugin for look up weather information", |
| 13 | logo_url="https://cdn.weatherapi.com/v4/images/weatherapi_logo.png", |
| 14 | contact_email="hello@contact.com", |
| 15 | legal_info_url="hello@legal.com" |
| 16 | ) |
| 17 | |
| 18 | KEY = config["subscription_key"] |
| 19 | URL_CURRENT_WEATHER= "http://api.weatherapi.com/v1/current.json" |
| 20 | URL_FORECAST_WEATHER = "http://api.weatherapi.com/v1/forecast.json" |
| 21 | |
| 22 | @tool.get("/get_weather_today") |
| 23 | def get_weather_today(location : str): |
| 24 | '''Get today's the weather |
| 25 | ''' |
| 26 | param = { |
| 27 | "key": KEY, |
| 28 | "q": location |
| 29 | } |
| 30 | res_completion = requests.get(URL_CURRENT_WEATHER, params=param) |
| 31 | data = json.loads(res_completion.text.strip()) |
| 32 | output = {} |
| 33 | output["overall"]= f"{data['current']['condition']['text']},\n" |
| 34 | output["name"]= f"{data['location']['name']},\n" |
| 35 | output["region"]= f"{data['location']['region']},\n" |
| 36 | output["country"]= f"{data['location']['country']},\n" |
| 37 | output["localtime"]= f"{data['location']['localtime']},\n" |
| 38 | output["temperature"]= f"{data['current']['temp_c']}(C), {data['current']['temp_f']}(F),\n" |
| 39 | output["percipitation"]= f"{data['current']['precip_mm']}(mm), {data['current']['precip_in']}(inch),\n" |
| 40 | output["pressure"]= f"{data['current']['pressure_mb']}(milibar),\n" |
| 41 | output["humidity"]= f"{data['current']['humidity']},\n" |
| 42 | output["cloud"]= f"{data['current']['cloud']},\n" |
| 43 | output["body temperature"]= f"{data['current']['feelslike_c']}(C), {data['current']['feelslike_f']}(F),\n" |
| 44 | output["wind speed"]= f"{data['current']['gust_kph']}(kph), {data['current']['gust_mph']}(mph),\n" |
| 45 | output["visibility"]= f"{data['current']['vis_km']}(km), {data['current']['vis_miles']}(miles),\n" |
| 46 | output["UV index"]= f"{data['current']['uv']},\n" |
| 47 | |
| 48 | text_output = f"Today's weather report for {data['location']['name']} is:\n"+"".join([f"{key}: {output[key]}" for key in output.keys()]) |
| 49 | return text_output |
| 50 | |
| 51 | @tool.get("/forecast_weather") |
| 52 | def forecast_weather(location : str, days : int): |
| 53 | '''Forecast weather in the upcoming days. |
| 54 | ''' |
| 55 | param = { |
| 56 | "key": KEY, |
| 57 | "q": location, |
| 58 | "days": int(days), |
| 59 | } |
| 60 | res_completion = requests.get(URL_FORECAST_WEATHER, params=param) |
| 61 | res_completion = json.loads(res_completion.text.strip()) |
| 62 | MAX_DAYS = 14 |
| 63 | res_completion = res_completion["forecast"]["forecastday"][int(days)-1 if int(days) < MAX_DAYS else MAX_DAYS-1] |
| 64 | output_dict = {} |