
    -hm                     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   DDGSzSCould not import ddgs python package. Please install it with `pip install -U ddgs`.)ddgsr    ImportError)clsr   r    s      _/root/34ku/venv/lib/python3.13/site-packages/langchain_community/utilities/duckduckgo_search.pyvalidate_environment/DuckDuckGoSearchAPIWrapper.validate_environment,   s5    	!   	@ 	s   
  Nqueryc           
      D   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    r!   ddgs_genrs          r$   
_ddgs_text%DuckDuckGoSearchAPIWrapper._ddgs_text9   s     	Vtyy{{??))';4+;+; ! H #+,8a8, V   	 - V 	s   AB)B4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B )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A:A5A:5A::
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   r1   r5   *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.   r2   r6   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OUS:X  a5  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OEU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)snippetrG   linkr1   urldater   )rI   rG   rJ   rL   r   r5   	thumbnailimageheightwidth)rG   rM   rN   rK   rO   rP   r   Resultr9   )r   r.   r2   r6   )r+   r'   r   r   r-   rB   s         r$   rB   "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.   r2   r6   rC   rB   __static_attributes__r=   rE   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rE   r   N)rW   typingr   r   r   r   pydanticr   r   r	   r   r=   rE   r$   <module>r`      s&    - , ; ;f frE   