
    dhb                     x    S r SSKJrJrJrJr  SSKJr  SSKJ	r	  SSK
JrJrJr  \" SSSS	9 " S
 S\5      5       rg)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            
       
   \ rS rSr% SrSr\\S'   Sr\	\
   \S'   Sr\	\
   \S'   Sr\\S'   Sr\\S	'   \" S
S9rS\
S\S\\   4S jr\" SS9\S\S\4S j5       5       rS\
S\
4S jr SS\
S\S\	\\
\
4      S\\   4S jjrSrg)GoogleSearchAPIWrapper
   u  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                     U R                   R                  5       nU R                  (       a  UR                  5       nUR                  " SXR                  S.UD6R                  5       nUR                  S/ 5      $ )N)qcxitems )r   cser   listr   executeget)selfr   r   r!   ress        c/var/www/html/shao/venv/lib/python3.13/site-packages/langchain_community/utilities/google_search.py_google_search_results-GoogleSearchAPIWrapper._google_search_results@   sc      $$&""$ChhF););FvFNNPwww##    before)modevaluesc                     [        USS5      nX!S'   [        USS5      nX1S'    SSKJn  U" SS	US
9nXQS'   U$ ! [         a    [        S5      ef = f)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.discoveryr1   ImportError)clsr-   r   r   r1   services         r'   validate_environment+GoogleSearchAPIWrapper.validate_environmentG   s     .$&6
 $2 ,V_oV"/	7 >J")  	 	s	   8 Aqueryc                     / nU R                  XR                  S9n[        U5      S:X  a  gU H  nSU;   d  M  UR                  US   5        M!     SR	                  U5      $ )z0Run query through GoogleSearch and parse result.)numr   &No good Google Search Result was foundsnippet )r(   r   lenappendjoin)r%   r;   snippetsresultsresults        r'   runGoogleSearchAPIWrapper.runb   sa    --e-@w<1;FF"y 12  xx!!r*   num_resultssearch_paramsc                     / nU R                   " U4SU0U=(       d    0 D6n[        U5      S:X  a  SS0/$ U H-  nUS   US   S.nSU;   a  US   US'   UR                  U5        M/     U$ )	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)rM   rN   r?   )r(   rA   rB   )r%   r;   rI   rJ   metadata_resultsrE   rF   metadata_results           r'   rE   GoogleSearchAPIWrapper.resultsn   s    & --
"
'4':
 w<1GHIIFvO F"-3I->	*##O4   r*   r    )N)__name__
__module____qualname____firstlineno____doc__r   r   __annotations__r   r   strr   r   intr   boolr
   model_configr   dictr(   r   classmethodr   r9   rG   rE   __static_attributes__r    r*   r'   r   r   
   s    $L M3$(NHSM(#'M8C='AsKL$L$# $ $d $ (#$ 3   $2
" 
" 
"  37	" "  "   S#X/	" 
 
d"  " r*   r   N)rV   typingr   r   r   r   langchain_core._api.deprecationr   langchain_core.utilsr   pydanticr	   r
   r   r   r    r*   r'   <module>rc      sF    $ , , 6 5 ; ; 
J
A Y A 
A r*   