Powerful Collaboration of AI Agents with CrewAI

A hands-on marketing use case

Toon Beerten
Towards Data Science

--

Image by author (with)

In this article, I’ll show you how to orchestrate AI puppets to successfully tackle a real-life marketing challenge where the agents work together to:

  • analyze customer data profiles
  • select the ideal products for targeted marketing
  • create compelling promotional text for those products.

We will use a new framework called CrewAI, enabling autonomous AI agents to collaborate and achieve common goals. Everything is documented in the provided Colab notebook, so you can replicate and adapt it to your own use case.

What is CrewAI?

CrewAI is a new framework that facilitates the collaboration of AI agents. The agents can adopt specific roles, share common objectives, and — as a whole — function as an efficient team. It is open source and built upon Langchain. Some alternatives in the same field are Microsofts AutoGen and ChatDev.

Image taken from official documentation

The main concepts of CrewAI revolve around three core entities: Agents, Tasks, and Crews.

  1. Agents: These are standalone units programmed to perform tasks , make decisions and communicate with other agents. They can use Tools which can be simple search functions or complex integrations involving other chains, APIs, etc.
  2. Tasks: Tasks are assignments or jobs that an AI agent needs to complete. They can include additional information like which agent should do it and what tools they might need.
  3. A Crew is a team of agents, each with a specific role, that work together to achieve a common goal. The process of forming a crew involves assembling agents, defining their tasks and establishing a sequence of task execution.

To test out what it’s capable of, I devised the following scenario…

Marketing Challenge

Imagine you are the head of a local retailer. Next week you’ll run a promotion campaign on 12 products. Which products will you target to which customers? As you can imagine, it makes very little sense to promote lipstick to male clients. Can this be optimized with AI?

Through use of loyalty cards and datamining, you have access to a database with the customer personality analysis. This helps to understand the characteristics, preferences and behaviors of customers. With a well designed prompt (see notebook) I got this dataset of customers in seconds:

birth_year,sex,marital_status,yearly_household_income_percentile,number_of_toddlers,number_of_teens,highest_education,monthly_spend_on_wine,monthly_spend_on_vegetables,monthly_spend_on_toys,last_month_coupon_use
1975,F,married,65,1,0,masters,30,65,20,high
1992,M,single,30,0,0,bachelors,15,40,0,none
1980,F,married,80,0,2,bachelors,50,80,85,low
1968,M,divorced,45,0,0,high_school,45,50,0,high
1990,F,single,25,0,0,associates,10,35,0,low
1985,M,married,90,2,1,phd,80,100,120,high
2000,F,single,40,0,0,bachelors,25,55,0,none
1972,M,married,70,0,1,masters,60,70,40,low
1988,F,married,55,1,0,associates,20,40,30,high
1970,M,single,60,0,0,high_school,35,60,0,none

For the possible campaign products I came up with these:

Fresh Lettuce
Diapers
Irish whiskey
laundry detergent
Chips
Spaghetti cans (ready to eat)
Minecraft Video Game
Mascara
Toilet Paper (best value)
Wagyu beef steak
Organic avocados
Cigarettes

These were especially chosen because I expect to see correlations. For example between the promotion of diapers to the number of toddlers and wagyu steak to the household income. Can the agents work together to discover this logic by using common sense?

Implementation

My first crew will target the right products by selecting the 3 most suited for each specific customer. In this crew, I created three agents:

  • Chief Promotion Director: gets the main task, oversees work of others
  • Customer Profiler: to create an understanding of the customer
  • Product Specialist: expertly links products to customers

Each needs a role, goal and backstory. We can use natural language for this. In my notebook it became this:

profiler = Agent(
role='profiler',
goal='''From limited data, you logically deduct conclusions about people.''',
backstory='You are an expert psychologist with decades of experience.',
llm=llm,
verbose=True,
allow_delegation=True
)
product_specialist = Agent(
role='product specialist',
goal='''Match the product to the customer''',
backstory='You have exceptional knowledge of the products and can say
how valuable they are to a customer.',
llm=llm,
verbose=True,
allow_delegation=True
)

Chief_Promotional_Director = Agent(
role="Chief Promotion Director",
goal='''
Oversee the work done by your team to make sure it's the best possible
and aligned with the product's goals, review, approve, ask clarifying
question or delegate follow up work if necessary to make decisions''',
backstory='''
You're the Chief Promotion Officer of a large retailer. You're
launching a personalized ad campaign, trying to make sure your team
is crafting the best possible content for the customer.''',
tools=[],
llm=llm,
verbose=True
)

The following task is given to agent Chief Promotion Director, who delegates work to the other agents:

select_3_products_task = f'''You're creating a targeted marketing campaign
tailored to what we know about our customers.
For each customer, we have to choose exactly three products to promote
in the next campaign. Make sure the selection is the best possible and
aligned with the customer. Review, approve, ask clarifying question or
delegate follow up work if necessary to make decisions. When delegating
work send the full draft as part of the information.
This is the list of all the products participating in the campaign: {products}.
This is all we know so far from the customer: {customer_description}.
To start this campaign we will need to build first an understanding of our
customer. Once we have a profile about the customers interests, lifestyle and
means and needs, we have to select exactly three products that have the
highest chance to be bought by them.
Your final answer MUST be exactly 3 products from the list, each with a short
description why it matches with this customer. '''

All agents interact with each other until agent Chief Promotion Director is happy and ends the task. To better understand what happens when this crew is kicked off, I created this schematic:

Image by author

The second crew will write a short promotional text alongside the product. It should also be in line with what can be deducted from the customers data. For this I create an additional Creative Content Creator agent:

creative_content_creator_agent = Agent(
role="Creative Content Creator",
goal=dedent("""\
Develop compelling and innovative content
for ad campaigns, with a focus customer specific ad copies."""),
backstory=dedent("""\
As a Creative Content Creator at a top-tier digital marketing
agency, you excel in crafting advertisements that resonate with
potential customers. Your expertise lies in turning marketing
strategies into engaging stories that capture attention and
inspire buying action."""),
llm=llm,
verbose=True
)

This agent will work together with the previous ‘boss’ agent in a new crew with the following task:

get_ad_campaign_written_task = f'''
You're creating a targeted marketing campaign tailored to what we know
about our customers.
For each customer, we have chosen three products to promote in the next
campaign. This selection is tailored specifically to the customer: {selection}
To end this campaign succesfully we will need a promotional message
advertising these products to the customer with the ultimate intent that
they buy from us. This message should be around 3 paragraphs, so that it can
be easily integrated into the full letter. For example:
Tired of making dinner, get our best ready made canned tuna.
Your lifestyle deserves a taste of this fresh lobster.
In the weekends, go on a day trip with the kids with these new lunch box containers.

You need to review, approve, and delegate follow up work if necessary to
have the complete promotional message. When delegating work send the full
draft as part of the information.
Your final answer MUST include the 3 products from the list,
each with a short promotional message.'''

Each agent is linked to Large Language Model that it will use. I linked them to the Mistral API. I first integrated their Mistral 7B Instruct v0.2 model, but it reasoned very strangely, such as this explanation for selecting diapers: “Although the customer is currently single, they might have children in the future. Diapers are a practical investment for potential parenthood, as they are essential for the care of infants and toddlers.
That was solved by upgrading to a bigger model: Mixtral 8x7B. I haven’t seen any such logic flaws again.

Results

With my two crews set up, it’s time to let them loose on the 10 customers. By setting the verbose mode to 2, I am able to see behind the screen and follow the agents thoughts and interactions. E.g.:

Thought: Do I need to use a tool? Yes
Action: Ask question to co-worker
Action Input: profiler|Can you provide a customer profile based on the given demographic and behavioral data?|The customer was born in 1968. Its sex is M. Its marital status is divorced. Its yearly household income percentile is 45. The customer has 0 toddlers. The customer has 0 teenage kids. The customer highest education is high_school. The customer spends on average 45 $ on wine. The customer spends on average 50 $ on vegetables. The customer spends on average 0 $ on toys. The customer's coupon use is high.

Observation: The profiler responds with: Our customer is a 54-year-old divorced man with a mid-level income. He has no children living with him. He has a high school education. He enjoys wine and vegetables, and does not spend on toys. He is likely to be budget-conscious and looking for deals, given his high coupon use.

Thought: Do I need to use a tool? Yes
Action: Ask question to co-worker
Action Input: product specialist|Which three products would you recommend for this customer, based on his profile?|54-year-old divorced man with a mid-level income, high school education, enjoys wine and vegetables, budget-conscious, and uses coupons.

Observation: The product specialist responds with: Based on the customer's profile, I would recommend:
1. Laundry detergent: A necessary household item, and we can offer a deal or discount to appeal to his budget-conscious nature.
2. Fresh lettuce and vegetables: Given his interest in vegetables, promoting fresh produce is a good fit.
3. Wine or Irish whiskey: Since he spends a significant amount on wine, promoting a higher-end option like Irish whiskey could be appealing, especially if we offer a discount.

Thought: Do I need to use a tool? No
Final Answer:
1. Laundry detergent : A necessary household item, and we can offer a deal or discount to appeal to his budget-conscious nature.
2. Fresh lettuce and vegetables : Given his interest in vegetables, promoting fresh produce is a good fit.
3. Irish whiskey : Since he spends a significant amount on wine, promoting a higher-end option like Irish whiskey could be appealing, especially if we offer a discount.

Observation: I have provided the three recommended products for the customer based on their profile.

Now, let’s dissect two customers in detail. The first customer was:

1990,F,single,25,0,0,associates,10,35,0,low

Giving that to the profiler agent, its response is: “Our customer is a single woman in her late 20s or early 30s with a moderate income. She enjoys wine and vegetables, likely cooking for herself. She doesn’t have children and has a higher education background, suggesting she might be more health-conscious and have a busy lifestyle. She doesn’t use coupons often, indicating she might prefer convenience over savings.

The product specialist agent responds with: “Based on the customer profile, I recommend the following products:

  • Fresh Lettuce: Given her interest in vegetables, she might enjoy fresh lettuce as a healthy addition to her meals.
  • Mascara: As a single woman, she might appreciate beauty products like mascara, fitting with her higher education and busy lifestyle.
  • Spaghetti cans (ready to eat): Due to her busy lifestyle, she might not always have time to cook, making ready-to-eat spaghetti a convenient option.

These are very reasonable selections! Let’s see now what the other crew came up with as promotional messages. I pasted them in this mock up. The product messages are very spot on:

Image by author

Let’s have a look at how they managed with the 2nd customer:

1985,M,married,90,2,1,phd,80,100,120,high

The profile built was very basic, but complete: “Customer was born in 1985, male, married, 90th percentile yearly household income, has 2 toddlers and 1 teenage kid, highest education is PhD, spends 80 $ on wine, 100 $ on vegetables, 120 $ on toys, high coupon use.

Here are the products selected and promoted:

Image by author

The product selection and promo texts match very well with what is known of the customer. They picked Wagyu because of the high income, spaghetti because he’s a busy parent with 3 children and a game because he has teen kids.

Zooming out, here are my observations from the overall crew output:

✔️ diapers were only proposed to customers with toddlers
✔️ mascara was only proposed to female customers
✔️ wagyu was only proposed to highest income customers
✔️ laundry detergent was 4 times proposed. Two times to women, but also twice to not-married (!) men
✔️ cigarettes were never promoted (pats Mixtral — good AI)
✔️ all the accompanying texts were tailored to the customer without being creepily specific or containing hallucinations
✔️ I never saw product suggestions that were out of place

That’s exactly what a marketer would be hoping for! The crew seems to be aware of some common-sense knowledge and can reason around it.

Ethicality

When I said common sense, please be aware that it is not a universal common sense. Each large language model has inherently a certain bias, based on the training data and training optimization (eg. RLHF).
For example: I found it striking that in my example the model proposed laundry detergent only to men when they are not married. Was this just by chance or is it supposing that married men in a household don’t take care of laundry? Maybe because the model has only seen couples where only women do the laundry? This might statistically be correct, but the fact is that we don’t know which other biases there are. We should be wary about putting these frameworks in production without scrutiny, as they may propagate stereotypes around gender, politics, religion, ethnicity, etc. It is still a field of ongoing research to mitigate these risks. For further reading I can suggest: OpenAI’s blogpost, 🤗 Evaluate library and practical tips.

Conclusion

My results demonstrate that the AI agents can reason to pick the best products based on customer profiles and provide spot-on promotional messages tailored to each customer, taking into account factors such as age, gender, marital status, and income.

We saw autonomous AI agents collaborate, delegate or be assigned a sub task, call for help and review each others work. When they do that, their output quality is significantly amplified, resulting in more reliable and common-sense decisions. Frameworks like CrewAI offer a straightforward, yet effective way to harness this power by allowing natural language instructions. I also touched on the subject of the common knowledge ingrained in LLMs and that there will be bias and one should strive for fairness.

This was just a simple example focused on marketing. There are so many other use cases: parsing data, an automatic social media poster, a markdown validator or a city trip planner … The possibilities are endless. Maybe it will help you with your next challenge?

--

--