U
    h                     @   s   d dl Z d dlmZmZmZmZmZmZmZ d dl	Z	d dl
mZ d dlmZ d dlmZ d dlmZ dZdZeeeeeef d	d
dZeeeeeddf d	ddZG dd deZdS )    N)AnyDict	GeneratorIteratorListLiteralOptional)CallbackManagerForLLMRun)LLM)GenerationChunk)Fieldzhttps://chat-api.you.com/smartz!https://chat-api.you.com/research)base_urlapi_keykwargsreturnc                 K   s(   d|i}t j| ||d}|  | S )z
    NOTE: This function can be replaced by a OpenAPI-generated Python SDK in the future,
    for better input/output typing support.
    	x-api-key)headersjson)requestspostraise_for_statusr   )r   r   r   r   response r   @/tmp/pip-unpacked-wheel-9gdii04g/langchain_community/llms/you.py_request   s    r   c           
      k   s   d|i}t f |ddi}tj| |d|d}|  dd |D }zdd l}||}W n tk
rt   tdY nX | D ]P}	|	jd	krq~|	jd
kr|	j	V  q~|	jdkrt
d|	j	 q~td|	j q~d S )Nr   streamT)r   r   r   c                 s   s   | ]
}|V  qd S )Nr   ).0Z
bytestringr   r   r   	<genexpr>"   s     z"_request_stream.<locals>.<genexpr>r   zPCould not import `sseclient`. Please install it with `pip install sseclient-py`.)Zsearch_resultsdonetokenerrorzError in response: zUnknown event type )dictr   r   r   	sseclientZ	SSEClientImportErroreventseventdata
ValueErrorNotImplementedError)
r   r   r   r   paramsr   Zevent_sourcer"   clientr%   r   r   r   _request_stream   s(    




r+   c                   @   s   e Zd ZU dZedddZed ed< edddZe	e
 ed	< de
e	ee
  e	e ee
d
ddZde
e	ee
  e	e eee d
ddZee
dddZee
dddZee
dddZdS )Youa  Wrapper around You.com's conversational Smart and Research APIs.

    Each API endpoint is designed to generate conversational
    responses to a variety of query types, including inline citations
    and web results when relevant.

    Smart Endpoint:
    - Quick, reliable answers for a variety of questions
    - Cites the entire web page URL

    Research Endpoint:
    - In-depth answers with extensive citations for a variety of questions
    - Cites the specific web page snippet relevant to the claim

    To connect to the You.com api requires an API key which
    you can get at https://api.you.com.

    For more information, check out the documentations at
    https://documentation.you.com/api-reference/.

    Args:
        endpoint: You.com conversational endpoints. Choose from "smart" or "research"
        ydc_api_key: You.com API key, if `YDC_API_KEY` is not set in the environment
    smartzCYou.com conversational endpoints. Choose from "smart" or "research")description)r-   ZresearchendpointNz>You.com API key, if `YDC_API_KEY` is not set in the envriomentydc_api_key)promptstoprun_managerr   r   c                 K   s4   |rt dd|i}t| jfd| ji|}|d S )N5Stop words are not implemented for You.com endpoints.queryr   Zanswer)r(   r   _request_endpoint_api_key)selfr1   r2   r3   r   r)   r   r   r   r   _call`   s    z	You._callc                 k   sB   |rt dd|i}t| jfd| ji|D ]}t|dV  q,d S )Nr4   r5   r   )text)r(   r+   r6   r7   r   )r8   r1   r2   r3   r   r)   r   r   r   r   _streamo   s    
zYou._stream)r   c                 C   s   | j dkrtS tS )Nr-   )r/   SMART_ENDPOINTRESEARCH_ENDPOINTr8   r   r   r   r6      s    
zYou._request_endpointc                 C   s   | j ptjd S )NZYDC_API_KEY)r0   osenvironr>   r   r   r   r7      s    zYou._api_keyc                 C   s   dS )Nzyou.comr   r>   r   r   r   	_llm_type   s    zYou._llm_type)NN)NN)__name__
__module____qualname____doc__r   r/   r   __annotations__r0   r   strr   r	   r   r9   r   r   r;   propertyr6   r7   rA   r   r   r   r   r,   ;   sB   
  
  
r,   )r?   typingr   r   r   r   r   r   r   r   Z langchain_core.callbacks.managerr	   Z#langchain_core.language_models.llmsr
   Zlangchain_core.outputsr   Zlangchain_core.pydantic_v1r   r<   r=   rG   r   r+   r,   r   r   r   r   <module>   s   $  "