
    3fi                         d 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  G d de      Z G d	 d
e	      Z G d de	      Zy)zUtil that calls Google Search.    )AnyDictListOptional)CallbackManagerForToolRun)BaseTool)get_from_dict_or_env)	BaseModel
ConfigDictmodel_validatorc            
          e Zd ZU dZdZeed<   dZee	   ed<   	 dZ
ee	   ed<   	 dZeed<   	 dZeed	<   	  ed
      Zde	dedee   fdZ ed      ededefd              Zde	de	fdZ	 dde	dedeee	e	f      dee   fdZy)GoogleSearchAPIWrapperaT  Wrapper for Google Custom Search API.

    Performs web searches using Google Custom Search API and returns results
    with snippets, titles, and links.

    !!! note "Setup Required"

        1. Enable [Custom Search API](https://console.cloud.google.com/apis/library/customsearch.googleapis.com)
        2. Create API key in [Google Cloud Console](https://console.cloud.google.com/apis/credentials)
        3. Create custom search engine at [Programmable Search Engine](https://programmablesearchengine.google.com)
        4. Set `GOOGLE_API_KEY` and `GOOGLE_CSE_ID` environment variables
    Nsearch_enginegoogle_api_keygoogle_cse_id
   kFsiterestrictforbid)extrasearch_termkwargsreturnc                     | j                   j                         }| j                  r|j                         } |j                  d|| j                  d|j                         }|j                  dg       S )N)qcxitems )r   cser   listr   executeget)selfr   r   r   ress        _/var/www/auto_recruiter/arenv/lib/python3.12/site-packages/langchain_google_community/search.py_google_search_resultsz-GoogleSearchAPIWrapper._google_search_results+   sg      $$&""$CchhF););FvFNNPwww##    before)modevaluesc                     t        |dd      }||d<   t        |dd      }||d<   	 ddlm}  |dd	|
      }||d<   |S # t        $ r t        d      w xY w)z?Validate that api key and python package exists in environment.r   GOOGLE_API_KEYr   GOOGLE_CSE_IDr   )buildzjgoogle-api-python-client is not installed. Please install it with `pip install langchain-google-community`customsearchv1)developerKeyr   )r	   googleapiclient.discoveryr.   ImportError)clsr*   r   r   r.   services         r%   validate_environmentz+GoogleSearchAPIWrapper.validate_environment2   s     .$&6
 $2 ,V_oV"/	7 >J")  	R 	s	   > Aqueryc                     g }| j                  || j                        }t        |      dk(  ry|D ]  }d|v s|j                  |d           dj	                  |      S )z1Run query through Google Search and parse result.)numr   &No good Google Search Result was foundsnippet )r&   r   lenappendjoin)r#   r7   snippetsresultsresults        r%   runzGoogleSearchAPIWrapper.runL   sh    --e-@w<1; 	3FF"y 12	3 xx!!r'   num_resultssearch_paramsc                     g } | j                   |fd|i|xs i }t        |      dk(  rddigS |D ]*  }|d   |d   d}d|v r|d   |d<   |j                  |       , |S )	aC  Run query through Google Search and return metadata.

        Args:
            query: The query to search for.
            num_results: Number of results to return.
            search_params: Additional search parameters.

        Returns:
            Search results with snippet, title, and link for each result.
        r9   r   Resultr:   titlelink)rH   rI   r;   )r&   r=   r>   )r#   r7   rD   rE   metadata_resultsrA   rB   metadata_results           r%   rA   zGoogleSearchAPIWrapper.resultsX   s      -$--
"
'4':
 w<1GHII 	5FvO F"-3I->	*##O4	5  r'   N)__name__
__module____qualname____doc__r   r   __annotations__r   r   strr   r   intr   boolr   model_configr   dictr&   r   classmethodr   r6   rC   rA   r   r'   r%   r   r      s     M3$(NHSM(,#'M8C='"AsK&L$7L$# $ $d $ (#$ 3   $0
" 
" 
"  37	      S#X/	 
 
d r'   r   c                   V    e Zd ZU dZdZeed<   dZeed<   eed<   	 dded	e	e
   d
efdZy)GoogleSearchRunzTool that queries the Google Custom Search API.

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

    Returns concatenated snippets from search results.
    google_searchnamezA wrapper around Google Search. Useful for when you need to answer questions about current events. Input should be a search query.descriptionapi_wrapperNr7   run_managerr   c                 8    | j                   j                  |      S )zExecute the Google search.

        Args:
            query: Search query string.
            run_manager: Optional callback manager.

        Returns:
            Concatenated snippets from search results.
        )r]   rC   r#   r7   r^   s      r%   _runzGoogleSearchRun._run   s     ##E**r'   rL   )rM   rN   rO   rP   r[   rR   rQ   r\   r   r   r   ra   r   r'   r%   rY   rY   z   sY      D#	*  
 ('
 <@++ 78+ 
	+r'   rY   c                   d    e Zd ZU dZdZeed<   dZeed<   dZe	ed<   e
ed<   	 dd
edee   defdZy	)GoogleSearchResultszTool that queries the Google Custom Search API and returns JSON.

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

    Returns structured search results with metadata.
    google_search_results_jsonr[   zA wrapper around Google Search. Useful for when you need to answer questions about current events. Input should be a search query. Output is a JSON array of the query resultsr\      rD   r]   Nr7   r^   r   c                 `    t        | j                  j                  || j                              S )a  Execute the Google search and return structured results.

        Args:
            query: Search query string.
            run_manager: Optional callback manager.

        Returns:
            JSON string of search results with title, link, and snippet.
        )rR   r]   rA   rD   r`   s      r%   ra   zGoogleSearchResults._run   s(     4##++E43C3CDEEr'   rL   )rM   rN   rO   rP   r[   rR   rQ   r\   rD   rS   r   r   r   ra   r   r'   r%   rc   rc      sh     -D#,	V  
 K''
 <@FF 78F 
	Fr'   rc   N)rP   typingr   r   r   r   langchain_core.callbacksr   langchain_core.toolsr   langchain_core.utilsr	   pydanticr
   r   r   r   rY   rc   r   r'   r%   <module>rl      sG    $ , , > ) 5 ; ;l Y l ^+h +BF( Fr'   