a
    bg                     @   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	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_keyZforbid)extrabefore)mode)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   y/var/www/html/cobodadashboardai.evdpl.com/venv/lib/python3.9/site-packages/langchain_community/utilities/tavily_search.py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-   D   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 r}|jdkr| I d H }|W  d   I d H  W  d   I d H  S td|j d|j	 W d   I d H  q1 I d H s0    Y  W d   I d H  q 1 I d H s0    Y  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   r9   Zresults_json_strr   r8   r   raw_results_asyncs   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)__name__
__module____qualname____doc__r
   __annotations__r	   Zmodel_configr   classmethodr   r   r   r:   r   intr   boolr,   r-   r<   r=   r.   r   r   r   r   r      s   


!

2

(

r   )rE   r#   typingr   r   r   r   r0   r%   Zlangchain_core.utilsr   Zpydanticr   r	   r
   r   r'   r   r   r   r   r   <module>   s   