handrew / browserpilot

Natural language browser automation
MIT License
497 stars 60 forks source link
browser-automation generative-ai gpt-3 robotic-process-automation rpa selenium selenium-python

πŸ›« BrowserPilot

An intelligent web browsing agent controlled by natural language.

demo

Language is the most natural interface through which humans give and receive instructions. Instead of writing bespoke automation or scraping code which is brittle to changes, creating and adding agents should be as simple as writing plain English.

πŸ—οΈ Installation

  1. pip install browserpilot
  2. Download Chromedriver (latest stable release) from here and place it in the same folder as this file. Unzip. In Finder, right click the unpacked chromedriver and click "Open". This will remove the restrictive default permissions and allow Python to access it.
  3. Create an environment variable in your favorite manner setting OPENAI_API_KEY to your API key.

🦭 Usage

πŸ—ΊοΈ API

The form factor is fairly simple (see below).

from browserpilot.agents.gpt_selenium_agent import GPTSeleniumAgent

instructions = """Go to Google.com
Find all textareas.
Find the first visible textarea.
Click on the first visible textarea.
Type in "buffalo buffalo buffalo buffalo buffalo" and press enter.
Wait 2 seconds.
Find all anchor elements that link to Wikipedia.
Click on the first one.
Wait for 10 seconds."""

agent = GPTSeleniumAgent(instructions, "/path/to/chromedriver")
agent.run()

The harder (but funner) part is writing the natural language prompts.

πŸ“‘ Writing Prompts

It helps if you are familiar with how Selenium works and programming in general. This is because this project uses GPT-3 to translate natural language into code, so you should be as precise as you can. In this way, it is more like writing code with Copilot than it is talking to a friend; for instance, it helps to refer to things as inputs or textareas (vs. "text box" "search box") or "button which says 'Log in'" rather than "the login button". Sometimes, it will also not pick up on specific words that are important, so it helps to break them out into separate lines. Instead of "find all the visible textareas", you do "find all the textareas" and then "find the first visible textarea".

You can look at some examples in prompts/examples to get started.

Create "functions" by enclosing instructions in BEGIN_FUNCTION func_name and END_FUNCTION, and then call them by starting a line with RUN_FUNCTION or INJECT_FUNCTION. Below is an example:

BEGIN_FUNCTION search_buffalo
Go to Google.com
Find all textareas.
Find the first visible textarea.
Click on the first visible textarea.
Type in "buffalo buffalo buffalo buffalo buffalo" and press enter.
Wait 2 seconds.
Get all anchors on the page that contain the word "buffalo".
Click on the first link.
END_FUNCTION

RUN_FUNCTION search_buffalo
Wait for 10 seconds.

You may also choose to create a yaml or json file with a list of instructions. In general, it needs to have an instructions field, and optionally a compiled field which has the processed code.

See buffalo wikipedia example.

You may pass a instruction_output_file to the constructor of GPTSeleniumAgent which will output a yaml file with the compiled instructions from GPT-3, to avoid having to pay API costs.

βœ‹πŸΌ Contributing

There are two ways I envision folks contributing.

⛩️ Architecture and Prompt Patterns

This repo was inspired by the work of Yihui He, Adept.ai, and Nat Friedman. In particular, the basic abstractions and prompts used were built off of Yihui's hackathon code. The idea to preprocess HTML and use GPT-3 to intelligently pick elements out is from Nat.

πŸŽ‰ Finished

0.2.51

-0.2.42 - 0.2.44

0.2.38 - 0.2.41

0.2.26 - 0.2.37

0.2.14 - 0.2.25

0.2.10 - 0.2.13

0.2.7 - 0.2.9

0.2.4 - 0.2.6

0.2.3

0.2.2

0.2.1

0.2.0

<0.2.0

🚨 Disclaimer 🚨

This package runs code output from the OpenAI API in Python using exec. 🚨 This is not considered a safe convention 🚨. Accordingly, you should be extra careful when using this package. The standard disclaimer follows.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.