
    dhe                     r    S r SSKrSSKJrJrJrJr  SSKrSSKrSSK	J
r
  SSKJrJrJr  Sr " S S\5      rg)	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                   
   \ rS rSr% Sr\\S'   Sr\\S'   \	" SS9r
       SS	\S
\S\\\      S\\\      S\\   S\\   S\\   S\\   S\\   S\\   4S jjr\" SS9\S\S\4S j5       5       r       SS	\S
\S\\\      S\\\      S\\   S\\   S\\   S\\   S\\   S\\   4S jjr       SS	\S
\S\\\      S\\\      S\\   S\\   S\\   S\\   S\\   S\\   4S jjrS\\   S\\   4S jrSrg)MetaphorSearchAPIWrapper   z 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
           
          SU R                   0n
UUUUUUUUU	S.	n[        R                  " [         S3U
US9nUR	                  5         UR                  5       nU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                 e/var/www/html/shao/venv/lib/python3.13/site-packages/langchain_community/utilities/metaphor_search.py_metaphor_search_results1MetaphorSearchAPIWrapper._metaphor_search_results   s{      5 56%--.*"6 2+

 == (
 	!!#!i((    before)modevaluesc                 (    [        USS5      nX!S'   U$ )z9Validate that api key and endpoint exists in environment.r   METAPHOR_API_KEYr   )clsr;   r   s      r5   validate_environment-MetaphorSearchAPIWrapper.validate_environment=   s(     0&(:
 &6!"r8   c
                 R    U R                  UUUUUUUUU	S9	n
U R                  U
5      $ )a  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   )r6   _clean_results)r1   r   r   r   r   r   r   r   r   r   raw_search_resultss              r5   r,    MetaphorSearchAPIWrapper.resultsH   sJ    B "::#++-)!51) ; 

 ""#566r8   c
                    ^ ^^^^^^^^^	#    S[         4UUUUUUU UUU	4
S jjn
U
" 5       I Sh  vN n[        R                  " U5      nT R                  US   5      $  N.7f)z8Get results from the Metaphor Search API asynchronously.r   c            
      >  >
#    STR                   0n T	T
TTTTTTTS.	n[        R                  " 5        IS h  vN nUR                  [         S3XS9 IS h  vN nUR
                  S:X  a<  UR                  5       I S h  vN nUsS S S 5      IS h  vN   sS S S 5      IS h  vN   $ [        SUR
                   SUR                   35      e N Nw NQ NA N2! , IS h  vN  (       d  f       O= fS S S 5      IS h  vN    g ! , IS h  vN  (       d  f       g = f7f)Nr   r    r)   )r+   r*      zError z: )	r   aiohttpClientSessionr.   r/   statustext	Exceptionreason)r*   r2   sessionresdatar   r   r   r   r   r   r1   r   r   r   s        r5   fetch5MetaphorSearchAPIWrapper.results_async.<locals>.fetch   s    "D$9$9:G)"1"1"2 .&:$6!/
F ,,..'"<<'(0v (  zzS(%(XXZ/#   /.. (&Bszzl(KLL /  0	 /   /.....s   4DCDDCD$C?C CDCDD"C#D(%CDDCDDC.	C C.	*D1D<C?=DD	D
DDNr,   )strr+   loadsrB   )r1   r   r   r   r   r   r   r   r   r   rQ   results_json_strresults_jsons   ``````````   r5   results_async&MetaphorSearchAPIWrapper.results_asyncv   sW     	MS 	M 	M 	M. "'=zz"23""<	#:;; )s   "AA/ArC   c           
          / nU HY  nUR                  UR                  SS5      UR                  SS5      UR                  SS5      UR                  SS5      S	.5        M[     U$ )
NtitlezUnknown TitleurlzUnknown URLauthorzUnknown AuthorpublishedDatezUnknown Date)rZ   r[   r\   published_date)appendget)r1   rC   cleaned_resultsresults       r5   rB   'MetaphorSearchAPIWrapper._clean_results   se    (F""#ZZA!::e];$jj3CD&,jj.&Q	 ) r8    )NNNNNNN)__name__
__module____qualname____firstlineno____doc__rS   __annotations__r   intr
   model_configr   r   booldictr6   r   classmethodr   r   r?   r,   rW   rB   __static_attributes__rd   r8   r5   r   r      sf   *AsKL 04/3*.(,.2,0)- ) )  ) "$s),	 )
 "$s), ) #3- ) ! ) 'sm ) %SM ) ! ) 
d )D (#$ 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
 r8   r   )ri   r+   typingr   r   r   r   rH   r-   langchain_core.utilsr   pydanticr	   r
   r   r/   r   rd   r8   r5   <module>rt      s8   
  , ,   5 ; ;1 Zy Zr8   