
    hG                         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j*                  e      Z G d d	e      Zy)
    N)AnyDictIteratorListMappingOptional)CallbackManagerForLLMRun)LLM)GenerationChunk)get_from_dict_or_envpre_init)enforce_stop_tokensc                      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       Zedefd       Zedeeef   fd       Zedeeef   fd       Zdeee      dedefdZededeee      dee   defd       Z	 	 d%dedeee      dee    dedef
d Z!d!i fded"edefd#Z"	 	 d%dedeee      dee    dede#e$   f
d$Z%y)&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                 F    t        |dd      |d<   t        |dd      |d<   |S )z?Validate that api key and python package exists in environment.r   EAS_SERVICE_URLr   EAS_SERVICE_TOKEN)r   )clsr   s     g/var/www/html/eduruby.in/venv/lib/python3.12/site-packages/langchain_community/llms/pai_eas_endpoint.pyvalidate_environmentz#PaiEasEndpoint.validate_environment6   s?     %9%'8%
 ! ';')<'
"#     c                      y)zReturn type of llm.pai_eas_endpoint selfs    r!   	_llm_typezPaiEasEndpoint._llm_typeB   s     "r#   c                 b    | 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'   s    r!   _default_paramszPaiEasEndpoint._default_paramsG   s1     #11++ZZZZ 
 	
r#   c                 X    | j                   xs i }| j                  | j                  d|S )zGet the identifying parameters.)r   r   )r   r   r   )r(   _model_kwargss     r!   _identifying_paramsz"PaiEasEndpoint._identifying_paramsR   s:     ))/R#33!%!7!7
 
 	
r#   kwargsc                     | j                   }| j                  |t        d      | j                  | j                  |d<   n||d<   | j                  r|j	                  | j                         i ||S )Nz2`stop` found in both the input and default params.stop)r+   r   
ValueErrorr   update)r(   r   r/   paramss       r!   _invocation_paramsz!PaiEasEndpoint._invocation_params\   s{     %%*~/IQRR  ,!00F6N+F6NMM$++,#&#F##r#   responser1   c                 Z    |dk(  r| }n| d   }|rt        ||      }dj                  |      S )N1.0r6    )r   join)r6   r1   r   texts       r!   _process_responsez PaiEasEndpoint._process_responsej   s8     eDJ'D&tT2Dwwt}r#   promptrun_managerc                 ~    | j                   |fi |}|j                         }d }	 | j                  r-d} | j                  |||fi |D ]  }||j                  z  } |S | j                  ||      }|j                  d      }	| j                  ||	| j                        S # t        $ r}
t        d|
       d }
~
ww xY w)Nr9   r1   zError raised by the service: )r5   stripr   _streamr;   	_call_easgetr<   r   	Exceptionr2   )r(   r=   r1   r>   r/   r4   r6   
completionchunk_stoperrors              r!   _callzPaiEasEndpoint._callw   s     )((88	F~~
)T\\&$NvN -E%**,J-!!>>&&9

6*--ht||LL 	F<UGDEE	Fs   8B   ?B   	B<)B77B<r9   r4   c                    d| j                    d}| j                  dk(  rd| i}nd| i}|j                         D ]
  \  }}|||<    t        j                  | j
                  ||      }|j                  dk7  r%t        d|j                   d	|j                         	 t        j                  |j                        S # t        $ r<}t        |t        j                  j                        r|j                  cY d
}~S |d
}~ww xY w)z#Generate text from the eas service.zapplication/json)zContent-TypeAuthorizationr8   	input_idsr=   )headersjson   z Request failed with status code z and message N)r   r   itemsrequestspostr   status_coderD   r;   rN   loads
isinstancedecoderJSONDecodeError)	r(   r=   r4   rM   bodykeyvaluer6   es	            r!   rB   zPaiEasEndpoint._call_eas   s    / $ 6 67
 <<5 xD
 VHD
 !,,. 	JCDI	 ==!5!5wTR3&283G3G2H0 
	::hmm,, 	!T\\99:}}$G	s$   B> >	D/C>6D<C>>Dc              +     K    | j                   |fi |}d| j                   d}| j                  dk(  rad|i|}t        j                  | j
                  ||d      }t        |j                        }	|r|j                  |	j                         |	 y |dd	|}t        j                  | j
                  ||d      }|j                  d
dd      D ]  }
|
st        j                  |
j                  d            }|d   }d }|d   D ]	  }||v s|} d }|r|d |j                  |       }n|}|r-t        |      }	|r|j                  |	j                         |	 |s y  y w)NzTest Client)z
User-AgentrK   r8   rL   T)rM   rN   stream)r;   True)r=   use_stream_chati    F    )
chunk_sizedecode_unicode	delimiterzutf-8r6   r1   )r5   r   r   rQ   rR   r   r   r;   on_llm_new_token
iter_linesrN   rT   decodeindex)r(   r=   r1   r>   r/   invocation_paramsrM   ploadr6   resrF   dataoutputstop_seq_foundstop_seqr;   s                   r!   rA   zPaiEasEndpoint._stream   s     4D33DCFC ( $ 6 67

 <<5  &>,=>E}}$$gE$H "x}}5C,,SXX6 I%&VDUVE}}$$gE$H ",, -   ::ell7&;<D!*-F48N$5f$= 6#v--5N6
 +/D%%&D^(DE% -48&'88B!	 &9s   CE'7E'AE'#E')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.   r5   staticmethodr<   r	   rI   rB   r   r   rA   r&   r#   r!   r   r      sC     &$'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   )rN   loggingtypingr   r   r   r   r   r   rQ   langchain_core.callbacksr	   #langchain_core.language_models.llmsr
   langchain_core.outputsr   langchain_core.utilsr   r   langchain_community.llms.utilsr   	getLoggerro   loggerr   r&   r#   r!   <module>r      sC      ? ?  = 3 2 ? >			8	$_S _r#   