
    h                         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      Z G d	 d
e
      Zy)    )AnyDictListMappingOptionalN)CallbackManagerForLLMRun)LLM)
ConfigDict)enforce_stop_tokensc            	       \    e Zd ZdZededeeef   deeef   fd       Zededefd       Z	y)	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                     ||dS )N)inputs
parameters )clsr   r   s      i/var/www/html/eduruby.in/venv/lib/python3.12/site-packages/langchain_community/llms/amazon_api_gateway.pytransform_inputz.ContentHandlerAmazonAPIGateway.transform_input   s     !==    responsec                 .    |j                         d   d   S )Nr   generated_text)json)r   r   s     r   transform_outputz/ContentHandlerAmazonAPIGateway.transform_output   s    }}q!"233r   N)
__name__
__module____qualname____doc__classmethodstrr   r   r   r   r   r   r   r   r      se    3 >>(,S#X>	c3h> >
 4 4 4 4r   r   c                       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      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)AmazonAPIGatewayz6Amazon API Gateway to access LLM models hosted on AWS.api_urlNheadersr   content_handlerforbid)extrar   c                 `    | j                   xs i }i | j                  | j                  dd|iS )zGet the identifying parameters.)r&   r'   r   )r   r&   r'   )self_model_kwargss     r   _identifying_paramsz$AmazonAPIGateway._identifying_params3   s@     ))/R
,,4<<@
}-
 	
r   c                      y)zReturn type of llm.amazon_api_gatewayr   )r,   s    r   	_llm_typezAmazonAPIGateway._llm_type<   s     $r   r   stoprun_managerkwargsc                 H   | j                   xs i }| j                  j                  ||      }	 t        j                  | j
                  | j                  |      }| j                  j                  |      }|t        ||      }|S # t        $ r}	t        d|	       d}	~	ww xY w)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   r2   r3   r4   r-   payloadr   texterrors
             r   _callzAmazonAPIGateway._callA   s    * ))/R&&66v}M		F}}H
 ''88BD
 &tT2D  	F<UGDEE	Fs   AB 	B!BB!)NN)r   r   r    r!   r#   __annotations__r'   r   r   r   r   r(   r
   model_configpropertyr   r   r.   r1   r   r   r=   r   r   r   r%   r%      s    @L"GXd^"C#'L(4.'16T6VO3V
 L 
WS#X%6 
 
 $3 $ $ %):>	&& tCy!& 67	&
 & 
&r   r%   )typingr   r   r   r   r   r6   langchain_core.callbacksr   #langchain_core.language_models.llmsr	   pydanticr
   langchain_community.llms.utilsr   r   r%   r   r   r   <module>rF      s2    5 5  = 3  >4 4$Js Jr   