a
    bg^                     @   sX   d dl 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 G dd de	ZdS )    )AnyDictOptionalNget_from_dict_or_env)	BaseModel
ConfigDictmodel_validatorc                   @   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< eddZed	d
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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
aiosessionT)Zarbitrary_types_allowedbefore)mode)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   u/var/www/html/cobodadashboardai.evdpl.com/venv/lib/python3.9/site-packages/langchain_community/utilities/searchapi.pyvalidate_environment"   s
    z(SearchApiAPIWrapper.validate_environment)querykwargsr   c                 K   s   | j |fi |}| |S N)results_result_as_stringselfr   r   r   r   r   r   run,   s    zSearchApiAPIWrapper.runc                    s"   | j |fi |I d H }| |S r   )aresultsr   r   r   r   r   arun0   s    zSearchApiAPIWrapper.arunc                 K   s   | j |fi |}|S r   )_search_api_resultsr   r   r   r   r   4   s    zSearchApiAPIWrapper.resultsc                    s   | j |fi |I d H }|S r   )_async_search_api_resultsr   r   r   r   r   8   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 ur||qS r   r   ).0keyvaluer   r   r   
<dictcomp>E       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i |}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    I   s    z'SearchApiAPIWrapper._search_api_resultsc              
      s   | j |fi |}| jst 4 I dH v}|j|d |d |d dd4 I dH $}| I dH }W d  I dH  q1 I dH s0    Y  W d  I dH  q1 I dH s0    Y  nf| jj|d |d |d dd4 I dH &}| I dH }W d  I dH  q1 I dH s0    Y  |S )zFUse aiohttp to send request to SearchApi API and return results async.Nr)   r*   r+   T)r)   r*   r+   r0   )r-   r   aiohttpClientSessionr/   r1   )r   r   r   r2   sessionr3   r   r   r   r   r!   S   s(    d<z-SearchApiAPIWrapper._async_search_api_results)resultr   c                 C   s  d}d|   v r.d| d   v r.| d d }nd|   v rXd| d   v rX| d d }nd|   v rr| d d }nd|   v rdd	 | d D }d
|}n~d|   v rdd	 | d D }d
|}nTd|   v rdd	 | d D }d
|}n*d|   v 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 |  v r|d  qS )r8   keysr$   rr   r   r   
<listcomp>s   s   z9SearchApiAPIWrapper._result_as_string.<locals>.<listcomp>
jobsc                 S   s    g | ]}d |  v r|d  qS )r9   r:   r<   r   r   r   r>   x   s   videosc                 S   s0   g | ](}d |  v rd|d   d|d  qS )titleTitle: "" Link: linkr:   r<   r   r   r   r>   }   s   imagesc                 S   s4   g | ],}d |  v rd|d  d|d  d  qS )originalrC   rB   rD   rE   r:   r<   r   r   r   r>      s   )r;   join)r7   ZtoretZsnippetsr@   rA   rF   r   r   r   r   i   s8    
z%SearchApiAPIWrapper._result_as_string)__name__
__module____qualname____doc__r   str__annotations__r   r   r   r4   r5   r   Zmodel_configr	   classmethodr   r   r   r   r   dictr   r   r-   r    r!   staticmethodr   r   r   r   r   r
   	   s&   

r
   )typingr   r   r   r4   r.   Zlangchain_core.utilsr   Zpydanticr   r   r	   r
   r   r   r   r   <module>   s
   