
    Ng                    T   d Z ddlmZ ddlZddlmZmZmZmZm	Z	m
Z
mZ ddlmZ ddlmZ ddlmZmZmZ ddlmZ erdd	lmZ  G d
 de          Z G d de          Z G d dee          Z G d de          Z G d dee          Z G d de          Z G d dee          ZdS )z8Tools for interacting with an Apache Cassandra database.    )annotationsN)TYPE_CHECKINGAnyDictOptionalSequenceTypeUnion)CallbackManagerForToolRun)BaseTool)	BaseModel
ConfigDictField)CassandraDatabase)	ResultSetc                  N    e Zd ZU dZ ed          Zded<    ed          ZdS )BaseCassandraDatabaseToolz<Base tool for interacting with an Apache Cassandra database.T)excluder   db)arbitrary_types_allowedN)	__name__
__module____qualname____doc__r   r   __annotations__r   model_config     m/var/www/html/ai-engine/env/lib/python3.11/site-packages/langchain_community/tools/cassandra_database/tool.pyr   r      sM         FF!E$///B////: $  LLLr   r   c                  4    e Zd ZU  edd          Zded<   dS ) _QueryCassandraDatabaseToolInput.z!A detailed and correct CQL query.descriptionstrqueryN)r   r   r   r   r%   r   r   r   r   r!   r!      s3         s(KLLLELLLLLLr   r!   c                  J    e Zd ZU dZdZded<   dZded<   eZded<   	 dddZ	d	S )QueryCassandraDatabaseToolzATool for querying an Apache Cassandra database with provided CQL.cassandra_db_queryr$   namez
    Execute a CQL query against the database and get back the result.
    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.
    r#   Type[BaseModel]args_schemaNr%   run_manager#Optional[CallbackManagerForToolRun]return/Union[str, Sequence[Dict[str, Any]], ResultSet]c                    	 | j                             |          S # t          $ r$}	 d| dt          j                     cY d}~S d}~ww xY w)z:Execute the query, return the results or an error message.Error: 
N)r   run	Exception	traceback
format_exc)selfr%   r,   es       r   _runzQueryCassandraDatabaseTool._run+   sk    	;7;;u%%% 	; 	; 	;*:Q::)"6"8"8::::::::	;s    
A
AA
A
N)r%   r$   r,   r-   r.   r/   )
r   r   r   r   r)   r   r#   r!   r+   r9   r   r   r   r'   r'       s{         KK$D$$$$K    
 $DKCCCC
 <@
; 
; 
; 
; 
; 
; 
;r   r'   c                  4    e Zd ZU  edd          Zded<   dS )$_GetSchemaCassandraDatabaseToolInput.z8The name of the keyspace for which to return the schema.r"   r$   keyspaceN)r   r   r   r   r=   r   r   r   r   r<   r<   8   sF         EO  H      r   r<   c                  J    e Zd ZU dZdZded<   dZded<   eZded<   	 dddZ	d	S )GetSchemaCassandraDatabaseToolzJTool for getting the schema of a keyspace in an Apache Cassandra database.cassandra_db_schemar$   r)   a  
    Input to this tool is a keyspace name, output is a table description 
    of Apache Cassandra tables.
    If the query is not correct, an error message will be returned.
    If an error is returned, report back to the user that the keyspace 
    doesn't exist and stop.
    r#   r*   r+   Nr=   r,   r-   r.   c                    	 | j                             |          }d                    d |D                       S # t          $ r$}	 d| dt	          j                     cY d}~S d}~ww xY w)zGet the schema for a keyspace. c                <    g | ]}|                                 d z   S )z

)as_markdown).0tables     r   
<listcomp>z7GetSchemaCassandraDatabaseTool._run.<locals>.<listcomp>U   s)    MMMUE--//&8MMMr   r1   r2   N)r   get_keyspace_tablesjoinr4   r5   r6   )r7   r=   r,   tablesr8   s        r   r9   z#GetSchemaCassandraDatabaseTool._runM   s    	;W00::F77MMfMMMNNN 	; 	; 	;*:Q::)"6"8"8::::::::	;s   8; 
A)A$A)$A)r:   )r=   r$   r,   r-   r.   r$   )
r   r   r   r   r)   r   r#   r<   r+   r9   r   r   r   r?   r?   ?   s{         TT%D%%%%K     $HKGGGG
 <@; ; ; ; ; ; ;r   r?   c                      e Zd ZU  edd          Zded<    edd          Zded<    edd          Zded	<    edd
          Zded<   dS )'_GetTableDataCassandraDatabaseToolInput.z.The name of the keyspace containing the table.r"   r$   r=   z/The name of the table for which to return data.rF   z6The predicate for the query that uses the primary key.	predicatez%The maximum number of rows to return.intlimitN)	r   r   r   r   r=   r   rF   rM   rO   r   r   r   rL   rL   [   s         EE  H     F  E     UM  I     <  E      r   rL   c                  J    e Zd ZU dZdZded<   dZded<   eZded<   	 dddZ	d	S )!GetTableDataCassandraDatabaseToolaO  
    Tool for getting data from a table in an Apache Cassandra database.
    Use the WHERE clause to specify the predicate for the query that uses the
    primary key. A blank predicate will return all rows. Avoid this if possible.
    Use the limit to specify the number of rows to return. A blank limit will
    return all rows.
    cassandra_db_select_table_datar$   r)   aS  
    Tool for getting data from a table in an Apache Cassandra database. 
    Use the WHERE clause to specify the predicate for the query that uses the 
    primary key. A blank predicate will return all rows. Avoid this if possible. 
    Use the limit to specify the number of rows to return. A blank limit will 
    return all rows.
    r#   r*   r+   Nr=   rF   rM   rO   rN   r,   r-   r.   c                    	 | j                             ||||          S # t          $ r$}	 d| dt          j                     cY d}~S d}~ww xY w)z$Get data from a table in a keyspace.r1   r2   N)r   get_table_datar4   r5   r6   )r7   r=   rF   rM   rO   r,   r8   s          r   r9   z&GetTableDataCassandraDatabaseTool._run   ss    	;7))(E9eLLL 	; 	; 	;*:Q::)"6"8"8::::::::	;s    
AAAAr:   )r=   r$   rF   r$   rM   r$   rO   rN   r,   r-   r.   r$   )
r   r   r   r   r)   r   r#   rL   r+   r9   r   r   r   rQ   rQ   n   s           1D0000K     $KKJJJJ <@; ; ; ; ; ; ;r   rQ   )r   
__future__r   r5   typingr   r   r   r   r   r	   r
   langchain_core.callbacksr   langchain_core.toolsr   pydanticr   r   r   0langchain_community.utilities.cassandra_databaser   cassandra.clusterr   r   r!   r'   r<   r?   rL   rQ   r   r   r   <module>r\      s   > > " " " " " "     L L L L L L L L L L L L L L L L L L > > > > > > ) ) ) ) ) ) 1 1 1 1 1 1 1 1 1 1 N N N N N N ,++++++    	   M M M M My M M M; ; ; ; ;!:H ; ; ;0    9   ; ; ; ; ;%> ; ; ;8    i   & ;  ;  ;  ;  ;(A8  ;  ;  ;  ;  ;r   