
    hP                        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.y)"    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                   T    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y)AzureMLEndpointClientz AzureML Managed Endpoint client. endpoint_urlendpoint_api_keydeployment_nametimeoutreturnNc                 Z    |r|st        d      || _        || _        || _        || _        y)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        g/var/www/html/eduruby.in/venv/lib/python3.12/site-packages/langchain_community/llms/azureml_endpoint.py__init__zAzureMLEndpointClient.__init__   s<      |6  ) 0.    bodyrun_managerkwargsc                 R   dd| j                   z   d}| j                  dk7  r| j                  |d<   t        j                  j	                  | j
                  ||      }t        j                  j                  ||j                  d| j                              }|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%262F2FG./nn$$T%6%6gF>>))It||< * 
 r%   N)__name__
__module____qualname____doc__DEFAULT_TIMEOUTstrintr$   bytesr	   r   r   r6    r%   r#   r   r      sx    *  "&  	
  
* ;? 67 	
 
r%   r   c                       e Zd ZdZdZdZdZy)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Jr%   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y)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                 n    dddddddd}|j                         D ]  \  }}| j                  ||      }  | 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   sZ     

 2<1A1A1C 	G-O-^^O5EFF	G 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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                      y)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@/GXc]/G	$ c  # #  $ 2T*@%A 2 2 ,B+K+K	
$
$ 
$ )	
$
 

$  ,B+K+K ) 
	 r%   rG   c                   X    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y
)GPT2ContentFormatterzContent handler for GPT2r   c                 $    t         j                  gS r7   r[   r\   s    r#   r]   z(GPT2ContentFormatter.supported_api_types       &0011r%   rK   r^   r_   c                     t         j                  |      }t        j                  dd| dgi|d      }t        j                  |      S )Ninput_stringrN   inputs
parametersrG   rY   jsondumpsr=   encoder"   rK   r^   r_   request_payloads        r#   rb   z+GPT2ContentFormatter.format_request_payload   sM     &??G**)axqM?;*
 zz/**r%   rc   c                     	 t        j                  |      d   d   }t        |      S # t        t        t        f$ r+}t        | j                  j                  |            |d }~ww xY w)Nr   0r_   text	rs   loadsKeyError
IndexError	TypeErrorr!   rJ   formatr   r"   rc   r_   choicees        r#   re   z,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(Nr8   r9   r:   r;   rh   r   rB   r]   r=   r   r?   rb   r   re   r@   r%   r#   rj   rj      sf    "2T*@%A 2 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_formatterc                 L    t         |           t        j                  d       y )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 "s!
 
r%   r   c                   X    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y
)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         j                  |       t        j                  d| dg|d      }t        j                  |      S )NrN   ro   rr   rv   s        r#   rb   z)HFContentFormatter.format_request_payload   sF     	66v>**vha=/*
 zz/**r%   rc   c                     	 t        j                  |      d   d   d   }t        |      S # t        t        t        f$ r+}t        | j                  j                  |            |d }~ww xY w)Nr   ry   generated_textrz   r{   r}   r   s        r#   re   z*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+Nr   r@   r%   r#   r   r      sf    @2T*@%A 2 2
+
+)-
+9O
+	
+'''='	'r%   r   c                   X    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y
)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         j                  |      }t        j                  dd| dgi|d      }t        j                  |      S )Nrn   rN   )
input_datarq   rr   rv   s        r#   rb   z,DollyContentFormatter.format_request_payload  sM     &??G**-!F81?*
 zz/**r%   rc   c                     	 t        j                  |      d   }t        |      S # t        t        t        f$ r+}t        | j                  j                  |            |d }~ww xY w)Nr   rz   r{   r}   r   s        r#   re   z-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%Nr   r@   r%   r#   r   r      sf    42T*@%A 2 2
+
+)-
+9O
+	
+'''='	'r%   r   c                   X    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y
)CustomOpenAIContentFormatterzAContent formatter for models that use the OpenAI like API scheme.r   c                 B    t         j                  t         j                  gS r7   )rB   rC   rE   r\   s    r#   r]   z0CustomOpenAIContentFormatter.supported_api_types  s    &002H2S2STTr%   rK   r^   r_   c                 R   t         j                  |      }|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        j                  |      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",,"++
 
 #jj +,VHA&2#O /:::"jj(F)Kl)KLOhZ'JK  zz/**r%   rc   c           	         |t         j                  t         j                  fv r(	 t        j                  |      d   d   }t        |      S |t         j                  k(  r	 t        j                  |      d   d   }t        |t              st        d      	 t        |d   j                         t        |j!                  d	      |j!                  d
                  S t        d| d      # t
        t        t        f$ r+}t        | j                  j                  |            |d}~ww xY w# t
        t        t        f$ r+}t        | j                  j                  |            |d}~ww xY w)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;  sk    ",,"++
 
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, 6D. ,D+ &D&&D+.E-&E((E-Nr   r@   r%   r#   r   r     sm    KUT*@%A U U++)-+9O+	+2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   c                 L    t         |           t        j                  d       y )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%y)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&
!" "6N$:"
~ %9%'@"%
 ! ';'",,	'
"# 1 	
y r%   field_valuec           	          |j                  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  sh    
 #JJ':;K$C$CC%d;&7%8 911<1P1P0Q R##4"5Q8 
 r%   aftervaluec                     |j                  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  sN     >>##2JEuoxx388s?7  r%   c                    t        |j                  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m  r%   T)alwaysc                     |j                  d      }|j                  d      }|j                  d      }|j                  dt              }t        ||j                         ||      }|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  sg     zz.1zz"45 **%67**Y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  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                       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y)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                 J    | j                   xs 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  s>     ))/R
 $"6"67
}-
 	
r%   c                      y)zReturn type of llm.azureml_endpointr@   r\   s    r#   	_llm_typezAzureMLOnlineEndpoint._llm_type  s     "r%   Npromptsstopr'   r(   c                 t   | j                   xs i }|j                  |       |r||d<   g }|D ]~  }| j                  j                  ||| j                        }| j
                  j                  ||      }	| j                  j                  |	| j                        }
|j                  |
g        t        |      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&! 
	1F"44KKt'='=O  $//44$+  5   "33KK $"8"8N /0
	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 
 
 "3 " " %):>	$2c$2 tCy!$2 67	$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      s         5 5 ! E 7 8 L  ,F ,^	S$ 	] ]@'/ ':
. 
 '- ':'0 ':?V#7 ?VD
8 
 I) IX@2G%8 @2r%   