CoolFace
Apppublic

Gonalb/multi_agentic_sql_generator

sourceHugging Faceupdated 2y agoView on Hugging Face
1likes
app.cpython-313.pyc73 linesDownload Raw Back to __pycache__
12�~�g�5��R�SSKrSSKrSSKrSSKJrJr SSKJ	r	 SSK3Jr SSKJ
r
 SSKJr SSKJr SSKJr SS	KJr \R,S45r\R.S\R04Sj5r\R2"S
5S5r\R2"S5S5r\S:Xagg)�N)�Dict�Any)�table_selection_agent)�sample_data_retrieval_agent)�sql_generation_agent)�!query_validation_and_optimization)�execution_agent)�init_bigquery_connection)�save_feedback_to_bigqueryc���# �[5n[RRSU5 [R"SSS9R5IShv�N [R"SSS9R5IShv�N /SQnSR
UVs/sHnS	U3PM5 sn5nUS6-
n[R"USS9R5IShv�N gN�N`s snfN7f)zInitialize the chat session.�clientul👋 Welcome to the Natural Language to SQL Query Assistant! Ask me any question about your e-commerce data.�
SQL Assistant��content�authorNz,Here are some example questions you can ask:)z'What are the top 5 products by revenue?z.How many orders were placed in the last month?z'Which customers spent the most in 2023?z4What is the average order value by product category?z7 8u• z@9 10(You can copy and paste any of these examples to try them out))r11�cl�user_session�set�Message�send�join)r
�examples�example�
examples_texts    �;/home/usuario/Escritorio/multi_agentic_sql_generator/app.py�
on_chat_startrs����&�
'�F��O�O����&�)�
�*�*�~����d�f�
�
�
�*�*�>����d�f�
�
�12�H��K�K�� J��g�4��y�!1�� J�K�M��Y�Y�M�13�*�*�����d�f�
�
�-
�
��!K�
�sB�AC(�C�*C(�>C�?C(�C!�%2C(�C&�C(�C(�!C(�messagec14��6# �URn[RRSS5nU(Ga[RRS5n[RRS5n[RRS5n[RRS5nSU3n[	UUUUU5n[RRSS5 U(a,[R"SS	S159R5IShv�N g[R"SS	S169R5IShv�N g[RRS5n[RRSU5 [R"S
S	S179R5IShv�Nn	SU	lU	R5IShv�N XS.n18[U195nURS/5n[R"S5IShv�N U(aVSn
U
SRUVs/sH	nSUS3PM sn5-
n
[R"U
S	S209R5IShv�N SU	lU	R5IShv�N [R"S5IShv�N U21RU5 [U225nSU	lU	R5IShv�N [R"S5IShv�N U23RU5 [U245nURSS5n[RRSU5 [R"SUS3S	S259R5IShv�N SU	lU	R5IShv�N [R"S5IShv�N U26RU5 [U275nURSS5n[RRSU5 [R"SUS3S	S289R5IShv�N SU	lU	R5IShv�N [R"S5IShv�N U29RU5 [U305nURS05n[!U["5(aGS U;aAURS S!5n[R"S"U3S	S319R5IShv�N gU(d,[R"S#S	S329R5IShv�N g[!US$[$5(a^UR'U5nUR)5R*nUVs/sHnUR,PM nn[.R0"UUS%9nO[.R0"U5n[R"S'S	S339R5IShv�N [R8"US(9/n[R"S)US	S*9R5IShv�N [7U5n[7UR:5n[R<"S+US,US-3[R>"S.S/S00S1S29[R>"S3S/S40S5S29/S69R5IShv�NnU(GasURS705RS/5n [RRS5n[RRS5n[RRS5n[RRS5nU S0:Xan[	UUUUS05nU(a,[R"S8S	S349R5IShv�N g[R"SS	S359R5IShv�N gU S4:Xa[[R"S9S	S369R5IShv�N [RRSS:5 [	UUUUS45 gggGNGN�GN�GNfGN*s snfGN�GN�GN�GNfGNKGN�GN�GN�GN GNGN�GNcGN3s snf![2aI [5[7US$55Vs/sHnS&U3PM37 Os snfnn[.R0"UUS%9nGN�f=fGN�GN�GNGN4GNN�![2aBn![R"S;[AU!53S	S389R5IShv�N Sn!A!gSn!A!ff=f![2ann!S<[AU!53U	lU	R5IShv�N [R"S=[AU!53S	S399R5IShv�N Sn!A!gSn!A!ff=f7f)>zHandle user messages.�awaiting_feedbackFr
�original_query�
generated_sql�
optimized_sqlz40negative: zMThanks for your detailed feedback! I've saved it to improve future responses.rrN�NThanks for your feedback! (Note: There was an issue saving it to the database)u🤔 Thinking...u!🔍 Analyzing relevant tables...)�	sql_queryr
�relevant_tables�z7I've identified these relevant tables for your query:41 42�43z- `�`u📊 Retrieving sample data...u💻 Generating SQL query...zNo SQL generatedz*Here's the SQL query I generated:44 45```sql46z47```u🔧 Optimizing the query...zNo optimized SQLz3Here's the optimized version of the query:48 49```sql50u⚙️ Executing query...�execution_result�errorzUnknown error occurredu❌ Error executing query: u8✅ Query executed successfully but returned no results.r)�columns�Column_u6✅ Query executed successfully! Here are the results:)�data�)r�elementsrzThe query returned z51 rows and z# columns.52 53Was this result helpful?�feedback_positive�value�positiveu👍 Good results)�name�payload�label�feedback_negative�negativeu👎 Not what I wanted)r�actionsr4�MThanks for your positive feedback! I've saved it to improve future responses.�`I'm sorry the results weren't what you expected. Please type your feedback about what was wrong.Tu❌ Error formatting results: u❌ Error: z4I encountered an error while processing your query: )!rrr�getrrrr�updater�sleeprrrrr	�54isinstance�dict�tuple�query�result�schemar3�pd�	DataFrame�	Exception�range�len�	Dataframer+�AskActionMessage�Action�str)"rrArr
r r!r"�feedback_details�success�thinking_msg�state�tables_stater%�tables_text�table�sample_data_state�	sql_state�optimization_state�execution_stater)�	error_msg�	query_jobrC�field�column_names�df�ir+r/�num_rows�num_cols�res�feedback_value�es"                                  r�55on_messagerc3s����
�O�O�E����+�+�,?��G������$�$�X�.�����,�,�-=�>�����+�+�O�<�
����+�+�O�<�
�(��w�/��+������56��	�����/��7���*�*�%t�~M�N�S�S�U�
U�
U�	��*�*�%u�N�O�T�T�V�
V�
V���_�_�
 �
 ��
*�F��O�O���(�%�0����,>��W�\�\�^�^�L�v�B����!�!�#�#�#�$�6��,�U�3��&�*�*�+<�b�A���h�h�q�k����U�K��4�9�9�/�%R�/���E�7�!�n�/�%R�S�S�K��*�*�[��I�N�N�P�P�P� @����!�!�#�#�#��h�h�q�k���	���\�"�7��>�� >����!�!�#�#�#��h�h�q�k���	���&�'�(��/�	�!�
�
�o�7I�J�
�	�����O�]�;��j�j�C�M�?�RW�X�"�57��$�&�	�	� >����!�!�#�#�#��h�h�q�k���	���Y��>�u�E��*�.�.��@R�S�
�	�����O�]�;��j�j�L�]�O�[`�a�"�58��$�&�	�	� ;����!�!�#�#�#��h�h�q�k���	���'�(�)�%�0��*�.�.�/A�2�F���&��-�-�'�=M�2M�(�,�,�W�6N�O�I��*�*�5�i�[�A�&���d�f�
�
�"��*�*�R�&���d�f�
�
�59U
��.�q�1�5�9�9�M�$*�L�L��$?�	�!*�!1�!1�!3�!:�!:��@F�'G��u��6061���'G� �\�\�*:�L�Q�����&6�7�B��j�j�T�*���$�&����L�L�b�1�2���j�j��h��W�\�\�^�^�^��r�7���r�z�z�?���/�/�1�(��:�h�Z�Ot�u��	�	�':�W�j�DY�at�u��	�	�':�W�j�DY�ay�z����$�&�
���%(�W�W�Y��%;�%?�%?��%H�N��_�_�0�0��:�F�%'�_�_�%8�%8�9I�%J�N�$&�O�O�$7�$7��$H�M�$&�O�O�$7�$7��$H�M�%��3�";�"�*�)�)�&�#��#�"$�*�*�6E�N]�#^�#c�#c�#e�e�e�"$�*�*�6F�O^�#_�#d�#d�#f�f�f�'�:�5� �j�j�2T�]l�m�r�r�t�t�t����+�+�,?��F�2�"�*�)�)�&��6�/�a
U�
V�_�62	$�	��&S�P�	$��	$��	�	$��	�	$��
�63
��(H��%�M�:?��DT�UV�DW�@X�:Y�"Z�:Y�Q�W�Q�C�=�:Y��"Z��"Z��\�\�*:�G�L��M���_��6e�f�t���
��j�j�<�S��V�H�E�*���$�&����
����!,�S��V�H�5����!�!�#�#�#��j�j�J�3�q�6�(�S�"�64��$�&�	�	���s��Df�`(�+f�`+�A*f�+`.�,f�1d�`1�>d�65`4�d�(`766�8/d�'`<�(d�`?�d�$a�%:d�a� d�=a�>A<d�:a�;d�a�d�7a�8A<d�4a�5d�a�d�1a�2Bd�:a�;d�?f�-d�-a �.d�2f�4c�
/a(�<a#�a(�)=c�&b>�'Ac�'c�(A?c�'c�(C+c�c�c�f�&c�?c67�c�f�,c�1c
�23c�%f�+f�.f�1d�4d�7d�?d�d�d�d�d�d�d�d�d�d�d� d�#a(�(#b;�b�b;�7c�:b;�;c�c�c�c�68c�
c�69d�2d�d�d�d�f�d�d�70f�(&f�e�7f�f	�f�f�f�fr0c���# �[RRS5n[RRS5n[RRS5n[RRS5n[UUUUS5nU(a,[R"SSS9R5IS	hv�N g	[R"S71SS9R5IS	hv�N g	N1N7f)zHandle positive feedback.r
r r!r"r2r9rrNr#)rrr;rrr)�actionr
r r!r"rNs      r�on_feedback_positiverfs�����_�_�
 �
 ��
*�F��_�_�(�(�)9�:�N��O�O�'�'��8�M��O�O�'�'��8�M�(�������G���j�j�!p�zI�J�O�O�Q�	Q�	Q��j�j�!q�{J�K�P�P�R�	R�	R�	Q�	R�s$�B9C1�;C-�<+C1�'C/�(C1�/C1r6c��# �[R"SSS9R5IShv�N [RR	SS5 [RRS5n[RRS5n[RRS	5n[RRS725n[
UUUUS5 gN�7f)zHandle negative feedback.r:rrNrTr
r r!r"r7)rrrrrr;r)rer
r r!r"s     r�on_feedback_negativerh,s����
�*�*��IX�Y�^�^�`�`�`��O�O���+�T�2�
�_�_�
 �
 ��
*�F��_�_�(�(�)9�:�N��O�O�'�'��8�M��O�O�'�'��8�M��������`�s�'C�C�B1C�__main__)�chainlitr�pandasrD�time�typingrr�agents.table_selectionr�agents.data_retrievalr�agents.sql_generationr�agents.validationr�agents.executionr	�utils.bigquery_utilsr73�utils.feedback_utilsrrrcr�action_callbackrfrh�__name__��r�<module>rys�������8�=�6�?�,�9�:����"
��"
�H���_�b�j�j�_��_�D���'�(�R�)�R�*���'�(��)��.�z��	�rx