
    7|hFQ                        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+y)!    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                   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        h/var/www/html/test/engine/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   r4    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)r6   r7   r8   r9   rA   rB   rC   r>   r#   r!   r@   r@   @   s     IHJr#   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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)rI   
escape_mapescape_sequenceescaped_sequences       r!   escape_special_charactersz.ContentFormatterBase.escape_special_charactersy   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.r@   rA   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    rI   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    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@/GXc]/G	$ c  # #  $ 2T*@%A 2 2 ,B+K+K	
$
$ 
$ )	
$
 

$  ,B+K+K ) 
	 r#   rE   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 r5   rY   rZ   s    r!   r[   z(GPT2ContentFormatter.supported_api_types       &0011r#   rI   r\   r]   c                     t         j                  |      }t        j                  dd| dgi|d      }t        j                  |      S )Ninput_stringrL   inputs
parametersrE   rW   jsondumpsr;   encoder    rI   r\   r]   request_payloads        r!   r`   z+GPT2ContentFormatter.format_request_payload   sM     &??G**)axqM?;*
 zz/**r#   ra   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	rq   loadsKeyError
IndexError	TypeErrorr   rH   formatr   r    ra   r]   choicees        r!   rc   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r6   r7   r8   r9   rf   r   r@   r[   r;   r   r=   r`   r   rc   r>   r#   r!   rh   rh      sf    "2T*@%A 2 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_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	r6   r7   r8   r9   r   r   rd   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 r5   rY   rZ   s    r!   r[   z&HFContentFormatter.supported_api_types   rj   r#   rI   r\   r]   c                     t         j                  |       t        j                  d| dg|d      }t        j                  |      S )NrL   rm   rp   rt   s        r!   r`   z)HFContentFormatter.format_request_payload   sF     	66v>**vha=/*
 zz/**r#   ra   c                     	 t        j                  |      d   d   d   }t        |      S # t        t        t        f$ r+}t        | j                  j                  |            |d }~ww xY w)Nr   rw   generated_textrx   ry   r{   r   s        r!   rc   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 r5   rY   rZ   s    r!   r[   z)DollyContentFormatter.supported_api_types   rj   r#   rI   r\   r]   c                     t         j                  |      }t        j                  dd| dgi|d      }t        j                  |      S )Nrl   rL   )
input_dataro   rp   rt   s        r!   r`   z,DollyContentFormatter.format_request_payload   sM     &??G**-!F81?*
 zz/**r#   ra   c                     	 t        j                  |      d   }t        |      S # t        t        t        f$ r+}t        | j                  j                  |            |d }~ww xY w)Nr   rx   ry   r{   r   s        r!   rc   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 r5   )r@   rA   rC   rZ   s    r!   r[   z0CustomOpenAIContentFormatter.supported_api_types  s    &002H2S2STTr#   rI   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   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",,"++
 
 #jj +,VHA&2#O /:::"jj(F)Kl)KLOhZ'JK  zz/**r#   ra   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   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_payload3  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__\  s    	
r#   r   r   r   s   @r!   r   r   U  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$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   r@   rA   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 .)r.   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#   c                 j    |j                  d      r|dd }|j                  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 `/models/chat/completions` for `endpoint_api_type='serverless'`)endswithr   )r   r   s     r!   validate_endpoint_urlz)AzureMLBaseEndpoint.validate_endpoint_url  sG     $%cr*K 897  r#   c                 ,   |j                  d      }|t        j                  k(  s|t        j                  k(  r|j	                  d      st        d      |t        j                  k(  r-|j	                  d      s|j	                  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`/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.   r@   rA   rB   r   r   rC   )r   r   r   r   s       r!   validate_endpoint_api_typez.AzureMLBaseEndpoint.validate_endpoint_api_type  s    
 zz.1 5???"8"A"AA ))(3@  0;;;!!.1$$%?@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   )r.   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#   )%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   e  s   *L#+ 1G0P0P-PC #8";i;/ OSM #GS"3KK!s! $(L(4.'126L(#d s   $2 "#'+	 $ ~    "#'+	 $> }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>   rZ   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   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&! 
	1F"44KKt'='=O  $//44$+  5   "33KK $"8"8N /0
	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 
 
 "3 " " %):>	$2c$2 tCy!$2 67	$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 E 7 8 L Q Q,F ,^	S$ 	] ]@'/ ':
. 
 '- ':'0 ':?V#7 ?VD
8 
 J) JZ@2G%8 @2r#   