
    Ng                        d Z ddlmZmZmZ ddlmZmZmZm	Z	m
Z
 ddlmZ ddlmZmZ ddlmZ ddlmZ ddlmZ dd	lmZ  G d
 de          Z G d dee          Z G d dee          Z G d dee          Z G d dee          ZdS )z%Tools for interacting with Spark SQL.    )AnyDictOptional)	BaseModelFieldroot_validatormodel_validator
ConfigDict)BaseLanguageModel)AsyncCallbackManagerForToolRunCallbackManagerForToolRun)PromptTemplate)SparkSQL)BaseTool)QUERY_CHECKERc                   N    e Zd ZU dZ ed          Zeed<    ed          Z	dS )BaseSparkSQLToolz)Base tool for interacting with Spark SQL.T)excludedb)arbitrary_types_allowedN)
__name__
__module____qualname____doc__r   r   r   __annotations__r
   model_config     d/var/www/html/ai-engine/env/lib/python3.11/site-packages/langchain_community/tools/spark_sql/tool.pyr   r      sL         335&&&B&&&: $  LLLr   r   c                   T    e Zd ZU dZdZeed<   dZeed<   	 ddedee	         d	efd
Z
dS )QuerySparkSQLToolzTool for querying a Spark SQL.query_sql_dbnamez
    Input to this tool is a detailed and correct SQL query, output is a result from the Spark SQL.
    If the query is not correct, an error message will be returned.
    If an error is returned, rewrite the query, check the query, and try again.
    descriptionNqueryrun_managerreturnc                 6    | j                             |          S )z:Execute the query, return the results or an error message.)r   run_no_throwselfr%   r&   s      r   _runzQuerySparkSQLTool._run'   s     w##E***r   Nr   r   r   r   r#   strr   r$   r   r   r,   r   r   r   r!   r!      s         ((D#K    <@+ ++ 78+ 
	+ + + + + +r   r!   c                   T    e Zd ZU dZdZeed<   dZeed<   	 ddedee	         d	efd
Z
dS )InfoSparkSQLToolz,Tool for getting metadata about a Spark SQL.schema_sql_dbr#   z
    Input to this tool is a comma-separated list of tables, output is the schema and sample rows for those tables.
    Be sure that the tables actually exist by calling list_tables_sql_db first!

    Example Input: "table1, table2, table3"
    r$   Ntable_namesr&   r'   c                 \    | j                             |                    d                    S )z4Get the schema for tables in a comma-separated list., )r   get_table_info_no_throwsplit)r+   r3   r&   s      r   r,   zInfoSparkSQLTool._run;   s(     w..{/@/@/F/FGGGr   r-   r.   r   r   r   r1   r1   0   s         66D#K    <@H HH 78H 
	H H H H H Hr   r1   c                   V    e Zd ZU dZdZeed<   dZeed<   	 	 dded	ee	         d
efdZ
dS )ListSparkSQLToolzTool for getting tables names.list_tables_sql_dbr#   zVInput is an empty string, output is a comma separated list of tables in the Spark SQL.r$    N
tool_inputr&   r'   c                 Z    d                     | j                                                  S )z$Get the schema for a specific table.r5   )joinr   get_usable_table_names)r+   r<   r&   s      r   r,   zListSparkSQLTool._runJ   s$     yy7799:::r   )r;   Nr.   r   r   r   r9   r9   D   s         (($D#$$$oKooo ;?; ;; 78; 
	; ; ; ; ; ;r   r9   c                      e Zd ZU dZeZeed<   eed<    e	d          Z
eed<   dZeed<   d	Zeed
<    ed          edeeef         defd                        Z	 ddedee         defdZ	 ddedee         defdZdS )QueryCheckerToolzUse an LLM to check if a query is correct.
    Adapted from https://www.patterns.app/blog/2023/01/18/crunchbot-sql-analyst-gpt/templatellmF)init	llm_chainquery_checker_sql_dbr#   z
    Use this tool to double check if your query is correct before executing it.
    Always use this tool before executing a query with query_sql_db!
    r$   before)modevaluesr'   c                     d|vr>ddl m}  ||                    d          t          t          dg                    |d<   |d         j        j        dgk    rt          d          |S )	NrE   r   )LLMChainrC   r%   )rB   input_variables)rC   promptz_LLM chain for QueryCheckerTool need to use ['query'] as input_variables for the embedded prompt)langchain.chains.llmrK   getr   r   rM   rL   
ValueError)clsrI   rK   s      r   initialize_llm_chainz%QueryCheckerTool.initialize_llm_chain`   s     f$$555555"*(JJu%%%*WI  # # #F; +%5'BB*  
 r   Nr%   r&   c                 f    | j                             ||r|                                nd          S )zUse the LLM to check the query.Nr%   	callbacks)rE   predict	get_childr*   s      r   r,   zQueryCheckerTool._runu   s>     ~%%k#S;#8#8#:#:#:t & 
 
 	
r   c                 v   K   | j                             ||r|                                nd            d {V S )NrT   )rE   apredictrW   r*   s      r   _arunzQueryCheckerTool._arun   s`      
 ^,,k#S;#8#8#:#:#:t - 
 
 
 
 
 
 
 
 	
r   r-   )r   r   r   r   r   rB   r/   r   r   r   rE   r   r#   r$   r	   classmethodr   rR   r   r   r,   r   rZ   r   r   r   rA   rA   S   sV        X X "Hc!!!	U&&&Is&&&&D#&&&K   
 _(###$sCx. S    [ $#, <@
 

 78
 
	
 
 
 
 AE
 

 <=
 
	
 
 
 
 
 
r   rA   N)r   typingr   r   r   pydanticr   r   r   r	   r
   langchain_core.language_modelsr   langchain_core.callbacksr   r   langchain_core.promptsr   'langchain_community.utilities.spark_sqlr   langchain_core.toolsr   *langchain_community.tools.spark_sql.promptr   r   r!   r1   r9   rA   r   r   r   <module>rd      s   + + & & & & & & & & & & R R R R R R R R R R R R R R < < < < < <        2 1 1 1 1 1 < < < < < < ) ) ) ) ) ) D D D D D D    y   + + + + +(( + + +&H H H H H' H H H(; ; ; ; ;' ; ; ;3
 3
 3
 3
 3
' 3
 3
 3
 3
 3
r   