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mZ d dlmZ d dlmZ d dlmZmZ d dlmZ d dlmZ d	ZG d
d deZee ee dddZe	jee dddZeee dddZeee dddZ dS )    N)AnyAsyncIteratorDictIteratorListMappingOptional)AsyncCallbackManagerForLLMRunCallbackManagerForLLMRun)LLM)GenerationChunk)get_from_dict_or_envpre_init)
ConfigDict)Requestsz$meta-llama/Meta-Llama-3-70B-Instructc                   @   sp  e Zd ZU dZeZeed< dZe	e
 ed< dZe	e ed< eddZee
e
dd	d
Zeeeef dddZeedddZedddZe
dddZeee
dddZeeddddZd&ee	ee  e	e eedddZ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!d)ee	ee  e	e ee"e  dd!d"Z#edd#d$d%Z$dS )*	DeepInfraa  DeepInfra models.

    To use, you should have the environment variable ``DEEPINFRA_API_TOKEN``
    set with your API token, or pass it as a named parameter to the
    constructor.

    Only supports `text-generation` and `text2text-generation` for now.

    Example:
        .. code-block:: python

            from langchain_community.llms import DeepInfra
            di = DeepInfra(model_id="google/flan-t5-xl",
                                deepinfra_api_token="my-api-key")
    model_idNmodel_kwargsdeepinfra_api_tokenZforbid)extra)valuesreturnc                 C   s   t |dd}||d< |S )z?Validate that api key and python package exists in environment.r   ZDEEPINFRA_API_TOKEN)r   )clsr   r    r   p/var/www/html/cobodadashboardai.evdpl.com/venv/lib/python3.9/site-packages/langchain_community/llms/deepinfra.pyvalidate_environment-   s
    zDeepInfra.validate_environment)r   c                 C   s   i d| j id| jiS )zGet the identifying parameters.r   r   )r   r   selfr   r   r   _identifying_params6   s
    zDeepInfra._identifying_paramsc                 C   s   dS )zReturn type of llm.Z	deepinfrar   r   r   r   r   	_llm_type>   s    zDeepInfra._llm_typec                 C   s   d| j  S )Nz'https://api.deepinfra.com/v1/inference/)r   r   r   r   r   _urlC   s    zDeepInfra._urlc                 C   s   d| j  ddS )Nzbearer zapplication/json)AuthorizationzContent-Type)r   r   r   r   r   _headersF   s    
zDeepInfra._headers)promptkwargsr   c                 C   s"   | j pi }i ||}d|i|S )Ninput)r   )r   r#   r$   r   r   r   r   _bodyL   s    
zDeepInfra._body)codetextr   c                 C   s   |dkrt d| n|dkr*t dnr|dkr<t dn`|dkrVt d| j nF|dkrht d	n4|d
krtd| n|dkrt d| d| d S )Ni  zDeepInfra Server: Error i  zDeepInfra Server: Unauthorizedi  i  z"DeepInfra Server: Model not found i  z%DeepInfra Server: Rate limit exceededi  z'DeepInfra received an invalid payload:    z6DeepInfra returned an unexpected response with status z: )	Exceptionr   
ValueError)r   r'   r(   r   r   r   _handle_statusU   s(    


zDeepInfra._handle_status)r#   stoprun_managerr$   r   c                 K   sP   t |  d}|j|  | ||d}| |j|j | }|d d d S )al  Call out to DeepInfra's inference API endpoint.

        Args:
            prompt: The prompt to pass into the model.
            stop: Optional list of stop words to use when generating.

        Returns:
            The string generated by the model.

        Example:
            .. code-block:: python

                response = di("Tell me a joke.")
        headersurldataresultsr   generated_text)	r   r"   postr    r&   r,   status_coder(   jsonr   r#   r-   r.   r$   requestresponser3   r   r   r   _callh   s
    zDeepInfra._callc              	      s   t |  d}|j|  | ||d4 I d H D}| |j|j | I d H }|d d d W  d   I d H  S 1 I d H s0    Y  d S )Nr/   r1   r4   r   r5   )	r   r"   apostr    r&   r,   statusr(   r8   r9   r   r   r   _acall   s    zDeepInfra._acallc           
      k   s   t |  d}|j|  | |i |ddid}|j}| | | |j|j t	|
 D ]&}t|}	|	r`|r||	j |	V  q`d S Nr/   streamTr1   )r   r"   r6   r    r&   r(   _handle_body_errorsr,   r7   _parse_stream
iter_lines_handle_sse_lineon_llm_new_token
r   r#   r-   r.   r$   r:   r;   Zresponse_textlinechunkr   r   r   _stream   s    
zDeepInfra._streamc           
   	   K  s   t |  d}|j|  | |i |ddid4 I d H }| I d H }| | | |j|j t	|j
2 z43 d H W }t|}	|	rp|r||	jI d H  |	V  qp6 W d   I d H  q1 I d H s0    Y  d S r@   )r   r"   r=   r    r&   r(   rB   r,   r>   _parse_stream_asynccontentrE   rF   rG   r   r   r   _astream   s    
zDeepInfra._astream)bodyr   c                 C   sr   d|v rnzB| dr$|tdd }t|}|dd}td| W n" tjyl   td| Y n0 dS )z
        Example error response:
        data: {"error_type": "validation_error",
        "error_message": "ConnectionError: ..."}
        errorzdata:Nerror_messagezUnknown errorzDeepInfra Server Error: zDeepInfra Server: )
startswithlenr8   loadsgetr*   JSONDecodeError)r   rN   Z
error_datarP   r   r   r   rB      s    

zDeepInfra._handle_body_errors)NN)NN)NN)NN)%__name__
__module____qualname____doc__DEFAULT_MODEL_IDr   str__annotations__r   r   r   r   r   Zmodel_configr   r   propertyr   r   r   r   r    r"   r&   intr,   r   r
   r<   r	   r?   r   r   rJ   r   rM   rB   r   r   r   r   r      sl   
	  
!  
  
  
r   )rbodyr   c                 c   s$   | D ]}t |}|d ur|V  qd S N_parse_stream_helperr_   rH   _liner   r   r   rC      s    rC   c                 C  s.   | 2 z"3 d H W }t |}|d ur|V  q6 d S r`   ra   rc   r   r   r   rK      s    rK   )rH   r   c                 C   sX   | rT|  drT|  dr*| tdd  } n| tdd  } |  dkrJd S | dS d S )Ns   data:s   data: s   [DONE]zutf-8)rQ   rR   stripdecode)rH   r   r   r   rb      s    

rb   c                 C   s>   z$t | }t|di ddW S  ty8   Y d S 0 d S )Ntokenr(   )r(   )r8   rS   r   rT   r*   )rH   objr   r   r   rE      s    
rE   )!r8   typingr   r   r   r   r   r   r   ZaiohttpZlangchain_core.callbacksr	   r
   Z#langchain_core.language_models.llmsr   Zlangchain_core.outputsr   Zlangchain_core.utilsr   r   Zpydanticr   Z&langchain_community.utilities.requestsr   rZ   r   bytesr[   rC   StreamReaderrK   rb   rE   r   r   r   r   <module>   s   $ @