o
    Zh3                     @   s*  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 dZedeee
e
e  e	f dZG dd dZG dd deeef Z G dd de eef Z!eddddG d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                   @   s:   e Zd ZdZdeddfddZdddZdefd	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                 C   s   t || _t | _d| _d S )Nr   )iterbyte_iteratorioBytesIObufferread_pos)selfr    r   b/var/www/html/lang_env/lib/python3.10/site-packages/langchain_community/llms/sagemaker_endpoint.py__init__2   s   


zLineIterator.__init__c                 C   s   | S )Nr   r   r   r   r    __iter__7   s   zLineIterator.__iter__c                 C   s   	 | j | j | j  }|r&|d tdkr&|  jt|7  _|d d S zt| j}W n ty@   | j| j 	 j
k r?Y q  w d|vrFq | j dtj | j |d d  q)NT
ZPayloadPartr   Bytes)r   seekr   readlineordlennextr   StopIteration	getbuffernbytesr   SEEK_ENDwrite)r   linechunkr   r   r    __next__:   s$   
zLineIterator.__next__)r   r   )__name__
__module____qualname____doc__r   r!   r#   r3   r   r   r   r    r      s
    
r   c                   @   sh   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d	Ze	d
edefd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                 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   r;   r<   r   r   r    transform_inputm       z"ContentHandlerBase.transform_inputoutputc                 C   r=   )z[Transforms the output from the model to string that
        the LLM class expects.
        Nr   )r   r@   r   r   r    transform_outputu   r?   z#ContentHandlerBase.transform_outputN)r4   r5   r6   r7   r9   r	   str__annotations__r:   r   r   r   bytesr>   r   rA   r   r   r   r    r8   N   s   
 r8   c                   @   s   e Zd ZdZdS )LLMContentHandlerzContent handler for LLM class.N)r4   r5   r6   r7   r   r   r   r    rE   |   s    rE   z0.3.16z1.0z$langchain_aws.llms.SagemakerEndpoint)ZsinceZremovalZalternative_importc                   @   s
  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dZededefddZedeeef fddZedefddZ		ddedeee  dee dedef
d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	streamingr<   endpoint_kwargsZforbid)extravaluesr   c              
   C   s   | ddur	|S 	 z8ddl}z"|d dur|j|d d}n| }|jd|d d|d< W W |S  tyB } ztd	|d}~ww  tyM   td
w )z.Dont do anything if client provided externallyrG   Nr   rK   )Zprofile_namezsagemaker-runtimerJ   )rJ   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SessionrG   	Exception
ValueErrorImportError)clsrP   rR   sessioner   r   r    validate_environment  s8   z&SagemakerEndpoint.validate_environmentc                 C   s    | j pi }i d| jid|iS )zGet the identifying parameters.rI   r<   )r<   rI   )r   _model_kwargsr   r   r    _identifying_params'  s   
z%SagemakerEndpoint._identifying_paramsc                 C   r=   )zReturn type of llm.Zsagemaker_endpointr   r"   r   r   r    	_llm_type0  s   zSagemakerEndpoint._llm_typer;   stoprun_managerkwargsc              
   K   s\  | j pi }i ||}| jpi }| j||}| jj}| jj}	| jrv|rvz>| jjd
| j	|| jjd|}
t
|
d }d}|D ] }t|}
|
dd }|durVt||}||7 }|| q?|W S  tyu } ztd| d}~ww z| jjd
| j	|||	d|}W n ty } ztd	| d}~ww | j|d }|durt||}|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ContentTyperb   rH   Zoutputsr   Nz.Error raised by streaming inference endpoint: )ra   rb   rc   Acceptz$Error raised by inference endpoint: r   )r<   rN   rL   r>   r9   r:   rM   rG   Z$invoke_endpoint_with_response_streamrI   r   jsonloadsrQ   r   Zon_llm_new_tokenrT   rU   Zinvoke_endpointrA   )r   r;   r^   r_   r`   r[   Z_endpoint_kwargsbodyr9   r:   respiteratorZcurrent_completionr1   Zresp_outputrY   responsetextr   r   r    _call5  s\   






zSagemakerEndpoint._call)NN)r4   r5   r6   r7   rG   r   rC   rI   rB   rJ   rK   r	   rE   rM   boolr<   r   rN   r   Zmodel_configr   rZ   propertyr   r\   r]   r   r   rl   r   r   r   r    rF      sT   
 0$
rF   )#r7   r   re   abcr   typingr   r   r   r   r   r   r	   r
   r   Zlangchain_core._api.deprecationr   Zlangchain_core.callbacksr   Z#langchain_core.language_models.llmsr   Zlangchain_core.utilsr   Zpydanticr   Zlangchain_community.llms.utilsr   rB   r   floatr   r   r8   rE   rF   r   r   r   r    <module>   s,    ,:.