a
    bg3                     @   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eddddZd dddZed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/
    N)streamreturnc                 C   s   t || _t | _d| _d S )Nr   )iterbyte_iteratorioBytesIObufferread_pos)selfr    r   y/var/www/html/cobodadashboardai.evdpl.com/venv/lib/python3.9/site-packages/langchain_community/llms/sagemaker_endpoint.py__init__2   s    

zLineIterator.__init__r   c                 C   s   | S )Nr   r   r   r   r    __iter__7   s    zLineIterator.__iter__c                 C   s   | j | j | j  }|rJ|d tdkrJ|  jt|7  _|d d S zt| j}W n* ty   | j| j 	 j
k r|Y q  Y n0 d|vrq | j dtj | j |d d  q d S )N
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__)__name__
__module____qualname____doc__r   r!   r$   r4   r   r   r   r    r      s   r   c                   @   s\   e Zd ZU dZdZee ed< dZee ed< e	e
eedddZe	eed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   s   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   s    z"ContentHandlerBase.transform_input)outputr   c                 C   s   dS )z[Transforms the output from the model to string that
        the LLM class expects.
        Nr   )r   r?   r   r   r    transform_outputu   s    z#ContentHandlerBase.transform_outputN)r5   r6   r7   r8   r:   r	   str__annotations__r;   r   r   r   bytesr>   r   r@   r   r   r   r    r9   N   s   
r9   c                   @   s   e Zd ZdZdS )LLMContentHandlerzContent handler for LLM class.N)r5   r6   r7   r8   r   r   r   r    rD   |   s   rD   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eedddZeeeef dddZeedddZdeeee  ee eed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 z|ddl}z@|d dur<|j|d d}n| }|jd|d d|d< W n. ty } ztd	|W Y d}~n
d}~0 0 W n ty   td
Y n0 |S )z.Dont do anything if client provided externallyrF   Nr   rJ   )Zprofile_namezsagemaker-runtimerI   )rI   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SessionrF   	Exception
ValueErrorImportError)clsrO   rQ   sessioner   r   r    validate_environment  s0    
z&SagemakerEndpoint.validate_environmentr"   c                 C   s    | j pi }i d| jid|iS )zGet the identifying parameters.rH   r=   )r=   rH   )r   _model_kwargsr   r   r    _identifying_params'  s    
z%SagemakerEndpoint._identifying_paramsc                 C   s   dS )zReturn type of llm.Zsagemaker_endpointr   r#   r   r   r    	_llm_type0  s    zSagemakerEndpoint._llm_type)r<   stoprun_managerkwargsr   c              
   K   s~  | j pi }i ||}| jpi }| j||}| jj}| jj}	| jr|rz|| jjf | j	|| jjd|}
t
|
d }d}|D ]@}t|}
|
dd }|durt||}||7 }|| q~|W S  ty } ztd| W Y d}~n
d}~0 0 nz"| jjf | j	|||	d|}W n4 tyP } ztd	| W Y d}~n
d}~0 0 | j|d }|durvt||}|S d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ContentTypera   rG   outputsr   Nz.Error raised by streaming inference endpoint: )r`   ra   rb   Acceptz$Error raised by inference endpoint: )r=   rM   rK   r>   r:   r;   rL   rF   Z$invoke_endpoint_with_response_streamrH   r   jsonloadsrP   r   Zon_llm_new_tokenrS   rT   Zinvoke_endpointr@   )r   r<   r]   r^   r_   rZ   Z_endpoint_kwargsbodyr:   r;   respiteratorZcurrent_completionr2   Zresp_outputrX   responsetextr   r   r    _call5  sT    




&
$

zSagemakerEndpoint._call)NN)r5   r6   r7   r8   rF   r   rB   rH   rA   rI   rJ   r	   rD   rL   boolr=   r   rM   r   Zmodel_configr   rY   propertyr   r[   r\   r   r   rl   r   r   r   r    rE      s6   
A$  
rE   )#r8   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   rA   r   floatr   r   r9   rD   rE   r   r   r   r    <module>   s*   ,:.