o
    ZhG                     @   s   d dl Z d dlZd dl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mZ d dlmZ eeZG dd	 d	eZdS )
    N)AnyDictIteratorListMappingOptional)CallbackManagerForLLMRun)LLM)GenerationChunk)get_from_dict_or_envpre_init)enforce_stop_tokensc                   @   s  e Zd ZU dZ	 eed< 	 eed< 	 dZee ed< dZ	ee
 ed< dZee
 ed	< d
Zee ed< dZeee  ed< 	 dZeed< 	 dZee ed< dZee ed< ededefddZedefddZedeeef fddZedeeef fddZdeee  dedefddZed ed!eee  de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(i fd$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S )/PaiEasEndpointam  Langchain LLM class to help to access eass llm service.

        To use this endpoint, must have a deployed eas chat llm service on PAI AliCloud.
    One can set the environment variable ``eas_service_url`` and ``eas_service_token``.
    The environment variables can set with your eas service url and service token.

    Example:
        .. code-block:: python

            from langchain_community.llms.pai_eas_endpoint import PaiEasEndpoint
            eas_chat_endpoint = PaiEasChatEndpoint(
                eas_service_url="your_service_url",
                eas_service_token="your_service_token"
            )
    eas_service_urleas_service_tokeni   max_new_tokensgffffff?temperatureg?top_pr   top_kNstop_sequencesF	streamingmodel_kwargsz2.0versionvaluesreturnc                 C   s$   t |dd|d< t |dd|d< |S )z?Validate that api key and python package exists in environment.r   ZEAS_SERVICE_URLr   ZEAS_SERVICE_TOKEN)r   )clsr    r   `/var/www/html/lang_env/lib/python3.10/site-packages/langchain_community/llms/pai_eas_endpoint.pyvalidate_environment6   s   z#PaiEasEndpoint.validate_environmentc                 C   s   dS )zReturn type of llm.Zpai_eas_endpointr   selfr   r   r   	_llm_typeB   s   zPaiEasEndpoint._llm_typec                 C   s   | j | j| j| jg dS )z2Get the default parameters for calling Cohere API.)r   r   r   r   r   )r   r   r   r   r   r   r   r   _default_paramsG   s   zPaiEasEndpoint._default_paramsc                 C   s   | j pi }| j| jd|S )zGet the identifying parameters.)r   r   )r   r   r   )r    Z_model_kwargsr   r   r   _identifying_paramsR   s   
z"PaiEasEndpoint._identifying_paramskwargsc                 K   s\   | j }| jd ur|d urtd| jd ur| j|d< n||d< | jr(|| j i ||S )Nz2`stop` found in both the input and default params.stop)r"   r   
ValueErrorr   update)r    r   r$   paramsr   r   r   _invocation_params\   s   
z!PaiEasEndpoint._invocation_paramsresponser%   c                 C   s.   |dkr| }n| d }|rt ||}d|S )N1.0r*    )r   join)r*   r%   r   textr   r   r   _process_responsej   s   

z PaiEasEndpoint._process_responsepromptrun_managerc              
   K   s   | j |fi |}| }d }z/| jr+d}| j|||fi |D ]}||j7 }q |W S | ||}|d}	| ||	| jW S  t	yQ }
 zt
d|
 d }
~
ww )Nr,   r%   zError raised by the service: )r)   stripr   _streamr.   	_call_easgetr/   r   	Exceptionr&   )r    r0   r%   r1   r$   r(   r*   
completionchunk_stoperrorr   r   r   _callw   s    
zPaiEasEndpoint._callr,   r(   c           	   
   C   s   d| j  d}| jdkrd| i}nd| i}| D ]\}}|||< qtj| j||d}|jdkr>td|j d	|j zt	
|jW S  tyc } zt|t	jjr]|jW  Y d
}~S |d
}~ww )z#Generate text from the eas service.zapplication/json)zContent-TypeAuthorizationr+   	input_idsr0   )headersjson   z Request failed with status code z and message N)r   r   itemsrequestspostr   status_coder6   r.   r?   loads
isinstancedecoderJSONDecodeError)	r    r0   r(   r>   bodykeyvaluer*   er   r   r   r4      s2   



zPaiEasEndpoint._call_easc                 k   s6   | j |fi |}d| j d}| jdkr9d|i|}tj| j||dd}t|jd}	|r4||	j |	V  d S |dd	|}tj| j||dd}|j	d
dddD ]F}
|
rt
|
d}|d }d }|d D ]}||v rp|}qhd }|r|d || }n|}|rt|d}	|r||	j |	V  |r d S qRd S )NzTest Client)z
User-Agentr<   r+   r=   T)r>   r?   stream)r.   True)r0   Zuse_stream_chati    F    )
chunk_sizedecode_unicode	delimiterzutf-8r*   r%   )r)   r   r   rB   rC   r   r
   r.   Zon_llm_new_token
iter_linesr?   rE   decodeindex)r    r0   r%   r1   r$   Zinvocation_paramsr>   Zploadr*   resr8   dataoutputZstop_seq_foundZstop_seqr.   r   r   r   r3      sT   





zPaiEasEndpoint._stream)NN)&__name__
__module____qualname____doc__str__annotations__r   r   intr   floatr   r   r   r   r   boolr   dictr   r   r   r   propertyr!   r   r"   r   r#   r)   staticmethodr/   r   r;   r4   r   r
   r3   r   r   r   r   r      s   
 
	




&
r   )r?   loggingtypingr   r   r   r   r   r   rB   Zlangchain_core.callbacksr   Z#langchain_core.language_models.llmsr	   Zlangchain_core.outputsr
   Zlangchain_core.utilsr   r   Zlangchain_community.llms.utilsr   	getLoggerrY   loggerr   r   r   r   r   <module>   s     
