Team Ai
Apppublic

Tsah00/sql-env

sourceHugging Facemitupdated 6mo agoView on Hugging Face
0likes
sql_environment.cpython-314.pyc99 linesDownload Raw Back to __pycache__
1+
2(D�i���v�Rt^RIHt^RIt^RIt^RIHt^RIHtH	t	H3t4^RIHtH
t
HtHt^t!RR4tR#)a�5server/sql_environment.py - Core SQL Query Learning Environment.6 7Implements the OpenEnv Environment interface with:8  - reset(): Initialize a fresh episode with a SQLite database9  - step(action): Execute an agent SQL query, grade it, return observation10  - state: Current episode metadata11 12The environment hosts an in-memory SQLite database seeded with e-commerce13data across 4 tables (customers, products, orders, order_items).14)�annotationsN)�Optional)�	SQLAction�SQLObservation�SQLState)�TASKS�SCHEMA_INFO�grade�
seed_databasec�p�]tRt^tRtRRltRRRlltRRlt]R	R15l4t	RRlt16R
RltRtR#)�SQLEnvironmenta�17SQL Query Learning Environment.18 19At each step the agent submits a SQL query string. The environment:20  1. Executes the query against the local SQLite database.21  2. Compares the result against the reference answer for the active task.22  3. Returns an observation with the result, reward, and feedback.23 24Episode flow:25  reset(difficulty='easy'|'medium'|'hard') -> SQLObservation26  step(SQLAction)                          -> SQLObservation27  state                                    -> SQLState28c��V^8�dQhRR/#���return�None�)�formats"�?/home/Tejesh/Documents/meta_hackathon/server/sql_environment.py�__annotate__�SQLEnvironment.__annotate__*s��	"�	"�$�	"�c	�t�RVn\4VnRVnR.ROR.ROR.RO/Vn^VnR#)N�easy�medium�hard)�easy_1�easy_2�easy_3)�medium_1�medium_2�medium_3)�hard_1�hard_2�hard_3)�_connr�_state�
_current_task�_task_ids_by_difficulty�_task_index��selfs&r�__init__�SQLEnvironment.__init__*s@��37��29��j���-1����4��:��4�(30��$�31!"��rNc�$�V^8�dQhRRRRRR/#)r�32difficulty�str�task_idz
Optional[str]rrr)rs"rrr9s"��633�634��635�}�636�P^�637rc

���VP'dVPP4\P!R4Vn\PVPn\
VP4V'dV\9d\V,VnM=VPPVR.4p^Vn\V^,,Vn\\\P!44^VPR,VPR,R^RRRR7	Vn\#.RRRR	\$VPR38,VPR,^/R739#)
z�40Start a new episode.41 42Args:43    difficulty: Task difficulty tier ('easy', 'medium', 'hard').44    task_id: Optional specific task ID to start with.45 46Returns:47    Initial SQLObservation with schema info and task description.48z:memory:r�idr/��)	�49episode_id�50step_count�current_task_id�current_difficulty�total_reward�tasks_completed�best_reward�51last_query�52last_errorFz�Episode started. Write a SQL query to complete the task.53You can explore the schema using: SELECT name FROM sqlite_master WHERE type='table';�description�expected_columns�54�result�error�reward�done�message�schema_info�task_descriptionr@r7�score_breakdown)r%�close�sqlite3�connect�Row�row_factoryr55rr'r(�getr)rr0�uuid�uuid4r&rr)r+r/r1�idss&&& r�reset�SQLEnvironment.reset9s&���:�:�:��J�J�����_�_�Z�0��56�!(����57�58���d�j�j�!��w�%�'�!&�w��D���.�.�2�2�:��z�J�C� �D��!&�s�1�v��D����4�:�:�<�(�� �.�.�t�4�#�1�1�,�?������5960��������E�$�!�/�/�
�>�!�/�/�0B�C���61�	62rc� �V^8�dQhRRRR/#)r�actionrrrr)rs"rrrqs��K63�K64�9�K65��K66rc

��VPeVPf\RRRRR7#VP;P^,
unVP67VPnVP;'gVPPpVP'd�VPVPP8wd�VPPVPR.4pV^,p\P!W P4VnW PnVPVPn68\W!P69VP4wrErgVP;PV,
unW@PP 8�dW@PnRpVR8�dIVP;P"^,
unVR	,
pVP%4p	V	'gRpR70pVPP\&8�dRpVR\&R2,
pR
VPnRV^8�d%\+VR,\-VR4,^4MRRRR\+V\/VR,R4,71^4/p72\TR
TTT\0V'gVPR,MRV'gVPR,M.VPPV73R774#)z�75Execute a SQL query and return graded observation.76 77Args:78    action: SQLAction with the query string to execute.79 80Returns:81    SQLObservation with result rows, reward, and feedback.82z0Environment not initialised. Call reset() first.r4Tz$Call reset() to start a new episode.)rCrDrErFrFg�?z Moving to next task...z"All tasks completed! Episode done.z Episode ended (max z steps reached).r5�correctnessgffffff�?g����MbP?�
keyword_bonusg�������?�efficiency_bonusr?zEpisode complete.r@rA)r%r'rr&r7�queryr=r1r8r/r9r(rOrr	r:r<r;�
_advance_task�	MAX_STEPSr>�round�max�minr)r+rVr1rRrDrF�83agent_rows�
expected_rowsrE�advancedrIs&&         r�step�SQLEnvironment.stepqso���:�:���!3�!3�!;�!�H���>�	�
�	
�����!�#��!'��������.�.�?�?�D�K�K�$?�$?�������!2�!2�d�k�k�6T�6T�!T��.�.�2�2�6�3D�3D�x�j�Q�C��!�f�G�!&���7�4F�4F�!G�D��*1�K�K�'�-3�->�->�D�K�K�*�6;��\�\�4�:�:�684�2���85	
��� � �F�*� ��K�K�+�+�+�&,�K�K�#����S�=��K�K�'�'�1�,�'��0�0�G��)�)�+�H����>���;�;�!�!�Y�.��D��-�i�[�8H�I�I�G�!#������&�ST�*�5��#���F�E�0B�!B�A�F�Z]��S���f�s�6�C�<��/E�&E�q� I�86��������#�FJ�T�/�/�
�>�Pc�KO�T�/�/�0B�C�UW��{�{�-�-�+�87�	88rc��V^8�dQhRR/#)rrrr)rs"rrr�s����x�rc
��VP#)zReturn current episode state.)r&r*s&r�state�SQLEnvironment.state�s���{�{�rc��V^8�dQhRR/#)rr�boolr)rs"rrr�s����t�rc
�<�VPPpVPPV.4pV;P^,
unVP\V48d7W P,p\V,VnW0PnR#R#)zq89Move to the next task in the current difficulty tier.90 91Returns True if advanced, False if no more tasks remain.92TF)	r&r9r(rOr)�lenrr'r8)r+r/rR�next_ids&   rr\�SQLEnvironment._advance_task�sx���[�[�3�3�93��*�*�.�.�z�2�>�����A������c�#�h�&��*�*�+�G�!&�w��D��*1�K�K�'��rc��V^8�dQhRR/#rr)rs"rrr�s����t�rc
�p�VP'd$VPP4RVnR#R#)zClose the SQLite connection.N)r%rJr*s&rrJ�SQLEnvironment.close�s'���:�:�:��J�J�����D�J�r)r%r'r&r(r))rN)
�__name__�94__module__�__qualname__�__firstlineno__�__doc__r,rSrd�propertyrhr\rJ�__static_attributes__rrrrrs<���	"�695�pK96�Z����� �rr)rw�97__future__rrKrP�typingr�modelsrrr�server.tasksrrr	r98r]rrrr�<module>r~s7��99�#����6�6�A�A�
�	�@�@r