You can use either OpenAI or Anthropic as your LLM provider.
4. Quick Start Code Example
The following Python script demonstrates how to use browser-use to control a browser. In this example, the agent is tasked with visiting Reddit, searching for "browser-use," and returning the first comment on the top post.
Code Example
python
from langchain_openai import ChatOpenAI
from browser_use import Agent
import asyncio
from dotenv import load_dotenv
# Load API keys from .env file
load_dotenv()
# Task for the agent
task = "Go to Reddit, search for 'browser-use', click on the first post, and return the first comment."
# Main function to execute the task
async def main():
# Initialize the agent with task and LLM
agent = Agent(
task=task,
llm=ChatOpenAI(model="gpt-4o"), # Use GPT-4
)
# Run the agent and get the result
result = await agent.run()
print(result)
# Run the async function
asyncio.run(main())
Steps to Run the Script
Save the above script in a .py file (e.g., example.py).
Run the script:
bash
python example.py
5. Optional UI for Testing
You can also use a Gradio-based UI to test the package functionality interactively.
Install Gradio
bash
pip install gradio
Run Gradio Demo
Run the following script to launch a Gradio-based user interface:
python
python examples/ui/gradio_demo.py
This will open a web interface where you can input tasks for the agent to perform.
6. Additional Features
Custom Tasks: You can define tasks such as scraping data, filling forms, clicking links, or navigating websites. Modify the task parameter accordingly.
Cloud Version: If you don’t want to set up the package locally, you can try the hosted version directly: Try it now.
Test with Examples: The repository includes several example scripts for different use cases. Explore the examples folder for more ideas.
7. Troubleshooting
If you encounter issues, ensure that:
All dependencies are installed (pip install -r requirements.txt if available).
API keys in the .env file are valid.
For debugging, you can enable logs in your script or use the example scripts in the repository.