
    dhP                        S SK r S SKrS SKrS SKJr  S SKJr  S SKJ	r	J
r
JrJrJ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Jr  S S
KJrJrJrJrJrJ r   Sr! " S S\"5      r# " S S\$\5      r% " S S5      r& " S S\&5      r' " S S\'5      r( " S S\&5      r) " S S\&5      r* " S S\&5      r+ " S S\+5      r, " S S\5      r- " S  S!\\-5      r.g)"    N)abstractmethod)Enum)AnyDictListMappingOptional)urlparse)CallbackManagerForLLMRun)BaseLLM)
Generation	LLMResult)convert_to_secret_strget_from_dict_or_env)	BaseModel
ConfigDict	SecretStrfield_validatormodel_validator	validator2   c                   d    \ rS rSrSrS\4S\S\S\S\SS	4
S
 jjr SS\	S\
\   S\S\	4S jjrSrg	)AzureMLEndpointClient   z AzureML Managed Endpoint client. endpoint_urlendpoint_api_keydeployment_nametimeoutreturnNc                 f    U(       a  U(       d  [        S5      eXl        X l        X0l        X@l        g)zInitialize the class.zXA key/token and REST endpoint should 
                be provided to invoke the endpointN)
ValueErrorr   r   r   r   )selfr   r   r   r   s        a/var/www/html/shao/venv/lib/python3.13/site-packages/langchain_community/llms/azureml_endpoint.py__init__AzureMLEndpointClient.__init__   s4      |6  ) 0.    bodyrun_managerkwargsc                 J   SSU R                   -   S.nU R                  S:w  a  U R                  US'   [        R                  R	                  U R
                  X5      n[        R                  R                  XSR                  SU R                  5      S9nUR                  5       nU$ )zcall.application/jsonzBearer )zContent-TypeAuthorizationr   zazureml-model-deploymentr   )r   )
r   r   urllibrequestRequestr   urlopengetr   read)r#   r(   r)   r*   headersreqresponseresults           r$   callAzureMLEndpointClient.call.   s     /'$*?*??
 2%262F2FG./nn$$T%6%6F>>))It||< * 
 r'   )r   r   r   r   N)__name__
__module____qualname____firstlineno____doc__DEFAULT_TIMEOUTstrintr%   bytesr	   r   r   r8   __static_attributes__ r'   r$   r   r      s}    *  "&  	
  
* ;? 67 	
 
 r'   r   c                   $    \ rS rSrSrSrSrSrSrg)AzureMLEndpointApiTypeH   zAzure ML endpoints API types. Use `dedicated` for models deployed in hosted
infrastructure (also known as Online Endpoints in Azure Machine Learning),
or `serverless` for models deployed as a service with a
pay-as-you-go billing or PTU.
	dedicatedrealtime
serverlessrE   N)	r;   r<   r=   r>   r?   rI   rJ   rK   rD   rE   r'   r$   rG   rG   H   s     IHJr'   rG   c            	           \ rS rSr% Sr Sr\\   \S'    Sr	\\   \S'    Sr
\\S'   \S\S	\4S
 j5       r\S	\\   4S j5       r\R"                  4S\S\S\S	\4S jjr\\R"                  4S\S\S	\4S jj5       rSrg)ContentFormatterBaseT   zRTransform request and response of AzureML endpoint to match with
required schema.
r,   content_typeacceptszError while formatting response payload for chat model of type  `{api_type}`. Are you using the right formatter for the deployed  model and endpoint type?format_error_msgpromptr    c                 n    SSSSSSSS.nUR                  5        H  u  p#U R                  X#5      n M     U $ )	z*Escapes any special characters in `prompt`z\\z\"z\bz\fz\nz\rz\t)\"
	)itemsreplace)rR   
escape_mapescape_sequenceescaped_sequences       r$   escape_special_characters.ContentFormatterBase.escape_special_characters   sN     

 2<1A1A1C-O^^OFF 2D r'   c                 $    [         R                  /$ )zSupported APIs for the given formatter. Azure ML supports
deploying models using different hosting methods. Each method may have
a different API structure.rG   rI   r#   s    r$   supported_api_types(ContentFormatterBase.supported_api_types   s     '0011r'   model_kwargsapi_typec                     [        5       e)zFormats the request body according to the input schema of
the model. Returns bytes or seekable file like object in the
format specified in the content_type request header.
)NotImplementedError)r#   rR   rg   rh   s       r$   format_request_payload+ContentFormatterBase.format_request_payload   s     "##r'   outputc                     g)zFormats the response body according to the output
schema of the model. Returns the data type that is
received from the response.
NrE   )r#   rm   rh   s      r$   format_response_payload,ContentFormatterBase.format_response_payload   s    r'   rE   N)r;   r<   r=   r>   r?   rO   r	   rA   __annotations__rP   rQ   staticmethodr`   propertyr   rG   re   rI   r   r   rk   r   rC   r   ro   rD   rE   r'   r$   rM   rM   T   s    8 #5L(3-4@/GXc]/G	$ c  # #  $ 2T*@%A 2 2 ,B+K+K	
$
$ 
$ )	
$
 

$  ,B+K+K ) 
	 r'   rM   c                   f    \ rS rSrSr\S\\   4S j5       rS\	S\
S\S\4S jrS	\S\S\4S
 jrSrg)GPT2ContentFormatter   zContent handler for GPT2r    c                 $    [         R                  /$ r:   rc   rd   s    r$   re   (GPT2ContentFormatter.supported_api_types       &0011r'   rR   rg   rh   c                     [         R                  U5      n[        R                  " SSU S3/0US.5      n[        R                  U5      $ )Ninput_stringrU   inputs
parametersrM   r`   jsondumpsrA   encoder#   rR   rg   rh   request_payloads        r$   rk   +GPT2ContentFormatter.format_request_payload   sM     &??G**)axqM?;*
 zz/**r'   rm   c                      [         R                  " U5      S   S   n[        US9$ ! [        [        [        4 a(  n[        U R                  R                  US95      UeS nAff = f)Nr   0rh   text	r   loadsKeyError
IndexError	TypeErrorr"   rQ   formatr   r#   rm   rh   choicees        r$   ro   ,GPT2ContentFormatter.format_response_payload   sj    	UZZ'*3/F v&& *i0 	UT229989LMSTT	Us   ' A$#AA$rE   Nr;   r<   r=   r>   r?   rs   r   rG   re   rA   r   rC   rk   r   ro   rD   rE   r'   r$   ru   ru      sf    "2T*@%A 2 2
+
+)-
+9O
+	
+'''='	'r'   ru   c                   @   ^  \ rS rSr% SrSr\\S'   SU 4S jjrSr	U =r
$ )OSSContentFormatter   z\Deprecated: Kept for backwards compatibility

Content handler for LLMs from the OSS catalog.Ncontent_formatterc                 N   > [         TU ]  5         [        R                  " S5        g )Nz`OSSContentFormatter` will be deprecated in the future. 
                      Please use `GPT2ContentFormatter` instead.  
                      superr%   warningswarnr#   	__class__s    r$   r%   OSSContentFormatter.__init__   s    	
r'   rE   r    Nr;   r<   r=   r>   r?   r   r   rq   r%   rD   __classcell__r   s   @r$   r   r      s    6 "s!
 
r'   r   c                   f    \ rS rSrSr\S\\   4S j5       rS\	S\
S\S\4S jrS	\S\S\4S
 jrSrg)HFContentFormatter   z6Content handler for LLMs from the HuggingFace catalog.r    c                 $    [         R                  /$ r:   rc   rd   s    r$   re   &HFContentFormatter.supported_api_types   ry   r'   rR   rg   rh   c                     [         R                  U5        [        R                  " SU S3/US.5      n[        R                  U5      $ )NrU   r|   r   r   s        r$   rk   )HFContentFormatter.format_request_payload   sF     	66v>**vha=/*
 zz/**r'   rm   c                      [         R                  " U5      S   S   S   n[        US9$ ! [        [        [        4 a(  n[        U R                  R                  US95      UeS nAff = f)Nr   r   generated_textr   r   r   r   s        r$   ro   *HFContentFormatter.format_response_payload   sp    	UZZ'*3/0@AF v&& *i0 	UT229989LMSTT	Us   * A'#A""A'rE   Nr   rE   r'   r$   r   r      sf    @2T*@%A 2 2
+
+)-
+9O
+	
+'''='	'r'   r   c                   f    \ rS rSrSr\S\\   4S j5       rS\	S\
S\S\4S jrS	\S\S\4S
 jrSrg)DollyContentFormatter   z*Content handler for the Dolly-v2-12b modelr    c                 $    [         R                  /$ r:   rc   rd   s    r$   re   )DollyContentFormatter.supported_api_types  ry   r'   rR   rg   rh   c                     [         R                  U5      n[        R                  " SSU S3/0US.5      n[        R                  U5      $ )Nr{   rU   )
input_datar~   r   r   s        r$   rk   ,DollyContentFormatter.format_request_payload  sM     &??G**-!F81?*
 zz/**r'   rm   c                      [         R                  " U5      S   n[        US9$ ! [        [        [        4 a(  n[        U R                  R                  US95      UeS nAff = f)Nr   r   r   r   r   s        r$   ro   -DollyContentFormatter.format_response_payload  se    	UZZ'*F v&& *i0 	UT229989LMSTT	Us   $ A!#AA!rE   Nr   rE   r'   r$   r   r      sf    42T*@%A 2 2
+
+)-
+9O
+	
+'''='	'r'   r   c                   f    \ rS rSrSr\S\\   4S j5       rS\	S\
S\S\4S jrS	\S\S\4S
 jrSrg)CustomOpenAIContentFormatteri  zAContent formatter for models that use the OpenAI like API scheme.r    c                 B    [         R                  [         R                  /$ r:   )rG   rI   rK   rd   s    r$   re   0CustomOpenAIContentFormatter.supported_api_types  s    &002H2S2STTr'   rR   rg   rh   c                 \   [         R                  U5      nU[        R                  [        R                  4;   a!  [
        R                  " SSU S3/US.05      nO>U[        R                  :X  a  [
        R                  " SU0UE5      nO[        SU S35      e[        R                  U5      $ )z/Formats the request according to the chosen apir   rU   )r{   r~   rR   `api_type` # is not supported by this formatter)rM   r`   rG   rI   rJ   r   r   rK   r"   rA   r   r   s        r$   rk   3CustomOpenAIContentFormatter.format_request_payload"  s     &??G",,"++
 
 #jj +,VHA&2#O /:::"jj(F)Kl)KLOhZ'JK  zz/**r'   rm   c           	         U[         R                  [         R                  4;   a&   [        R                  " U5      S   S   n[        US9$ U[         R                  :X  a   [        R                  " U5      S   S   n[        U[        5      (       d  [        S5      e [        US   R                  5       [        UR!                  S	5      UR!                  S
5      S9S9$ [        SU S35      e! [
        [        [        4 a(  n[        U R                  R                  US95      UeSnAff = f! [
        [        [        4 a(  n[        U R                  R                  US95      UeSnAff = f)zFormats responser   r   r   Nr   choicesziEndpoint response is not well formed for a chat model. Expected `dict` but `{type(choice)}` was received.r   finish_reasonlogprobs)r   r   )r   generation_infor   r   )rG   rI   rJ   r   r   r   r   r   r"   rQ   r   r   rK   
isinstancedictstripr2   r   s        r$   ro   4CustomOpenAIContentFormatter.format_response_payload;  sn    ",,"++
 
YF+A.s3 6**-888	YF+I6q9!&$//#$  0 F^))+ $"(**_"=#ZZ
3!  ;xj0STUU+ j)4 Y !6!6!=!=x!=!PQWXXY j)4 Y !6!6!=!=x!=!PQWXXYs/   C-  <D- -D*#D%%D*-E*#E%%E*rE   Nr   rE   r'   r$   r   r     sm    KUT*@%A U U++)-+9O+	+2VV'=V	Vr'   r   c                   @   ^  \ rS rSr% SrSr\\S'   SU 4S jjrSr	U =r
$ )LlamaContentFormatteri]  zJDeprecated: Kept for backwards compatibility

Content formatter for Llama.Nr   c                 N   > [         TU ]  5         [        R                  " S5        g )Nz`LlamaContentFormatter` will be deprecated in the future. 
                Please use `CustomOpenAIContentFormatter` instead.  
            r   r   s    r$   r%   LlamaContentFormatter.__init__d  s    	
r'   rE   r   r   r   s   @r$   r   r   ]  s    $ "s!
 
r'   r   c                      \ rS rSr% SrSr\\S'    \R                  r
\\S'    \" S5      r\\S'    Sr\\S'    \r\\S'    S	r\\S
'   Sr\\S'   S	r\\S'    S	r\\   \S'    \" SS9r\" SS9\S\S\4S j5       5       r\" S5      S\S\S\ 4S j5       r!\"" SSS9\S\S\4S j5       5       r#\" S5      S\S\S\4S j5       r$\" S
SS9S\S\S\%4S j5       r&Sr'g	)AzureMLBaseEndpointim  z Azure ML Online Endpoint models.r   r   endpoint_api_typer   r   r   Nhttp_client   max_retriesr   rg   rE   )protected_namespacesbefore)modevaluesr    c                     [        [        USS5      5      US'   [        USS5      US'   [        USSS5      US'   [        USS	[        R                  5      US'   [        US
S[	        [
        5      5      US
'   U$ )Nr   AZUREML_ENDPOINT_API_KEYr   AZUREML_ENDPOINT_URLr   AZUREML_DEPLOYMENT_NAMEr   r   AZUREML_ENDPOINT_API_TYPEr   AZUREML_TIMEOUT)r   r   rG   rI   rA   r@   )clsr   s     r$   validate_environ$AzureMLBaseEndpoint.validate_environ  s     &; );=WX&
!" "6N$:"
~ %9%'@"%
 ! ';'",,	'
"# 1 	
y r'   field_valuec           	          UR                  S5      nX1R                  ;  a(  [        S[        U5       SUR                   SU S35      eU$ )z>Validate that content formatter is supported by endpoint type.r   zContent formatter fz8 is not supported by this endpoint. Supported types are z but endpoint is .)r2   re   r"   type)r   r   r   r   s       r$   validate_content_formatter.AzureMLBaseEndpoint.validate_content_formatter  sf    
 #JJ':;$C$CC%d;&7%8 911<1P1P0Q R##4"5Q8 
 r'   aftervaluec                     UR                  S5      (       a  USS n[        U5      nUR                  (       a  UR                  S:X  a  [        S5      eU$ )z'Validate that endpoint url is complete./Nz`endpoint_url` should contain the full invocation URL including `/score` for `endpoint_api_type='dedicated'` or `/completions` or `/models/chat/completions` for `endpoint_api_type='serverless'`)endswithr
   pathr"   )r   r   urls      r$   validate_endpoint_url)AzureMLBaseEndpoint.validate_endpoint_url  sQ     >>##2JEuoxx388s?7  r'   c                    [        UR                  S5      5      nU[        R                  :X  d  U[        R                  :X  a  UR
                  S:X  d  [        S5      eU[        R                  :X  a  UR
                  S;  a  [        S5      eU$ )zBValidate that endpoint api type is compatible with the URL format.r   z/scorezEndpoints of type `dedicated` should follow the format `https://<your-endpoint>.<your_region>.inference.ml.azure.com/score`. If your endpoint URL ends with `/completions` or`/models/chat/completions`,use `endpoint_api_type='serverless'` instead.)z/completionsz/models/chat/completionszEndpoints of type `serverless` should follow the format `https://<your-endpoint>.<your_region>.inference.ml.azure.com/completions` or `https://<your-endpoint>.<your_region>.inference.ml.azure.com/models/chat/completions`)r
   r2   rG   rI   rJ   r   r"   rK   )r   r   r   r   s       r$   validate_endpoint_api_type.AzureMLBaseEndpoint.validate_endpoint_api_type  s    
  

> :;1;;;4===##x/@  1<<<!!)UUm  r'   T)alwaysc                     UR                  S5      nUR                  S5      nUR                  S5      nUR                  S[        5      n[        UUR                  5       UU5      nU$ )z?Validate that api key and python package exists in environment.r   r   r   r   )r2   r@   r   get_secret_value)r   r   r   r   endpoint_keyr   r   r   s           r$   validate_client#AzureMLBaseEndpoint.validate_client  sg     zz.1zz"45 **%67**Y8+))+	
 r'   )(r;   r<   r=   r>   r?   r   rA   rq   rG   rI   r   r   r   r   r   r@   r   rB   r   r   r   r   rg   r	   r   r   model_configr   classmethodr   r   r   rM   r   r   r   r   r   r   rD   rE   r'   r$   r   r   m  s   *L#+ 1G0P0P-PC #8";i;/ OSM #GS"3KK!s! $(L(4.'126L(#d s   $2 "#'+	 $ ^'2# #   3 "#'+	 $8 }T*# t @U  +r'   r   c                       \ rS rSrSr\S\\\4   4S j5       r	\S\4S j5       r
  SS\\   S\\\      S	\\   S
\S\4
S jjrSrg)AzureMLOnlineEndpointi  a  Azure ML Online Endpoint models.

Example:
    .. code-block:: python
        azure_llm = AzureMLOnlineEndpoint(
            endpoint_url="https://<your-endpoint>.<your_region>.inference.ml.azure.com/score",
            endpoint_api_type=AzureMLApiType.dedicated,
            endpoint_api_key="my-api-key",
            timeout=120,
            content_formatter=content_formatter,
        )
r    c                 T    U R                   =(       d    0 n0 SU R                  0ESU0E$ )zGet the identifying parameters.r   rg   )rg   r   )r#   _model_kwargss     r$   _identifying_params)AzureMLOnlineEndpoint._identifying_params  s>     ))/R
 $"6"67
}-
 	
r'   c                     g)zReturn type of llm.azureml_endpointrE   rd   s    r$   	_llm_typeAzureMLOnlineEndpoint._llm_type  s     "r'   Npromptsstopr)   r*   c                 x   U R                   =(       d    0 nUR                  U5        U(       a  X%S'   / nU Hy  nU R                  R                  XuU R                  5      nU R
                  R                  XS9n	U R                  R                  XR                  5      n
UR                  U
/5        M{     [        US9$ )a&  Run the LLM on the given prompts.

Args:
    prompts: 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 = azureml_model.invoke("Tell me a joke.")
r  )r(   r)   )generations)
rg   updater   rk   r   r   r8   ro   appendr   )r#   r  r  r)   r*   r   r
  rR   r   response_payloadr   s              r$   	_generateAzureMLOnlineEndpoint._generate  s    $ ))/RV$$(&!F"44KKt'='=O  $//44$  5   "33KK "8"8N /0  [11r'   rE   )NN)r;   r<   r=   r>   r?   rs   r   rA   r   r  r  r   r	   r   r   r  rD   rE   r'   r$   r   r     s     
WS#X%6 
 
 "3 " " %):>	$2c$2 tCy!$2 67	$2
 $2 
$2 $2r'   r   )/r   urllib.requestr.   r   abcr   enumr   typingr   r   r   r   r	   urllib.parser
    langchain_core.callbacks.managerr   #langchain_core.language_models.llmsr   langchain_core.outputsr   r   langchain_core.utilsr   r   pydanticr   r   r   r   r   r   r@   objectr   rA   rG   rM   ru   r   r   r   r   r   r   r   rE   r'   r$   <module>r     s         5 5 ! E 7 8 L  ,F ,^	S$ 	] ]@'/ ':
. 
 '- ':'0 ':?V#7 ?VD
8 
 I) IX@2G%8 @2r'   