Add new weather API function to fetch additional open weather data
Description
This PR introduces a new API function, and a set of test cases to validate its functionality. This feature enhancement enhances the CAMEL project by providing access to current weather data based on geographical coordinates. This update integrates features related to the OpenWeatherMap API, enabling new weather-related functionality.
In order to use these new features, an API key from OpenWeatherMap is required. Contributors and users who want to run this code will need to:
- Obtain an API key from OpenWeatherMap by signing up at https://openweathermap.org/api (free API is enough to this function).
- Once you have the key, it should be stored securely in the environment variables as 'OPENWEATHER_API_KEY'. This is critical to ensure the security and privacy of your API key. Please do not hardcode the key into your codebase.
Please ensure the 'OPENWEATHER_API_KEY' is correctly configured in the environment before running the application. Incorrect handling of the API key may lead to errors or unexpected behavior.
Thank you for your attention to this requirement as we integrate these exciting new weather features.
Motivation and Context
Motivation:
Weather data is a valuable resource for a wide range of applications, from optimizing energy consumption to improving travel safety. Users of the CAMEL project have expressed the need for a reliable and easy-to-use way to access current weather information based on geographical coordinates. This addition of the fetch_current_weather API fulfills that need, providing real-time weather data to CAMEL users, enhancing their experience, and enabling new possibilities for data analysis.
Context:
Prior to this change, users had to rely on external services or implement custom solutions to obtain weather data. The fetch_current_weather API simplifies this process, making it more accessible and integrated within the CAMEL framework. This addition aligns with our project's goal of providing comprehensive and user-friendly AI capabilities. It leverages the OpenWeatherMap API to retrieve accurate and up-to-date weather information, which complements the existing functionality of CAMEL.
An issue has been raised to propose this change (https://github.com/camel-ai/camel/issues/57)
Types of changes
- [ ] Bug fix (non-breaking change which fixes an issue)
- [x] New feature (non-breaking change which adds core functionality)
- [ ] Breaking change (fix or feature that would cause existing functionality to change)
- [ ] Documentation (update in the documentation)
- [ ] Example (update in the folder of example)
Implemented Tasks
- [x] Current Weather
- [ ] 3-hour Forecast 5 days
- [ ] Basic weather maps
- [ ] Air Pollution API
- [ ] Geocoding API
- [ ] A example of role playing with weather function
Checklist
- [x] I have read the CONTRIBUTION guide. (required)
- [ ] My change requires a change to the documentation.
- [x] I have updated the tests accordingly. (required for a bug fix or a new feature)
- [ ] I have updated the documentation accordingly.
I suspect that the test failure might be attributed to the lack of an API KEY for https://openweathermap.org/api in the repository's environment variables, necessitating only the application for a free plan. I would deeply appreciate any support or guidance the project maintainers could offer regarding this matter. Thank you in advance for your time and assistance.
Thanks @yiyiyi0817 for the PR! The current implementation is based on lat and lon which is great. But it may not be very convenient for the users. Should we also support fetching the weather by the city name? It would be also great if we could support units for both 'celsius' and 'fahrenheit'.
There is a geocoding-api from openweathermap: https://openweathermap.org/api/geocoding-api.
We can also use pyowm if https://pyowm.readthedocs.io/en/latest/v3/code-recipes.html.
Here is an example of how we can do it by GPT-4:
https://chat.openai.com/share/cb43a906-0d28-4894-8037-01d795617c6f
Thank you very much for @lightaime. I noticed that open weather map needs to use geocoding-api to obtain the latitude and longitude of the city. However, I am not sure whether geocoding-api should be a separate geocoding_functions, because it may not only need the weather query function to use the latitude and longitude and city name conversion, but may also need geocoding-api for other needs, such as calculating the time difference and so on.
Thank you very much for @lightaime. I noticed that open weather map needs to use geocoding-api to obtain the latitude and longitude of the city. However, I am not sure whether geocoding-api should be a separate geocoding_functions, because it may not only need the weather query function to use the latitude and longitude and city name conversion, but may also need geocoding-api for other needs, such as calculating the time difference and so on.
How about let's use pyowm? Here is an example by GPT-4:
import pyowm
def get_weather(city_name, api_key, units='metric'):
owm = pyowm.OWM(api_key)
observation = owm.weather_at_place(city_name)
weather = observation.get_weather()
temperature = weather.get_temperature(units)['temp']
humidity = weather.get_humidity()
weather_conditions = weather.get_status()
print(f'The current weather in {city_name} is:')
print(f'Temperature: {temperature}°{get_unit_symbol(units)}')
print(f'Humidity: {humidity}%')
print(f'Weather conditions: {weather_conditions}')
def get_unit_symbol(units):
if units == 'metric':
return 'C'
elif units == 'imperial':
return 'F'
else:
return 'K'
# Replace YOUR_API_KEY with your OpenWeatherMap API key
api_key = 'YOUR_API_KEY'
city_name = input('Enter the name of the city: ')
units = input('Enter the units (standard, metric, or imperial): ')
get_weather(city_name, api_key, units)
Sounds good! Let's use pyowm. Thanks for providing the example! Using pyowm seems like it might be more convenient and robust compared to requests. I'll make the necessary changes to my code.
@dandansamax Of course. I'd be happy to add an example of role-playing with a weather function after completing this.
Move to #334
Closed since this is a duplicated PR: https://github.com/camel-ai/camel/pull/334.