|
| 1 | +# =========== Copyright 2023 @ CAMEL-AI.org. All Rights Reserved. =========== |
| 2 | +# Licensed under the Apache License, Version 2.0 (the “License”); |
| 3 | +# you may not use this file except in compliance with the License. |
| 4 | +# You may obtain a copy of the License at |
| 5 | +# |
| 6 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 7 | +# |
| 8 | +# Unless required by applicable law or agreed to in writing, software |
| 9 | +# distributed under the License is distributed on an “AS IS” BASIS, |
| 10 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 11 | +# See the License for the specific language governing permissions and |
| 12 | +# limitations under the License. |
| 13 | +# =========== Copyright 2023 @ CAMEL-AI.org. All Rights Reserved. =========== |
| 14 | +import os |
| 15 | +from typing import List |
| 16 | + |
| 17 | +from camel.functions import OpenAIFunction |
| 18 | + |
| 19 | + |
| 20 | +def get_openweathermap_api_key() -> str: |
| 21 | + r"""Retrieve the OpenWeatherMap API key from environment variables. |
| 22 | +
|
| 23 | + Returns: |
| 24 | + str: The OpenWeatherMap API key. |
| 25 | +
|
| 26 | + Raises: |
| 27 | + ValueError: If the API key is not found in the environment variables. |
| 28 | + """ |
| 29 | + # Get `OPENWEATHERMAP_API_KEY` here: https://openweathermap.org |
| 30 | + OPENWEATHERMAP_API_KEY = os.environ.get('OPENWEATHERMAP_API_KEY') |
| 31 | + if not OPENWEATHERMAP_API_KEY: |
| 32 | + raise ValueError("`OPENWEATHERMAP_API_KEY` not found in environment " |
| 33 | + "variables. Get `OPENWEATHERMAP_API_KEY` here: " |
| 34 | + "`https://openweathermap.org`.") |
| 35 | + return OPENWEATHERMAP_API_KEY |
| 36 | + |
| 37 | + |
| 38 | +def get_weather_data(city: str, temp_units: str = 'kelvin', |
| 39 | + wind_units: str = 'meters_sec', |
| 40 | + visibility_units: str = 'meters', |
| 41 | + time_units: str = 'unix') -> str: |
| 42 | + r"""Fetch and return a comprehensive weather report for a given city as a |
| 43 | + string. The report includes current weather conditions, temperature, |
| 44 | + wind details, visibility, and sunrise/sunset times, all formatted as |
| 45 | +
|
| 46 | + The function interacts with the OpenWeatherMap API to retrieve the data. |
| 47 | +
|
| 48 | + Args: |
| 49 | + city (string): The name of the city for which the weather information |
| 50 | + is desired. Format "City, CountryCode" (e.g., "Paris, FR" |
| 51 | + for Paris, France). If the country code is not provided, |
| 52 | + the API will search for the city in all countries, which |
| 53 | + may yield incorrect results if multiple cities with the |
| 54 | + same name exist. |
| 55 | + temp_units (string): Units for temperature. Options: 'kelvin', |
| 56 | + 'celsius', 'fahrenheit'. (default: :obj:`kelvin`) |
| 57 | + wind_units (string): Units for wind speed. Options: 'meters_sec', |
| 58 | + 'miles_hour', 'knots', 'beaufort'. (default: :obj:`meters_sec`) |
| 59 | + visibility_units (string): Units for visibility distance. Options: |
| 60 | + 'meters', 'miles'. (default: :obj:`meters`) |
| 61 | + time_units (string): Format for sunrise and sunset times. Options: |
| 62 | + 'unix', 'iso', 'date'. (default: :obj:`unix`) |
| 63 | +
|
| 64 | + Returns: |
| 65 | + str: A string containing the fetched weather data, formatted in a |
| 66 | + readable manner. If an error occurs, a message indicating the |
| 67 | + error will be returned instead. |
| 68 | +
|
| 69 | + Example of return string: |
| 70 | + "Weather in Paris, FR: 15°C, feels like 13°C. Max temp: 17°C, Min temp |
| 71 | + : 12°C. |
| 72 | + Wind: 5 m/s at 270 degrees. Visibility: 10 kilometers. |
| 73 | + Sunrise at 05:46:05 (UTC), Sunset at 18:42:20 (UTC)." |
| 74 | +
|
| 75 | + Note: |
| 76 | + Please ensure that the API key is valid and has permissions to access |
| 77 | + the weather data. |
| 78 | + """ |
| 79 | + # NOTE: This tool may not work as expected since the input arguments like |
| 80 | + # `time_units` should be enum types which are not supported yet. |
| 81 | + |
| 82 | + try: |
| 83 | + import pyowm |
| 84 | + except ImportError: |
| 85 | + raise ImportError( |
| 86 | + "Please install `pyowm` first. You can install it by running " |
| 87 | + "`pip install pyowm`.") |
| 88 | + |
| 89 | + OPENWEATHERMAP_API_KEY = get_openweathermap_api_key() |
| 90 | + owm = pyowm.OWM(OPENWEATHERMAP_API_KEY) |
| 91 | + mgr = owm.weather_manager() |
| 92 | + |
| 93 | + try: |
| 94 | + observation = mgr.weather_at_place(city) |
| 95 | + weather = observation.weather |
| 96 | + |
| 97 | + # Temperature |
| 98 | + temperature = weather.temperature(temp_units) |
| 99 | + |
| 100 | + # Wind |
| 101 | + wind_data = observation.weather.wind(unit=wind_units) |
| 102 | + wind_speed = wind_data.get('speed') |
| 103 | + # 'N/A' if the degree is not available |
| 104 | + wind_deg = wind_data.get('deg', 'N/A') |
| 105 | + |
| 106 | + # Visibility |
| 107 | + visibility_distance = observation.weather.visibility_distance |
| 108 | + visibility = (str(visibility_distance) if visibility_units == 'meters' |
| 109 | + else str(observation.weather.visibility(unit='miles'))) |
| 110 | + |
| 111 | + # Sunrise and Sunset |
| 112 | + sunrise_time = str(weather.sunrise_time(timeformat=time_units)) |
| 113 | + sunset_time = str(weather.sunset_time(timeformat=time_units)) |
| 114 | + |
| 115 | + # Compile all the weather details into a report string |
| 116 | + weather_report = ( |
| 117 | + f"Weather in {city}: {temperature['temp']}°{temp_units.title()}, " |
| 118 | + f"feels like {temperature['feels_like']}°{temp_units.title()}. " |
| 119 | + f"Max temp: {temperature['temp_max']}°{temp_units.title()}, " |
| 120 | + f"Min temp: {temperature['temp_min']}°{temp_units.title()}. " |
| 121 | + f"Wind: {wind_speed} {wind_units} at {wind_deg} degrees. " |
| 122 | + f"Visibility: {visibility} {visibility_units}. " |
| 123 | + f"Sunrise at {sunrise_time}, Sunset at {sunset_time}.") |
| 124 | + |
| 125 | + return weather_report |
| 126 | + |
| 127 | + except Exception as e: |
| 128 | + error_message = ( |
| 129 | + f"An error occurred while fetching weather data for {city}: " |
| 130 | + f"{str(e)}.") |
| 131 | + return error_message |
| 132 | + |
| 133 | + |
| 134 | +WEATHER_FUNCS: List[OpenAIFunction] = [ |
| 135 | + OpenAIFunction(func) for func in [get_weather_data] |
| 136 | +] |
0 commit comments