
    dh                     v    S r SSKrSSKJrJrJrJr  SSKrSSKrSSK	J
r
  SSKJrJrJrJr  Sr " S S\5      rg)	zUtil that calls Tavily Search API.

In order to set this up, follow instructions at:
https://docs.tavily.com/docs/tavily-api/introduction
    N)AnyDictListOptionalget_from_dict_or_env)	BaseModel
ConfigDict	SecretStrmodel_validatorzhttps://api.tavily.comc                   `   \ rS rSr% Sr\\S'   \" SS9r\	" SS9\
S\S	\4S
 j5       5       rSS/ / SSS4S\S\\   S\\   S\\\      S\\\      S\\   S\\   S\\   S	\4S jjrSS/ / SSS4S\S\\   S\\   S\\\      S\\\      S\\   S\\   S\\   S	\\   4S jjrSS/ / SSS4S\S\\   S\\   S\\\      S\\\      S\\   S\\   S\\   S	\4S jjrSS/ / SSS4S\S\\   S\\   S\\\      S\\\      S\\   S\\   S\\   S	\\   4S jjrS\\   S	\\   4S jrSrg)TavilySearchAPIWrapper   zWrapper for Tavily Search API.tavily_api_keyforbid)extrabefore)modevaluesreturnc                 (    [        USS5      nX!S'   U$ )z9Validate that api key and endpoint exists in environment.r   TAVILY_API_KEYr   )clsr   r   s      c/var/www/html/shao/venv/lib/python3.13/site-packages/langchain_community/utilities/tavily_search.pyvalidate_environment+TavilySearchAPIWrapper.validate_environment   s'     .$&6
 $2        advancedFquerymax_resultssearch_depthinclude_domainsexclude_domainsinclude_answerinclude_raw_contentinclude_imagesc	           
          U R                   R                  5       UUUUUUUUS.	n	[        R                  " [         S3U	S9n
U
R                  5         U
R                  5       $ )N	api_keyr    r!   r"   r#   r$   r%   r&   r'   /searchjson)r   get_secret_valuerequestspostTAVILY_API_URLraise_for_statusr-   )selfr    r!   r"   r#   r$   r%   r&   r'   paramsresponses              r   raw_results"TavilySearchAPIWrapper.raw_results&   sm     **;;=&(..,#6,

 ==g&
 	!!#}}r   c	                 V    U R                  UUUUUUUUS9n	U R                  U	S   5      $ )a&  Run query through Tavily Search and return metadata.

Args:
    query: The query to search for.
    max_results: The maximum number of results to return.
    search_depth: The depth of the search. Can be "basic" or "advanced".
    include_domains: A list of domains to include in the search.
    exclude_domains: A list of domains to exclude from the search.
    include_answer: Whether to include the answer in the results.
    include_raw_content: Whether to include the raw content in the results.
    include_images: Whether to include images in the results.
Returns:
    query: The query that was searched for.
    follow_up_questions: A list of follow up questions.
    response_time: The response time of the query.
    answer: The answer to the query.
    images: A list of images.
    results: A list of dictionaries containing the results:
        title: The title of the result.
        url: The url of the result.
        content: The content of the result.
        score: The score of the result.
        raw_content: The raw content of the result.
)r!   r"   r#   r$   r%   r&   r'   results)r6   clean_results)
r3   r    r!   r"   r#   r$   r%   r&   r'   raw_search_resultss
             r   r9   TavilySearchAPIWrapper.resultsC   sL    F "--#%++) 3) . 	
 !!"4Y"?@@r   c	           
         ^ ^^^^^^^^#    S[         4UUUUUUUUU 4	S jjn	U	" 5       I Sh  vN n
[        R                  " U
5      $  N7f)z6Get results from the Tavily Search API asynchronously.r   c            
      R  >	#    TR                   R                  5       T
T	TTTTTTS.	n [        R                  " 5        IS h  vN nUR	                  [
         S3U S9 IS h  vN nUR                  S:X  a<  UR                  5       I S h  vN nUsS S S 5      IS h  vN   sS S S 5      IS h  vN   $ [        SUR                   SUR                   35      e N Nw NQ NA N2! , IS h  vN  (       d  f       O= fS S S 5      IS h  vN    g ! , IS h  vN  (       d  f       g = f7f)Nr)   r+   r,      zError z: )
r   r.   aiohttpClientSessionr0   r1   statustext	Exceptionreason)r4   sessionresdatar$   r%   r#   r'   r&   r!   r    r"   r3   s       r   fetch7TavilySearchAPIWrapper.raw_results_async.<locals>.fetch   s     ..??A* ,#2#2"0':"0
F ,,..'"<<>*:'(B<PPTWzzS(%(XXZ/# QPP /.. (&Bszzl(KLL /P/ Q /PPP /.....s   >D'CD'D!C"D%$C!	C
C!DCD D',C-D'2%C!D'DC!DD'!C8	'C*(C8	4D;D'D	D'D$DD$ D'N)strr-   loads)r3   r    r!   r"   r#   r$   r%   r&   r'   rI   results_json_strs   `````````  r   raw_results_async(TavilySearchAPIWrapper.raw_results_asyncr   s<     	MS 	M 	M( "'=zz*++ )s   !A	AA	c	                 r   #    U R                  UUUUUUUUS9I S h  vN n	U R                  U	S   5      $  N7f)N)r    r!   r"   r#   r$   r%   r&   r'   r9   )rN   r:   )
r3   r    r!   r"   r#   r$   r%   r&   r'   results_jsons
             r   results_async$TavilySearchAPIWrapper.results_async   sV      "33#%++) 3) 4 	
 	
 !!,y"9::	
s   757r9   c                     / nU HC  nUS   US   US   US   S.nUR                  S5      =n(       a  XTS'   UR                  U5        ME     U$ )z%Clean results from Tavily Search API.titleurlcontentscore)rU   rV   rW   rX   raw_content)getappend)r3   r9   r:   resultclean_resultrY   s         r   r:   $TavilySearchAPIWrapper.clean_results   si    Fe}!),	L %jj77{7.9]+  .  r    N)__name__
__module____qualname____firstlineno____doc__r   __annotations__r
   model_configr   classmethodr   r   r   rK   r   intr   boolr6   r9   rN   rR   r:   __static_attributes__r_   r   r   r   r      s   (L (#$ 3   $ &'&0/1/1)..3). c] sm	
 "$s), "$s), ! &d^ ! 
@ &'&0/1/1)..3).-A-A c]-A sm	-A
 "$s),-A "$s),-A !-A &d^-A !-A 
d-Ad &'&0/1/1)..3).#,#, c]#, sm	#,
 "$s),#, "$s),#, !#, &d^#, !#, 
#,P &'&0/1/1)..3).;; c]; sm	;
 "$s),; "$s),; !; &d^; !; 
d;.T$Z DJ r   r   )rd   r-   typingr   r   r   r   r@   r/   langchain_core.utilsr   pydanticr	   r
   r   r   r1   r   r_   r   r   <module>rn      s7     , ,   5 F F)iY ir   