
    7|h'                         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y)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
<   y)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/test/engine/venv/lib/python3.12/site-packages/langchain_community/utilities/you.pyr   r      sE    /#<=E3=!89C9+QRM3R)CDKDr   r   c                   4    e Zd ZU dZ ed      Zee   ed<   y)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,EFHd3iFr   r    c                   4    e Zd ZU dZ ed      Zee   ed<   y)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#   #   s    "CD$v, r   r#   c                   8    e Zd ZU dZ ed      Zeed<   eed<   y)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&   +   s    (*?@L#@r   r&   c                      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$y)"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                 *    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 +}r   afterc                    | j                   dk7  rAd}|D ]:  }t        | |      st        j                  d| d| j                    dt               < | j                   dvr&| j
                  rt        j                  dt               | S )Nr-   )r7   r8   r9   zNews API-specific field 'z' is set but `endpoint_type="z"`. This will have no effect.r,   r/   zGField 'n_snippets_per_hit' only has effect on `endpoint_type="search"`.)r0   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'MM7w ?//3/A/A.B C88 $	 %::&&4   r   c                 t    | j                   dk(  r(t        j                  d| j                    dt               | S )Nr!   z`endpoint_type="z7"` is deprecated. Use `endpoint_type="search"` instead.)r0   rI   rJ   DeprecationWarning)rL   s    r   %warn_if_deprecated_endpoints_are_usedz9YouSearchAPIWrapper.warn_if_deprecated_endpoints_are_used   s@    +MM&t'9'9&: ;< < # r   querykwargsc                    | j                   | j                  d|}| j                  dv r|j                  || j                         nM| j                  dk(  r>|j                  || j                  | j
                  | j                  | j                         |j                         D ci c]  \  }}|	|| }}}|S c c}}w )z
        Parse parameters required for different You.com APIs.

        Args:
            query: The query to search for.
        )r5   r6   rG   )rS   r1   r-   )qcountr7   r8   r9   )	r5   r6   r0   updater1   r7   r8   r9   items)rL   rS   rT   paramsr:   vs         r   _generate_paramsz$YouSearchAPIWrapper._generate_params   s     //||
 
 !66MM $ 4 4   6)MM** ,,??   $*<<>C41aQ]!Q$CC Ds   (
B=3B=raw_search_resultsc                 Z   | j                   dk(  rA|d   d   }| j                  |d| j                   }|D cg c]  }t        |d   |       c}S g }|d   D ]  }| j                  xs t	        |j                  d            }|j                  d      d| D ]  }|j                  t        ||j                  d      |j                  d	      |j                  d
      |j                  d      d             | j                  mt	        |      | j                  k\  s|c c S   |S c c}w )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-   resultsNr   )r'   r(   r$   r!   r   r   r   )r   r   r   r   )r0   r:   r   r;   lengetappend)rL   r]   news_resultsresultdocshitr;   r/   s           r   _parse_resultsz"YouSearchAPIWrapper._parse_results   s6    '-f5i@Lvv!+Hdff5 + f]&;fM 
 %f- 	 C!%!8!8!TC
@S<T77:./B0BC  %,#&775>-0WW_-E%(WWW%5+.77=+A	"
 66%#d)tvv*=K 	   -s   D(c                    d| j                   xs di} | j                  |fi |}| j                  dk(  rd| _        t        j                  t
         d| j                   ||      }|j                          |j                         S )zRun query through you.com Search and return hits.

        Args:
            query: The query to search for.
        Returns: YouAPIOutput
        	X-API-Key r/   r,   /)rZ   headers)r+   r\   r0   requestsra   YOU_API_URLraise_for_statusjson)rL   rS   rT   rl   rZ   responses         r   raw_resultszYouSearchAPIWrapper.raw_results   s      0 0 6B7&&&u77 *!)D<<m1T//01	
 	!!#}}r   c           
           | j                   |fi |j                         D ci c]  \  }}|	|| c}}}| j                  |      S c c}}w )zCRun query through you.com Search and parses results into Documents.)rr   rY   rg   )rL   rS   rT   keyvaluer]   s         r   r_   zYouSearchAPIWrapper.results   sZ     .T--
,2LLNPjc5e>OsEzP
 ""#566 Qs
   
A
A
c                   K   d| j                   xs di} | j                  |fi |}| j                  dk(  rd| _        t        j                         4 d{   }|j                  t         d| j                   ||      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)z7Get results from the you.com Search API asynchronously.ri   rj   r/   r,   Nrk   )r   rZ   rl      zError z: )r+   r\   r0   aiohttpClientSessionra   rn   statusrp   	Exceptionreason)rL   rS   rT   rl   rZ   sessionresr_   s           r   raw_results_asyncz%YouSearchAPIWrapper.raw_results_async  sY      0 0 6B7&&&u77 *!)D((* 
	I 
	Ig{{"m1T%7%7$89 #  	I 	I ::$$'HHJ.G"	I 	I 	I
	I 
	I 
	I $fSZZL3::,$GHH
	I	I /	I
	I	I 	I 	I
	I 
	I 
	I 
	I 
	Is   AEC;E,D,C=D,
#D-C?.D3D,?D D,EDE%D;E=D,?DD,ED	DD	D,E%D(&E,D>2D53D>:Ec           
         K    | j                   |fi |j                         D ci c]  \  }}|	|| c}} d {   }| j                  |      S c c}}w 7 w)N)r   rY   rg   )rL   rS   rT   rt   ru   raw_search_results_asyncs         r   results_asyncz!YouSearchAPIWrapper.results_async"  si     
 *@)?)?*
,2LLNPjc5e>OsEzP*
 $
  ""#;<< Q$
s   #A
AA	AAA)%r   r   r   r   r+   r   r   r   r0   r   r1   intr5   r6   r7   r8   r9   boolr:   r;   r<   r   classmethodr   r   rD   r   rO   rR   r\   r   r   rg   rr   r_   r   r   r   r   r   r*   r*   2   s   %N "&K#% BJM7=>I &*OXc])AEJ!<=>E!GXc]! "&K#%!GXc]!!%J%Ax}(,, FHSM (#$ 3   $ '"4  #0 '"	t 	 #	c S T >$ $$x. $L  
	477 7 
h	7II I 
	I4	=	= 	= 
h		=r   r*   )r   rI   typingr   r   r   r   r   rx   rm   langchain_core.documentsr   langchain_core.utilsr
   pydanticr   r   r   typing_extensionsr   rn   r   r    r#   r&   r*   r   r   r   <module>r      sq     5 5   - 5 6 6 "(EY EG^ G9 ) y=) y=r   