
    IgJ3                     F   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 y)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                   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     h/var/www/html/answerous/venv/lib/python3.12/site-packages/langchain_community/llms/sagemaker_endpoint.py__init__zLineIterator.__init__1   s"    !&\jjl    c                     | S )N r    s    r!   __iter__zLineIterator.__iter__6   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__9   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'   r9   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    rB   rC   s      r!   transform_inputz"ContentHandlerBase.transform_inputl       r#   outputc                      y)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   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!   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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	streamingrC   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 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	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.rS   rC   )rC   rS   )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_type*  s     $r#   rB   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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++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=   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      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#   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 = 3 )  >\sDI~)>?
m5d4;6G1Q+RS7= 7=t+[!89 +\)*384 )t tr#   