a
    bgG                     @   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eedddZeedddZeeeef dddZeeeef dddZeee  eedddZ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&i feeed'd(d)Z"d-eeee  ee  ee#e$ d#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   w/var/www/html/cobodadashboardai.evdpl.com/venv/lib/python3.9/site-packages/langchain_community/llms/pai_eas_endpoint.pyvalidate_environment6   s    z#PaiEasEndpoint.validate_environment)r   c                 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)r   kwargsr   c                 K   s^   | j }| jd ur"|d ur"tdn| jd ur8| j|d< n||d< | jrR|| 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%   r   r   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)promptr%   run_managerr$   r   c              
   K   s   | j |fi |}| }d }zb| jrVd}| j|||fi |D ]}||j7 }q@|W S | ||}|d}	| ||	| jW S W n2 t	y }
 zt
d|
 W Y d }
~
n
d }
~
0 0 d S )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,   )r0   r(   r   c           	   
   C   s   d| j  d}| jdkr$d| i}n
d| i}| D ]\}}|||< q6tj| j||d}|jdkr|td|j d	|j zt	
|jW S  ty } z0t|t	jjr|jW  Y d
}~S |W Y d
}~n
d
}~0 0 d
S )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      s.    



zPaiEasEndpoint._call_easc                 k   s6  | j |fi |}d| j d}| jdkrnd|i|}tj| j||dd}t|jd}	|rf||	j |	V  n|dd	|}tj| j||dd}|j	d
dddD ]}
|
rt
|
d}|d }d }|d D ]}||v r|}qd }|r|d || }n|}|r&t|d}	|r ||	j |	V  |r q2qd 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      sL    




zPaiEasEndpoint._stream)NN)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      sZ   


  
&  
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    
