File size: 2,243 Bytes
508bc3b
 
24c085e
508bc3b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
24c085e
 
 
 
508bc3b
 
 
 
 
 
24c085e
 
508bc3b
 
 
 
 
24c085e
 
508bc3b
 
 
 
 
 
 
 
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
"""Data Cleaning Env Environment Client."""

from typing import Dict, Optional

from openenv.core import EnvClient
from openenv.core.client_types import StepResult
from openenv.core.env_server.types import State

from .models import DataCleaningAction, DataCleaningObservation

class DataCleaningEnv(
    EnvClient[DataCleaningAction, DataCleaningObservation, State]
):
    """
    Client for the Data Cleaning Env Environment.

    Example:
        >>> with DataCleaningEnv(base_url="http://localhost:8000") as client:
        ...     result = client.reset()
        ...     obs = result.observation
        ...     print(obs.current_text)
        ...     result = client.step(DataCleaningAction(operation="remove_duplicates"))
        ...     print(result.reward)
    """

    def _step_payload(self, action: DataCleaningAction) -> Dict:
        """Convert DataCleaningAction to JSON payload."""
        payload = {"operation": action.operation}
        if action.column is not None:
            payload["column"] = action.column
        return payload

    def _parse_result(self, payload: Dict) -> StepResult[DataCleaningObservation]:
        """Parse server response into StepResult[DataCleaningObservation]."""
        obs_data = payload.get("observation", {})
        
        # done and reward live at top-level in the server response
        done   = payload.get("done", obs_data.get("done", False))
        reward = payload.get("reward", obs_data.get("reward"))

        observation = DataCleaningObservation(
            current_text=obs_data.get("current_text", ""),
            is_normalized=obs_data.get("is_normalized", False),
            html_found=obs_data.get("html_found", False),
            remaining_typos=obs_data.get("remaining_typos", 0),
            done=done,
            reward=reward,
            metadata=obs_data.get("metadata", {}),
        )

        return StepResult(
            observation=observation,
            reward=reward,
            done=done,
        )

    def _parse_state(self, payload: Dict) -> State:
        """Parse server response into State object."""
        return State(
            episode_id=payload.get("episode_id"),
            step_count=payload.get("step_count", 0),
        )