a
    bg                     @   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                   @   sH   e Zd ZdZdZdZeeeef  e	dddZ
eee d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   /var/www/html/cobodadashboardai.evdpl.com/venv/lib/python3.9/site-packages/langchain_community/cross_encoders/sagemaker_endpoint.pytransform_input   s    z*CrossEncoderContentHandler.transform_input)outputr   c                 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eedddZeeeef  ee d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   zzddl }z>|dr(|j|d d}n| }|jd|d d|d< W n. tyv } ztd	|W Y d}~n
d}~0 0 W n ty   td
Y n0 |S )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)clsr2   r3   sessioner   r   r   validate_environment`   s,    

z2SagemakerEndpointCrossEncoder.validate_environmentr   c              
   C   s   | j pi }| j|}| jj}| jj}z"| jjf | j|||d|}W n2 tyz } zt	d| W Y d}~n
d}~0 0 | 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'   Zinvoke_endpointr)   r6   r7   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%   rA   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
   