
    dh              	           S SK JrJrJrJr  S SKJr  S SKJr  S SK	J
r
Jr  S SKJr   " S S\\\   \\\      4   5      r " S S	\
\5      rg
)    )AnyDictListOptional)
Embeddings)pre_init)	BaseModel
ConfigDict)ContentHandlerBasec                       \ rS rSrSrSrg)EmbeddingsContentHandler
   zContent handler for LLM class. N)__name__
__module____qualname____firstlineno____doc____static_attributes__r       i/var/www/html/shao/venv/lib/python3.13/site-packages/langchain_community/embeddings/sagemaker_endpoint.pyr   r   
   s    (r   r   c            	       0   \ rS rSr% Sr Sr\\S'   Sr\	\S'    Sr
\	\S'    Sr\\	   \S'    \\S	'     Sr\\   \S
'    Sr\\   \S'    \" SSSS9r\S\S\4S j5       rS\\	   S\\\      4S jr SS\\	   S\S\\\      4S jjrS\	S\\   4S jrSrg)SagemakerEndpointEmbeddings   a  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                 .   UR                  S5      b  U$   SSKn US   b  UR                  US   S9nOUR                  5       nUR                  SUS   S9US'   U$ ! [         a  n[        S	U 35      UeSnAff = f! [         a    [        S
5      ef = f)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_environment0SagemakerEndpointEmbeddings.validate_environmentu   s     ::h+MX	45A#mm%+,F%G , G
 $mmoG#*>>'VM5J $2 $x      //0c3 	  	> 	s(   A> ?A 
A;'A66A;;A> >Btextsc                    [        [        S U5      5      nU R                  =(       d    0 nU R                  =(       d    0 nU R                  R                  X5      nU R                  R                  nU R                  R                  n U R                  R                  " SU R                  UUUS.UD6nU R                  R                  US   5      $ ! [         a  n[        SU 35      eSnAff = f)z3Call out to SageMaker Inference embedding endpoint.c                 &    U R                  SS5      $ )N
 )replace)xs    r   <lambda>=SagemakerEndpointEmbeddings._embedding_func.<locals>.<lambda>   s    199T3#7r   )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)	selfr6   _model_kwargs_endpoint_kwargsbodyrF   rG   responser3   s	            r   _embedding_func+SagemakerEndpointEmbeddings._embedding_func   s     S7?@))/R//52##33EI++88&&..		I{{22 !//(	
 #H ##44Xf5EFF  	ICA3GHH	Is   	+C 
C/C**C/
chunk_sizec                     / nU[        U5      :  a  [        U5      OUn[        S[        U5      U5       H)  nU R                  XXT-    5      nUR                  U5        M+     U$ )aK  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rangerO   extend)rJ   r6   rQ   results_chunk_sizeirN   s          r   embed_documents+SagemakerEndpointEmbeddings.embed_documents   s`     $.U$;c%jq#e*k2A++Eao,FGHNN8$ 3 r   textc                 ,    U R                  U/5      S   $ )zCompute query embeddings using a SageMaker inference endpoint.

Args:
    text: The text to embed.

Returns:
    Embeddings for the text.
r   )rO   )rJ   r[   s     r   embed_query'SagemakerEndpointEmbeddings.embed_query   s     ##TF+A..r   )@   )r   r   r   r   r   r   r   __annotations__r   strr   r   r   r   r!   r   r"   r
   model_configr   r4   r   floatrO   intrY   r]   r   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   ra   rc   r   r   r   r   r   <module>rj      sK    , , 0 ) * J)1$s)T$u+=N2NO )D/)Z D/r   