§
    æ˜ºh‰3  ã                   óŽ  — 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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)ÚExtraÚroot_validator)Ú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/  
    A helper class for parsing 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     ús/var/www/html/services/rag-service/venv/lib/python3.11/site-packages/langchain_community/llms/sagemaker_endpoint.pyÚ__init__zLineIterator.__init__0   s(   € Ý! &™\œ\ˆÔÝ”j‘l”lˆŒØˆŒˆˆó    c                 ó   — | S )N© ©r    s    r!   Ú__iter__zLineIterator.__iter__5   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__8   s  € ð	=ØŒK×Ò˜Tœ]Ñ+Ô+Ð+Ø”;×'Ò'Ñ)Ô)ˆDØð !˜˜Rœ¥C¨¡I¤IÒ-Ð-Ø�”¥ T¡¤Ñ*�”Ø˜C˜R˜C”yÐ ðÝ˜TÔ/Ñ0Ô0��øÝ ð ð ð Ø”= 4¤;×#8Ò#8Ñ#:Ô#:Ô#AÒAÐAØ�HØðøøøð  EÐ)Ð)àØŒK×Ò˜Q¥¤Ñ,Ô,Ð,ØŒK×Ò˜e MÔ2°7Ô;Ñ<Ô<Ð<ñ!	=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 )ÚContentHandlerBasezÛA handler 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 )zÙTransforms 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_inputk   ó   € € € 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_outputs   rF   r#   N)r:   r;   r<   r=   r@   r
   ÚstrÚ__annotations__rA   r   r   r   ÚbytesrE   r   rI   r%   r#   r!   r?   r?   L   s¶   € € € € € € ðð ðð" #/€L�(˜3”-Ð.Ð.Ñ.Ø<à)€GˆX�cŒ]Ð)Ð)Ñ)Ø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   z   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<   	  G d„ 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_kwargsc                   ó    — e Zd ZdZej        ZdS )úSagemakerEndpoint.Configz'Configuration for this pydantic object.N)r:   r;   r<   r=   r   ÚforbidÚextrar%   r#   r!   ÚConfigrZ   ÷   s   € € € € € Ø5Ð5à”ˆˆˆr#   r]   Ú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   z‚Could 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^   rb   ÚsessionÚes        r!   Úvalidate_environmentz&SagemakerEndpoint.validate_environmentü   s  € ð �:Š:�hÑÔÐ+ØˆMàXð	ØˆLˆLˆLðØÐ4Ô5ÐAØ#ŸmšmØ%+Ð,FÔ%Gð ,ñ ô �G�Gð
 $Ÿmšm™oœo�Gà#*§>¢>Ø'°V¸MÔ5Jð $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ÚContentTyperv   rR   Úoutputsr   Nz.Error raised by streaming inference endpoint: )ru   rv   rw   ÚAcceptz$Error raised by inference endpoint: r%   )rC   rX   rV   rE   r@   rA   rW   rQ   Ú$invoke_endpoint_with_response_streamrS   r   ÚjsonÚloadsra   r   Úon_llm_new_tokenrd   re   Úinvoke_endpointrI   )r    rB   rq   rr   rs   rl   Ú_endpoint_kwargsÚbodyr@   rA   ÚrespÚiteratorÚcurrent_completionr7   Úresp_outputri   ÚresponseÚtexts                     r!   Ú_callzSagemakerEndpoint._call/  s*  € ð* Ô)Ð/¨RˆØ3˜=Ð3¨FÐ3ˆØÔ/Ð5°2ÐàÔ#×3Ò3°F¸MÑJÔJˆØÔ+Ô8ˆØÔ&Ô.ˆàŒ>ð '	˜kð '	ðWØG�t”{ÔGð Ø!%Ô!3ØØ $Ô 4Ô Aðð ð 'ð	ð �õ (¨¨V¬Ñ5Ô5�Ø*,Ð"Ø$ð >ð >�DÝœ: dÑ+Ô+�DØ"&§(¢(¨9Ñ"5Ô"5°aÔ"8�KØÐ'å&9¸+ÀtÑ&LÔ&L˜Ø&¨+Ñ5Ð&Ø×0Ò0°Ñ=Ô=Ð=Ð=Ø)Ð)øÝð Wð Wð WÝ Ð!UÐRSÐ!UÐ!UÑVÔVÐVøøøøðWøøøð	MØ6˜4œ;Ô6ð Ø!%Ô!3ØØ ,Ø"ð	ð ð
 'ðð ��øõ ð Mð Mð MÝ Ð!KÈÐ!KÐ!KÑLÔLÐLøøøøðMøøøð Ô'×8Ò8¸À&Ô9IÑJÔJˆDØÐõ +¨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]   r   rj   Úpropertyr	   rm   rp   r   r   r‡   r%   r#   r!   rP   rP   ~   s  € € € € € € ðð ð"/ð` €FˆCÐÐÑØ,à€M�3ÐÐÑð,ð €K�ÐÐÑØPà.2Ð˜h sœmÐ2Ð2Ñ2ðð 'Ð&Ð&Ñ&ðð
 €IˆtÐÐÑØ(ðð& $(€L�(˜4”.Ð'Ð'Ñ'Ø1à&*€O�X˜d”^Ð*Ð*Ñ*ðð
ð ð ð ð ñ ô ð ð
 €^ÑÔð"¨$ð "°4ð "ð "ð "ñ Ôð"ðH ð
 W¨S°#¨XÔ%6ð 
ð 
ð 
ñ „Xð
ð ð$˜3ð $ð $ð $ñ „Xð$ð %)Ø:>ð	Dð DàðDð �t˜C”yÔ!ðDð Ð6Ô7ð	Dð
 ðDð 
ðDð Dð Dð Dð Dð Dr#   rP   ) r=   r   r{   Úabcr   Útypingr   r   r   r   r   r	   r
   r   r   Úlangchain_core.callbacksr   Ú#langchain_core.language_models.llmsr   Úlangchain_core.pydantic_v1r   r   Ú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�\¨¨s°D¸´I¨~Ô)>Ð?Ñ?Ô?€
Øˆg�m¨5°°d¸4À¼;Ô6GÈÐ1QÔ+RÐSÑSÔS€ð8=ð 8=ð 8=ð 8=ð 8=ñ 8=ô 8=ð 8=ðv+ð +ð +ð +ð +˜ ¨[Ð!8Ô9ñ +ô +ð +ð\)ð )ð )ð )ð )Ð*¨3°¨8Ô4ñ )ô )ð )ðuð uð uð uð u˜ñ uô uð uð uð ur#   