
    ~
iP                        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 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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)urlparse)CallbackManagerForLLMRun)BaseLLM)
Generation	LLMResult)convert_to_secret_strget_from_dict_or_env)	BaseModel
ConfigDict	SecretStrfield_validator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        C:\Users\Dell Inspiron 16\Desktop\tws\AgrotaPowerBi\back-agrota-powerbi\mcp-client-agrota\venv\Lib\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   r6    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)r8   r9   r:   r;   rC   rD   rE   r@   r%   r#   rB   rB   H   s)          IHJJJr%   rB   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)rK   
escape_mapescape_sequenceescaped_sequences       r#   escape_special_charactersz.ContentFormatterBase.escape_special_characters   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.rB   rC   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"   rK   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"   rc   r_   s      r#   format_response_payloadz,ContentFormatterBase.format_response_payload   s      r%   N)r8   r9   r:   r;   rH   r	   r=   __annotations__rI   rJ   staticmethodrY   propertyr   rB   r]   rC   r   r   rb   r   r?   r   re   r@   r%   r#   rG   rG   T   s]         8 #5L(3-444@/GXc]///G	$ c    # #    \$ 2T*@%A 2 2 2 X2 ,B+K	
$ 
$
$ 
$ )	
$
 

$ 
$ 
$ 
$  ,B+K  ) 
	   ^  r%   rG   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 r7   r[   r\   s    r#   r]   z(GPT2ContentFormatter.supported_api_types       &011r%   rK   r^   r_   c                     t                               |          }t          j        dd| dgi|d          }t                              |          S )Ninput_stringrN   inputs
parametersrG   rY   jsondumpsr=   encoder"   rK   r^   r_   request_payloads        r#   rb   z+GPT2ContentFormatter.format_request_payload   s[     &??GG*)MMMM?;* 
 
 zz/***r%   rc   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	rs   loadsKeyError
IndexError	TypeErrorr!   rJ   formatr   r"   rc   r_   choicees        r#   re   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r8   r9   r:   r;   rh   r   rB   r]   r=   r   r?   rb   r   re   r@   r%   r#   rj   rj      s        ""2T*@%A 2 2 2 X2
+
+)-
+9O
+	
+ 
+ 
+ 
+'''='	' ' ' ' ' 'r%   rj   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	r8   r9   r:   r;   r   r   rf   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 r7   r[   r\   s    r#   r]   z&HFContentFormatter.supported_api_types   rl   r%   rK   r^   r_   c                     t                               |           t          j        d| dg|d          }t                              |          S )NrN   ro   rr   rv   s        r#   rb   z)HFContentFormatter.format_request_payload   sW     	66v>>>*(v===/* 
 
 zz/***r%   rc   c                    	 t          j        |          d         d         d         }nH# t          t          t          f$ r.}t          | j                            |                    |d }~ww xY wt          |          S )Nr   ry   generated_textrz   r{   r}   r   s        r#   re   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 r7   r[   r\   s    r#   r]   z)DollyContentFormatter.supported_api_types  rl   r%   rK   r^   r_   c                     t                               |          }t          j        dd| dgi|d          }t                              |          S )Nrn   rN   )
input_datarq   rr   rv   s        r#   rb   z,DollyContentFormatter.format_request_payload  s[     &??GG*-F?* 
 
 zz/***r%   rc   c                     	 t          j        |          d         }nH# t          t          t          f$ r.}t          | j                            |                    |d }~ww xY wt          |          S )Nr   rz   r{   r}   r   s        r#   re   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 r7   )rB   rC   rE   r\   s    r#   r]   z0CustomOpenAIContentFormatter.supported_api_types  s    &02H2STTr%   rK   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   rN   )rn   rq   rK   `api_type` # is not supported by this formatter)rG   rY   rB   rC   rD   rs   rt   rE   r!   r=   ru   rv   s        r#   rb   z3CustomOpenAIContentFormatter.format_request_payload"  s     &??GG","+
 
 
 #j )6V&2# # OO /:::"j(F)Kl)KLLOOKhKKK   zz/***r%   rc   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   ry   rz   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   )rB   rC   rD   rs   r~   r   r   r   r!   rJ   r   r   rE   
isinstancedictstripr0   r   s        r#   re   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__d  s<    	
 	
 	
 	
 	
r%   r   r   r   s   @r#   r   r   ]  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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   rB   rC   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 .)r0   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%   aftervaluec                     |                     d          r
|dd         }t          |          }|j        r|j        dk    rt          d          |S )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_urlz)AzureMLBaseEndpoint.validate_endpoint_url  sb     >># 	#2#JEuoox 	38s??7   r%   c                    t          |                    d                    }|t          j        k    s|t          j        k    r|j        dk    st          d          |t          j        k    r|j        dvr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`/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
   r0   rB   rC   rD   r   r!   rE   )r   r   r   r   s       r#   validate_endpoint_api_typez.AzureMLBaseEndpoint.validate_endpoint_api_type  s    
  

> : :;;1;;;4===#x//@   1<<<!)UUUm   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   )r0   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%   )&r8   r9   r:   r;   r   r=   rf   rB   rC   r   r   r   r   r   r<   r   r>   r   r   r   r   r^   r	   r   r   model_configr   classmethodr   r   r   rG   r   r   r   r   r   r   r@   r%   r#   r   r   m  sZ        **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"##'+	   $# _^'222# #    [ 32 Y"##'+	   $#8 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@   r\   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   rb   r   r   r6   re   appendr   )r"   r   r   r'   r(   r   r   rK   rw   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)r8   r9   r:   r;   rh   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   )/rs   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   r=   rB   rG   rj   r   r   r   r   r   r   r   r@   r%   r#   <module>r      s5                     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                , , , , ,F , , ,^	 	 	 	 	S$ 	 	 	] ] ] ] ] ] ] ]@' ' ' ' '/ ' ' ':
 
 
 
 
. 
 
 
 ' ' ' ' '- ' ' ':' ' ' ' '0 ' ' ':?V ?V ?V ?V ?V#7 ?V ?V ?VD
 
 
 
 
8 
 
 
 I I I I I) I I IX@2 @2 @2 @2 @2G%8 @2 @2 @2 @2 @2r%   