
    dh3                     \   S r SSKrSSKrSSKJr  SSKJrJrJrJ	r	J
r
JrJrJrJr  SSKJr  SSKJr  SSKJr  SSKJr  SS	KJr  SS
KJr  \" S\\\
\   4   S9r\" S\\\
\
\      \	4   S9r " S S5      r " S S\\\4   5      r  " S S\ \\4   5      r!\" SSSS9 " S S\5      5       r"g)zSagemaker InvokeEndpoint API.    N)abstractmethod)	AnyDictGenericIteratorListMappingOptionalTypeVarUnion)
deprecated)CallbackManagerForLLMRun)LLM)pre_init)
ConfigDict)enforce_stop_tokens
INPUT_TYPE)boundOUTPUT_TYPEc                   B    \ rS rSrSrS\SS4S jrS
S jrS\4S jrS	r	g)LineIterator   a  Parse the byte stream input.

    The output of the model will be in the following format:

    b'{"outputs": [" a"]}
'
    b'{"outputs": [" challenging"]}
'
    b'{"outputs": [" problem"]}
'
    ...

    While usually each PayloadPart event from the event stream will
    contain a byte array with a full json, this is not guaranteed
    and some of the json objects may be split acrossPayloadPart events.

    For example:

    {'PayloadPart': {'Bytes': b'{"outputs": '}}
    {'PayloadPart': {'Bytes': b'[" problem"]}
'}}


    This class accounts for this by concatenating bytes written via the 'write' function
    and then exposing a method which will return lines (ending with a '
' character)
    within the buffer via the 'scan_lines' function.
    It maintains the position of the last read position to ensure
    that previous bytes are not exposed again.

    For more details see:
    https://aws.amazon.com/blogs/machine-learning/elevating-the-generative-ai-experience-introducing-streaming-support-in-amazon-sagemaker-hosting/
    streamreturnNc                 f    [        U5      U l        [        R                  " 5       U l        SU l        g )Nr   )iterbyte_iteratorioBytesIObufferread_pos)selfr   s     c/var/www/html/shao/venv/lib/python3.13/site-packages/langchain_community/llms/sagemaker_endpoint.py__init__LineIterator.__init__2   s"    !&\jjl    c                     U $ )N r"   s    r#   __iter__LineIterator.__iter__7   s    r&   c                 Z    U R                   R                  U R                  5        U R                   R                  5       nU(       a5  US   [	        S5      :X  a#  U =R                  [        U5      -  sl        US S $  [        U R                  5      nSU;  a  M  U R                   R                  S[        R                  5        U R                   R                  US   S   5        M  ! [         a8    U R                  U R                   R                  5       R                  :  a   GM'  e f = f)N
PayloadPartr   Bytes)r    seekr!   readlineordlennextr   StopIteration	getbuffernbytesr   SEEK_ENDwrite)r"   linechunks      r#   __next__LineIterator.__next__:   s    KKT]]+;;'')DRCI-T*CRy T//0
 E)KKQ,KKeM27;<!  ! ==4;;#8#8#:#A#AAs   >C( (<D*(D*)r    r   r!   )r   r   )
__name__
__module____qualname____firstlineno____doc__r   r$   r*   r=   __static_attributes__r(   r&   r#   r   r      s+    :s t 
=# =r&   r   c                       \ rS rSr% Sr Sr\\   \S'    Sr	\\   \S'    \
S\S\S\4S	 j5       r\
S
\S\4S j5       rSrg)ContentHandlerBaseN   zHandler class to transform input from LLM to a
format that SageMaker endpoint expects.

Similarly, the class handles transforming output from the
SageMaker endpoint to a format that LLM class expects.
z
text/plaincontent_typeacceptspromptmodel_kwargsr   c                     g)zTransforms the input to a format that model can accept
as the request Body. Should return bytes or seekable file
like object in the format specified in the content_type
request header.
Nr(   )r"   rJ   rK   s      r#   transform_input"ContentHandlerBase.transform_inputm       r&   outputc                     g)zKTransforms the output from the model to string that
the LLM class expects.
Nr(   )r"   rP   s     r#   transform_output#ContentHandlerBase.transform_outputu   rO   r&   r(   N)r?   r@   rA   rB   rC   rH   r
   str__annotations__rI   r   r   r   bytesrM   r   rR   rD   r(   r&   r#   rF   rF   N   sy    " #/L(3-.<)GXc])Cj     u   r&   rF   c                       \ rS rSrSrSrg)LLMContentHandler|   zContent handler for LLM class.r(   N)r?   r@   rA   rB   rC   rD   r(   r&   r#   rX   rX   |   s    (r&   rX   z0.3.16z1.0z$langchain_aws.llms.SagemakerEndpoint)sinceremovalalternative_importc                   F   \ rS rSr% Sr Sr\\S'    Sr\	\S'    Sr
\	\S'    Sr\\	   \S'    \\S	'    S
r\\S'     Sr\\   \S'    Sr\\   \S'    \" SS9r\S\S\4S j5       r\S\\	\4   4S j5       r\S\	4S j5       r  SS\	S\\\	      S\\   S\S\	4
S jjrSrg)SagemakerEndpoint   a  Sagemaker Inference Endpoint models.

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_handlerF	streamingrK   endpoint_kwargsforbid)extravaluesr   c                 (   UR                  S5      b  U$   SSKn US   b  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)z.Dont do anything if client provided externallyr`   Nr   rd   )profile_namezsagemaker-runtimerc   )rc   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`.)getboto3Sessionr`   	Exception
ValueErrorImportError)clsrj   rn   sessiones        r#   validate_environment&SagemakerEndpoint.validate_environment  s     ::h+MX	45A#mm%+,F%G , G
 $mmoG#*>>'VM5J $2 $x      . 	  	> 	s(   A; ?A 
A8'A33A88A; ;Bc                 T    U R                   =(       d    0 n0 SU R                  0ESU0E$ )zGet the identifying parameters.rb   rK   )rK   rb   )r"   _model_kwargss     r#   _identifying_params%SagemakerEndpoint._identifying_params'  s>     ))/R
 2 23
}-
 	
r&   c                     g)zReturn type of llm.sagemaker_endpointr(   r)   s    r#   	_llm_typeSagemakerEndpoint._llm_type0  s     $r&   rJ   stoprun_managerkwargsc                    U R                   =(       d    0 n0 UEUEnU R                  =(       d    0 nU R                  R                  X5      nU R                  R                  nU R                  R
                  n	U R                  (       a  U(       a   U R                  R                  " S
U R                  UU R                  R                  S.UD6n
[        U
S   5      nSnU HP  n[        R                  " U5      n
U
R                  S5      S   nUb  [        X5      nX-  nUR                  U5        MR     U$  U R                  R$                  " S
U R                  UUU	S.UD6nU R                  R'                  US   5      nUb  [        UU5      nU$ ! [          a  n[#        SU 35      eSnAff = f! [          a  n[#        S	U 35      eSnAff = f)a  Call out to Sagemaker inference endpoint.

Args:
    prompt: The prompt to pass into the model.
    stop: Optional list of stop words to use when generating.

Returns:
    The string generated by the model.

Example:
    .. code-block:: python

        response = se("Tell me a joke.")
)EndpointNameBodyContentTyper   ra   outputsr   Nz.Error raised by streaming inference endpoint: )r   r   r   Acceptz$Error raised by inference endpoint: r(   )rK   rg   re   rM   rH   rI   rf   r`   $invoke_endpoint_with_response_streamrb   r   jsonloadsrm   r   on_llm_new_tokenrp   rq   invoke_endpointrR   )r"   rJ   r   r   r   ry   _endpoint_kwargsbodyrH   rI   respiteratorcurrent_completionr;   resp_outputru   responsetexts                     r#   _callSagemakerEndpoint._call5  s   * ))/R3=3F3//52##33FJ++88&&..>>kW{{GG !%!3!3 $ 4 4 A A '	 (V5*,"$D::d+D"&((9"5a"8K'&9+&L&5&00= % *)	M;;66 !%!3!3 ,"	
 ' ''88&9IJD +46K)  W #QRSQT!UVVW  M #Gs!KLLMs1   B%F 8+F2 
F/F**F/2
G<G

Gr(   )NN)r?   r@   rA   rB   rC   r`   r   rU   rb   rT   rc   rd   r
   rX   rf   boolrK   r   rg   r   model_configr   rv   propertyr	   rz   r~   r   r   r   rD   r(   r&   r#   r^   r^      s^   "/` FC,M3, KP.2hsm2 '&
 It(& $(L(4.'1&*OXd^*
 L "$ "4 " "H 
WS#X%6 
 
 $3 $ $ %):>	DD tCy!D 67	D
 D 
D Dr&   r^   )#rC   r   r   abcr   typingr   r   r   r   r   r	   r
   r   r   langchain_core._api.deprecationr   langchain_core.callbacksr   #langchain_core.language_models.llmsr   langchain_core.utilsr   pydanticr   langchain_community.llms.utilsr   rT   r   floatr   r   rF   rX   r^   r(   r&   r#   <module>r      s    # 	   X X X 6 = 3 )  >\sDI~)>?
m5d4;6G1Q+RS7= 7=t+[!89 +\)*384 ) 
=
t t
tr&   