U
    h                     @   sh   d Z ddlZddlmZmZmZ ddlZddlZddlm	Z	m
Z
mZ ddlmZ dZG dd de	ZdS )	zUtil that calls Tavily Search API.

In order to set this up, follow instructions at:
https://docs.tavily.com/docs/tavily-api/introduction
    N)DictListOptional)	BaseModel	SecretStrroot_validatorget_from_dict_or_envzhttps://api.tavily.comc                   @   s  e Zd ZU dZeed< G dd dZeddeeddd	Z	d
dg g dddfe
ee ee
 eee
  eee
  ee ee ee ed	ddZd
dg g dddfe
ee ee
 eee
  eee
  ee ee ee ee d	ddZd
dg g dddfe
ee ee
 eee
  eee
  ee ee ee ed	ddZd
dg g dddfe
ee ee
 eee
  eee
  ee ee ee ee d	ddZee ee dddZdS )TavilySearchAPIWrapperzWrapper for Tavily Search API.tavily_api_keyc                   @   s   e Zd ZdZdS )zTavilySearchAPIWrapper.ConfigZforbidN)__name__
__module____qualname__extra r   r   O/tmp/pip-unpacked-wheel-9gdii04g/langchain_community/utilities/tavily_search.pyConfig   s   r   T)pre)valuesreturnc                 C   s   t |dd}||d< |S )z9Validate that api key and endpoint exists in environment.r   ZTAVILY_API_KEYr   )clsr   r   r   r   r   validate_environment   s      z+TavilySearchAPIWrapper.validate_environment   ZadvancedF)	querymax_resultssearch_depthinclude_domainsexclude_domainsinclude_answerinclude_raw_contentinclude_imagesr   c	              
   C   sB   | j  ||||||||d	}	tjt d|	d}
|
  |
 S )N	Z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responser   r   r   raw_results$   s     z"TavilySearchAPIWrapper.raw_resultsc	           
   
   C   s(   | j ||||||||d}	| |	d S )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)r-   clean_results)
r*   r   r   r   r   r   r   r   r    Zraw_search_resultsr   r   r   r.   B   s    #
zTavilySearchAPIWrapper.resultsc	              
      s8   t d f	dd}	|	 I dH }
t|
S )z6Get results from the Tavily Search API asynchronously.)r   c                     s   j   d	} t 4 I d H }|jt d| d4 I d H ^}|jdkr| I d H }|W  5 Q I d H R  W  5 Q I d H R  S td|j d|j	 W 5 Q I d H R X W 5 Q I d H R X d S )Nr!   r"   r#      zError z: )
r   r%   aiohttpZClientSessionr'   r(   statustext	Exceptionreason)r+   sessionresdata	r   r   r   r    r   r   r   r   r*   r   r   fetch   s     
(z7TavilySearchAPIWrapper.raw_results_async.<locals>.fetchN)strr$   loads)r*   r   r   r   r   r   r   r   r    r:   Zresults_json_strr   r9   r   raw_results_asyncq   s    "z(TavilySearchAPIWrapper.raw_results_asyncc	           
   
      s.   | j ||||||||dI d H }	| |	d S )N)r   r   r   r   r   r   r   r    r.   )r=   r/   )
r*   r   r   r   r   r   r   r   r    Zresults_jsonr   r   r   results_async   s    
z$TavilySearchAPIWrapper.results_async)r.   r   c                 C   s*   g }|D ]}| |d |d d q|S )z%Clean results from Tavily Search API.urlcontent)r?   r@   )append)r*   r.   r/   resultr   r   r   r/      s    z$TavilySearchAPIWrapper.clean_resultsN)r   r   r   __doc__r   __annotations__r   r   r   r   r;   r   intr   boolr-   r.   r=   r>   r/   r   r   r   r   r
      s   


!

2

(

r
   )rC   r$   typingr   r   r   r1   r&   Zlangchain_core.pydantic_v1r   r   r   Zlangchain_core.utilsr	   r(   r
   r   r   r   r   <module>   s   