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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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)Requestsz$meta-llama/Meta-Llama-3-70B-Instructc                   @   st  e Zd ZU dZeZeed< dZe	e
 ed< dZe	e ed< G d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_tokenc                   @   s   e Zd ZdZdS )zDeepInfra.ConfigZforbidN)__name__
__module____qualname__extra r   r   F/tmp/pip-unpacked-wheel-9gdii04g/langchain_community/llms/deepinfra.pyConfig(   s   r   )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   r   r   validate_environment+   s      zDeepInfra.validate_environment)r   c                 C   s   d| j id| jiS )zGet the identifying parameters.r   r   )r   r   selfr   r   r   _identifying_params4   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   _urlA   s    zDeepInfra._urlc                 C   s   d| j  ddS )Nzbearer zapplication/json)AuthorizationzContent-Type)r   r   r   r   r   _headersD   s    
zDeepInfra._headers)promptkwargsr   c                 C   s   | j pi }||}d|i|S )Ninput)r   )r    r&   r'   r   r   r   r   _bodyJ   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_statusS   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&   r0   r1   r'   requestresponser6   r   r   r   _callf   s
    zDeepInfra._callc                    s   t |  d}|j|  | ||d4 I d H B}| |j|j | I d H }|d d d W  5 Q I d H R  S Q I d H R X d S )Nr2   r4   r7   r   r8   )	r   r%   apostr#   r)   r/   statusr+   r;   r<   r   r   r   _acall   s     
zDeepInfra._acallc           
      k   s   t |  d}|j|  | ||ddid}|j}| | | |j|j t	|
 D ]&}t|}	|	r\|r|||	j |	V  q\d S Nr2   streamTr4   )r   r%   r9   r#   r)   r+   _handle_body_errorsr/   r:   _parse_stream
iter_lines_handle_sse_lineon_llm_new_token
r    r&   r0   r1   r'   r=   r>   Zresponse_textlinechunkr   r   r   _stream   s     
zDeepInfra._streamc           
   
   K  s   t |  d}|j|  | ||ddid4 I d H p}| I d H }| | | |j|j t	|j
2 z43 d H W }t|}	|	rl|r||	jI d H  |	V  ql6 W 5 Q I d H R X d S rC   )r   r%   r@   r#   r)   r+   rE   r/   rA   _parse_stream_asynccontentrH   rI   rJ   r   r   r   _astream   s     
zDeepInfra._astream)bodyr   c                 C   st   d|krpzB| dr$|tdd }t|}|dd}td| W n$ tjk
rn   td| Y nX 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lenr;   loadsgetr-   JSONDecodeError)r    rQ   Z
error_datarS   r   r   r   rE      s    

zDeepInfra._handle_body_errors)NN)NN)NN)NN)$r   r   r   __doc__DEFAULT_MODEL_IDr   str__annotations__r   r   r   r   r   r   r   propertyr   r   r!   r"   r#   r%   r)   intr/   r   r
   r?   r	   rB   r   r   rM   r   rP   rE   r   r   r   r   r      sh   
	  
!  
  
  
r   )rbodyr   c                 c   s$   | D ]}t |}|d k	r|V  qd S N_parse_stream_helperr_   rK   _liner   r   r   rF      s    rF   c                 C  s.   | 2 z"3 d H W }t |}|d k	r|V  q6 d S r`   ra   rc   r   r   r   rN      s    rN   )rK   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)rT   rU   stripdecode)rK   r   r   r   rb      s    

rb   c                 C   s@   z$t | }t|di ddW S  tk
r:   Y d S X d S )Ntokenr+   )r+   )r;   rV   r   rW   r-   )rK   objr   r   r   rH      s    
rH   )r;   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&langchain_community.utilities.requestsr   rZ   r   bytesr[   rF   StreamReaderrN   rb   rH   r   r   r   r   <module>   s   $ ?