a
    bg                     @   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   j/var/www/html/cobodadashboardai.evdpl.com/venv/lib/python3.9/site-packages/langchain_community/llms/you.py_request   s    r   c           
      k   s   d|i}t f i |ddi}tj| |d|d}|  dd |D }zdd l}||}W n tyv   tdY n0 | D ]P}	|	jd	v rq|	jd
kr|	j	V  q|	jdkrt
d|	j	 qtd|	j qd S )Nr   streamT)r   r   r   c                 s   s   | ]
}|V  qd S )Nr   ).0
bytestringr   r   r   	<genexpr>"       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)selfr3   r4   r5   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 )Nr6   r7   r   )text)r*   r-   r8   r9   r   )r:   r3   r4   r5   r   r+   r!   r   r   r   _streamo   s    
zYou._stream)r   c                 C   s   | j dkrtS tS )Nr/   )r1   SMART_ENDPOINTRESEARCH_ENDPOINTr:   r   r   r   r8      s    
zYou._request_endpointc                 C   s   | j ptjd S )NZYDC_API_KEY)r2   osenvironr@   r   r   r   r9      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   r1   r   __annotations__r2   r   strr   r	   r   r;   r   r   r=   propertyr8   r9   rC   r   r   r   r   r.   ;   sB   
  
  
r.   )rA   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?   rI   r   r-   r.   r   r   r   r   <module>   s   $"