
    {h                     r    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y)	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y)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|
|      }|j	                          |j                         }|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                 g/var/www/html/engine/venv/lib/python3.12/site-packages/langchain_community/utilities/metaphor_search.py_metaphor_search_resultsz1MetaphorSearchAPIWrapper._metaphor_search_results   s{      5 56%--.*"6 2+

 == (	
 	!!#!i((    before)modevaluesc                 *    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
                 X    | j                  |||||||||		      }
| j                  |
      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   sJ    B "::#++-)!51) ; 

 ""#566r6   c
                     	K   dt         f 	f
d}
 |
        d{   }t        j                  |      } j                  |d         S 7 -w)z8Get results from the Metaphor Search API asynchronously.r   c            
      .  
K   dj                   i} 	
d	}t        j                         4 d {   }|j                  t         d||       4 d {   }|j
                  dk(  r<|j                          d {   }|cd d d       d {    cd d d       d {    S t        d|j
                   d|j                         7 7 v7 Q7 A7 2# 1 d {  7  sw Y   nxY wd d d       d {  7   y # 1 d {  7  sw Y   y xY ww)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$9:G)"1"1"2 .&:$6!/
F ,,. M M'"<<'(0vw (  M MzzS(%(XXZ/#M M MM M M (&Bszzl(KLLMM  0	MMM M MM M M M Ms   3DCD D CD #CCCD CD D$C%D*%CDD CD DC+	C" C+	'D .D9C<:D DD	DD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   sR     	MS 	M 	M. "'=zz"23""<	#:;; )s    AA.Ar@   c           
          g }|D ]X  }|j                  |j                  dd      |j                  dd      |j                  dd      |j                  dd      d	       Z |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   sj    ( 	F""#ZZA!::e];$jj3CD&,jj.&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      sf   *AsKL 04/3*.(,.2,0)-!)!) !) "$s),	!)
 "$s),!) #3-!) !!) 'sm!) %SM!) !!) 
d!)F (#$ 3   $ 04/3*.(,.2,0)-,7,7 ,7 "$s),	,7
 "$s),,7 #3-,7 !,7 'sm,7 %SM,7 !,7 
d,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      s8   
  , ,   5 ; ;1 [y [r6   