File size: 1,508 Bytes
de07414
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import requests
from typing import Dict, Any, Optional

from server.models import Action, StepResult, ResetResult

class EnvironmentalClient:
    """
    Official OpenEnv client wrapper to interface with the containerized environment endpoints.
    Can be used by baseline inference scripts or remote agent evaluations.
    """
    def __init__(self, base_url: str = "http://127.0.0.1:7860"):
        self.base_url = base_url.rstrip('/')
        
    def reset(self, task_id: int = 1) -> ResetResult:
        """
        Calls the /reset endpoint to begin a new episode for the specified task.
        """
        response = requests.post(f"{self.base_url}/reset", json={"task_id": task_id})
        response.raise_for_status()
        return ResetResult(**response.json())
        
    def step(self, action: Action) -> StepResult:
        """
        Submits a step (action) to the environment and returns the updated state/reward.
        """
        # Pydantic dict serialization for requests
        response = requests.post(f"{self.base_url}/step", json=action.dict())
        response.raise_for_status()
        return StepResult(**response.json())
        
    def state(self) -> Dict[str, Any]:
        """
        Extracts the unstructured metadata state of the running environment.
        """
        response = requests.get(f"{self.base_url}/state")
        response.raise_for_status()
        return response.json()

# Provide a default singleton for ease of use
client = EnvironmentalClient()