Spaces:
Sleeping
Sleeping
Commit ·
0e2347e
1
Parent(s): 14fb228
use openenv core base classes
Browse files- sql_env/server.py +109 -137
sql_env/server.py
CHANGED
|
@@ -1,142 +1,114 @@
|
|
| 1 |
"""
|
| 2 |
-
FastAPI HTTP wrapper for SQLCorrectionEnv.
|
| 3 |
-
|
| 4 |
-
Exposes the OpenEnv-required endpoints: /reset, /step, /state + /tasks for validator.
|
| 5 |
"""
|
| 6 |
-
|
| 7 |
-
from
|
| 8 |
-
from
|
| 9 |
-
|
| 10 |
-
from
|
| 11 |
-
from
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
|
| 96 |
-
|
| 97 |
-
|
| 98 |
-
|
| 99 |
-
|
| 100 |
-
|
| 101 |
-
|
| 102 |
-
|
| 103 |
-
|
| 104 |
-
|
| 105 |
-
|
| 106 |
-
|
| 107 |
-
|
| 108 |
-
|
| 109 |
-
|
| 110 |
-
|
| 111 |
-
|
| 112 |
-
|
| 113 |
-
|
| 114 |
-
"difficulty": "medium",
|
| 115 |
-
"description": "Fix multiple errors. No hint.",
|
| 116 |
-
"max_steps": 5,
|
| 117 |
-
"has_grader": True,
|
| 118 |
-
"grader": "sql_env.grader.grade",
|
| 119 |
-
},
|
| 120 |
-
{
|
| 121 |
-
"name": "hard",
|
| 122 |
-
"difficulty": "hard",
|
| 123 |
-
"description": "Fix complex multi-join queries. Schema provided.",
|
| 124 |
-
"max_steps": 4,
|
| 125 |
-
"has_grader": True,
|
| 126 |
-
"grader": "sql_env.grader.grade",
|
| 127 |
-
},
|
| 128 |
-
]
|
| 129 |
-
}
|
| 130 |
-
|
| 131 |
-
|
| 132 |
-
@app.get("/")
|
| 133 |
-
async def root():
|
| 134 |
-
return {
|
| 135 |
-
"name": "SQL Correction RL Environment",
|
| 136 |
-
"version": "1.0.0",
|
| 137 |
-
"endpoints": ["/reset", "/step", "/state", "/health", "/tasks"],
|
| 138 |
-
"tasks": ["easy", "medium", "hard"],
|
| 139 |
-
}
|
| 140 |
|
| 141 |
|
| 142 |
def main():
|
|
|
|
| 1 |
"""
|
| 2 |
+
FastAPI HTTP wrapper for SQLCorrectionEnv using openenv.core base classes.
|
|
|
|
|
|
|
| 3 |
"""
|
| 4 |
+
from openenv.core.env_server import create_fastapi_app
|
| 5 |
+
from openenv.core.env_server.interfaces import Environment
|
| 6 |
+
from openenv.core.env_server.types import State
|
| 7 |
+
from sql_env.models import SQLAction, SQLObservation
|
| 8 |
+
from sql_env.tasks import ALL_TASKS, TASK_SETS
|
| 9 |
+
from sql_env.grader import grade, generate_feedback
|
| 10 |
+
import random
|
| 11 |
+
|
| 12 |
+
|
| 13 |
+
class SQLCorrectionEnvironment(Environment):
|
| 14 |
+
|
| 15 |
+
def __init__(self):
|
| 16 |
+
super().__init__()
|
| 17 |
+
self._difficulty = "easy"
|
| 18 |
+
self._current_task = None
|
| 19 |
+
self._step_count = 0
|
| 20 |
+
self._done = False
|
| 21 |
+
self._last_reward = 0.0
|
| 22 |
+
self._previous_attempt = None
|
| 23 |
+
self._feedback = None
|
| 24 |
+
self._rewards_history = []
|
| 25 |
+
|
| 26 |
+
def reset(self, difficulty: str = "easy") -> SQLObservation:
|
| 27 |
+
self._difficulty = difficulty
|
| 28 |
+
tasks = TASK_SETS.get(difficulty, TASK_SETS["easy"])
|
| 29 |
+
self._current_task = random.choice(tasks)
|
| 30 |
+
self._step_count = 0
|
| 31 |
+
self._done = False
|
| 32 |
+
self._last_reward = 0.0
|
| 33 |
+
self._previous_attempt = None
|
| 34 |
+
self._feedback = None
|
| 35 |
+
self._rewards_history = []
|
| 36 |
+
return SQLObservation(
|
| 37 |
+
task_id=self._current_task.task_id,
|
| 38 |
+
broken_query=self._current_task.broken_query,
|
| 39 |
+
schema_context=self._current_task.schema_context,
|
| 40 |
+
error_hint=self._current_task.error_hint,
|
| 41 |
+
step_number=0,
|
| 42 |
+
previous_attempt=None,
|
| 43 |
+
feedback=None,
|
| 44 |
+
)
|
| 45 |
+
|
| 46 |
+
def step(self, action: SQLAction) -> SQLObservation:
|
| 47 |
+
self._step_count += 1
|
| 48 |
+
reward_obj = grade(action, self._current_task)
|
| 49 |
+
reward = reward_obj.value
|
| 50 |
+
self._last_reward = reward
|
| 51 |
+
self._previous_attempt = action.corrected_query
|
| 52 |
+
self._feedback = generate_feedback(action, self._current_task, reward_obj)
|
| 53 |
+
self._rewards_history.append(reward)
|
| 54 |
+
done = (reward >= 0.95) or (self._step_count >= self._current_task.max_steps)
|
| 55 |
+
self._done = done
|
| 56 |
+
return SQLObservation(
|
| 57 |
+
task_id=self._current_task.task_id,
|
| 58 |
+
broken_query=self._current_task.broken_query,
|
| 59 |
+
schema_context=self._current_task.schema_context,
|
| 60 |
+
error_hint=self._current_task.error_hint,
|
| 61 |
+
step_number=self._step_count,
|
| 62 |
+
previous_attempt=self._previous_attempt,
|
| 63 |
+
feedback=self._feedback,
|
| 64 |
+
)
|
| 65 |
+
|
| 66 |
+
@property
|
| 67 |
+
def state(self) -> dict:
|
| 68 |
+
if self._current_task is None:
|
| 69 |
+
return {"status": "not_initialized"}
|
| 70 |
+
return {
|
| 71 |
+
"task_id": self._current_task.task_id,
|
| 72 |
+
"difficulty": self._difficulty,
|
| 73 |
+
"step_count": self._step_count,
|
| 74 |
+
"done": self._done,
|
| 75 |
+
"last_reward": self._last_reward,
|
| 76 |
+
"rewards_history": self._rewards_history,
|
| 77 |
+
}
|
| 78 |
+
|
| 79 |
+
def get_tasks(self):
|
| 80 |
+
return {
|
| 81 |
+
"tasks": [
|
| 82 |
+
{
|
| 83 |
+
"name": "easy",
|
| 84 |
+
"difficulty": "easy",
|
| 85 |
+
"description": "Fix a single syntax error. Error hint provided.",
|
| 86 |
+
"max_steps": 5,
|
| 87 |
+
"has_grader": True,
|
| 88 |
+
"grader": "sql_env.grader.grade",
|
| 89 |
+
},
|
| 90 |
+
{
|
| 91 |
+
"name": "medium",
|
| 92 |
+
"difficulty": "medium",
|
| 93 |
+
"description": "Fix multiple errors. No hint.",
|
| 94 |
+
"max_steps": 5,
|
| 95 |
+
"has_grader": True,
|
| 96 |
+
"grader": "sql_env.grader.grade",
|
| 97 |
+
},
|
| 98 |
+
{
|
| 99 |
+
"name": "hard",
|
| 100 |
+
"difficulty": "hard",
|
| 101 |
+
"description": "Fix complex multi-join queries. Schema provided.",
|
| 102 |
+
"max_steps": 4,
|
| 103 |
+
"has_grader": True,
|
| 104 |
+
"grader": "sql_env.grader.grade",
|
| 105 |
+
},
|
| 106 |
+
]
|
| 107 |
+
}
|
| 108 |
+
|
| 109 |
+
|
| 110 |
+
env = SQLCorrectionEnvironment()
|
| 111 |
+
app = create_fastapi_app(env, SQLAction, SQLObservation)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 112 |
|
| 113 |
|
| 114 |
def main():
|