
    7|h3                     n   d Z ddlZddlZddlmZ ddlmZmZmZm	Z	m
Z
mZmZmZmZ ddlmZ ddlmZ ddlmZ ddlmZ dd	lmZ dd
lmZ  edeee
e   f         Z edeee
e
e      e	f         Z G d d      Z G d deeef         Z  G d de eef         Z! eddd       G d de             Z"y)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                   4    e Zd ZdZdeddfdZddZdefdZy)	LineIteratora  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                 d    t        |      | _        t        j                         | _        d| _        y )Nr   )iterbyte_iteratorioBytesIObufferread_pos)selfr   s     j/var/www/html/test/engine/venv/lib/python3.12/site-packages/langchain_community/llms/sagemaker_endpoint.py__init__zLineIterator.__init__2   s"    !&\jjl    c                     | S )N r!   s    r"   __iter__zLineIterator.__iter__7   s    r$   c                 @   	 | j                   j                  | j                         | j                   j                         }|r4|d   t	        d      k(  r#| xj                  t        |      z  c_        |d d S 	 t        | j                        }d|vr| j                   j                  dt        j                         | j                   j                  |d   d          # t        $ r6 | j                  | j                   j                         j                  k  rY  w xY w)N
PayloadPartr   Bytes)r   seekr    readlineordlennextr   StopIteration	getbuffernbytesr   SEEK_ENDwrite)r!   linechunks      r"   __next__z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   8C :DD)r   r   )__name__
__module____qualname____doc__r   r#   r(   r:   r&   r$   r"   r   r      s+    :s t 
=# =r$   r   c                   x    e Zd ZU dZ	 dZee   ed<   	 dZee   ed<   	 e	de
dedefd       Ze	d	edefd
       Zy)ContentHandlerBasezHandler 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                      y)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!   rC   rD   s      r"   transform_inputz"ContentHandlerBase.transform_inputm       r$   outputc                      y)z[Transforms the output from the model to string that
        the LLM class expects.
        Nr&   )r!   rH   s     r"   transform_outputz#ContentHandlerBase.transform_outputu   rG   r$   N)r;   r<   r=   r>   rA   r
   str__annotations__rB   r   r   r   bytesrF   r   rJ   r&   r$   r"   r@   r@   N   sy    " #/L(3-.<)GXc])Cj     u   r$   r@   c                       e Zd ZdZy)LLMContentHandlerzContent handler for LLM class.N)r;   r<   r=   r>   r&   r$   r"   rO   rO   |   s    (r$   rO   z0.3.16z1.0z$langchain_aws.llms.SagemakerEndpoint)sinceremovalalternative_importc                   :   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ed<   	 d	Zeed
<   	 	 dZee   ed<   	 dZee   ed<   	  ed      Zededefd       Zedeeef   fd       Zedefd       Z	 	 ddedeee      dee   dedef
dZy)SagemakerEndpointa  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	streamingrD   endpoint_kwargsforbid)extravaluesr   c                 ,   |j                  d      |S 	 	 ddl}	 |d   |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)z.Dont do anything if client provided externallyrU   Nr   rY   )profile_namezsagemaker-runtimerX   )rX   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SessionrU   	Exception
ValueErrorImportError)clsr_   rc   sessiones        r"   validate_environmentz&SagemakerEndpoint.validate_environment  s     ::h+MX	45A#mm%+,F%G , G
 $mmoG#*>>'VM5J $2 $x      . 	  	> 	s)   A> AA! !	A;*A66A;;A> >Bc                 J    | j                   xs i }i d| j                  id|iS )zGet the identifying parameters.rW   rD   )rD   rW   )r!   _model_kwargss     r"   _identifying_paramsz%SagemakerEndpoint._identifying_params'  s>     ))/R
 2 23
}-
 	
r$   c                      y)zReturn type of llm.sagemaker_endpointr&   r'   s    r"   	_llm_typezSagemakerEndpoint._llm_type0  s     $r$   rC   stoprun_managerkwargsc                 t   | j                   xs i }i ||}| j                  xs i }| j                  j                  ||      }| j                  j                  }| j                  j
                  }	| j                  r|r	  | j                  j                  d
| j                  || j                  j                  d|}
t        |
d         }d}|D ]O  }t        j                  |      }
|
j                  d      d   }|t        ||      }||z  }|j                  |       Q |S 	  | j                  j$                  d
| j                  |||	d|}| j                  j'                  |d         }|t        ||      }|S # t         $ r}t#        d|       d}~ww xY w# t         $ r}t#        d	|       d}~ww xY w)af  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ContentTyperw   rV   outputsr   Nz.Error raised by streaming inference endpoint: )rv   rw   rx   Acceptz$Error raised by inference endpoint: r&   )rD   r\   rZ   rF   rA   rB   r[   rU   $invoke_endpoint_with_response_streamrW   r   jsonloadsrb   r   on_llm_new_tokenre   rf   invoke_endpointrJ   )r!   rC   rr   rs   rt   rm   _endpoint_kwargsbodyrA   rB   respiteratorcurrent_completionr8   resp_outputrj   responsetexts                     r"   _callzSagemakerEndpoint._call5  s   * ))/R3=3F3//52##33FMJ++88&&..>>kWGt{{GG !%!3!3 $ 4 4 A A '	 (V5*,"$ >D::d+D"&((9"5a"8K'&9+t&L&+5&00=> *)	M64;;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#E< #+F <	FFF	F7$F22F7)NN)r;   r<   r=   r>   rU   r   rL   rW   rK   rX   rY   r
   rO   r[   boolrD   r   r\   r   model_configr   rk   propertyr	   rn   rq   r   r   r   r&   r$   r"   rT   rT      sX   "/` 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r$   rT   )#r>   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   rK   r   floatr   r   r@   rO   rT   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$   