Multi AI Agent Methods 101

0
45
Multi AI Agent Methods 101


Automating Routine Duties in Knowledge Supply Administration with CrewAI

Picture by DALL-E 3

Initially, when ChatGPT simply appeared, we used easy prompts to get solutions to our questions. Then, we encountered points with hallucinations and started utilizing RAG (Retrieval Augmented Era) to offer extra context to LLMs. After that, we began experimenting with AI brokers, the place LLMs act as a reasoning engine and may determine what to do subsequent, which instruments to make use of, and when to return the ultimate reply.

The following evolutionary step is to create groups of such brokers that may collaborate with one another. This strategy is logical because it mirrors human interactions. We work in groups the place every member has a particular position:

  • The product supervisor proposes the subsequent venture to work on.
  • The designer creates its look and really feel.
  • The software program engineer develops the answer.
  • The analyst examines the info to make sure it performs as anticipated and identifies methods to enhance the product for patrons.

Equally, we are able to create a workforce of AI brokers, every specializing in one area. They’ll collaborate and attain a remaining conclusion collectively. Simply as specialization enhances efficiency in actual life, it might additionally profit the efficiency of AI brokers.

One other benefit of this strategy is elevated flexibility. Every agent can function with its personal immediate, set of instruments and even LLM. As an illustration, we are able to use totally different fashions for various elements of our system. You should use GPT-4 for the agent that wants extra reasoning and GPT-3.5 for the one which does solely easy extraction. We are able to even fine-tune the mannequin for small particular duties and use it in our crew of brokers.

The potential drawbacks of this strategy are time and value. A number of interactions and information sharing between brokers require extra calls to LLM and eat extra tokens. This might lead to longer wait occasions and elevated bills.

There are a number of frameworks obtainable for multi-agent programs at the moment.
Listed below are a number of the hottest ones:

  • AutoGen: Developed by Microsoft, AutoGen makes use of a conversational strategy and was one of many earliest frameworks for multi-agent programs,
  • LangGraph: Whereas not strictly a multi-agent framework, LangGraph permits for outlining complicated interactions between actors utilizing a graph construction. So, it will also be tailored to create multi-agent programs.
  • CrewAI: Positioned as a high-level framework, CrewAI facilitates the creation of “crews” consisting of role-playing brokers able to collaborating in varied methods.

I’ve determined to start out experimenting with multi-agent frameworks from CrewAI because it’s fairly extensively well-liked and person pleasant. So, it appears to be like like a great possibility to start with.

On this article, I’ll stroll you thru learn how to use CrewAI. As analysts, we’re the area consultants chargeable for documenting varied knowledge sources and addressing associated questions. We’ll discover learn how to automate these duties utilizing multi-agent frameworks.

Organising the atmosphere

Let’s begin with establishing the atmosphere. First, we have to set up the CrewAI primary package deal and an extension to work with instruments.

pip set up crewai
pip set up 'crewai[tools]'

CrewAI was developed to work primarily with OpenAI API, however I’d additionally prefer to strive it with an area mannequin. In keeping with the ChatBot Enviornment Leaderboard, the most effective mannequin you’ll be able to run in your laptop computer is Llama 3 (8b parameters). It will likely be essentially the most possible possibility for our use case.

We are able to entry Llama fashions utilizing Ollama. Set up is fairly easy. It is advisable obtain Ollama from the web site after which undergo the set up course of. That’s it.

Now, you’ll be able to take a look at the mannequin in CLI by working the next command.

ollama run llama3

For instance, you’ll be able to ask one thing like this.

1*LKh7JSgBW gtHM2YTFIBrw

Let’s create a customized Ollama mannequin to make use of later in CrewAI.

We’ll begin with a ModelFile (documentation). I solely specified the bottom mannequin (llama3), temperature and cease sequence. Nonetheless, you may add extra options. For instance, you’ll be able to decide the system message utilizing SYSTEM key phrase.

FROM llama3

# set parameters
PARAMETER temperature 0.5
PARAMETER cease End result

I’ve saved it right into a Llama3ModelFile file.

Let’s create a bash script to load the bottom mannequin for Ollama and create the customized mannequin we outlined in ModelFile.

#!/bin/zsh

# outline variables
model_name="llama3"
custom_model_name="crewai-llama3"

# load the bottom mannequin
ollama pull $model_name

# create the mannequin file
ollama create $custom_model_name -f ./Llama3ModelFile

Let’s execute this file.

chmod +x ./llama3_setup.sh
./llama3_setup.sh

Yow will discover each information on GitHub: Llama3ModelFile and llama3_setup.sh

We have to initialise the next environmental variables to make use of the native Llama mannequin with CrewAI.

os.environ["OPENAI_API_BASE"]='http://localhost:11434/v1'

os.environ["OPENAI_MODEL_NAME"]='crewai-llama3'
# custom_model_name from the bash script

os.environ["OPENAI_API_KEY"] = "NA"

We’ve completed the setup and are able to proceed our journey.

Use circumstances: working with documentation

As analysts, we regularly play the position of subject material consultants for knowledge and a few data-related instruments. In my earlier workforce, we used to have a channel with nearly 1K contributors, the place we had been answering plenty of questions on our knowledge and the ClickHouse database we used as storage. It took us various time to handle this channel. It might be attention-grabbing to see whether or not such duties may be automated with LLMs.

For this instance, I’ll use the ClickHouse database. Should you’re , You possibly can study extra about ClickHouse and learn how to set it up domestically in my earlier article. Nonetheless, we gained’t utilise any ClickHouse-specific options, so be happy to stay to the database you know.

I’ve created a fairly easy knowledge mannequin to work with. There are simply two tables in our DWH (Knowledge Warehouse): ecommerce_db.customers and ecommerce_db.classes. As you may guess, the primary desk accommodates details about the customers of our service.

1*LukeFk5kSyBCOU4bV3yfzg

The ecommerce_db.classes desk shops details about person classes.

1*WqZwGilme NwyQtRobomSw

Relating to knowledge supply administration, analysts usually deal with duties like writing and updating documentation and answering questions on this knowledge. So, we’ll use LLM to put in writing documentation for the desk within the database and educate it to reply questions on knowledge or ClickHouse.

However earlier than shifting on to the implementation, let’s study extra in regards to the CrewAI framework and its core ideas.

CrewAI fundamental ideas

The cornerstone of a multi-agent framework is an agent idea. In CrewAI, brokers are powered by role-playing. Position-playing is a tactic once you ask an agent to undertake a persona and behave like a top-notch backend engineer or useful buyer help agent. So, when making a CrewAI agent, you have to specify every agent's position, objective, and backstory in order that LLM is aware of sufficient to play this position.

The brokers’ capabilities are restricted with out instruments (capabilities that brokers can execute and get outcomes). With CrewAI, you should use one of many predefined instruments (for instance, to go looking the Web, parse an internet site, or do RAG on a doc), create a customized device your self or use LangChain instruments. So, it’s fairly straightforward to create a strong agent.

Let’s transfer on from brokers to the work they’re doing. Brokers are engaged on duties (particular assignments). For every process, we have to outline an outline, anticipated output (definition of carried out), set of obtainable instruments and assigned agent. I actually like that these frameworks observe the managerial finest practices like a transparent definition of carried out for the duties.

The following query is learn how to outline the execution order for duties: which one to work on first, which of them can run in parallel, and many others. CrewAI carried out processes to orchestrate the duties. It offers a few choices:

  • Sequential —essentially the most easy strategy when duties are referred to as one after one other.
  • Hierarchical — when there’s a supervisor (specified as LLM mannequin) that creates and delegates duties to the brokers.

Additionally, CrewAI is engaged on a consensual course of. In such a course of, brokers will be capable of make choices collaboratively with a democratic strategy.

There are different levers you should use to tweak the method of duties’ execution:

  • You possibly can mark duties as “asynchronous”, then they are going to be executed in parallel, so it is possible for you to to get a solution sooner.
  • You should use the “human enter” flag on a process, after which the agent will ask for human approval earlier than finalising the output of this process. It could possibly help you add an oversight to the course of.

We’ve outlined all the first constructing blocks and may talk about the holly grail of CrewAI — crew idea. The crew represents the workforce of brokers and the set of duties they are going to be engaged on. The strategy for collaboration (processes we mentioned above) will also be outlined on the crew stage.

Additionally, we are able to arrange the reminiscence for a crew. Reminiscence is essential for environment friendly collaboration between the brokers. CrewAI helps three ranges of reminiscence:

  • Quick-term reminiscence shops data associated to the present execution. It helps brokers to work collectively on the present process.
  • Lengthy-term reminiscence is knowledge in regards to the earlier executions saved within the native database. This sort of reminiscence permits brokers to study from earlier iterations and enhance over time.
  • Entity reminiscence captures and constructions details about entities (like personas, cities, and many others.)

Proper now, you’ll be able to solely change on all kinds of reminiscence for a crew with none additional customisation. Nonetheless, it doesn’t work with the Llama fashions.

We’ve discovered sufficient in regards to the CrewAI framework, so it’s time to start out utilizing this data in observe.

Use case: writing documentation

Let’s begin with a easy process: placing collectively the documentation for our DWH. As we mentioned earlier than, there are two tables in our DWH, and I wish to create an in depth description for them utilizing LLMs.

First strategy

At first, we want to consider the workforce construction. Consider this as a typical managerial process. Who would you rent for such a job?

I’d break this process into two elements: retrieving knowledge from a database and writing documentation. So, we want a database specialist and a technical author. The database specialist wants entry to a database, whereas the author gained’t want any particular instruments.

1*0YLuYhwdFr0hKuS46qjEfA

Now, we have now a high-level plan. Let’s create the brokers.

For every agent, I’ve specified the position, objective and backstory. I’ve tried my finest to offer brokers with all of the wanted context.

database_specialist_agent = Agent(
position = "Database specialist",
objective = "Present knowledge to reply enterprise questions utilizing SQL",
backstory = '''You might be an professional in SQL, so you'll be able to assist the workforce
to collect wanted knowledge to energy their choices.
You might be very correct and take into consideration all of the nuances in knowledge.''',
allow_delegation = False,
verbose = True
)

tech_writer_agent = Agent(
position = "Technical author",
objective = '''Write partaking and factually correct technical documentation
for knowledge sources or instruments''',
backstory = '''
You might be an professional in each know-how and communications, so you'll be able to simply clarify even subtle ideas.
You base your work on the factual data supplied by your colleagues.
Your texts are concise and may be simply understood by a large viewers.
You employ skilled however reasonably an off-the-cuff model in your communication.
''',
allow_delegation = False,
verbose = True
)

We’ll use a easy sequential course of, so there’s no want for brokers to delegate duties to one another. That’s why I specified allow_delegation = False.

The following step is setting the duties for brokers. However earlier than shifting to them, we have to create a customized device to hook up with the database.

First, I put collectively a perform to execute ClickHouse queries utilizing HTTP API.

CH_HOST = 'http://localhost:8123' # default deal with 

def get_clickhouse_data(question, host = CH_HOST, connection_timeout = 1500):
r = requests.put up(host, params = {'question': question},
timeout = connection_timeout)
if r.status_code == 200:
return r.textual content
else:
return 'Database returned the next error:n' + r.textual content

When working with LLM brokers, it’s essential to make instruments fault-tolerant. For instance, if the database returns an error (status_code != 200), my code gained’t throw an exception. As an alternative, it’ll return the error description to the LLM so it might probably try and resolve the subject.

To create a CrewAI customized device, we have to derive our class from crewai_tools.BaseTool, implement the _run technique after which create an occasion of this class.

from crewai_tools import BaseTool

class DatabaseQuery(BaseTool):
title: str = "Database Question"
description: str = "Returns the results of SQL question execution"

def _run(self, sql_query: str) -> str:
# Implementation goes right here
return get_clickhouse_data(sql_query)

database_query_tool = DatabaseQuery()

Now, we are able to set the duties for the brokers. Once more, offering clear directions and all of the context to LLM is essential.

table_description_task = Process(
description = '''Present the excellent overview for the info
in desk {desk}, in order that it's straightforward to grasp the construction
of the info. This process is essential to place collectively the documentation
for our database''',
expected_output = '''The great overview of {desk} within the md format.
Embody 2 sections: columns (record of columns with their varieties)
and examples (the primary 30 rows from desk).''',
instruments = [database_query_tool],
agent = database_specialist_agent
)

table_documentation_task = Process(
description = '''Utilizing supplied details about the desk,
put collectively the detailed documentation for this desk in order that
individuals can use it in observe''',
expected_output = '''Nicely-written detailed documentation describing
the info scheme for the desk {desk} in markdown format,
that offers the desk overview in 1-2 sentences then then
describes every columm. Construction the columns description
as a markdown desk with column title, kind and outline.''',
instruments = [],
output_file="table_documentation.md",
agent = tech_writer_agent
)

You might need observed that I’ve used {desk} placeholder within the duties’ descriptions. We’ll use desk as an enter variable when executing the crew, and this worth might be inserted into all placeholders.

Additionally, I’ve specified the output file for the desk documentation process to avoid wasting the ultimate consequence domestically.

We have now all we want. Now, it’s time to create a crew and execute the method, specifying the desk we’re occupied with. Let’s strive it with the customers desk.

crew = Crew(
brokers = [database_specialist_agent, tech_writer_agent],
duties = [table_description_task, table_documentation_task],
verbose = 2
)

consequence = crew.kickoff({'desk': 'ecommerce_db.customers'})

It’s an thrilling second, and I’m actually wanting ahead to seeing the consequence. Don’t fear if execution takes a while. Brokers make a number of LLM calls, so it’s completely regular for it to take a couple of minutes. It took 2.5 minutes on my laptop computer.

We requested LLM to return the documentation in markdown format. We are able to use the next code to see the formatted lead to Jupyter Pocket book.

from IPython.show import Markdown
Markdown(consequence)

At first look, it appears to be like nice. We’ve bought the legitimate markdown file describing the customers' desk.

1*5Ls4Gp N feYnJr4jO5k g

However wait, it’s incorrect. Let’s see what knowledge we have now in our desk.

1*LukeFk5kSyBCOU4bV3yfzg

The columns listed within the documentation are utterly totally different from what we have now within the database. It’s a case of LLM hallucinations.

We’ve set verbose = 2 to get the detailed logs from CrewAI. Let’s learn by the execution logs to determine the foundation reason behind the drawback.

First, the database specialist couldn’t question the database on account of issues with quotes.

1*8F9OpgR0LJA mWw7ib hrw

The specialist didn’t handle to resolve this drawback. Lastly, this chain has been terminated by CrewAI with the next output: Agent stopped on account of iteration restrict or time restrict.

This implies the technical author didn’t obtain any factual details about the info. Nonetheless, the agent continued and produced utterly pretend outcomes. That’s how we ended up with incorrect documentation.

Fixing the points

Though our first iteration wasn’t profitable, we’ve discovered loads. We have now (at the very least) two areas for enchancment:

  • Our database device is just too troublesome for the mannequin, and the agent struggles to make use of it. We are able to make the device extra tolerant by eradicating quotes from the start and finish of the queries. This resolution is just not best since legitimate SQL can finish with a quote, however let’s strive it.
  • Our technical author isn’t basing its output on the enter from the database specialist. We have to tweak the immediate to spotlight the significance of offering solely factual data.

So, let’s attempt to repair these issues. First, we’ll repair the device — we are able to leverage strip to remove quotes.

CH_HOST = 'http://localhost:8123' # default deal with 

def get_clickhouse_data(question, host = CH_HOST, connection_timeout = 1500):
r = requests.put up(host, params = {'question': question.strip('"').strip("'")},
timeout = connection_timeout)
if r.status_code == 200:
return r.textual content
else:
return 'Database returned the next error:n' + r.textual content

Then, it’s time to replace the immediate. I’ve included statements emphasizing the significance of sticking to the details in each the agent and process definitions.


tech_writer_agent = Agent(
position = "Technical author",
objective = '''Write partaking and factually correct technical documentation
for knowledge sources or instruments''',
backstory = '''
You might be an professional in each know-how and communications, so that you
can simply clarify even subtle ideas.
Your texts are concise and may be simply understood by extensive viewers.
You employ skilled however reasonably casual model in your communication.
You base your work on the factual data supplied by your colleagues.
You stick with the details within the documentation and use ONLY
data supplied by the colleagues not including something.''',
allow_delegation = False,
verbose = True
)

table_documentation_task = Process(
description = '''Utilizing supplied details about the desk,
put collectively the detailed documentation for this desk in order that
individuals can use it in observe''',
expected_output = '''Nicely-written detailed documentation describing
the info scheme for the desk {desk} in markdown format,
that offers the desk overview in 1-2 sentences then then
describes every columm. Construction the columns description
as a markdown desk with column title, kind and outline.
The documentation is predicated ONLY on the knowledge supplied
by the database specialist with none additions.''',
instruments = [],
output_file = "table_documentation.md",
agent = tech_writer_agent
)

Let’s execute our crew as soon as once more and see the outcomes.

We’ve achieved a bit higher consequence. Our database specialist was in a position to execute queries and consider the info, which is a major win for us. Moreover, we are able to see all of the related fields within the consequence desk, although there are many different fields as effectively. So, it’s nonetheless not solely appropriate.

I as soon as once more seemed by the CrewAI execution log to determine what went incorrect. The difficulty lies in getting the record of columns. There’s no filter by database, so it returns some unrelated columns that seem within the consequence.

SELECT column_name 
FROM information_schema.columns
WHERE table_name = 'customers'

Additionally, after a number of makes an attempt, I observed that the database specialist, now and again, executes choose * from <desk> question. It’d trigger some points in manufacturing as it’d generate plenty of knowledge and ship it to LLM.

Extra specialised instruments

We are able to present our agent with extra specialised instruments to enhance our resolution. Presently, the agent has a device to execute any SQL question, which is versatile and highly effective however susceptible to errors. We are able to create extra centered instruments, similar to getting desk construction and top-N rows from the desk. Hopefully, it’ll cut back the variety of errors.

class TableStructure(BaseTool):
title: str = "Desk construction"
description: str = "Returns the record of columns and their varieties"

def _run(self, desk: str) -> str:
desk = desk.strip('"').strip("'")
return get_clickhouse_data(
'describe {desk} format TabSeparatedWithNames'
.format(desk = desk)
)

class TableExamples(BaseTool):
title: str = "Desk examples"
description: str = "Returns the primary N rows from the desk"

def _run(self, desk: str, n: int = 30) -> str:
desk = desk.strip('"').strip("'")
return get_clickhouse_data(
'choose * from {desk} restrict {n} format TabSeparatedWithNames'
.format(desk = desk, n = n)
)

table_structure_tool = TableStructure()
table_examples_tool = TableExamples()

Now, we have to specify these instruments within the process and re-run our script. After the primary try, I bought the next output from the Technical Author.

Process output: This remaining reply offers an in depth and factual description 
of the ecommerce_db.customers desk construction, together with column names, varieties,
and descriptions. The documentation adheres to the supplied data
from the database specialist with none additions or modifications.

Extra centered instruments helped the database specialist retrieve the proper desk data. Nonetheless, though the author had all the mandatory data, we didn’t get the anticipated consequence.

As we all know, LLMs are probabilistic, so I gave it one other strive. And hooray, this time, the consequence was fairly good.

1*yWLsOgw FPo4ys0XYfUQGQ

It’s not good because it nonetheless consists of some irrelevant feedback and lacks the general description of the desk. Nonetheless, offering extra specialised instruments has positively paid off. It additionally helped to forestall points when the agent tried to load all the info from the desk.

High quality assurance specialist

We’ve achieved fairly good outcomes, however let’s see if we are able to enhance them additional. A typical observe in multi-agent setups is high quality assurance, which provides the ultimate evaluate stage earlier than finalising the outcomes.

1*aYHqvVqM9DEsEMGmgT3UYw

Let’s create a brand new agent — a High quality Assurance Specialist, who might be in control of evaluate.

qa_specialist_agent = Agent(
position = "High quality Assurance specialist",
objective = """Guarantee the very best high quality of the documentation we offer
(that it's appropriate and simple to grasp)""",
backstory = '''
You're employed as a High quality Assurance specialist, checking the work
from the technical author and guaranteeing that it's inline
with our highest requirements.
It is advisable test that the technical author offers the complete full
solutions and make no assumptions.
Additionally, you have to be sure that the documentation addresses
all of the questions and is straightforward to grasp.
''',
allow_delegation = False,
verbose = True
)

Now, it’s time to explain the evaluate process. I’ve used the context parameter to specify that this process requires outputs from each table_description_task and table_documentation_task.

qa_review_task = Process(
description = '''
Evaluate the draft documentation supplied by the technical author.
Make sure that the documentation absolutely solutions all of the questions:
the aim of the desk and its construction within the type of desk.
Make it possible for the documentation is according to the knowledge
supplied by the database specialist.
Double test that there are not any irrelevant feedback within the remaining model
of documentation.
''',
expected_output = '''
The ultimate model of the documentation in markdown format
that may be printed.
The documentation ought to absolutely deal with all of the questions, be constant
and observe our skilled however casual tone of voice.
''',
instruments = [],
context = [table_description_task, table_documentation_task],
output_file="checked_table_documentation.md",
agent = qa_specialist_agent
)

Let’s replace our crew and run it.

full_crew = Crew(
brokers=[database_specialist_agent, tech_writer_agent, qa_specialist_agent],
duties=[table_description_task, table_documentation_task, qa_review_task],
verbose = 2,
reminiscence = False # don't work with Llama
)

full_result = full_crew.kickoff({'desk': 'ecommerce_db.customers'})

We now have extra structured and detailed documentation because of the addition of the QA stage.

1*U7Yt4hWfUD4y4BqKt YhEA

Delegation

With the addition of the QA specialist, it will be attention-grabbing to check the delegation mechanism. The QA specialist agent might need questions or requests that it might delegate to different brokers.

I attempted utilizing the delegation with Llama 3, however it didn’t go effectively. Llama 3 struggled to name the co-worker device appropriately. It couldn’t specify the proper co-worker’s title.

We achieved fairly good outcomes with an area mannequin that may run on any laptop computer, however now it’s time to change gears and use a far more highly effective mannequin — GPT-4o.

To do it, we simply have to replace the next atmosphere variables.

os.environ["OPENAI_MODEL_NAME"] = 'gpt-4o'  
os.environ["OPENAI_API_KEY"] = config['OPENAI_API_KEY'] # your OpenAI key

To modify on the delegation, we must always specify allow_delegation = True for the QA specialist agent.

Additionally, we are able to use helpful reminiscence performance for OpenAI fashions, as talked about above. The reminiscence will enable our brokers to share their information with one another throughout execution and leverage long-term reminiscence to get data from earlier executions.

full_crew = Crew(
brokers = [database_specialist_agent, tech_writer_agent, qa_specialist_agent],
duties = [table_description_task, table_documentation_task, qa_review_task],
verbose = 2,
reminiscence = True
)

full_result = full_crew.kickoff({'desk': 'ecommerce_db.customers'})

Let’s see the CrewAI execution logs to grasp how delegation works. Listed below are all of the logs for the QA specialist. We are able to see that it reached out to the database specialist to double-check the knowledge.

Thought: I have to evaluate the supplied draft documentation to make sure it 
absolutely solutions all of the questions and is full, appropriate, and simple
to grasp. Particularly, I want to make sure that it explains the aim
of the desk, its construction, and consists of related examples with out
any irrelevant feedback.

First, I'll evaluate the 2 drafts to determine any discrepancies and
lacking data. Then, I'll confirm the accuracy and completeness of
the knowledge supplied.

**Comparability:**
1. **Introduction:**
- First draft: Consists of an introduction and examples.
- Second draft: Provides a extra detailed introduction in regards to the goal and significance of the desk.

2. **Columns:**
- Each drafts have the identical column construction and descriptions.

3. **Extra Info:**
- Each drafts embrace related extra data, however the second draft is extra concise.

4. **Examples:**
- The primary draft consists of examples, that are lacking within the second draft.

5. **Closing Checks:**
- Guarantee there are not any irrelevant feedback.
- Confirm consistency with data supplied by the database specialist.

Motion: Ask query to co-worker
Motion Enter: 1000001

<-- omitted logs from the database specialist -->

Based mostly on the supplied context and examples for the `ecommerce_db.customers` desk,
the column descriptions and examples seem correct. Listed below are the main points:
- `user_id`: This column accommodates a novel identifier for every person.
The supplied examples (e.g., 1000001, 1000002) align with this description
as they're distinctive numeric identifiers.

- `nation`: This column specifies the nation of the person. The supplied
examples (e.g., United Kingdom, France, Germany, Netherlands) are
all legitimate nation names.

- `is_active`: This column signifies whether or not the person is lively (1) or not (0). The supplied examples (e.g., 0, 1) appropriately signify this binary standing.

- `age`: This column exhibits the age of the person. The supplied examples (e.g., 70,
87, 88, 25, 48, 78, 65, 31, 66, 73) are all legitimate ages.

Subsequently, the column descriptions and examples supplied for the
`ecommerce_db.customers` desk are correct and meet the anticipated standards.

Thought: Based mostly on the remark from the database specialist,
the column descriptions and examples supplied for the `ecommerce_db.customers`
desk are correct. Now, I'll consolidate the most effective parts from each drafts,
guaranteeing the documentation is full, clear, and freed from irrelevant feedback.

Motion: Finalize the documentation in markdown format, incorporating
the detailed introduction, examples, and guaranteeing the tone is skilled
however casual.

Once I tried the delegation for the primary time, I didn’t allow reminiscence, which led to incorrect outcomes. The info specialist and the technical author initially returned the proper data. Nonetheless, when the QA specialist returned with the follow-up questions, they began to hallucinate. So, it appears to be like like delegation works higher when reminiscence is enabled.

Right here’s the ultimate output from GPT-4o. The consequence appears to be like fairly good now. We positively can use LLMs to automate documentation.

1* 567hW4cSLS Me6iKfRQWw

So, the primary process has been solved!

I used the identical script to generate documentation for the ecommerce_db.classes desk as effectively. It will likely be helpful for our subsequent process. So, let’s not waste any time and transfer on.

Use case: answering questions

Our subsequent process is answering questions primarily based on the documentation because it’s widespread for a lot of knowledge analysts (and different specialists).

We’ll begin easy and can create simply two brokers:

  • The documentation help specialist might be answering questions primarily based on the docs,
  • The help QA agent will evaluate the reply earlier than sharing it with the client.
1* KEcNJLuLb4N 5POD9Wz4Q

We might want to empower the documentation specialist with a few instruments that can enable them to see all of the information saved within the listing and browse the information. It’s fairly easy since CrewAI has carried out such instruments.

from crewai_tools import DirectoryReadTool, FileReadTool

documentation_directory_tool = DirectoryReadTool(
listing = '~/crewai_project/ecommerce_documentation')

base_file_read_tool = FileReadTool()

Nonetheless, since Llama 3 retains scuffling with quotes when calling instruments, I needed to create a customized device on prime of the FileReaderTool to beat this subject.

from crewai_tools import BaseTool

class FileReadToolUPD(BaseTool):
title: str = "Learn a file's content material"
description: str = "A device that can be utilized to learn a file's content material."

def _run(self, file_path: str) -> str:
# Implementation goes right here
return base_file_read_tool._run(file_path = file_path.strip('"').strip("'"))

file_read_tool = FileReadToolUPD()

Subsequent, as we did earlier than, we have to create brokers, duties and crew.

data_support_agent = Agent(
position = "Senior Knowledge Assist Agent",
objective = "Be essentially the most useful help for you colleagues",
backstory = '''You're employed as a help for data-related questions
within the firm.
Though you're an enormous professional in our knowledge warehouse, you double test
all of the details in documentation.
Our documentation is totally up-to-date, so you'll be able to absolutely depend on it
when answering questions (you don't have to test the precise knowledge
in database).
Your work is essential for the workforce success. Nonetheless, bear in mind
that examples of desk rows don't present all of the doable values.
It is advisable make sure that you present the very best help: answering
all of the questions, making no assumptions and sharing solely the factual knowledge.
Be artistic strive your finest to resolve the client drawback.
''',
allow_delegation = False,
verbose = True
)

qa_support_agent = Agent(
position = "Assist High quality Assurance Agent",
objective = """Guarantee the very best high quality of the solutions we offer
to the shoppers""",
backstory = '''You're employed as a High quality Assurance specialist, checking the work
from help brokers and guaranteeing that it's inline with our highest requirements.
It is advisable test that the agent offers the complete full solutions
and make no assumptions.
Additionally, you have to be sure that the documentation addresses all
the questions and is straightforward to grasp.
''',
allow_delegation = False,
verbose = True
)

draft_data_answer = Process(
description = '''Crucial buyer {buyer} reached out to you
with the next query:
```
{query}
```

Your process is to offer the most effective reply to all of the factors within the query
utilizing all obtainable data and never making any assumprions.
Should you don't have sufficient data to reply the query, simply say
that you simply don't know.''',
expected_output = '''The detailed informative reply to the client's
query that addresses all the purpose talked about.
Make it possible for reply is full and stict to details
(with none extra data not primarily based on the factual knowledge)''',
instruments = [documentation_directory_tool, file_read_tool],
agent = data_support_agent
)

answer_review = Process(
description = '''
Evaluate the draft reply supplied by the help agent.
Make sure that the it absolutely solutions all of the questions talked about
within the preliminary inquiry.
Make it possible for the reply is constant and doesn't embrace any assumptions.
''',
expected_output = '''
The ultimate model of the reply in markdown format that may be shared
with the client.
The reply ought to absolutely deal with all of the questions, be constant
and observe our skilled however casual tone of voice.
We're very chill and pleasant firm, so don't neglect to incorporate
all of the well mannered phrases.
''',
instruments = [],
agent = qa_support_agent
)

qna_crew = Crew(
brokers = [data_support_agent, qa_support_agent],
duties = [draft_data_answer, answer_review],
verbose = 2,
reminiscence = False # don't work with Llama
)

Let’s see the way it works in observe.

consequence = qna_crew.kickoff(
{'buyer': "Max",
'query': """Hey workforce, I hope you're doing effectively. I want to search out
the numbers earlier than our CEO presentation tomorrow, so I'll actually
recognize your assist.
I have to calculate the variety of classes from our Home windows customers in 2023. I've tried to search out the desk with such knowledge in our knowledge warehouse, however wasn't in a position to.
Do you will have any concepts whether or not we retailer the wanted knowledge someplace,
in order that I can question it? """
}
)

We’ve bought a well mannered, sensible and useful reply in return. That’s actually nice.

**Good day Max,**

Thanks for reaching out along with your query! I'm blissful that can assist you
discover the variety of classes from Home windows customers in 2023.
After reviewing our documentation, I discovered that we do retailer knowledge
associated to classes and customers in our ecommerce database, particularly in
the `ecommerce_db.classes` desk.

To reply your query, I can offer you a step-by-step information
on learn how to question this desk utilizing SQL. First, you should use the `session_id`
column together with the `os` column filtering for "Home windows" and
the `action_date` column filtering for dates in 2023.
Then, you'll be able to group the outcomes by `os` utilizing the `GROUP BY` clause
to depend the variety of classes that meet these situations.

Right here's a pattern SQL question that ought to provide the desired output:

```sql
SELECT COUNT(*)
FROM ecommerce_db.classes
WHERE os = 'Home windows'
AND action_date BETWEEN '2023-01-01' AND '2023-12-31'
GROUP BY os;
```

This question will return the entire variety of classes from Home windows
customers in 2023. I hope this helps! When you have any additional questions or
want extra help, please don't hesitate to ask.

Let’s complicate the duty a bit. Suppose we are able to get not solely questions on our knowledge but additionally about our device (ClickHouse). So, we can have one other agent within the crew — ClickHouse Guru. To present our CH agent some information, I’ll share a documentation web site with it.

from crewai_tools import ScrapeWebsiteTool, WebsiteSearchTool
ch_documenation_tool = ScrapeWebsiteTool(
'https://clickhouse.com/docs/en/guides/creating-tables')

If you have to work with a prolonged doc, you may strive utilizing RAG (Retrieval Augmented technology) — WebsiteSearchTool. It’ll calculate embeddings and retailer them domestically in ChromaDB. In our case, we’ll stick with a easy web site scraper device.

Now that we have now two subject material consultants, we have to determine who might be engaged on the questions. So, it’s time to make use of a hierarchical course of and add a supervisor to orchestrate all of the duties.

1*5O56Xqw Cb87wZ nKGg0iA

CrewAI offers the supervisor implementation, so we solely have to specify the LLM mannequin. I’ve picked the GPT-4o.

from langchain_openai import ChatOpenAI
from crewai import Course of

complext_qna_crew = Crew(
brokers = [ch_support_agent, data_support_agent, qa_support_agent],
duties = [draft_ch_answer, draft_data_answer, answer_review],
verbose = 2,
manager_llm = ChatOpenAI(mannequin='gpt-4o', temperature=0),
course of = Course of.hierarchical,
reminiscence = False
)

At this level, I needed to change from Llama 3 to OpenAI fashions once more to run a hierarchical course of because it hasn’t labored for me with Llama (just like this subject).

Now, we are able to strive our new crew with several types of questions (both associated to our knowledge or ClickHouse database).

ch_result = complext_qna_crew.kickoff(
{'buyer': "Maria",
'query': """Good morning, workforce. I'm utilizing ClickHouse to calculate
the variety of prospects.
Might you please remind whether or not there's an possibility so as to add totals
in ClickHouse?"""
}
)

doc_result = complext_qna_crew.kickoff(
{'buyer': "Max",
'query': """Hey workforce, I hope you're doing effectively. I want to search out
the numbers earlier than our CEO presentation tomorrow, so I'll actually
recognize your assist.
I have to calculate the variety of classes from our Home windows customers
in 2023. I've tried to search out the desk with such knowledge
in our knowledge warehouse, however wasn't in a position to.
Do you will have any concepts whether or not we retailer the wanted knowledge someplace,
in order that I can question it. """
}
)

If we have a look at the ultimate solutions and logs (I’ve omitted them right here since they’re fairly prolonged, but you’ll find them and full logs on GitHub), we’ll see that the supervisor was in a position to orchestrate appropriately and delegate duties to co-workers with related information to handle the client's query. For the primary (ClickHouse-related) query, we bought an in depth reply with examples and doable implications of utilizing WITH TOTALS performance. For the data-related query, fashions returned roughly the identical data as we’ve seen above.

So, we’ve constructed a crew that may reply varied kinds of questions primarily based on the documentation, whether or not from an area file or an internet site. I believe it’s a wonderful consequence.

Yow will discover all of the code on GitHub.

Abstract

On this article, we’ve explored utilizing the CrewAI multi-agent framework to create an answer for writing documentation primarily based on tables and answering associated questions.

Given the intensive performance we’ve utilised, it’s time to summarise the strengths and weaknesses of this framework.

General, I discover CrewAI to be an extremely helpful framework for multi-agent programs:

  • It’s easy, and you’ll construct your first prototype shortly.
  • Its flexibility permits to resolve fairly subtle enterprise issues.
  • It encourages good practices like role-playing.
  • It offers many helpful instruments out of the field, similar to RAG and an internet site parser.
  • The help of several types of reminiscence enhances the brokers’ collaboration.
  • Constructed-in guardrails assist forestall brokers from getting caught in repetitive loops.

Nonetheless, there are areas that may very well be improved:

  • Whereas the framework is straightforward and simple to make use of, it’s not very customisable. As an illustration, you presently can’t create your individual LLM supervisor to orchestrate the processes.
  • Typically, it’s fairly difficult to get the complete detailed data from the documentation. For instance, it’s clear that CrewAI carried out some guardrails to forestall repetitive perform calls, however the documentation doesn’t absolutely clarify the way it works.
  • One other enchancment space is transparency. I like to grasp how frameworks work below the hood. For instance, in Langchain, you should use langchain.debug = True to see all of the LLM calls. Nonetheless, I haven’t discovered learn how to get the identical stage of element with CrewAI.
  • The complete help for the native fashions can be a fantastic addition, as the present implementation both lacks some options or is troublesome to get working correctly.

The area and instruments for LLMs are evolving quickly, so I’m hopeful that we’ll see plenty of progress within the close to future.

Thank you numerous for studying this text. I hope this text was insightful for you. When you have any follow-up questions or feedback, please depart them within the feedback part.

Reference

This text is impressed by the “Multi AI Agent Methods with CrewAI” quick course from DeepLearning.AI.

stat?event=post


Multi AI Agent Methods 101 was initially printed in In the direction of Knowledge Science on Medium, the place persons are persevering with the dialog by highlighting and responding to this story.



Supply hyperlink

LEAVE A REPLY

Please enter your comment!
Please enter your name here