o
    Zh                     @   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                   @   sr   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d
edefddZdedef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   sP   t |dd}||d< zddl}W n ty   tdw |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   _/var/www/html/lang_env/lib/python3.10/site-packages/langchain_community/utilities/dataherald.pyvalidate_environment   s   z)DataheraldAPIWrapper.validate_environmentpromptc                 C   s\   ddl m} ||| jd}| jjj|d}z|j}|sW dS d| W S  ty-   Y dS w )z9Generate a sql query through Dataherald and parse result.r   )Prompt)textr   )r   z	No answerzAnswer: z#Dataherald wasn't able to answer it)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
    