o
    ȳg                     @   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_key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   DATAHERALD_API_KEYr   NzLdataherald is not installed. Please install it with `pip install dataherald`)api_keyr
   )r   
dataheraldImportError
Dataherald)clsr   r   r   client r   d/var/www/html/chatdoc2/venv/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)-dataherald.types.sql_generation_create_paramsr   r   r
   sql_generationscreatesqlStopIteration)selfr   r   
prompt_objresanswerr   r   r   run4   s   zDataheraldAPIWrapper.run)__name__
__module____qualname____doc__r
   r   __annotations__strr   r   r   model_configr   classmethodr   r   r)   r   r   r   r   r	   	   s   
 r	   N)r-   typingr   r   r   langchain_core.utilsr   pydanticr   r   r   r	   r   r   r   r   <module>   s
    