
    Ng              	           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__     m/var/www/html/ai-engine/env/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   zCould 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	LLL45A#mm%+,F%G ,  GG
 $mmooG#*>>'VM5J $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    199T3#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/52#33E=II+8&.		I2t{2 !/(	 
 # HH  	I 	I 	IGAGGHHH	I #44Xf5E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%jjjq#e**k22 	% 	%A++E!a+o2E,FGGHNN8$$$$r   textc                 :    |                      |g          d         S )zCompute 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     ##TF++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         "< FCM3, KP.2hsm222 .---
& $(L(4.'''1&*OXd^***
 : $H2  L "$ "4 " " " X"HGT#Y G4U3D G G G G4 35 #Y,/	d5k	   ,	/ 	/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+=N2NO ) ) )D/ D/ D/ D/ D/)Z D/ D/ D/ D/ D/r   