Building a Multi-agent Internet Research Assistant
- Rifx.Online
- Programming , Machine Learning , Chatbots
- 27 Dec, 2024
…with OpenAI Swarm & Llama 3.2 (100% local).
Let’s build a multi-agent internet research assistant using OpenAI Swarm & Llama 3.2 (100% local):
Hands-on tutorial to build a multi-agent internet research assistant app that:
- Accepts a user query.
- Searches the web about it.
- And turns it into a well-crafted article.
We used three agents in this app:
- Agent 1 → Accepts the user query and searches the web.
- Agent 2 → Accepts the web results from Agent 1 and refines them.
- Agent 3 → A technical writing agent that accepts the refined results, drafts an article, and sends it back to the user.
Recently, OpenAI released Swarm.
It’s an open-source framework designed to manage and coordinate multiple AI agents in a highly customizable way.
AI Agents are autonomous systems that can reason, think, plan, figure out the relevant sources and extract information from them when needed, take actions, and even correct themselves if something goes wrong.
Today, let’s cover a practical and hands-on demo of this. We’ll build an internet research assistant app that:
- Accepts a user query.
- Searches the web about it.
- And turns it into a well-crafted article.
We shall use:
- OpenAI Swarm for multi-agent orchestration.
- Streamlit for the UI.
Here’s the step-by-step workflow of our multi-agent app.
As depicted above, we have three agents:
- Agent 1 → Accepts the user query and searches the web.
- Agent 2 → Accepts the web results from Agent 1 and refines them.
- Agent 3 → A technical writing agent that accepts the refined results, drafts an article, and sends it back to the user.
Let’s build this application!
Imports
First, we begin with some standard imports:
- We use Swarm from OpenAI to build our multi-agent app.
- We use duckduckgo_search to search the web.
Next, we define the MODEL name, and initialize swarm client and search client:
Agent 1
This agent must accept the user’s query, search the web, and return the raw web results.
To build this, we first implement a function that accepts the query and returns the raw results.
- Line 7: Search the web.
- Line 9–16: Collect all the web results (title + URL + Body) in a single string and return it.
Next, we define our web search agent that will utilize the above function, and we also specify the instructions for this agent below:
Line 1: We specify the role.
Line 6–11: We define an object of Agent class (from OpenAI Swarm), specify the above function, the instructions, and the LLM.
Agent 2
The results returned by Agent 1 will be pretty messy and may have a ton of irrelevant information. We need another agent to filter the appropriate information.
Like Agent 1, we define another object of the Agent
class and pass the instructions:
Agent 3
Finally, we build another agent that accepts the above-filtered results and drafts an article:
Stitch them together
While we have defined the three agents above, the multi-agent app does not know the order in which these agents must run and whose output must be passed to the next agent.
Thus, we need to stitch them together in a workflow function.
Let’s do it step by step for simplicity.
- First, we pass the user query to the web search agent, which generates raw responses:
Next, we pass the raw responses to the web filter agent:
Done!
This is the entire code for the run_workflow method:
Executing the run_workflow
method generates the desired output, as depicted below (and in the video at the top of this newsletter:
Of course, we did not cover the Streamlit part in this tutorial.
We launched this repo today, wherein we’ll publish the code for such hands-on AI engineering newsletter issues.
This repository will be dedicated to:
- In-depth tutorials on LLMs and RAGs.
- Real-world AI agent applications.
- Examples to implement, adapt, and scale in your projects.
Do you like the topic? Join our newsletter Subscribe
If you like the article and would like to support me, make sure to:
👏 Clap for the story (100 Claps) and follow me 👉🏻 Mohammed Lubbad
📑 View more content on my Medium Profile
🔔 Follow Me: LinkedIn | Medium | GitHub | Twitter | Telegram
🚀 Help me reach a wider audience by sharing my content with your friends and colleagues.