Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add new weather API function to fetch additional open weather data #328

Closed
wants to merge 1 commit into from

Conversation

yiyiyi0817
Copy link
Member

@yiyiyi0817 yiyiyi0817 commented Oct 23, 2023

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:

  1. Obtain an API key from OpenWeatherMap by signing up at https://openweathermap.org/api (free API is enough to this function).
  2. 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 (#57)

Types of changes

  • Bug fix (non-breaking change which fixes an issue)
  • 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

  • Current Weather
  • 3-hour Forecast 5 days
  • Basic weather maps
  • Air Pollution API
  • Geocoding API
  • A example of role playing with weather function

Checklist

  • I have read the CONTRIBUTION guide. (required)
  • My change requires a change to the documentation.
  • I have updated the tests accordingly. (required for a bug fix or a new feature)
  • I have updated the documentation accordingly.

@yiyiyi0817 yiyiyi0817 changed the title Add new weather API function to fetch additional weather data Add new weather API function to fetch additional open weather data Oct 23, 2023
@yiyiyi0817
Copy link
Member Author

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.

@yiyiyi0817 yiyiyi0817 self-assigned this Oct 23, 2023
@lightaime
Copy link
Member

lightaime commented Oct 24, 2023

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

@yiyiyi0817
Copy link
Member Author

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.

@lightaime
Copy link
Member

lightaime commented Oct 24, 2023

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)

@yiyiyi0817
Copy link
Member Author

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.

Copy link
Contributor

@zhiyu-01 zhiyu-01 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You are doing well, keep trying!

Copy link
Collaborator

@dandansamax dandansamax left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The current implementation looks very good! Thank you.

A small improvement can be adding an exmaple to let a ChatAgent use this API. After finishing @lightaime 's request, could you consider adding this?

@yiyiyi0817
Copy link
Member Author

@dandansamax Of course. I'd be happy to add an example of role-playing with a weather function after completing this.

@dandansamax
Copy link
Collaborator

Move to #334

@dandansamax dandansamax closed this Nov 1, 2023
@WHALEEYE WHALEEYE deleted the weather_function branch July 24, 2024 23:55
@WHALEEYE WHALEEYE restored the weather_function branch July 25, 2024 00:04
@WHALEEYE WHALEEYE reopened this Jul 25, 2024
@lightaime
Copy link
Member

Closed since this is a duplicated PR: #334.

@lightaime lightaime closed this Aug 21, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants