
    NgP                        d dl Z d dlZd dlZd dlmZ d dlmZ d dlm	Z	m
Z
mZmZmZ d dlmZ d dlmZ d dlmZmZ d dlmZmZ d d	lmZmZmZmZmZ d
Z G d de          Z  G d de!e          Z" G d d          Z# G d de#          Z$ G d de$          Z% G d de#          Z& G d de#          Z' G d de#          Z( G d de(          Z) G d de          Z* G d d ee*          Z+dS )!    N)abstractmethod)Enum)AnyDictListMappingOptional)CallbackManagerForLLMRun)BaseLLM)
Generation	LLMResult)convert_to_secret_strget_from_dict_or_env)	BaseModel
ConfigDict	SecretStrmodel_validator	validator2   c                   \    e Zd ZdZdefdededededdf
d	Z	 dd
ede	e
         dedefdZdS )AzureMLEndpointClientz AzureML Managed Endpoint client. endpoint_urlendpoint_api_keydeployment_nametimeoutreturnNc                 d    |r|st          d          || _        || _        || _        || _        dS )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        e/var/www/html/ai-engine/env/lib/python3.11/site-packages/langchain_community/llms/azureml_endpoint.py__init__zAzureMLEndpointClient.__init__   sO       	| 	6   ) 0.    bodyrun_managerkwargsc                 2   dd| j         z   d}| j        dk    r
| j        |d<   t          j                            | j        ||          }t          j                            ||                    d| j                            }|	                                }|S )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!   callzAzureMLEndpointClient.call&   s     /'$*??
 
 2%%262FG./n$$T%6gFF>))It|<< * 
 
 r#   N)__name__
__module____qualname____doc__DEFAULT_TIMEOUTstrintr"   bytesr	   r
   r   r4    r#   r!   r   r      s        **  "&   	
  
   * ;?  67 	
 
     r#   r   c                       e Zd ZdZdZdZdZdS )AzureMLEndpointApiTypezAzure 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
serverlessN)r6   r7   r8   r9   rA   rB   rC   r>   r#   r!   r@   r@   @   s)          IHJJJr#   r@   c            	           e Zd ZU dZ	 dZee         ed<   	 dZee         ed<   	 dZ	eed<   e
dedefd	            Zedee         fd
            Zej        fdedededefdZeej        fdededefd            ZdS )ContentFormatterBasezZTransform 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                 z    dddddddd}|                                 D ]\  }}|                     ||          } | S )	z*Escapes any special characters in `prompt`z\\z\"z\bz\fz\nz\rz\t)\"
	)itemsreplace)rI   
escape_mapescape_sequenceescaped_sequences       r!   escape_special_charactersz.ContentFormatterBase.escape_special_charactersy   se     
 

 2<1A1A1C1C 	G 	G-O-^^O5EFFFFr#   c                     t           j        gS )zSupported APIs for the given formatter. Azure ML supports
        deploying models using different hosting methods. Each method may have
        a different API structure.r@   rA   r    s    r!   supported_api_typesz(ContentFormatterBase.supported_api_types   s     '011r#   model_kwargsapi_typec                     t                      )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    rI   r\   r]   s       r!   format_request_payloadz+ContentFormatterBase.format_request_payload   s     "###r#   outputc                     dS )zFormats the response body according to the output
        schema of the model. Returns the data type that is
        received from the response.
        Nr>   )r    ra   r]   s      r!   format_response_payloadz,ContentFormatterBase.format_response_payload   s      r#   N)r6   r7   r8   r9   rF   r	   r;   __annotations__rG   rH   staticmethodrW   propertyr   r@   r[   rA   r   r   r`   r   r=   r   rc   r>   r#   r!   rE   rE   L   s]         8 #5L(3-444@/GXc]///G	$ c    # #    \$ 2T*@%A 2 2 2 X2 ,B+K	
$ 
$
$ 
$ )	
$
 

$ 
$ 
$ 
$  ,B+K  ) 
	   ^  r#   rE   c                   f    e Zd ZdZedee         fd            Zdede	dede
fdZde
dedefd	Zd
S )GPT2ContentFormatterzContent handler for GPT2r   c                     t           j        gS r5   rY   rZ   s    r!   r[   z(GPT2ContentFormatter.supported_api_types       &011r#   rI   r\   r]   c                     t                               |          }t          j        dd| dgi|d          }t                              |          S )Ninput_stringrL   inputs
parametersrE   rW   jsondumpsr;   encoder    rI   r\   r]   request_payloads        r!   r`   z+GPT2ContentFormatter.format_request_payload   sV     &??GG*&V8UU
 
 zz/***r#   ra   c                     	 t          j        |          d         d         }nH# t          t          t          f$ r.}t          | j                            |                    |d }~ww xY wt          |          S )Nr   0r]   text	rq   loadsKeyError
IndexError	TypeErrorr   rH   formatr   r    ra   r]   choicees        r!   rc   z,GPT2ContentFormatter.format_response_payload   s    	UZ''*3/FF*i0 	U 	U 	UT29989LLMMSTT	Uv&&&&s    # A()A##A(Nr6   r7   r8   r9   rf   r   r@   r[   r;   r   r=   r`   r   rc   r>   r#   r!   rh   rh      s        ""2T*@%A 2 2 2 X2++)-+9O+	+ + + +'''='	' ' ' ' ' 'r#   rh   c                   4     e Zd ZU dZdZeed<   d fdZ xZS )OSSContentFormatterz`Deprecated: Kept for backwards compatibility

    Content handler for LLMs from the OSS catalog.Ncontent_formatterr   c                 p    t                                                       t          j        d           d S )Nz`OSSContentFormatter` will be deprecated in the future. 
                      Please use `GPT2ContentFormatter` instead.  
                      superr"   warningswarnr    	__class__s    r!   r"   zOSSContentFormatter.__init__   s<    	
 	
 	
 	
 	
r#   r   N	r6   r7   r8   r9   r   r   rd   r"   __classcell__r   s   @r!   r   r      s\         6 6 "s!!!
 
 
 
 
 
 
 
 
 
r#   r   c                   f    e Zd ZdZedee         fd            Zdede	dede
fdZde
dedefd	Zd
S )HFContentFormatterz6Content handler for LLMs from the HuggingFace catalog.r   c                     t           j        gS r5   rY   rZ   s    r!   r[   z&HFContentFormatter.supported_api_types   rj   r#   rI   r\   r]   c                     t                               |           t          j        d| dg|d          }t                              |          S )NrL   rm   rp   rt   s        r!   r`   z)HFContentFormatter.format_request_payload   sR     	66v>>>*%FlCC
 
 zz/***r#   ra   c                    	 t          j        |          d         d         d         }nH# t          t          t          f$ r.}t          | j                            |                    |d }~ww xY wt          |          S )Nr   rw   generated_textrx   ry   r{   r   s        r!   rc   z*HFContentFormatter.format_response_payload   s    	UZ''*3/0@AFF*i0 	U 	U 	UT29989LLMMSTT	Uv&&&&s   &) A. )A))A.Nr   r>   r#   r!   r   r      s        @@2T*@%A 2 2 2 X2++)-+9O+	+ + + +'''='	' ' ' ' ' 'r#   r   c                   f    e Zd ZdZedee         fd            Zdede	dede
fdZde
dedefd	Zd
S )DollyContentFormatterz*Content handler for the Dolly-v2-12b modelr   c                     t           j        gS r5   rY   rZ   s    r!   r[   z)DollyContentFormatter.supported_api_types   rj   r#   rI   r\   r]   c                     t                               |          }t          j        dd| dgi|d          }t                              |          S )Nrl   rL   )
input_dataro   rp   rt   s        r!   r`   z,DollyContentFormatter.format_request_payload   s[     &??GG*-F?* 
 
 zz/***r#   ra   c                     	 t          j        |          d         }nH# t          t          t          f$ r.}t          | j                            |                    |d }~ww xY wt          |          S )Nr   rx   ry   r{   r   s        r!   rc   z-DollyContentFormatter.format_response_payload  s~    	UZ''*FF*i0 	U 	U 	UT29989LLMMSTT	Uv&&&&s    A")AA"Nr   r>   r#   r!   r   r      s        442T*@%A 2 2 2 X2
+
+)-
+9O
+	
+ 
+ 
+ 
+'''='	' ' ' ' ' 'r#   r   c                   f    e Zd ZdZedee         fd            Zdede	dede
fdZde
dedefd	Zd
S )CustomOpenAIContentFormatterzAContent formatter for models that use the OpenAI like API scheme.r   c                 2    t           j        t           j        gS r5   )r@   rA   rC   rZ   s    r!   r[   z0CustomOpenAIContentFormatter.supported_api_types  s    &02H2STTr#   rI   r\   r]   c                 T   t                               |          }|t          j        t          j        fv rt          j        dd| dg|di          }n<|t          j        k    rt          j        d|i|          }nt          d| d          t          
                    |          S )z/Formats the request according to the chosen apir   rL   )rl   ro   rI   `api_type` # is not supported by this formatter)rE   rW   r@   rA   rB   rq   rr   rC   r   r;   rs   rt   s        r!   r`   z3CustomOpenAIContentFormatter.format_request_payload  s     &??GG","+
 
 
 #j )6V&2# # OO /:::"j(F)Kl)KLLOOKhKKK   zz/***r#   ra   c           	      H   |t           j        t           j        fv rz	 t          j        |          d         d         }nH# t
          t          t          f$ r.}t          | j	        
                    |                    |d}~ww xY wt          |          S |t           j        k    r	 t          j        |          d         d         }t          |t                    st          d          nH# t
          t          t          f$ r.}t          | j	        
                    |                    |d}~ww xY wt          |d                                         t          |                    d	          |                    d
                              S t          d| d          )zFormats responser   rw   rx   Nry   choicesziEndpoint response is not well formed for a chat model. Expected `dict` but `{type(choice)}` was received.rz   finish_reasonlogprobs)r   r   )rz   generation_infor   r   )r@   rA   rB   rq   r|   r}   r~   r   r   rH   r   r   rC   
isinstancedictstripr.   r   s        r!   rc   z4CustomOpenAIContentFormatter.format_response_payload-  s    ","+
 
 
YF++A.s3j)4 Y Y Y !6!=!=x!=!P!PQQWXXY6****-888	YF++I6q9!&$// #$   j)4 Y Y Y !6!=!=x!=!P!PQQWXXYF^))++ $"(**_"="=#ZZ
33! ! !    TxTTTUUUs.    = B)A==B&AC+ +D0)D++D0Nr   r>   r#   r!   r   r     s        KKUT*@%A U U U XU++)-+9O+	+ + + +2VV'=V	V V V V V Vr#   r   c                   4     e Zd ZU dZdZeed<   d fdZ xZS )LlamaContentFormatterzNDeprecated: Kept for backwards compatibility

    Content formatter for Llama.Nr   r   c                 p    t                                                       t          j        d           d S )Nz`LlamaContentFormatter` will be deprecated in the future. 
                Please use `CustomOpenAIContentFormatter` instead.  
            r   r   s    r!   r"   zLlamaContentFormatter.__init__V  s<    	
 	
 	
 	
 	
r#   r   r   r   s   @r!   r   r   O  s\         $ $ "s!!!
 
 
 
 
 
 
 
 
 
r#   r   c                      e Zd ZU dZdZeed<   	 ej        Z	eed<   	  e
d          Zeed<   	 dZeed<   	 eZeed<   	 dZeed	<   d
Zeed<   dZeed<   	 dZee         ed<   	  ed          Z ed          ededefd                        Z ed          dededefd            Z  ed          dedefd            Z! ed          dededefd            Z" ed	d          dedede#fd            Z$dS )AzureMLBaseEndpointz Azure ML Online Endpoint models.r   r   endpoint_api_typer   r   r   Nhttp_client   max_retriesr   r\   r>   )protected_namespacesbefore)modevaluesr   c                 &   t          t          |dd                    |d<   t          |dd          |d<   t          |ddd          |d<   t          |dd	t          j                  |d<   t          |d
dt	          t
                              |d
<   |S )Nr   AZUREML_ENDPOINT_API_KEYr   AZUREML_ENDPOINT_URLr   AZUREML_DEPLOYMENT_NAMEr   r   AZUREML_ENDPOINT_API_TYPEr   AZUREML_TIMEOUT)r   r   r@   rA   r;   r:   )clsr   s     r!   validate_environz$AzureMLBaseEndpoint.validate_environ  s     &; );=WXX&
 &
!" "6N$:"
 "
~ %9%'@"%
 %
 ! ';'",	'
 '
"# 1  	
 
y r#   field_valuec           	          |                     d          }||j        vr+t          dt          |           d|j         d| d          |S )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 .)r.   r[   r   type)r   r   r   r   s       r!   validate_content_formatterz.AzureMLBaseEndpoint.validate_content_formatter  sz    
 #JJ':;;K$CCC8d;&7&7 8 81<1P8 8#48 8 8  
 r#   c                     |                     d          r
|dd         }|                     d          rt          d          |S )z'Validate that endpoint url is complete./Nzinference.ml.azure.comz`endpoint_url` should contain the full invocation URL including `/score` for `endpoint_api_type='dedicated'` or `/completions` or `/chat/completions` for `endpoint_api_type='serverless'`)endswithr   )r   r   s     r!   validate_endpoint_urlz)AzureMLBaseEndpoint.validate_endpoint_url  s_     $$ 	+%crc*K 899 	N  
 r#   c                 J   |                     d          }|t          j        k    s|t          j        k    r$|                    d          st          d          |t          j        k    r9|                    d          s$|                    d          st          d          |S )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`/chat/completions`, use `endpoint_api_type='serverless'` instead.z/completionsz/chat/completionszEndpoints of type `serverless` should follow the format `https://<your-endpoint>.<your_region>.inference.ml.azure.com/chat/completions` or `https://<your-endpoint>.<your_region>.inference.ml.azure.com/chat/completions`)r.   r@   rA   rB   r   r   rC   )r   r   r   r   s       r!   validate_endpoint_api_typez.AzureMLBaseEndpoint.validate_endpoint_api_type  s    
 zz.11 5???"8"AAA ))(33 B U   0;;;!!.11 <$$%899 < f   r#   T)alwaysc                    |                     d          }|                     d          }|                     d          }|                     dt                    }t          ||                                ||          }|S )z?Validate that api key and python package exists in environment.r   r   r   r   )r.   r:   r   get_secret_value)r   r   r   r   endpoint_keyr   r   r   s           r!   validate_clientz#AzureMLBaseEndpoint.validate_client  s{     zz.11zz"455 **%677**Y88+))++	
 
 r#   )%r6   r7   r8   r9   r   r;   rd   r@   rA   r   r   r   r   r   r:   r   r<   r   r   r   r   r\   r	   r   r   model_configr   classmethodr   r   r   rE   r   r   r   r   r   r>   r#   r!   r   r   _  sK        **L#+ 1G0P-PPPC #8"7";";i;;;/ OSM #GS"""3KK!s!!! $(L(4.'''1:2666L_(###d s    [ $#2 Y"##'+	   $# Y~
 
 
 
 
 
 Y"##'+	   $#< Y}T***# t @U    +*  r#   r   c                       e Zd ZdZedeeef         fd            Zedefd            Z		 	 dde
e         dee
e                  dee         d	edef
d
ZdS )AzureMLOnlineEndpointa  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                 2    | j         pi }i d| j        id|iS )zGet the identifying parameters.r   r\   )r\   r   )r    _model_kwargss     r!   _identifying_paramsz)AzureMLOnlineEndpoint._identifying_params  s8     )/R
 $"67
}-
 	
r#   c                     dS )zReturn type of llm.azureml_endpointr>   rZ   s    r!   	_llm_typezAzureMLOnlineEndpoint._llm_type  s
     "!r#   Npromptsstopr%   r&   c                 `   | j         pi }|                    |           |r||d<   g }|D ]u}| j                            ||| j                  }| j                            ||          }	| j                            |	| j                  }
|                    |
g           vt          |          S )an  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)
r\   updater   r`   r   r   r4   rc   appendr   )r    r   r   r%   r&   r   r   rI   ru   response_payloadr   s              r!   	_generatezAzureMLOnlineEndpoint._generate  s    $ )/RV$$$ 	)$(M&! 
	1 
	1F"4KKt'= O  $/44$+  5     "3KK $"8 N /0000[1111r#   )NN)r6   r7   r8   r9   rf   r   r;   r   r   r   r   r	   r
   r   r   r>   r#   r!   r   r     s          
WS#X%6 
 
 
 X
 "3 " " " X" %):>	$2 $2c$2 tCy!$2 67	$2
 $2 
$2 $2 $2 $2 $2 $2r#   r   ),rq   urllib.requestr*   r   abcr   enumr   typingr   r   r   r   r	    langchain_core.callbacks.managerr
   #langchain_core.language_models.llmsr   langchain_core.outputsr   r   langchain_core.utilsr   r   pydanticr   r   r   r   r   r:   objectr   r;   r@   rE   rh   r   r   r   r   r   r   r   r>   r#   r!   <module>r      s                     5 5 5 5 5 5 5 5 5 5 5 5 5 5 E E E E E E 7 7 7 7 7 7 8 8 8 8 8 8 8 8 L L L L L L L L Q Q Q Q Q Q Q Q Q Q Q Q Q Q, , , , ,F , , ,^	 	 	 	 	S$ 	 	 	] ] ] ] ] ] ] ]@' ' ' ' '/ ' ' '4
 
 
 
 
. 
 
 
 ' ' ' ' '- ' ' '4' ' ' ' '0 ' ' ':?V ?V ?V ?V ?V#7 ?V ?V ?VD
 
 
 
 
8 
 
 
 H H H H H) H H HV@2 @2 @2 @2 @2G%8 @2 @2 @2 @2 @2r#   