To create a custom task in BrowserGym, inherit from the AbstractBrowserTask class. You must implement several key methods to define the task's lifecycle:
get_task_id() (classmethod): Returns a unique string identifier for the task.setup(page): Initializes the environment (e.g., navigating to a URL) and returns a goal string and an info dictionary.validate(page, chat_messages): Determines if the task was successful. Returns a tuple containing reward (float), success (bool), a message (str), and info (dict).cheat(page, chat_messages) (optional): Provides an 'oracle' or hard-coded solution to solve the task in a single step.teardown(): Cleans up resources before the environment closes.
from typing import Tuple
import playwright.sync_api
from browsergym.core.task import AbstractBrowserTask
class SampleTask(AbstractBrowserTask):
def __init__(self, seed: int) -> None:
super().__init__(seed)
@classmethod
def get_task_id(cls):
return "sample_task"
def setup(self, page: playwright.sync_api.Page) -> Tuple[str, dict]:
page.goto("https://www.google.com", timeout=10000)
goal = "Search for 'Eiffel Tower' Wikipedia page."
info = {}
return goal, info
def validate(
self, page: playwright.sync_api.Page, chat_messages: list[str]
) -> Tuple[float, bool, str, dict]:
if page.url == "https://en.wikipedia.org/wiki/Eiffel_Tower":
return 1.0, True, "Task completed", {}
else:
return 0.0, False, "", {}
def cheat(self, page: playwright.sync_api.Page, chat_messages: list[str]) -> None:
page.get_by_text("Search").fill("Eiffel Tower")
page.get_by_text("Google Search").click()
page.get_by_text("Eiffel Tower - Wikipedia").click()
def teardown(self) -> None:
pass