In recent years, large language models (LLMs) have redefined the capabilities of artificial intelligence by generating human-like text, solving complex problems and performing tasks autonomously.
However, as tasks become more intricate and interdisciplinary, a single AI model might not always be sufficient. This is where the concept of multiagent systems (MAS) in LLMs comes into play. MAS allows multiple AI agents to collaborate, each specializing in different aspects of a problem, working together to achieve a common goal.
This tutorial will explore the latest trend of multiagent systems in LLMs using Python. We’ll cover what multiagent systems are, why they are important and how to implement them step by step with Python using tools like LangChain.
What Are Multiagent Systems?
A multiagent system (MAS) is an environment where several autonomous agents interact, cooperate or even compete with each other to solve problems. Each agent has its abilities, strengths and focus areas, allowing the system to handle complex tasks more efficiently. These systems excel in scenarios that require collaboration, parallel task execution or even negotiation.
In LLMs, multiagent systems can:
- Collaborate on tasks that require multiple areas of expertise (for instance, one agent focuses on math while another handles natural language understanding).
- Negotiate with each other to resolve conflicting objectives.
- Solve complex, multistep problems in parallel, improving speed and accuracy.
Use Cases of Multiagent Systems
- Financial planning: One agent could focus on analyzing stock trends while another agent could predict the future behavior of the market.
- Health care: One agent focuses on diagnostic analysis, while another assists in patient history review, collaborating for a comprehensive health care recommendation.
- Supply chain optimization: Agents can specialize in logistics, procurement or demand forecasting, improving decision-making for the entire supply chain.
Why Use Multiagent Systems?
- Specialization: Different agents specialize in different tasks, making problem-solving more efficient.
- Parallelism: Agents can work simultaneously, significantly reducing the time required to complete multistep operations.
- Collaboration: Multiple agents work together, leveraging their unique strengths to achieve optimal results.
- Adaptability: Agents can negotiate or adjust strategies in real time, adapting to evolving tasks.
Setting Up a Multiagent System With Python
Let’s move from theory to practice. In this section, we will demonstrate how to build a multiagent system using Python with the LangChain library, which allows seamless interaction between different LLM-powered agents.
Installing Dependencies
To get started, we need to install LangChain and set up an LLM service like OpenAI.
pip install langchain openai
You will also need an OpenAI API key, which you can obtain by signing up for OpenAI’s API service.
Initializing Agents and Tools
First, we’ll define our LLM (GPT model) and a set of tools that our agents will use. These tools could be anything from a calculator to web search functionality. Let’s initialize agents that collaborate to solve a task involving both information retrieval and mathematical computation.
from langchain.agents import initialize_agent, load_tools
from langchain.llms import OpenAI
# Initialize OpenAI model
llm = OpenAI(api_key="your_openai_api_key", model="gpt-4")
# Load tools (agents) such as search and calculator
tools = load_tools(["serpapi", "calculator"], llm)
# Initialize a multi-agent system
multi_agent = initialize_agent(
tools,
llm,
agent_type="multi-agent",
verbose=True
)
# Example task: Find the exchange rate of USD to EUR and calculate for 1500 units
task = "What is the current exchange rate of USD to EUR? Multiply it by 1500."
# Run the multi-agent system to complete the task
result = multi_agent.run(task)
print(result)
How It Works
- Agent collaboration: In this example, one agent fetches the real-time exchange rate using a search tool (such as SERP API), while another agent uses the calculator tool to multiply the rate by 1,500.
- Task decomposition: The LLM breaks the task into subtasks (fetching the rate and performing a calculation) and assigns these subtasks to the appropriate agents.
Building a Complex Multiagent System
Now that we’ve seen a basic example, let’s build a more complex system involving multiple agents that solve distinct parts of a problem. Consider a scenario where we are building a travel assistant that can handle multiple queries related to booking flights, checking the weather and performing budget calculations.
Step-by-Step Code: Travel Assistant Multiagent System
# Define task-specific tools
from langchain.tools import Tool
# Weather checking tool
def get_weather(city):
return f"The weather in {city} is sunny with a temperature of 25°C."
# Flight booking tool
def book_flight(destination, date):
return f"Flight to {destination} on {date} has been booked."
# Budget calculation tool
def calculate_budget(amount, expenses):
remaining = amount - sum(expenses)
return f"Your remaining budget is {remaining}."
# Define our agents
weather_tool = Tool("get_weather", get_weather)
flight_tool = Tool("book_flight", book_flight)
budget_tool = Tool("calculate_budget", calculate_budget)
# Combine agents into a multi-agent system
tools = [weather_tool, flight_tool, budget_tool]
multi_agent = initialize_agent(tools, llm, agent_type="multi-agent", verbose=True)
# Example task
task = """
I want to book a flight to Paris for December 20th, check the weather in Paris,
and calculate my remaining budget if I have $2000 and my expenses are $500 and $300.
"""
# Execute the multi-agent system
result = multi_agent.run(task)
print(result)
What’s Happening?
- Flight agent: The
book_flight
agent handles the flight booking part of the task. - Weather agent: The
get_weather
agent retrieves weather data for Paris. - Budget agent: The
calculate_budget
agent computes the user’s remaining budget based on their input.
In this scenario, each agent works on a specific component of the larger problem, and they collaborate to provide a comprehensive result. The entire process is driven by the LLM, which coordinates the efforts of the agents.
Advanced Use Cases of Multiagent Systems
Health Care Collaboration
In health care, different agents can focus on various parts of a patient’s treatment process. For example:
- One agent could analyze medical imaging.
- Another agent reviews a patient’s medical history.
- A third agent provides diagnostic recommendations.
By working together, these agents can generate a comprehensive report that aids in more accurate and faster medical decisions.
Supply Chain Optimization
Multiagent systems can be used to manage different aspects of the supply chain:
- A logistics agent tracks shipment times.
- A procurement agent monitors inventory levels.
- A forecasting agent predicts future demand.
Together, they can optimize the supply chain by reducing delays, cutting costs and improving overall efficiency.
Conclusion
Multiagent systems (MAS) represent a groundbreaking trend in the development of AI-driven solutions. By allowing multiple agents to collaborate, each with its own area of expertise, MAS dramatically enhance the efficiency and effectiveness of large-scale problem-solving tasks. With Python tools like LangChain, implementing multiagent systems is becoming easier, enabling developers to create intelligent systems that go beyond simple automation.
About the author: Oladimeji Sowole
Oladimeji Sowole is a member of the Andela Talent Network, a private marketplace for global tech talent. A Data Scientist and Data Analyst with more than 6 years of professional experience building data visualizations with different tools and predictive models for actionable insights, he has hands-on expertise in implementing technologies such as Python, R, and SQL to develop solutions that drive client satisfaction. A collaborative team player, he has a great passion for solving problems.