a
    bg                     @   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   /var/www/html/cobodadashboardai.evdpl.com/venv/lib/python3.9/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eedddZee eee  dddZdee eeee  dddZeee d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 zddl}z@|d dur<|j|d d}n| }|jd|d d|d< W n4 ty } ztd	| |W Y d}~n
d}~0 0 W n ty   td
Y n0 |S )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   s4    
z0SagemakerEndpointEmbeddings.validate_environment)textsr   c           	   
   C   s   t tdd |}| jpi }| jp$i }| j||}| jj}| jj}z"| jj	f | j
|||d|}W n2 ty } ztd| W Y d}~n
d}~0 0 | j|d S )z3Call out to SageMaker Inference embedding endpoint.c                 S   s   |  ddS )N
 )replace)xr   r   r   <lambda>       z=SagemakerEndpointEmbeddings._embedding_func.<locals>.<lambda>)ZEndpointNameBodyZContentTypeAcceptz$Error raised by inference endpoint: Nr/   )listmapr   r   r   Ztransform_inputcontent_typeacceptsr   Zinvoke_endpointr   r!   r"   Ztransform_output)	selfr(   Z_model_kwargsZ_endpoint_kwargsbodyr3   r4   responser&   r   r   r   _embedding_func   s$    


$z+SagemakerEndpointEmbeddings._embedding_func@   )r(   
chunk_sizer   c                 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ranger8   extend)r5   r(   r:   results_chunk_sizeir7   r   r   r   embed_documents   s    z+SagemakerEndpointEmbeddings.embed_documents)textr   c                 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   )r8   )r5   rB   r   r   r   embed_query   s    	z'SagemakerEndpointEmbeddings.embed_query)r9   )r   r   r   r   r   r   __annotations__r   strr   r   r   r   r   r   r   r	   Zmodel_configr   r'   r   floatr8   intrA   rC   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
   rE   rF   r   r   r   r   r   r   <module>   s   $