
    3fis                         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	e      Z G d
 de      Z G d de
      Zy)z#Chain that calls Google Places API.    N)AnyDictOptionalType)CallbackManagerForToolRun)BaseTool)get_from_dict_or_env)	BaseModel
ConfigDictFieldmodel_validatorc                       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      Z ed	
      edede	fd              ZdedefdZdedee   fdZdeee	f   dee   fdZy)GooglePlacesAPIWrappera  Wrapper around Google Places API.

    Searches for places using Google Maps Platform. Returns detailed information
    including addresses, phone numbers, and websites.

    !!! note "Installation"

        Requires additional dependencies:

        ```bash
        pip install langchain-google-community[places]
        ```

    !!! note "Setup Required"

        Set `GPLACES_API_KEY` environment variable or pass `gplaces_api_key`
        parameter with your Google Maps Platform API key.
    Ngplaces_api_keygoogle_map_clienttop_k_resultsforbidT)extraarbitrary_types_allowedbefore)modevaluesreturnc                     t        |dd      }||d<   	 ddl}|j                  |      |d<   |S # t        $ r t        d      w xY w)z6Validate that API key is in your environment variable.r   GPLACES_API_KEYr   Nr   zCould not import googlemaps python package. Please, install places dependency group: `pip install langchain-google-community[places]`)r	   
googlemapsClientImportError)clsr   r   r   s       c/var/www/auto_recruiter/arenv/lib/python3.12/site-packages/langchain_google_community/places_api.pyvalidate_environmentz+GooglePlacesAPIWrapper.validate_environment2   sm     /%'8
 %4 !		*4*;*;O*LF&'   	C 	s	   . Aqueryc           	         | j                   j                  |      d   }t        |      }g }|dk(  ry| j                  |nt	        || j                        }t        |      D ]/  }||   }| j                  |d         }||j                  |       1 dj                  t        |      D cg c]  \  }}|dz    d|  c}}      S c c}}w )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 descriptionplace_id
   z. )
r   placeslenr   minrangefetch_place_detailsappendjoin	enumerate)	selfr"   search_resultsnum_to_returnr(   iresultdetailsitems	            r    runzGooglePlacesAPIWrapper.runF   s    //66u=iHN+AU !!) ]D$6$67 	 }% 	'A#A&F..vj/ABG"g&	' yyIf<MNDQUG2dV,NOONs   ,C
r%   c                     	 | j                   j                  |      }||d<   | j                  |      }|S # t        $ r"}t	        j
                  d|        Y d }~y d }~ww xY w)Nr%   z0An Error occurred while fetching place details: )r   placeformat_place_details	Exceptionloggingerror)r0   r%   place_detailsformatted_detailses        r    r,   z*GooglePlacesAPIWrapper.fetch_place_details_   se    	 2288BM(0M*% $ 9 9- H$$ 	MMLQCPQ	s   25 	A AA r>   c           
         	 |j                  di       j                  dd      }|j                  di       j                  dd      }|j                  di       j                  dd      }|j                  di       j                  dd      }|j                  di       j                  dd      }| d| d	| d
| d| d
}|S # t        $ r"}t        j                  d|        Y d }~y d }~ww xY w)Nr4   nameUnknownformatted_addressformatted_phone_numberwebsiter%   z

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

Website: z

z2An error occurred while formatting place details: )getr;   r<   r=   )	r0   r>   rB   addressphone_numberrF   r%   r?   r@   s	            r    r:   z+GooglePlacesAPIWrapper.format_place_detailsi   s   	 $$Xr266vyID#''"599#YG ),,Xr:>>()L $''"599)YOG$((26:::yQH &G9 -$$,: .&{7)4A 
 %$ 	MMNqcRS	s   B<B? ?	C*C%%C*)__name__
__module____qualname____doc__r   r   str__annotations__r   r   r   intr   model_configr   classmethodr   r!   r7   r,   r:        r    r   r      s    & &*OXc])'!s!#'M8C='. $L
 (#$ 3   $$P P P2C HSM $sCx. Xc] rT   r   c                   ,    e Zd ZU dZ ed      Zeed<   y)GooglePlacesSchemaz$Input schema for `GooglePlacesTool`..r"   N)rJ   rK   rL   rM   r   r"   rN   rO   rS   rT   r    rV   rV      s    .sE3&rT   rV   c                   |    e Zd ZU dZdZeed<   dZeed<    ee	      Z
e	ed<   eZee   ed<   	 dd
edee   defdZy	)GooglePlacesToolzTool that queries the Google Places API.

    Inherits from [`BaseTool`][langchain_core.tools.BaseTool].

    Validates and discovers addresses from ambiguous text using Google Maps Platform.
    google_placesrB   zA wrapper around Google Places. Useful for when you need to validate or discover addresses from ambiguous text. Input should be a search query.description)default_factoryapi_wrapperargs_schemaNr"   run_managerr   c                 8    | j                   j                  |      S )zSearch for places matching the query.

        Args:
            query: Search query for Google Maps.
            run_manager: Optional callback manager.

        Returns:
            Formatted string with place details for each result.
        )r\   r7   )r0   r"   r^   s      r    _runzGooglePlacesTool._run   s     ##E**rT   )N)rJ   rK   rL   rM   rB   rN   rO   rZ   r   r   r\   rV   r]   r   r
   r   r   r`   rS   rT   r    rX   rX      sr      D#	*   +0@V*WK'W#5Ki5
 <@++ 78+ 
	+rT   rX   )rM   r<   typingr   r   r   r   langchain_core.callbacksr   langchain_core.toolsr   langchain_core.utilsr	   pydanticr
   r   r   r   r   rV   rX   rS   rT   r    <module>rf      sK    )  , , > ) 5 lY l^' '#+x #+rT   