§
    æ˜ºh(  ã                   óZ  — d dl Z d dlZd dl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  G d„ d	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e¦  «        ZdS )é    N)Úabstractmethod)ÚAnyÚDictÚListÚMappingÚOptional)ÚCallbackManagerForLLMRun)ÚLLM)Ú	BaseModelÚ	validator)Úget_from_dict_or_envc            	       óB   — e Zd ZdZ	 ddedededdfd„Zd	ed
edefd„ZdS )ÚAzureMLEndpointClientz AzureML Managed Endpoint client.Ú Úendpoint_urlÚendpoint_api_keyÚdeployment_nameÚreturnNc                 óV   — |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   )Úselfr   r   r   s       úq/var/www/html/services/rag-service/venv/lib/python3.11/site-packages/langchain_community/llms/azureml_endpoint.pyÚ__init__zAzureMLEndpointClient.__init__   sJ   € ð  ð 	 |ð 	Ýð6ñô ð ð )ˆÔØ 0ˆÔØ.ˆÔÐÐó    ÚbodyÚkwargsc                 ó(  — dd| j         z   dœ}| j        dk    r
| j        |d<   t          j                             | j        ||¦  «        }t          j                             ||                     dd¦  «        ¬¦  «        }|                     ¦   «         }|S )	zcall.úapplication/jsonzBearer )zContent-TypeÚAuthorizationr   zazureml-model-deploymentÚtimeouté2   )r    )	r   r   ÚurllibÚrequestÚRequestr   ÚurlopenÚgetÚread)r   r   r   ÚheadersÚreqÚresponseÚresults          r   ÚcallzAzureMLEndpointClient.call   s’   € ð /Ø'¨$Ô*?Ñ?ð
ð 
ˆð Ô 2Ò%Ð%Ø26Ô2FˆGÐ.Ñ/åŒn×$Ò$ TÔ%6¸¸gÑFÔFˆÝ”>×)Ò)¨#°v·z²zÀ)ÈRÑ7PÔ7PÐ)ÑQÔQˆØ—’‘”ˆØˆr   )r   )	Ú__name__Ú
__module__Ú__qualname__Ú__doc__Ústrr   Úbytesr   r,   © r   r   r   r      sƒ   € € € € € Ø*Ð*ð PRð/ð /Øð/Ø36ð/ØILð/à	ð/ð /ð /ð /ð˜ð ¨#ð °%ð ð ð ð ð ð r   r   c                   ó²   — e Zd ZU dZ	 dZee         ed<   	 dZee         ed<   	 e	dedefd„¦   «         Z
edededefd	„¦   «         Z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ÚacceptsÚ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)r8   Ú
escape_mapÚescape_sequenceÚescaped_sequences       r   Úescape_special_charactersz.ContentFormatterBase.escape_special_charactersT   se   € ð ØØØØØØð
ð 
ˆ
ð 2<×1AÒ1AÑ1CÔ1Cð 	Gð 	GÑ-ˆOÐ-Ø—^’^ OÐ5EÑFÔFˆFˆFàˆr   Úmodel_kwargsc                 ó   — dS )zÄFormats 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.
        Nr3   )r   r8   rG   s      r   Úformat_request_payloadz+ContentFormatterBase.format_request_payloadg   ó   € € € r   Úoutputc                 ó   — dS )z™Formats the response body according to the output
        schema of the model. Returns the data type that is
        received from the response.
        Nr3   ©r   rK   s     r   Úformat_response_payloadz,ContentFormatterBase.format_response_payloadn   rJ   r   N)r-   r.   r/   r0   r6   r   r1   Ú__annotations__r7   ÚstaticmethodrF   r   r   r2   rI   rN   r3   r   r   r5   r5   0   sä   € € € € € € ðð ðð2 #5€L�(˜3”-Ð4Ð4Ñ4Ø@à/€GˆX�cŒ]Ð/Ð/Ñ/ØGàð¨#ð °#ð ð ð ñ „\ðð$ ð¨Sð Àð Èð ð ð ñ „^ðð ð¨eð ¸ð ð ð ñ „^ðð ð r   r5   c                   ó6   — e Zd ZdZdededefd„Zdedefd„ZdS )	ÚGPT2ContentFormatterzContent handler for GPT2r8   rG   r   c                 ó¦   — t                                |¦  «        }t          j        dd|› d�gi|dœ¦  «        }t                               |¦  «        S )NÚinput_stringr;   ©ÚinputsÚ
parameters©r5   rF   ÚjsonÚdumpsr1   Úencode©r   r8   rG   Úrequest_payloads       r   rI   z+GPT2ContentFormatter.format_request_payloady   sT   € Ý%×?Ò?ÀÑGÔGˆÝœ*Ø&¨¨V¨¨¨¨Ð8ÈÐUÐUñ
ô 
ˆõ �zŠz˜/Ñ*Ô*Ð*r   rK   c                 óB   — t          j        |¦  «        d         d         S )Nr   Ú0©rY   ÚloadsrM   s     r   rN   z,GPT2ContentFormatter.format_response_payload€   s   € ÝŒz˜&Ñ!Ô! !Ô$ SÔ)Ð)r   N©	r-   r.   r/   r0   r1   r   r2   rI   rN   r3   r   r   rR   rR   v   se   € € € € € Ø"Ð"ð+¨Sð +Àð +Èð +ð +ð +ð +ð*¨eð *¸ð *ð *ð *ð *ð *ð *r   rR   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)	r-   r.   r/   r0   re   r   rO   r   Ú__classcell__)rj   s   @r   rd   rd   „   s\   ø€ € € € € € ð6ð 6ð "Ð�sÐ!Ð!Ñ!ð
ð 
ð 
ð 
ð 
ð 
ð 
ð 
ð 
ð 
r   rd   c                   ó6   — e Zd ZdZdededefd„Zdedefd„ZdS )	ÚHFContentFormatterz6Content handler for LLMs from the HuggingFace catalog.r8   rG   r   c                 ó¢   — t                                |¦  «         t          j        d|› d�g|dœ¦  «        }t                               |¦  «        S )Nr;   rU   rX   r\   s       r   rI   z)HFContentFormatter.format_request_payload—   sP   € Ý×6Ò6°vÑ>Ô>Ð>Ýœ*Ø%˜F˜˜˜�°lÐCÐCñ
ô 
ˆõ �zŠz˜/Ñ*Ô*Ð*r   rK   c                 óB   — t          j        |¦  «        d         d         S )Nr   Úgenerated_textr`   rM   s     r   rN   z*HFContentFormatter.format_response_payloadž   s   € ÝŒz˜&Ñ!Ô! !Ô$Ð%5Ô6Ð6r   Nrb   r3   r   r   rm   rm   ”   se   € € € € € Ø@Ð@ð+¨Sð +Àð +Èð +ð +ð +ð +ð7¨eð 7¸ð 7ð 7ð 7ð 7ð 7ð 7r   rm   c                   ó6   — e Zd ZdZdededefd„Zdedefd„ZdS )	ÚDollyContentFormatterz*Content handler for the Dolly-v2-12b modelr8   rG   r   c                 ó¦   — t                                |¦  «        }t          j        dd|› d�gi|dœ¦  «        }t                               |¦  «        S )NrT   r;   )Ú
input_datarW   rX   r\   s       r   rI   z,DollyContentFormatter.format_request_payload¥   sY   € Ý%×?Ò?ÀÑGÔGˆÝœ*à-°°F°°°¨Ð?Ø*ðð ñ
ô 
ˆõ �zŠz˜/Ñ*Ô*Ð*r   rK   c                 ó6   — t          j        |¦  «        d         S )Nr   r`   rM   s     r   rN   z-DollyContentFormatter.format_response_payload¯   s   € ÝŒz˜&Ñ!Ô! !Ô$Ð$r   Nrb   r3   r   r   rr   rr   ¢   se   € € € € € Ø4Ð4ð+¨Sð +Àð +Èð +ð +ð +ð +ð%¨eð %¸ð %ð %ð %ð %ð %ð %r   rr   c                   ó6   — e Zd ZdZdededefd„Zdedefd„ZdS )	ÚLlamaContentFormatterzContent formatter for LLaMar8   rG   r   c                 ó¦   — t                                |¦  «        }t          j        dd|› d�g|dœi¦  «        }t                               |¦  «        S )z/Formats the request according to the chosen apirt   r;   )rT   rW   rX   r\   s       r   rI   z,LlamaContentFormatter.format_request_payload¶   s\   € å%×?Ò?ÀÑGÔGˆÝœ*àØ%2¨ ] ] ] OØ".ðð ðñ
ô 
ˆõ �zŠz˜/Ñ*Ô*Ð*r   rK   c                 óB   — t          j        |¦  «        d         d         S )zFormats responser   r_   r`   rM   s     r   rN   z-LlamaContentFormatter.format_response_payloadÃ   s   € åŒz˜&Ñ!Ô! !Ô$ SÔ)Ð)r   Nrb   r3   r   r   rw   rw   ³   se   € € € € € Ø%Ð%ð+¨Sð +Àð +Èð +ð +ð +ð +ð*¨eð *¸ð *ð *ð *ð *ð *ð *r   rw   c                   óX  — e Zd ZU dZdZeed<   	 d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d
d
¬¦  «        ede
dedefd„¦   «         ¦   «         Zedeee
f         fd„¦   «         Zedefd„¦   «         Z	 	 ddedeee                  dee         de
def
d„ZdS )ÚAzureMLOnlineEndpointaW  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_key="my-api-key",
                content_formatter=content_formatter,
            )
    r   r   r   r   NÚhttp_clientre   rG   T)ÚalwaysÚallow_reuseÚfield_valueÚvaluesr   c                 ó�   — t          |dd¦  «        }t          |dd¦  «        }t          |ddd¦  «        }t          |||¦  «        }|S )z?Validate that api key and python package exists in environment.r   ÚAZUREML_ENDPOINT_API_KEYr   ÚAZUREML_ENDPOINT_URLr   ÚAZUREML_DEPLOYMENT_NAMEr   )r   r   )Úclsr   r€   Úendpoint_keyr   r   r|   s          r   Úvalidate_clientz%AzureMLOnlineEndpoint.validate_clientë   sj   € õ ,ØÐ&Ð(Bñ
ô 
ˆõ ,Ø�NÐ$:ñ
ô 
ˆõ /ØÐ%Ð'@À"ñ
ô 
ˆõ ,¨L¸,ÈÑXÔXˆØÐr   c                 ó2   — | j         pi }i d| j        i¥d|i¥S )zGet the identifying parameters.r   rG   )rG   r   )r   Ú_model_kwargss     r   Ú_identifying_paramsz)AzureMLOnlineEndpoint._identifying_paramsû   s8   € ð Ô)Ð/¨Rˆð
Ø  $Ô"6Ð7ð
à˜}Ð-ð
ð 	
r   c                 ó   — dS )zReturn type of llm.Úazureml_endpointr3   )r   s    r   Ú	_llm_typezAzureMLOnlineEndpoint._llm_type  s
   € ð "Ð!r   r8   ÚstopÚrun_managerr   c                 ó¨   — | j         pi }| j                             ||¦  «        } | j        j        |fi |¤Ž}| j                             |¦  «        }|S )as  Call out to an AzureML Managed Online endpoint.
        Args:
            prompt: 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("Tell me a joke.")
        )rG   re   rI   r|   r,   rN   )	r   r8   rŽ   r�   r   r‰   r]   Úresponse_payloadrp   s	            r   Ú_callzAzureMLOnlineEndpoint._call	  sq   € ð" Ô)Ð/¨RˆàÔ0×GÒGØ�Mñ
ô 
ˆð 1˜4Ô+Ô0°ÐKÐKÀFÐKÐKÐØÔ/×GÒGØñ
ô 
ˆð Ðr   )NN)r-   r.   r/   r0   r   r1   rO   r   r   r|   r   re   rG   r   Údictr   Úclassmethodr   r   r‡   Úpropertyr   rŠ   r�   r   r	   r’   r3   r   r   r{   r{   È   s¯  € € € € € € ð
ð 
ð €L�#ÐÐÑð+ð Ð�cÐÐÑð/ð €O�SÐÐÑðMð €K�ÐÐÑà!Ð�sÐ!Ð!Ñ!ðð $(€L�(˜4”.Ð'Ð'Ñ'Ø1à€Yˆ} T°tÐ<Ñ<Ô<Øð¨#ð °tð Ð@Uð ð ð ñ „[ñ =Ô<ðð ð
 W¨S°#¨XÔ%6ð 
ð 
ð 
ñ „Xð
ð ð"˜3ð "ð "ð "ñ „Xð"ð %)Ø:>ð	ð àðð �t˜C”yÔ!ðð Ð6Ô7ð	ð
 ðð 
ðð ð ð ð ð r   r{   )rY   Úurllib.requestr"   rh   Úabcr   Útypingr   r   r   r   r   Úlangchain_core.callbacksr	   Ú#langchain_core.language_models.llmsr
   Úlangchain_core.pydantic_v1r   r   Úlangchain_core.utilsr   Úobjectr   r5   rR   rd   rm   rr   rw   r{   r3   r   r   ú<module>rž      s   ðØ €€€Ø Ð Ð Ð Ø €€€Ø Ð Ð Ð Ð Ð Ø 5Ð 5Ð 5Ð 5Ð 5Ð 5Ð 5Ð 5Ð 5Ð 5Ð 5Ð 5Ð 5Ð 5à =Ð =Ð =Ð =Ð =Ð =Ø 3Ð 3Ð 3Ð 3Ð 3Ð 3Ø ;Ð ;Ð ;Ð ;Ð ;Ð ;Ð ;Ð ;Ø 5Ð 5Ð 5Ð 5Ð 5Ð 5ð ð  ð  ð  ð  ˜Fñ  ô  ð  ðFCð Cð Cð Cð Cñ Cô Cð CðL*ð *ð *ð *ð *Ð/ñ *ô *ð *ð
ð 
ð 
ð 
ð 
Ð.ñ 
ô 
ð 
ð 7ð 7ð 7ð 7ð 7Ð-ñ 7ô 7ð 7ð%ð %ð %ð %ð %Ð0ñ %ô %ð %ð"*ð *ð *ð *ð *Ð0ñ *ô *ð *ð*[ð [ð [ð [ð [˜C ñ [ô [ð [ð [ð [r   