
    Fd              	          d Z ddlmZ ddlZddlZddlmZ ddlmZ ddl	m
Z
 ddlmZ dZ ed	d
dddddi          d!d"d            Z edddddddid d          d!d#d            Zd$d ZdS )%z"Google search command for Autogpt.    )annotationsN)islice)DDGS)Agent)command   
web_searchzSearches the webquerystringzThe search queryT)typedescriptionrequired   stragentr   num_resultsintreturnc                b   g }d}|t           k     r{| st          j        |          S t                                          |           }t          t          ||                    }|rn$t          j        d           |dz  }|t           k     {t          j        |dd          }t          |          S )zReturn the results of a Google search

    Args:
        query (str): The search query.
        num_results (int): The number of results to return.

    Returns:
        str: The results of the search.
    r      F   )ensure_asciiindent)
DUCKDUCKGO_MAX_ATTEMPTSjsondumpsr   textlistr   timesleepsafe_google_results)r
   r   r   search_resultsattemptsresultss         OC:\Users\Administrator\Downloads\Auto-GPT-master\autogpt\commands\web_search.pyr	   r	      s    * NH
,
,
, 	.:n---&&++e$$fWk::;; 	
1A ,
,
, jeAFFFGw'''    googlezGoogle Searchc                R    t          | j                  ot          | j                  S )N)boolgoogle_api_keygoogle_custom_search_engine_id)configs    r%   <lambda>r-   C   s'    4-.. 4V233 r&   z5Configure google_api_key and custom_search_engine_id.str | list[str]c                   ddl m} ddlm} 	 |j        j        }|j        j        } |dd|          }|                                                    | ||          	                                }|
                    dg           }	d	 |	D             }
n# |$ r}t          j        |j                                                  }|
                    d
i           
                    d          dk    r2d|
                    d
i           
                    dd          v rY d}~dS d| cY d}~S d}~ww xY wt          |
          S )zReturn the results of a Google search using the official Google API

    Args:
        query (str): The search query.
        num_results (int): The number of results to return.

    Returns:
        str: The results of the search.
    r   )build)	HttpErrorcustomsearchv1)developerKey)qcxnumitemsc                    g | ]
}|d          S )link ).0items     r%   
<listcomp>zgoogle.<locals>.<listcomp>h   s    HHHVHHHr&   errorcodei  zinvalid API keymessage Nz9Error: The provided Google API key is invalid or missing.zError: )googleapiclient.discoveryr0   googleapiclient.errorsr1   r,   r*   r+   cser   executegetr   loadscontentdecoder!   )r
   r   r   r0   r1   api_keycustom_search_engine_idserviceresultr"   search_results_linkseerror_detailss                r%   r'   r'   9   s   2 0/////000000!!,-"',"M %7CCC KKMMTE5;TGGWYY 	  GR00  IHHHH ! ! !
19#3#3#5#566 Wb))--
 
 &-*;*;GR*H*H*L*Lr+
 +
 
 
 ONNNNN Q==      !  3444s%   BB D1BD,"D,&D1,D1r$   
str | listc                    t          | t                    rt          j        d | D                       }n)|                     dd                              d          }|S )z
        Return the results of a google search in a safe format.

    Args:
        results (str | list): The search results.

    Returns:
        str: The results of the search.
    c                `    g | ]+}|                     d d                              d           ,S )utf-8ignore)encoderJ   )r<   rN   s     r%   r>   z'safe_google_results.<locals>.<listcomp>   s4    TTT&V]]7H--44W==TTTr&   rU   rV   )
isinstancer   r   r   rW   rJ   )r$   safe_messages     r%   r!   r!   }   sd     '4   IzTTGTTT
 
 ~~gx88??HHr&   )r   )r
   r   r   r   r   r   r   r   )r
   r   r   r   r   r   r   r.   )r$   rR   r   r   )__doc__
__future__r   r   r   	itertoolsr   duckduckgo_searchr   autogpt.agent.agentr   autogpt.command_decoratorr   r   r	   r'   r!   r;   r&   r%   <module>r`      sS   ( ( " " " " " "         " " " " " " % % % % % % - - - - - -  	-
 

 
( ( ( (
 
(< 	-
 
4 4; 35 35 35 35 35l     r&   