
    dh                     t    S SK r S SKJrJrJrJrJr  S SKJrJ	r	J
r
  S SKJr   " S S5      r " S S\\5      rg)	    N)AnyDictListOptionalTuple)	BaseModel
ConfigDictmodel_validator)BaseCrossEncoderc                   Z    \ rS rSrSrSrSrS\\\	\	4      S\
4S jrS\S\\   4S jrS	rg
)CrossEncoderContentHandler	   z'Content handler for CrossEncoder class.zapplication/json
text_pairsreturnc                 T    [         R                  " SU05      nUR                  S5      $ )Nr   utf-8)jsondumpsencode)selfr   	input_strs      m/var/www/html/shao/venv/lib/python3.13/site-packages/langchain_community/cross_encoders/sagemaker_endpoint.pytransform_input*CrossEncoderContentHandler.transform_input   s&    JJj9:	((    outputc                 v    [         R                  " UR                  5       R                  S5      5      nUS   nU$ )Nr   scores)r   loadsreaddecode)r   r   response_jsonr   s       r   transform_output+CrossEncoderContentHandler.transform_output   s0    

6;;=#7#7#@Ax(r    N)__name__
__module____qualname____firstlineno____doc__content_typeacceptsr   r   strbytesr   r   floatr#   __static_attributes__r%   r   r   r   r   	   sF    1%L G)$uS#X*? )E )s tE{ r   r   c                   
   \ rS rSr% Sr Sr\\S'   Sr\	\S'    Sr
\	\S'    Sr\\	   \S'    \" 5       r\\S	'   Sr\\   \S
'    Sr\\   \S'    \" SSSS9r\" SS9\S\S\4S j5       5       rS\\\	\	4      S\\   4S jrSrg)SagemakerEndpointCrossEncoder   a  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                     SSK n UR                  S5      (       a  UR                  US   S9nOUR                  5       nUR                  SUS   S9US'   U$ ! [         a  n[        S	5      UeSnAff = f! [         a    [        S
5      ef = f)zJValidate that AWS credentials to and python package exists in environment.r   Nr8   )profile_namezsagemaker-runtimer7   )r7   r4   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Sessionr4   	Exception
ValueErrorImportError)clsrB   rE   sessiones        r   validate_environment2SagemakerEndpointCrossEncoder.validate_environment`   s    	::899#mm%+,F%G , G
 $mmoG#*>>'VM5J $2 $x      . 	  	> 	s)   A6 AA 
A3"A..A33A6 6Br   c                    U R                   =(       d    0 nU R                  R                  U5      nU R                  R                  nU R                  R                  n U R
                  R                  " SU R                  UUUS.UD6nU R                  R                  US   5      $ ! [         a  n[        SU 35      eSnAff = f)z6Call out to SageMaker Inference CrossEncoder endpoint.)EndpointNameBodyContentTypeAcceptz$Error raised by inference endpoint: NrR   r%   )r;   r9   r   r+   r,   r4   invoke_endpointr6   rH   rI   r#   )r   r   _endpoint_kwargsbodyr+   r,   responserM   s           r   score#SagemakerEndpointCrossEncoder.score   s    //52##33J?++88&&..		I{{22 !//(	
 #H ##44Xf5EFF  	ICA3GHH	Is   +B' '
C1B??C)r&   r'   r(   r)   r*   r4   r   __annotations__r6   r-   r7   r8   r   r   r9   r:   r   r;   r	   model_configr
   classmethodrN   r   r   r/   rY   r0   r%   r   r   r2   r2      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   r2   )r   typingr   r   r   r   r   pydanticr   r	   r
   'langchain_community.cross_encoders.baser   r   r2   r%   r   r   <module>ra      s4     3 3 ; ; D  }GI/? }Gr   