a
    bg                     @   sv   d dl mZmZmZmZmZ d dlZd dlmZ d dl	m
Z
 d dlmZ d dlmZ G dd dZG d	d
 d
e
ZdS )    )AnyDictListMappingOptionalN)CallbackManagerForLLMRun)LLM)
ConfigDict)enforce_stop_tokensc                   @   sJ   e Zd ZdZeeeeef eeef dddZeeedddZ	dS )	ContentHandlerAmazonAPIGatewayzAdapter to prepare the inputs from Langchain to a format
    that LLM model expects.

    It also provides helper function to extract
    the generated text from the model response.)promptmodel_kwargsreturnc                 C   s
   ||dS )N)inputs
parameters )clsr   r   r   r   y/var/www/html/cobodadashboardai.evdpl.com/venv/lib/python3.9/site-packages/langchain_community/llms/amazon_api_gateway.pytransform_input   s    z.ContentHandlerAmazonAPIGateway.transform_input)responser   c                 C   s   |  d d S )Nr   Zgenerated_text)json)r   r   r   r   r   transform_output   s    z/ContentHandlerAmazonAPIGateway.transform_outputN)
__name__
__module____qualname____doc__classmethodstrr   r   r   r   r   r   r   r   r      s   
r   c                   @   s   e Zd ZU dZeed< dZee ed< dZ	ee ed< e
 Ze
ed< e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 )AmazonAPIGatewayz6Amazon API Gateway to access LLM models hosted on AWS.api_urlNheadersr   content_handlerZforbid)extra)r   c                 C   s$   | j pi }i | j| jdd|iS )zGet the identifying parameters.)r   r    r   )r   r   r    )self_model_kwargsr   r   r   _identifying_params3   s    
z$AmazonAPIGateway._identifying_paramsc                 C   s   dS )zReturn type of llm.Zamazon_api_gatewayr   )r#   r   r   r   	_llm_type<   s    zAmazonAPIGateway._llm_type)r   stoprun_managerkwargsr   c           
   
   K   s   | j pi }| j||}z$tj| j| j|d}| j|}W n2 tyn }	 zt	d|	 W Y d}	~	n
d}	~	0 0 |durt
||}|S )ab  Call out to Amazon API Gateway model.

        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.")
        )r    r   zError raised by the service: N)r   r!   r   requestspostr   r    r   	Exception
ValueErrorr
   )
r#   r   r'   r(   r)   r$   payloadr   texterrorr   r   r   _callA   s    
$
zAmazonAPIGateway._call)NN)r   r   r   r   r   __annotations__r    r   r   r   r   r!   r	   Zmodel_configpropertyr   r   r%   r&   r   r   r1   r   r   r   r   r      s*   
  
r   )typingr   r   r   r   r   r*   Zlangchain_core.callbacksr   Z#langchain_core.language_models.llmsr   Zpydanticr	   Zlangchain_community.llms.utilsr
   r   r   r   r   r   r   <module>   s   