google-gemini/computer-use-preview
Computer Use Preview
Quick Start
This section will guide you through setting up and running the Computer Use Preview model, either the Gemini Developer API or Vertex AI. Follow these steps to get started.
1. Installation
Clone the Repository
|
|
Set up Python Virtual Environment and Install Dependencies
|
|
Install Playwright and Browser Dependencies
|
|
2. Configuration
You can get started using either the Gemini Developer API or Vertex AI.
A. If using the Gemini Developer API:
You need a Gemini API key to use the agent:
|
|
Or to add this to your virtual environment:
|
|
Replace YOUR_GEMINI_API_KEY with your actual key.
B. If using the Vertex AI Client:
You need to explicitly use Vertex AI, then provide project and location to use the agent:
|
|
Or to add this to your virtual environment:
|
|
Replace YOUR_PROJECT_ID and YOUR_LOCATION with your actual project and location.
3. Running the Tool
The primary way to use the tool is via the main.py script.
General Command Structure:
|
|
Available Environments:
You can specify a particular environment with the --env <environment> flag. Available options:
playwright: Runs the browser locally using Playwright.browserbase: Connects to a Browserbase instance.
Local Playwright
Runs the agent using a Chrome browser instance controlled locally by Playwright.
|
|
You can also specify an initial URL for the Playwright environment:
|
|
Browserbase
Runs the agent using Browserbase as the browser backend. Ensure the proper Browserbase environment variables are set:BROWSERBASE_API_KEY and BROWSERBASE_PROJECT_ID.
|
|
Agent CLI
The main.py script is the command-line interface (CLI) for running the browser agent.
Command-Line Arguments
| Argument | Description | Required | Default | Supported Environment(s) |
|---|---|---|---|---|
--query |
The natural language query for the browser agent to execute. | Yes | N/A | All |
--env |
The computer use environment to use. Must be one of the following: playwright, or browserbase |
No | N/A | All |
--initial_url |
The initial URL to load when the browser starts. | No | https://www.google.com | All |
--highlight_mouse |
If specified, the agent will attempt to highlight the mouse cursor’s position in the screenshots. This is useful for visual debugging. | No | False (not highlighted) | playwright |
Environment Variables
| Variable | Description | Required |
|---|---|---|
| GEMINI_API_KEY | Your API key for the Gemini model. | Yes |
| BROWSERBASE_API_KEY | Your API key for Browserbase. | Yes (when using the browserbase environment) |
| BROWSERBASE_PROJECT_ID | Your Project ID for Browserbase. | Yes (when using the browserbase environment) |
Known Issues
Playwright Dropdown Menu
On certain operating systems, the Playwright browser is unable to capture <select> elements because they are rendered by the operating system. As a result, the agent is unable to send the correct screenshot to the model.
There are several ways to mitigate this.
- Use the Browserbase option instead of Playwright.
- Inject a script like proxy-select to render a custom
<select>element. You must injectproxy-select.cssandproxy-select.jsinto each page that has a non-custom<select>element. You can do this in thePlaywright.__enter__method by adding a few lines of code, like the following (replacingPROXY_SELECT_JSandPROXY_SELECT_CSSwith the appropriate variables):
|
|
Note, option 2 does not work 100% of the time, but is a temporary workaround for certain websites. The better option is to use Browserbase.