o
    ȳg                     @   sl   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 dZG dd deZdS )	zWUtil that calls Metaphor Search API.

In order to set this up, follow instructions at:
    N)AnyDictListOptionalget_from_dict_or_env)	BaseModel
ConfigDictmodel_validatorzhttps://api.metaphor.systemsc                   @   s  e Zd ZU dZeed< dZeed< eddZ								d ded	ed
e
ee  de
ee  de
e de
e de
e de
e de
e dee fddZeddededefddZ							d ded	ed
e
ee  de
ee  de
e de
e de
e de
e de
e dee fddZ							d ded	ed
e
ee  de
ee  de
e de
e de
e de
e de
e dee fddZdee dee fddZdS )!MetaphorSearchAPIWrapperz Wrapper for Metaphor Search API.metaphor_api_key
   kforbid)extraNquerynum_resultsinclude_domainsexclude_domainsstart_crawl_dateend_crawl_datestart_published_dateend_published_dateuse_autopromptreturnc
              
   C   sP   d| j i}
|||||||||	d	}tjt d|
|d}|  | }|d S )N	X-Api-Key	
numResultsr   includeDomainsexcludeDomainsstartCrawlDateendCrawlDatestartPublishedDateendPublishedDateuseAutoprompt/search)headersjsonresults)r   requestspostMETAPHOR_API_URLraise_for_statusr'   )selfr   r   r   r   r   r   r   r   r   r&   paramsresponsesearch_results r1   i/var/www/html/chatdoc2/venv/lib/python3.10/site-packages/langchain_community/utilities/metaphor_search.py_metaphor_search_results   s&   
z1MetaphorSearchAPIWrapper._metaphor_search_resultsbefore)modevaluesc                 C   s   t |dd}||d< |S )z9Validate that api key and endpoint exists in environment.r   METAPHOR_API_KEYr   )clsr6   r   r1   r1   r2   validate_environment>   s
   z-MetaphorSearchAPIWrapper.validate_environmentc
                 C   s&   | j |||||||||	d	}
| |
S )aL  Run query through Metaphor Search and return metadata.

        Args:
            query: The query to search for.
            num_results: The number of results to return.
            include_domains: A list of domains to include in the search. Only one of include_domains and exclude_domains should be defined.
            exclude_domains: A list of domains to exclude from the search. Only one of include_domains and exclude_domains should be defined.
            start_crawl_date: If specified, only pages we crawled after start_crawl_date will be returned.
            end_crawl_date: If specified, only pages we crawled before end_crawl_date will be returned.
            start_published_date: If specified, only pages published after start_published_date will be returned.
            end_published_date: If specified, only pages published before end_published_date will be returned.
            use_autoprompt: If true, we turn your query into a more Metaphor-friendly query. Adds latency.

        Returns:
            A list of dictionaries with the following keys:
                title - The title of the page
                url - The url
                author - Author of the content, if applicable. Otherwise, None.
                published_date - Estimated date published
                    in YYYY-MM-DD format. Otherwise, None.
        )r   r   r   r   r   r   r   r   )r3   _clean_results)r-   r   r   r   r   r   r   r   r   r   raw_search_resultsr1   r1   r2   r(   I   s   !
z MetaphorSearchAPIWrapper.resultsc
                    sJ   dt f 	f
dd}
|
 I dH }t|}|d S )z8Get results from the Metaphor Search API asynchronously.r   c               
      s   dj i}  	d	}t 4 I d H U}|jt d|| d4 I d H /}|jdkrM| I d H }|W  d   I d H  W  d   I d H  S td|j d|j 1 I d H s_w   Y  W d   I d H  d S 1 I d H suw   Y  d S )Nr   r   r%   )r'   r&      zError z: )	r   aiohttpClientSessionr*   r+   statustext	Exceptionreason)r&   r.   sessionresdata
r   r   r   r   r   r   r-   r   r   r   r1   r2   fetch   s2   

.z5MetaphorSearchAPIWrapper.results_async.<locals>.fetchNr(   )strr'   loadsr:   )r-   r   r   r   r   r   r   r   r   r   rG   results_json_strresults_jsonr1   rF   r2   results_asyncw   s
   $
z&MetaphorSearchAPIWrapper.results_asyncr;   c              
   C   sF   g }|D ]}| |dd|dd|dd|ddd	 q|S )
NtitlezUnknown TitleurlzUnknown URLauthorzUnknown AuthorpublishedDatezUnknown Date)rM   rN   rO   published_date)appendget)r-   r;   cleaned_resultsresultr1   r1   r2   r:      s   



z'MetaphorSearchAPIWrapper._clean_results)NNNNNNN)__name__
__module____qualname____doc__rH   __annotations__r   intr	   model_configr   r   booldictr3   r
   classmethodr   r   r9   r(   rL   r:   r1   r1   r1   r2   r      s   
 

	

#

	

2

	

*r   )rY   r'   typingr   r   r   r   r=   r)   langchain_core.utilsr   pydanticr   r	   r
   r+   r   r1   r1   r1   r2   <module>   s    