o
    Zh                     @   s   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 deee eee  f ZG dd	 d	e
eZd
S )    )AnyDictListOptional)
Embeddings)pre_init)	BaseModel
ConfigDict)ContentHandlerBasec                   @   s   e Zd ZdZdS )EmbeddingsContentHandlerzContent handler for LLM class.N)__name__
__module____qualname____doc__ r   r   h/var/www/html/lang_env/lib/python3.10/site-packages/langchain_community/embeddings/sagemaker_endpoint.pyr   
   s    r   c                	   @   s   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dZededefddZdee deee  fddZ	ddee dedeee  fddZdedee fd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_kwargsTZforbidr   )Zarbitrary_types_allowedextraZprotected_namespacesvaluesreturnc              
   C   s   | ddur	|S 	 z;ddl}z"|d dur|j|d d}n| }|jd|d d|d< W W |S  tyE } ztd	| |d}~ww  tyP   td
w )z.Dont do anything if client provided externallyr   Nr   r   )Z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er   r   r   validate_environmentu   s<   z0SagemakerEndpointEmbeddings.validate_environmenttextsc           	   
   C   s   t tdd |}| jpi }| jpi }| j||}| jj}| jj}z| jj	d| j
|||d|}W n tyF } ztd| d}~ww | j|d S )z3Call out to SageMaker Inference embedding endpoint.c                 S   s   |  ddS )N
 )replace)xr   r   r   <lambda>   s    z=SagemakerEndpointEmbeddings._embedding_func.<locals>.<lambda>)ZEndpointNameBodyZContentTypeAcceptz$Error raised by inference endpoint: Nr.   r   )listmapr   r   r   Ztransform_inputcontent_typeacceptsr   Zinvoke_endpointr   r!   r"   Ztransform_output)	selfr(   Z_model_kwargsZ_endpoint_kwargsbodyr2   r3   responser&   r   r   r   _embedding_func   s(   


z+SagemakerEndpointEmbeddings._embedding_func@   
chunk_sizec                 C   sV   g }|t |krt |n|}tdt ||D ]}| ||||  }|| q|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ranger7   extend)r4   r(   r9   results_chunk_sizeir6   r   r   r   embed_documents   s   z+SagemakerEndpointEmbeddings.embed_documentstextc                 C   s   |  |gd S )zCompute query embeddings using a SageMaker inference endpoint.

        Args:
            text: The text to embed.

        Returns:
            Embeddings for the text.
        r   )r7   )r4   rA   r   r   r   embed_query   s   	z'SagemakerEndpointEmbeddings.embed_query)r8   )r   r   r   r   r   r   __annotations__r   strr   r   r   r   r   r   r   r	   Zmodel_configr   r'   r   floatr7   intr@   rB   r   r   r   r   r      s@   
 $

r   N)typingr   r   r   r   Zlangchain_core.embeddingsr   Zlangchain_core.utilsr   Zpydanticr   r	   Z+langchain_community.llms.sagemaker_endpointr
   rD   rE   r   r   r   r   r   r   <module>   s    $