UtkarshSatav commited on
Commit
c51dde5
·
verified ·
1 Parent(s): e5011a0

Upload folder using huggingface_hub

Browse files
Files changed (1) hide show
  1. README.md +73 -221
README.md CHANGED
@@ -1,255 +1,107 @@
1
  ---
2
- title: Sql Env Environment Server
3
- emoji: 🗜
4
  colorFrom: blue
5
- colorTo: gray
6
  sdk: docker
7
  pinned: false
8
- app_port: 8000
9
- base_path: /web
10
  tags:
11
  - openenv
12
  ---
13
 
14
- # Sql Env Environment
15
 
16
- A simple test environment that echoes back messages. Perfect for testing the env APIs as well as demonstrating environment usage patterns.
17
 
18
- ## Quick Start
19
 
20
- The simplest way to use the Sql Env environment is through the `SqlEnv` class:
21
 
22
- ```python
23
- from sql_env import SqlAction, SqlEnv
24
 
25
- try:
26
- # Create environment from Docker image
27
- sql_envenv = SqlEnv.from_docker_image("sql_env-env:latest")
 
 
28
 
29
- # Reset
30
- result = sql_envenv.reset()
31
- print(f"Reset: {result.observation.echoed_message}")
32
 
33
- # Send multiple messages
34
- messages = ["Hello, World!", "Testing echo", "Final message"]
 
 
 
 
 
35
 
36
- for msg in messages:
37
- result = sql_envenv.step(SqlAction(message=msg))
38
- print(f"Sent: '{msg}'")
39
- print(f" → Echoed: '{result.observation.echoed_message}'")
40
- print(f" → Length: {result.observation.message_length}")
41
- print(f" → Reward: {result.reward}")
42
 
43
- finally:
44
- # Always clean up
45
- sql_envenv.close()
46
  ```
47
 
48
- That's it! The `SqlEnv.from_docker_image()` method handles:
49
- - Starting the Docker container
50
- - Waiting for the server to be ready
51
- - Connecting to the environment
52
- - Container cleanup when you call `close()`
53
-
54
- ## Building the Docker Image
55
-
56
- Before using the environment, you need to build the Docker image:
57
-
58
- ```bash
59
- # From project root
60
- docker build -t sql_env-env:latest -f server/Dockerfile .
61
- ```
62
-
63
- ## Deploying to Hugging Face Spaces
64
-
65
- You can easily deploy your OpenEnv environment to Hugging Face Spaces using the `openenv push` command:
66
-
67
- ```bash
68
- # From the environment directory (where openenv.yaml is located)
69
- openenv push
70
-
71
- # Or specify options
72
- openenv push --namespace my-org --private
73
- ```
74
-
75
- The `openenv push` command will:
76
- 1. Validate that the directory is an OpenEnv environment (checks for `openenv.yaml`)
77
- 2. Prepare a custom build for Hugging Face Docker space (enables web interface)
78
- 3. Upload to Hugging Face (ensuring you're logged in)
79
-
80
- ### Prerequisites
81
-
82
- - Authenticate with Hugging Face: The command will prompt for login if not already authenticated
83
-
84
- ### Options
85
-
86
- - `--directory`, `-d`: Directory containing the OpenEnv environment (defaults to current directory)
87
- - `--repo-id`, `-r`: Repository ID in format 'username/repo-name' (defaults to 'username/env-name' from openenv.yaml)
88
- - `--base-image`, `-b`: Base Docker image to use (overrides Dockerfile FROM)
89
- - `--private`: Deploy the space as private (default: public)
90
-
91
- ### Examples
92
-
93
- ```bash
94
- # Push to your personal namespace (defaults to username/env-name from openenv.yaml)
95
- openenv push
96
-
97
- # Push to a specific repository
98
- openenv push --repo-id my-org/my-env
99
-
100
- # Push with a custom base image
101
- openenv push --base-image ghcr.io/meta-pytorch/openenv-base:latest
102
-
103
- # Push as a private space
104
- openenv push --private
105
-
106
- # Combine options
107
- openenv push --repo-id my-org/my-env --base-image custom-base:latest --private
108
- ```
109
-
110
- After deployment, your space will be available at:
111
- `https://huggingface.co/spaces/<repo-id>`
112
-
113
- The deployed space includes:
114
- - **Web Interface** at `/web` - Interactive UI for exploring the environment
115
- - **API Documentation** at `/docs` - Full OpenAPI/Swagger interface
116
- - **Health Check** at `/health` - Container health monitoring
117
- - **WebSocket** at `/ws` - Persistent session endpoint for low-latency interactions
118
-
119
- ## Environment Details
120
-
121
- ### Action
122
- **SqlAction**: Contains a single field
123
- - `message` (str) - The message to echo back
124
-
125
- ### Observation
126
- **SqlObservation**: Contains the echo response and metadata
127
- - `echoed_message` (str) - The message echoed back
128
- - `message_length` (int) - Length of the message
129
- - `reward` (float) - Reward based on message length (length × 0.1)
130
- - `done` (bool) - Always False for echo environment
131
- - `metadata` (dict) - Additional info like step count
132
-
133
- ### Reward
134
- The reward is calculated as: `message_length × 0.1`
135
- - "Hi" → reward: 0.2
136
- - "Hello, World!" → reward: 1.3
137
- - Empty message → reward: 0.0
138
-
139
- ## Advanced Usage
140
-
141
- ### Connecting to an Existing Server
142
-
143
- If you already have a Sql Env environment server running, you can connect directly:
144
-
145
- ```python
146
- from sql_env import SqlEnv
147
-
148
- # Connect to existing server
149
- sql_envenv = SqlEnv(base_url="<ENV_HTTP_URL_HERE>")
150
-
151
- # Use as normal
152
- result = sql_envenv.reset()
153
- result = sql_envenv.step(SqlAction(message="Hello!"))
154
  ```
155
 
156
- Note: When connecting to an existing server, `sql_envenv.close()` will NOT stop the server.
157
 
158
- ### Using the Context Manager
159
 
160
- The client supports context manager usage for automatic connection management:
 
 
 
 
 
161
 
162
- ```python
163
- from sql_env import SqlAction, SqlEnv
164
-
165
- # Connect with context manager (auto-connects and closes)
166
- with SqlEnv(base_url="http://localhost:8000") as env:
167
- result = env.reset()
168
- print(f"Reset: {result.observation.echoed_message}")
169
- # Multiple steps with low latency
170
- for msg in ["Hello", "World", "!"]:
171
- result = env.step(SqlAction(message=msg))
172
- print(f"Echoed: {result.observation.echoed_message}")
173
- ```
174
 
175
- The client uses WebSocket connections for:
176
- - **Lower latency**: No HTTP connection overhead per request
177
- - **Persistent session**: Server maintains your environment state
178
- - **Efficient for episodes**: Better for many sequential steps
 
 
179
 
180
- ### Concurrent WebSocket Sessions
181
-
182
- The server supports multiple concurrent WebSocket connections. To enable this,
183
- modify `server/app.py` to use factory mode:
184
-
185
- ```python
186
- # In server/app.py - use factory mode for concurrent sessions
187
- app = create_app(
188
- SqlEnvironment, # Pass class, not instance
189
- SqlAction,
190
- SqlObservation,
191
- max_concurrent_envs=4, # Allow 4 concurrent sessions
192
- )
193
- ```
194
 
195
- Then multiple clients can connect simultaneously:
196
 
197
- ```python
198
- from sql_env import SqlAction, SqlEnv
199
- from concurrent.futures import ThreadPoolExecutor
 
 
200
 
201
- def run_episode(client_id: int):
202
- with SqlEnv(base_url="http://localhost:8000") as env:
203
- result = env.reset()
204
- for i in range(10):
205
- result = env.step(SqlAction(message=f"Client {client_id}, step {i}"))
206
- return client_id, result.observation.message_length
207
-
208
- # Run 4 episodes concurrently
209
- with ThreadPoolExecutor(max_workers=4) as executor:
210
- results = list(executor.map(run_episode, range(4)))
211
- ```
212
-
213
- ## Development & Testing
214
-
215
- ### Direct Environment Testing
216
-
217
- Test the environment logic directly without starting the HTTP server:
218
 
219
  ```bash
220
- # From the server directory
221
- python3 server/sql_env_environment.py
 
222
  ```
223
 
224
- This verifies that:
225
- - Environment resets correctly
226
- - Step executes actions properly
227
- - State tracking works
228
- - Rewards are calculated correctly
229
-
230
- ### Running Locally
231
-
232
- Run the server locally for development:
233
 
234
- ```bash
235
- uvicorn server.app:app --reload
236
- ```
237
-
238
- ## Project Structure
239
-
240
- ```
241
- sql_env/
242
- ├── .dockerignore # Docker build exclusions
243
- ├── __init__.py # Module exports
244
- ├── README.md # This file
245
- ├── openenv.yaml # OpenEnv manifest
246
- ├── pyproject.toml # Project metadata and dependencies
247
- ├── uv.lock # Locked dependencies (generated)
248
- ├── client.py # SqlEnv client
249
- ├── models.py # Action and Observation models
250
- └── server/
251
- ├── __init__.py # Server module exports
252
- ├── sql_env_environment.py # Core environment logic
253
- ├── app.py # FastAPI application (HTTP + WebSocket endpoints)
254
- └── Dockerfile # Container image definition
255
- ```
 
1
  ---
2
+ title: SQLEnv - SQL Query Writing Environment
3
+ emoji: 🗃
4
  colorFrom: blue
5
+ colorTo: green
6
  sdk: docker
7
  pinned: false
8
+ app_port: 7860
 
9
  tags:
10
  - openenv
11
  ---
12
 
13
+ # SQLEnv SQL Query Writing Environment for AI Agents
14
 
15
+ An OpenEnv-compatible reinforcement learning environment where an AI agent learns to write correct SQL queries from natural language questions against a realistic e-commerce database.
16
 
17
+ ## Overview
18
 
19
+ The agent receives a database schema and a natural language question, submits SQL queries, and gets graded with rich partial-credit scoring.
20
 
21
+ **3 difficulty levels, 5 questions each:**
 
22
 
23
+ | Task | Difficulty | SQL Features |
24
+ |---|---|---|
25
+ | `basic_select` | Easy | WHERE, ORDER BY, LIMIT, COUNT |
26
+ | `join_aggregate` | Medium | JOIN, GROUP BY, HAVING, AVG, SUM |
27
+ | `advanced_analytics` | Hard | Subqueries, RANK(), LAG(), PARTITION BY |
28
 
29
+ ## API Endpoints
 
 
30
 
31
+ | Endpoint | Method | Description |
32
+ |---|---|---|
33
+ | `/health` | GET | Health check |
34
+ | `/reset` | POST | Reset environment, get first question |
35
+ | `/step` | POST | Submit SQL query, get graded result |
36
+ | `/state` | GET | Current episode state |
37
+ | `/docs` | GET | Interactive API documentation |
38
 
39
+ ## Action Space
 
 
 
 
 
40
 
41
+ ```json
42
+ {"action": {"query": "SELECT name, age FROM customers WHERE age > 30 ORDER BY age DESC"}}
 
43
  ```
44
 
45
+ ## Observation Space
46
+
47
+ ```json
48
+ {
49
+ "observation": {
50
+ "task_name": "basic_select",
51
+ "question": "Find all customers older than 30...",
52
+ "schema_description": "=== DATABASE SCHEMA === ...",
53
+ "query_result": "name | age ...",
54
+ "error": "",
55
+ "steps_remaining": 2,
56
+ "question_index": 1,
57
+ "total_questions": 5
58
+ },
59
+ "reward": 1.0,
60
+ "done": false
61
+ }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
62
  ```
63
 
64
+ ## Reward Function
65
 
66
+ Multi-component partial credit scoring (0.0 to 1.0):
67
 
68
+ | Component | Weight | What It Measures |
69
+ |---|---|---|
70
+ | Syntax | 0.1 | Query executes without error |
71
+ | Columns | 0.2 | Expected columns present |
72
+ | Rows | 0.3 | Expected rows match |
73
+ | Exact | 0.4 | Full result set matches ground truth |
74
 
75
+ ## Database
 
 
 
 
 
 
 
 
 
 
 
76
 
77
+ Realistic e-commerce database with 5 tables:
78
+ - **customers** (20 rows) - name, email, age, city, signup_date
79
+ - **products** (15 rows) - name, category, price, stock
80
+ - **orders** (30 rows) - customer_id, order_date, status, total_amount
81
+ - **order_items** (46 rows) - order_id, product_id, quantity, unit_price
82
+ - **reviews** (25 rows) - product_id, customer_id, rating, review_text
83
 
84
+ ## Baseline Scores
 
 
 
 
 
 
 
 
 
 
 
 
 
85
 
86
+ Tested with Llama 3.3 70B (via Groq):
87
 
88
+ | Task | Score |
89
+ |---|---|
90
+ | basic_select | 1.000 |
91
+ | join_aggregate | 1.000 |
92
+ | advanced_analytics | 0.969 |
93
 
94
+ ## Setup
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
95
 
96
  ```bash
97
+ pip install openenv-core
98
+ cd sql_env
99
+ uvicorn server.app:app --host 0.0.0.0 --port 7860
100
  ```
101
 
102
+ ## Environment Variables
 
 
 
 
 
 
 
 
103
 
104
+ | Variable | Default | Description |
105
+ |---|---|---|
106
+ | SQL_ENV_TASK | basic_select | Task to load |
107
+ | SQL_ENV_MAX_STEPS | 15 | Max steps per episode |