U
    h8                     @   sf   d Z ddlmZmZmZmZ ddlmZ ddlm	Z	m
Z
 ddlmZ edddd	G d
d de	ZdS )zUtil that calls Google Search.    )AnyDictListOptional)
deprecated)	BaseModelroot_validator)get_from_dict_or_envz0.0.33z1.0z1langchain_google_community.GoogleSearchAPIWrapper)ZsinceZremovalZalternative_importc                   @   s   e Zd ZU 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	< G d
d dZeeee dddZeddeedddZeedddZdeeeeeef  ee dddZdS )GoogleSearchAPIWrapperuY  Wrapper for Google Search API.

    Adapted from: Instructions adapted from https://stackoverflow.com/questions/
    37083058/
    programmatically-searching-google-in-python-using-custom-search

    TODO: DOCS for using it
    1. Install google-api-python-client
    - If you don't already have a Google account, sign up.
    - If you have never created a Google APIs Console project,
    read the Managing Projects page and create a project in the Google API Console.
    - Install the library using pip install google-api-python-client

    2. Enable the Custom Search API
    - Navigate to the APIs & Services→Dashboard panel in Cloud Console.
    - Click Enable APIs and Services.
    - Search for Custom Search API and click on it.
    - Click Enable.
    URL for it: https://console.cloud.google.com/apis/library/customsearch.googleapis
    .com

    3. To create an API key:
    - Navigate to the APIs & Services → Credentials panel in Cloud Console.
    - Select Create credentials, then select API key from the drop-down menu.
    - The API key created dialog box displays your newly created key.
    - You now have an API_KEY

    Alternatively, you can just generate an API key here:
    https://developers.google.com/custom-search/docs/paid_element#api_key

    4. Setup Custom Search Engine so you can search the entire web
    - Create a custom search engine here: https://programmablesearchengine.google.com/.
    - In `What to search` to search, pick the `Search the entire Web` option.
    After search engine is created, you can click on it and find `Search engine ID`
      on the Overview page.

    search_engineNgoogle_api_keygoogle_cse_id
   kFsiterestrictc                   @   s   e Zd ZdZdS )zGoogleSearchAPIWrapper.ConfigZforbidN)__name__
__module____qualname__extra r   r   O/tmp/pip-unpacked-wheel-9gdii04g/langchain_community/utilities/google_search.pyConfig<   s   r   )search_termkwargsreturnc                 K   s@   | j  }| jr| }|jf || jd| }|dg S )N)qZcxitems)r   cser   listr   executeget)selfr   r   r   resr   r   r   _google_search_results?   s
    
z-GoogleSearchAPIWrapper._google_search_resultsT)pre)valuesr   c                 C   sp   t |dd}||d< t |dd}||d< zddlm} W n tk
rT   tdY nX |dd	|d
}||d< |S )z?Validate that api key and python package exists in environment.r   ZGOOGLE_API_KEYr   ZGOOGLE_CSE_IDr   )buildzqgoogle-api-python-client is not installed. Please install it with `pip install google-api-python-client>=2.100.0`ZcustomsearchZv1)ZdeveloperKeyr   )r	   Zgoogleapiclient.discoveryr&   ImportError)clsr%   r   r   r&   Zservicer   r   r   validate_environmentF   s"      
z+GoogleSearchAPIWrapper.validate_environment)queryr   c                 C   sN   g }| j || jd}t|dkr$dS |D ]}d|kr(||d  q(d|S )z0Run query through GoogleSearch and parse result.)numr   &No good Google Search Result was foundsnippet )r#   r   lenappendjoin)r!   r*   Zsnippetsresultsresultr   r   r   run`   s    zGoogleSearchAPIWrapper.run)r*   num_resultssearch_paramsr   c                 C   sr   g }| j |fd|i|pi }t|dkr4ddigS |D ]4}|d |d d}d|krb|d |d< || q8|S )	a  Run query through GoogleSearch and return metadata.

        Args:
            query: The query to search for.
            num_results: The number of results to return.
            search_params: Parameters to be passed on search

        Returns:
            A list of dictionaries with the following keys:
                snippet - The description of the result.
                title - The title of the result.
                link - The link to the result.
        r+   r   ZResultr,   titlelink)r7   r8   r-   )r#   r/   r0   )r!   r*   r5   r6   Zmetadata_resultsr2   r3   Zmetadata_resultr   r   r   r2   l   s$    
zGoogleSearchAPIWrapper.results)N)r   r   r   __doc__r   __annotations__r   r   strr   r   intr   boolr   r   dictr#   r   r   r)   r4   r2   r   r   r   r   r
   
   s$   
& r
   N)r9   typingr   r   r   r   Zlangchain_core._api.deprecationr   Zlangchain_core.pydantic_v1r   r   Zlangchain_core.utilsr	   r
   r   r   r   r   <module>   s   