o
    Zh(                     @   s   d Z ddlZddlmZmZmZmZ ddlmZ ddl	m
Z
 ddlmZ ddlmZmZmZmZ G dd	 d	eZG d
d deZG dd de
ZdS )z#Chain that calls Google Places API.    N)AnyDictOptionalType)CallbackManagerForToolRun)BaseTool)get_from_dict_or_env)	BaseModel
ConfigDictFieldmodel_validatorc                   @   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dede	fddZdedefddZdedee fddZdeee	f dee f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_resultsZforbidT)extraZarbitrary_types_allowedbefore)modevaluesreturnc                 C   sH   t |dd}||d< zddl}|||d< W |S  ty#   tdw )z6Validate that api key is in your environment variable.r   ZGPLACES_API_KEYr   Nr   zCould not import googlemaps python package. Please, install places dependency group: `pip install langchain-google-community[places]`)r   
googlemapsZClientImportError)clsr   r   r    r   \/var/www/html/lang_env/lib/python3.10/site-packages/langchain_google_community/places_api.pyvalidate_environment.   s   z+GooglePlacesAPIWrapper.validate_environmentqueryc                 C   s   | j |d }t|}g }|dkrdS | jdu r|nt|| j}t|D ]}|| }| |d }|dur;|| q%d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>Y   s   " 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   runB   s    


zGooglePlacesAPIWrapper.runr   c              
   C   s\   z| j |}||d< | |}|W S  ty- } ztd|  W Y d }~d S d }~ww )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)   [   s   
z*GooglePlacesAPIWrapper.fetch_place_detailsr5   c           	   
   C   s   zF| 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ya } ztd|  W Y d }~d S d }~ww )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: )getr2   r3   r4   )	r-   r5   r8   addressZphone_numberr:   r   r6   r7   r   r   r   r1   e   s0   z+GooglePlacesAPIWrapper.format_place_details)__name__
__module____qualname____doc__r   r   str__annotations__r   r   r   intr
   Zmodel_configr   classmethodr   r   r0   r)   r1   r   r   r   r   r      s   
 "
r   c                   @   s&   e Zd ZU dZedddZeed< dS )GooglePlacesSchemazInput for GooglePlacesTool..zQuery for google maps)descriptionr   N)r=   r>   r?   r@   r   r   rA   rB   r   r   r   r   rE   |   s   
 rE   c                   @   sj   e Zd ZU dZdZeed< dZeed< ee	dZ
e	ed< eZee ed< 		dd
edee defddZd	S )GooglePlacesToolz(Tool that queries the Google places API.Zgoogle_placesr8   zA wrapper around Google Places. Useful for when you need to validate or discover addressed from ambiguous text. Input should be a search query.rF   )default_factoryapi_wrapperargs_schemaNr   run_managerr   c                 C   s   | j |S )zUse the tool.)rI   r0   )r-   r   rK   r   r   r   _run   s   zGooglePlacesTool._run)N)r=   r>   r?   r@   r8   rA   rB   rF   r   r   rI   rE   rJ   r   r	   r   r   rL   r   r   r   r   rG      s   
 
rG   )r@   r3   typingr   r   r   r   Zlangchain_core.callbacksr   Zlangchain_core.toolsr   Zlangchain_core.utilsr   Zpydanticr	   r
   r   r   r   rE   rG   r   r   r   r   <module>   s    k