Spaces:
Sleeping
Sleeping
Upload folder using huggingface_hub
Browse files
README.md
CHANGED
|
@@ -18,6 +18,15 @@ short_description: OpenCode coding agent in an E2B sandbox with logprob capture
|
|
| 18 |
an isolated [E2B](https://e2b.dev) sandbox against any OpenAI-compatible
|
| 19 |
LLM endpoint, optionally capturing per-token logprobs for GRPO training.
|
| 20 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 21 |
The env is **task-agnostic** — every rollout is configured at call-time
|
| 22 |
with a uniform Task shape:
|
| 23 |
|
|
@@ -33,41 +42,81 @@ a float to `/home/user/logs/verifier/reward.txt` (override).
|
|
| 33 |
|
| 34 |
## Quick Start
|
| 35 |
|
| 36 |
-
###
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 37 |
|
| 38 |
```python
|
| 39 |
import os
|
| 40 |
from opencode_env import OpenCodeEnv
|
| 41 |
|
| 42 |
-
|
|
|
|
| 43 |
env.reset()
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
instruction=(
|
| 47 |
-
"Create binary_search.py exposing def binary_search(arr, target) -> int "
|
| 48 |
-
"that returns the index of target in arr, or -1 if absent. Use a "
|
| 49 |
-
"relative path."
|
| 50 |
-
),
|
| 51 |
-
setup=[],
|
| 52 |
-
verify=[
|
| 53 |
-
"test -f /home/user/workdir/binary_search.py",
|
| 54 |
-
"python -c \"import sys; sys.path.insert(0, '/home/user/workdir'); "
|
| 55 |
-
"import binary_search; "
|
| 56 |
-
"assert binary_search.binary_search([1,2,3], 2) == 1\"",
|
| 57 |
-
],
|
| 58 |
-
task_id="binary_search_v1",
|
| 59 |
-
template="opencode-rl", # prebaked E2B template
|
| 60 |
-
)
|
| 61 |
-
print("reward:", result.reward)
|
| 62 |
-
print("turns:", len(result.proxy_turns))
|
| 63 |
-
print("files:", list(result.files.keys()))
|
| 64 |
```
|
| 65 |
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
|
|
|
| 69 |
|
| 70 |
-
|
| 71 |
|
| 72 |
```python
|
| 73 |
import os
|
|
@@ -83,7 +132,7 @@ factory = OpenCodeSessionFactory(
|
|
| 83 |
model="gpt-4o-mini",
|
| 84 |
),
|
| 85 |
sandbox_backend=E2BSandboxBackend(),
|
| 86 |
-
mode="transparent_proxy", #
|
| 87 |
)
|
| 88 |
session = factory.create(task=OpenCodeTask(instruction="..."))
|
| 89 |
session.wait_for_completion()
|
|
|
|
| 18 |
an isolated [E2B](https://e2b.dev) sandbox against any OpenAI-compatible
|
| 19 |
LLM endpoint, optionally capturing per-token logprobs for GRPO training.
|
| 20 |
|
| 21 |
+
**🚀 Try it live**: [`AdithyaSK/opencode-env`](https://huggingface.co/spaces/AdithyaSK/opencode-env)
|
| 22 |
+
|
| 23 |
+
The deployed Space exposes:
|
| 24 |
+
|
| 25 |
+
- **Web UI** at [`/web`](https://adithyask-opencode-env.hf.space/web) — pick endpoint, write task, hit Run, watch live phase log + reward + logprobs.
|
| 26 |
+
- **MCP tool API** at [`/mcp`](https://adithyask-opencode-env.hf.space/mcp) — programmatic `run_rollout` calls.
|
| 27 |
+
- **OpenAPI docs** at [`/docs`](https://adithyask-opencode-env.hf.space/docs).
|
| 28 |
+
- **Health** at [`/health`](https://adithyask-opencode-env.hf.space/health).
|
| 29 |
+
|
| 30 |
The env is **task-agnostic** — every rollout is configured at call-time
|
| 31 |
with a uniform Task shape:
|
| 32 |
|
|
|
|
| 42 |
|
| 43 |
## Quick Start
|
| 44 |
|
| 45 |
+
### Async (default — talk to the deployed Space)
|
| 46 |
+
|
| 47 |
+
```python
|
| 48 |
+
import asyncio
|
| 49 |
+
import os
|
| 50 |
+
from opencode_env import OpenCodeEnv
|
| 51 |
+
from opencode_env.client import _extract_text
|
| 52 |
+
from opencode_env.models import RolloutResult
|
| 53 |
+
|
| 54 |
+
|
| 55 |
+
async def main():
|
| 56 |
+
SPACE = "https://adithyask-opencode-env.hf.space"
|
| 57 |
+
|
| 58 |
+
async with OpenCodeEnv(base_url=SPACE) as env:
|
| 59 |
+
await env.reset()
|
| 60 |
+
|
| 61 |
+
# The MCP tool returns JSON; deserialize via the typed model.
|
| 62 |
+
raw = await env.call_tool(
|
| 63 |
+
"run_rollout",
|
| 64 |
+
endpoint="openai", # vllm | openai | hf_router
|
| 65 |
+
api_key=os.environ["OPENAI_API_KEY"], # or set as a Space secret
|
| 66 |
+
instruction=(
|
| 67 |
+
"Create binary_search.py exposing def binary_search(arr, target) -> int "
|
| 68 |
+
"that returns the index of target in arr, or -1 if absent. Use a "
|
| 69 |
+
"relative path."
|
| 70 |
+
),
|
| 71 |
+
setup=[],
|
| 72 |
+
verify=[
|
| 73 |
+
"test -f /home/user/workdir/binary_search.py",
|
| 74 |
+
"python -c \"import sys; sys.path.insert(0, '/home/user/workdir'); "
|
| 75 |
+
"import binary_search; "
|
| 76 |
+
"assert binary_search.binary_search([1,2,3], 2) == 1; print('OK')\"",
|
| 77 |
+
],
|
| 78 |
+
template="opencode-rl", # prebaked E2B template
|
| 79 |
+
task_id="binary_search_v1",
|
| 80 |
+
)
|
| 81 |
+
result = RolloutResult.model_validate_json(_extract_text(raw))
|
| 82 |
+
|
| 83 |
+
print("reward:", result.reward)
|
| 84 |
+
print("turns:", len(result.proxy_turns))
|
| 85 |
+
print("files:", list(result.files.keys()))
|
| 86 |
+
print("wall:", result.wall_s, "s")
|
| 87 |
+
|
| 88 |
+
|
| 89 |
+
asyncio.run(main())
|
| 90 |
+
```
|
| 91 |
+
|
| 92 |
+
Expected output (~20s with the prebaked template):
|
| 93 |
+
|
| 94 |
+
```
|
| 95 |
+
reward: 1.0
|
| 96 |
+
turns: 3
|
| 97 |
+
files: ['/home/user/workdir/binary_search.py', ...]
|
| 98 |
+
wall: 19.8 s
|
| 99 |
+
```
|
| 100 |
+
|
| 101 |
+
### Sync wrapper
|
| 102 |
|
| 103 |
```python
|
| 104 |
import os
|
| 105 |
from opencode_env import OpenCodeEnv
|
| 106 |
|
| 107 |
+
# .sync() returns a synchronous wrapper around the async client.
|
| 108 |
+
with OpenCodeEnv(base_url="https://adithyask-opencode-env.hf.space").sync() as env:
|
| 109 |
env.reset()
|
| 110 |
+
# MCP tools are reachable via env.call_tool(...) / env.step(...) sync-wrapped.
|
| 111 |
+
# See the async example above for the full run_rollout signature.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 112 |
```
|
| 113 |
|
| 114 |
+
Point `base_url` at `http://localhost:8000` to talk to a local container
|
| 115 |
+
instead of the public Space.
|
| 116 |
+
|
| 117 |
+
### In-process primitive (no HTTP)
|
| 118 |
|
| 119 |
+
For trainers that want to drive a sandbox directly without an HTTP boundary:
|
| 120 |
|
| 121 |
```python
|
| 122 |
import os
|
|
|
|
| 132 |
model="gpt-4o-mini",
|
| 133 |
),
|
| 134 |
sandbox_backend=E2BSandboxBackend(),
|
| 135 |
+
mode="transparent_proxy", # captures per-token logprobs
|
| 136 |
)
|
| 137 |
session = factory.create(task=OpenCodeTask(instruction="..."))
|
| 138 |
session.wait_for_completion()
|