
    Ng'                        d Z ddlZddlmZ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 ddlmZmZmZ ddlmZ dZ G d	 d
e          Z G d de          Z G d de          Z G d de          Z G d de          ZdS )zUtil that calls you.com Search API.

In order to set this up, follow instructions at:
https://documentation.you.com/quickstart
    N)AnyDictListLiteralOptionalDocumentget_from_dict_or_env)	BaseModelFieldmodel_validator)Selfzhttps://api.ydc-index.ioc                       e Zd ZU dZ ed          Zeed<    ed          Zeed<    ed          Z	eed<    ed	          Z
eed
<   dS )YouHitMetadataz%Metadata on a single hit from you.comzThe title of the resultdescriptiontitlezThe url of the resulturlz$Thumbnail associated with the resultthumbnail_urlzDetails about the resultr   N)__name__
__module____qualname____doc__r   r   str__annotations__r   r   r        ]/var/www/html/ai-engine/env/lib/python3.11/site-packages/langchain_community/utilities/you.pyr   r      s         //#<===E3===u!8999C999+QRRRM3RRRu)CDDDKDDDDDr   r   c                   B    e Zd ZU dZ ed          Zee         ed<   dS )YouHitz>A single hit from you.com, which may contain multiple snippetszOne or snippets of textr   snippetsN)	r   r   r   r   r   r"   r   r   r   r   r   r   r!   r!      s:         HH%,EFFFHd3iFFFFFr   r!   c                   B    e Zd ZU dZ ed          Zee         ed<   dS )YouAPIOutputzOutput from you.com API.z-A list of dictionaries containing the resultsr   hitsN)	r   r   r   r   r   r%   r   r!   r   r   r   r   r$   r$   #   sJ         ""C  D$v,     r   r$   c                   @    e Zd ZU dZ ed          Zeed<   eed<   dS )YouDocumentzOutput of parsing one snippet.zOne snippet of textr   page_contentmetadataN)	r   r   r   r   r   r(   r   r   r   r   r   r   r'   r'   +   sB         ((*?@@@L#@@@r   r'   c                   z   e Zd ZU dZdZee         ed<   dZe	d         ed<   dZ
ee         ed<   dZee	d                  ed	<   dZee         ed
<   dZee         ed<   dZee         ed<   dZee         ed<   dZee         ed<   dZee         ed<   dZee         ed<    ed          ededefd                        Z ed          defd            Z ed          defd            ZdededefdZdedee         fdZ dededefdZ!dededee         fdZ"dededefd Z#dededee         fd!Z$dS )"YouSearchAPIWrapperaf  Wrapper for you.com Search and News API.

    To connect to the You.com api requires an API key which
    you can get at https://api.you.com.
    You can check out the docs at https://documentation.you.com/api-reference/.

    You need to set the environment variable `YDC_API_KEY` for retriever to operate.

    Attributes
    ----------
    ydc_api_key: str, optional
        you.com api key, if YDC_API_KEY is not set in the environment
    endpoint_type: str, optional
        you.com endpoints: search, news, rag;
        `web` and `snippet` alias `search`
        `rag` returns `{'message': 'Forbidden'}`
        @todo `news` endpoint
    num_web_results: int, optional
        The max number of web results to return, must be under 20.
        This is mapped to the `count` query parameter for the News API.
    safesearch: str, optional
        Safesearch settings, one of off, moderate, strict, defaults to moderate
    country: str, optional
        Country code, ex: 'US' for United States, see api docs for list
    search_lang: str, optional
        (News API) Language codes, ex: 'en' for English, see api docs for list
    ui_lang: str, optional
        (News API) User interface language for the response, ex: 'en' for English,
                   see api docs for list
    spellcheck: bool, optional
        (News API) Whether to spell check query or not, defaults to True
    k: int, optional
        max number of Documents to return using `results()`
    n_hits: int, optional, deprecated
        Alias for num_web_results
    n_snippets_per_hit: int, optional
        limit the number of snippets returned per hit
    Nydc_api_keysearch)r-   newsragsnippetendpoint_typenum_web_results)offmoderatestrict
safesearchcountrysearch_langui_lang
spellcheckkn_snippets_per_hitn_hitsbefore)modevaluesreturnc                 2    t          |dd          }||d<   |S )z,Validate that api key exists in environment.r,   YDC_API_KEYr
   )clsr@   r,   s      r   validate_environmentz(YouSearchAPIWrapper.validate_environmentn   s$     +6=-PP +}r   afterc                     | j         dk    r=d}|D ]8}t          | |          r&t          j        d| d| j          dt                     9| j         dvr!| j        rt          j        dt                     | S )Nr.   )r8   r9   r:   zNews API-specific field 'z' is set but `endpoint_type="z"`. This will have no effect.r-   r0   zGField 'n_snippets_per_hit' only has effect on `endpoint_type="search"`.)r1   getattrwarningswarnUserWarningr<   )selfnews_api_fieldsfields      r   !warn_if_set_fields_have_no_effectz5YouSearchAPIWrapper.warn_if_set_fields_have_no_effectw   s    ''FO( 	 	4'' M8 8 8/3/A8 8 8 $   %:::& 4     r   c                 b    | j         dk    r#t          j        d| j          dt                     | S )Nr"   z`endpoint_type="z7"` is deprecated. Use `endpoint_type="search"` instead.)r1   rJ   rK   DeprecationWarning)rM   s    r   %warn_if_deprecated_endpoints_are_usedz9YouSearchAPIWrapper.warn_if_deprecated_endpoints_are_used   sI    ++M<t'9 < < < #   r   querykwargsc                 "   | j         | j        d|}| j        dv r|                    || j                   n9| j        dk    r.|                    || j        | j        | j        | j                   d |                                D             }|S )z
        Parse parameters required for different You.com APIs.

        Args:
            query: The query to search for.
        )r6   r7   rH   )rT   r2   r.   )qcountr8   r9   r:   c                     i | ]
\  }}|||S Nr   ).0r;   vs      r   
<dictcomp>z8YouSearchAPIWrapper._generate_params.<locals>.<dictcomp>   s    CCC41aQ]!Q]]]r   )	r6   r7   r1   updater2   r8   r9   r:   items)rM   rT   rU   paramss       r   _generate_paramsz$YouSearchAPIWrapper._generate_params   s     /|
 
 
 !666MM $ 4      6))MM* ,?     DC6<<>>CCCr   raw_search_resultsc                 \   | j         dk    r0|d         d         }| j        |d| j                 }d |D             S g }|d         D ]}| j        p!t          |                    d                    }|                    d          d|         D ]}|                    t          ||                    d          |                    d          |                    d	          |                    d
          d                     | j        t          |          | j        k    r|c c S |S )z
        Extracts snippets from each hit and puts them in a Document
        Parameters:
            raw_search_results: A dict containing list of hits
        Returns:
            List[YouDocument]: A dictionary of parsed results
        r.   resultsNc                 <    g | ]}t          |d          |          S )r   r(   r)   r   )r[   results     r   
<listcomp>z6YouSearchAPIWrapper._parse_results.<locals>.<listcomp>   s:        f]&;fMMM  r   r%   r"   r   r   r   r   )r   r   r   r   rf   )r1   r;   r<   lengetappendr	   )rM   rb   news_resultsdocshitr<   r0   s          r   _parse_resultsz"YouSearchAPIWrapper._parse_results   sc    ''-f5i@Lv!+HdfH5 *   
 %f- 	  	 C!%!8!TC
@S@S<T<T77:../B0B/BC    %,#&775>>-0WW_-E-E%(WWW%5%5+.77=+A+A	" "  
 
 
 6%#d))tv*=*=KKKKK  r   c                     d| j         pdi} | j        |fi |}| j        dk    rd| _        t          j        t
           d| j         ||          }|                                 |                                S )zRun query through you.com Search and return hits.

        Args:
            query: The query to search for.
        Returns: YouAPIOutput
        	X-API-Key r0   r-   /)r`   headers)r,   ra   r1   requestsrj   YOU_API_URLraise_for_statusjson)rM   rT   rU   rt   r`   responses         r   raw_resultszYouSearchAPIWrapper.raw_results   s      0 6B7&&u7777 **!)D<11T/11	
 
 
 	!!###}}r   c                      | j         |fi d |                                D             }|                     |          S )zCRun query through you.com Search and parses results into Documents.c                     i | ]
\  }}|||S rZ   r   r[   keyvalues      r   r]   z/YouSearchAPIWrapper.results.<locals>.<dictcomp>  #    PPPjc5e>OsE>O>O>Or   )rz   r_   ro   )rM   rT   rU   rb   s       r   rd   zYouSearchAPIWrapper.results   sV     .T-
 
PPFLLNNPPP
 
 ""#5666r   c                 2  K   d| j         pdi} | j        |fi |}| j        dk    rd| _        t          j                    4 d{V }|                    t           d| j         ||          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 )z7Get results from the you.com Search API asynchronously.rq   rr   r0   r-   Nrs   )r   r`   rt      zError z: )r,   ra   r1   aiohttpClientSessionrj   rv   statusrx   	Exceptionreason)rM   rT   rU   rt   r`   sessionresrd   s           r   raw_results_asyncz%YouSearchAPIWrapper.raw_results_async  s       0 6B7&&u7777 **!)D(** 
	I 
	I 
	I 
	I 
	I 
	I 
	Ig{{"99T%799 #   	I 	I 	I 	I 	I 	I 	I 	I :$$$'HHJJ......G"	I 	I 	I 	I 	I 	I 	I 	I 	I 	I 	I 	I 	I
	I 
	I 
	I 
	I 
	I 
	I 
	I 
	I 
	I 
	I 
	I 
	I 
	I 
	I $$GSZ$G$G3:$G$GHHH	I 	I 	I 	I 	I 	I 	I 	I 	I 	I 	I 	I 	I 	I 	I
	I 
	I 
	I 
	I 
	I 
	I 
	I 
	I 
	I 
	I 
	I 
	I 
	I 
	I 
	I 
	I 
	I 
	I 
	I 
	I 
	I 
	I 
	I 
	I 
	I 
	I 
	I 
	I 
	I 
	Is<   .D6'C!DC!!
C+	+D.C+	/D
DDc                    K    | j         |fi d |                                D              d {V }|                     |          S )Nc                     i | ]
\  }}|||S rZ   r   r}   s      r   r]   z5YouSearchAPIWrapper.results_async.<locals>.<dictcomp>)  r   r   )r   r_   ro   )rM   rT   rU   raw_search_results_asyncs       r   results_asyncz!YouSearchAPIWrapper.results_async"  sy      
 *@)?*
 *
PPFLLNNPPP*
 *
 $
 $
 $
 $
 $
 $
  ""#;<<<r   )%r   r   r   r   r,   r   r   r   r1   r   r2   intr6   r7   r8   r9   r:   boolr;   r<   r=   r   classmethodr   r   rE   r   rP   rS   ra   r   r	   ro   rz   rd   r   r   r   r   r   r+   r+   2   s        % %N "&K#%%% BJM7=>III &*OXc])))AEJ!<=>EEE!GXc]!!! "&K#%%%!GXc]!!!!%J%%%Ax}(,,,, FHSM   _(###$ 3    [ $# _'"""4    #"0 _'"""	t 	 	 	 #"	c S T    >$ $$x. $ $ $ $L  
	   477 7 
h	7 7 7 7II I 
	I I I I4	=	= 	= 
h		= 	= 	= 	= 	= 	=r   r+   )r   rJ   typingr   r   r   r   r   r   ru   langchain_core.documentsr	   langchain_core.utilsr   pydanticr   r   r   typing_extensionsr   rv   r   r!   r$   r'   r+   r   r   r   <module>r      s     5 5 5 5 5 5 5 5 5 5 5 5 5 5   - - - - - - 5 5 5 5 5 5 6 6 6 6 6 6 6 6 6 6 " " " " " "(E E E E EY E E EG G G G G^ G G G    9       )   y= y= y= y= y=) y= y= y= y= y=r   