o
    Zh                     @   sh   d dl Z d dlmZmZmZmZmZ d dlmZm	Z	m
Z
 d dlmZ G dd dZG dd deeZdS )	    N)AnyDictListOptionalTuple)	BaseModel
ConfigDictmodel_validator)BaseCrossEncoderc                   @   sL   e Zd ZdZdZdZdeeeef  de	fddZ
dedee fdd	Zd
S )CrossEncoderContentHandlerz'Content handler for CrossEncoder class.zapplication/json
text_pairsreturnc                 C   s   t d|i}|dS )Nr   utf-8)jsondumpsencode)selfr   Z	input_str r   l/var/www/html/lang_env/lib/python3.10/site-packages/langchain_community/cross_encoders/sagemaker_endpoint.pytransform_input   s   
z*CrossEncoderContentHandler.transform_inputoutputc                 C   s    t | d}|d }|S )Nr   scores)r   loadsreaddecode)r   r   Zresponse_jsonr   r   r   r   transform_output   s   z+CrossEncoderContentHandler.transform_outputN)__name__
__module____qualname____doc__content_typeacceptsr   r   strbytesr   r   floatr   r   r   r   r   r   	   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 Zeed< dZee ed	< 	 dZee ed
< 	 eddddZeddededefddZdeeeef  dee fddZdS )SagemakerEndpointCrossEncodera  SageMaker Inference CrossEncoder endpoint.

    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before)modevaluesr   c              
   C   s   z7ddl }z!|dr|j|d d}n| }|jd|d d|d< W W |S  ty7 } ztd	|d}~ww  tyB   td
w )zJValidate that AWS credentials to and python package exists in environment.r   Nr*   )Zprofile_namezsagemaker-runtimer)   )r)   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`.)boto3getSessionr&   	Exception
ValueErrorImportError)clsr1   r2   sessioner   r   r   validate_environment`   s2   
z2SagemakerEndpointCrossEncoder.validate_environmentr   c              
   C   s   | j pi }| j|}| jj}| jj}z| jjd| j|||d|}W n ty7 } zt	d| d}~ww | j
|d S )z6Call out to SageMaker Inference CrossEncoder endpoint.)ZEndpointNameBodyZContentTypeAcceptz$Error raised by inference endpoint: Nr<   r   )r-   r+   r   r    r!   r&   Zinvoke_endpointr(   r5   r6   r   )r   r   Z_endpoint_kwargsbodyr    r!   responser:   r   r   r   score   s$   

z#SagemakerEndpointCrossEncoder.score)r   r   r   r   r&   r   __annotations__r(   r"   r)   r*   r   r   r+   r,   r   r-   r   Zmodel_configr	   classmethodr;   r   r   r$   r@   r   r   r   r   r%      s,   
 & r%   )r   typingr   r   r   r   r   Zpydanticr   r   r	   Z'langchain_community.cross_encoders.baser
   r   r%   r   r   r   r   <module>   s    