U
    hm                     @   sT   d Z ddlmZmZ ddlZddlmZmZmZ ddl	m
Z
 dZG dd deZdS )	zUtil that calls Bing Search.    )DictListN)	BaseModelFieldroot_validator)get_from_dict_or_envz*https://api.bing.microsoft.com/v7.0/searchc                   @   s   e Zd ZU dZeed< eed< dZeed< ee	dZ
e	ed< G dd	 d	Zeeee	 d
ddZeddeedddZeedddZeeee dddZdS )BingSearchAPIWrapperz Wrapper for Bing Web Search API.bing_subscription_keybing_search_url
   k)default_factorysearch_kwargsc                   @   s   e Zd ZdZdS )zBingSearchAPIWrapper.ConfigZforbidN)__name__
__module____qualname__extra r   r   M/tmp/pip-unpacked-wheel-9gdii04g/langchain_community/utilities/bing_search.pyConfig%   s   r   )search_termcountreturnc                 C   sX   d| j i}||ddd| j}tj| j||d}|  | }d|krT|d d S g S )NzOcp-Apim-Subscription-KeyTZHTML)qr   ZtextDecorationsZ
textFormat)headersparamsZwebPagesvalue)r	   r   requestsgetr
   raise_for_statusjson)selfr   r   r   r   responseZsearch_resultsr   r   r   _bing_search_results(   s$    
z)BingSearchAPIWrapper._bing_search_resultsT)pre)valuesr   c                 C   s0   t |dd}||d< t |ddtd}||d< |S )z9Validate that api key and endpoint exists in environment.r	   ZBING_SUBSCRIPTION_KEYr
   ZBING_SEARCH_URL)default)r   DEFAULT_BING_SEARCH_ENDPOINT)clsr%   r	   r
   r   r   r   validate_environment<   s      z)BingSearchAPIWrapper.validate_environment)queryr   c                 C   sF   g }| j || jd}t|dkr$dS |D ]}||d  q(d|S )z.Run query through BingSearch and parse result.r   r   $No good Bing Search Result was foundsnippet )r#   r   lenappendjoin)r!   r*   Zsnippetsresultsresultr   r   r   runO   s    zBingSearchAPIWrapper.run)r*   num_resultsr   c                 C   sX   g }| j ||d}t|dkr(ddigS |D ]&}|d |d |d d}|| q,|S )	a  Run query through BingSearch and return metadata.

        Args:
            query: The query to search for.
            num_results: The number of results to return.

        Returns:
            A list of dictionaries with the following keys:
                snippet - The description of the result.
                title - The title of the result.
                link - The link to the result.
        r+   r   ZResultr,   r-   nameurl)r-   titlelink)r#   r/   r0   )r!   r*   r5   Zmetadata_resultsr2   r3   Zmetadata_resultr   r   r   r2   Z   s    
zBingSearchAPIWrapper.resultsN)r   r   r   __doc__str__annotations__r   intr   dictr   r   r   r#   r   r   r)   r4   r2   r   r   r   r   r      s   
r   )r:   typingr   r   r   Zlangchain_core.pydantic_v1r   r   r   Zlangchain_core.utilsr   r'   r   r   r   r   r   <module>   s   