
    Ng                     z    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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                      e Zd ZU dZeed<   dZeed<    e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Z ed          ededef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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Zdee         dee         f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
           
          d| j         i}
|||||||||	d	}t          j        t           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_resultss                 i/var/www/html/ai-engine/env/lib/python3.11/site-packages/langchain_community/utilities/metaphor_search.py_metaphor_search_resultsz1MetaphorSearchAPIWrapper._metaphor_search_results   s      56%--.*"6 2+

 

 =(((	
 
 
 	!!###!i((    before)modevaluesc                 2    t          |dd          }||d<   |S )z9Validate that api key and endpoint exists in environment.r   METAPHOR_API_KEYr   )clsr9   r   s      r4   validate_environmentz-MetaphorSearchAPIWrapper.validate_environment>   s/     0&(:
 
 &6!"r6   c
                 h    |                      |||||||||		  	        }
|                     |
          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   )r5   _clean_results)r0   r   r   r   r   r   r   r   r   r   raw_search_resultss              r4   r+   z MetaphorSearchAPIWrapper.resultsI   sR    B "::#++-)!51) ; 

 

 ""#5666r6   c
                     	K   dt           f 	f
d}
 |
             d{V }t          j        |          }                     |d                   S )z8Get results from the Metaphor Search API asynchronously.r   c            
        
K   dj         i} 	
d	}t          j                    4 d {V }|                    t           d||           4 d {V 	 }|j        dk    r@|                                 d {V }|cd d d           d {V  cd d d           d {V  S t          d|j         d|j                   # 1 d {V swxY w Y   	 d d d           d {V  d S # 1 d {V swxY w Y   d S )Nr   r   r(   )r*   r)      zError z: )	r   aiohttpClientSessionr-   r.   statustext	Exceptionreason)r)   r1   sessionresdatar   r   r   r   r   r   r0   r   r   r   s        r4   fetchz5MetaphorSearchAPIWrapper.results_async.<locals>.fetch   s     "D$9:G)"1"1"2 .&:$6!/
 
F ,.. M M M M M M M'"<<'000vw (   M M M M M M M MzS((%(XXZZ//////#M M M M M M M M M M M M MM M M M M M M M M M M M M M ((K(K(Ksz(K(KLLLM M M M M M M M M M M M M M MM M M M M M M M M M M M M M M M M M M M M M M M M M M M M Ms;   'C*'CC*&C
C	C*C	C**
C47C4Nr+   )strr*   loadsr?   )r0   r   r   r   r   r   r   r   r   r   rM   results_json_strresults_jsons   ``````````   r4   results_asyncz&MetaphorSearchAPIWrapper.results_asyncw   s      	MS 	M 	M 	M 	M 	M 	M 	M 	M 	M 	M 	M 	M 	M 	M 	M. "'======z"233""<	#:;;;r6   r@   c           
          g }|D ]l}|                     |                    dd          |                    dd          |                    dd          |                    dd          d	           m|S )
NtitlezUnknown TitleurlzUnknown URLauthorzUnknown AuthorpublishedDatezUnknown Date)rT   rU   rV   published_date)appendget)r0   r@   cleaned_resultsresults       r4   r?   z'MetaphorSearchAPIWrapper._clean_results   s    ( 	 	F""#ZZAA!::e];;$jj3CDD&,jj.&Q&Q	     r6   )NNNNNNN)__name__
__module____qualname____doc__rN   __annotations__r   intr
   model_configr   r   booldictr5   r   classmethodr   r   r=   r+   rR   r?    r6   r4   r   r      s        **AsKKK:  L 04/3*.(,.2,0)-!) !)!) !) "$s),	!)
 "$s),!) #3-!) !!) 'sm!) %SM!) !!) 
d!) !) !) !)F _(###$ 3    [ $# 04/3*.(,.2,0)-,7 ,7,7 ,7 "$s),	,7
 "$s),,7 #3-,7 !,7 'sm,7 %SM,7 !,7 
d,7 ,7 ,7 ,7d 04/3*.(,.2,0)-(< (<(< (< "$s),	(<
 "$s),(< #3-(< !(< 'sm(< %SM(< !(< 
d(< (< (< (<Td T
      r6   r   )r`   r*   typingr   r   r   r   rD   r,   langchain_core.utilsr   pydanticr	   r
   r   r.   r   rg   r6   r4   <module>rk      s    
  , , , , , , , , , , , ,   5 5 5 5 5 5 ; ; ; ; ; ; ; ; ; ;1 [ [ [ [ [y [ [ [ [ [r6   