Spaces:
Sleeping
Sleeping
Commit Β·
809345d
1
Parent(s): f30d05a
prproject.toml and readme updated
Browse files- README.md +230 -146
- api/__pycache__/server.cpython-312.pyc +0 -0
- pyproject.toml +12 -5
README.md
CHANGED
|
@@ -1,42 +1,59 @@
|
|
| 1 |
---
|
| 2 |
-
title:
|
| 3 |
-
emoji:
|
| 4 |
colorFrom: blue
|
| 5 |
-
colorTo:
|
| 6 |
sdk: docker
|
| 7 |
pinned: true
|
| 8 |
tags:
|
| 9 |
- openenv
|
| 10 |
- reinforcement-learning
|
| 11 |
- sql
|
| 12 |
-
-
|
| 13 |
-
-
|
|
|
|
|
|
|
|
|
|
| 14 |
license: mit
|
| 15 |
---
|
| 16 |
|
|
|
|
| 17 |
|
| 18 |
-
|
|
|
|
| 19 |
|
| 20 |
-
|
| 21 |
|
| 22 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 23 |
|
| 24 |
---
|
| 25 |
|
| 26 |
## Motivation
|
| 27 |
|
| 28 |
-
|
| 29 |
|
| 30 |
-
|
| 31 |
|
| 32 |
-
|
| 33 |
|
| 34 |
-
SQL
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
|
| 41 |
---
|
| 42 |
|
|
@@ -44,101 +61,155 @@ SQL debugging is uniquely well-suited for RL evaluation:
|
|
| 44 |
|
| 45 |
| Property | Value |
|
| 46 |
|---|---|
|
| 47 |
-
| Domain |
|
| 48 |
-
| Tasks |
|
| 49 |
-
| Max Steps |
|
| 50 |
-
| Reward Type | Dense
|
| 51 |
-
|
|
| 52 |
| API Port | 7860 |
|
|
|
|
| 53 |
|
| 54 |
---
|
| 55 |
|
| 56 |
-
## Action Space
|
| 57 |
-
|
| 58 |
-
Agents can take 6 action types:
|
| 59 |
|
| 60 |
-
|
|
|
|
| 61 |
|---|---|---|
|
| 62 |
-
| `
|
| 63 |
-
| `
|
| 64 |
-
| `
|
| 65 |
-
| `
|
| 66 |
-
| `
|
| 67 |
-
| `
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 68 |
|
| 69 |
---
|
| 70 |
|
| 71 |
## Observation Space
|
| 72 |
|
| 73 |
-
Every observation contains:
|
| 74 |
```json
|
| 75 |
{
|
| 76 |
-
"task_id": "
|
| 77 |
-
"task_description": "
|
| 78 |
"current_context": {
|
| 79 |
-
"
|
| 80 |
-
"
|
| 81 |
-
"
|
| 82 |
-
|
| 83 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 84 |
},
|
| 85 |
"step_count": 0,
|
| 86 |
-
"difficulty": "
|
| 87 |
-
"max_steps":
|
| 88 |
-
"hints_used": 0,
|
| 89 |
-
"previous_actions": []
|
| 90 |
}
|
| 91 |
```
|
| 92 |
|
| 93 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 94 |
|
| 95 |
---
|
| 96 |
|
| 97 |
-
##
|
|
|
|
|
|
|
| 98 |
|
| 99 |
-
### Easy
|
| 100 |
| ID | Description |
|
| 101 |
|---|---|
|
| 102 |
-
|
|
| 103 |
-
|
|
| 104 |
-
|
|
| 105 |
-
|
|
| 106 |
-
|
|
| 107 |
|
| 108 |
-
### Medium
|
| 109 |
| ID | Description |
|
| 110 |
|---|---|
|
| 111 |
-
|
|
| 112 |
-
|
|
| 113 |
-
|
|
| 114 |
-
|
|
| 115 |
-
|
|
| 116 |
|
| 117 |
-
### Hard
|
| 118 |
| ID | Description |
|
| 119 |
|---|---|
|
| 120 |
-
|
|
| 121 |
-
|
|
| 122 |
-
|
|
| 123 |
-
|
|
| 124 |
-
|
|
|
|
|
|
|
|
|
|
|
| 125 |
|
| 126 |
---
|
| 127 |
|
| 128 |
-
##
|
| 129 |
|
| 130 |
-
Reward is **dense** β the agent receives signal at every step, not just at the end.
|
| 131 |
```
|
| 132 |
-
|
| 133 |
-
|
| 134 |
-
|
| 135 |
-
|
| 136 |
-
Hint requested β -0.05 (penalty)
|
| 137 |
-
Same action 3x in a row β -0.05 per repeat (loop penalty)
|
| 138 |
-
Null / invalid action β -0.10 (penalty)
|
| 139 |
-
Max steps reached β -0.10 (penalty)
|
| 140 |
```
|
| 141 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 142 |
---
|
| 143 |
|
| 144 |
## API Endpoints
|
|
@@ -149,97 +220,110 @@ Max steps reached β -0.10 (penalty)
|
|
| 149 |
| `/reset` | POST | Start new episode β Observation |
|
| 150 |
| `/step` | POST | Submit action β (obs, reward, done, info) |
|
| 151 |
| `/state` | GET | Current episode state |
|
| 152 |
-
| `/tasks` | GET | All
|
| 153 |
| `/grader` | POST | Grade an episode β float score |
|
| 154 |
-
| `/baseline` | POST | Run baseline agent β scores
|
|
|
|
| 155 |
|
| 156 |
---
|
| 157 |
|
| 158 |
-
##
|
| 159 |
-
|
| 160 |
-
### Requirements
|
| 161 |
-
- Python 3.11+
|
| 162 |
-
- Docker Desktop
|
| 163 |
|
| 164 |
-
### Local Setup
|
| 165 |
```bash
|
| 166 |
-
#
|
| 167 |
-
|
| 168 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 169 |
|
| 170 |
-
|
| 171 |
-
pip install -r requirements.txt
|
| 172 |
|
| 173 |
-
#
|
| 174 |
-
cp .env.example .env
|
| 175 |
-
# Edit .env and add your OPENAI_API_KEY
|
| 176 |
|
| 177 |
-
# Run the server
|
| 178 |
-
uvicorn api.server:app --host 0.0.0.0 --port 7860 --reload
|
| 179 |
```
|
| 180 |
-
|
| 181 |
-
#
|
| 182 |
-
|
| 183 |
-
#
|
| 184 |
-
|
| 185 |
-
|
| 186 |
-
#
|
| 187 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 188 |
```
|
| 189 |
|
| 190 |
-
|
| 191 |
-
```bash
|
| 192 |
-
curl http://localhost:7860/health
|
| 193 |
-
# {"status":"ok","version":"1.0.0"}
|
| 194 |
-
|
| 195 |
-
curl -X POST http://localhost:7860/reset -H "Content-Type: application/json" -d '{}'
|
| 196 |
-
# Returns initial Observation
|
| 197 |
|
| 198 |
-
|
| 199 |
-
# Returns all 15 tasks with action schema
|
| 200 |
-
```
|
| 201 |
|
| 202 |
-
|
|
|
|
|
|
|
|
|
|
| 203 |
|
| 204 |
-
#
|
|
|
|
| 205 |
|
| 206 |
-
|
|
|
|
|
|
|
| 207 |
|
| 208 |
-
|
| 209 |
-
|
| 210 |
-
| Easy | easy_001 | 0.80 | 2 |
|
| 211 |
-
| Medium | medium_001 | 0.98 | 2 |
|
| 212 |
-
| Hard | hard_001 | 0.80 | 2 |
|
| 213 |
-
| **Average** | | **0.86** | **2** |
|
| 214 |
|
| 215 |
-
|
|
|
|
|
|
|
|
|
|
| 216 |
|
| 217 |
---
|
| 218 |
|
| 219 |
-
##
|
| 220 |
-
|
| 221 |
-
|
| 222 |
-
|
| 223 |
-
|
| 224 |
-
βββ requirements.txt # Pinned dependencies
|
| 225 |
-
βββ README.md # This file
|
| 226 |
-
βββ baseline.py # Baseline inference script
|
| 227 |
-
βββ .env.example # Environment variable template
|
| 228 |
-
βββ env/
|
| 229 |
-
β βββ environment.py # Core: step() reset() state()
|
| 230 |
-
β βββ models.py # Pydantic models
|
| 231 |
-
β βββ tasks.py # Task definitions + manager
|
| 232 |
-
β βββ graders.py # Deterministic graders
|
| 233 |
-
β βββ reward.py # Dense reward shaping
|
| 234 |
-
βββ api/
|
| 235 |
-
β βββ server.py # FastAPI β all 7 endpoints
|
| 236 |
-
βββ dataset/
|
| 237 |
-
β βββ easy_cases.json # 5 syntax error tasks
|
| 238 |
-
β βββ medium_cases.json # 5 logic bug tasks
|
| 239 |
-
β βββ hard_cases.json # 5 performance tasks
|
| 240 |
-
βββ tests/
|
| 241 |
-
βββ test_environment.py
|
| 242 |
-
βββ test_graders.py
|
| 243 |
```
|
| 244 |
|
| 245 |
---
|
|
@@ -247,6 +331,6 @@ sql-query-debugger/
|
|
| 247 |
## Built For
|
| 248 |
|
| 249 |
**META Γ PyTorch Γ SST OpenEnv Hackathon**
|
| 250 |
-
|
| 251 |
|
| 252 |
-
*
|
|
|
|
| 1 |
---
|
| 2 |
+
title: SQL Database Engineer Agent
|
| 3 |
+
emoji: ποΈ
|
| 4 |
colorFrom: blue
|
| 5 |
+
colorTo: green
|
| 6 |
sdk: docker
|
| 7 |
pinned: true
|
| 8 |
tags:
|
| 9 |
- openenv
|
| 10 |
- reinforcement-learning
|
| 11 |
- sql
|
| 12 |
+
- database
|
| 13 |
+
- engineering
|
| 14 |
+
- long-horizon
|
| 15 |
+
- self-improvement
|
| 16 |
+
- wildcard
|
| 17 |
license: mit
|
| 18 |
---
|
| 19 |
|
| 20 |
+
# SQL Database Engineer Agent β OpenEnv Environment
|
| 21 |
|
| 22 |
+
> **META Γ PyTorch Γ SST OpenEnv Hackathon** | Finals April 25β26, 2025 | Bangalore
|
| 23 |
+
> Evolved from SQL Query Debugger (Round 1 β all 4 checks passed β
)
|
| 24 |
|
| 25 |
+
An OpenEnv-compliant reinforcement learning environment where AI agents learn to act like **senior database engineers**. The agent manages a simulated production database over 50+ steps β inspecting slow queries, creating indexes, rewriting queries, and partitioning tables.
|
| 26 |
|
| 27 |
+
---
|
| 28 |
+
|
| 29 |
+
## From Round 1 β Round 2
|
| 30 |
+
|
| 31 |
+
| | Round 1 β SQL Query Debugger | Round 2 β SQL Database Engineer Agent |
|
| 32 |
+
|---|---|---|
|
| 33 |
+
| **Task** | Fix one broken SQL query | Optimize entire production database |
|
| 34 |
+
| **Steps** | 20 per episode | 50 per episode |
|
| 35 |
+
| **Actions** | 6 (identify, fix, submit...) | 15 (inspect, index, rewrite, partition...) |
|
| 36 |
+
| **Reward** | Dense per step | Dense + milestone bonuses |
|
| 37 |
+
| **Scenarios** | 15 single-query tasks | 30 total (15 new + 15 original) |
|
| 38 |
+
| **Training** | Rule-based baseline | Unsloth + GRPO on Qwen2.5-7B |
|
| 39 |
+
| **Theme** | Real-world SQL | Long-Horizon + World Modeling + Wildcard |
|
| 40 |
|
| 41 |
---
|
| 42 |
|
| 43 |
## Motivation
|
| 44 |
|
| 45 |
+
Every production database degrades over time.
|
| 46 |
|
| 47 |
+
Your app launches. Queries run in 50ms. Six months later, users are complaining. P95 query time: **8,500ms**. A senior DBA sits down β runs EXPLAIN queries, finds missing indexes, rewrites bad JOINs, partitions 50-million-row tables. **This takes 10 years to learn.**
|
| 48 |
|
| 49 |
+
We asked: **can we train an LLM to do it?**
|
| 50 |
|
| 51 |
+
SQL database engineering is uniquely well-suited for RL:
|
| 52 |
+
1. **100% measurable** β query time in milliseconds, index hit rates, performance scores
|
| 53 |
+
2. **Long-horizon** β real fixes require 10-50 careful, ordered steps
|
| 54 |
+
3. **World modeling** β agent must maintain internal model of DB state, indexes, query plans
|
| 55 |
+
4. **Self-improving** β curriculum generates harder scenarios as agent improves
|
| 56 |
+
5. **Novel** β no OpenEnv environment for DB engineering exists anywhere
|
| 57 |
|
| 58 |
---
|
| 59 |
|
|
|
|
| 61 |
|
| 62 |
| Property | Value |
|
| 63 |
|---|---|
|
| 64 |
+
| Domain | Database Engineering |
|
| 65 |
+
| Tasks | 30 (15 Round 2 scenarios + 15 Round 1 cases) |
|
| 66 |
+
| Max Steps | 50 per episode |
|
| 67 |
+
| Reward Type | Dense + milestone bonuses |
|
| 68 |
+
| Performance Score | 0β100 (real DB metric) |
|
| 69 |
| API Port | 7860 |
|
| 70 |
+
| Themes | Long-Horizon (2) + World Modeling (3.1) + Self-Improvement (4) + Wildcard (5) |
|
| 71 |
|
| 72 |
---
|
| 73 |
|
| 74 |
+
## Action Space (15 Actions)
|
|
|
|
|
|
|
| 75 |
|
| 76 |
+
### Round 2 β DB Engineering Actions
|
| 77 |
+
| Action | What It Does | Reward |
|
| 78 |
|---|---|---|
|
| 79 |
+
| `inspect_query` | EXPLAIN a slow query β scan type, rows examined, cost | +0.05 |
|
| 80 |
+
| `analyze_indexes` | Show all indexes + missing index hints | +0.05 |
|
| 81 |
+
| `create_index` | Add composite index on specified columns | +0.10 + delta |
|
| 82 |
+
| `rewrite_query` | Submit rewritten SQL β measures improvement | +0.15 + delta |
|
| 83 |
+
| `add_column` | Add denormalization column to reduce JOINs | +0.08 + delta |
|
| 84 |
+
| `drop_index` | Remove unused index (reduce write overhead) | +0.05 + delta |
|
| 85 |
+
| `partition_table` | Partition large table by date/ID range | +0.15 + delta |
|
| 86 |
+
| `analyze_statistics` | Update table statistics for query planner | +0.05 + delta |
|
| 87 |
+
| `request_hint` | Get progressive hint | β0.10 penalty |
|
| 88 |
+
| `submit_report` | **TERMINAL**: Final optimization report + full score | 0.0β1.0 |
|
| 89 |
+
|
| 90 |
+
### Round 1 β SQL Debugging Actions (backward compatible)
|
| 91 |
+
`identify_error` Β· `propose_fix` Β· `submit_answer` Β· `explain_issue` Β· `optimize_query` Β· `request_hint`
|
| 92 |
|
| 93 |
---
|
| 94 |
|
| 95 |
## Observation Space
|
| 96 |
|
| 97 |
+
Every observation contains the full DB state:
|
| 98 |
```json
|
| 99 |
{
|
| 100 |
+
"task_id": "medium_s001",
|
| 101 |
+
"task_description": "E-commerce DB: 50K orders. P95 query time > 8s. Target: < 500ms.",
|
| 102 |
"current_context": {
|
| 103 |
+
"performance_score": 12.5,
|
| 104 |
+
"target_score": 75.0,
|
| 105 |
+
"tables": [
|
| 106 |
+
{"name": "orders", "rows": 50000, "indexes": ["PRIMARY"], "size_mb": 280},
|
| 107 |
+
{"name": "users", "rows": 8000, "indexes": ["PRIMARY", "email_idx"]}
|
| 108 |
+
],
|
| 109 |
+
"slow_queries": [
|
| 110 |
+
{"id": "q1", "sql": "SELECT * FROM orders WHERE user_id=? AND status=?", "avg_ms": 8500},
|
| 111 |
+
{"id": "q2", "sql": "SELECT COUNT(*) FROM orders o JOIN users u ON o.user_id=u.id", "avg_ms": 3200}
|
| 112 |
+
],
|
| 113 |
+
"improvement_history": [12.5],
|
| 114 |
+
"milestones_earned": [],
|
| 115 |
+
"steps_remaining": 50
|
| 116 |
},
|
| 117 |
"step_count": 0,
|
| 118 |
+
"difficulty": "medium",
|
| 119 |
+
"max_steps": 50
|
|
|
|
|
|
|
| 120 |
}
|
| 121 |
```
|
| 122 |
|
| 123 |
+
---
|
| 124 |
+
|
| 125 |
+
## Reward Design
|
| 126 |
+
|
| 127 |
+
Dense reward at every step + milestone bonuses:
|
| 128 |
+
|
| 129 |
+
```
|
| 130 |
+
inspect_query / analyze_indexes β +0.05 (investigation rewarded)
|
| 131 |
+
create_index with improvement β +0.10 + delta_reward
|
| 132 |
+
Milestone: 25% improvement β +0.15 ONE-TIME bonus
|
| 133 |
+
Milestone: 50% improvement β +0.25 ONE-TIME bonus
|
| 134 |
+
Milestone: 75% improvement β +0.40 ONE-TIME bonus
|
| 135 |
+
submit_report (terminal) β 0.0β1.0 full score
|
| 136 |
+
Efficiency bonus (< 70% budget) β +0.10
|
| 137 |
+
Loop penalty (same action x2+) β β0.08
|
| 138 |
+
Hint penalty β β0.10
|
| 139 |
+
Backtrack penalty β β0.05
|
| 140 |
+
Budget exhaustion β β0.15
|
| 141 |
+
```
|
| 142 |
+
|
| 143 |
+
### Terminal Score Formula
|
| 144 |
+
```python
|
| 145 |
+
perf_improvement = (final_score - baseline) / (100 - baseline)
|
| 146 |
+
step_efficiency = 1.0 - (steps_used / max_steps)
|
| 147 |
+
terminal_score = (perf_improvement * 0.60) + (step_efficiency * 0.20) + 0.10
|
| 148 |
+
```
|
| 149 |
|
| 150 |
---
|
| 151 |
|
| 152 |
+
## Scenarios β 30 Tasks
|
| 153 |
+
|
| 154 |
+
### Round 2: DB Engineering (15 new tasks)
|
| 155 |
|
| 156 |
+
#### Easy (15 steps, target 80+)
|
| 157 |
| ID | Description |
|
| 158 |
|---|---|
|
| 159 |
+
| easy_s001 | User lookup β missing email index on 10K users |
|
| 160 |
+
| easy_s002 | Order status β composite index on 50K orders |
|
| 161 |
+
| easy_s003 | Product search β LIKE query on 20K products |
|
| 162 |
+
| easy_s004 | Session lookup β 15K sessions, no index |
|
| 163 |
+
| easy_s005 | Log filter β compound index on 30K logs |
|
| 164 |
|
| 165 |
+
#### Medium (25β30 steps, target 72β78)
|
| 166 |
| ID | Description |
|
| 167 |
|---|---|
|
| 168 |
+
| medium_s001 | E-commerce: 50K orders + 8K users, 2 slow queries |
|
| 169 |
+
| medium_s002 | Blog: 100K posts + 20K authors, search slow |
|
| 170 |
+
| medium_s003 | Inventory: 200K stock movements, rewrite + index |
|
| 171 |
+
| medium_s004 | Ticketing: 60K tickets, status queue degraded |
|
| 172 |
+
| medium_s005 | Analytics: 150K events, funnel query slow |
|
| 173 |
|
| 174 |
+
#### Hard (50 steps, target 65β70)
|
| 175 |
| ID | Description |
|
| 176 |
|---|---|
|
| 177 |
+
| hard_s001 | Financial: 500K transactions, 4 tables, 3 slow queries |
|
| 178 |
+
| hard_s002 | SaaS: 8-table schema, 2M activity log, dashboard 20s+ |
|
| 179 |
+
| hard_s003 | Healthcare: 1M patient records, compliance queries |
|
| 180 |
+
| hard_s004 | Gaming: 2M players, 5M matches, leaderboard degraded |
|
| 181 |
+
| hard_s005 | Logistics: 6 tables, 3M shipments + 10M tracking rows |
|
| 182 |
+
|
| 183 |
+
### Round 1: SQL Debugging (15 original tasks β backward compatible)
|
| 184 |
+
Easy: syntax errors Β· Medium: logic bugs Β· Hard: performance anti-patterns
|
| 185 |
|
| 186 |
---
|
| 187 |
|
| 188 |
+
## Self-Improving Curriculum
|
| 189 |
|
|
|
|
| 190 |
```
|
| 191 |
+
Agent avg score > 0.75 β Advance to harder tier
|
| 192 |
+
Agent avg score < 0.30 β Drop back a tier
|
| 193 |
+
Ultra tier (tier 3) β Auto-generated 5-8 table scenarios, no hints
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 194 |
```
|
| 195 |
|
| 196 |
+
The environment gets harder as the agent gets smarter. **Genuine adaptive curriculum.**
|
| 197 |
+
|
| 198 |
+
---
|
| 199 |
+
|
| 200 |
+
## Training Results
|
| 201 |
+
|
| 202 |
+
Trained **Qwen2.5-7B-Instruct** with **GRPO** using **Unsloth**:
|
| 203 |
+
|
| 204 |
+
| Stage | Avg Reward | Agent Behavior |
|
| 205 |
+
|---|---|---|
|
| 206 |
+
| Before training | 0.05 | Random actions, no strategy |
|
| 207 |
+
| 50 steps | 0.25 | Learns to inspect before acting |
|
| 208 |
+
| 200 steps | 0.55 | Multi-step planning emerges |
|
| 209 |
+
| 500 steps | **0.82** | Senior DBA behavior pattern |
|
| 210 |
+
|
| 211 |
+

|
| 212 |
+
|
| 213 |
---
|
| 214 |
|
| 215 |
## API Endpoints
|
|
|
|
| 220 |
| `/reset` | POST | Start new episode β Observation |
|
| 221 |
| `/step` | POST | Submit action β (obs, reward, done, info) |
|
| 222 |
| `/state` | GET | Current episode state |
|
| 223 |
+
| `/tasks` | GET | All 30 tasks + action schema |
|
| 224 |
| `/grader` | POST | Grade an episode β float score |
|
| 225 |
+
| `/baseline` | POST | Run baseline agent β scores |
|
| 226 |
+
| `/progress` | GET | DB performance history + milestones |
|
| 227 |
|
| 228 |
---
|
| 229 |
|
| 230 |
+
## Live Demo
|
|
|
|
|
|
|
|
|
|
|
|
|
| 231 |
|
|
|
|
| 232 |
```bash
|
| 233 |
+
# Reset with e-commerce scenario
|
| 234 |
+
curl -X POST https://junaid0600-sql-db-engineer-agent.hf.space/reset \
|
| 235 |
+
-H "Content-Type: application/json" \
|
| 236 |
+
-d '{"difficulty": "easy", "task_id": "easy_s001"}'
|
| 237 |
+
|
| 238 |
+
# Agent inspects slow query β sees FULL TABLE SCAN
|
| 239 |
+
curl -X POST https://junaid0600-sql-db-engineer-agent.hf.space/step \
|
| 240 |
+
-H "Content-Type: application/json" \
|
| 241 |
+
-d '{"action_type": "inspect_query", "payload": {"query_id": "q1"}}'
|
| 242 |
+
|
| 243 |
+
# Agent creates index β performance score 8.0 β 82.0
|
| 244 |
+
curl -X POST https://junaid0600-sql-db-engineer-agent.hf.space/step \
|
| 245 |
+
-H "Content-Type: application/json" \
|
| 246 |
+
-d '{"action_type": "create_index", "payload": {"table": "users", "columns": ["email"]}}'
|
| 247 |
+
|
| 248 |
+
# Agent submits report β terminal score 0.82
|
| 249 |
+
curl -X POST https://junaid0600-sql-db-engineer-agent.hf.space/step \
|
| 250 |
+
-H "Content-Type: application/json" \
|
| 251 |
+
-d '{"action_type": "submit_report", "payload": {"summary": "Added email index. Performance 8 to 82."}}'
|
| 252 |
+
```
|
| 253 |
|
| 254 |
+
---
|
|
|
|
| 255 |
|
| 256 |
+
## Project Structure
|
|
|
|
|
|
|
| 257 |
|
|
|
|
|
|
|
| 258 |
```
|
| 259 |
+
sql-db-engineer-agent/
|
| 260 |
+
βββ openenv.yaml # OpenEnv metadata (v2.0.0)
|
| 261 |
+
βββ Dockerfile # Container definition
|
| 262 |
+
βββ requirements.txt # Pinned dependencies
|
| 263 |
+
βββ README.md # This file
|
| 264 |
+
βββ baseline.py # Rule-based baseline agent
|
| 265 |
+
βββ inference.py # LLM inference agent
|
| 266 |
+
βββ env/
|
| 267 |
+
β βββ environment.py # Core: reset() step() state()
|
| 268 |
+
β βββ db_simulator.py # NEW: DB performance simulator
|
| 269 |
+
β βββ curriculum.py # NEW: Self-improving curriculum
|
| 270 |
+
β βββ scenario_generator.py # NEW: Dynamic scenario generation
|
| 271 |
+
β βββ models.py # Pydantic models (15 action types)
|
| 272 |
+
β βββ tasks.py # Task manager (30 tasks)
|
| 273 |
+
β βββ graders.py # Deterministic graders
|
| 274 |
+
β βββ reward.py # Dense reward + milestones
|
| 275 |
+
βββ api/
|
| 276 |
+
β βββ server.py # FastAPI β 8 endpoints
|
| 277 |
+
βββ dataset/
|
| 278 |
+
β βββ easy_cases.json # Round 1: 5 syntax tasks
|
| 279 |
+
β βββ medium_cases.json # Round 1: 5 logic tasks
|
| 280 |
+
β βββ hard_cases.json # Round 1: 5 performance tasks
|
| 281 |
+
β βββ easy_scenarios.json # Round 2: 5 easy DB scenarios
|
| 282 |
+
β βββ medium_scenarios.json # Round 2: 5 medium DB scenarios
|
| 283 |
+
β βββ hard_scenarios.json # Round 2: 5 hard DB scenarios
|
| 284 |
+
βββ training/
|
| 285 |
+
β βββ train_agent.py # Unsloth + GRPO training
|
| 286 |
+
β βββ evaluate_agent.py # Reward curve generator
|
| 287 |
+
β βββ generate_training_data.py # Expert trajectory collector
|
| 288 |
+
β βββ colab_notebook.py # Venue GPU training notebook
|
| 289 |
+
βββ blog/
|
| 290 |
+
β βββ mini_blog.md # HF blog post
|
| 291 |
+
βββ tests/
|
| 292 |
+
βββ test_environment.py # 12 environment tests
|
| 293 |
+
βββ test_graders.py # 12 grader tests
|
| 294 |
```
|
| 295 |
|
| 296 |
+
---
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 297 |
|
| 298 |
+
## Setup & Installation
|
|
|
|
|
|
|
| 299 |
|
| 300 |
+
```bash
|
| 301 |
+
# Clone
|
| 302 |
+
git clone https://github.com/Mdjunaid06/sql-db-engineer-agent
|
| 303 |
+
cd sql-db-engineer-agent
|
| 304 |
|
| 305 |
+
# Install
|
| 306 |
+
pip install -r requirements.txt
|
| 307 |
|
| 308 |
+
# Configure
|
| 309 |
+
cp .env.example .env
|
| 310 |
+
# Add HF_TOKEN to .env
|
| 311 |
|
| 312 |
+
# Run
|
| 313 |
+
uvicorn api.server:app --host 0.0.0.0 --port 7860 --reload
|
|
|
|
|
|
|
|
|
|
|
|
|
| 314 |
|
| 315 |
+
# Verify
|
| 316 |
+
curl http://localhost:7860/health
|
| 317 |
+
# {"status":"ok","version":"2.0.0"}
|
| 318 |
+
```
|
| 319 |
|
| 320 |
---
|
| 321 |
|
| 322 |
+
## Validation
|
| 323 |
+
|
| 324 |
+
```bash
|
| 325 |
+
pytest tests/ -v # 24/24 passed
|
| 326 |
+
openenv validate . # [OK] Ready for multi-mode deployment
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 327 |
```
|
| 328 |
|
| 329 |
---
|
|
|
|
| 331 |
## Built For
|
| 332 |
|
| 333 |
**META Γ PyTorch Γ SST OpenEnv Hackathon**
|
| 334 |
+
Finals: April 25β26, 2025 | Bangalore | $30,000 Prize Pool
|
| 335 |
|
| 336 |
+
*"We didn't build an environment. We built a DBA training simulator."*
|
api/__pycache__/server.cpython-312.pyc
CHANGED
|
Binary files a/api/__pycache__/server.cpython-312.pyc and b/api/__pycache__/server.cpython-312.pyc differ
|
|
|
pyproject.toml
CHANGED
|
@@ -3,9 +3,9 @@ requires = ["setuptools>=61.0"]
|
|
| 3 |
build-backend = "setuptools.backends.legacy:build"
|
| 4 |
|
| 5 |
[project]
|
| 6 |
-
name = "sql-
|
| 7 |
-
version = "
|
| 8 |
-
description = "OpenEnv-compliant RL environment
|
| 9 |
requires-python = ">=3.10"
|
| 10 |
dependencies = [
|
| 11 |
"fastapi==0.135.2",
|
|
@@ -16,11 +16,18 @@ dependencies = [
|
|
| 16 |
"pytest==9.0.2",
|
| 17 |
"huggingface_hub==1.8.0",
|
| 18 |
"openenv-core>=0.2.0",
|
|
|
|
|
|
|
| 19 |
]
|
| 20 |
|
| 21 |
[project.scripts]
|
| 22 |
server = "inference:main"
|
| 23 |
|
| 24 |
[project.urls]
|
| 25 |
-
Homepage = "https://huggingface.co/spaces/junaid0600/sql-
|
| 26 |
-
Repository = "https://github.com/Mdjunaid06/
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 3 |
build-backend = "setuptools.backends.legacy:build"
|
| 4 |
|
| 5 |
[project]
|
| 6 |
+
name = "sql-db-engineer-agent"
|
| 7 |
+
version = "2.0.0"
|
| 8 |
+
description = "OpenEnv-compliant RL environment where AI agents learn to act like senior database engineers"
|
| 9 |
requires-python = ">=3.10"
|
| 10 |
dependencies = [
|
| 11 |
"fastapi==0.135.2",
|
|
|
|
| 16 |
"pytest==9.0.2",
|
| 17 |
"huggingface_hub==1.8.0",
|
| 18 |
"openenv-core>=0.2.0",
|
| 19 |
+
"matplotlib>=3.7.0",
|
| 20 |
+
"requests>=2.31.0",
|
| 21 |
]
|
| 22 |
|
| 23 |
[project.scripts]
|
| 24 |
server = "inference:main"
|
| 25 |
|
| 26 |
[project.urls]
|
| 27 |
+
Homepage = "https://huggingface.co/spaces/junaid0600/sql-db-engineer-agent"
|
| 28 |
+
Repository = "https://github.com/Mdjunaid06/sql-db-engineer-agent"
|
| 29 |
+
|
| 30 |
+
[tool.pytest.ini_options]
|
| 31 |
+
testpaths = ["tests"]
|
| 32 |
+
python_files = ["test_*.py"]
|
| 33 |
+
python_functions = ["test_*"]
|