Team Ai
Apppublic

openenv/coding_env

sourceHugging Faceupdated 3mo agoView on Hugging Face
21likes
README.md164 linesDownload Raw Back to coding_env
1---2title: Coding Environment Server3emoji: ๐Ÿ’ป4colorFrom: blue5colorTo: blue6sdk: docker7pinned: false8app_port: 80009base_path: /web10tags:11  - openenv12---13 14# Coding Environment15 16A Python code execution environment that runs arbitrary Python code and returns results. Perfect for testing code execution infrastructure and demonstrating environment usage patterns.17 18## Quick Start19 20The simplest way to use the Coding environment is through the `CodingEnv` class. The client is **async by default**:21 22```python23import asyncio24from coding_env import CodeAction, CodingEnv25 26async def main():27    # Create environment from Docker image28    client = await CodingEnv.from_docker_image("coding-env:latest")29 30    async with client:31        # Reset32        result = await client.reset()33        print(f"Reset complete: exit_code={result.observation.exit_code}")34 35        # Execute Python code36        code_samples = [37            "print('Hello, World!')",38            "x = 5 + 3\nprint(f'Result: {x}')",39            "import math\nprint(math.pi)"40        ]41 42        for code in code_samples:43            result = await client.step(CodeAction(code=code))44            print(f"Code: {code}")45            print(f"  โ†’ stdout: {result.observation.stdout.strip()}")46            print(f"  โ†’ exit_code: {result.observation.exit_code}")47 48asyncio.run(main())49```50 51For **synchronous usage**, use the `.sync()` wrapper:52 53```python54from coding_env import CodeAction, CodingEnv55 56with CodingEnv(base_url="http://localhost:8000").sync() as client:57    result = client.reset()58    result = client.step(CodeAction(code="print('Hello!')"))59    print(result.observation.stdout)60```61 62The `CodingEnv.from_docker_image()` method handles:63- Starting the Docker container64- Waiting for the server to be ready65- Connecting to the environment66- Container cleanup when the context manager exits67 68## Building the Docker Image69 70Before using the environment, you need to build the Docker image:71 72```bash73# From project root74docker build -t coding-env:latest -f envs/coding_env/server/Dockerfile .75```76 77## Environment Details78 79### Action80**CodeAction**: Contains a single field81- `code` (str) - The Python code to execute82 83### Observation84**CodeObservation**: Contains the execution results85- `stdout` (str) - Standard output from code execution86- `stderr` (str) - Standard error from code execution87- `exit_code` (int) - Exit code (0 for success, non-zero for errors)88 89### State90**CodeState**: Tracks execution state91- `episode_id` (str) - Unique identifier for the episode92- `step_count` (int) - Number of steps taken93- `last_exit_code` (int) - Exit code from the last execution94 95## Advanced Usage96 97### Connecting to an Existing Server98 99If you already have a Coding environment server running, you can connect directly:100 101```python102from coding_env import CodeAction, CodingEnv103 104# Async usage105async with CodingEnv(base_url="http://localhost:8000") as client:106    result = await client.reset()107    result = await client.step(CodeAction(code="print('Hello!')"))108 109# Sync usage110with CodingEnv(base_url="http://localhost:8000").sync() as client:111    result = client.reset()112    result = client.step(CodeAction(code="print('Hello!')"))113```114 115Note: When connecting to an existing server, closing the client will NOT stop the server.116 117## Development & Testing118 119### Running Tests120 121Install the coding_env package with dev dependencies and run the tests from the repo root:122 123```bash124# Install coding_env with dev dependencies (includes smolagents and pytest)125uv pip install -e "envs/coding_env[dev]"126 127# Run unit tests (no Docker required)128uv run pytest tests/envs/test_python_codeact_reset.py tests/envs/test_python_codeact_rewards.py -v129 130# Run integration tests (requires Docker image to be built)131docker build -t coding-env:latest -f envs/coding_env/server/Dockerfile .132SKIP_DOCKER_TESTS=0 uv run pytest tests/envs/test_coding_env_integration.py -v133```134 135### Running the Full Example136 137Run the complete example that demonstrates the full workflow:138 139```bash140python3 envs/coding_env/client/example_usage.py141```142 143This example shows:144- Creating an environment from a Docker image145- Resetting and executing code through the environment146- Automatic cleanup with `close()`147 148## Project Structure149 150```151coding_env/152โ”œโ”€โ”€ README.md              # This file153โ”œโ”€โ”€ models.py              # Action, Observation, and State models154โ”œโ”€โ”€ client/155โ”‚   โ”œโ”€โ”€ coding_env_client.py  # CodingEnv client implementation156โ”‚   โ””โ”€โ”€ example_usage.py      # Usage examples157โ””โ”€โ”€ server/158    โ”œโ”€โ”€ python_codeact_env.py  # Core environment logic159    โ”œโ”€โ”€ app.py                 # FastAPI application160    โ”œโ”€โ”€ transforms.py          # Observation transforms161    โ”œโ”€โ”€ Dockerfile             # Container image definition162    โ””โ”€โ”€ README.md              # Server-specific documentation163```164