o
    ȳgb                     @   sj   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mZ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)get_from_dict_or_env)	BaseModel
ConfigDictmodel_validatorz0.0.33z1.0z1langchain_google_community.GoogleSearchAPIWrapper)sinceremovalalternative_importc                
   @   s   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
dZde	dedee fddZeddededefddZde	de	fddZ	dde	dedeee	e	f  dee f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.

    Nsearch_enginegoogle_api_keygoogle_cse_id
   kFsiterestrictforbid)extrasearch_termkwargsreturnc                 K   s@   | j  }| jr| }|jd|| jd| }|dg S )N)qcxitems )r   cser   listr   executeget)selfr   r   r   resr   r   g/var/www/html/chatdoc2/venv/lib/python3.10/site-packages/langchain_community/utilities/google_search.py_google_search_results@   s
   
z-GoogleSearchAPIWrapper._google_search_resultsbefore)modevaluesc                 C   sj   t |dd}||d< t |dd}||d< zddlm} W n ty'   tdw |dd	|d
}||d< |S )z?Validate that api key and python package exists in environment.r   GOOGLE_API_KEYr   GOOGLE_CSE_IDr   )buildzqgoogle-api-python-client is not installed. Please install it with `pip install google-api-python-client>=2.100.0`customsearchv1)developerKeyr   )r   googleapiclient.discoveryr+   ImportError)clsr(   r   r   r+   servicer   r   r$   validate_environmentG   s    z+GoogleSearchAPIWrapper.validate_environmentqueryc                 C   sN   g }| j || jd}t|dkrdS |D ]}d|v 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"   r4   snippetsresultsresultr   r   r$   runb   s   
zGoogleSearchAPIWrapper.runnum_resultssearch_paramsc                 C   sr   g }| j |fd|i|pi }t|dkrddigS |D ]}|d |d d}d|v r1|d |d< || q|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.
        r5   r   Resultr6   titlelink)rC   rD   r7   )r%   r9   r:   )r"   r4   r@   rA   metadata_resultsr=   r>   metadata_resultr   r   r$   r=   n   s$   
zGoogleSearchAPIWrapper.results)N)__name__
__module____qualname____doc__r   r   __annotations__r   r   strr   r   intr   boolr	   model_configr   dictr%   r
   classmethodr   r3   r?   r=   r   r   r   r$   r   
   s2   
 &r   N)rJ   typingr   r   r   r   langchain_core._api.deprecationr   langchain_core.utilsr   pydanticr   r	   r
   r   r   r   r   r$   <module>   s    