U
    h]	                     @   sH   d Z ddlmZmZmZ ddlmZmZ ddlm	Z	 G dd deZ
dS )z+Util that calls OpenWeatherMap using PyOWM.    )AnyDictOptional)	BaseModelroot_validator)get_from_dict_or_envc                   @   st   e Zd ZU dZeed< dZee ed< G dd dZ	e
ddeed	d
dZeeedddZeedddZdS )OpenWeatherMapAPIWrapperzWrapper for OpenWeatherMap API using PyOWM.

    Docs for using:

    1. Go to OpenWeatherMap and sign up for an API key
    2. Save your API KEY into OPENWEATHERMAP_API_KEY env variable
    3. pip install pyowm
    owmNopenweathermap_api_keyc                   @   s   e Zd ZdZdS )zOpenWeatherMapAPIWrapper.ConfigZforbidN)__name__
__module____qualname__extra r   r   P/tmp/pip-unpacked-wheel-9gdii04g/langchain_community/utilities/openweathermap.pyConfig   s   r   T)pre)valuesreturnc                 C   sL   t |dd}zddl}W n tk
r4   tdY nX ||}||d< |S )z,Validate that api key exists in environment.r
   ZOPENWEATHERMAP_API_KEYr   NzBpyowm is not installed. Please install it with `pip install pyowm`r	   )r   pyowmImportErrorZOWM)clsr   r
   r   r	   r   r   r   validate_environment   s      

z-OpenWeatherMapAPIWrapper.validate_environment)locationwr   c           
      C   s   |j }| }|j}|d}|j}|j}|j}	d| d| d|d  d|d  d| d	|d
  d|d  d|d  d|d  d| d| d|	 dS )NZcelsiuszIn z6, the current weather is as follows:
Detailed status: z
Wind speed: speedz m/s, direction: degu   °
Humidity: z%
Temperature: 
  - Current: tempu   °C
  - High: Ztemp_maxu   °C
  - Low: Ztemp_minu   °C
  - Feels like: Z
feels_likeu
   °C
Rain: z
Heat index: z
Cloud cover: %)detailed_statuswindhumiditytemperaturerain
heat_indexclouds)
selfr   r   r   r    r!   r"   r#   r$   r%   r   r   r   _format_weather_info-   s    
dz-OpenWeatherMapAPIWrapper._format_weather_info)r   r   c                 C   s&   | j  }||}|j}| ||S )z=Get the current weather information for a specified location.)r	   Zweather_managerZweather_at_placeZweatherr'   )r&   r   ZmgrZobservationr   r   r   r   runE   s    

zOpenWeatherMapAPIWrapper.run)r   r   r   __doc__r   __annotations__r
   r   strr   r   r   r   r'   r(   r   r   r   r   r   	   s   
	r   N)r)   typingr   r   r   Zlangchain_core.pydantic_v1r   r   Zlangchain_core.utilsr   r   r   r   r   r   <module>   s   