tools.tools.toolkits.weather¶
Weather Information Toolkit Module.
This toolkit provides tools for retrieving current weather information using the OpenWeatherMap API. It supports structured data parsing and temperature unit conversion between Celsius and Fahrenheit.
Features: - City and country-based weather lookups - Structured data output with detailed weather metrics - Customizable temperature units (Celsius/Fahrenheit) - Automatic parsing of OpenWeatherMap API responses
- Required Environment Variables:
OPENWEATHERMAP_API_KEY: Your OpenWeatherMap API key (will prompt if not found)
Examples
>>> from haive.tools.toolkits.weather import get_weather_by_city_country
>>> # Get structured weather data for Tokyo, Japan
>>> weather = get_weather_by_city_country("Tokyo", "jp", parse=True, temperature_unit="celsius")
>>> print(f"Current temperature in {weather['location']}: {weather['temp_current_c']}°C")
Current temperature in Tokyo, JP: 15.2°C
>>> # Get raw weather data for New York, US in Fahrenheit
>>> weather = get_weather_by_city_country("New York", "us", parse=True, temperature_unit="fahrenheit")
>>> print(f"Feels like: {weather['temp_feels_like_c']}°F")
Feels like: 46.58°F
Attributes¶
Classes¶
Input schema for weather query by city and country. |
|
Structured weather data model. |
Functions¶
|
Fetch current weather information for a specific city and country. |
Module Contents¶
- class tools.tools.toolkits.weather.CityCountryWeatherInput(/, **data: Any)¶
Bases:
pydantic.BaseModelInput schema for weather query by city and country.
This model defines the parameters needed to request weather information from the OpenWeatherMap API, including location and formatting options.
- temperature_unit: Literal['celsius', 'fahrenheit'] = None¶
- class tools.tools.toolkits.weather.WeatherData(/, **data: Any)¶
Bases:
pydantic.BaseModelStructured weather data model.
This model represents parsed weather information from the OpenWeatherMap API, with fields for various weather metrics including temperature, wind, humidity, and precipitation.
- convert_to_fahrenheit() WeatherData¶
Convert all temperature values from Celsius to Fahrenheit.
- Returns:
A new WeatherData instance with temperatures in Fahrenheit.
- Return type:
- classmethod from_openweather_response(response: str) WeatherData¶
Parse the text response from OpenWeatherMap API into structured data.
This method uses regular expressions to extract weather information from the text-based API response and creates a structured WeatherData object.
- Parameters:
response (str) – Raw text response from the OpenWeatherMap API.
- Returns:
Structured weather data extracted from the response.
- Return type:
- tools.tools.toolkits.weather.get_weather_by_city_country(city: str, country: str, parse: bool = True, temperature_unit: Literal['celsius', 'fahrenheit'] = 'celsius')¶
Fetch current weather information for a specific city and country.
This function retrieves weather data from the OpenWeatherMap API for the specified location. It can return either raw API response or structured data with various weather metrics.
- Parameters:
city (str) – The name of the city (e.g., “London”, “Tokyo”).
country (str) – The 2-letter ISO country code in lowercase (e.g., “gb”, “jp”).
parse (bool, optional) – Whether to parse the response into structured data. Defaults to True.
temperature_unit (str, optional) – Unit for temperature values, either “celsius” or “fahrenheit”. Defaults to “celsius”.
- Returns:
- Structured weather data as a dictionary if parse=True,
otherwise the raw API response as a string.
- Return type:
- Raises:
ValueError – If the API key is not provided and the user cancels the prompt.
Exception – If the weather API request fails.
- tools.tools.toolkits.weather.weather_tool¶