How AI Agents Using CrewAI Collaborate for Effective Edtech Solutions?

Nibedita Dutta Last Updated : 23 Oct, 2024
11 min read

AI agents are highly intelligent software programs that can operate independently to help with various tasks. These agents aren’t just skilled at doing tasks independently; they’re also excellent at 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 instance, in marketing, AI agents can analyze customer preferences, understand what individual customers are interested in, and then assist in creating personalized marketing campaigns just for them. This kind of customization makes customers feel more valued and engaged, often leading to higher satisfaction and better business outcomes.

One 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 toward a common goal. Each agent has its 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 match their learning needs and interests, improving their educational experience.

CrewAI
Source: Author

Learning Objectives

  • Understanding the Components of CrewAI.
  • Recommendation Campaign Generation for Students Using Agents.
  • Analysis of Generated Campaigns.

This article was published as a part of the Data Science Blogathon.

Components of CrewAI – Agents, Tasks & Crew

Agents

Agents are independent, self-directed units designed to execute specific tasks, make decisions, and interact dynamically with other agents in a system. Each agent operates autonomously, which means it can analyze its environment, respond to inputs, and make choices based on its programming and objectives. What makes agents particularly powerful is their ability to use a wide array of tools. These tools range from simple search functions for data retrieval to advanced integrations with other agents, APIs, and even entire blockchain networks. This ability enables agents to perform complex tasks that involve real-time data gathering, decision-making, and multi-agent collaboration.

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 in integrated platforms. Each task is often broken down into subtasks, which may require specialized tools or access to certain resources. Additionally, tasks may be designed to include specific details, such as identifying which agent is responsible for a particular task and outlining the required tools, processes, and execution paths. Task management in an agent-based system ensures efficient workflows and precise outputs.

Crew

A crew is a collaborative group of agents working together to accomplish a shared objective. Rather than functioning independently, crew agents are organized based on their unique capabilities and roles, ensuring they can collectively tackle more complex, multifaceted problems. The process of crew formation 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 for ensuring that tasks are completed in the right sequence, especially when dependencies exist between different agents’ actions. A well-organized crew can significantly enhance performance by leveraging the specialized skills of each agent while ensuring synchronized execution.

Also read: Top 4 Agentic AI Design Patterns for Architecting AI Systems

Problem Description: Recommendation Campaign Generation for Students Using Agents

Imagine you are running an education counselling 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 studying Environmental Science rather than one majoring in Computer Science.

The student’s profile dataset looks like the following:

Student Profile

The possible list of courses that should be used for recommendation to the students could be the following:

Courses

Our first Crew consists of the following AI agents, with the help of which we will first generate the top 3 suitable courses for each of the students in our dataset –

  • Chief Recommendation Director: AI agent that gets the main task and oversees work of others
  • Student Profiler: AI agent to understand the profiles of the different students
  • Course Specialist: AI agent that can help in linking appropriate courses to each student

Python Implementation

Install & Import Necessary Libraries  

Let us first start by installing the needed Python libraries –

!pip install crewai
!pip install langchain_openai

Next, we import all the required 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 check with any LLM model of our choice. Here, we have utilized 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 Datasets To be Used

Next, we define the csv for the student profile data and also the list of products/courses to be recommended to the 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 Agents To be Used

Post this, we begin defining the different AI agents for our first crew. Each AI agent needs a role, goal and backstory, as shown below in the code block:

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 a recommendation text for the 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 above agent works with the previous agent in a new crew. Now, let us define the tasks 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 DataFrame

Each agent is linked to a LLM model. Here, we have used the ‘gpt-3.5-turbo-16k’ model. Let us now execute the whole process for each row of our student profile dataset :

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)

Output in a pandas dataframe looks like the following with the generated recommendation text in the last column of ‘promo_msg’ :

Dataframe

Analysis of Generated Campaigns

Let’s dive into one student profile and the generated campaign using Crew AI in detail.

Let us consider the following student’s profile: 

Student profile

The product specialist agent has selected the following courses based on the profile of the student – – Academic Goals: To major in psychology

product specialist agent

The second crew for generating a recommendation text for the courses recommended to a student came up with the following recommendation messages – 

Output

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 various customers’ features, including their choices and preferences. Analyzing these profiles, the AI agents can generate personalized recommendations for each customer. Additionally, AI agents work together like a well-oiled machine, ensuring improved output quality and leading to accurate and logical decisions. With open-source frameworks like CrewAI, users can provide instructions in natural language and utilize different agents for different tasks to make them all work together as a unified team toward a common goal.

Also, to understand the Agent AI better, explore: The Agentic AI Pioneer Program

Key Takeaways

  1. AI agents are autonomous software programs capable of analyzing data, making predictions, and providing recommendations across various industries, including marketing and education.
  2. CrewAI is a framework that organizes AI agents into specialized teams, allowing them to collaborate efficiently on complex tasks.
  3. In the Edtech industry, CrewAI can enhance the learning experience by recommending personalized courses to students based on their profiles, interests, and skills.
  4. The components of CrewAI include agents (autonomous units), tasks (specific duties), and crews (collaborative groups working towards a shared goal).
  5. The article demonstrates how CrewAI can improve marketing by analyzing customer data to create personalized campaigns, which can lead to increased engagement and better business outcomes.

The media shown in this article is not owned by Analytics Vidhya and is used at the Author’s discretion.

Frequently Asked Questions

Q1. What are AI agents, and how do they function?

Ans. AI agents are intelligent software programs that operate independently to perform various tasks. They analyze data, make predictions, and recommend actions, making them valuable across industries like marketing and education.

Q2. How can AI agents benefit the marketing sector?

Ans. In marketing, AI agents can analyze customer data to understand preferences, helping create personalized campaigns that increase customer engagement and satisfaction, ultimately leading to better business outcomes.

Q3. What is CrewAI, and how does it utilize AI agents?

Ans. 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.

Q4. How can CrewAI be applied in the Edtech industry?

Ans. 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, enhancing their learning experience.

Q5. What are the main components of CrewAI?

Ans. CrewAI consists of agents, tasks, and crews. Agents execute specific tasks autonomously, tasks define the work to be done, and crews are collaborative groups of agents working together to achieve a common goal.

Nibedita completed her master’s in Chemical Engineering from IIT Kharagpur in 2014 and is currently working as a Senior Data Scientist. In her current capacity, she works on building intelligent ML-based solutions to improve business processes.

Responses From Readers

Clear

We use cookies essential for this site to function well. Please click to help us improve its usefulness with additional cookies. Learn about our use of cookies in our Privacy Policy & Cookies Policy.

Show details