|
| 1 | +# OpenEnv Core |
| 2 | + |
| 3 | +Core components for OpenEnv - a framework for building HTTP-based agentic environments. |
| 4 | + |
| 5 | +## Overview |
| 6 | + |
| 7 | +`openenv-core` provides the foundational building blocks for creating and interacting with containerized environments over HTTP. It enables you to build agent environments that can be deployed as Docker containers and accessed via a simple HTTP API. |
| 8 | + |
| 9 | +## Features |
| 10 | + |
| 11 | +- **HTTPEnvClient**: Generic HTTP client for interacting with remote environments |
| 12 | +- **HTTPEnvServer**: FastAPI-based server wrapper for exposing environments over HTTP |
| 13 | +- **Container Providers**: Pluggable architecture for running containers (Docker, Kubernetes, etc.) |
| 14 | +- **Type System**: Strongly-typed Action/Observation/State interfaces |
| 15 | +- **Web Interface**: Optional web UI for interacting with environments |
| 16 | + |
| 17 | +## Installation |
| 18 | + |
| 19 | +```bash |
| 20 | +pip install openenv-core |
| 21 | +``` |
| 22 | + |
| 23 | +For development: |
| 24 | +```bash |
| 25 | +pip install openenv-core[dev] |
| 26 | +``` |
| 27 | + |
| 28 | +## Quick Start |
| 29 | + |
| 30 | +### Creating an Environment Client |
| 31 | + |
| 32 | +```python |
| 33 | +from openenv_core import HTTPEnvClient, StepResult |
| 34 | +from dataclasses import dataclass |
| 35 | + |
| 36 | +@dataclass |
| 37 | +class MyAction: |
| 38 | + text: str |
| 39 | + |
| 40 | +@dataclass |
| 41 | +class MyObservation: |
| 42 | + response: str |
| 43 | + |
| 44 | +class MyEnvClient(HTTPEnvClient[MyAction, MyObservation]): |
| 45 | + def _step_payload(self, action: MyAction) -> dict: |
| 46 | + return {"text": action.text} |
| 47 | + |
| 48 | + def _parse_result(self, payload: dict) -> StepResult[MyObservation]: |
| 49 | + obs_data = payload["observation"] |
| 50 | + return StepResult( |
| 51 | + observation=MyObservation(**obs_data), |
| 52 | + reward=payload.get("reward"), |
| 53 | + done=payload.get("done", False) |
| 54 | + ) |
| 55 | + |
| 56 | + def _parse_state(self, payload: dict) -> Any: |
| 57 | + return payload |
| 58 | + |
| 59 | +# Use with Docker |
| 60 | +env = MyEnvClient.from_docker_image("my-env:latest") |
| 61 | +result = env.reset() |
| 62 | +step_result = env.step(MyAction(text="hello")) |
| 63 | +env.close() |
| 64 | +``` |
| 65 | + |
| 66 | +### Creating an Environment Server |
| 67 | + |
| 68 | +```python |
| 69 | +from openenv_core.env_server import Environment, HTTPEnvServer, create_app |
| 70 | +from dataclasses import dataclass |
| 71 | + |
| 72 | +@dataclass |
| 73 | +class MyAction: |
| 74 | + text: str |
| 75 | + |
| 76 | +@dataclass |
| 77 | +class MyObservation: |
| 78 | + response: str |
| 79 | + reward: float = 0.0 |
| 80 | + done: bool = False |
| 81 | + |
| 82 | +class MyEnvironment(Environment): |
| 83 | + def reset(self) -> MyObservation: |
| 84 | + return MyObservation(response="Ready") |
| 85 | + |
| 86 | + def step(self, action: MyAction) -> MyObservation: |
| 87 | + return MyObservation( |
| 88 | + response=f"Echo: {action.text}", |
| 89 | + reward=1.0, |
| 90 | + done=False |
| 91 | + ) |
| 92 | + |
| 93 | +# Create FastAPI app |
| 94 | +env = MyEnvironment() |
| 95 | +app = create_app(env, MyAction, MyObservation) |
| 96 | + |
| 97 | +# Run with: uvicorn module:app --host 0.0.0.0 --port 8000 |
| 98 | +``` |
| 99 | + |
| 100 | +## Container Providers |
| 101 | + |
| 102 | +OpenEnv Core supports multiple container providers: |
| 103 | + |
| 104 | +### Local Docker Provider |
| 105 | + |
| 106 | +```python |
| 107 | +from openenv_core.containers.runtime import LocalDockerProvider |
| 108 | + |
| 109 | +provider = LocalDockerProvider() |
| 110 | +base_url = provider.start_container("my-env:latest") |
| 111 | +provider.wait_for_ready(base_url) |
| 112 | +# Use environment... |
| 113 | +provider.stop_container() |
| 114 | +``` |
| 115 | + |
| 116 | +### Kubernetes Provider (Coming Soon) |
| 117 | + |
| 118 | +```python |
| 119 | +from openenv_core.containers.runtime import KubernetesProvider |
| 120 | + |
| 121 | +provider = KubernetesProvider(namespace="envs") |
| 122 | +base_url = provider.start_container("my-env:latest") |
| 123 | +# Use environment... |
| 124 | +provider.stop_container() |
| 125 | +``` |
| 126 | + |
| 127 | +## Architecture |
| 128 | + |
| 129 | +OpenEnv Core follows a client-server architecture: |
| 130 | + |
| 131 | +``` |
| 132 | +┌─────────────────┐ HTTP ┌─────────────────┐ |
| 133 | +│ │◄─────────────────────►│ │ |
| 134 | +│ HTTPEnvClient │ /reset, /step │ HTTPEnvServer │ |
| 135 | +│ │ /state, /health │ │ |
| 136 | +└─────────────────┘ └─────────────────┘ |
| 137 | + │ │ |
| 138 | + │ │ |
| 139 | + ▼ ▼ |
| 140 | +┌─────────────────┐ ┌─────────────────┐ |
| 141 | +│ Container │ │ Environment │ |
| 142 | +│ Provider │ │ Implementation │ |
| 143 | +└─────────────────┘ └─────────────────┘ |
| 144 | +``` |
| 145 | + |
| 146 | +## API Reference |
| 147 | + |
| 148 | +### HTTPEnvClient |
| 149 | + |
| 150 | +Base class for environment clients with these abstract methods: |
| 151 | + |
| 152 | +- `_step_payload(action)`: Convert action to JSON |
| 153 | +- `_parse_result(payload)`: Parse response to StepResult |
| 154 | +- `_parse_state(payload)`: Parse state response |
| 155 | + |
| 156 | +### HTTPEnvServer |
| 157 | + |
| 158 | +Server wrapper with these methods: |
| 159 | + |
| 160 | +- `register_routes(app)`: Register endpoints on FastAPI app |
| 161 | +- `_deserialize_action(data)`: Convert JSON to Action |
| 162 | +- `_serialize_observation(obs)`: Convert Observation to JSON |
| 163 | + |
| 164 | +### Environment Interface |
| 165 | + |
| 166 | +Base interface for environment implementations: |
| 167 | + |
| 168 | +- `reset()`: Reset environment and return initial observation |
| 169 | +- `step(action)`: Execute action and return observation |
| 170 | +- `state`: Property returning current environment state |
| 171 | + |
| 172 | +## License |
| 173 | + |
| 174 | +This project is licensed under the BSD-3-Clause License - see the LICENSE file for details. |
| 175 | + |
| 176 | +## Contributing |
| 177 | + |
| 178 | +Contributions are welcome! Please see the main OpenEnv repository for contribution guidelines. |
| 179 | + |
| 180 | +## Links |
| 181 | + |
| 182 | +- **Homepage**: https://github.com/facebookresearch/OpenEnv |
| 183 | +- **Documentation**: https://github.com/facebookresearch/OpenEnv/blob/main/README.md |
| 184 | +- **Bug Tracker**: https://github.com/facebookresearch/OpenEnv/issues |
0 commit comments