o
    Zh                     @   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d	ed
ededeeef fddZd	ed
ededeeddf f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   S/var/www/html/lang_env/lib/python3.10/site-packages/langchain_community/llms/you.py_request   s   r   c           
      k   s    d|i}t di |ddi}tj| |d|d}|  dd |D }zdd l}||}W n ty:   tdw | D ]&}	|	jd	v rGq?|	jd
krQ|	j	V  q?|	jdkr^t
d|	j	 td|	j d S )Nr   streamT)r   r   r   c                 s   s    | ]}|V  qd S )Nr   ).0
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 r   )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d
e
de	ee
  de	e dede
f
ddZ		dd
e
de	ee
  de	e dedee f
ddZede
fddZede
fddZede
f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)selfr2   r3   r4   r   r*   r   r   r   r   _call`   s   z	You._callc                 k   sD    |rt dd|i}t| jfd| ji|D ]}t|dV  qd S )Nr5   r6   r   )text)r)   r,   r7   r8   r   )r9   r2   r3   r4   r   r*   r    r   r   r   _streamo   s   
zYou._streamc                 C   s   | j dkrtS tS )Nr.   )r0   SMART_ENDPOINTRESEARCH_ENDPOINTr9   r   r   r   r7      s   
zYou._request_endpointc                 C   s   | j ptjd S )NZYDC_API_KEY)r1   osenvironr?   r   r   r   r8      s   zYou._api_keyc                 C   s   dS )Nzyou.comr   r?   r   r   r   	_llm_type   s   zYou._llm_type)NN)__name__
__module____qualname____doc__r   r0   r   __annotations__r1   r   strr   r	   r   r:   r   r   r<   propertyr7   r8   rB   r   r   r   r   r-   ;   sT   
 



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pydanticr   r=   r>   rH   r   r,   r-   r   r   r   r   <module>   s(    $"
"