Riding the Web with WebSurferAgent
Author: Robert Jambrecic Introduction In our Adding Browsing Capabilities to AG2 guide, we explored how to build agents with basic web surfing capabilities. Now, let’s take it to the next level with WebSurferAgent—a powerful agent that comes with built-in web browsing tools right out of the box! With WebSurferAgent, your agents can seamlessly browse the web, retrieve real-time information, and interact with web pages—all with minimal setup. WebSurferAgent with BrowserUseTool Installation To get started with the Browser Use integration in AG2, follow these steps: Install AG2 with the browser-use extra: pip install ag2[browser-use] Set up Playwright: playwright install playwright install-deps For running the code in Jupyter, use nest_asyncio to 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.agentchat import UserProxyAgent from autogen.agents import WebSurferAgent nest_asyncio.apply() Configure WebSurferAgent with Browser Use config_list = [{"model": "gpt-4o-mini", "api_key": os.environ["OPENAI_API_KEY"]}] llm_config = { "config_list": config_list, } WebSurferAgent is the one responsible for browsing the web and retrieving information. The web_tool="browser_use" tells the agent to use the BrowserUseTool to surf the web. After creating the WebSurferAgent there are two ways to start the chat session: Recommended: Using the run Method The new run method simplifies the process by eliminating the need for manual UserProxyAgent creation. ✅ Easier setup – No need to manually register tools websurfer = WebSurferAgent(name="WebSurfer", llm_config=llm_config, web_tool="browser_use") websurfer.run( message="Get info from https://docs.ag2.ai/docs/Home", tools=websurfer.tools, max_turns=2, user_input=False, ) Manual Setup: Using initiate_chat Method This method requires manually creating a UserProxyAgent and registering tools for execution. ⚠️ More setup required ⚠️ Must manually register tools websurfer = WebSurferAgent(name="WebSurfer", llm_config=llm_config, web_tool="browser_use") user_proxy = UserProxyAgent(name="user_proxy", human_input_mode="NEVER") # WebSurferAgent has a list of tools which are registered for LLM # We need to register the tools for execution with the UserProxyAgent for tool in websurfer.tools: tool.register_for_execution(user_proxy) user_proxy.initiate_chat( recipient=websurfer, message="Get info from https://docs.ag2.ai/docs/Home", max_turns=2, ) Output user_proxy (to WebSurfer): Get info from https://docs.ag2.ai/docs/Home -------------------------------------------------------------------------------- >>>>>>>> USING AUTO REPLY... WebSurfer (to user_proxy): ***** Suggested tool call (call_rl777jGrOGhc68goW5142urK): browser_use ***** Arguments: {"task":"Get info from https://docs.ag2.ai/docs/Home"} **************************************************************************** -------------------------------------------------------------------------------- >>>>>>>> EXECUTING FUNCTION browser_use... Call ID: call_rl777jGrOGhc68goW5142urK Input arguments: {'task': 'Get info from https://docs.ag2.ai/docs/Home'} INFO [agent]
![Riding the Web with WebSurferAgent](https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F9dum7qeotnf0dqvs57df.gif)
Author: Robert Jambrecic
Introduction
In our Adding Browsing Capabilities to AG2 guide, we explored how to build agents with basic web surfing capabilities. Now, let’s take it to the next level with WebSurferAgent
—a powerful agent that comes with built-in web browsing tools right out of the box!
With WebSurferAgent
, your agents can seamlessly browse the web, retrieve real-time information, and interact with web pages—all with minimal setup.
WebSurferAgent with BrowserUseTool
Installation
To get started with the Browser Use
integration in AG2, follow these steps:
-
Install AG2 with the
browser-use
extra:
pip install ag2[browser-use]
-
Set up Playwright:
playwright install playwright install-deps
-
For running the code in Jupyter, use
nest_asyncio
to 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.agentchat import UserProxyAgent
from autogen.agents import WebSurferAgent
nest_asyncio.apply()
Configure WebSurferAgent with Browser Use
config_list = [{"model": "gpt-4o-mini", "api_key": os.environ["OPENAI_API_KEY"]}]
llm_config = {
"config_list": config_list,
}
WebSurferAgent
is the one responsible for browsing the web and retrieving information. The web_tool="browser_use"
tells the agent to use the BrowserUseTool
to surf the web.
After creating the WebSurferAgent
there are two ways to start the chat session:
Recommended: Using the run
Method
The new run
method simplifies the process by eliminating the need for manual UserProxyAgent
creation.
- ✅ Easier setup – No need to manually register tools
websurfer = WebSurferAgent(name="WebSurfer", llm_config=llm_config, web_tool="browser_use")
websurfer.run(
message="Get info from https://docs.ag2.ai/docs/Home",
tools=websurfer.tools,
max_turns=2,
user_input=False,
)
Manual Setup: Using initiate_chat
Method
This method requires manually creating a UserProxyAgent
and registering tools for execution.
⚠️ More setup required
⚠️ Must manually register tools
websurfer = WebSurferAgent(name="WebSurfer", llm_config=llm_config, web_tool="browser_use")
user_proxy = UserProxyAgent(name="user_proxy", human_input_mode="NEVER")
# WebSurferAgent has a list of tools which are registered for LLM
# We need to register the tools for execution with the UserProxyAgent
for tool in websurfer.tools:
tool.register_for_execution(user_proxy)
user_proxy.initiate_chat(
recipient=websurfer,
message="Get info from https://docs.ag2.ai/docs/Home",
max_turns=2,
)
Output
user_proxy (to WebSurfer):
Get info from https://docs.ag2.ai/docs/Home
--------------------------------------------------------------------------------
>>>>>>>> USING AUTO REPLY...
WebSurfer (to user_proxy):
***** Suggested tool call (call_rl777jGrOGhc68goW5142urK): browser_use *****
Arguments:
{"task":"Get info from https://docs.ag2.ai/docs/Home"}
****************************************************************************
--------------------------------------------------------------------------------
>>>>>>>> EXECUTING FUNCTION browser_use...
Call ID: call_rl777jGrOGhc68goW5142urK
Input arguments: {'task': 'Get info from https://docs.ag2.ai/docs/Home'}
INFO [agent]