
    7|h              	           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y
)    )AnyDictListOptional)
Embeddings)pre_init)	BaseModel
ConfigDict)ContentHandlerBasec                       e Zd ZdZy)EmbeddingsContentHandlerzContent handler for LLM class.N)__name__
__module____qualname____doc__     p/var/www/html/test/engine/venv/lib/python3.12/site-packages/langchain_community/embeddings/sagemaker_endpoint.pyr   r   
   s    (r   r   c            	       $   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y)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                 2   |j                  d      |S 	 	 ddl}	 |d   |j                  |d         }n|j                         }|j                  d|d         |d<   |S # t        $ r}t        d	|       |d}~ww xY w# t        $ r t        d
      w xY w)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	45A#mm%+,F%G , G
 $mmoG#*>>'VM5J $2 $x      //0c3 	  	> 	s)   B AA! !	A>*A99A>>B Btextsc                    t        t        d |            }| j                  xs i }| j                  xs i }| j                  j                  ||      }| j                  j                  }| j                  j                  }	  | j                  j                  d| j                  |||d|}| j                  j                  |d         S # t        $ r}t        d|       d}~ww xY w)z3Call out to SageMaker Inference embedding endpoint.c                 &    | j                  dd      S )N
 )replace)xs    r   <lambda>z=SagemakerEndpointEmbeddings._embedding_func.<locals>.<lambda>   s    199T3#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?@))/R//52##33E=I++88&&..		I2t{{22 !//(	
 #H ##44Xf5EFF  	ICA3GHH	Is    +C	 		C%C  C%
chunk_sizec                     g }|t        |      kD  rt        |      n|}t        dt        |      |      D ]*  }| j                  ||||z          }|j                  |       , |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   rJ   results_chunk_sizeirH   s          r   embed_documentsz+SagemakerEndpointEmbeddings.embed_documents   si     $.U$;c%jq#e*k2 	%A++E!a+o,FGHNN8$	% r   textc                 ,    | j                  |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   rS   s     r   embed_queryz'SagemakerEndpointEmbeddings.embed_query   s     ##TF+A..r   )@   )r   r   r   r   r   r   __annotations__r   strr   r   r   r   r   r   r   r
   model_configr   r0   r   floatrI   intrR   rU   r   r   r   r   r      s!   "< FCM3, KP.2hsm2 .-
& $(L(4.'1&*OXd^*
  $H2L "$ "4 " "HGT#Y G4U3D 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      sK    , , 0 ) * J)1$s)T$u+=N2NO )D/)Z D/r   