
    h                     t    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y)	    N)AnyDictListOptionalTuple)	BaseModel
ConfigDictmodel_validator)BaseCrossEncoderc                   N    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y)	CrossEncoderContentHandlerz'Content handler for CrossEncoder class.zapplication/json
text_pairsreturnc                 R    t        j                  d|i      }|j                  d      S )Nr   utf-8)jsondumpsencode)selfr   	input_strs      s/var/www/html/eduruby.in/venv/lib/python3.12/site-packages/langchain_community/cross_encoders/sagemaker_endpoint.pytransform_inputz*CrossEncoderContentHandler.transform_input   s&    JJj9:	((    outputc                 t    t        j                  |j                         j                  d            }|d   }|S )Nr   scores)r   loadsreaddecode)r   r   response_jsonr   s       r   transform_outputz+CrossEncoderContentHandler.transform_output   s0    

6;;=#7#7#@Ax(r   N)__name__
__module____qualname____doc__content_typeacceptsr   r   strbytesr   r   floatr!    r   r   r   r   	   sF    1%L G)$uS#X*? )E )s tE{ r   r   c                   
   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y)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 }	 |j                  d      r|j                  |d         }n|j                         }|j                  d|d         |d<   |S # t        $ r}t        d	      |d}~ww xY w# t        $ r t        d
      w xY w)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    	::89#mm%+,F%G , G
 $mmoG#*>>'VM5J $2 $x      . 	  	> 	s)   A6 AA 	A3"A..A33A6 6Br   c                    | j                   xs i }| j                  j                  |      }| j                  j                  }| j                  j                  }	  | j
                  j                  d| j                  |||d|}| j                  j                  |d         S # t        $ r}t        d|       d}~ww xY w)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8&&..		I2t{{22 !//(	
 #H ##44Xf5EFF  	ICA3GHH	Is   +B" "	B>+B99B>)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-      s    "* FCM3+ KP.2hsm2 3M2NO/N#'L(4.'1&*OXd^*
  $H2L (#$ 3   $@GU38_ 5 G$u+ Gr   r-   )r   typingr   r   r   r   r   pydanticr   r	   r
   'langchain_community.cross_encoders.baser   r   r-   r+   r   r   <module>rY      s4     3 3 ; ; D  }GI/? }Gr   