a
    bg                     @   sL   d Z ddlmZmZmZ ddlmZ ddlmZm	Z	m
Z
 G dd deZdS )zUtil that calls Dataherald.    )AnyDictOptional)get_from_dict_or_env)	BaseModel
ConfigDictmodel_validatorc                   @   sn   e Zd ZU dZdZeed< eed< dZe	e ed< e
ddZedd	e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

    Ndataherald_clientdb_connection_iddataherald_api_keyZforbid)extrabefore)mode)valuesreturnc                 C   sT   t |dd}||d< zddl}W n ty:   tdY n0 |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   client r   v/var/www/html/cobodadashboardai.evdpl.com/venv/lib/python3.9/site-packages/langchain_community/utilities/dataherald.pyvalidate_environment   s    
z)DataheraldAPIWrapper.validate_environment)promptr   c                 C   sd   ddl m} ||| jd}| jjj|d}z |j}|s<W dS d| W S W n ty^   Y dS 0 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createsqlStopIteration)selfr   r   Z
prompt_objresZanswerr   r   r   run4   s    zDataheraldAPIWrapper.run)__name__
__module____qualname____doc__r
   r   __annotations__strr   r   r   Zmodel_configr   classmethodr   r   r!   r   r   r   r   r	   	   s   
r	   N)r%   typingr   r   r   Zlangchain_core.utilsr   Zpydanticr   r   r   r	   r   r   r   r   <module>   s   