BrowserGym Documentation

repository·main·Indexed 23 days ago

https://github.com/servicenow/browsergym

An extensible framework for web agent research that provides standardized environments and benchmarks compatible with the Gymnasium API for web task automation in the Chromium browser. It supports multiple benchmark integrations including MiniWoB, WebArena, VisualWebArena, WebArena Verified, WorkArena, AssistantBench, WebLINX, and TimeWarp. The framework is distributed via several packages, including browsergym-core and browsergym-experiments.

Tokens
25K
Snippets
40
Records
145
Agent score
78%

What's inside BrowserGym

  1. Overview of BrowserGym

    main

    BrowserGym is a Python library designed for web task automation in the Chromium browser. It provides a gymnasium environment interface, allowing developers to use standard reinforcement learning and agent training workflows for web-based tasks.

    By default, BrowserGym includes support for several benchmarks:

    • MiniWob++: A benchmark for basic web interactions.
    • WebArena: A benchmark for complex web navigation and task completion.
    • WorkArena: A benchmark for professional/work-oriented web tasks.
  2. How to create a custom task by inheriting from AbstractBrowserTask

    main

    To create a custom task in BrowserGym, inherit from the AbstractBrowserTask class. You must implement several key methods to define the task's lifecycle:

    1. get_task_id() (classmethod): Returns a unique string identifier for the task.
    2. setup(page): Initializes the environment (e.g., navigating to a URL) and returns a goal string and an info dictionary.
    3. validate(page, chat_messages): Determines if the task was successful. Returns a tuple containing reward (float), success (bool), a message (str), and info (dict).
    4. cheat(page, chat_messages) (optional): Provides an 'oracle' or hard-coded solution to solve the task in a single step.
    5. 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
  3. Install WorkArena and dependencies

    main

    Install the WorkArena environment for BrowserGym, upload the necessary benchmark data to your ServiceNow instance, and install the required Playwright browser binaries using these commands:

    # Install the WorkArena package
    pip install browsergym-workarena
    
    # Upload benchmark data to your ServiceNow instance
    workarena-install
    
    # Install Playwright Chromium
    playwright install chromium
    pip install browsergym-workarena
    workarena-install
    playwright install chromium
  4. Automated setup for Miniwob benchmark

    main

    If you are working from the BrowserGym root directory, you can use the provided Makefile to automate the installation of the Miniwob benchmark. This process clones the MiniWoB++ repository, resets it to a specific commit for reproducibility, and configures the MINIWOB_URL in your .env file.

    After running the setup, ensure you load the environment variables into your shell session.

    make setup-miniwob
    source .env
  5. Configure WebArena environment variables

    main

    To use WebArena with BrowserGym, you must set several environment variables. These variables use the WA_ prefix and must point to the specific URLs/ports used in your WebArena instance.

    Important:

    • Ensure the ports match your specific WebArena setup.
    • If your instance supports the FULL_RESET feature, set WA_FULL_RESET to the appropriate endpoint.
    • If it does not support it, either do not set WA_FULL_RESET or set it to an empty string ("").
    BASE_URL=<YOUR_SERVER_URL_HERE>  # example: "http://myazuremachine.eastus.cloudapp.azure.com"
    
    # webarena environment variables (change ports as needed)
    export WA_SHOPPING="$BASE_URL:8082/"
    export WA_SHOPPING_ADMIN="$BASE_URL:8083/admin"
    export WA_REDDIT="$BASE_URL:8080"
    export WA_GITLAB="$BASE_URL:9001"
    export WA_WIKIPEDIA="$BASE_URL:8081/wikipedia_en_all_maxi_2022-05/A/User:The_other_Kiwix_guy/Landing"
    export WA_MAP="$BASE_URL:443"
    export WA_HOMEPAGE="$BASE_URL:80"
    
    # if your webarena instance offers the FULL_RESET feature (optional)
    export WA_FULL_RESET="$BASE_URL:7565"
    
    # otherwise, be sure to NOT set WA_FULL_RESET, or set it to an empty string
    export WA_FULL_RESET=""
  6. Configure VisualWebArena environment variables

    main

    VisualWebArena requires several environment variables to map the various web domains to your running server instance. All variables must use the VWA_ prefix.

    Ensure the ports used in the environment variables match the ports configured in your WebArena instance.

    Note on VWA_FULL_RESET:

    • If your WebArena instance supports the FULL_RESET feature, set VWA_FULL_RESET to the appropriate URL (e.g., http://<BASE_URL>:7565).
    • If it does not support this feature, do not set the variable, or set it to an empty string (export VWA_FULL_RESET="").
    BASE_URL=<YOUR_SERVER_URL_HERE>  # example: "http://myazuremachine.eastus.cloudapp.azure.com"
    
    # visualwebarena environment variables (change ports as needed)
    export VWA_CLASSIFIEDS="$BASE_URL:8083"
    export VWA_CLASSIFIEDS_RESET_TOKEN="4b61655535e7ed388f0d40a93600254c"
    export VWA_SHOPPING="$BASE_URL:8082"
    export VWA_REDDIT="$BASE_URL:8080"
    export VWA_WIKIPEDIA="$BASE_URL:8081"
    export VWA_HOMEPAGE="$BASE_URL:80"
    
    # if your webarena instance offers the FULL_RESET feature (optional)
    export VWA_FULL_RESET="$BASE_URL:7565"
    
    # otherwise, be sure to NOT set VWA_FULL_RESET, or set it to an empty string
    export VWA_FULL_RESET=""
  7. Set up a ServiceNow Developer Instance for WorkArena

    main

    To use the WorkArena environment, you must first obtain a ServiceNow Developer Instance:

    1. Create an account at https://developer.servicenow.com/.
    2. Request an instance and select the Washington release.
    3. Once initialized, retrieve your instance URL and credentials.
    4. Configure the following environment variables in your shell:
      • SNOW_INSTANCE_URL: The URL of your ServiceNow developer instance.
      • SNOW_INSTANCE_UNAME: Set this to admin.
      • SNOW_INSTANCE_PWD: Your instance password. Note: Wrap the value in quotes and ensure special shell characters are escaped. Verify by running echo $SNOW_INSTANCE_PWD.
    5. Log into the instance via a browser using the admin credentials and close any initial popups (like analytics agreements).
  8. Install demo-agent

    main

    To set up the demo-agent environment, use Conda to create the environment from the provided configuration file, activate it, and then install the required Chromium browser via Playwright.

    conda env create -f environment.yml
    conda activate demo-agent
    playwright install chromium
  9. Install BrowserGym

    main

    You can install BrowserGym using pip. Depending on your needs, you can install the full package or specific subsets:

    • pip install browsergym: Recommended. Installs everything.
    • pip install browsergym-experiments: Installs experiment utilities (agent, loop, benchmarks) and everything else.
    • pip install browsergym-core: Installs only core functionalities (no benchmarks, just open-ended tasks).
    • pip install browsergym-miniwob: Core + MiniWoB.
    • pip install browsergym-webarena: Core + WebArena.
    • pip install browsergym-webarena-verified: Core + WebArenaVerified.
    • pip install browsergym-visualwebarena: Core + VisualWebArena.
    • pip install browsergym-workarena: Core + WorkArena.
    • pip install browsergym-assistantbench: Core + AssistantBench.
    • pip install weblinx-browsergym: Core + WebLINX.
    • pip install browsergym-timewarp: Core + TimeWarp.

    After installing, you must install the Playwright Chromium browser:

    playwright install chromium
    pip install browsergym