o
    b+ i^                     @   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dedefddZdededefddZdededefddZdededefddZdededefddZdededefddZdededefddZdededefddZededef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   s/home/app/PaddleOCR-VL-test/.venv_paddleocr/lib/python3.10/site-packages/langchain_community/utilities/searchapi.pyvalidate_environment"   s
   z(SearchApiAPIWrapper.validate_environmentquerykwargsc                 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   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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s^t 4 I dH <}|j|d |d |d dd4 I dH }| I dH }W d  I dH  n1 I dH sAw   Y  W d  I dH  |S 1 I dH sWw   Y  |S | jj|d |d |d dd4 I dH }| I dH }W d  I dH  |S 1 I dH sw   Y  |S )zFUse aiohttp to send request to SearchApi API and return results async.Nr(   r)   r*   T)r(   r)   r*   r/   )r,   r   aiohttpClientSessionr.   r0   )r   r   r   r1   sessionr2   r   r   r   r   r!   S   s:   *z-SearchApiAPIWrapper._async_search_api_resultsresultc                 C   s(  d}d|   v rd| d   v r| d d }|S d|   v r.d| d   v r.| d d }|S d|   v r<| d d }|S d|   v rRdd	 | d D }d
|}|S d|   v rhdd	 | d D }d
|}|S d|   v r~dd	 | d D }d
|}|S d|   v rdd	 | d D }d
|}|S )NzNo good search result foundZ
answer_boxZanswersnippetZknowledge_graphdescriptionZorganic_resultsc                 S       g | ]}d |  v r|d  qS )r7   keysr$   rr   r   r   
<listcomp>s       z9SearchApiAPIWrapper._result_as_string.<locals>.<listcomp>
jobsc                 S   r9   )r8   r:   r<   r   r   r   r>   x   r?   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 )originalrD   rC   rE   rF   r:   r<   r   r   r   r>      s
    )r;   join)r6   ZtoretZsnippetsrA   rB   rG   r   r   r   r   i   sD   



z%SearchApiAPIWrapper._result_as_string)__name__
__module____qualname____doc__r   str__annotations__r   r   r   r3   r4   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   r3   r-   Zlangchain_core.utilsr   Zpydanticr   r   r	   r
   r   r   r   r   <module>   s    