File size: 5,203 Bytes
dee32b3
 
 
 
 
5688cdb
19947c7
 
dee32b3
 
 
9540270
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
---
title: RL Doc Parser
emoji: πŸ€–
colorFrom: blue
colorTo: green
sdk: docker
app_port: 7860
app_file: root/server.py
pinned: false
---

# RL-Documentation-Parser

A Reinforcement Learning environment built with Gymnasium where an agent learns to navigate a mock file system to answer user queries using technical documentation.

## What the Environment Does

The `DocParserEnv` simulates an agent navigating a file system to answer technical questions - simulating a developer searching through documentation:

1. **Task**: Given a user query about technical documentation, the agent must explore directories, read relevant .md files, and submit the correct answer
2. **Actions**: The agent can perform 4 actions:
   - `ls` (0) - List current directory contents
   - `cd` (1) - Change to a subdirectory
   - `read` (2) - Open and extract text from a file
   - `submit_answer` (3) - Submit the final answer to end the episode
3. **Rewards**:
   - +100: Correct answer submitted (matches ground truth)
   - -1: Per-step penalty (encourages shortest path to solution)
   - -5: Invalid action (e.g., cd into a file, read a folder)
   - -20: Wrong answer submitted

## Project Structure
 
```
RL-Documentation-Parser/
β”œβ”€β”€ app.py                 # FastAPI wrapper (HuggingFace Spaces entry point)
β”œβ”€β”€ rl_doc_parser.py       # DocParserEnv + training loop
β”œβ”€β”€ inference.py           # Baseline inference script (OpenAI API)
β”œβ”€β”€ validate.py            # Pre-submission validation script
β”œβ”€β”€ openenv.yaml           # OpenEnv spec metadata
β”œβ”€β”€ qa_pairs.json          # Ground truth QA pairs
β”œβ”€β”€ Dockerfile             # Docker config for HuggingFace Spaces
β”œβ”€β”€ requirements.txt       # Python dependencies
└── root/                  # Mock filesystem
    β”œβ”€β”€ network/
    β”‚   β”œβ”€β”€ ssl_config.md
    β”‚   └── protocols.md
    β”œβ”€β”€ db/
    β”‚   β”œβ”€β”€ postgres_setup.md
    β”‚   └── optimization.md
    └── api/
        └── oauth_guide.md
```

## Installation

### 1. Create Virtual Environment

```bash
python -m venv venv
```

### 2. Activate Virtual Environment

```bash
# Linux/macOS
source venv/bin/activate

# Windows
venv\Scripts\activate
```

### 3. Install Dependencies

```bash
pip install -r requirements.txt
```

## How to Run the Demo

```bash
python rl_doc_parser.py
```

## API Endpoints
 
The environment is served via FastAPI on port 7860:
 
| Method | Path | Description |
|--------|------|-------------|
| `GET` | `/` | Landing page |
| `POST` | `/reset` | Reset the environment |
| `POST` | `/step` | Take a step |
| `GET` | `/state` | Current state |
| `GET` | `/openenv.yaml` | OpenEnv metadata |
| `GET` | `/tasks` | List tasks |
| `GET` | `/health` | Health check |
| `GET` | `/docs` | Swagger UI |
 
### Example: Run an Episode via curl
 
```bash
# Reset
curl -X POST https://<your-space>.hf.space/reset
 
# Take an action (ls = 0)
curl -X POST https://<your-space>.hf.space/step \
  -H "Content-Type: application/json" \
  -d '{"action": 0}'
 
# cd into network/
curl -X POST https://<your-space>.hf.space/step \
  -H "Content-Type: application/json" \
  -d '{"action": 1, "target": "network"}'
 
# Read a file
curl -X POST https://<your-space>.hf.space/step \
  -H "Content-Type: application/json" \
  -d '{"action": 2, "target": "ssl_config.md"}'
 
# Submit answer
curl -X POST https://<your-space>.hf.space/step \
  -H "Content-Type: application/json" \
  -d '{"action": 3, "target": "TLS 1.3"}'
```
## Local Development
 
```bash
# 1. Clone
git clone https://github.com/MalG4850/RL-Documentation-Parser.git
cd RL-Documentation-Parser
 
# 2. Install deps
pip install -r requirements.txt
 
# 3. Run the API server
uvicorn app:app --host 0.0.0.0 --port 7860 --reload
 
# 4. Or run the original demo
python rl_doc_parser.py
```
 
## Docker
 
```bash
# Build
docker build -t rl-doc-parser .
 
# Run
docker run -p 7860:7860 rl-doc-parser
```
 
## Baseline Inference
 
```bash
export OPENAI_API_KEY=sk-...
python inference.py
```
 
Results are saved to `baseline_results.json`.
 
## Validation
 
```bash
python validate.py
```
 
## Tasks
 
| ID | Difficulty | Description |
|----|-----------|-------------|
| `find_ssl_config` | Easy | Locate SSL/TLS configuration details |
| `find_postgres_setup` | Medium | Find PostgreSQL setup instructions |
| `find_oauth_guide` | Hard | Retrieve OAuth 2.0 authentication steps |

This runs a 100-step training loop demonstrating the environment:
- Shows user queries being posed
- Displays actions taken and rewards received
- Outputs episode statistics at completion

## Important Setup Steps

1. **Virtual Environment**: Always activate `venv` before running
2. **Working Directory**: Run from the project root (`RL-Documentation-Parser/`)
3. **File System**: The `root/` directory contains mock documentation - do not modify during execution
4. **Offline Execution**: The demo runs entirely offline with random actions

## Environment Details

- **Observation Space**: Dict containing current_path, dir_contents, user_query
- **Action Space**: Discrete(4)
- **Max Steps per Episode**: 20
- **Ground Truth**: 5 QA pairs stored in qa_pairs.json