§
    šŠtj—  ã            	       óÀ   — d dl 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ee         eee                  f         ¦  «        Z G d„ d	e
e¦  «        Zd
S )é    )ÚAnyÚDictÚListÚOptional)Ú
Embeddings)Úpre_init)Ú	BaseModelÚ
ConfigDict)ÚContentHandlerBasec                   ó   — e Zd ZdZdS )ÚEmbeddingsContentHandlerzContent handler for LLM class.N)Ú__name__Ú
__module__Ú__qualname__Ú__doc__© ó    úo/var/www/html/CA-Chatbot/venv/lib/python3.11/site-packages/langchain_community/embeddings/sagemaker_endpoint.pyr   r   
   s   € € € € € Ø(Ð(Ð(Ð(r   r   c            	       ón  — 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         ed<   	 eed<   	 	 dZee         ed	<   	 dZee         ed
<   	  eddd¬¦  «        Zededefd„¦   «         Zdee         deee                  fd„Z	 ddee         dedeee                  fd„Zdedee         fd„ZdS )ÚSagemakerEndpointEmbeddingsa®  Custom Sagemaker Inference Endpoints.

    To use, you must supply the endpoint name from your deployed
    Sagemaker model & the region where it is deployed.

    To authenticate, the AWS client uses the following methods to
    automatically load credentials:
    https://boto3.amazonaws.com/v1/documentation/api/latest/guide/credentials.html

    If a specific credential profile should be used, you must pass
    the name of the profile from the ~/.aws/credentials file that is to be used.

    Make sure the credentials / roles used have the required policies to
    access the Sagemaker endpoint.
    See: https://docs.aws.amazon.com/IAM/latest/UserGuide/access_policies.html
    NÚclientÚ Úendpoint_nameÚregion_nameÚcredentials_profile_nameÚcontent_handlerÚmodel_kwargsÚendpoint_kwargsTÚforbidr   )Úarbitrary_types_allowedÚextraÚprotected_namespacesÚvaluesÚreturnc                 ó|  — |                      d¦  «        �|S 	 	 ddl}	 |d         �|                     |d         ¬¦  «        }n|                     ¦   «         }|                     d|d         ¬¦  «        |d<   n%# t          $ r}t          d	|› �¦  «        |‚d}~ww xY wn# t          $ r t          d
¦  «        ‚w xY w|S )z.Dont do anything if client provided externallyr   Nr   r   )Úprofile_namezsagemaker-runtimer   )r   zƒCould not load credentials to authenticate with AWS client. Please check that credentials in the specified profile name are valid. zRCould not import boto3 python package. Please install it with `pip install boto3`.)ÚgetÚboto3ÚSessionr   Ú	ExceptionÚ
ValueErrorÚImportError)Úclsr#   r(   ÚsessionÚes        r   Úvalidate_environmentz0SagemakerEndpointEmbeddings.validate_environmentu   s#  € ð �:Š:�hÑÔÐ+ØˆMàXð	ØˆLˆLˆLðØÐ4Ô5ÐAØ#ŸmšmØ%+Ð,FÔ%Gð ,ñ ô �G�Gð
 $Ÿmšm™oœo�Gà#*§>¢>Ø'°V¸MÔ5Jð $2ñ $ô $��xÑ Ð øõ ð ð ð Ý ð3à/0ð3ð 3ñô ð ð	øøøøðøøøð	 !øõ ð 	ð 	ð 	Ýð>ñô ð ð	øøøð
 ˆs/   šB ŸAA9 Á8B Á9
BÂBÂBÂB ÂB9Útextsc                 óŒ  — t          t          d„ |¦  «        ¦  «        }| j        pi }| j        pi }| j                             ||¦  «        }| j        j        }| j        j        }	  | j        j	        d| j
        |||dœ|¤Ž}n$# t          $ r}t          d|› �¦  «        ‚d}~ww xY w| j                             |d         ¦  «        S )z3Call out to SageMaker Inference embedding endpoint.c                 ó.   — |                       dd¦  «        S )Nú
ú )Úreplace)Úxs    r   ú<lambda>z=SagemakerEndpointEmbeddings._embedding_func.<locals>.<lambda>�   s   €  1§9¢9¨T°3Ñ#7Ô#7€ r   )ÚEndpointNameÚBodyÚContentTypeÚAcceptz$Error raised by inference endpoint: Nr:   r   )ÚlistÚmapr   r   r   Útransform_inputÚcontent_typeÚacceptsr   Úinvoke_endpointr   r*   r+   Útransform_output)	Úselfr1   Ú_model_kwargsÚ_endpoint_kwargsÚbodyr@   rA   Úresponser/   s	            r   Ú_embedding_funcz+SagemakerEndpointEmbeddings._embedding_funcš   s  € õ •SÐ7Ð7¸Ñ?Ô?Ñ@Ô@ˆØÔ)Ð/¨RˆØÔ/Ð5°2ÐàÔ#×3Ò3°E¸=ÑIÔIˆØÔ+Ô8ˆØÔ&Ô.ˆð		IØ2�t”{Ô2ð Ø!Ô/ØØ(Øð	ð ð
 #ðð ˆHˆHøõ ð 	Ið 	Ið 	IÝÐGÀAÐGÐGÑHÔHÐHøøøøð	Iøøøð Ô#×4Ò4°X¸fÔ5EÑFÔFÐFs   Á%B Â
B#ÂBÂB#é@   Ú
chunk_sizec                 óþ   — g }|t          |¦  «        k    rt          |¦  «        n|}t          dt          |¦  «        |¦  «        D ]7}|                      ||||z   …         ¦  «        }|                     |¦  «         Œ8|S )a‹  Compute doc embeddings using a SageMaker Inference Endpoint.

        Args:
            texts: The list of texts to embed.
            chunk_size: The chunk size defines how many input texts will
                be grouped together as request. If None, will use the
                chunk size specified by the class.


        Returns:
            List of embeddings, one for each text.
        r   )ÚlenÚrangerI   Úextend)rD   r1   rK   ÚresultsÚ_chunk_sizeÚirH   s          r   Úembed_documentsz+SagemakerEndpointEmbeddings.embed_documents³   s„   € ð ˆØ$.µ°U±´Ò$;Ð$;•c˜%‘j”j�jÀˆÝ�q�#˜e™*œ* kÑ2Ô2ð 	%ð 	%ˆAØ×+Ò+¨E°!°a¸+±oÐ2EÔ,FÑGÔGˆHØ�NŠN˜8Ñ$Ô$Ð$Ð$Øˆr   Útextc                 ó:   — |                       |g¦  «        d         S )z²Compute query embeddings using a SageMaker inference endpoint.

        Args:
            text: The text to embed.

        Returns:
            Embeddings for the text.
        r   )rI   )rD   rT   s     r   Úembed_queryz'SagemakerEndpointEmbeddings.embed_queryÉ   s   € ð ×#Ò# T FÑ+Ô+¨AÔ.Ð.r   )rJ   )r   r   r   r   r   r   Ú__annotations__r   Ústrr   r   r   r   r   r   r   r
   Úmodel_configr   r0   r   ÚfloatrI   ÚintrS   rV   r   r   r   r   r      s±  € € € € € € ðð ð"ð< €FˆCÐÐÑà€M�3ÐÐÑð,ð €K�ÐÐÑØPà.2Ð˜h sœmÐ2Ð2Ñ2ðð .Ð-Ð-Ñ-ðð
ð& $(€L�(˜4”.Ð'Ð'Ñ'Ø1à&*€O�X˜d”^Ð*Ð*Ñ*ðð
 �:Ø $¨HÈ2ðñ ô €Lð ð"¨$ð "°4ð "ð "ð "ñ „Xð"ðHG T¨#¤Yð G°4¸¸U¼Ô3Dð Gð Gð Gð Gð4 35ðð Ø˜#”YðØ,/ðà	ˆd�5ŒkÔ	ðð ð ð ð,	/ ð 	/¨¨U¬ð 	/ð 	/ð 	/ð 	/ð 	/ð 	/r   r   N)Útypingr   r   r   r   Úlangchain_core.embeddingsr   Úlangchain_core.utilsr   Úpydanticr	   r
   Ú+langchain_community.llms.sagemaker_endpointr   rX   rZ   r   r   r   r   r   ú<module>ra      sù   ðØ ,Ð ,Ð ,Ð ,Ð ,Ð ,Ð ,Ð ,Ð ,Ð ,Ð ,Ð ,à 0Ð 0Ð 0Ð 0Ð 0Ð 0Ø )Ð )Ð )Ð )Ð )Ð )Ø *Ð *Ð *Ð *Ð *Ð *Ð *Ð *à JÐ JÐ JÐ JÐ JÐ Jð)ð )ð )ð )ð )Ð1°$°s´)¸TÀ$ÀuÄ+Ô=NÐ2NÔOñ )ô )ð )ðD/ð D/ð D/ð D/ð D/ )¨Zñ D/ô D/ð D/ð D/ð D/r   