ai agents are highly intelligent software programs that can work independently to help with various tasks. These agents are not only experts at performing tasks independently; They are also great for analyzing data, making predictions, and recommending the best actions to take. This can be a huge advantage in many industries, including sales and marketing.
For example, in marketing, ai agents can analyze customer preferences, understand what individual customers are interested in, and then help create personalized marketing campaigns just for them. This type of personalization makes customers feel more valued and engaged, which often leads to greater satisfaction and better business results.
An interesting framework for organizing and using ai agents is CrewAI, built on a platform called Langchain. In this system, different agents are assigned different tasks, but they all work together as a unified team towards a common goal. Each agent has their own specialized role, but they collaborate seamlessly to get the job done efficiently and effectively.
In this article, we will explore how these ai agents, using the CrewAI framework, can be applied to recommend personalized courses for students at an edtech company. By leveraging the capabilities of ai agents, edtech platforms can offer students course recommendations that fit their learning needs and interests, enhancing their educational experience.
Learning objectives
- Understand the components of ai Crew.
- Generation of recommendation campaigns for students using agents.
- Analysis of Generated Campaigns.
This article was published as part of the Data Science Blogathon.
CrewAI Components: Agents, Tasks, and Crew
Agents
Agents are independent, self-directed units designed to execute specific tasks, make decisions, and dynamically interact with other agents in a system. Each agent operates autonomously, meaning it can analyze its environment, respond to inputs, and make decisions based on its programming and goals. What makes agents particularly powerful is their ability to use a wide range of tools. These tools range from simple search functions for data retrieval to advanced integrations with other brokers, APIs, and even entire blockchain networks. This capability allows agents to perform complex tasks that involve real-time data collection, decision making, and collaboration between multiple agents.
Tasks
Tasks refer to the specific assignments or duties that an ai agent is responsible for executing. These tasks can vary widely, from data analysis to decision making or even direct actions such as controlling external systems or triggering processes on integrated platforms. Each task is usually divided into subtasks, which may require specialized tools or access to certain resources. Additionally, tasks can be designed to include specific details, such as identifying which agent is responsible for a particular task and describing the necessary tools, processes, and execution paths. Task management in an agent-based system ensures efficient workflows and accurate results.
Crew
A crew is a collaborative group of agents working together to achieve a shared goal. Instead of functioning independently, crew agents are organized based on their unique capabilities and roles, ensuring they can collectively address more complex and multifaceted problems. The process of forming a crew involves not only assembling the right set of agents but also defining their respective roles, assigning tasks, and coordinating their efforts. This organization is essential to ensure that tasks are completed in the correct sequence, especially when there are dependencies between the actions of different agents. A well-organized team can significantly improve performance by leveraging each agent's specialized skills while ensuring synchronized execution.
Also Read: Top 4 Agent ai Design Patterns for Designing ai Systems
Problem Description: Generating Referral Campaigns for Students Using Agents
Imagine you run an educational consulting company and want to suggest the best courses for your students based on their degrees, academic goals, hobbies, and computer skills. How do you decide which courses to recommend to each student? For example, it would be more logical to suggest a course on climate change to a student in Environmental Sciences than one in Computer Science.
The student profile data set looks like this:
The possible list of courses that should be used to recommend to students could be the following:
Our first team consists of the following ai agents, with the help of which we will first generate the top 3 courses suitable for each of the students in our data set:
- Chief Recommendations Director: ai agent that performs the main task and supervises the work of others.
- Student profiles: ai agent to understand the profiles of different students
- Course Specialist: ai agent that can help match appropriate courses for each student
Python implementation
Install and import necessary libraries
Let's first start by installing the necessary Python libraries:
!pip install crewai
!pip install langchain_openai
Next, we import all the necessary Python libraries:
import os
import re
import json
import pandas as pd
from tqdm import tqdm
from crewai import Agent, Task, Crew, Process
from langchain_openai import ChatOpenAI
from textwrap import dedent
Define the LLM model with the API key
We then define the LLM model to be used and the respective API key. We can consult with any LLM model of our choice. Here we have used gpt-3.5-turbo-16k.
import os
openai_api_key = ''
os.environ('OPENAI_API_KEY') = openai_api_key
llm = ChatOpenAI(
model="gpt-3.5-turbo-16k",
temperature=0.1,
max_tokens=8000
)
Define data sets to be used
Next, we define the csv of the student profile data and also the list of products/courses to recommend to students:
csv = '''Academic Goals, Major, Hobbies, Computer Skills, Interest in Languages, GPA
To become a software engineer, Computer Science, Gaming, Advanced, Spanish, 3.7
To study environmental science, Environmental Science, Hiking, Intermediate, French, 3.5
To pursue a career in medicine, Pre-Med, Playing the piano, Advanced, Spanish, 3.9
To major in psychology, Psychology, Reading, Intermediate, German, 3.6
To work in international relations, Political Science, Traveling, Basic, Mandarin, 3.8
To become a teacher, Education, Painting, Advanced, Spanish, 3.4
To study literature, English Literature, Writing, Intermediate, French, 3.9
To pursue a career in business, Business Administration, Playing soccer, Basic, Mandarin, 3.5
To become a biologist, Biology, Photography, Advanced, German, 3.7
To work in data analysis, Statistics, Cooking, Intermediate, Japanese, 3.6
'''
#creating the df_customers dataframe
from io import StringIO
# Convert String into StringIO
csvStringIO = StringIO(csv)
df_customers = pd.read_csv(csvStringIO, sep=",")
products=""'
"Introduction to Computer Science" - Offered by Harvard University on edX
"Biology: Life on Earth" - Offered by Coursera
"Introduction to Psychology" - Offered by Yale University on Coursera
"Environmental Science" - Offered by University of Leeds on FutureLearn
"Introduction to Literature" - Offered by MIT on edX
"Medical Terminology" - Offered by University of Pittsburgh on Coursera
"Data Science and Machine Learning" - Offered by Stanford University on Coursera
"Cell Biology" - Offered by Massachusetts Institute of technology on edX
"Positive Psychology" - Offered by University of North Carolina at Chapel Hill on Coursera
"Environmental Law and Policy" - Offered by Vermont Law School on Coursera
"Programming for Everybody (Getting Started with Python)" - Offered by University of Michigan on Coursera
"Anatomy: Human Neuroanatomy" - Offered by University of Michigan on Coursera
"Introduction to Cognitive Psychology" - Offered by Duke University on Coursera
"Climate Change and Health: From Science to Action" - Offered by Harvard University on edX
"English for Science, technology, Engineering, and Mathematics" - Offered by University of Pennsylvania on Coursera
"An Introduction to American Law" - Offered by University of Pennsylvania on Coursera
"Introduction to Chemistry: Reactions and Ratios" - Offered by Duke University on Coursera
"Epidemiology: The Basic Science of Public Health" - Offered by University of North Carolina at Chapel Hill on Coursera
"Computer Science: Programming with a Purpose" - Offered by Princeton University on Coursera
"Introduction to Statistics and Data Analysis" - Offered by Rice University on Coursera
"Genes and the Human Condition (From Behavior to Biotechnology)" - Offered by University of Maryland on Coursera
"Ethics, technology, and the Future of Medicine" - Offered by Georgetown University on edX
"Fundamentals of Immunology" - Offered by Harvard University
'''
Define the agents to be used
After this, we started defining the different ai agents for our first team. Each ai agent needs a role, a goal, and a backstory, as shown in the code block below:
student_profiler = Agent(
role="student_profiler",
goal=""'From limited data, you logically deduct conclusions about students.''',
backstory='You are an expert psychologist with decades of experience.',
llm = llm,allow_delegation=False,verbose=True)
course_specialist = Agent(
role="course specialist",
goal=""'Match the suitable course to the students''',
backstory='You have exceptional knowledge of the courses and can say how valuable they are to a student.',
llm = llm,allow_delegation=False,verbose=True)
Chief_Recommendation_Director = Agent(
role="Chief Recomeendation Director",
goal=dedent("""\Oversee the work done by your team to make sure it's the best
possible and aligned with the course's goals, review, approve,
ask clarifying question or delegate follow up work if necessary to make
decisions"""),
backstory=dedent("""\You're the Chief Promotion Officer of a large edtech company. You're launching a personalized ad campaign,
trying to make sure your team is crafting the best possible
content for the customer."""),
llm = llm,tools=(),allow_delegation=False, verbose=True)
The second Crew will be used to generate recommendation text for courses recommended to a student.
campaign_agent = Agent(
role="campaign_agent",
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,allow_delegation=False, verbose=True)#import csv
Define tasks for each agent
The previous agent works with the previous agent on a new team. Now, let's define the tasks that each agent will perform.
def get_ad_campaign_task(agent, customer_description, products):
return Task(description=dedent(f"""\
You're creating a targeted marketing campaign tailored to what we know about our student customers.
For each student customer, we have to choose exactly three courses to promote in the next campaign.
Make sure the selection is the best possible and aligned with the student 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 courses participating in the campaign: {products}.
This is all we know so far from the student customer: {customer_description}.
To start this campaign we will need to build first an understanding of our student customer.
Once we have a profile about the student customers interests, lifestyle and means and needs,
we have to select exactly three courses that have the highest chance to be bought by them.
Your final answer MUST be exactly 3 courses from the list, each with a short description
why it matches with this student customer. It must be formatted like this example:
:
:
:
"""),
agent=agent,expected_output="A refined finalized version of the marketing campaign in markdown format"
)
def get_ad_campaign_written_task(agent, selection):
return Task(description=dedent(f"""\
You're creating a targeted marketing campaign tailored to what we know about our student customer.
For each student customer, we have chosen three courses 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 courses to the student 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:
Interested in learning data science, get yourself enrolled in this course from Harvard University.
Take Your career to the next level with the help of this course.
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.
"""),
agent=agent,expected_output="A refined finalized version of the marketing campaign in markdown format"
)
Iterate through each row of the student profile data frame
Each agent is linked to an LLM model. Here we have used the 'gpt-3.5-turbo-16k' model. Let's now run the entire process for each row of our student profile data set:
df_output_list = () #to store results
for index, row in df_customers.iterrows():
print('############################################## '+ str(index))
customer_description = f'''
Their academic goals are {row('Academic Goals')}.
Their major is in {row(' Major')}.
Their Hobbies are {row(' Hobbies')}.
Their computer skills are {row(' Computer Skills')}.
Their interest in languages are {row(' Interest in Languages')}.
Their GPA is {row(' GPA')}.
'''
print(customer_description)
#Define Task 1 for selecting top 3 relevant products
task1 = get_ad_campaign_task(Chief_Recommendation_Director ,customer_description, products)
#start crew
targetting_crew = Crew(
agents=(student_profiler, course_specialist ,Chief_Recommendation_Director ),
tasks=(task1),
verbose=True,
process=Process.sequential # Sequential process will have tasks executed one after the other and the outcome of the previous one is passed as extra content into this next.
)
targetting_result = targetting_crew.kickoff()
#Define Task 2 for Generating Recommendation Campaign
task2 = get_ad_campaign_written_task(Chief_Recommendation_Director ,targetting_result)
copywriting_crew = Crew(
agents=(campaign_agent,Chief_Recommendation_Director ),
tasks=(task2),
verbose=True,
process=Process.sequential # Sequential process will have tasks executed one after the other and the outcome of the previous one is passed as extra content into this next.
)
copywriting_result = copywriting_crew.kickoff()
#create one line in output df
df_output_list.append({'customer':customer_description,
'targeted_products':targetting_result,
'promo_msg':copywriting_result,
})
#loop ended, collect results in dataframe
df_output = pd.DataFrame(df_output_list)
The output in a pandas data frame looks like this with the recommendation text generated in the last column of 'promo_msg':
Analysis of Generated Campaigns
Let's delve into detail into a student's profile and the campaign generated using Crew ai.
Let's consider the following student profile:
The product specialist agent has selected the following courses based on the student's profile – – Academic Goals: Specialize in psychology
The second computer to generate a recommendation text for the courses recommended to a student generated the following recommendation messages:
Conclusion
In this article, we saw that ai agents can make smart decisions when choosing the best products for customers using detailed customer profiles. These profiles consider the characteristics of various customers, including their choices and preferences. By analyzing these profiles, ai agents can generate personalized recommendations for each customer. Furthermore, ai agents work together like a well-oiled machine, ensuring better production quality and leading to accurate and logical decisions. With open source frameworks like ai CrewUsers can provide natural language instructions and use different agents for different tasks so everyone works together as a unified team toward a common goal.
Additionally, to better understand Agent ai, explore: The Pioneering Agent ai Program
Key takeaways
- ai agents are autonomous software programs capable of analyzing data, making predictions, and providing recommendations in various industries, including marketing and education.
- CrewAI is a framework that organizes ai agents into specialized teams, allowing them to collaborate efficiently on complex tasks.
- In the edtech industry, CrewAI can enhance the learning experience by recommending personalized courses to students based on their profiles, interests, and skills.
- CrewAI components include agents (autonomous units), tasks (specific tasks), and crews (collaborative groups working to achieve a shared goal).
- The article demonstrates how CrewAI can improve marketing by analyzing customer data to create personalized campaigns, which can lead to higher engagement and better business results.
The media shown in this article is not the property of Analytics Vidhya and is used at the author's discretion.
Frequently asked questions
Answer. ai agents are intelligent software programs that operate independently to perform various tasks. They analyze data, make predictions, and recommend actions, making them valuable in industries such as marketing and education.
Answer. In marketing, ai agents can analyze customer data to understand preferences, helping to create personalized campaigns that increase customer engagement and satisfaction, ultimately leading to better business results.
Answer. CrewAI is a framework that organizes ai agents into teams or “crews,” each with specialized roles. These agents collaborate to complete tasks efficiently, ensuring smooth execution of complex projects.
Answer. CrewAI can be used to recommend personalized courses for students. By analyzing student profiles, ai agents can suggest courses that align with students' interests, goals, and skills, improving their learning experience.
Answer. CrewAI consists of agents, tasks, and crews. Agents execute specific tasks autonomously, tasks define the work to be performed, and teams are collaborative groups of agents that work together to achieve a common goal.