U
    h;                     @   sT   d dl mZmZmZ d dlZd dlZd dlmZmZ d dl	m
Z
 G dd deZdS )    )AnyDictOptionalN)	BaseModelroot_validatorget_from_dict_or_envc                   @   s   e Zd ZU dZdZeed< dZee ed< dZ	ee
j ed< G dd dZed	d
eedddZeeedddZeeedddZeeedddZeeedddZeeedddZeeedddZeeedddZeeedddZdS ) SearchApiAPIWrappera}  
    Wrapper around SearchApi API.

    To use, you should have the environment variable ``SEARCHAPI_API_KEY``
    set with your API key, or pass `searchapi_api_key`
    as a named parameter to the constructor.

    Example:
        .. code-block:: python

            from langchain_community.utilities import SearchApiAPIWrapper
            searchapi = SearchApiAPIWrapper()
    ZgoogleengineNsearchapi_api_key
aiosessionc                   @   s   e Zd ZdZdS )zSearchApiAPIWrapper.ConfigTN)__name__
__module____qualname__Zarbitrary_types_allowed r   r   K/tmp/pip-unpacked-wheel-9gdii04g/langchain_community/utilities/searchapi.pyConfig   s   r   T)pre)valuesreturnc                 C   s   t |dd}||d< |S )z,Validate that API key exists in environment.r   ZSEARCHAPI_API_KEYr   )clsr   r   r   r   r   validate_environment!   s      z(SearchApiAPIWrapper.validate_environment)querykwargsr   c                 K   s   | j |f|}| |S N)results_result_as_stringselfr   r   r   r   r   r   run*   s    zSearchApiAPIWrapper.runc                    s   | j |f|I d H }| |S r   )aresultsr   r   r   r   r   arun.   s    zSearchApiAPIWrapper.arunc                 K   s   | j |f|}|S r   )_search_api_resultsr   r   r   r   r   2   s    zSearchApiAPIWrapper.resultsc                    s   | j |f|I d H }|S r   )_async_search_api_resultsr   r   r   r   r    6   s    zSearchApiAPIWrapper.aresultsc                 K   s2   ddd| j  i| j|ddd | D dS )Nz&https://www.searchapi.io/api/v1/searchAuthorizationzBearer )r
   qc                 S   s   i | ]\}}|d k	r||qS r   r   ).0keyvaluer   r   r   
<dictcomp>C   s       z8SearchApiAPIWrapper._prepare_request.<locals>.<dictcomp>)urlheadersparams)r   r
   items)r   r   r   r   r   r   _prepare_request:   s     
z$SearchApiAPIWrapper._prepare_requestc                 K   s:   | j |f|}tj|d |d |d d}|  | S )Nr*   r,   r+   )r*   r,   r+   )r.   requestsgetraise_for_statusjson)r   r   r   request_detailsresponser   r   r   r"   G   s    z'SearchApiAPIWrapper._search_api_resultsc                    s   | j |f|}| js~t 4 I dH L}|j|d |d |d dd4 I dH }| I dH }W 5 Q I dH R X W 5 Q I dH R X nH| jj|d |d |d dd4 I dH }| I dH }W 5 Q I dH R X |S )zFUse aiohttp to send request to SearchApi API and return results async.Nr*   r+   r,   T)r*   r+   r,   r1   )r.   r   aiohttpClientSessionr0   r2   )r   r   r   r3   sessionr4   r   r   r   r   r#   Q   s(    0z-SearchApiAPIWrapper._async_search_api_results)resultr   c                 C   s  d}d|   kr.d| d   kr.| d d }nd|   krXd| d   krX| d d }nd|   krr| d d }nd|   krdd	 | d D }d
|}n~d|   krdd	 | d D }d
|}nTd|   krdd	 | d D }d
|}n*d|   krdd	 | d D }d
|}|S )NzNo good search result foundZ
answer_boxZanswersnippetZknowledge_graphdescriptionZorganic_resultsc                 S   s    g | ]}d |  kr|d  qS )r9   keysr&   rr   r   r   
<listcomp>q   s     z9SearchApiAPIWrapper._result_as_string.<locals>.<listcomp>
jobsc                 S   s    g | ]}d |  kr|d  qS )r:   r;   r=   r   r   r   r?   v   s     videosc                 S   s0   g | ](}d |  krd|d   d|d  qS )titleTitle: "" Link: linkr;   r=   r   r   r   r?   {   s   imagesc                 S   s4   g | ],}d |  krd|d  d|d  d  qS )originalrD   rC   rE   rF   r;   r=   r   r   r   r?      s   )r<   join)r8   ZtoretZsnippetsrA   rB   rG   r   r   r   r   g   s8    
z%SearchApiAPIWrapper._result_as_string)r   r   r   __doc__r
   str__annotations__r   r   r   r5   r6   r   r   r   r   r   r   r!   dictr   r    r.   r"   r#   staticmethodr   r   r   r   r   r	   	   s    

r	   )typingr   r   r   r5   r/   Zlangchain_core.pydantic_v1r   r   Zlangchain_core.utilsr   r	   r   r   r   r   <module>   s
   