|
| 1 | +# Vendored from https://github.com/mll-lab-nu/VAGEN |
| 2 | +# These are pure abstract base classes with no heavy dependencies. |
| 3 | +# Vendored to avoid requiring the full VAGEN installation. |
| 4 | +# Last synced: 2026-03-02 |
| 5 | + |
| 6 | +from __future__ import annotations |
| 7 | + |
| 8 | +from abc import abstractmethod |
| 9 | +from typing import Any, Dict, Tuple |
| 10 | + |
| 11 | +from .gym_base_env import GymBaseEnv |
| 12 | + |
| 13 | + |
| 14 | +class GymImageEnv(GymBaseEnv): |
| 15 | + """ |
| 16 | + GymImageEnv is a base environment class that supports optional |
| 17 | + **image-based multi-modal observations**, while keeping the same API |
| 18 | + as GymBaseEnv. |
| 19 | +
|
| 20 | + -------------------------------------------------------------------- |
| 21 | + Observation Protocol |
| 22 | + -------------------------------------------------------------------- |
| 23 | +
|
| 24 | + WITH images |
| 25 | + ------------------------------- |
| 26 | + If the environment returns images, the observation should follow: |
| 27 | +
|
| 28 | + obs = { |
| 29 | + "obs_str": "... <image> ...", |
| 30 | + "multi_modal_input": { |
| 31 | + "<image>": [PIL.Image.Image, ...] |
| 32 | + } |
| 33 | + } |
| 34 | +
|
| 35 | + - Images are stored under obs["multi_modal_input"]["<image>"]. |
| 36 | + - "<image>" in obs_str is a placeholder indicating where each image |
| 37 | + should appear in the prompt. |
| 38 | + - The number of "<image>" in obs_str should match the number of |
| 39 | + images in the list. |
| 40 | +
|
| 41 | + WITHOUT images: |
| 42 | + ---------------------------------- |
| 43 | + Can simply use: |
| 44 | +
|
| 45 | + obs = { |
| 46 | + "obs_str": "..." |
| 47 | + } |
| 48 | +
|
| 49 | + - "multi_modal_input" is optional and may be omitted. |
| 50 | + - obs_str should NOT contain "<image>" placeholders. |
| 51 | +
|
| 52 | +
|
| 53 | + -------------------------------------------------------------------- |
| 54 | + Agent-Loop Rollout |
| 55 | + -------------------------------------------------------------------- |
| 56 | + - sys : system prompt (from system_prompt()). |
| 57 | + - init_obs : observation from reset(). |
| 58 | + - step_obs : observation from step(). |
| 59 | + - res_i : agent response at step i. |
| 60 | +
|
| 61 | + Concat mode (single growing context): |
| 62 | + sys + init_obs + res_0 + step_obs_1 + res_1 + ... |
| 63 | +
|
| 64 | + Non-concat mode (step-wise independent contexts): |
| 65 | + Step 0: sys + init_obs + res_0 |
| 66 | + Step 1: sys + step_obs_1 + res_1 |
| 67 | + Step 2: sys + step_obs_2 + res_2 |
| 68 | +
|
| 69 | + -------------------------------------------------------------------- |
| 70 | + Info |
| 71 | + -------------------------------------------------------------------- |
| 72 | + The `info` dict returned by reset() and step() may include: |
| 73 | + - success (bool): whether the task/episode is considered |
| 74 | + successful, this will be used for wandb logging. |
| 75 | + """ |
| 76 | + |
| 77 | + def __init__(self, env_config: Dict[str, Any]): |
| 78 | + """ |
| 79 | + Initialize the environment. |
| 80 | +
|
| 81 | + Args: |
| 82 | + env_config (Dict[str, Any]): |
| 83 | + Environment configuration. The exact schema is defined by |
| 84 | + the concrete environment implementation and/or GymBaseEnv. |
| 85 | +
|
| 86 | + Side effects: |
| 87 | + - Calls GymBaseEnv.__init__(env_config). |
| 88 | + """ |
| 89 | + super().__init__(env_config) |
| 90 | + |
| 91 | + @abstractmethod |
| 92 | + async def close(self) -> None: |
| 93 | + """ |
| 94 | + Close the environment and release all resources. |
| 95 | +
|
| 96 | + This should clean up anything created by the environment, e.g.: |
| 97 | + - windows / renderers |
| 98 | + - subprocesses |
| 99 | + - file handles |
| 100 | + - GPU memory / models |
| 101 | +
|
| 102 | + Returns: |
| 103 | + None |
| 104 | + """ |
| 105 | + raise NotImplementedError |
| 106 | + |
| 107 | + @abstractmethod |
| 108 | + async def system_prompt(self) -> Dict[str, Any]: |
| 109 | + """ |
| 110 | + Return the system-level prompt/observation for the environment. |
| 111 | +
|
| 112 | + Returns: |
| 113 | + obs (Dict[str, Any]): |
| 114 | + A dict representing the system prompt observation. |
| 115 | +
|
| 116 | + If returning images, it must follow: |
| 117 | +
|
| 118 | + obs = { |
| 119 | + "obs_str": "... <image> ...", |
| 120 | + "multi_modal_input": { |
| 121 | + "<image>": [PIL.Image.Image, ...] |
| 122 | + } |
| 123 | + } |
| 124 | +
|
| 125 | + If returning no images, it must follow: |
| 126 | +
|
| 127 | + obs = { |
| 128 | + "obs_str": "..." |
| 129 | + } |
| 130 | + """ |
| 131 | + raise NotImplementedError |
| 132 | + |
| 133 | + @abstractmethod |
| 134 | + async def reset(self, seed: int) -> Tuple[Dict[str, Any], Dict[str, Any]]: |
| 135 | + """ |
| 136 | + Reset the environment to the initial state. |
| 137 | +
|
| 138 | + Args: |
| 139 | + seed (int): |
| 140 | + Random seed used to initialize the environment |
| 141 | +
|
| 142 | + Returns: |
| 143 | + obs (Dict[str, Any]): |
| 144 | + The initial observation after reset. |
| 145 | +
|
| 146 | + If returning images, it must follow: |
| 147 | +
|
| 148 | + obs = { |
| 149 | + "obs_str": "... <image> ...", |
| 150 | + "multi_modal_input": { |
| 151 | + "<image>": [PIL.Image.Image, ...] |
| 152 | + } |
| 153 | + } |
| 154 | +
|
| 155 | + If returning no images, it must follow: |
| 156 | +
|
| 157 | + obs = { |
| 158 | + "obs_str": "..." |
| 159 | + } |
| 160 | +
|
| 161 | + info (Dict[str, Any]): |
| 162 | + A dict containing any additional metadata about the reset, |
| 163 | + e.g. debug information, episode identifiers, etc. |
| 164 | + """ |
| 165 | + raise NotImplementedError |
| 166 | + |
| 167 | + @abstractmethod |
| 168 | + async def step( |
| 169 | + self, action_str: str |
| 170 | + ) -> Tuple[Dict[str, Any], float, bool, Dict[str, Any]]: |
| 171 | + """ |
| 172 | + Execute one environment step using an agent-provided action. |
| 173 | +
|
| 174 | + Args: |
| 175 | + action_str (str): |
| 176 | + The action produced by the agent, in text form. |
| 177 | +
|
| 178 | + Returns: |
| 179 | + obs (Dict[str, Any]): |
| 180 | + The next observation after applying the action. |
| 181 | +
|
| 182 | + If returning images, it must follow: |
| 183 | +
|
| 184 | + obs = { |
| 185 | + "obs_str": "... <image> ...", |
| 186 | + "multi_modal_input": { |
| 187 | + "<image>": [PIL.Image.Image, ...] |
| 188 | + } |
| 189 | + } |
| 190 | +
|
| 191 | + If returning no images, it must follow: |
| 192 | +
|
| 193 | + obs = { |
| 194 | + "obs_str": "..." |
| 195 | + } |
| 196 | +
|
| 197 | + reward (float): |
| 198 | + Scalar reward for the current step. |
| 199 | +
|
| 200 | + done (bool): |
| 201 | + Whether the current episode has terminated after this |
| 202 | + step. |
| 203 | +
|
| 204 | + info (Dict[str, Any]): |
| 205 | + Additional step-level metadata. |
| 206 | +
|
| 207 | + Common optional keys: |
| 208 | + - success (bool): whether the task/episode is |
| 209 | + considered successful, typically used for logging |
| 210 | + (e.g. wandb). |
| 211 | + """ |
| 212 | + raise NotImplementedError |
0 commit comments