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

Update weather function API by using pyowm #334

Merged
merged 8 commits into from
Nov 9, 2023
Merged

Conversation

yiyiyi0817
Copy link
Member

@yiyiyi0817 yiyiyi0817 commented Oct 29, 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 city name.
This update integrates features related to the OpenWeatherMap API, enabling new weather-related functionality. It is a new edition of #328 .

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 'OPENWEATHERMAP_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 'get_current_weather', 'get_current_wind', 'get_current_visibility_distance', 'get_sunrise_sunset' 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. 'get_current_weather', 'get_current_wind', 'get_current_visibility_distance', 'get_sunrise_sunset' 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(Temperature and weather status)
  • Current Wind(speed and direction)
  • Current Visibility Distance
  • Sunrise&Sunset Time
  • Air Pollution
  • Weather Forecast(need to subscribe plan "One Call by Call" and pay for it)

Checklist

Go over all the following points, and put an x in all the boxes that apply.
If you are unsure about any of these, don't hesitate to ask. We are here to help!

  • 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 update weather function API by using pyowm Update weather function API by using pyowm Oct 29, 2023
@yiyiyi0817 yiyiyi0817 self-assigned this Oct 29, 2023
@yiyiyi0817 yiyiyi0817 added API Modifies the API New Feature labels Oct 30, 2023
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.

Excellent code! The tests coverage is high enough. Leave some tiny suggestions.

pyproject.toml Show resolved Hide resolved
examples/function_call/role_playing_with_function.py Outdated Show resolved Hide resolved
camel/functions/weather_functions.py Outdated Show resolved Hide resolved
camel/functions/weather_functions.py Outdated Show resolved Hide resolved
@dandansamax
Copy link
Collaborator

Hi @yiyiyi0817. Sorry, I cannot see your updates. Did you push the changes?

@yiyiyi0817
Copy link
Member Author

yiyiyi0817 commented Oct 30, 2023

Hi @yiyiyi0817. Sorry, I cannot see your updates. Did you push the changes?

@dandansamax Sorry if my previous response was unclear. What I meant is that I will push my updates later.

@yiyiyi0817
Copy link
Member Author

Hello@dandansamax, I have pushed my updates and would appreciate it if you could review them and consider merging them into the master branch. Thank you!

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.

LGTM. Please wait @lightaime to have a look.

@lightaime
Copy link
Member

Thanks @yiyiyi0817! It looks awesome. Some small comments:

  • I am thinking should we merge all functions into one? Because all of them are just a call to mgr.weather_at_place.
  • Also, It could be great to just return a str typed content which is more friendly for LLMs.
  • It would be great to have an example with function calling. Like: https://github.com/camel-ai/camel/blob/master/examples/function_call/role_playing_with_function.py. We can change the task to
    "Assuming the current year is 2023, estimate KAUST's current age and then add 10 more years to this age, and get the current weather of the city where KAUST is located."

@yiyiyi0817
Copy link
Member Author

Thanks @yiyiyi0817! It looks awesome. Some small comments:

  • I am thinking should we merge all functions into one? Because all of them are just a call to mgr.weather_at_place.
  • Also, It could be great to just return a str typed content which is more friendly for LLMs.
  • It would be great to have an example with function calling. Like: https://github.com/camel-ai/camel/blob/master/examples/function_call/role_playing_with_function.py. We can change the task to
    "Assuming the current year is 2023, estimate KAUST's current age and then add 10 more years to this age, and get the current weather of the city where KAUST is located."

Hi Guohao@lightaime, Thank you for your suggestion.

I will convert the output to a string type and include the enhancement in role_playing_with_function.py to add "and get the current weather of the city where KAUST is located."

I've separated the weather module into four distinct functions because the required units for temperature, wind, visibility distance, and sunrise/sunset are different, potentially leading to a function with numerous parameters. Moreover, there might be instances where the LLM would only need information on one of those aspects at a time, not all together.

Hence, I'm contemplating whether combining them would indeed streamline the process or if it's more practical to maintain them as separate functions to cater to specific needs efficiently. I’m open to further discussion on this matter if needed.

@lightaime
Copy link
Member

Hi @yiyiyi0817. Thanks for your explanation. It makes sense to separate them since the units. However, it will also increase the difficulty for function calling because more functions need to be considered in the function calling process. How about we introduce temp_units, wind_units, ... and combine them into a single function? WDYT?

@yiyiyi0817
Copy link
Member Author

Hi @yiyiyi0817. Thanks for your explanation. It makes sense to separate them since the units. However, it will also increase the difficulty for function calling because more functions need to be considered in the function calling process. How about we introduce temp_units, wind_units, ... and combine them into a single function? WDYT?

Given the convenience it brings to function calling, I agree with the proposal to merge these functions into one. I'll push the updated version soon. Thank you for the feedback! @lightaime

@dandansamax
Copy link
Collaborator

Hi. Sorry to modify your code. But I added the OPENWEATHERMAP_API_KEY to the github workflow so that pytest doesn't fail.

@yiyiyi0817
Copy link
Member Author

Hello, I have submitted new code and look forward to your review and merging. @lightaime @dandansamax

Copy link
Member

@lightaime lightaime left a comment

Choose a reason for hiding this comment

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

Thanks @yiyiyi0817. It looks great!! Just some last picky comments.

Comment on lines 35 to 37
raise ValueError("OPENWEATHERMAP_API_KEY not found in environment \
variables. Get OPENWEATHERMAP_API_KEY here: \
https://openweathermap.org/")
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
raise ValueError("OPENWEATHERMAP_API_KEY not found in environment \
variables. Get OPENWEATHERMAP_API_KEY here: \
https://openweathermap.org/")
raise ValueError("`OPENWEATHERMAP_API_KEY` not found in environment \
variables. Get `OPENWEATHERMAP_API_KEY` here: \
`https://openweathermap.org`.")

Comment on lines 23 to 24
"""
Retrieve the OpenWeatherMap API key from environment variables.
Copy link
Member

Choose a reason for hiding this comment

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

We usually use r-string for docstrings.

Suggested change
"""
Retrieve the OpenWeatherMap API key from environment variables.
r"""Retrieve the OpenWeatherMap API key from environment variables.

Raises:
ValueError: If the API key is not found in the environment variables.
"""
# Get OPENWEATHERMAP_API_KEY here: https://openweathermap.org/
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
# Get OPENWEATHERMAP_API_KEY here: https://openweathermap.org/
# Get `OPENWEATHERMAP_API_KEY` here: https://openweathermap.org

Comment on lines 45 to 48
"""
Fetch and return a comprehensive weather report for a given city as a\
string. The report includes current weather conditions, temperature, \
wind details, visibility, and sunrise/sunset times, all formatted as \
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
"""
Fetch and return a comprehensive weather report for a given city as a\
string. The report includes current weather conditions, temperature, \
wind details, visibility, and sunrise/sunset times, all formatted as \
r"""Fetch and return a comprehensive weather report for a given city as a
string. The report includes current weather conditions, temperature,
wind details, visibility, and sunrise/sunset times, all formatted as

Comment on lines 54 to 68
city (string): The name of the city for which the weather information
is desired. Format "City, CountryCode" (e.g., "Paris, FR"
for Paris, France). If the country code is not provided,
the API will search for the city in all countries, which
may yield incorrect results if multiple cities with the
same name exist.
temp_units (string): Units for temperature. Options: 'kelvin',
'celsius', 'fahrenheit'. Default is 'kelvin'.
wind_units (string): Units for wind speed. Options: 'meters_sec',
'miles_hour', 'knots', 'beaufort'.
Default is 'meters_sec'.
visibility_units (string): Units for visibility distance. Options:
'meters', 'miles'. Default is 'meters'.
time_units (string): Format for sunrise and sunset times. Options:
'unix', 'iso', 'date'. Default is 'unix'.
Copy link
Member

Choose a reason for hiding this comment

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

The indentation of the docstring is a bit weird here. We use 4 spaces for indentation usually.

Comment on lines 87 to 90
assert visibility is not None, \
"Visibility information is missing from the report"
assert visibility_min <= visibility <= visibility_max, \
f"Visibility {visibility} not in range for {visibility_units}"
Copy link
Member

Choose a reason for hiding this comment

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

Same here. Can we use brackets instead of back slash for formatting?

Comment on lines 111 to 112
assert match, f"Sunrise and sunset information in {time_units} \
format is missing from the report."
Copy link
Member

Choose a reason for hiding this comment

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

Same here. Can we use brackets instead of back slash for formatting?

Comment on lines 132 to 133
assert sunrise_time < sunset_time, "Sunrise time is not before \
sunset time in the report."
Copy link
Member

Choose a reason for hiding this comment

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

Same here. Can we use brackets instead of a back slash for formatting?

return key


def test_temperature(api_key):
Copy link
Member

Choose a reason for hiding this comment

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

We can also do all the 4 tests in one function. What do you think?

Copy link
Member

Choose a reason for hiding this comment

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

Let's add WEATHER_FUNCS to the function_list and test it.

@yiyiyi0817
Copy link
Member Author

yiyiyi0817 commented Nov 9, 2023

Thanks @yiyiyi0817. It looks great!! Just some last picky comments.

Hello @lightaime, I’ve updated my code format based on the previous suggestions and pushed the new version. If there are still any other details that need attention, please let me know. Thank you!

@lightaime
Copy link
Member

Thanks @yiyiyi0817!!

@lightaime lightaime merged commit a8639ac into master Nov 9, 2023
7 checks passed
@lightaime lightaime deleted the weather_function_pyowm branch November 9, 2023 19:13
FUYICC pushed a commit to FUYICC/camel that referenced this pull request Nov 13, 2023
Co-authored-by: Tianqi Xu <tianqi.xu@kaust.edu.sa>
Co-authored-by: lig <guohao.li@kaust.edu.sa>
FUYICC pushed a commit to FUYICC/camel that referenced this pull request Nov 13, 2023
Co-authored-by: Tianqi Xu <tianqi.xu@kaust.edu.sa>
Co-authored-by: lig <guohao.li@kaust.edu.sa>
@forever-ly forever-ly mentioned this pull request Nov 28, 2023
9 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
API Modifies the API New Feature
Projects
Status: Merged or Closed
Development

Successfully merging this pull request may close these issues.

3 participants