Name: Towards AI Legal Name: Towards AI, Inc. Description: Towards AI is the world's leading artificial intelligence (AI) and technology publication. Read by thought-leaders and decision-makers around the world. Phone Number: +1-650-246-9381 Email: [email protected]
228 Park Avenue South New York, NY 10003 United States
Website: Publisher: https://towardsai.net/#publisher Diversity Policy: https://towardsai.net/about Ethics Policy: https://towardsai.net/about Masthead: https://towardsai.net/about
Name: Towards AI Legal Name: Towards AI, Inc. Description: Towards AI is the world's leading artificial intelligence (AI) and technology publication. Founders: Roberto Iriondo, , Job Title: Co-founder and Advisor Works for: Towards AI, Inc. Follow Roberto: X, LinkedIn, GitHub, Google Scholar, Towards AI Profile, Medium, ML@CMU, FreeCodeCamp, Crunchbase, Bloomberg, Roberto Iriondo, Generative AI Lab, Generative AI Lab Denis Piffaretti, Job Title: Co-founder Works for: Towards AI, Inc. Louie Peters, Job Title: Co-founder Works for: Towards AI, Inc. Louis-François Bouchard, Job Title: Co-founder Works for: Towards AI, Inc. Cover:
Towards AI Cover
Logo:
Towards AI Logo
Areas Served: Worldwide Alternate Name: Towards AI, Inc. Alternate Name: Towards AI Co. Alternate Name: towards ai Alternate Name: towardsai Alternate Name: towards.ai Alternate Name: tai Alternate Name: toward ai Alternate Name: toward.ai Alternate Name: Towards AI, Inc. Alternate Name: towardsai.net Alternate Name: pub.towardsai.net
5 stars – based on 497 reviews

Frequently Used, Contextual References

TODO: Remember to copy unique IDs whenever it needs used. i.e., URL: 304b2e42315e

Resources

Take our 85+ lesson From Beginner to Advanced LLM Developer Certification: From choosing a project to deploying a working product this is the most comprehensive and practical LLM course out there!

Publication

Building a Multi-Agent System to Accomplish Complex Tasks
Latest   Machine Learning

Building a Multi-Agent System to Accomplish Complex Tasks

Last Updated on June 3, 2024 by Editorial Team

Author(s): Najib Sharifi

Originally published on Towards AI.

Building a Multi-Agent System to Accomplish Complex Tasks

When ChatGPT first arrived, it was game-changing. Now, it is used by people in all sectors and lines of work. ChatGPT demonstrated the strength of these machine learning models that most of us thought was not possible anytime soon. Whilst these LLMs have become increasingly more powerful in their capabilities, however, a very exciting development with immense potential is the use of multi-agents systems. For example, Devine AI, the first autonomous AI software engineer, is based multi-agent framework.

A Climate Analyst

If you ask chatgpt to write you an article about any topic, there are several problems which result in chatgpt not producing a good report such as no access to up-to-date data about the topic, which can lead to hallucinations. What if we break down this complex to individual tasks? Consider a climate change analyst writing a report on the latest environmental trends; he/she would need to do a number of tasks (I appreciate this may be a simplification of the role but this is just for a demonstration purpose):

Β· Research to find out all the key data from reliable sources.

Β· Analyse all the resultant data and extract key interpretations of the data.

Β· Write a report explaining the findings.

Β· The report would then get peer-reviewed to ensure the scientific report is accurate and the findings are supported by the data presented.

What if we have specialized agents for each task? i.e. one agent is the researcher, and another agent behaves as the analyst to analyze the data found, another agent is the writer, and a 4th agent is the critic who will ensure that the article findings are supported by the data presented (but hopefully unlikely real scientists, it won’t ask you to reference their completely irrelevant work in your article). These systems leverage the strengths of individual agents, each with specialized roles and capabilities, to collaboratively achieve complex tasks. This article delves into the potential of LLM agents to drive the next wave of developments, demonstrating their capabilities through a practical example of building a multiagent system.

Building a Multiagent System: Climate Change Analyst

You can build multiagent systems using frameworks like CrewAI, the work demonstrated in this article is nothing but a humble attempt at building a very simple framework for the multiagent system. How these agents communicate, remember (they have memory, short and long!) and are coordinated are crucial to their performance. The aim of this article is to set up a simple framework, by doing so, we can gain a deeper understanding of these systems rather than importing everything from a readymade library and treating it as a Blackbox. We are going to build a system that can write an article on the latest trends in climate change, as mentioned above. We will develop a team of specialized agents that can research, analyze, write a scientific report, and peer review that report.

Image generated with copilate.

Lets Dive in! Setting up the environment and importing key libraries. We need to provide the agent doing the research a tool. This tool will allow the agent to google search through an API, I will use the crewai library SerperDevTool to do this.

import os
import openai
from openai import OpenAI
from crewai_tools import SerperDevTool

os.environ["SERPER_API_KEY"] = "your serper api key goes here"
os.environ["OPENAI_API_KEY"] = "your open AI key goes here"

client = OpenAI(api_key=os.getenv("OPENAI_API_KEY"))
search_tool = SerperDevTool(api_key=os.getenv("SERPER_API_KEY"))

Defining the agent class, each agent will have a specialised role, goal and backstory which we will assign later. The agent is able to store the task queried as well as the corresponding output, enabling short term memory. It can also message all other agents, as well as read messages sent from other agents.

class Agent:
def __init__(self, name, role, backstory, goal, tools=None):
self.name = name
self.backstory = backstory
self.goal = goal
self.role = role
self.memory = []
self.tools = tools if tools else []
self.message_box = []
# adding memory for the agent to store recent tasks and outputs
def add_to_memory(self, entry):
self.memory.append(entry)

# sending messages to other agents
def send_message(self, recipient, message):
recipient.message_box.append((self.name, message))

# reading the messages sent from other agents before performing task
# this is done by removing messages from message box and added to memory
def read_messages(self):
while self.message_box:
sender, message = self.message_box.pop(0)
self.add_to_memory(f"message from the {sender}: {message}")

# we now define the function that will do the task assigned
# reading messages and adding task to the memory first
# the agent will take up the specialised role assigned and querry gpt3.5

def do_task(self, task, inputs):
self.read_messages()
task_info = task.info
self.add_to_memory(f"doing task: {task_info}")

'''for the research agent, the search_tool will be assigned to the agent
which it will be able to use to do a google search online'''


if 'search_tool' in self.tools:
search_query = task_info
search_results = search_tool.run(query=search_query)
inputs['search_results'] = search_results
task_info += f"\n\nsearch results:\n{search_results}"

llm_response = client.chat.completions.create(
model="gpt-3.5-turbo",
messages=[
{"role": "system", "content": f"you are a {self.role}. {self.backstory} Your goal is {self.goal}."},
{"role": "user", "content": task_info}
]
)
output = llm_response.choices[0].message.content
self.add_to_memory(f"task output: {output}")

return output

The Architect, whose purpose is to (besides creating the matrix of course) assign the tasks to the corresponding agents and coordinate the flow of information between the agents. In this framework, besides the messaging between agents, the work is in a sequential form i.e. work from each agent is passed to the next one, there is no delegation or iteration of the tasks for the agents. However, in crewai framework, I think it has these properties which make it very powerful in its capabilities.

class TheArchitect:
def __init__(self, agents, tasks):
# dictionary of all agents based on name
self.agents = {agent.name: agent for agent in agents}
self.tasks = tasks

def process(self, inputs):
results = {}
current_result = None

for task in self.tasks:
task_agent = self.agents[task.agent.name]
'''to help with debugging and also checking flow of info
we can check/print which tasks are assigned to which agent'''

print(f"assignin task {task.name} to agent {task_agent.name}: {task.info}")

if current_result:
inputs['previous_result'] = current_result

if 'search' in inputs:
search_query = inputs['search']
search_results = search_tool.run(query=search_query)
inputs['search_results'] = search_results

agent_output = task_agent.do_task(task, inputs)
current_result = agent_output

# send the agent's output as a message to all other agents
for agent_name, agent in self.agents.items():
if agent_name != task_agent.name:
task_agent.send_message(agent, agent_output)

results[task.agent.name] = agent_output

return results

Now that we have defined the agents class and the architect class, lets create instances of these classes to define different agents with different roles.

We can now define all the agents and give them names, roles, goals, backstories and tools. These agents only data collectors, data scientists, and report writers. In this case, we only have the researcher/data collector with a tool.

data_collector = Agent(
name="researcher",
role="Climate Data Collector",
goal="Collect comprehensive climate data from multiple sources.",
backstory="You gather climate data on temperature, carbon dioxide levels and other variables relevant to climate change, from reliable sources.",
tools=['search_tool']
)

data_analyst = Agent(
name="Data Scientist",
role="Climate Data Scientist",
goal="Analyse the collected climate data to identify significant trends.",
backstory="You analyse climate data to find significant trends and understand the impact of various factors on climate change.",
tools=[]
)

report_writer = Agent(
name="Report Writer",
role="Senior Scientific Report Writer",
goal="Generate a comprehensive report on climate change findings.",
backstory="You write detailed scientific reports based on the analysed climate data, highlighting key findings and implications.",
tools=[]
)

peer_reviewer = Agent(
name="Peer Reviewer",
role="Scientific Peer Reviewer",
goal="Review the scientific report for accuracy, clarity, and completeness.",
backstory="You review scientific reports to ensure they are accurate, clear, and meet the standards for scientific publication.",
tools=[]
)

final_report_writer = Agent(
name="Final Report Writer",
role="Final Report Writer",
goal="Incorporate peer review feedback and finalize the scientific report.",
backstory="You finalize the scientific report by incorporating feedback from peer reviewer and ensure it is publication ready.",
tools=[]
)

We need to split any problem into a series of tasks and assign an agent to each one. The information and expected_output is critical to getting the agent to behave and output what you desire from the agent.

analyse_data = Task(
info=(
"Using the following climate data, analyze for trends and patterns:\n{previous_result}\n"
"1. Identify significant trends in temperature, CO2 levels, and precipitation.\n"
"2. Determine potential causes of observed trends.\n"
"3. Summarize key findings in a detailed analysis report."
),
expected_output="Detailed analysis report on climate data trends and potential causes.",
agent=data_analyst,
name="Data Analysis"
)

write_report = Task(
info=(
"Using the following analysis report, write a comprehensive scientific report on climate change findings:\n{previous_result}\n"
"1. Include an introduction, methodology, results, discussion, and conclusion.\n"
"2. Use clear and precise language suitable for a scientific audience.\n"
"3. Ensure all findings are supported by data and analysis."
),
expected_output="Comprehensive scientific report on climate change findings.",
agent=report_writer,
name="Report Writing"
)

review_report = Task(
info=(
"Using the following scientific report, review for accuracy, clarity, and completeness:\n{previous_result}\n"
"1. Ensure the report adheres to scientific standards.\n"
"2. Check for any errors or inaccuracies in data and analysis.\n"
"3. Provide feedback and suggestions for improvement."
),
expected_output="Reviewed and revised scientific report, ready for publication.",
agent=peer_reviewer,
name="Peer Review"
)

finalize_report = Task(
info=(
"Using the following peer-reviewed report, incorporate feedback and finalize the scientific report:\n{previous_result}\n"
"1. Address all feedback and suggestions provided by the peer reviewer.\n"
"2. Ensure the report is polished and ready for publication.\n"
"3. Provide the final version of the scientific report."
),
expected_output="Finalized scientific report, ready for publication.",
agent=final_report_writer,
name="Finalize Report"
)

Let’s bring it all together now. We can now create a system of agents and tasks and run it.

ClimateResearchSystem = TheArchitect(
agents=[data_collector, data_analyst, report_writer, peer_reviewer, final_report_writer],
tasks=[collect_data, analyse_data, write_report, review_report, finalize_report]
)


result = ClimateResearchSystem.process(inputs={
"topic": "Climate Change",
"search": "latest climate data trends"
})

The final report, lets visualise the output of the multiagent system using markdown. The key question is, is this report any better than using chatgpt (no agents) to write a scientific report on climate trends?

from IPython.display import Markdown
Markdown(result['Final Report Writer'])

This article could be significantly improved if you provide it with better tools; for example, providing some mathematical tools for data analysis could allow the agent to perform numerical analysis and present findings. These systems will only be as good as the tools you provide it with, which in this case, we have not provided any, besides a tool to search the internet. That brings yes to the end of the article; thank you for taking the time to read it, I hope you found it insightful! If you are interested, definitely explore these multiagent systems for a range of different problems, it’s not just about writing reports but with the right tools defined, these agents can be very powerful. This topic is still under heavy development, it is very exciting to see how it will develop.

Unless otherwise noted, all images are by the author

Join thousands of data leaders on the AI newsletter. Join over 80,000 subscribers and keep up to date with the latest developments in AI. From research to projects and ideas. If you are building an AI startup, an AI-related product, or a service, we invite you to consider becoming aΒ sponsor.

Published via Towards AI

Feedback ↓