U
    h	                     @   sD   d Z ddlZddlmZmZmZ ddlmZmZ G dd deZ	dS )zUtil that calls Lambda.    N)AnyDictOptional)	BaseModelroot_validatorc                   @   s   e Zd ZU dZeed< dZee ed< dZ	ee ed< dZ
ee ed< G dd dZed	d
eedddZeedddZdS )LambdaWrappera7  Wrapper for AWS Lambda SDK.
    To use, you should have the ``boto3`` package installed
    and a lambda functions built from the AWS Console or
    CLI. Set up your AWS credentials with ``aws configure``

    Example:
        .. code-block:: bash

            pip install boto3

            aws configure

    lambda_clientNfunction_nameawslambda_tool_nameawslambda_tool_descriptionc                   @   s   e Zd ZdZdS )zLambdaWrapper.ConfigZforbidN)__name__
__module____qualname__extra r   r   K/tmp/pip-unpacked-wheel-9gdii04g/langchain_community/utilities/awslambda.pyConfig!   s   r   T)pre)valuesreturnc                 C   s<   zddl }W n tk
r(   tdY nX |d|d< |S )z3Validate that python package exists in environment.r   NzBboto3 is not installed. Please install it with `pip install boto3`lambdar   )boto3ImportErrorclient)clsr   r   r   r   r   validate_environment$   s    
z"LambdaWrapper.validate_environment)queryr   c                 C   s   | j j| jdtd|id}z(|d }| d}t|d }W n tk
r\   Y dS X |dksn|dkrrd	S d
| S dS )z
        Invokes the lambda function and returns the
        result.

        Args:
            query: an input to passed to the lambda
                function as the ``body`` of a JSON
                object.
        ZRequestResponsebody)ZFunctionNameZInvocationTypePayloadr   zutf-8z$Failed to parse response from LambdaN zRequest failed.zResult: )	r   Zinvoker	   jsondumpsreaddecodeloadsStopIteration)selfr   resZpayload_streamZpayload_stringZanswerr   r   r   run3   s    
zLambdaWrapper.run)r   r   r   __doc__r   __annotations__r	   r   strr
   r   r   r   r   r   r(   r   r   r   r   r   	   s   
r   )
r)   r    typingr   r   r   Zlangchain_core.pydantic_v1r   r   r   r   r   r   r   <module>   s   