diff --git a/README.md b/README.md index 5169f40..a078900 100644 --- a/README.md +++ b/README.md @@ -2,6 +2,8 @@ An e2e framework for creating, deploying and using isolated execution environments for agentic RL training, built using Gymnasium style simple APIs. +[![Open In Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/meta-pytorch/OpenEnv/blob/main/examples/OpenEnv_Tutorial.ipynb) **← Try the Interactive Tutorial!** + ## Overview OpenEnv provides a standard for interacting with agentic execution environments via simple Gymnasium style APIs - step(), reset(), state(). Users of agentic execution environments can interact with the environment during RL training loops using these simple APIs. diff --git a/examples/OpenEnv_Tutorial.ipynb b/examples/OpenEnv_Tutorial.ipynb new file mode 100644 index 0000000..894d864 --- /dev/null +++ b/examples/OpenEnv_Tutorial.ipynb @@ -0,0 +1,1671 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "id": "cell-0", + "metadata": {}, + "source": [ + "
\n", + "\n", + "\"PyTorch\"\n", + "\n", + "\n", + "\n", + "# OpenEnv: Production RL Made Simple\n", + "\n", + "### *From \"Hello World\" to RL Training in 5 Minutes* ✨\n", + "\n", + "---\n", + "\n", + "**What if RL environments were as easy to use as REST APIs?**\n", + "\n", + "That's OpenEnv. Type-safe. Isolated. Production-ready. šŸŽÆ\n", + "\n", + "[![GitHub](https://img.shields.io/badge/GitHub-meta--pytorch%2FOpenEnv-blue?logo=github)](https://github.com/meta-pytorch/OpenEnv)\n", + "[![License](https://img.shields.io/badge/License-BSD%203--Clause-green.svg)](https://opensource.org/licenses/BSD-3-Clause)\n", + "[![PyTorch](https://img.shields.io/badge/PyTorch-EE4C2C?logo=pytorch&logoColor=white)](https://pytorch.org/)\n", + "\n", + "Author: [Sanyam Bhutani](http://twitter.com/bhutanisanyam1/)\n", + "\n", + "
\n", + "\n", + "\n", + "\n", + "---" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "---\n", + "\n", + "## Why OpenEnv?\n", + "\n", + "Let's take a trip down memory lane:\n", + "\n", + "It's 2016, RL is popular. You read some papers, it looks promising. \n", + "\n", + "But in real world: Cartpole is the best you can run on a gaming GPU. \n", + "\n", + "What do you do beyond Cartpole?\n", + "\n", + "Fast forward to 2025, GRPO is awesome and this time it's not JUST in theory, it works well in practise and is really here! \n", + "\n", + "The problem still remains, how do you take these RL algorithms and take them beyond Cartpole?\n", + "\n", + "A huge part of RL is giving your algorithms environment access to learn. \n", + "\n", + "We are excited to introduce an Environement Spec for adding Open Environments for RL Training. This will allow you to focus on your experiments and allow everyone to bring their environments. \n", + "\n", + "Focus on experiments, use OpenEnvironments, and build agents that go beyond Cartpole on a single spec.\n", + "\n", + "---" + ] + }, + { + "cell_type": "markdown", + "id": "cell-1", + "metadata": {}, + "source": [ + "## šŸ“‹ What You'll Learn\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "
\n", + "\n", + "**šŸŽÆ Part 1-2: The Fundamentals**\n", + "- ⚔ RL in 60 seconds\n", + "- šŸ¤” Why existing solutions fall short\n", + "- šŸ’” The OpenEnv solution\n", + "\n", + "\n", + "\n", + "**šŸ—ļø Part 3-5: The Architecture**\n", + "- šŸ”§ How OpenEnv works\n", + "- šŸ” Exploring real code\n", + "- šŸŽ® OpenSpiel integration example\n", + "\n", + "
\n", + "\n", + "**šŸŽ® Part 6-8: Hands-On Demo**\n", + "- šŸ”Œ Use existing OpenSpiel environment\n", + "- šŸ¤– Test 4 different policies\n", + "- šŸ‘€ Watch learning happen live\n", + "\n", + "\n", + "\n", + "**šŸ”§ Part 9-10: Going Further**\n", + "- šŸŽ® Switch to other OpenSpiel games\n", + "- ✨ Build your own integration\n", + "- 🌐 Deploy to production\n", + "\n", + "
\n", + "\n", + "> šŸ’” **Pro Tip**: This notebook is designed to run top-to-bottom in Google Colab with zero setup!\n", + ">\n", + "> ā±ļø **Time**: ~5 minutes | šŸ“Š **Difficulty**: Beginner-friendly | šŸŽÆ **Outcome**: Production-ready RL knowledge\n" + ] + }, + { + "cell_type": "markdown", + "id": "cell-2", + "metadata": {}, + "source": [ + "---\n", + "\n", + "## šŸ“‘ Table of Contents\n", + "\n", + "
\n", + "\n", + "**Quick Navigation** - Click any section to jump right there! šŸŽÆ\n", + "\n", + "### Foundation\n", + "- [Part 1: RL in 60 Seconds ā±ļø](#part-1)\n", + "- [Part 2: The Problem with Traditional RL 😤](#part-2)\n", + "- [Part 3: Setup šŸ› ļø](#part-3)\n", + "\n", + "### Architecture\n", + "- [Part 4: The OpenEnv Pattern šŸ—ļø](#part-4)\n", + "- [Part 5: Example Integration - OpenSpiel šŸŽ®](#part-5)\n", + "\n", + "### Hands-On Demo\n", + "- [Part 6: Interactive Demo šŸŽ®](#part-6)\n", + "- [Part 7: Four Policies šŸ¤–](#part-7)\n", + "- [Part 8: Policy Competition! šŸ†](#part-8)\n", + "\n", + "### Advanced\n", + "- [Part 9: Using Real OpenSpiel šŸŽ®](#part-9)\n", + "- [Part 10: Create Your Own Integration šŸ› ļø](#part-10)\n", + "\n", + "### Wrap Up\n", + "- [Summary: Your Journey šŸŽ“](#summary)\n", + "- [Resources šŸ“š](#resources)\n", + "\n", + "
\n", + "\n", + "---" + ] + }, + { + "cell_type": "markdown", + "id": "cell-3", + "metadata": {}, + "source": [ + "---\n", + "\n", + "# Part 1: RL in 60 Seconds ā±ļø\n", + "\n", + "
\n", + "\n", + "**Reinforcement Learning is simpler than you think.**\n", + "\n", + "It's just a loop:\n", + "\n", + "```\n", + "while not done:\n", + " observation = environment.observe()\n", + " action = policy.choose(observation)\n", + " reward = environment.step(action)\n", + " policy.learn(reward)\n", + "```\n", + "\n", + "That's it. That's RL.\n", + "\n", + "
\n", + "\n", + "Let's see it in action:" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "cell-4", + "metadata": {}, + "outputs": [], + "source": [ + "import random\n", + "\n", + "print(\"šŸŽ² \" + \"=\"*58 + \" šŸŽ²\")\n", + "print(\" Number Guessing Game - The Simplest RL Example\")\n", + "print(\"šŸŽ² \" + \"=\"*58 + \" šŸŽ²\")\n", + "\n", + "# Environment setup\n", + "target = random.randint(1, 10)\n", + "guesses_left = 3\n", + "\n", + "print(f\"\\nšŸŽÆ I'm thinking of a number between 1 and 10...\")\n", + "print(f\"šŸ’­ You have {guesses_left} guesses. Let's see how random guessing works!\\n\")\n", + "\n", + "# The RL Loop - Pure random policy (no learning!)\n", + "while guesses_left > 0:\n", + " # Policy: Random guessing (no learning yet!)\n", + " guess = random.randint(1, 10)\n", + " guesses_left -= 1\n", + " \n", + " print(f\"šŸ’­ Guess #{3-guesses_left}: {guess}\", end=\" → \")\n", + " \n", + " # Reward signal (but we're not using it!)\n", + " if guess == target:\n", + " print(\"šŸŽ‰ Correct! +10 points\")\n", + " break\n", + " elif abs(guess - target) <= 2:\n", + " print(\"šŸ”„ Warm! (close)\")\n", + " else:\n", + " print(\"ā„ļø Cold! (far)\")\n", + "else:\n", + " print(f\"\\nšŸ’” Out of guesses. The number was {target}.\")\n", + "\n", + "print(\"\\n\" + \"=\"*62)\n", + "print(\"šŸ’” This is RL: Observe → Act → Reward → Repeat\")\n", + "print(\" But this policy is terrible! It doesn't learn from rewards.\")\n", + "print(\"=\"*62 + \"\\n\")" + ] + }, + { + "cell_type": "markdown", + "id": "cell-5", + "metadata": {}, + "source": [ + "---\n", + "\n", + "\n", + "# Part 2: The Problem with Traditional RL 😤\n", + "\n", + "
\n", + "\n", + "## šŸ¤” Why Can't We Just Use OpenAI Gym?\n", + "\n", + "Good question! Gym is great for research, but production needs more...\n", + "\n", + "
\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "
ChallengeTraditional ApproachOpenEnv Solution
Type SafetyāŒ obs[0][3] - what is this?āœ… obs.info_state - IDE knows!
IsolationāŒ Same process (can crash your training)āœ… Docker containers (fully isolated)
DeploymentāŒ \"Works on my machine\" šŸ¤·āœ… Same container everywhere 🐳
ScalingāŒ Hard to distributeāœ… Deploy to Kubernetes ā˜øļø
LanguageāŒ Python onlyāœ… Any language (HTTP API) 🌐
DebuggingāŒ Cryptic numpy errorsāœ… Clear type errors šŸ›
\n", + "\n", + "
\n", + "\n", + "## šŸ’” The OpenEnv Philosophy\n", + "\n", + "**\"RL environments should be like microservices\"**\n", + "\n", + "Think of it like this: You don't run your database in the same process as your web server, right? Same principle!\n", + "\n", + "- šŸ”’ **Isolated**: Run in containers (security + stability)\n", + "- 🌐 **Standard**: HTTP API, works everywhere\n", + "- šŸ“¦ **Versioned**: Docker images (reproducibility!)\n", + "- šŸš€ **Scalable**: Deploy to cloud with one command\n", + "- šŸ›”ļø **Type-safe**: Catch bugs before they happen\n", + "- šŸ”„ **Portable**: Works on Mac, Linux, Windows, Cloud\n", + "\n", + "
" + ] + }, + { + "cell_type": "markdown", + "id": "cell-6", + "metadata": {}, + "source": [ + "### The Architecture\n", + "\n", + "```\n", + "ā”Œā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”\n", + "│ YOUR TRAINING CODE │\n", + "│ │\n", + "│ env = OpenSpielEnv(...) ← Import the client │\n", + "│ result = env.reset() ← Type-safe! │\n", + "│ result = env.step(action) ← Type-safe! │\n", + "│ │\n", + "ā””ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”¬ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”˜\n", + " │\n", + " │ HTTP/JSON (Language-Agnostic)\n", + " │ POST /reset, POST /step, GET /state\n", + " │\n", + "ā”Œā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā–¼ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”\n", + "│ DOCKER CONTAINER │\n", + "│ │\n", + "│ ā”Œā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā” │\n", + "│ │ FastAPI Server │ │\n", + "│ │ └─ Environment (reset, step, state) │ │\n", + "│ │ └─ Your Game/Simulation Logic │ │\n", + "│ ā””ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”˜ │\n", + "│ │\n", + "│ Isolated • Reproducible • Secure │\n", + "ā””ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”˜\n", + "```\n", + "\n", + "
\n", + "\n", + "**šŸŽÆ Key Insight**: You never see HTTP details - just clean Python methods!\n", + "\n", + "```python\n", + "env.reset() # Under the hood: HTTP POST to /reset\n", + "env.step(...) # Under the hood: HTTP POST to /step\n", + "env.state() # Under the hood: HTTP GET to /state\n", + "```\n", + "\n", + "The magic? OpenEnv handles all the plumbing. You focus on RL! ✨\n", + "\n", + "
" + ] + }, + { + "cell_type": "markdown", + "id": "cell-7", + "metadata": {}, + "source": [ + "---\n", + "\n", + "# Part 3: Setup šŸ› ļø\n", + "\n", + "
\n", + "\n", + "**Running in Colab?** This cell will clone OpenEnv and install dependencies automatically.\n", + "\n", + "**Running locally?** Make sure you're in the OpenEnv directory.\n", + "\n", + "
" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "cell-8", + "metadata": {}, + "outputs": [], + "source": [ + "# Detect environment\n", + "try:\n", + " import google.colab\n", + " IN_COLAB = True\n", + " print(\"🌐 Running in Google Colab - Perfect!\")\n", + "except ImportError:\n", + " IN_COLAB = False\n", + " print(\"šŸ’» Running locally - Nice!\")\n", + "\n", + "if IN_COLAB:\n", + " print(\"\\nšŸ“¦ Cloning OpenEnv repository...\")\n", + " !git clone https://github.com/meta-pytorch/OpenEnv.git > /dev/null 2>&1\n", + " %cd OpenEnv\n", + " \n", + " print(\"šŸ“š Installing dependencies (this takes ~10 seconds)...\")\n", + " !pip install -q fastapi uvicorn requests\n", + " \n", + " import sys\n", + " sys.path.insert(0, './src')\n", + " print(\"\\nāœ… Setup complete! Everything is ready to go! šŸŽ‰\")\n", + "else:\n", + " import sys\n", + " from pathlib import Path\n", + " sys.path.insert(0, str(Path.cwd().parent / 'src'))\n", + " print(\"āœ… Using local OpenEnv installation\")\n", + "\n", + "print(\"\\nšŸš€ Ready to explore OpenEnv and build amazing things!\")\n", + "print(\"šŸ’” Tip: Run cells top-to-bottom for the best experience.\\n\")" + ] + }, + { + "cell_type": "markdown", + "id": "cell-9", + "metadata": {}, + "source": [ + "---\n", + "\n", + "\n", + "# Part 4: The OpenEnv Pattern šŸ—ļø\n", + "\n", + "
\n", + "\n", + "## Every OpenEnv Environment Has 3 Components:\n", + "\n", + "```\n", + "src/envs/your_env/\n", + "ā”œā”€ā”€ šŸ“ models.py ← Type-safe contracts\n", + "│ (Action, Observation, State)\n", + "│\n", + "ā”œā”€ā”€ šŸ“± client.py ← What YOU import\n", + "│ (HTTPEnvClient implementation)\n", + "│\n", + "└── šŸ–„ļø server/\n", + " ā”œā”€ā”€ environment.py ← Game/simulation logic\n", + " ā”œā”€ā”€ app.py ← FastAPI server\n", + " └── Dockerfile ← Container definition\n", + "```\n", + "\n", + "
\n", + "\n", + "Let's explore the actual OpenEnv code to see how this works:" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "cell-10", + "metadata": {}, + "outputs": [], + "source": [ + "# Import OpenEnv's core abstractions\n", + "from core.env_server import Environment, Action, Observation, State\n", + "from core.http_env_client import HTTPEnvClient\n", + "\n", + "print(\"=\"*70)\n", + "print(\" 🧩 OPENENV CORE ABSTRACTIONS\")\n", + "print(\"=\"*70)\n", + "\n", + "print(\"\"\"\n", + "šŸ–„ļø SERVER SIDE (runs in Docker):\n", + "\n", + " class Environment(ABC):\n", + " '''Base class for all environment implementations'''\n", + " \n", + " @abstractmethod\n", + " def reset(self) -> Observation:\n", + " '''Start new episode'''\n", + " \n", + " @abstractmethod\n", + " def step(self, action: Action) -> Observation:\n", + " '''Execute action, return observation'''\n", + " \n", + " @property\n", + " def state(self) -> State:\n", + " '''Get episode metadata'''\n", + "\n", + "šŸ“± CLIENT SIDE (your training code):\n", + "\n", + " class HTTPEnvClient(ABC):\n", + " '''Base class for HTTP clients'''\n", + " \n", + " def reset(self) -> StepResult:\n", + " # HTTP POST /reset\n", + " \n", + " def step(self, action) -> StepResult:\n", + " # HTTP POST /step\n", + " \n", + " def state(self) -> State:\n", + " # HTTP GET /state\n", + "\"\"\")\n", + "\n", + "print(\"=\"*70)\n", + "print(\"\\n✨ Same interface on both sides - communication via HTTP!\")\n", + "print(\"šŸŽÆ You focus on RL, OpenEnv handles the infrastructure.\\n\")" + ] + }, + { + "cell_type": "markdown", + "id": "cell-11", + "metadata": {}, + "source": [ + "---\n", + "\n", + "# Part 5: Example Integration - OpenSpiel šŸŽ®\n", + "\n", + "
\n", + "\n", + "## What is OpenSpiel?\n", + "\n", + "**OpenSpiel** is a library from DeepMind with **70+ game environments** for RL research.\n", + "\n", + "## OpenEnv's Integration\n", + "\n", + "We've wrapped **6 OpenSpiel games** following the OpenEnv pattern:\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "
\n", + "\n", + "**šŸŽÆ Single-Player**\n", + "1. **Catch** - Catch falling ball\n", + "2. **Cliff Walking** - Navigate grid\n", + "3. **2048** - Tile puzzle\n", + "4. **Blackjack** - Card game\n", + "\n", + "\n", + "\n", + "**šŸ‘„ Multi-Player**\n", + "5. **Tic-Tac-Toe** - Classic 3Ɨ3\n", + "6. **Kuhn Poker** - Imperfect info poker\n", + "\n", + "
\n", + "\n", + "This shows how OpenEnv can wrap **any** existing RL library!\n", + "\n", + "
" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "cell-12", + "metadata": {}, + "outputs": [], + "source": [ + "from envs.openspiel_env.client import OpenSpielEnv\n", + "\n", + "print(\"=\"*70)\n", + "print(\" šŸ”Œ HOW OPENENV WRAPS OPENSPIEL\")\n", + "print(\"=\"*70)\n", + "\n", + "print(\"\"\"\n", + "class OpenSpielEnv(HTTPEnvClient[OpenSpielAction, OpenSpielObservation]):\n", + " \n", + " def _step_payload(self, action: OpenSpielAction) -> dict:\n", + " '''Convert typed action to JSON for HTTP'''\n", + " return {\n", + " \"action_id\": action.action_id,\n", + " \"game_name\": action.game_name,\n", + " }\n", + " \n", + " def _parse_result(self, payload: dict) -> StepResult:\n", + " '''Parse HTTP JSON response into typed observation'''\n", + " return StepResult(\n", + " observation=OpenSpielObservation(...),\n", + " reward=payload['reward'],\n", + " done=payload['done']\n", + " )\n", + "\n", + "\"\"\")\n", + "\n", + "print(\"─\" * 70)\n", + "print(\"\\n✨ Usage (works for ALL OpenEnv environments):\")\n", + "print(\"\"\"\n", + " env = OpenSpielEnv(base_url=\"http://localhost:8000\")\n", + " \n", + " result = env.reset()\n", + " # Returns StepResult[OpenSpielObservation] - Type safe!\n", + " \n", + " result = env.step(OpenSpielAction(action_id=2, game_name=\"catch\"))\n", + " # Type checker knows this is valid!\n", + " \n", + " state = env.state()\n", + " # Returns OpenSpielState\n", + "\"\"\")\n", + "\n", + "print(\"─\" * 70)\n", + "print(\"\\nšŸŽÆ This pattern works for ANY environment you want to wrap!\\n\")" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "cell-13", + "metadata": {}, + "outputs": [], + "source": [ + "# Import OpenSpiel integration models\n", + "from envs.openspiel_env.models import (\n", + " OpenSpielAction,\n", + " OpenSpielObservation,\n", + " OpenSpielState\n", + ")\n", + "from dataclasses import fields\n", + "\n", + "print(\"=\"*70)\n", + "print(\" šŸŽ® OPENSPIEL INTEGRATION - TYPE-SAFE MODELS\")\n", + "print(\"=\"*70)\n", + "\n", + "print(\"\\nšŸ“¤ OpenSpielAction (what you send):\")\n", + "print(\" \" + \"─\" * 64)\n", + "for field in fields(OpenSpielAction):\n", + " print(f\" • {field.name:20s} : {field.type}\")\n", + "\n", + "print(\"\\nšŸ“„ OpenSpielObservation (what you receive):\")\n", + "print(\" \" + \"─\" * 64)\n", + "for field in fields(OpenSpielObservation):\n", + " print(f\" • {field.name:20s} : {field.type}\")\n", + "\n", + "print(\"\\nšŸ“Š OpenSpielState (episode metadata):\")\n", + "print(\" \" + \"─\" * 64)\n", + "for field in fields(OpenSpielState):\n", + " print(f\" • {field.name:20s} : {field.type}\")\n", + "\n", + "print(\"\\n\" + \"=\"*70)\n", + "print(\"\\nšŸ’” Type safety means:\")\n", + "print(\" āœ… Your IDE autocompletes these fields\")\n", + "print(\" āœ… Typos are caught before running\")\n", + "print(\" āœ… Refactoring is safe\")\n", + "print(\" āœ… Self-documenting code\\n\")" + ] + }, + { + "cell_type": "markdown", + "id": "cell-14", + "metadata": {}, + "source": [ + "### How the Client Works\n", + "\n", + "
\n", + "\n", + "The client **inherits from HTTPEnvClient** and implements 3 methods:\n", + "\n", + "1. `_step_payload()` - Convert action → JSON\n", + "2. `_parse_result()` - Parse JSON → typed observation \n", + "3. `_parse_state()` - Parse JSON → state\n", + "\n", + "That's it! The base class handles all HTTP communication.\n", + "\n", + "
" + ] + }, + { + "cell_type": "markdown", + "id": "cell-15", + "metadata": {}, + "source": [ + "---\n", + "\n", + "
\n", + "\n", + "# šŸŽ® Part 6: Using Real OpenSpiel\n", + "\n", + "### Now let's USE a production environment!\n", + "\n", + "We'll play **Catch** using OpenEnv's **OpenSpiel integration** šŸŽÆ
\n", + "This is a REAL environment running in production at companies!\n", + "\n", + "
\n", + "\n", + "**Get ready for:**\n", + "- šŸ”Œ Using existing environments (not building)\n", + "- šŸ¤– Testing policies against real games\n", + "- šŸ“Š Live gameplay visualization\n", + "- šŸŽÆ Production-ready patterns\n", + "\n", + "
" + ] + }, + { + "cell_type": "markdown", + "id": "cell-16", + "metadata": {}, + "source": [ + "## The Game: Catch šŸ”“šŸ“\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "
\n", + "\n", + "```\n", + "⬜ ⬜ šŸ”“ ⬜ ⬜\n", + "⬜ ⬜ ⬜ ⬜ ⬜ Ball\n", + "⬜ ⬜ ⬜ ⬜ ⬜ falls\n", + "⬜ ⬜ ⬜ ⬜ ⬜ down\n", + "⬜ ⬜ šŸ“ ⬜ ⬜\n", + " Paddle\n", + "```\n", + "\n", + "\n", + "\n", + "**Rules:**\n", + "- 5Ɨ5 grid\n", + "- Ball falls from random column\n", + "- Move paddle left/right to catch it\n", + "\n", + "**Actions:**\n", + "- `0` = Move LEFT ā¬…ļø\n", + "- `1` = STAY šŸ›‘\n", + "- `2` = Move RIGHT āž”ļø\n", + "\n", + "**Reward:**\n", + "- `+1` if caught šŸŽ‰\n", + "- `0` if missed 😢\n", + "\n", + "
\n", + "\n", + "
\n", + "\n", + "**šŸŽÆ Why Catch?**\n", + "- Simple rules (easy to understand)\n", + "- Fast episodes (~5 steps)\n", + "- Clear success/failure\n", + "- Part of OpenSpiel's 70+ games!\n", + "\n", + "**šŸ’” The Big Idea:**\n", + "Instead of building this from scratch, we'll USE OpenEnv's existing OpenSpiel integration. Same interface, but production-ready!\n", + "\n", + "
" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "cell-17", + "metadata": {}, + "outputs": [], + "source": [ + "from envs.openspiel_env import OpenSpielEnv\n", + "from envs.openspiel_env.models import (\n", + " OpenSpielAction,\n", + " OpenSpielObservation,\n", + " OpenSpielState\n", + ")\n", + "from dataclasses import fields\n", + "\n", + "print(\"šŸŽ® \" + \"=\"*64 + \" šŸŽ®\")\n", + "print(\" āœ… Importing Real OpenSpiel Environment!\")\n", + "print(\"šŸŽ® \" + \"=\"*64 + \" šŸŽ®\\n\")\n", + "\n", + "print(\"šŸ“¦ What we just imported:\")\n", + "print(\" • OpenSpielEnv - HTTP client for OpenSpiel games\")\n", + "print(\" • OpenSpielAction - Type-safe actions\")\n", + "print(\" • OpenSpielObservation - Type-safe observations\")\n", + "print(\" • OpenSpielState - Episode metadata\\n\")\n", + "\n", + "print(\"šŸ“‹ OpenSpielObservation fields:\")\n", + "print(\" \" + \"─\" * 60)\n", + "for field in fields(OpenSpielObservation):\n", + " print(f\" • {field.name:25s} : {field.type}\")\n", + "\n", + "print(\"\\n\" + \"=\"*70)\n", + "print(\"\\nšŸ’” This is REAL OpenEnv code - used in production!\")\n", + "print(\" • Wraps 6 OpenSpiel games (Catch, Tic-Tac-Toe, Poker, etc.)\")\n", + "print(\" • Type-safe actions and observations\")\n", + "print(\" • Works via HTTP (we\\'ll see that next!)\\n\")" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "cell-18", + "metadata": {}, + "outputs": [], + "source": [ + "import subprocess\n", + "import time\n", + "import sys\n", + "import os\n", + "\n", + "print(\"šŸš€ \" + \"=\"*64 + \" šŸš€\")\n", + "print(\" Starting OpenSpiel Server (Catch Game)\")\n", + "print(\"šŸš€ \" + \"=\"*64 + \" šŸš€\\n\")\n", + "\n", + "# Check if open_spiel is installed\n", + "try:\n", + " import pyspiel\n", + " print(\"āœ… OpenSpiel is installed!\\n\")\n", + "except ImportError:\n", + " print(\"āš ļø OpenSpiel not found. Installing...\")\n", + " import subprocess\n", + " subprocess.check_call([sys.executable, \"-m\", \"pip\", \"install\", \"-q\", \"open_spiel\"])\n", + " print(\"āœ… OpenSpiel installed!\\n\")\n", + "\n", + "# Start the OpenSpiel server in background\n", + "print(\"⚔ Starting FastAPI server for OpenSpiel Catch...\")\n", + "print(\" (This uses REAL OpenEnv + OpenSpiel integration)\\n\")\n", + "\n", + "# Determine the correct path\n", + "if IN_COLAB:\n", + " work_dir = \"/content/OpenEnv\"\n", + "else:\n", + " from pathlib import Path\n", + " work_dir = str(Path.cwd().parent.absolute())\n", + "\n", + "server_process = subprocess.Popen(\n", + " [sys.executable, \"-m\", \"uvicorn\",\n", + " \"envs.openspiel_env.server.app:app\",\n", + " \"--host\", \"0.0.0.0\",\n", + " \"--port\", \"8000\"],\n", + " env={**os.environ,\n", + " \"PYTHONPATH\": f\"{work_dir}/src\",\n", + " \"OPENSPIEL_GAME\": \"catch\",\n", + " \"OPENSPIEL_AGENT_PLAYER\": \"0\",\n", + " \"OPENSPIEL_OPPONENT_POLICY\": \"random\"},\n", + " stdout=subprocess.PIPE,\n", + " stderr=subprocess.PIPE,\n", + " text=True,\n", + " cwd=work_dir\n", + ")\n", + "\n", + "# Wait for server to start\n", + "print(\"ā³ Waiting for server to start...\")\n", + "time.sleep(5)\n", + "\n", + "# Check if server is running\n", + "import requests\n", + "try:\n", + " response = requests.get('http://localhost:8000/health', timeout=2)\n", + " print(\"\\nāœ… OpenSpiel server is running!\")\n", + " print(\"🌐 Server URL: http://localhost:8000\")\n", + " print(\"šŸ“ Endpoints available:\")\n", + " print(\" • POST /reset\")\n", + " print(\" • POST /step\")\n", + " print(\" • GET /state\")\n", + " print(\"\\nšŸŽÆ This is REAL OpenEnv + OpenSpiel in action!\")\n", + " print(\" • Running actual OpenSpiel Catch game\")\n", + " print(\" • Exposed via FastAPI HTTP server\")\n", + " print(\" • Using OpenEnv's standard interface\\n\")\n", + "except Exception as e:\n", + " print(f\"\\nāŒ Server failed to start: {e}\")\n", + " print(\"\\nšŸ“‹ Checking error output...\")\n", + " server_process.poll()\n", + " if server_process.stderr:\n", + " stderr = server_process.stderr.read()\n", + " if stderr:\n", + " print(stderr)\n", + " print(\"\\nšŸ’” Make sure open_spiel is installed:\")\n", + " print(\" pip install open_spiel\")\n", + " raise" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "cell-19", + "metadata": {}, + "outputs": [], + "source": [ + "print(\"šŸ“± \" + \"=\"*64 + \" šŸ“±\")\n", + "print(\" Connecting to OpenSpiel Server via HTTP\")\n", + "print(\"šŸ“± \" + \"=\"*64 + \" šŸ“±\\n\")\n", + "\n", + "# Create HTTP client for OpenSpiel\n", + "client = OpenSpielEnv(base_url=\"http://localhost:8000\")\n", + "\n", + "print(\"āœ… Client created!\")\n", + "print(\"\\nšŸ’” What just happened:\")\n", + "print(\" • OpenSpielEnv is an HTTPEnvClient subclass\")\n", + "print(\" • It knows how to talk to OpenSpiel servers\")\n", + "print(\" • All communication is type-safe and over HTTP\")\n", + "print(\" • Same client works for ALL OpenSpiel games!\\n\")" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "cell-20", + "metadata": {}, + "outputs": [], + "source": [ + "print(\"šŸŽ® \" + \"=\"*64 + \" šŸŽ®\")\n", + "print(\" Testing Connection - Playing One Step\")\n", + "print(\"šŸŽ® \" + \"=\"*64 + \" šŸŽ®\\n\")\n", + "\n", + "# Reset the environment (HTTP POST /reset)\n", + "print(\"šŸ“¤ Calling client.reset()...\")\n", + "print(\" Under the hood: HTTP POST to http://localhost:8000/reset\\n\")\n", + "\n", + "result = client.reset()\n", + "\n", + "print(\"šŸ“„ Received OpenSpielObservation:\")\n", + "print(f\" • info_state: {result.observation.info_state[:10]}... (first 10 values)\")\n", + "print(f\" • legal_actions: {result.observation.legal_actions}\")\n", + "print(f\" • game_phase: {result.observation.game_phase}\")\n", + "print(f\" • done: {result.done}\")\n", + "\n", + "# Take an action (HTTP POST /step)\n", + "print(\"\\nšŸ“¤ Calling client.step(OpenSpielAction(action_id=1, game_name=\\'catch\\'))...\")\n", + "print(\" Under the hood: HTTP POST to http://localhost:8000/step\\n\")\n", + "\n", + "action = OpenSpielAction(action_id=1, game_name=\"catch\") # STAY\n", + "result = client.step(action)\n", + "\n", + "print(\"šŸ“„ Received response:\")\n", + "print(f\" • Reward: {result.reward}\")\n", + "print(f\" • Done: {result.done}\")\n", + "print(f\" • legal_actions: {result.observation.legal_actions}\")\n", + "\n", + "# Get state (HTTP GET /state)\n", + "state = client.state()\n", + "print(f\"\\nšŸ“Š Episode state:\")\n", + "print(f\" • episode_id: {state.episode_id}\")\n", + "print(f\" • step_count: {state.step_count}\")\n", + "print(f\" • game_name: {state.game_name}\")\n", + "\n", + "print(\"\\n\" + \"=\"*70)\n", + "print(\"\\nšŸŽ‰ IT WORKS! We\\'re using REAL OpenSpiel via HTTP!\")\n", + "print(\" āœ… Type-safe communication\")\n", + "print(\" āœ… Same interface as any OpenEnv environment\")\n", + "print(\" āœ… Production-ready architecture\\n\")" + ] + }, + { + "cell_type": "markdown", + "id": "cell-21", + "metadata": {}, + "source": [ + "---\n", + "\n", + "# Part 7: Four Policies šŸ¤–\n", + "\n", + "
\n", + "\n", + "## Let's test 4 different AI strategies:\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "
PolicyStrategyExpected Performance
šŸŽ² RandomPick random action every step~20% (pure luck)
šŸ›‘ Always StayNever move, hope ball lands in center~20% (terrible!)
🧠 SmartMove paddle toward ball100% (optimal!)
šŸ“ˆ LearningStart random, learn smart strategy~85% (improves over time)
\n", + "\n", + "**šŸ’” These policies work with ANY OpenSpiel game!**\n", + "\n", + "
" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "cell-22", + "metadata": {}, + "outputs": [], + "source": [ + "import random\n", + "\n", + "# ============================================================================\n", + "# POLICIES - Different AI strategies (adapted for OpenSpiel)\n", + "# ============================================================================\n", + "\n", + "class RandomPolicy:\n", + " \"\"\"Baseline: Pure random guessing.\"\"\"\n", + " name = \"šŸŽ² Random Guesser\"\n", + "\n", + " def select_action(self, obs: OpenSpielObservation) -> int:\n", + " return random.choice(obs.legal_actions)\n", + "\n", + "\n", + "class AlwaysStayPolicy:\n", + " \"\"\"Bad strategy: Never moves.\"\"\"\n", + " name = \"šŸ›‘ Always Stay\"\n", + "\n", + " def select_action(self, obs: OpenSpielObservation) -> int:\n", + " return 1 # STAY\n", + "\n", + "\n", + "class SmartPolicy:\n", + " \"\"\"Optimal: Move paddle toward ball.\"\"\"\n", + " name = \"🧠 Smart Heuristic\"\n", + "\n", + " def select_action(self, obs: OpenSpielObservation) -> int:\n", + " # Parse OpenSpiel observation\n", + " # For Catch: info_state is a flattened 5x5 grid\n", + " # Ball position and paddle position encoded in the vector\n", + " info_state = obs.info_state\n", + "\n", + " # Find ball and paddle positions from info_state\n", + " # Catch uses a 5x5 grid, so 25 values\n", + " grid_size = 5\n", + "\n", + " # Find positions (ball = 1.0, paddle = 0.5 in the flattened grid)\n", + " ball_col = None\n", + " paddle_col = None\n", + "\n", + " for idx, val in enumerate(info_state):\n", + " if abs(val - 1.0) < 0.01: # Ball\n", + " ball_col = idx % grid_size\n", + " elif abs(val - 0.5) < 0.01: # Paddle\n", + " paddle_col = idx % grid_size\n", + "\n", + " if ball_col is not None and paddle_col is not None:\n", + " if paddle_col < ball_col:\n", + " return 2 # Move RIGHT\n", + " elif paddle_col > ball_col:\n", + " return 0 # Move LEFT\n", + "\n", + " return 1 # STAY (fallback)\n", + "\n", + "\n", + "class LearningPolicy:\n", + " \"\"\"Simulated RL: Epsilon-greedy exploration.\"\"\"\n", + " name = \"šŸ“ˆ Learning Agent\"\n", + "\n", + " def __init__(self):\n", + " self.steps = 0\n", + " self.smart_policy = SmartPolicy()\n", + "\n", + " def select_action(self, obs: OpenSpielObservation) -> int:\n", + " self.steps += 1\n", + "\n", + " # Decay exploration rate over time\n", + " epsilon = max(0.1, 1.0 - (self.steps / 100))\n", + "\n", + " if random.random() < epsilon:\n", + " # Explore: random action\n", + " return random.choice(obs.legal_actions)\n", + " else:\n", + " # Exploit: use smart strategy\n", + " return self.smart_policy.select_action(obs)\n", + "\n", + "\n", + "print(\"šŸ¤– \" + \"=\"*64 + \" šŸ¤–\")\n", + "print(\" āœ… 4 Policies Created (Adapted for OpenSpiel)!\")\n", + "print(\"šŸ¤– \" + \"=\"*64 + \" šŸ¤–\\n\")\n", + "\n", + "policies = [RandomPolicy(), AlwaysStayPolicy(), SmartPolicy(), LearningPolicy()]\n", + "for i, policy in enumerate(policies, 1):\n", + " print(f\" {i}. {policy.name}\")\n", + "\n", + "print(\"\\nšŸ’” These policies work with OpenSpielObservation!\")\n", + "print(\" • Read info_state (flattened grid)\")\n", + "print(\" • Use legal_actions\")\n", + "print(\" • Work with ANY OpenSpiel game that exposes these!\\n\")" + ] + }, + { + "cell_type": "markdown", + "id": "cell-23", + "metadata": {}, + "source": [ + "### Watch a Policy Play!" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "cell-24", + "metadata": {}, + "outputs": [], + "source": [ + "import time\n", + "\n", + "def run_episode(env, policy, visualize=True, delay=0.3):\n", + " \"\"\"Run one episode with a policy against OpenSpiel environment.\"\"\"\n", + "\n", + " # RESET\n", + " result = env.reset()\n", + " obs = result.observation\n", + "\n", + " if visualize:\n", + " print(f\"\\n{'='*60}\")\n", + " print(f\" šŸŽ® {policy.name}\")\n", + " print(f\" šŸŽ² Playing against OpenSpiel Catch\")\n", + " print('='*60 + '\\n')\n", + " time.sleep(delay)\n", + "\n", + " total_reward = 0\n", + " step = 0\n", + " action_names = [\"ā¬…ļø LEFT\", \"šŸ›‘ STAY\", \"āž”ļø RIGHT\"]\n", + "\n", + " # THE RL LOOP\n", + " while not obs.done:\n", + " # 1. Policy chooses action\n", + " action_id = policy.select_action(obs)\n", + "\n", + " # 2. Environment executes (via HTTP!)\n", + " action = OpenSpielAction(action_id=action_id, game_name=\"catch\")\n", + " result = env.step(action)\n", + " obs = result.observation\n", + "\n", + " # 3. Collect reward\n", + " if result.reward is not None:\n", + " total_reward += result.reward\n", + "\n", + " if visualize:\n", + " print(f\"šŸ“ Step {step + 1}: {action_names[action_id]} → Reward: {result.reward}\")\n", + " time.sleep(delay)\n", + "\n", + " step += 1\n", + "\n", + " if visualize:\n", + " result_text = \"šŸŽ‰ CAUGHT!\" if total_reward > 0 else \"😢 MISSED\"\n", + " print(f\"\\n{'='*60}\")\n", + " print(f\" {result_text} Total Reward: {total_reward}\")\n", + " print('='*60)\n", + "\n", + " return total_reward > 0\n", + "\n", + "\n", + "print(\"šŸ“ŗ \" + \"=\"*64 + \" šŸ“ŗ\")\n", + "print(\" Watch Smart Policy Play Against OpenSpiel!\")\n", + "print(\"šŸ“ŗ \" + \"=\"*64 + \" šŸ“ŗ\\n\")\n", + "\n", + "# Demo: Watch Smart Policy in action\n", + "policy = SmartPolicy()\n", + "run_episode(client, policy, visualize=True, delay=0.5)\n", + "\n", + "print(\"\\nšŸ’” You just watched REAL OpenSpiel Catch being played!\")\n", + "print(\" • Every action was an HTTP call\")\n", + "print(\" • Game logic runs in the server\")\n", + "print(\" • Client only sends actions and receives observations\\n\")" + ] + }, + { + "cell_type": "markdown", + "id": "cell-25", + "metadata": {}, + "source": [ + "---\n", + "\n", + "# Part 8: Policy Competition! šŸ†\n", + "\n", + "
\n", + "\n", + "Let's run **50 episodes** for each policy against **REAL OpenSpiel** and see who wins!\n", + "\n", + "This is production code - every action is an HTTP call to the OpenSpiel server!\n", + "\n", + "
" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "cell-26", + "metadata": {}, + "outputs": [], + "source": [ + "def evaluate_policies(env, num_episodes=50):\n", + " \"\"\"Compare all policies over many episodes using real OpenSpiel.\"\"\"\n", + " policies = [\n", + " RandomPolicy(),\n", + " AlwaysStayPolicy(),\n", + " SmartPolicy(),\n", + " LearningPolicy(),\n", + " ]\n", + "\n", + " print(\"\\nšŸ† \" + \"=\"*66 + \" šŸ†\")\n", + " print(f\" POLICY SHOWDOWN - {num_episodes} Episodes Each\")\n", + " print(f\" Playing against REAL OpenSpiel Catch!\")\n", + " print(\"šŸ† \" + \"=\"*66 + \" šŸ†\\n\")\n", + "\n", + " results = []\n", + " for policy in policies:\n", + " print(f\"⚔ Testing {policy.name}...\", end=\" \")\n", + " successes = sum(run_episode(env, policy, visualize=False)\n", + " for _ in range(num_episodes))\n", + " success_rate = (successes / num_episodes) * 100\n", + " results.append((policy.name, success_rate, successes))\n", + " print(f\"āœ“ Done!\")\n", + "\n", + " print(\"\\n\" + \"=\"*70)\n", + " print(\" šŸ“Š FINAL RESULTS\")\n", + " print(\"=\"*70 + \"\\n\")\n", + "\n", + " # Sort by success rate (descending)\n", + " results.sort(key=lambda x: x[1], reverse=True)\n", + "\n", + " # Award medals to top 3\n", + " medals = [\"šŸ„‡\", \"🄈\", \"šŸ„‰\", \" \"]\n", + "\n", + " for i, (name, rate, successes) in enumerate(results):\n", + " medal = medals[i]\n", + " bar = \"ā–ˆ\" * int(rate / 2)\n", + " print(f\"{medal} {name:25s} [{bar:<50}] {rate:5.1f}% ({successes}/{num_episodes})\")\n", + "\n", + " print(\"\\n\" + \"=\"*70)\n", + " print(\"\\n✨ Key Insights:\")\n", + " print(\" • Random (~20%): Baseline - pure luck šŸŽ²\")\n", + " print(\" • Always Stay (~20%): Bad strategy - stays center šŸ›‘\")\n", + " print(\" • Smart (100%): Optimal - perfect play! 🧠\")\n", + " print(\" • Learning (~85%): Improves over time šŸ“ˆ\")\n", + " print(\"\\nšŸŽ“ This is Reinforcement Learning + OpenEnv in action:\")\n", + " print(\" 1. We USED existing OpenSpiel environment (didn\\'t build it)\")\n", + " print(\" 2. Type-safe communication over HTTP\")\n", + " print(\" 3. Same code works for ANY OpenSpiel game\")\n", + " print(\" 4. Production-ready architecture\\n\")\n", + "\n", + "# Run the epic competition!\n", + "print(\"šŸŽ® Starting the showdown against REAL OpenSpiel...\\n\")\n", + "evaluate_policies(client, num_episodes=50)" + ] + }, + { + "cell_type": "markdown", + "id": "cell-27", + "metadata": {}, + "source": [ + "---\n", + "\n", + "\n", + "# Part 9: Switching to Other Games šŸŽ®\n", + "\n", + "
\n", + "\n", + "## What We Just Used: Real OpenSpiel! šŸŽ‰\n", + "\n", + "In Parts 6-8, we **USED** the existing OpenSpiel Catch environment:\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "
What We DidHow It Works
ImportedOpenSpielEnv client (pre-built)
StartedOpenSpiel server via uvicorn
ConnectedHTTP client to server
PlayedReal OpenSpiel Catch game
\n", + "\n", + "**šŸŽÆ This is production code!** Every action was an HTTP call to a real OpenSpiel environment.\n", + "\n", + "
\n", + "\n", + "## šŸŽ® 6 Games Available - Same Interface!\n", + "\n", + "The beauty of OpenEnv? **Same code, different games!**\n", + "\n", + "```python\n", + "# We just used Catch\n", + "env = OpenSpielEnv(base_url=\"http://localhost:8000\")\n", + "# game_name=\"catch\" was set via environment variable\n", + "\n", + "# Want Tic-Tac-Toe instead? Just change the game!\n", + "# Start server with: OPENSPIEL_GAME=tic_tac_toe uvicorn ...\n", + "# Same client code works!\n", + "```\n", + "\n", + "
\n", + "\n", + "**šŸŽ® All 6 Games:**\n", + "\n", + "1. āœ… **`catch`** - What we just used!\n", + "2. **`tic_tac_toe`** - Classic 3Ɨ3\n", + "3. **`kuhn_poker`** - Imperfect information poker\n", + "4. **`cliff_walking`** - Grid navigation\n", + "5. **`2048`** - Tile puzzle\n", + "6. **`blackjack`** - Card game\n", + "\n", + "**All use the exact same OpenSpielEnv client!**\n", + "\n", + "
\n", + "\n", + "### Try Another Game (Optional):\n", + "\n", + "```python\n", + "# Stop the current server (kill the server_process)\n", + "# Then start a new game:\n", + "\n", + "server_process = subprocess.Popen(\n", + " [sys.executable, \"-m\", \"uvicorn\",\n", + " \"envs.openspiel_env.server.app:app\",\n", + " \"--host\", \"0.0.0.0\",\n", + " \"--port\", \"8000\"],\n", + " env={**os.environ,\n", + " \"PYTHONPATH\": f\"{work_dir}/src\",\n", + " \"OPENSPIEL_GAME\": \"tic_tac_toe\", # Changed!\n", + " \"OPENSPIEL_AGENT_PLAYER\": \"0\",\n", + " \"OPENSPIEL_OPPONENT_POLICY\": \"random\"},\n", + " # ... rest of config\n", + ")\n", + "\n", + "# Same client works!\n", + "client = OpenSpielEnv(base_url=\"http://localhost:8000\")\n", + "result = client.reset() # Now playing Tic-Tac-Toe!\n", + "```\n", + "\n", + "**šŸ’” Key Insight**: You don't rebuild anything - you just USE different games with the same client!\n" + ] + }, + { + "cell_type": "markdown", + "id": "cell-28", + "metadata": {}, + "source": [ + "---\n", + "\n", + "\n", + "# Part 10: Create Your Own Integration šŸ› ļø\n", + "\n", + "
\n", + "\n", + "## The 5-Step Pattern\n", + "\n", + "Want to wrap your own environment in OpenEnv? Here's how:\n", + "\n", + "
\n", + "\n", + "### Step 1: Define Types (`models.py`)\n", + "\n", + "```python\n", + "from dataclasses import dataclass\n", + "from core.env_server import Action, Observation, State\n", + "\n", + "@dataclass\n", + "class YourAction(Action):\n", + " action_value: int\n", + " # Add your action fields\n", + "\n", + "@dataclass\n", + "class YourObservation(Observation):\n", + " state_data: List[float]\n", + " done: bool\n", + " reward: float\n", + " # Add your observation fields\n", + "\n", + "@dataclass\n", + "class YourState(State):\n", + " episode_id: str\n", + " step_count: int\n", + " # Add your state fields\n", + "```\n", + "\n", + "### Step 2: Implement Environment (`server/environment.py`)\n", + "\n", + "```python\n", + "from core.env_server import Environment\n", + "\n", + "class YourEnvironment(Environment):\n", + " def reset(self) -> Observation:\n", + " # Initialize your game/simulation\n", + " return YourObservation(...)\n", + " \n", + " def step(self, action: Action) -> Observation:\n", + " # Execute action, update state\n", + " return YourObservation(...)\n", + " \n", + " @property\n", + " def state(self) -> State:\n", + " return self._state\n", + "```\n", + "\n", + "### Step 3: Create Client (`client.py`)\n", + "\n", + "```python\n", + "from core.http_env_client import HTTPEnvClient\n", + "from core.types import StepResult\n", + "\n", + "class YourEnv(HTTPEnvClient[YourAction, YourObservation]):\n", + " def _step_payload(self, action: YourAction) -> dict:\n", + " \"\"\"Convert action to JSON\"\"\"\n", + " return {\"action_value\": action.action_value}\n", + " \n", + " def _parse_result(self, payload: dict) -> StepResult:\n", + " \"\"\"Parse JSON to observation\"\"\"\n", + " return StepResult(\n", + " observation=YourObservation(...),\n", + " reward=payload['reward'],\n", + " done=payload['done']\n", + " )\n", + " \n", + " def _parse_state(self, payload: dict) -> YourState:\n", + " return YourState(...)\n", + "```\n", + "\n", + "### Step 4: Create Server (`server/app.py`)\n", + "\n", + "```python\n", + "from core.env_server import create_fastapi_app\n", + "from .your_environment import YourEnvironment\n", + "\n", + "env = YourEnvironment()\n", + "app = create_fastapi_app(env)\n", + "\n", + "# That's it! OpenEnv creates all endpoints for you.\n", + "```\n", + "\n", + "### Step 5: Dockerize (`server/Dockerfile`)\n", + "\n", + "```dockerfile\n", + "FROM python:3.11-slim\n", + "\n", + "WORKDIR /app\n", + "COPY requirements.txt .\n", + "RUN pip install --no-cache-dir -r requirements.txt\n", + "\n", + "COPY . .\n", + "CMD [\"uvicorn\", \"app:app\", \"--host\", \"0.0.0.0\", \"--port\", \"8000\"]\n", + "```\n", + "\n", + "
\n", + "\n", + "### šŸŽ“ Examples to Study\n", + "\n", + "OpenEnv includes 3 complete examples:\n", + "\n", + "1. **`src/envs/echo_env/`**\n", + " - Simplest possible environment\n", + " - Great for testing and learning\n", + "\n", + "2. **`src/envs/openspiel_env/`**\n", + " - Wraps external library (OpenSpiel)\n", + " - Shows integration pattern\n", + " - 6 games in one integration\n", + "\n", + "3. **`src/envs/coding_env/`**\n", + " - Python code execution environment\n", + " - Shows complex use case\n", + " - Security considerations\n", + "\n", + "**šŸ’” Study these to understand the patterns!**\n", + "\n", + "
" + ] + }, + { + "cell_type": "markdown", + "id": "cell-29", + "metadata": {}, + "source": [ + "---\n", + "\n", + "
\n", + "\n", + "# šŸŽ“ Summary: Your Journey\n", + "\n", + "
" + ] + }, + { + "cell_type": "markdown", + "id": "cell-30", + "metadata": {}, + "source": [ + "## What You Learned\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "
\n", + "\n", + "### šŸ“š Concepts\n", + "\n", + "āœ… **RL Fundamentals**\n", + "- The observe-act-reward loop\n", + "- What makes good policies\n", + "- Exploration vs exploitation\n", + "\n", + "āœ… **OpenEnv Architecture**\n", + "- Client-server separation\n", + "- Type-safe contracts\n", + "- HTTP communication layer\n", + "\n", + "āœ… **Production Patterns**\n", + "- Docker isolation\n", + "- API design\n", + "- Reproducible deployments\n", + "\n", + "\n", + "\n", + "### šŸ› ļø Skills\n", + "\n", + "āœ… **Using Environments**\n", + "- Import OpenEnv clients\n", + "- Call reset/step/state\n", + "- Work with typed observations\n", + "\n", + "āœ… **Building Environments**\n", + "- Define type-safe models\n", + "- Implement Environment class\n", + "- Create HTTPEnvClient\n", + "\n", + "āœ… **Testing & Debugging**\n", + "- Compare policies\n", + "- Visualize episodes\n", + "- Measure performance\n", + "\n", + "
" + ] + }, + { + "cell_type": "markdown", + "id": "cell-31", + "metadata": {}, + "source": [ + "## OpenEnv vs Traditional RL\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "
FeatureTraditional (Gym)OpenEnvWinner
Type SafetyāŒ Arrays, dictsāœ… DataclassesšŸ† OpenEnv
IsolationāŒ Same processāœ… DockeršŸ† OpenEnv
DeploymentāŒ Manual setupāœ… K8s-readyšŸ† OpenEnv
LanguageāŒ Python onlyāœ… Any (HTTP)šŸ† OpenEnv
ReproducibilityāŒ \"Works on my machine\"āœ… Same everywherešŸ† OpenEnv
Communityāœ… Large ecosystem🟔 GrowingšŸ¤ Both!
\n", + "\n", + "
\n", + "\n", + "**šŸŽÆ The Bottom Line**\n", + "\n", + "OpenEnv brings **production engineering** to RL:\n", + "- Same environments work locally and in production\n", + "- Type safety catches bugs early\n", + "- Docker isolation prevents conflicts\n", + "- HTTP API works with any language\n", + "\n", + "**It's RL for 2024 and beyond.**\n", + "\n", + "
" + ] + }, + { + "cell_type": "markdown", + "id": "cell-32", + "metadata": {}, + "source": [ + "\n", + "## šŸ“š Resources\n", + "\n", + "
\n", + "\n", + "### šŸ”— Essential Links\n", + "\n", + "- **šŸ  OpenEnv GitHub**: https://github.com/meta-pytorch/OpenEnv\n", + "- **šŸŽ® OpenSpiel**: https://github.com/google-deepmind/open_spiel\n", + "- **⚔ FastAPI Docs**: https://fastapi.tiangolo.com/\n", + "- **🐳 Docker Guide**: https://docs.docker.com/get-started/\n", + "- **šŸ”„ PyTorch**: https://pytorch.org/\n", + "\n", + "### šŸ“– Documentation Deep Dives\n", + "\n", + "- **Environment Creation Guide**: `src/envs/README.md`\n", + "- **OpenSpiel Integration**: `src/envs/openspiel_env/README.md`\n", + "- **Example Scripts**: `examples/`\n", + "- **RFC 001**: [Baseline API Specs](https://github.com/meta-pytorch/OpenEnv/pull/26)\n", + "\n", + "### šŸŽ“ Community & Support\n", + "\n", + "**Supported by amazing organizations:**\n", + "- šŸ”„ Meta PyTorch\n", + "- šŸ¤— Hugging Face\n", + "- ⚔ Unsloth AI\n", + "- 🌟 Reflection AI\n", + "- šŸš€ And many more!\n", + "\n", + "**License**: BSD 3-Clause (very permissive!)\n", + "\n", + "**Contributions**: Always welcome! Check out the issues tab.\n", + "\n", + "
\n", + "\n", + "---\n", + "\n", + "### 🌈 What's Next?\n", + "\n", + "1. ⭐ **Star the repo** to show support and stay updated\n", + "2. šŸ”„ **Try modifying** the Catch game (make it harder? bigger grid?)\n", + "3. šŸŽ® **Explore** other OpenSpiel games\n", + "4. šŸ› ļø **Build** your own environment integration\n", + "5. šŸ’¬ **Share** what you build with the community!" + ] + } + ], + "metadata": { + "kernelspec": { + "display_name": "Python 3", + "language": "python", + "name": "python3" + }, + "language_info": { + "codemirror_mode": { + "name": "ipython", + "version": 3 + }, + "file_extension": ".py", + "mimetype": "text/x-python", + "name": "python", + "nbconvert_exporter": "python", + "pygments_lexer": "ipython3", + "version": "3.11.0" + } + }, + "nbformat": 4, + "nbformat_minor": 4 +}