md896 commited on
Commit
959117b
·
1 Parent(s): 5fb3e9e

initial commit

Browse files
Files changed (1) hide show
  1. README.md +5 -197
README.md CHANGED
@@ -1,202 +1,10 @@
1
  ---
2
- title: sql-debug-env
3
- emoji: "🧪"
4
- colorFrom: blue
5
- colorTo: green
6
  sdk: docker
7
  pinned: false
8
  ---
9
 
10
- # SQL Debug Environment (`sql-debug-env`)
11
-
12
- ![Python](https://img.shields.io/badge/Python-3.11+-3776AB?logo=python&logoColor=white)
13
- ![FastAPI](https://img.shields.io/badge/FastAPI-0.115-009688?logo=fastapi&logoColor=white)
14
- ![Pydantic](https://img.shields.io/badge/Pydantic-v2-E92063?logo=pydantic&logoColor=white)
15
- ![SQLite](https://img.shields.io/badge/SQLite-In_Memory-003B57?logo=sqlite&logoColor=white)
16
- ![Docker](https://img.shields.io/badge/Docker-Ready-2496ED?logo=docker&logoColor=white)
17
- ![OpenEnv](https://img.shields.io/badge/OpenEnv-Validated-2ea44f)
18
-
19
- An OpenEnv environment for a real task people do every day: **debugging SQL**. The agent gets a broken query, a live (in-memory) SQLite database, and a description of the expected output. It can inspect schema/errors/samples and submit fixed queries until it solves the task.
20
-
21
- ## What’s in this repo
22
- - **FastAPI server**: `server/main.py` (endpoints: `/health`, `/tasks`, `/reset`, `/step`, `/state`)
23
- - **Environment logic**: `server/env.py` + `server/database.py`
24
- - **Tasks**: `server/tasks/` (easy → medium → hard, deterministic seed data)
25
- - **Baseline agent**: `inference.py` (OpenAI client + `[START]/[STEP]/[END]` logs)
26
-
27
- ## Tech Stack
28
- - Python 3.11+
29
- - FastAPI + Uvicorn
30
- - Pydantic v2
31
- - SQLite (in-memory)
32
- - OpenEnv Core
33
- - Docker
34
- - OpenAI Python SDK (baseline inference)
35
-
36
- ## Production Notes
37
- - Stateless HTTP API with per-session environment instances keyed by `X-Session-Id`
38
- - Deterministic task data (in-memory SQLite) for reproducible grading
39
- - Reward clamped to `[0.0, 1.0]` with partial-progress shaping
40
- - Docker-first deployment path (local and Hugging Face Spaces)
41
- - Local benchmark endpoint for live latency checks (`/benchmark`)
42
-
43
- ## API Docs (FastAPI Auto Docs)
44
- Use these for interactive testing in browser:
45
-
46
- - Swagger UI: `http://localhost:7860/docs`
47
- - ReDoc: `http://localhost:7860/redoc`
48
- - OpenAPI spec: `http://localhost:7860/openapi.json`
49
-
50
- ## Action Space
51
- | Action | Required fields | Cost / reward effect |
52
- |---|---|---|
53
- | `submit_query` | `query` | Main evaluation step (dense reward based on grading) |
54
- | `inspect_schema` | none | Free information action (small positive reward component) |
55
- | `inspect_error` | none | Free information action (small positive reward component) |
56
- | `inspect_sample` | `table_name` | Free information action (small positive reward component) |
57
- | `reset_query` | none | Penalty action (reduces reward for that step) |
58
-
59
- ## Observation Space
60
- | Field | Type |
61
- |---|---|
62
- | `task_id` | `string` |
63
- | `task_description` | `string` |
64
- | `original_query` | `string` |
65
- | `current_query` | `string_or_null` |
66
- | `expected_description` | `string` |
67
- | `last_action_type` | `string` |
68
- | `last_query_result` | `object_or_null` |
69
- | `steps_taken` | `integer` |
70
- | `steps_remaining` | `integer` |
71
- | `current_score` | `float` |
72
- | `schema_info` | `object_or_null` |
73
- | `error_details` | `string_or_null` |
74
- | `sample_rows` | `array_or_null` |
75
- | `hint` | `string_or_null` |
76
- | `is_done` | `boolean` |
77
- | `success` | `boolean` |
78
-
79
- ## Reward Function
80
- | Component | Range | Description |
81
- |---|---|---|
82
- | `correctness` | `[0.0, 0.6]` | Row-level match vs expected output |
83
- | `efficiency` | `[0.0, 0.2]` | Bonus for solving with fewer steps |
84
- | `syntax_progress` | `[0.0, 0.1]` | Small reward for producing syntactically valid SQL |
85
- | `schema_bonus` | `[0.0, 0.1]` | Bonus for referencing correct tables/columns |
86
- | `penalty` | `[0.0, 0.2]` | Deduction magnitude for resets/regressions/urgency near step limit |
87
-
88
- ## Tasks
89
- ### Task 1: Easy — Syntax Error Fix (`easy_syntax_fix`)
90
- Two straightforward issues: a misspelled keyword (`GRUP BY`) and an `ORDER BY` alias mismatch.
91
-
92
- ### Task 2: Medium — Logic Error Fix (`medium_logic_fix`)
93
- Logic bugs around outer joins + filtering scope + aggregation scope.
94
-
95
- ### Task 3: Hard — Multi-Bug Fix (`hard_multi_bug`)
96
- Five bugs across correlated subqueries, window functions, CTE scope, date logic, and duplication.
97
-
98
- ## Baseline
99
- The baseline script is intentionally simple: it loops `reset → step` and asks an OpenAI model to choose the next JSON action.
100
-
101
- ## Reliability & Benchmarking
102
-
103
- ### Verified status (local)
104
- - `openenv validate --verbose`: **PASS**
105
- - `python3 -m unittest discover -s tests -p "test_*.py"`: **10/10 PASS**
106
- - Docker smoke test: **PASS** (`/health`, `/tasks`, `/reset`, `/step`)
107
- - FastAPI docs available: **PASS** (`/docs`, `/redoc`, `/openapi.json`)
108
-
109
- ### Endpoint benchmark (local Docker run, n=25)
110
- Measured with `scripts/benchmark_local.py` on a running local container:
111
-
112
- | Endpoint | avg | p50 | p95 |
113
- |---|---:|---:|---:|
114
- | `GET /health` | 0.69 ms | 0.67 ms | 0.76 ms |
115
- | `GET /tasks` | 0.82 ms | 0.81 ms | 0.90 ms |
116
- | `POST /reset` | 1.34 ms | 1.26 ms | 1.62 ms |
117
- | `POST /step` (`inspect_schema`) | 1.07 ms | 1.01 ms | 1.34 ms |
118
-
119
- Re-run anytime:
120
-
121
- ```bash
122
- python3 scripts/benchmark_local.py
123
- ```
124
-
125
- Notes:
126
- - These are local-machine numbers (single container, warm runtime).
127
- - For submission-grade reporting, also capture one run against your HF Space URL after deploy.
128
-
129
- ## Setup & Usage
130
-
131
- ### Local Development
132
- ```bash
133
- pip install -r requirements.txt
134
- uvicorn server.main:app --host 0.0.0.0 --port 7860
135
- ```
136
-
137
- ### Docker
138
- ```bash
139
- docker build -t sql-debug-env .
140
- docker run -p 7860:7860 sql-debug-env
141
- ```
142
-
143
- ### Quick smoke test
144
- ```bash
145
- curl http://localhost:7860/health
146
- curl http://localhost:7860/tasks
147
- curl -X POST http://localhost:7860/reset -H "Content-Type: application/json" -d '{"task_id":"easy_syntax_fix"}'
148
- curl -X POST http://localhost:7860/step -H "Content-Type: application/json" -d '{"action":{"action_type":"inspect_schema"}}'
149
- curl "http://localhost:7860/benchmark?runs=20"
150
- ```
151
-
152
- ### Real-time benchmark API (for dashboards/web pages)
153
- This is a live endpoint, not static/dummy data. Every request runs fresh measurements.
154
-
155
- - Endpoint: `GET /benchmark?runs=20`
156
- - `runs` range: `1` to `100`
157
- - Returns JSON with `avg_ms`, `p50_ms`, `p95_ms`, `n`, and a fresh `timestamp_epoch_ms`
158
-
159
- Example:
160
- ```bash
161
- curl "http://localhost:7860/benchmark?runs=30"
162
- ```
163
-
164
- ### Run Baseline
165
- ```bash
166
- export API_BASE_URL="https://api.openai.com/v1"
167
- export MODEL_NAME="gpt-4o-mini"
168
- export OPENAI_API_KEY="your-key"
169
- export ENV_BASE_URL="http://localhost:7860"
170
- export HF_TOKEN="$OPENAI_API_KEY"
171
- export SEED="1"
172
- python inference.py
173
- ```
174
-
175
- ### OpenEnv Validation
176
- ```bash
177
- pip install openenv-core
178
- openenv validate
179
- ```
180
-
181
- ### Suggested pre-submit check
182
- ```bash
183
- openenv validate --verbose
184
- python3 -m unittest discover -s tests -p "test_*.py"
185
- docker build -t sql-debug-env .
186
- docker run --rm -p 7860:7860 sql-debug-env
187
- # in another terminal:
188
- curl -s http://localhost:7860/health
189
- curl -s http://localhost:7860/docs >/dev/null
190
- curl -s "http://localhost:7860/benchmark?runs=20"
191
- ```
192
-
193
- ## Hugging Face Spaces (Docker)
194
- 1. Create a new **Space → Docker**.
195
- 2. Push this repo.
196
- 3. Update `openenv.yaml` → `api.base_url` to your Space URL: `https://<your-space>.hf.space`
197
- 4. Wait for build, then verify:
198
-
199
- ```bash
200
- curl -X POST https://<your-space>.hf.space/reset -H "Content-Type: application/json" -d '{}'
201
- ```
202
-
 
1
  ---
2
+ title: Sql Debug Env
3
+ emoji: 💻
4
+ colorFrom: indigo
5
+ colorTo: gray
6
  sdk: docker
7
  pinned: false
8
  ---
9
 
10
+ Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference