U
    h                     @   sH   d Z ddlmZmZmZ ddlmZmZ ddlm	Z	 G dd deZ
dS )zUtil that calls Dataherald.    )AnyDictOptional)	BaseModelroot_validator)get_from_dict_or_envc                   @   sj   e Zd ZU dZeed< 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 )DataheraldAPIWrapperzWrapper for Dataherald.

    Docs for using:

    1. Go to dataherald and sign up
    2. Create an API key
    3. Save your API key into DATAHERALD_API_KEY env variable
    4. pip install dataherald

    dataherald_clientdb_connection_idNdataherald_api_keyc                   @   s   e Zd ZdZdS )zDataheraldAPIWrapper.ConfigZforbidN)__name__
__module____qualname__extra r   r   L/tmp/pip-unpacked-wheel-9gdii04g/langchain_community/utilities/dataherald.pyConfig   s   r   T)pre)valuesreturnc                 C   sV   t |dd}||d< zddl}W n tk
r<   tdY nX |j|d}||d< |S )z?Validate that api key and python package exists in environment.r   ZDATAHERALD_API_KEYr   NzLdataherald is not installed. Please install it with `pip install dataherald`)Zapi_keyr	   )r   
dataheraldImportErrorZ
Dataherald)clsr   r   r   clientr   r   r   validate_environment   s      
z)DataheraldAPIWrapper.validate_environment)promptr   c                 C   sf   ddl m} ||| jd}| jjj|d}z |j}|s<W dS d| W S W n tk
r`   Y dS X dS )	z9Generate a sql query through Dataherald and parse result.r   )Prompt)textr
   )r   z	No answerzAnswer: z#Dataherald wasn't able to answer itN)Z-dataherald.types.sql_generation_create_paramsr   r
   r	   Zsql_generationscreateZsqlStopIteration)selfr   r   Z
prompt_objresZanswerr   r   r   run2   s    zDataheraldAPIWrapper.run)r   r   r   __doc__r   __annotations__strr   r   r   r   r   r   r"   r   r   r   r   r   	   s   
r   N)r#   typingr   r   r   Zlangchain_core.pydantic_v1r   r   Zlangchain_core.utilsr   r   r   r   r   r   <module>   s   