Multiagent Web Assistant
Have several agents collaborate in a multi-agent hierarchy ๐ค๐ค๐ค
Authored by: Aymeric Roucher
This tutorial is advanced. You should have notions from this other cookbook first!
In this notebook we will make a multi-agent web browser: an agentic system with several agents collaborating to solve problems using the web!
It will be a simple hierarchy, using a ManagedAgent object to wrap the managed web search agent:
+----------------+
| Manager agent |
+----------------+
|
_______________|______________
| |
Code interpreter +--------------------------------+
tool | Managed agent |
| +------------------+ |
| | Web Search agent | |
| +------------------+ |
| | | |
| Web Search tool | |
| Visit webpage tool |
+--------------------------------+
Let's set up this system.
Run the line below to install the required dependencies:
Let's login in order to call the HF Inference API:
โก๏ธ Our agent will be powered by Qwen/Qwen2.5-72B-Instruct using HfApiEngine class that uses HF's Inference API: the Inference API allows to quickly and easily run any OS model.
Note: The Inference API hosts models based on various criteria, and deployed models may be updated or replaced without prior notice. Learn more about it here.
๐ Create a web search tool
For web browsing, we can already use our pre-existing DuckDuckGoSearchTool tool to provide a Google search equivalent.
But then we will also need to be able to peak into the page found by the DuckDuckGoSearchTool.
To do so, we could import the library's built-in VisitWebpageTool, but we will build it again to see how it's done.
So let's create our VisitWebpageTool tool from scratch using markdownify.
Ok, now let's initialize and test our tool!
Hugging Face - Wikipedia [Jump to content](#bodyContent) Main menu Main menu move to sidebar hide Navigation * [Main page](/wiki/Main_Page "Visit the main page [z]") * [Contents](/wiki/Wikipedia:Contents "Guides to browsing Wikipedia") * [Current events](/wiki/Portal:Current_events "Articles related to current events") * [Random article](/wiki/Special:Random "Visit a randomly selected article [x]") * [About Wikipedia](/wiki/Wikipedia:About "Learn about Wikipedia and how it works") * [Contac
Build our multi-agent system ๐ค๐ค๐ค
Now that we have all the tools search and visit_webpage, we can use them to create the web agent.
Which configuration to choose for this agent?
- Web browsing is a single-timeline task that does not require parallel tool calls, so JSON tool calling works well for that. We thus choose a
ReactJsonAgent. - Also, since sometimes web search requires exploring many pages before finding the correct answer, we prefer to increase the number of
max_iterationsto 10.
We then wrap this agent into a ManagedAgent that will make it callable by its manager agent.
Finally we create a manager agent, and upon initialization we pass our managed agent to it in its managed_agents argument.
Since this agent is the one tasked with the planning and thinking, advanced reasoning will be beneficial, so a ReactCodeAgent will be the best choice.
Also, we want to ask a question that involves the current year: so let us add additional_authorized_imports=["time", "datetime"]
That's all! Now let's run our system! We select a question that requires some calculation and
15
Our agents managed to efficiently collaborate towards solving the task! โ
๐ก You can easily extend this to more agents: one does the code execution, one the web search, one handles file loadings...
๐ค๐ญ One could even think of doing more complex, tree-like hierarchies, with one CEO agent handling multiple middle managers, each with several reports.
We could even add more intermediate layers of management, each with multiple daily meetings, lots of agile stuff with scrum masters, and each new component adds enough friction to ensure the tasks never get done... Ehm wait, no, let's stick with our simple structure.