
    ~
i3                        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"dS )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                   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     C:\Users\Dell Inspiron 16\Desktop\tws\AgrotaPowerBi\back-agrota-powerbi\mcp-client-agrota\venv\Lib\site-packages\langchain_community/llms/sagemaker_endpoint.py__init__zLineIterator.__init__2   s(    !&\\jll    c                     | S )N r!   s    r"   __iter__zLineIterator.__iter__7   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__:   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(   r:   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!   rC   rD   s      r"   transform_inputz"ContentHandlerBase.transform_inputm         r$   outputc                     dS )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   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"   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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	streamingrD   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 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	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.rW   rD   )rD   rW   )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_type0  s
     $#r$   rC   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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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>   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      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$   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 X X X X X X X X X X X X X X X X X X X 6 6 6 6 6 6 = = = = = = 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$   