
    dh                     J    S r SSKJrJrJrJr  SSKJrJrJ	r	   " S S\5      r
g)ziUtil that calls DuckDuckGo Search.

No setup required. Free.
https://pypi.org/project/duckduckgo-search/
    )AnyDictListOptional)	BaseModel
ConfigDictmodel_validatorc                      \ rS rSr% SrSr\\   \S'    Sr	\\S'    Sr
\\   \S'    S	r\\S
'   Sr\\S'    Sr\\S'    \" SS9r\" SS9\S\S\4S j5       5       r SS\S
\\   S\\\\4      4S jjr SS\S
\\   S\\\\4      4S jjr SS\S
\\   S\\\\4      4S jjrS\S\4S jr SS\S
\S\\   S\\\\4      4S jjrSrg)DuckDuckGoSearchAPIWrapper   zIWrapper for DuckDuckGo Search API.

Free and does not require any setup.
zwt-wtregionmoderate
safesearchytime   max_resultsautobackendtextsourceforbid)extrabefore)modevaluesreturnc                 F     SSK Jn  U$ ! [         a    [        S5      ef = f)z3Validate that python package exists in environment.r   DDGSzmCould not import duckduckgo-search python package. Please install it with `pip install -U duckduckgo-search`.)duckduckgo_searchr    ImportError)clsr   r    s      g/var/www/html/shao/venv/lib/python3.13/site-packages/langchain_community/utilities/duckduckgo_search.pyvalidate_environment/DuckDuckGoSearchAPIWrapper.validate_environment,   s5    	.   	M 	s   
  Nqueryc           
      B   SSK Jn  U" 5        nUR                  UU R                  U R                  U R
                  U=(       d    U R                  U R                  S9nU(       a  U Vs/ sH  ofPM     snsSSS5        $  SSS5        / $ s  snf ! , (       d  f       / $ = f)z<Run query through DuckDuckGo text search and return results.r   r   )r   r   	timelimitr   r   N)r!   r    r   r   r   r   r   r   selfr'   r   r    ddgsddgs_genrs          r$   
_ddgs_text%DuckDuckGoSearchAPIWrapper._ddgs_text9   s     	+Vtyy{{??))';4+;+; ! H #+,8a8, V   	 - V 	s   AB)
B
3B
B
Bc           	      ,   SSK Jn  U" 5        nUR                  UU R                  U R                  U R
                  U=(       d    U R                  S9nU(       a  U Vs/ sH  ofPM     snsSSS5        $  SSS5        / $ s  snf ! , (       d  f       / $ = f)z<Run query through DuckDuckGo news search and return results.r   r   )r   r   r)   r   N)r!   r    newsr   r   r   r   r*   s          r$   
_ddgs_news%DuckDuckGoSearchAPIWrapper._ddgs_newsL   s     	+Vtyy{{??))';4+;+; ! H #+,8a8, V   	 - V 	s   AB
A?(B?B
Bc                    SSK Jn  U" 5        nUR                  UU R                  U R                  U=(       d    U R
                  S9nU(       a  U Vs/ sH  ofPM     snsSSS5        $  SSS5        / $ s  snf ! , (       d  f       / $ = f)z=Run query through DuckDuckGo image search and return results.r   r   )r   r   r   N)r!   r    imagesr   r   r   r*   s          r$   _ddgs_images'DuckDuckGoSearchAPIWrapper._ddgs_images^   s     	+Vt{{{{??';4+;+;	 # H #+,8a8, V   	 - V 	s   AA9
A4A94A99
Bc                    U R                   S:X  a  U R                  U5      nOFU R                   S:X  a  U R                  U5      nO$U R                   S:X  a  U R                  U5      nO/ nU(       d  gSR	                  S U 5       5      $ )z=Run query through DuckDuckGo and return concatenated results.r   r2   r6   *No good DuckDuckGo Search Result was found c              3   (   #    U H	  oS    v   M     g7f)bodyN ).0r.   s     r$   	<genexpr>1DuckDuckGoSearchAPIWrapper.run.<locals>.<genexpr>|   s     37a&	7s   )r   r/   r3   r7   join)r+   r'   resultss      r$   runDuckDuckGoSearchAPIWrapper.runo   sr    ;;& ooe,G[[F"ooe,G[[H$''.GG?xx37333    c                    U=(       d    U R                   nUS:X  a,  U R                  XS9 Vs/ sH  nUS   US   US   S.PM     nnO~US:X  a4  U R                  XS9 Vs/ sH  nUS   US   US   US	   US
   S.PM     nnODUS:X  a<  U R                  XS9 Vs/ sH"  nUS   US   US   US   US   US   US
   S.PM$     nnO/ nUc  SS0/nU$ s  snf s  snf s  snf )as  Run query through DuckDuckGo and return metadata.

Args:
    query: The query to search for.
    max_results: The number of results to return.
    source: The source to look from.

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   r=   titlehref)snippetrH   linkr2   urldater   )rJ   rH   rK   rM   r   r6   	thumbnailimageheightwidth)rH   rN   rO   rL   rP   rQ   r   Resultr:   )r   r/   r3   r7   )r+   r'   r   r   r.   rC   s         r$   rC   "DuckDuckGoSearchAPIWrapper.results~   sd     &4;;V HHA fI'
AfINH  G v H	 IA  !ywZeHfIk I  	G x **5*J KA wZ!";wZU8kwZk K  G G? "NOPGE
	s   C C(Cr>   )N)__name__
__module____qualname____firstlineno____doc__r   r   str__annotations__r   r   r   intr   r   r   model_configr	   classmethodr   r   r%   r   r/   r3   r7   rD   rC   __static_attributes__r>   rF   r$   r   r      s   
 $FHSM# !J  D(3- KGS FC L (#	$ 	3 	  $	 8<'/}	d38n	( 8<'/}	d38n	& 8<'/}	d38n	"4 4 4  EI44'*44<SM4	d38n	4 4rF   r   N)rX   typingr   r   r   r   pydanticr   r   r	   r   r>   rF   r$   <module>ra      s&    - , ; ;f frF   