Ë
    µŒjö3  ã                   ón  — 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"y)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                   ó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     úu/var/www/html/Fitness-lenito-AI-main/venv/lib/python3.12/site-packages/langchain_community/llms/sagemaker_endpoint.pyÚ__init__zLineIterator.__init__2   s"   € Ü! &›\ˆÔÜ—j‘j“lˆŒØˆ�ó    c                 ó   — | S )N© ©r!   s    r"   Ú__iter__zLineIterator.__iter__7   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__:   sî   € ØØ�K‰K×Ñ˜TŸ]™]Ô+Ø—;‘;×'Ñ'Ó)ˆDÙ˜˜R™¤C¨£IÒ-Ø—’¤ T£Ñ*•Ø˜C˜R�yÐ ðÜ˜T×/Ñ/Ó0�ð
  EÑ)àØ�K‰K×Ñ˜Q¤§¡Ô,Ø�K‰K×Ñ˜e MÑ2°7Ñ;Ô<ð! øô !ò Ø—=‘= 4§;¡;×#8Ñ#8Ó#:×#AÑ#AÒAÚØðús   Á8C Ã:DÄD)r   r   )Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r#   r(   r:   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)Ú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                  ó   — y)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                  ó   — y)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   sy   … ñðð" #/€L�(˜3‘-Ó.Ø<à)€GˆX�c‰]Ó)Ø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"   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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Ú	streamingrD   Ú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 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ð	ÛðØÐ4Ñ5ÐAØ#Ÿm™mØ%+Ð,FÑ%Gð ,ó ‘Gð
 $Ÿm™m›o�Gà#*§>¡>Ø'°V¸MÑ5Jð $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.rW   rD   )rD   rW   )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_type0  s   € ð $r$   rC   Ú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Ú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ˆØ×+Ñ+×8Ñ8ˆØ×&Ñ&×.Ñ.ˆà�>Š>™kðWØG�t—{‘{×GÑGð Ø!%×!3Ñ!3ØØ $× 4Ñ 4× AÑ Añð 'ñ	�ô (¨¨V©Ó5�Ø*,Ð"Û$�DÜŸ:™: dÓ+�DØ"&§(¡(¨9Ó"5°aÑ"8�KØÐ'ä&9¸+ÀtÓ&L˜Ø&¨+Ñ5Ð&Ø×0Ñ0°Õ=ð %ð *Ð)ð	MØ6˜4Ÿ;™;×6Ñ6ð Ø!%×!3Ñ!3ØØ ,Ø"ñ	ð
 'ñ�ð ×'Ñ'×8Ñ8¸À&Ñ9IÓJˆDØÐô +¨4°Ó6�àˆKøô) ò WÜ Ð#QÐRSÐQTÐ!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>   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   €   sX  … ñð"/ð` €FˆCÓØ,à€M�3Óð,ð €K�ÓØPà.2Ð˜h s™mÓ2ðð 'Ó&ðð
 €IˆtÓØ(ðð& $(€L�(˜4‘.Ó'Ø1à&*€O�X˜d‘^Ó*ðñ
 Øô€Lð ð"¨$ð "°4ò "ó ð"ðH ð
 W¨S°#¨XÑ%6ò 
ó ð
ð ð$˜3ò $ó ð$ð %)Ø:>ñ	DàðDð �t˜C‘yÑ!ðDð Ð6Ñ7ð	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å 6Ý =Ý 3Ý )Ý å >á�\¨¨s°D¸±I¨~Ñ)>Ô?€
Ù�m¨5°°d¸4À¹;Ñ6GÈÐ1QÑ+RÔS€÷7=ñ 7=ôt+˜ ¨[Ð!8Ñ9ô +ô\)Ð*¨3°¨8Ñ4ô )ñ Ø
ØØ=ôô
t˜ó tóñ
tr$   