Using AI Agents to Create Customized Customer Experiences

Nibedita Dutta Last Updated : 21 Nov, 2024
13 min read

In today’s fast-paced digital world, businesses are constantly looking for innovative ways to provide customized customer experiences that stand out. AI agents play a crucial role in personalizing these experiences by understanding customer behavior and tailoring interactions in real-time. In this article, we’ll explore how AI agents work to deliver customized customer experiences, examine the technologies behind them, and discuss practical applications across various industries to help businesses enhance their customer engagement and satisfaction.

Using AI Agents to Create Customized Customer Experiences

Learning Objectives

  • Understand how AI agents can be leveraged to create customized customer experiences by analyzing user preferences, behavior, and interactions.
  • Learn how to implement AI-driven solutions that deliver personalized services and enhance customer satisfaction through customized customer experiences across various industries.
  • Gain insights into practical use cases of AI agents in domains like personalized marketing and process automation.
  • Learn to implement multi-agent systems using Python libraries like CrewAI and LlamaIndex.
  • Develop skills in creating and orchestrating AI agents for real-world scenarios with step-by-step Python walkthroughs.

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

What Are AI Agents?

AI agents are specialized programs or models designed to perform tasks autonomously using artificial intelligence techniques, often mimicking human decision-making, reasoning, and learning. They interact with users or systems, learn from data, adapt to new information, and execute specific functions within a defined scope, like customer support, process automation, or complex data analysis.

In the real world, tasks rarely have single-step solutions. Instead, they typically involve a series of interconnected and standalone steps to be carried out. For example, for a question like –

“Which coffee had the highest sales in our Manhattan based store?” might have a single step answer.

However, for a question like –

“Which 3 coffee types would be liked by our customer Emily who works at Google, NYC office? She prefers low calories coffee and likes Lattes more than Cappuccinos. Also could you send a mail to her with a promotional campaign for these 3 coffee types mentioning the nearest location of our store to her office where she can grab these?”

A single LLM would not be able to handle such a complex query by itself and this is where the need for an AI agent consisting of multiple LLMs arises.

For handling such complex tasks, instead of prompting a single LLM, multiple LLMs can be combined together acting as AI agents to break the complex task into multiple independent tasks.

Key Features of AI Agents

We will now learn about key features of AI agents in detail below:

  • Agentic applications are built on one or more Language Models as their main framework, enabling them to produce intelligent, context-driven responses. These applications dynamically generate both responses and actions, adapting based on user interactions. This approach allows for highly interactive, responsive systems across various tasks.
  • Agents are good at handling complex ambiguous tasks by breaking one large task into multiple simple tasks. Each of these tasks can be handled by an independent agent.
  • Agents use a variety of specialized tools to perform tasks, each designed with a clear purpose— such as making API requests, or conducting online searches.
  • Human-in-the-Loop (HITL) acts as a valuable support mechanism within AI agent systems, allowing agents to defer to human expertise when complex situations arise or when additional context is required. This design empowers agents to achieve more accurate outcomes by combining automated intelligence with human judgment in scenarios that may involve nuanced decision-making, specialized knowledge, or ethical considerations.
  • Modern AI agents are multimodal and capable of processing and responding to a variety of input types, such as text, images, voice, and structured data like CSV files.

Building Blocks of AI Agents

AI agents are made up of certain building blocks. Let us go through them:

  • Perception. By perceiving their environment, AI agents can collect information, detect patterns, recognize objects, and grasp the context in which they function.
  • Decision-making. This involves the agent choosing the most effective action to reach a goal, relying on the data it perceives.
  • Action. The agent carries out the selected task, which may involve movement, sending data, or other types of external actions.
  • Learning. Over time, the agent enhances its abilities by drawing insights from previous interactions and feedback, typically through machine learning methods.

Step-by-Step Python Implementation

Let us consider a use case in which a coffee chain like Starbucks wants to build an AI agent for drafting and mailing personalized promotional campaigns recommending 3 types of coffee for their customers based on their coffee preferences. The promotional campaign should also include the location of the coffee store which is nearest to the customer’s location where the customer can easily grab these coffees.

Step1: Installing and Importing Required Libraries

Start by installing the necessary libraries.

!pip install llama-index-core
!pip install llama-index-readers-file
!pip install llama-index-embeddings-openai
!pip install llama-index-llms-llama-api
!pip install 'crewai[tools]'
!pip install llama-index-llms-langchain

We will create the multi agent system here using CrewAI. This framework enables creation of a collaborative group of AI agents working together to accomplish a shared objective.

import os
from crewai import Agent, Task, Crew, Process
from crewai_tools import LlamaIndexTool
from llama_index.core import SimpleDirectoryReader, VectorStoreIndex
from llama_index.llms.openai import OpenAI
from langchain_openai import ChatOpenAI

Step2: Save Open AI Key as an Environment Variable

openai_api_key = ''
os.environ['OPENAI_API_KEY']=openai_api_key

We will be using OpenAI LLMs to query with our CSV data in the next steps. Hence defining the OpenAI key here as an environment variable is necessary here.

Step3: Load Data using LlamaIndex’s SimpleDirectoryReader

We will be using the Starbucks Data here which has information on different types of Starbucks Coffee along with their nutritional information. 

reader = SimpleDirectoryReader(input_files=["starbucks.csv"])
docs = reader.load_data()

Step4: Create Query Tool For Interacting With the CSV Data

#we have used gpt-4o model here as the LLM. Other OpenAI models can also be used
llm = ChatOpenAI(temperature=0, model="gpt-4o", max_tokens=1000)

#creates a VectorStoreIndex from a list of documents (docs)
index = VectorStoreIndex.from_documents(docs)

#The vector store is transformed into a query engine. 
#Setting similarity_top_k=5 limits the results to the top 5 documents that are most similar to the query, 
#llm specifies that the LLM should be used to process and refine the query results
query_engine = index.as_query_engine(similarity_top_k=5, llm=llm)
query_tool = LlamaIndexTool.from_query_engine(
    query_engine,
    name="Coffee Promo Campaign",
    description="Use this tool to lookup the Starbucks Coffee Dataset",
)

Step5: Creating the Crew Consisting of Multiple Agents 

def create_crew(taste):
  
  #Agent For Choosing 3 Types of Coffee Based on Customer Preferences
  researcher = Agent(
        role="Coffee Chooser",
        goal="Choose Starbucks Coffee based on customer preferences",
        backstory="""You work at Starbucks.
      Your goal is to recommend 3 Types of Coffee FROM THE GIVEN DATA ONLY based on a given customer's lifestyle tastes %s. DO NOT USE THE WEB to recommend the coffee types."""%(taste),
        verbose=True,
        allow_delegation=False,
        tools=[query_tool],
    )
    
  
  #Agent For Drafting Promotional Campaign based on Chosen Coffee
  writer = Agent(
        role="Product Content Specialist",
        goal="""Craft a Promotional Campaign that can mailed to customer based on the 3 Types of the Coffee suggested by the previous agent.Also GIVE ACCURATE  Starbucks Location in the given location in the query %s using 'web_search_tool' from the WEB where the customer can enjoy these coffees in the writeup"""%(taste),
        backstory="""You are a renowned Content Specialist, known for writing to customers for promotional campaigns""",
        verbose=True,

        allow_delegation=False)
  
  #Task For Choosing 3 Types of Coffee Based on Customer Preferences
  task1 = Task(
      description="""Recommend 3 Types of Coffee FROM THE GIVEN DATA ONLY based on a given customer's lifestyle tastes %s. DO NOT USE THE WEB to recommend the coffee types."""%(taste),
      expected_output="List of 3 Types of Coffee",
      agent=researcher,
  )
  
  #Task For Drafting Promotional Campaign based on Chosen Coffee
  task2 = Task(
      description="""Using ONLY the insights provided, develop a Promotional Campaign that can mailed to customer based on 3 Types of the Coffee suggested by the previous agent.

    Also GIVE ACCURATE Starbucks Location in the given location in the query %s using 'web_search_tool' from the WEB where the customer can enjoy these coffees in the writeup. Your writing should be accurate and to the point. Make it respectful and customer friendly"""%(taste),
      expected_output="Full Response to customer on how to resolve the issue .",
      agent=writer
  )
  
  #Define the crew based on the defined agents and tasks
  crew = Crew(
      agents=[researcher,writer],
      tasks=[task1,task2],
      verbose=True,  # You can set it to 1 or 2 to different logging levels
  )

  result = crew.kickoff()
  return result

In the above code, we have set up a two-agent system where:

  • One agent recommends three types of Starbucks coffee based on customer preferences.
  • The second agent drafts a promotional campaign around those coffees, including location information from the web.

The tasks are coordinated within a Crew, and the process kicks off with the crew.kickoff() function, returning the final result of the task execution.

Using AI Agents to Create Customized Customer Experiences

Step6: Checking Output For a Sample Customer

text = create_crew("Emily is from Gurgaon, India and likes High calorie coffees and prefers Latte more than Cappuccino.")
print(text)

As we can see, we want to check the output of the MultiAgent system we created for a sample customer ‘Emily’ who stays in Gurgaon, India and and likes High calorie coffees and prefers Latte more than Cappuccino.

# Agent: Coffee Chooser
## Task: Recommend 3 Types of Coffee FROM THE GIVEN DATA ONLY based on a given customer's lifestyle tastes Name - Emily, Location - Gurgaon,  Tastes - likes High calorie coffees and prefers Latte more than Cappuccino.. DO NOT USE THE WEB to recommend the coffee types.


# Agent: Coffee Chooser
## Thought: I need to gather coffee options that match Emily's preferences for high calorie coffees, with a preference for lattes over cappuccinos.
## Using tool: Coffee Promo Campaign
## Tool Input: 
"{\"query\": \"high calorie lattes\"}"
## Tool Output: 
The high-calorie lattes include the Caffè Latte with 2% Milk, which has 240 calories, and the Caffè Latte with Soymilk, which has 190 calories.


# Agent: Coffee Chooser
## Thought: Thought: I need to perform another search to find more latte options that are high in calories to meet Emily's preferences.
## Using tool: Coffee Promo Campaign
## Tool Input: 
"{\"query\": \"high calorie coffees with latte base\"}"
## Tool Output: 
High-calorie coffee options with a latte base include:

1. Vanilla Latte (Or Other Flavoured Latte) with 2% Milk, Venti size, which has 320 calories.
2. Vanilla Latte (Or Other Flavoured Latte) with Soymilk, which has 270 calories.


# Agent: Coffee Chooser
## Final Answer: 
1. Caffè Latte with 2% Milk (240 calories)
2. Vanilla Latte with 2% Milk, Venti size (320 calories)
3. Caffè Latte with Soymilk (190 calories)


# Agent: Product Content Specialist
## Task: Using ONLY the insights provided, develop a Promotional Campaign that can mailed to customer based on 3 Types of the Coffee suggested by the previous agent.

    Also GIVE ACCURATE Starbucks Location in the given location in the query Name - Emily, Location - Gurgaon,  Tastes - likes High calorie coffees and prefers Latte more than Cappuccino. using 'web_search_tool' from the WEB where the customer can enjoy these coffees in the writeup. Your writing should be accurate and to the point. Make it respectful and customer friendly


# Agent: Product Content Specialist
## Final Answer: 
Dear Emily,

We're thrilled to connect with you and share an exciting lineup of coffees tailored to your love for high-calorie drinks and preference for lattes! Treat yourself to our carefully crafted selections, perfect for your taste buds.

1. **Caffè Latte with 2% Milk (240 calories)**: This classic option combines rich espresso with steamed 2% milk for a wonderfully creamy experience. Enjoy the perfect balance of flavors while indulging in those calories!

2. **Vanilla Latte with 2% Milk, Venti Size (320 calories)**: Elevate your day with our delightful Vanilla Latte! The infusion of vanilla syrup adds a sweet touch to the robust espresso and creamy milk, making it a luscious choice that checks all your boxes.

3. **Caffè Latte with Soymilk (190 calories)**: For a slightly lighter yet satisfying variant, try our Caffè Latte with soymilk. This drink maintains the creamy goodness while being a tad lower on calories, perfect for a mid-day refreshment!

To experience these luxurious lattes, visit us at:

**Starbucks Location**:  
Starbucks at **Galleria Market, DLF Phase 4, Gurgaon**  
Address: Shop No. 32, Ground Floor, Galleria Market, DLF Phase 4, Gurugram, Haryana 122002, India. 

We can't wait for you to dive into the delightful world of our lattes! Stop by and savor these beautifully crafted beverages that will surely brighten your day. 

We're here to make your coffee moments memorable.

Warm regards,  
[Your Name]  
Product Content Specialist  
Starbucks

Final Output Analysis

  • As we can see in the final output, three coffee types are recommended based on the customer’s preferences – Caffè Latte with 2% Milk (240 calories), Vanilla Latte with 2% Milk, Venti Size (320 calories), Caffè Latte with Soymilk (190 calories).
  • All of the recommendations are high calorie drinks and Lattes specifically since the query mentioned that Emily prefers high calorie coffees and Lattes.
  • Also, we can see in the drafted promotional campaign that a Starbucks location in Gurgaon has been accurately mentioned.
output

Step7: Agent For Automating the Process of Mailing Campaigns to Customers

from langchain.agents.agent_toolkits import GmailToolkit
from langchain import OpenAI
from langchain.agents import initialize_agent, AgentType

toolkit = GmailToolkit() 

llm = OpenAI(temperature=0, max_tokens=1000)

agent = initialize_agent(
    tools=toolkit.get_tools(),
    llm=llm,
    agent=AgentType.STRUCTURED_CHAT_ZERO_SHOT_REACT_DESCRIPTION,
)

print(agent.run("Send a mail to [email protected] with the following text %s"%(text)))

We have utilized Langchain’s GmailToolKit (Reference Document) here to send mail to our customer, Emily’s mail id ([email protected]) with the previously generated text. To use Langchain’s GmailToolKit, you will need to set up your credentials explained in the Gmail API docs. Once you’ve downloaded the credentials.json file, you can start using the Gmail API. 

Process for Fetching the credentials.json

  • An application that authenticates to Google (for example, in our case LangChain’s GmailToolKit) using OAuth 2.0 must provide two objects in GCP – OAuth consent screen and OAuth Client ID.
  • To provide the OAuth consent screen and OAuth client ID, you must create a Google Cloud Platform (GCP) project first on the developer console.
Creating New Project on GCP console
  • Also go to “Gmail API” on the developer console and click on “Enable”.
Enabling the Gmail API
  • To open the OAuth consent screen creator, select APIs & Services » OAuth consent screen in your GCP project.
Configuring the OAuth consent screen
  • Select the user type as External user type.

You can select the Internal user type only if the GCP project belongs to an organization and the connector users are members of the same organization.

The External user type causes the authentication to expire in seven days. If you choose this type, you need to renew authentication weekly.

  • Select Create.

Provide the following information:

  • App name
  • User support email: your email address
  • Developer contact information: your email address
Customized Customer Experiences
  • Select Save and continue.

For External user type:

  • Select Test users » Add users.
  • Enter the email addresses of users that are allowed to use the connector.
  • Select Add.

To finish configuration, select Save and continue » Back to dashboard.

Configuring the OAuth client ID

The following procedure describes how to configure the OAuth Client ID:

  • To open the OAuth consent screen creator, select APIs & Services » Credentials in your GCP project.
  • Select Create credentials » OAuth client ID.
Customized Customer Experiences
  • In the Application type dropdown list, select Desktop App.
Configuring the OAuth client ID
  • In the Name box, enter the desired name
  • Click “Create”
Configuring the OAuth client ID: Customized Customer Experiences

Post clicking on “Create”, the above window will pop out that will give the Client ID and Client Secret. This can be saved in a JSON format using the “DOWNLOAD JSON” option. Post downloading, we can save this JSON file in our project folder with the name “credentials.json”. Once you have this credentials.json in the local folder, only then you would be able to use the GmailToolKit.

Sample Mail Output on Promotional Campaign (sent using the above code on AI Agent)

Sample Mail Output on Promotional Campaign (sent using the above code on AI Agent)

With this, we can fully automate the process of creating and sending personalized promotional campaigns, taking into account customers’ unique lifestyles, preferences, and geographical locations. By analyzing data from customer interactions, past purchases, and demographic information, this multi agent AI system can craft tailored recommendations that are highly relevant to each individual. This level of personalization ensures that marketing content resonates with customers, improving the chances of engagement and conversion.

For marketing teams managing large customer bases, AI agents eliminate the complexity of targeting individuals based on their personal tastes, allowing for efficient and scalable marketing efforts. As a result, businesses can deliver more effective campaigns while saving time and resources.

Challenges of AI agents

We will now discuss the challenges of AI agents below:

  • Limited context. LLM agents are capable of processing only a small amount of information at once, which can result in them forgetting essential details from previous parts of a conversation or overlooking important instructions.
  • Instability in Outputs. Inconsistent results occur when LLM agents, depending on natural language to engage with tools and databases, generate unreliable results. Formatting errors or failure to follow instructions accurately can happen, leading to mistakes in the execution of tasks.
  • Nature of Prompts. The operation of LLM agents depends on prompts, which must be highly accurate. A minor modification can lead to major errors, making the process of crafting and refining these prompts both delicate and essential.
  • Resource Requirements. Operating LLM agents requires significant resources. The need to process large volumes of data rapidly can incur high costs and, if not properly managed, may result in slower performance.

Conclusion

AI agents are advanced programs designed to perform tasks autonomously by leveraging AI techniques to mimic human decision-making and learning. They excel at managing complex and ambiguous tasks by breaking them into simpler subtasks. Modern AI agents are multimodal, capable of processing diverse input types such as text, images, and voice. The core building blocks of an AI agent include perception, decision-making, action, and learning capabilities. However, these systems face challenges such as limited context processing, output instability, prompt sensitivity, and high resource demands.

For instance, a multi-agent system was developed in Python for Starbucks to create personalized coffee recommendations and promotional campaigns. This system utilized multiple agents: one focused on selecting coffees based on customer preferences, while another handled the creation of targeted promotional campaigns. This innovative approach allowed for efficient, scalable marketing with highly personalized content.

Key Takeaways

  • AI agents help create customized customer experiences by analyzing user data and preferences to deliver personalized interactions that enhance satisfaction and engagement.
  • Leveraging AI agents for complex tasks enables businesses to create customized customer experiences, providing tailored solutions that anticipate needs and improve overall service quality.
  • Tool integration and human-in-the-loop approaches enhance AI agents’ accuracy and capabilities.
  • Multimodal features allow AI agents to work seamlessly with diverse data types and formats.
  • Python frameworks like LlamaIndex and CrewAI simplify building multi-agent systems.
  • Real-world use cases demonstrate AI agents’ potential in personalized marketing and customer engagement.

Frequently Asked Questions

Q1. What are AI agents, and how do they differ from traditional AI models?

A. AI agents are specialized programs that perform tasks autonomously using AI techniques, mimicking human decision-making and learning. Unlike traditional AI models that often focus on single tasks, AI agents can handle complex, multi-step processes by interacting with users or systems and adapting to new information.

Q2. What challenges do AI agents face?

A. AI agents encounter difficulties such as limited context processing, output instability, prompt sensitivity, and high resource requirements. These challenges can impact their ability to deliver consistent and accurate results in certain scenarios.

Q3.  What tools do AI agents use to perform tasks?

A. AI agents use specialized tools like API requests, online searches, and other task-specific utilities to perform actions. These tools have clear purposes, ensuring efficient task completion.

Q4. Why are AI agents needed for complex tasks?

A. AI agents are necessary for complex tasks because real-world problems often involve interconnected steps that cannot be solved in one go. AI agents, especially multi-agent systems, break these tasks into smaller, manageable subtasks, each handled independently.

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

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