a
    bg                     @   sn   d Z ddlZddlmZmZmZ ddlmZ ddlm	Z	 ddl
mZmZmZ eddd	d
G dd deZdS )z#Chain that calls Google Places API.    N)AnyDictOptional)
deprecated)get_from_dict_or_env)	BaseModel
ConfigDictmodel_validatorz0.0.33z1.0z1langchain_google_community.GooglePlacesAPIWrapper)ZsinceZremovalZalternative_importc                   @   s   e Zd ZU dZdZee ed< dZe	ed< dZ
ee ed< edddZed	d
eee	dddZeedddZeee dddZeee	f ee dddZdS )GooglePlacesAPIWrappera  Wrapper around Google Places API.

    To use, you should have the ``googlemaps`` python package installed,
     **an API key for the google maps platform**,
     and the environment variable ''GPLACES_API_KEY''
     set with your API key , or pass 'gplaces_api_key'
     as a named parameter to the constructor.

    By default, this will return the all the results on the input query.
     You can use the top_k_results argument to limit the number of results.

    Example:
        .. code-block:: python


            from langchain_community.utilities import GooglePlacesAPIWrapper
            gplaceapi = GooglePlacesAPIWrapper()
    Ngplaces_api_keygoogle_map_clienttop_k_resultsTZforbid)Zarbitrary_types_allowedextrabefore)mode)valuesreturnc                 C   sN   t |dd}||d< zddl}|||d< W n tyH   tdY n0 |S )z6Validate that api key is in your environment variable.r   ZGPLACES_API_KEYr   Nr   z\Could not import googlemaps python package. Please install it with `pip install googlemaps`.)r   
googlemapsZClientImportError)clsr   r   r    r   }/var/www/html/cobodadashboardai.evdpl.com/venv/lib/python3.9/site-packages/langchain_community/utilities/google_places_api.pyvalidate_environment-   s    
z+GooglePlacesAPIWrapper.validate_environment)queryr   c                 C   s   | j |d }t|}g }|dkr(dS | jdu r6|n
t|| j}t|D ],}|| }| |d }|durJ|| qJddd t	|D S )	zDRun Places search and get k number of places that exists that match.resultsr   z@Google Places did not find any places that match the descriptionNplace_id
c                 S   s"   g | ]\}}|d   d| qS )   z. r   ).0iitemr   r   r   
<listcomp>W       z.GooglePlacesAPIWrapper.run.<locals>.<listcomp>)
r   placeslenr   minrangefetch_place_detailsappendjoin	enumerate)selfr   Zsearch_resultsZnum_to_returnr#   r   resultdetailsr   r   r   run@   s    
zGooglePlacesAPIWrapper.run)r   r   c              
   C   s`   z$| j |}||d< | |}|W S  tyZ } ztd|  W Y d }~d S d }~0 0 d S )Nr   z0An Error occurred while fetching place details: )r   Zplaceformat_place_details	Exceptionloggingerror)r+   r   place_detailsformatted_detailser   r   r   r'   Y   s    
z*GooglePlacesAPIWrapper.fetch_place_details)r3   r   c           	   
   C   s   z| di  dd}| di  dd}| di  dd}| di  dd}| di  dd}| d| d	| d
| d| d
}|W S  ty } ztd|  W Y d }~d S d }~0 0 d S )Nr,   nameUnknownZformatted_addressZformatted_phone_numberwebsiter   z

Address: z
Google place ID: z
Phone: z

Website: z

z2An error occurred while formatting place details: )getr0   r1   r2   )	r+   r3   r6   addressZphone_numberr8   r   r4   r5   r   r   r   r/   c   s,    z+GooglePlacesAPIWrapper.format_place_details)__name__
__module____qualname____doc__r   r   str__annotations__r   r   r   intr   Zmodel_configr	   classmethodr   r   r.   r'   r/   r   r   r   r   r
      s   

r
   )r>   r1   typingr   r   r   Zlangchain_core._api.deprecationr   Zlangchain_core.utilsr   Zpydanticr   r   r	   r
   r   r   r   r   <module>   s   