
    dhG                         S SK r S SKrS SKJrJrJrJrJrJr  S SK	r	S SK
Jr  S SKJr  S SKJr  S SKJrJr  S SKJr  \R*                  " \5      r " S S	\5      rg)
    N)AnyDictIteratorListMappingOptional)CallbackManagerForLLMRun)LLM)GenerationChunk)get_from_dict_or_envpre_init)enforce_stop_tokensc                   <   \ rS rSr% Sr \\S'    \\S'    Sr\\	   \S'   Sr
\\   \S'   S	r\\   \S
'   Sr\\	   \S'   Sr\\\      \S'    Sr\\S'    Sr\\   \S'   Sr\\   \S'   \S\S\4S j5       r\S\4S j5       r\S\\\4   4S j5       r\S\\\4   4S j5       rS\\\      S\S\4S jr\S\S\\\      S\\   S\4S j5       r   S'S\S\\\      S \\!   S\S\4
S! jjr"S"0 4S\S#\S\4S$ jjr#  S'S\S\\\      S \\!   S\S\$\%   4
S% jjr&S&r'g)(PaiEasEndpoint   aA  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                 F    [        USS5      US'   [        USS5      US'   U$ )z?Validate that api key and python package exists in environment.r   EAS_SERVICE_URLr   EAS_SERVICE_TOKEN)r   )clsr   s     a/var/www/html/shao/venv/lib/python3.13/site-packages/langchain_community/llms/pai_eas_endpoint.pyvalidate_environment#PaiEasEndpoint.validate_environment6   s?     %9%'8%
 ! ';')<'
"#     c                     g)zReturn type of llm.pai_eas_endpoint selfs    r"   	_llm_typePaiEasEndpoint._llm_typeB   s     "r%   c                 b    U R                   U R                  U R                  U R                  / S.$ )z2Get the default parameters for calling Cohere API.)r   r   r   r   r   )r   r   r   r   r)   s    r"   _default_paramsPaiEasEndpoint._default_paramsG   s1     #11++ZZZZ 
 	
r%   c                 b    U R                   =(       d    0 nU R                  U R                  S.UE$ )zGet the identifying parameters.)r   r   )r   r   r   )r*   _model_kwargss     r"   _identifying_params"PaiEasEndpoint._identifying_paramsR   s:     ))/R#33!%!7!7
 
 	
r%   kwargsc                     U R                   nU R                  b  Ub  [        S5      eU R                  b  U R                  US'   OXS'   U R                  (       a  UR	                  U R                  5        0 UEUE$ )Nz2`stop` found in both the input and default params.stop)r.   r   
ValueErrorr   update)r*   r   r4   paramss       r"   _invocation_params!PaiEasEndpoint._invocation_params\   sy     %%*~/IQRR  ,!00F6N+6NMM$++,#&#F##r%   responser6   c                 d    US:X  a  U nOU S   nU(       a  [        X15      nSR                  U5      $ )N1.0r<    )r   join)r<   r6   r   texts       r"   _process_response PaiEasEndpoint._process_responsej   s6     eDJ'D&t2Dwwt}r%   promptrun_managerc                    U R                   " U40 UD6nUR                  5       nS n U R                  (       a-  SnU R                  " XU40 UD6 H  nXxR                  -  nM     U$ U R                  X5      nUR                  S5      n	U R                  XiU R                  5      $ ! [         a  n
[        SU
 35      eS n
A
ff = f)Nr?   r6   zError raised by the service: )r:   stripr   _streamrA   	_call_easgetrB   r   	Exceptionr7   )r*   rD   r6   rE   r4   r9   r<   
completionchunk_stoperrors              r"   _callPaiEasEndpoint._callw   s     ((88	F~~
!\\&NvNE**,J O!!>>&9

6*--ht||LL 	F<UGDEE	Fs   =B# %=B# #
C -B;;C r?   r9   c                    SU R                    S.nU R                  S:X  a  SU 0nOSU 0nUR                  5        H	  u  pVXdU'   M     [        R                  " U R
                  X4S9nUR                  S:w  a%  [        SUR                   S	UR                   35      e [        R                  " UR                  5      $ ! [         aA  n[        U[        R                  R                  5      (       a  UR                  s S
nA$ UeS
nAff = f)z#Generate text from the eas service.zapplication/json)zContent-TypeAuthorizationr>   	input_idsrD   )headersjson   z Request failed with status code z and message N)r   r   itemsrequestspostr   status_coderK   rA   rV   loads
isinstancedecoderJSONDecodeError)	r*   rD   r9   rU   bodykeyvaluer<   es	            r"   rI   PaiEasEndpoint._call_eas   s     / $ 6 67
 <<5 xD
 VHD
 !,,.JCI ) ==!5!5wR3&283G3G2H0 
	::hmm,, 	!T\\99::}}$G	s$   B> >
D	4D<D	DD	c              +     #    U R                   " U40 UD6nSU R                   S.nU R                  S:X  a`  SU0UEn[        R                  " U R
                  XgSS9n[        UR                  S9n	U(       a  UR                  U	R                  5        U	v   g USS	.UEn[        R                  " U R
                  XgSS9nUR                  S
SSS9 H  n
U
(       d  M  [        R                  " U
R                  S5      5      nUS   nS nUS    H  nX;   d  M
  UnM     S nU(       a  US UR                  U5       nOUnU(       a/  [        US9n	U(       a  UR                  U	R                  5        U	v   U(       d  M    g    g 7f)NzTest Client)z
User-AgentrS   r>   rT   T)rU   rV   stream)rA   True)rD   use_stream_chati    F    )
chunk_sizedecode_unicode	delimiterzutf-8r<   r6   )r:   r   r   rY   rZ   r   r   rA   on_llm_new_token
iter_linesrV   r\   decodeindex)r*   rD   r6   rE   r4   invocation_paramsrU   ploadr<   resrM   dataoutputstop_seq_foundstop_seqrA   s                   r"   rH   PaiEasEndpoint._stream   s     !33DCFC ( $ 6 67

 <<5  &>,=>E}}$$g$H "x}}5C,,SXX6 I%&VDUVE}}$$g$H ",, -  5::ell7&;<D!*-F48N$5f$=#--5N %>
 +/D%%&D^(DE% -48&'88B!	 &~9s   CE?7E?A!E?:E?r(   )NN)(__name__
__module____qualname____firstlineno____doc__str__annotations__r   r   intr   floatr   r   r   r   r   boolr   dictr   r   r   r#   propertyr+   r   r.   r   r2   r:   staticmethodrB   r	   rP   rI   r   r   rH   __static_attributes__r(   r%   r"   r   r      sH     &$'NHSM'#'K%' E8E? E8C=*.NHT#Y'."ItK#'L(4.'"GXc]"	$ 	4 	 	 "3 " " 
c3h 
 
 
WS#X%6 
 
$&tCy1$=@$	$ 

%d3i0
;CC=
	
 
 %):>	FF tCy!F 67	F
 F 
F. ') ! !$ ! !L %):>	>> tCy!> 67	>
 > 
/	"> >r%   r   )rV   loggingtypingr   r   r   r   r   r   rY   langchain_core.callbacksr	   #langchain_core.language_models.llmsr
   langchain_core.outputsr   langchain_core.utilsr   r   langchain_community.llms.utilsr   	getLoggerry   loggerr   r(   r%   r"   <module>r      sA      ? ?  = 3 2 ? >			8	$_S _r%   