
    Ng                         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          Z G d dee          ZdS )	    N)AnyDictListOptionalTuple)	BaseModel
ConfigDictmodel_validator)BaseCrossEncoderc                   b    e Zd ZdZdZdZdeeeef                  de	fdZ
dedee         fdZdS )	CrossEncoderContentHandlerz'Content handler for CrossEncoder class.zapplication/json
text_pairsreturnc                 X    t          j        d|i          }|                    d          S )Nr   utf-8)jsondumpsencode)selfr   	input_strs      q/var/www/html/ai-engine/env/lib/python3.11/site-packages/langchain_community/cross_encoders/sagemaker_endpoint.pytransform_inputz*CrossEncoderContentHandler.transform_input   s*    Jj9::	(((    outputc                     t          j        |                                                    d                    }|d         }|S )Nr   scores)r   loadsreaddecode)r   r   response_jsonr   s       r   transform_outputz+CrossEncoderContentHandler.transform_output   s6    
6;;==#7#7#@#@AAx(r   N)__name__
__module____qualname____doc__content_typeacceptsr   r   strbytesr   r   floatr!    r   r   r   r   	   sy        11%L G)$uS#X*? )E ) ) ) )s tE{      r   r   c                   N   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          Z ed          ededefd                        Zdeeeef                  dee         f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_kwargsTforbidr+   )arbitrary_types_allowedextraprotected_namespacesbefore)modevaluesr   c                 `   	 ddl }	 |                    d          r|                    |d                   }n|                                }|                    d|d                   |d<   n"# t          $ r}t          d	          |d}~ww xY wn# t          $ r t          d
          w xY w|S )zJValidate that AWS credentials to and python package exists in environment.r   Nr2   )profile_namezsagemaker-runtimer1   )r1   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)clsr<   r?   sessiones        r   validate_environmentz2SagemakerEndpointCrossEncoder.validate_environment`   s   	LLL::899 .#mm%+,F%G ,  GG
 $mmooG#*>>'VM5J $2 $ $x       .  		 !  	 	 	>  	
 s/   B A&A. -B .
B8BBB B+r   c                 <   | j         pi }| j                            |          }| j        j        }| j        j        }	  | j        j        d| j        |||d|}n$# t          $ r}t          d|           d}~ww xY w| j        
                    |d                   S )z6Call out to SageMaker Inference CrossEncoder endpoint.)EndpointNameBodyContentTypeAcceptz$Error raised by inference endpoint: NrK   r+   )r5   r3   r   r&   r'   r.   invoke_endpointr0   rB   rC   r!   )r   r   _endpoint_kwargsbodyr&   r'   responserG   s           r   scorez#SagemakerEndpointCrossEncoder.score   s    /52#33J??+8&.		I2t{2 !/(	 
 # HH  	I 	I 	IGAGGHHH	I #44Xf5EFFFs   A 
A;$A66A;)r"   r#   r$   r%   r.   r   __annotations__r0   r(   r1   r2   r   r   r3   r4   r   r5   r	   model_configr
   classmethodrH   r   r   r*   rR   r+   r   r   r-   r-      si         "* FCM3+ KP.2hsm222 3M2L2N2NO/NNN#'L(4.'''1&*OXd^***
 : $H2  L _(###$ 3    [ $#@GU38_ 5 G$u+ G G G G G Gr   r-   )r   typingr   r   r   r   r   pydanticr   r	   r
   'langchain_community.cross_encoders.baser   r   r-   r+   r   r   <module>rY      s     3 3 3 3 3 3 3 3 3 3 3 3 3 3 ; ; ; ; ; ; ; ; ; ; D D D D D D        }G }G }G }G }GI/? }G }G }G }G }Gr   