Computer Use Preview

repository·main·Indexed 25 days ago

https://github.com/google-gemini/computer-use-preview

A tool that enables Gemini models to interact with a web browser via natural language queries to perform tasks such as navigating sites and typing into search bars. It supports multiple Gemini models, including gemini-3.6-flash, and provides execution environments via Playwright or Browserbase.

Tokens
1.6K
Snippets
6
Records
9
Agent score
35%

What's inside Computer Use Preview

  1. Run the Agent CLI via main.py

    main

    The main.py script is the primary interface for executing browser tasks. Use the --query flag to provide natural language instructions.

    Basic usage:

    python main.py --query "Go to Google and type 'Hello World' into the search bar"

    Available Environments (--env):

    • playwright: Runs the browser locally using Playwright (default behavior).
    • browserbase: Connects to a Browserbase instance (requires BROWSERBASE_API_KEY and BROWSERBASE_PROJECT_ID).

    Example with Playwright and initial URL:

    python main.py --query="Go to Google and type 'Hello World' into the search bar" --env="playwright" --initial_url="https://www.google.com/search?q=latest+AI+news"
    python main.py --query="Go to Google and type 'Hello World' into the search bar" --env="playwright" --initial_url="https://www.google.com/search?q=latest+AI+news"
  2. Install Computer Use Preview

    main

    Follow these steps to set up the repository and its dependencies:

    1. Clone the repository:

      git clone https://github.com/google-gemini/computer-use-preview.git
      cd computer-use-preview
    2. Set up a Python virtual environment and install requirements:

      python3 -m venv .venv
      source .venv/bin/activate
      pip install -r requirements.txt
    3. Install Playwright and Chrome dependencies:

      playwright install-deps chrome
      playwright install chrome
    git clone https://github.com/google-gemini/computer-use-preview.git
    cd computer-use-preview
    python3 -m venv .venv
    source .venv/bin/activate
    pip install -r requirements.txt
    playwright install-deps chrome
    playwright install chrome
  3. Configure Vertex AI Client

    main

    To use Vertex AI, you must enable it via environment variables and provide your project details:

    Current session:

    export USE_VERTEXAI=true
    export VERTEXAI_PROJECT="YOUR_PROJECT_ID"
    export VERTEXAI_LOCATION="YOUR_LOCATION"

    Persist in virtual environment:

    echo 'export USE_VERTEXAI=true' >> .venv/bin/activate
    echo 'export VERTEXAI_PROJECT="your-project-id"' >> .venv/bin/activate
    echo 'export VERTEXAI_LOCATION="your-location"' >> .venv/bin/activate
    deactivate
    source .venv/bin/activate
    export USE_VERTEXAI=true
    export VERTEXAI_PROJECT="YOUR_PROJECT_ID"
    export VERTEXAI_LOCATION="YOUR_LOCATION"
  4. Configure Gemini Developer API

    main

    To use the Gemini Developer API, set the GEMINI_API_KEY environment variable. You can do this in your current session or persist it in your virtual environment.

    Current session:

    export GEMINI_API_KEY="YOUR_GEMINI_API_KEY"

    Persist in virtual environment:

    echo 'export GEMINI_API_KEY="YOUR_GEMINI_API_KEY"' >> .venv/bin/activate
    deactivate
    source .venv/bin/activate
    export GEMINI_API_KEY="YOUR_GEMINI_API_KEY"
  5. Troubleshoot Playwright Dropdown Menus

    main

    On some operating systems, Playwright cannot capture <select> elements because they are rendered by the OS, preventing the agent from seeing them in screenshots.

    Mitigation Strategies:

    1. Use Browserbase: This is the recommended solution as it avoids local OS rendering issues.
    2. Inject a script: Use a library like proxy-select to render custom <select> elements. You must inject proxy-select.css and proxy-select.js into the page.

    Example injection logic for Playwright.__enter__:

    self._page.add_init_script(PROXY_SELECT_JS)
    def inject_style(page):
        try:
            page.add_style_tag(content=PROXY_SELECT_CSS)
        except Exception as e:
            print(f"Error injecting style: {e}")
    
    self._page.on('domcontentloaded', inject_style)
    self._page.add_init_script(PROXY_SELECT_JS)
    def inject_style(page):
        try:
            page.add_style_tag(content=PROXY_SELECT_CSS)
        except Exception as e:
            print(f"Error injecting style: {e}")
    
    self._page.on('domcontentloaded', inject_style)
  6. Agent CLI Reference

    main

    The main.py script accepts the following command-line arguments:

    ArgumentDescriptionRequiredDefaultSupported Environment(s)
    --queryThe natural language query for the browser agent to execute.YesN/AAll
    --envThe computer use environment to use. Must be one of: playwright, browserbaseNoN/AAll
    --initial_urlThe initial URL to load when the browser starts.Nohttps://www.google.comAll
    --highlight_mouseIf specified, the agent will attempt to highlight the mouse cursor's position in screenshots. Useful for visual debugging.NoFalseplaywright
    --modelThe model to use.Nogemini-3.6-flashAll

    Required Environment Variables:

    • GEMINI_API_KEY: Your API key for the Gemini model.
    • BROWSERBASE_API_KEY: Required when using --env browserbase.
    • BROWSERBASE_PROJECT_ID: Required when using --env browserbase.
  7. Select a Gemini Model

    main

    Specify the model using the --model <model name> flag.

    Available Models:

    • gemini-3.6-flash (Default)
    • gemini-3.5-flash-lite (Low-latency, cost-efficient)
    • gemini-3.5-flash (Earlier Flash model)
    • gemini-2.5-computer-use-preview-10-2025 (Earlier preview model)
    • gemini-3-flash-preview (Preview version of Gemini 3 Flash)
  8. Run the browser agent via CLI

    main

    The main.py script provides a command-line interface to execute a browser agent that performs tasks based on a natural language query. You can choose between different computer environments (playwright or browserbase), specify the target model, and set an initial URL.

    CLI Arguments

    ArgumentTypeDefaultDescription
    --querystrRequiredThe natural language query for the browser agent to execute.
    --envstrplaywrightThe computer use environment to use. Options: playwright, browserbase.
    --initial_urlstrhttps://www.google.comThe initial URL loaded for the computer.
    --highlight_mouseflagFalseIf present, attempts to highlight the location of the mouse.
    --modelstrgemini-3.6-flashThe Gemini model name to use for the agent.