|
| 1 | +#!/usr/bin/env python3 |
| 2 | +"""Import and use a tiny ORS/OpenReward-style environment.""" |
| 3 | + |
| 4 | +from __future__ import annotations |
| 5 | + |
| 6 | +import argparse |
| 7 | +import shutil |
| 8 | +import subprocess |
| 9 | +import sys |
| 10 | +from pathlib import Path |
| 11 | +from tempfile import TemporaryDirectory |
| 12 | +from typing import Any |
| 13 | + |
| 14 | +from openenv.core.env_server.mcp_types import CallToolAction, ListToolsAction |
| 15 | + |
| 16 | + |
| 17 | +ENV_NAME = "imported_ors_demo" |
| 18 | +EXAMPLE_DIR = Path(__file__).resolve().parent |
| 19 | + |
| 20 | + |
| 21 | +def import_environment(source_dir: Path, output_dir: Path) -> Path: |
| 22 | + """Run the OpenEnv importer and return the generated package directory.""" |
| 23 | + output_dir.mkdir(parents=True, exist_ok=True) |
| 24 | + generated_dir = output_dir / ENV_NAME |
| 25 | + if generated_dir.exists(): |
| 26 | + shutil.rmtree(generated_dir) |
| 27 | + |
| 28 | + subprocess.run( |
| 29 | + [ |
| 30 | + sys.executable, |
| 31 | + "-m", |
| 32 | + "openenv.cli", |
| 33 | + "import", |
| 34 | + str(source_dir), |
| 35 | + "--name", |
| 36 | + ENV_NAME, |
| 37 | + "--output-dir", |
| 38 | + str(output_dir), |
| 39 | + ], |
| 40 | + check=True, |
| 41 | + ) |
| 42 | + return generated_dir |
| 43 | + |
| 44 | + |
| 45 | +def _clear_demo_modules() -> None: |
| 46 | + for module_name in list(sys.modules): |
| 47 | + if module_name == ENV_NAME or module_name.startswith(f"{ENV_NAME}."): |
| 48 | + sys.modules.pop(module_name, None) |
| 49 | + if module_name == "ors" or module_name.startswith("ors."): |
| 50 | + sys.modules.pop(module_name, None) |
| 51 | + |
| 52 | + |
| 53 | +def run_imported_environment(output_dir: Path) -> dict[str, Any]: |
| 54 | + """Use the generated OpenEnv wrapper directly.""" |
| 55 | + _clear_demo_modules() |
| 56 | + sys.path.insert(0, str(output_dir)) |
| 57 | + try: |
| 58 | + from imported_ors_demo.server.imported_ors_demo_environment import ( # type: ignore |
| 59 | + ImportedOrsDemoEnvironment, |
| 60 | + ) |
| 61 | + |
| 62 | + env = ImportedOrsDemoEnvironment() |
| 63 | + reset_observation = env.reset(split="train", index=0) |
| 64 | + tools_observation = env.step(ListToolsAction()) |
| 65 | + answer_observation = env.step( |
| 66 | + CallToolAction(tool_name="answer", arguments={"value": "4"}) |
| 67 | + ) |
| 68 | + |
| 69 | + return { |
| 70 | + "prompt": reset_observation.metadata["prompt"][0]["text"], |
| 71 | + "tools": [tool.name for tool in tools_observation.tools], |
| 72 | + "result": answer_observation.result["blocks"][0]["text"], |
| 73 | + "reward": answer_observation.reward, |
| 74 | + "done": answer_observation.done, |
| 75 | + } |
| 76 | + finally: |
| 77 | + try: |
| 78 | + sys.path.remove(str(output_dir)) |
| 79 | + except ValueError: |
| 80 | + pass |
| 81 | + _clear_demo_modules() |
| 82 | + |
| 83 | + |
| 84 | +def _print_summary(generated_dir: Path, summary: dict[str, Any]) -> None: |
| 85 | + print(f"Generated environment: {generated_dir}") |
| 86 | + print(f"Prompt: {summary['prompt']}") |
| 87 | + print(f"Tools: {', '.join(summary['tools'])}") |
| 88 | + print(f"Tool result: {summary['result']}") |
| 89 | + print(f"Reward: {summary['reward']}") |
| 90 | + print(f"Done: {summary['done']}") |
| 91 | + |
| 92 | + |
| 93 | +def main(argv: list[str] | None = None) -> None: |
| 94 | + parser = argparse.ArgumentParser() |
| 95 | + parser.add_argument( |
| 96 | + "--output-dir", |
| 97 | + type=Path, |
| 98 | + help="Directory for the generated OpenEnv package. Defaults to a temp dir.", |
| 99 | + ) |
| 100 | + args = parser.parse_args(argv) |
| 101 | + |
| 102 | + source_dir = EXAMPLE_DIR / "source" |
| 103 | + if args.output_dir is not None: |
| 104 | + output_dir = args.output_dir.resolve() |
| 105 | + generated_dir = import_environment(source_dir, output_dir) |
| 106 | + summary = run_imported_environment(output_dir) |
| 107 | + _print_summary(generated_dir, summary) |
| 108 | + return |
| 109 | + |
| 110 | + with TemporaryDirectory() as tmp: |
| 111 | + output_dir = Path(tmp) |
| 112 | + generated_dir = import_environment(source_dir, output_dir) |
| 113 | + summary = run_imported_environment(output_dir) |
| 114 | + _print_summary(generated_dir, summary) |
| 115 | + |
| 116 | + |
| 117 | +if __name__ == "__main__": |
| 118 | + main() |
0 commit comments