
    NgJ3                        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  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 G d de          Z dS )zSagemaker InvokeEndpoint API.    N)abstractmethod)	AnyDictGenericIteratorListMappingOptionalTypeVarUnion)CallbackManagerForLLMRun)LLM)pre_init)
ConfigDict)enforce_stop_tokens
INPUT_TYPE)boundOUTPUT_TYPEc                   6    e Zd ZdZdeddfdZddZdefdZdS )	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                 l    t          |          | _        t          j                    | _        d| _        d S )Nr   )iterbyte_iteratorioBytesIObufferread_pos)selfr   s     g/var/www/html/ai-engine/env/lib/python3.11/site-packages/langchain_community/llms/sagemaker_endpoint.py__init__zLineIterator.__init__1   s(    !&\\jll    c                     | S )N r    s    r!   __iter__zLineIterator.__iter__6   s    r#   c                 8   	 | j                             | j                   | j                                         }|r@|d         t	          d          k    r'| xj        t          |          z  c_        |d d         S 	 t          | j                  }n8# t          $ r+ | j        | j         	                                j
        k     rY Ă w xY wd|vr| j                             dt          j                   | j                             |d         d                    )NT
PayloadPartr   Bytes)r   seekr   readlineordlennextr   StopIteration	getbuffernbytesr   SEEK_ENDwrite)r    linechunks      r!   __next__zLineIterator.__next__9   s   	=KT]+++;''))D !RCII--T*CRCy T/00    =4;#8#8#:#:#AAAH E))KQ,,,KeM27;<<<!	=s   =B 1CC)r   r   )__name__
__module____qualname____doc__r   r"   r'   r9   r%   r#   r!   r   r      so         :s t    
   =# = = = = = =r#   r   c                       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dS )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                     dS )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    rB   rC   s      r!   transform_inputz"ContentHandlerBase.transform_inputl         r#   outputc                     dS )z[Transforms the output from the model to string that
        the LLM class expects.
        Nr%   )r    rG   s     r!   transform_outputz#ContentHandlerBase.transform_outputt   rF   r#   N)r:   r;   r<   r=   r@   r
   str__annotations__rA   r   r   r   bytesrE   r   rI   r%   r#   r!   r?   r?   M   s          " #/L(3-...<)GXc])))Cj      ^ u     ^  r#   r?   c                       e Zd ZdZdS )LLMContentHandlerzContent handler for LLM class.N)r:   r;   r<   r=   r%   r#   r!   rN   rN   {   s        ((((r#   rN   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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dS )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	streamingrC   endpoint_kwargsforbid)extravaluesr   c                 v   |                     d          |S 	 	 ddl}	 |d         |                    |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 )z.Dont do anything if client provided externallyrQ   Nr   rU   )profile_namezsagemaker-runtimerT   )rT   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SessionrQ   	Exception
ValueErrorImportError)clsr[   r_   sessiones        r!   validate_environmentz&SagemakerEndpoint.validate_environment   s    ::h+MX	LLL45A#mm%+,F%G ,  GG
 $mmooG#*>>'VM5J $2 $ $x       .  		 !  	 	 	>  	
 s/   B AA9 8B 9
BBBB B6c                 2    | j         pi }i d| j        id|iS )zGet the identifying parameters.rS   rC   )rC   rS   )r    _model_kwargss     r!   _identifying_paramsz%SagemakerEndpoint._identifying_params!  s8     )/R
 23
}-
 	
r#   c                     dS )zReturn type of llm.sagemaker_endpointr%   r&   s    r!   	_llm_typezSagemakerEndpoint._llm_type*  s
     $#r#   rB   stoprun_managerkwargsc                    | j         pi }i ||}| j        pi }| j                            ||          }| j        j        }| j        j        }	| j        r|r	  | j        j        d
| j	        || j        j        d|}
t          |
d                   }d}|D ]]}t          j        |          }
|
                    d          d         }|t          ||          }||z  }|                    |           ^|S # t           $ r}t#          d|           d}~ww xY w	  | j        j        d
| j	        |||	d|}n$# t           $ r}t#          d	|           d}~ww xY w| j                            |d                   }|t          ||          }|S )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ContentTypers   rR   outputsr   Nz.Error raised by streaming inference endpoint: )rr   rs   rt   Acceptz$Error raised by inference endpoint: r%   )rC   rX   rV   rE   r@   rA   rW   rQ   $invoke_endpoint_with_response_streamrS   r   jsonloadsr^   r   on_llm_new_tokenra   rb   invoke_endpointrI   )r    rB   rn   ro   rp   ri   _endpoint_kwargsbodyr@   rA   respiteratorcurrent_completionr7   resp_outputrf   responsetexts                     r!   _callzSagemakerEndpoint._call/  s*   * )/R3=3F3/52#33FMJJ+8&.> '	k '	WGt{G !%!3 $ 4 A  '	  (V55*,"$ > >D:d++D"&((9"5"5a"8K'&9+t&L&L&+5&00====)) W W W !URS!U!UVVVW	M64;6 !%!3 ,"	 
 '   M M M !K!K!KLLLM '88&9IJJD +466Ks1   BC4 4
D>DDD6 6
E EE)NN)r:   r;   r<   r=   rQ   r   rK   rS   rJ   rT   rU   r
   rN   rW   boolrC   r   rX   r   model_configr   rg   propertyr	   rj   rm   r   r   r   r%   r#   r!   rP   rP      s         "/` FC,M3, KP.2hsm222 '&&&
 It(& $(L(4.'''1&*OXd^***
 :  L "$ "4 " " " X"H 
WS#X%6 
 
 
 X
 $3 $ $ $ X$ %):>	D DD tCy!D 67	D
 D 
D D D D D Dr#   rP   )!r=   r   rx   abcr   typingr   r   r   r   r   r	   r
   r   r   langchain_core.callbacksr   #langchain_core.language_models.llmsr   langchain_core.utilsr   pydanticr   langchain_community.llms.utilsr   rJ   r   floatr   r   r?   rN   rP   r%   r#   r!   <module>r      s   # # 				        X X X X X X X X X X X X X X X X X X X X X X = = = = = = 3 3 3 3 3 3 ) ) ) ) ) )       > > > > > >W\sDI~)>???
gm5d4;6G1Q+RSSS7= 7= 7= 7= 7= 7= 7= 7=t+ + + + +[!89 + + +\) ) ) ) )*384 ) ) )t t t t t t t t t tr#   