§
    šŠtjö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 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     úi/var/www/html/CA-Chatbot/venv/lib/python3.11/site-packages/langchain_community/llms/sagemaker_endpoint.pyÚ__init__zLineIterator.__init__2   s(   € Ý! &™\œ\ˆÔÝ”j‘l”lˆŒØˆŒˆˆó    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¨¡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(   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 )ÚContentHandlerBasezÙ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!   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”-Ð.Ð.Ñ.Ø<à)€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"   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   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ÚSessionrU   Ú	ExceptionÚ
ValueErrorÚImportError)Úclsr_   rc   Ú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.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˜=Ð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>   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ë  € € € € € € ðð ð"/ð` €FˆCÐÐÑØ,à€M�3ÐÐÑð,ð €K�ÐÐÑØPà.2Ð˜h sœmÐ2Ð2Ñ2ðð 'Ð&Ð&Ñ&ðð
 €IˆtÐÐÑØ(ðð& $(€L�(˜4”.Ð'Ð'Ñ'Ø1à&*€O�X˜d”^Ð*Ð*Ñ*ðð
 �:Øðñ ô €Lð ð"¨$ð "°4ð "ð "ð "ñ „Xð"ð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$   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�\¨¨s°D¸´I¨~Ô)>Ð?Ñ?Ô?€
Øˆg�m¨5°°d¸4À¼;Ô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$   