a
    bg                     @   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                   @   sD  e Zd ZU dZeed< dZeed< eddZ	deee
ee  e
ee  e
e e
e e
e e
e e
e ee d
d	d
ZeddeeedddZdeee
ee  e
ee  e
e e
e e
e e
e e
e ee d
ddZdeee
ee  e
ee  e
e e
e e
e e
e e
e ee d
ddZee ee dddZdS )MetaphorSearchAPIWrapperz Wrapper for Metaphor Search API.metaphor_api_key
   kZ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	Z
numResultsr   ZincludeDomainsZexcludeDomainsZstartCrawlDateZendCrawlDateZstartPublishedDateZendPublishedDateZ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responseZsearch_results r'   {/var/www/html/cobodadashboardai.evdpl.com/venv/lib/python3.9/site-packages/langchain_community/utilities/metaphor_search.py_metaphor_search_results   s&    
z1MetaphorSearchAPIWrapper._metaphor_search_resultsbefore)mode)valuesr   c                 C   s   t |dd}||d< |S )z9Validate that api key and endpoint exists in environment.r   ZMETAPHOR_API_KEYr   )clsr,   r   r'   r'   r(   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   )r)   _clean_results)r$   r   r   r   r   r   r   r   r   r   raw_search_resultsr'   r'   r(   r   I   s    !z MetaphorSearchAPIWrapper.resultsc
                    sH   t d 	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 }|jt d|| d4 I d H r}|jdkr| I d H }|W  d   I d H  W  d   I d H  S td|j d|j W d   I d H  q1 I d H s0    Y  W d   I d H  q1 I d H s0    Y  d S )Nr   r   r   )r   r      zError z: )	r   aiohttpZClientSessionr!   r"   statustext	Exceptionreason)r   r%   sessionresdata
r   r   r   r   r   r   r$   r   r   r   r'   r(   fetch   s(    

,z5MetaphorSearchAPIWrapper.results_async.<locals>.fetchNr   )strr   loadsr/   )r$   r   r   r   r   r   r   r   r   r   r;   Zresults_json_strZresults_jsonr'   r:   r(   results_asyncw   s    $
z&MetaphorSearchAPIWrapper.results_async)r0   r   c              
   C   sF   g }|D ]8}| |dd|dd|dd|ddd	 q|S )
NtitlezUnknown TitleurlzUnknown URLauthorzUnknown AuthorZpublishedDatezUnknown Date)r?   r@   rA   Zpublished_date)appendget)r$   r0   Zcleaned_resultsresultr'   r'   r(   r/      s    



z'MetaphorSearchAPIWrapper._clean_results)NNNNNNN)NNNNNNN)NNNNNNN)__name__
__module____qualname____doc__r<   __annotations__r   intr	   Zmodel_configr   r   booldictr)   r
   classmethodr   r   r.   r   r>   r/   r'   r'   r'   r(   r      s   
       

#       

2       

*r   )rH   r   typingr   r   r   r   r2   r    Zlangchain_core.utilsr   Zpydanticr   r	   r
   r"   r   r'   r'   r'   r(   <module>   s   