o
    ȳg                     @   sp   d Z ddlZddlmZmZmZmZ ddlZddlZddl	m
Z
 ddlmZmZmZ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)AnyDictListOptionalget_from_dict_or_env)	BaseModel
ConfigDict	SecretStrmodel_validatorzhttps://api.tavily.comc                   @   s  e Zd ZU dZeed< eddZedde	de
defd	d
Zddg g dddfdedee dee deee  deee  dee dee dee de
fddZddg g dddfdedee dee deee  deee  dee dee dee dee
 fddZddg g dddfdedee dee deee  deee  dee dee dee de
fddZddg g dddfdedee dee deee  deee  dee dee dee dee
 fddZdee
 dee
 fdd Zd!S )"TavilySearchAPIWrapperzWrapper for Tavily Search API.tavily_api_keyforbid)extrabefore)modevaluesreturnc                 C   s   t |dd}||d< |S )z9Validate that api key and endpoint exists in environment.r   TAVILY_API_KEYr   )clsr   r    r   g/var/www/html/chatdoc2/venv/lib/python3.10/site-packages/langchain_community/utilities/tavily_search.pyvalidate_environment   s
   z+TavilySearchAPIWrapper.validate_environment   advancedFquerymax_resultssearch_depthinclude_domainsexclude_domainsinclude_answerinclude_raw_contentinclude_imagesc	              
   C   sB   | j  ||||||||d	}	tjt d|	d}
|
  |
 S )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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)r0   clean_results)
r-   r   r   r   r   r   r    r!   r"   raw_search_resultsr   r   r   r1   D   s   #
zTavilySearchAPIWrapper.resultsc	              
      s:   dt f 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 T}|jt d| d4 I d H /}|jdkrJ| I d H }|W  d   I d H  W  d   I d H  S td|j d|j	 1 I d H s\w   Y  W d   I d H  d S 1 I d H srw   Y  d S )Nr#   r%   r&      zError z: )
r   r(   aiohttp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?   results_json_strr   r>   r   raw_results_asyncs   s   "
z(TavilySearchAPIWrapper.raw_results_asyncc	           
   
      s0   | j ||||||||dI d H }	| |	d S )N)r   r   r   r   r   r    r!   r"   r1   )rC   r2   )
r-   r   r   r   r   r   r    r!   r"   results_jsonr   r   r   results_async   s   
z$TavilySearchAPIWrapper.results_asyncr1   c                 C   s*   g }|D ]}| |d |d d q|S )z%Clean results from Tavily Search API.urlcontent)rF   rG   )append)r-   r1   r2   resultr   r   r   r2      s   z$TavilySearchAPIWrapper.clean_resultsN)__name__
__module____qualname____doc__r
   __annotations__r	   model_configr   classmethodr   r   r   r@   r   intr   boolr0   r1   rC   rE   r2   r   r   r   r   r      s   
 

	

!

	

2

	

(

	

r   )rM   r'   typingr   r   r   r   r5   r)   langchain_core.utilsr   pydanticr   r	   r
   r   r+   r   r   r   r   r   <module>   s    