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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dee dee fddZde	jdee fddZdedee fddZdedee f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                   @   s  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de
d	e
fd
dZed	eeef fddZed	efddZd	efddZd	e
fddZdeded	e
fddZdeded	dfddZ		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f
d d!Z		d)dede	ee  de	e ded	ee  f
d"d#Z!		d)dede	ee  de	e ded	e"e  f
d$d%Z#d&ed	df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   Y/var/www/html/lang_env/lib/python3.10/site-packages/langchain_community/llms/deepinfra.pyvalidate_environment-   s
   zDeepInfra.validate_environmentc                 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kwargsc                 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textc                 C   s   |dkrt d| |dkrt d|dkrt d|dkr't d| j |dkr/t d	|d
kr:td| |dkrHt 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stoprun_managerc                 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   _callg   s
   zDeepInfra._callc              	      s   t |  d}|j|  | ||d4 I d H "}| |j|j | I d H }|d d d W  d   I d H  S 1 I d H sAw   Y  d S )Nr/   r1   r4   r   r5   )	r   r"   apostr    r&   r,   statusr(   r8   r9   r   r   r   _acall   s   0zDeepInfra._acallc           
      k   s    t |  d}|j|  | |i |ddid}|j}| | | |j|j t	|
 D ]}t|}	|	rD|rA||	j |	V  q1d 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 A}| I d H }| | | |j|j t	|j
2 z3 d H W }t|}	|	rS|rP||	jI d H  |	V  q96 W d   I d H  d S 1 I d H sfw   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bodyc                 C   sj   d|v r3z| dr|tdd }t|}|dd}td|  tjy2   td| w 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)%__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      s   
 	

!





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  s0   | 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   c                 C   sX   | r*|  dr*|  dr| tdd  } n| tdd  } |  dkr%d 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y   Y d S w )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     $ ?