
    7|hb                     ~    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	       G d
 de             Zy)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            
          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)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                     | 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        j/var/www/html/test/engine/venv/lib/python3.12/site-packages/langchain_community/utilities/google_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   )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.discoveryr0   ImportError)clsr,   r   r   r0   services         r'   validate_environmentz+GoogleSearchAPIWrapper.validate_environmentG   s     .$&6
 $2 ,V_oV"/	7 >J")  	 	s	   > Aqueryc                     g }| j                  || j                        }t        |      dk(  ry|D ]  }d|v s|j                  |d           dj	                  |      S )z0Run query through GoogleSearch and parse result.)numr   &No good Google Search Result was foundsnippet )r(   r   lenappendjoin)r%   r9   snippetsresultsresults        r'   runzGoogleSearchAPIWrapper.runb   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 )	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   Resultr<   titlelink)rJ   rK   r=   )r(   r?   r@   )r%   r9   rF   rG   metadata_resultsrC   rD   metadata_results           r'   rC   zGoogleSearchAPIWrapper.resultsn   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   r8   rE   rC   r    r)   r'   r   r   
   s    $L M3$(NHSM(#'M8C='AsKL$L$# $ $d $ (#$ 3   $2
" 
" 
"  37	" "  "   S#X/	" 
 
d" r)   r   N)rQ   typingr   r   r   r   langchain_core._api.deprecationr   langchain_core.utilsr   pydanticr	   r
   r   r   r    r)   r'   <module>r]      sF    $ , , 6 5 ; ; 
J
A Y A 
A r)   