-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclient.py
More file actions
112 lines (88 loc) · 3.69 KB
/
Copy pathclient.py
File metadata and controls
112 lines (88 loc) · 3.69 KB
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
"""
CleanRL Python client — a thin HTTP wrapper around the CleanRL OpenEnv server.
Usage:
from client import CleanRLClient
client = CleanRLClient("http://localhost:7860")
obs = client.reset("basic_tabular_cleaning")
while True:
action = {"action_type": "remove_duplicates"}
result = client.step(action)
print(result["reward"], result["done"])
if result["done"]:
break
"""
from __future__ import annotations
import json
from typing import Any
import requests
class CleanRLClient:
"""HTTP client for the CleanRL OpenEnv server."""
def __init__(self, base_url: str = "http://localhost:7860", timeout: int = 30) -> None:
self.base_url = base_url.rstrip("/")
self.timeout = timeout
# ── Core OpenEnv methods ───────────────────────────────────────────────
def reset(self, task_id: str = "basic_tabular_cleaning") -> dict[str, Any]:
"""Start a new episode. Returns the initial observation."""
r = requests.post(
f"{self.base_url}/reset",
params={"task_id": task_id},
timeout=self.timeout,
)
r.raise_for_status()
return r.json()
def step(self, action: dict[str, Any]) -> dict[str, Any]:
"""
Submit one action. Returns::
{
"observation": {...},
"reward": float,
"done": bool,
"info": {...}
}
"""
r = requests.post(
f"{self.base_url}/step",
json=action,
timeout=self.timeout,
)
r.raise_for_status()
return r.json()
def state(self) -> dict[str, Any]:
"""Return full environment state for the current session."""
r = requests.get(f"{self.base_url}/state", timeout=self.timeout)
r.raise_for_status()
return r.json()
def close(self) -> None:
"""No-op — HTTP env has no persistent connection to close."""
# ── Convenience helpers ────────────────────────────────────────────────
def auto_step(self) -> dict[str, Any]:
"""Trigger one heuristic auto-step (no LLM required)."""
r = requests.post(f"{self.base_url}/auto", timeout=self.timeout)
r.raise_for_status()
return r.json()
def health(self) -> dict[str, Any]:
r = requests.get(f"{self.base_url}/health", timeout=self.timeout)
r.raise_for_status()
return r.json()
def tasks(self) -> list[dict[str, Any]]:
"""List all available tasks."""
r = requests.get(f"{self.base_url}/tasks", timeout=self.timeout)
r.raise_for_status()
return r.json()["tasks"]
def ground_truth(self) -> dict[str, Any]:
"""Return the clean (target) dataset for the current session."""
r = requests.get(f"{self.base_url}/ground_truth", timeout=self.timeout)
r.raise_for_status()
return r.json()
def validate(self) -> dict[str, Any]:
"""Run heuristic agent on all tasks and return scores."""
r = requests.get(f"{self.base_url}/validate", timeout=120)
r.raise_for_status()
return r.json()
# ── Context manager support ────────────────────────────────────────────
def __enter__(self) -> "CleanRLClient":
return self
def __exit__(self, *_: Any) -> None:
self.close()
def __repr__(self) -> str:
return f"CleanRLClient(base_url={self.base_url!r})"