Browser Use - AG2
Browser Use
AG2 now integrates Browser Use, allowing your agents to navigate websites, extract dynamic content, and interact with web pages. This makes automated data collection, web automation, and other tasks easier and more efficient.
Installation
Browser Use requires Python 3.11 or higher.
To get started with the Browser Use integration in AG2, follow these steps:
- Install AG2 with the
browser-useextra:
pip install ag2[openai,browser-use]
```
If you have been using `autogen` or `ag2`, all you need to do is upgrade it using:
pip install -U autogen[openai,browser-use]
```
or
pip install -U ag2[openai,browser-use]
```
as `autogen` and `ag2` are aliases for the same PyPI package.
2. Set up Playwright:
# Installs Playwright and browsers for all OS
playwright install
# Additional command, mandatory for Linux only
playwright install-deps
```
- For running the code in Jupyter, use
nest_asyncioto allow nested event loops.
pip install nest_asyncio
```
You're all set! Now you can start using browsing features in AG2.
### Imports
import os
import nest_asyncio
from autogen import AssistantAgent, UserProxyAgent, LLMConfig
from autogen.tools.experimental import BrowserUseTool
nest_asyncio.apply()
```
Agent Configuration
Configure the agents for the interaction.
config_listdefines the LLM configurations, including the model and API key.UserProxyAgentsimulates user inputs without requiring actual human interaction (set toNEVER).AssistantAgentrepresents the AI agent, configured with the LLM settings.
Browser Use supports the following models: Supported Models
We had great experience with OpenAI, Anthropic, and Gemini. However, DeepSeek and Ollama haven't performed as well.
llm_config = LLMConfig(config_list={
"api_type": "openai",
"model": "gpt-5-nano",
"api_key": os.environ["OPENAI_API_KEY"],
})
user_proxy = UserProxyAgent(name="user_proxy", human_input_mode="NEVER")
assistant = AssistantAgent(
name="assistant",
llm_config=llm_config,
)
```
### Web Browsing with Browser Use
The [`BrowserUseTool`](https://docs.ag2.ai/0.9.10/docs/api-reference/autogen/tools/experimental/BrowserUseTool) allows agents to interact with web pages—navigating, searching, and extracting information.
To see the agent's activity in real-time, set `headless` to `False` in the `browser_config`. If `True`, the browser runs in the background.
browser_use_tool = BrowserUseTool(
llm_config=llm_config,
browser_config={"headless": False},
)
browser_use_tool.register_for_execution(user_proxy)
browser_use_tool.register_for_llm(assistant)
```
Initiate Chat
Now, let’s run a task where the assistant searches Reddit for “AG2,” clicks the first post, and retrieves the first comment.
result = user_proxy.initiate_chat(
recipient=assistant,
message="Go to Reddit, search for 'ag2' in the search bar, click on the first post and return the first comment.",
max_turns=2,
)
```
The first comment on the Reddit post regarding 'ag2' is:
"My impression was that AG2 was a successor architecture for Autogen. I could be wrong, and I have not had the time to read into these very deeply."