top of page

ChatGPT for Customer Support


With the advent of conversational AI technologies like ChatGPT, creating intelligent chatbots that can engage with customers in natural language has become easier than ever. Gone are the days of frustratingly long wait times and repetitive inquiries.


With the magic of ChatGPT, you'll learn how to build a smart and responsive chatbot that not only delights your customers but also streamlines your support process. So, grab your virtual hard hat and get ready for an exciting journey through the process of building a robust and effective customer service chatbot using ChatGPT.



 

ChatGPT


Let's first get introduced to ChatGPT, the cornerstone of conversational AI


Developed by OpenAI, ChatGPT is an advanced language model built on the GPT (Generative Pre-trained Transformer) architecture, renowned for its ability to generate human-like text. But ChatGPT is more than just a fancy text generator; it's a sophisticated AI system trained on vast amounts of text data from the internet, allowing it to comprehend and generate responses to a wide array of queries.


Using deep learning techniques, ChatGPT is capable of understanding context, tone, and even nuances in language, making it one of the most versatile and powerful conversational AI models available today. This groundbreaking technology has transformed the way we interact with machines, opening up a world of possibilities for applications ranging from customer service chatbots to virtual assistants and beyond.


From customer service chatbots to virtual assistants and even creative writing tools, the applications of ChatGPT are virtually limitless. Its ability to generate human-like responses makes it an invaluable asset for businesses looking to automate customer interactions, streamline support processes, and provide a more personalized experience to their users.


Businesses and enterprises are constantly seeking innovative solutions to streamline their operations, enhance customer experiences, and stay ahead of the competition. Let's take a look at some of the prominent enterprises that have embraced ChatGPT to drive their success:



Companies like Shopify, Sprinklr, Koo, Zendesk, Spotify, Bain and Company, PwC, Zapier, Riot Games, and many more have recognized the transformative potential of ChatGPT and integrated it into their services to enhance customer experiences, streamline operations, and drive innovation.


 

Now that we are familiar with ChatGPT and its significance in the world of conversational AI, it's time to roll up our sleeves and get our hands dirty with some practical examples. In this tutorial, we'll walk through the process of building a customer service chatbot using ChatGPT, step-by-step.


Imagine you're running a tech store, and your customers are eager to know everything about the latest gadgets and gizmos. They're firing off questions left and right, from product specs to availability. But fear not! With the help of ChatGPT, we'll learn how to craft intelligent responses that not only answer your customers' queries but also provide a seamless and engaging experience.


We'll start by setting up our development environment and loading the necessary libraries. Then, we'll explore how to leverage ChatGPT's powerful language generation capabilities to create intelligent responses to customer queries. Along the way, we'll learn about best practices for training and fine-tuning our chatbot, as well as how to integrate it into existing systems and workflows.




Prerequisites


Before we start with building a customer service chatbot using ChatGPT, there are a few prerequisites you'll need to have in place:

  1. Python Environment: Ensure you have Python installed on your system. You can download and install Python from the official website (https://www.python.org/) if you haven't already.

  2. OpenAI API Key: Obtain an API key from OpenAI to access their GPT models. You can sign up for an API key on the OpenAI website (https://openai.com/). This key will be used to authenticate your requests to the OpenAI API.

  3. Python Libraries: Install the necessary Python libraries required for interacting with the OpenAI API and handling environmental variables. You can install these libraries using pip, Python's package manager. Run the following command in your terminal or command prompt: pip install openai python-dotenv

  4. Dotenv File: Create a .env file in your project directory to store your OpenAI API key. This file will be used to load the API key as an environmental variable in your Python script. Ensure that your .env file is in the same directory as your Python script. makefileCopy code # .env file OPENAI_API_KEY=your_api_key_here

  5. Basic Understanding of Python: Familiarize yourself with the basics of Python programming language syntax and concepts. This tutorial assumes you have a basic understanding of Python, including variables, functions, and control flow statements.

Once you have these prerequisites in place, you'll be all set to embark on the journey of building your very own customer service chatbot with ChatGPT!


 

And here are the steps involved in the process that we will be covering in the following sections of the blog:


Lets explore these steps in detail.


Step 1: Load the API Key and Relevant Python Libraries


First and foremost, we'll load the OpenAI API key and import the necessary Python libraries to interact with the OpenAI API and handle environmental variables.

Loading the API Key

The OpenAI API key is essential for authenticating requests to the OpenAI API. We'll load the API key from a `.env` file using the `python-dotenv` library, which allows us to store sensitive information like API keys securely.



import os
from dotenv import load_dotenv, find_dotenv

# Load the API key from the .env file
load_dotenv(find_dotenv())
openai_api_key = os.getenv("OPENAI_API_KEY")

Importing Relevant Python Libraries

Next, we'll import the relevant Python libraries required for interacting with the OpenAI API. In this case, we'll import the `openai` library, which provides functions for accessing various OpenAI models, including GPT.

import openai

Note: The version of openai used in this tutorial is 0.27.10.


Now that we've loaded the API key and imported the necessary libraries, we're ready to move on to the next steps, where we'll define functions and messages for interacting with the ChatGPT model.



Step 2: Define a Function to Get Completions from Messages using ChatGPT


Now that we have our API key loaded and libraries imported, it's time to define a function that will allow us to interact with ChatGPT and generate completions for user messages.


Creating the Function

We'll define a Python function called get_completion_from_messages() that takes a list of messages as input and returns a completion generated by ChatGPT. This function will utilize the openai.ChatCompletion.create() method, which sends a request to the OpenAI API to generate a text completion based on the provided messages.



def get_completion_from_messages(messages, model="text-davinci-003", temperature=0.5, max_tokens=100): 

response = openai.ChatCompletion.create( model=model, 
                                         messages=messages,     
                                         temperature=temperature, 
                                         max_tokens=max_tokens, ) 

return response.choices[0].message["content"]

Tips and Tricks:

  • Model Selection: The model parameter allows you to specify which ChatGPT model to use for generating completions. You can experiment with different models to see which one works best for your use case. The default model used here is "text-davinci-003", which refers to the Davinci model provided by OpenAI. The Davinci model is a powerful and versatile language model capable of generating human-like text across a wide range of topics.

  • Temperature: The temperature parameter controls the randomness of the generated completions. Higher temperatures result in more diverse responses, while lower temperatures result in more conservative responses. The default value is 0.5, which strikes a balance between creativity and coherence. Experiment with different temperature values to find the right balance for your application.

  • Max Tokens: The max_tokens parameter specifies the maximum number of tokens (words) in the generated completion. Adjust this parameter based on the desired length of the generated responses. Be mindful of the API's token usage limits when setting this value. The default value is 100 tokens.

By defining this function, we've laid the foundation for interacting with ChatGPT and generating completions for user messages. In the next steps, we'll create sample messages and use this function to generate completions for them. Let's continue building our customer service chatbot!



Step 3: Define a System Message with Chain-of-Thought Prompting Instructions


In this step, we'll define a system message that provides instructions for chain-of-thought prompting. Chain-of-thought prompting involves breaking down the reasoning process into steps, guiding the AI to generate responses in a structured manner.


Creating the System Message

We'll create a system message that contains the chain-of-thought prompting instructions. This message will be presented to the AI alongside user messages to guide its response generation process.



delimiter = "####"
system_message = f"""
Follow these steps to answer the customer queries.
The customer query will be delimited with four hashtags,\
i.e. {delimiter}.

Step 1:{delimiter} First decide whether the user is \
asking a question about a specific product or products. \
Product cateogry doesn't count. 

Step 2:{delimiter} If the user is asking about \
specific products, identify whether \
the products are in the following list.

All available products: 

1. Product: TechPro Ultrabook
   Category: Computers and Laptops
   Brand: TechPro
   Model Number: TP-UB100
   Warranty: 1 year
   Rating: 4.5
   Features: 13.3-inch display, 8GB RAM, 256GB SSD, Intel Core i5 processor
   Description: A sleek and lightweight ultrabook for everyday use.
   Price: $799.99

2. Product: BlueWave Gaming Laptop
   Category: Computers and Laptops
   Brand: BlueWave
   Model Number: BW-GL200
   Warranty: 2 years
   Rating: 4.7
   Features: 15.6-inch display, 16GB RAM, 512GB SSD, NVIDIA GeForce RTX 3060
   Description: A high-performance gaming laptop for an immersive experience.  
   Price: $1199.99

3. Product: PowerLite Convertible
   Category: Computers and Laptops
   Brand: PowerLite
   Model Number: PL-CV300 
   Warranty: 1 year 
   Rating: 4.3
   Features: 14-inch touchscreen, 8GB RAM, 256GB SSD, 360-degree hinge
   Description: A versatile convertible laptop with a responsive touchscreen.
   Price: $699.99

4. Product: TechPro Desktop
   Category: Computers and Laptops
   Brand: TechPro
   Model Number: TP-DT500
   Warranty: 1 year
   Rating: 4.4
   Features: Intel Core i7 processor, 16GB RAM, 1TB HDD, NVIDIA GeForce GTX 1660
   Description: A powerful desktop computer for work and play.
   Price: $999.99

5. Product: BlueWave Chromebook
   Category: Computers and Laptops
   Brand: BlueWave
   Model Number: BW-CB100
   Warranty: 1 year 
   Rating: 4.1
   Features: 11.6-inch display, 4GB RAM, 32GB eMMC, Chrome OS
   Description: A compact and affordable Chromebook for everyday tasks.
   Price: $249.99

Step 3:{delimiter} If the message contains products \
in the list above, list any assumptions that the \
user is making in their \
message e.g. that Laptop X is bigger than \
Laptop Y, or that Laptop Z has a 2 year warranty.

Step 4:{delimiter}: If the user made any assumptions, \
figure out whether the assumption is true based on your \
product information. 

Step 5:{delimiter}: First, politely correct the \
customer's incorrect assumptions if applicable. \
Only mention or reference products in the list of \
5 available products, as these are the only 5 \
products that the store sells. \

Answer the customer in a friendly tone.
Use the following format:
Step 1:{delimiter} <step 1 reasoning>
Step 2:{delimiter} <step 2 reasoning>
Step 3:{delimiter} <step 3 reasoning>
Step 4:{delimiter} <step 4 reasoning>

Response to user:{delimiter} <response to customer>

Make sure to include {delimiter} to separate every step.
"""

Alright, let's break down what we've done here!


We've crafted a special message designed for our chatbot system. This message isn't meant for users but rather for guiding the AI in generating responses. We call this type of message a "system message."


Now, this system message isn't just any ordinary instruction set; it's specifically tailored for what we call "chain-of-thought prompting." This technique is super useful because it helps our AI understand complex queries and respond in a structured and logical manner.


Imagine you're talking to a friend who's helping you solve a problem. They don't just blurt out an answer; instead, they guide you through a series of steps, asking questions and providing insights along the way. That's exactly what our system message does for our AI!


We've broken down the reasoning process into clear steps. First, the AI needs to determine if the user is asking about specific products. Then, it checks if those products match the ones we've listed. If the user makes any assumptions about the products, the AI verifies them and politely corrects any inaccuracies.


By using chain-of-thought prompting, we ensure that our AI provides accurate and helpful responses, just like a knowledgeable friend guiding you through a problem-solving process. It's a powerful technique that enhances the quality of interactions and builds trust with users.


Chain-of-Thought Prompting Instructions
  • Step 1: Determine whether the user is asking about specific products or a general query.

  • Step 2: Identify if the products mentioned in the user's message are among the listed products.

  • Step 3: List any assumptions the user is making about the products mentioned.

  • Step 4: Verify the accuracy of the user's assumptions based on the provided product information.

  • Step 5: Politely correct any incorrect assumptions and provide the user with a friendly response.

By providing these chain-of-thought prompting instructions, we guide the AI to generate responses in a structured and logical manner, ensuring accurate and helpful interactions with customers.



Step 4: Define a User Message with a Specific Query


In this step, we'll create a user message containing a specific query that we want our chatbot to respond to. This message will be used to test our chatbot's ability to generate accurate and helpful responses based on the provided query.


Creating the User Message

We'll define a user message that represents a query or inquiry that a customer might have. This message will be structured in a way that prompts the AI to provide information or assistance related to the query.



# Define a user message with a specific query 

user_message = f""" by how much is the BlueWave Chromebook more expensive \ than the TechPro Desktop """

Purpose of the User Message

The user message serves as a test case for our chatbot. It presents a specific query related to the prices of two products, the BlueWave Chromebook and the TechPro Desktop. By providing this query to our chatbot, we can evaluate its ability to understand the user's question, retrieve relevant information about the products, and generate a helpful response.


Importance of Test Cases

Test cases like this one are crucial for evaluating the performance and effectiveness of our chatbot. They allow us to assess how well the chatbot handles different types of queries and interactions, identify any areas for improvement, and refine the chatbot's capabilities over time.


By defining specific user messages with different queries and scenarios, we can thoroughly test our chatbot and ensure that it delivers accurate, relevant, and helpful responses to users. Let's proceed to the next step, where we'll use this user message to interact with our chatbot and evaluate its performance.



Step 5: Get a Completion Response from ChatGPT for the User Message


Now, we'll use the user message we defined earlier to interact with ChatGPT and generate a completion response. This completion response will be the AI-generated answer to the user's query.


Generating the Completion Response

We'll pass the user message to our previously defined function, get_completion_from_messages(), which sends a request to the OpenAI API and retrieves a completion response from ChatGPT based on the provided message.



user_message = f"""
by how much is the BlueWave Chromebook more expensive \
than the TechPro Desktop"""

messages =  [  {'role':'system', 
                'content': system_message},    
               {'role':'user', 
                'content': f"{delimiter}{user_message}{delimiter}"}, ] 

response = get_completion_from_messages(messages)
print(response)

Now, let's break down what's happening in the code above for better clarity of this implementation:

  1. Defining the User Message: First, we define a user message that represents a specific query from the user. In this case, the user is asking about the price difference between the BlueWave Chromebook and the TechPro Desktop. We use an f-string to format the message for clarity.

  2. Creating the Messages List: Next, we create a list called messages containing dictionaries. Each dictionary represents a message with a role (either "system" or "user") and its content. We include the system message, which provides chain-of-thought prompting instructions, and the user message we defined earlier. These messages are structured with a delimiter for clarity.

  3. Getting a Completion Response: We use the get_completion_from_messages() function to send the messages list to ChatGPT and obtain a completion response. This response contains the AI-generated content based on the user query and the instructions provided in the system message.

  4. Printing the Response: Finally, we print the completion response, which includes the AI-generated reasoning steps and the response to the user's query. The response is formatted with step numbers and delimiters for clarity.


The result shows how ChatGPT has processed the user's query step by step, including identifying the products mentioned, correcting any assumptions made by the user, and providing the accurate response about the price difference between the BlueWave Chromebook and the TechPro Desktop.


It's like having a virtual assistant walk you through the reasoning process and deliver the answer in a clear and informative manner!


Evaluating the Completion Response

Once we receive the completion response from ChatGPT, we'll evaluate it to determine if it accurately addresses the user's query. We'll assess factors such as relevance, coherence, and correctness to gauge the quality of the AI-generated response.


Importance of Evaluation

It's essential to evaluate the completion response to ensure that the chatbot provides accurate and helpful information to users. By assessing the quality of the AI-generated responses, we can identify any areas for improvement and refine the chatbot's capabilities to deliver better user experiences.


Let's proceed to generate the completion response and evaluate the AI-generated answer to the user's query. This step will help us assess the performance of our chatbot and make any necessary adjustments to enhance its effectiveness.



Step 6: Extract the Final Response from the Completion


Now that we've obtained a completion response from ChatGPT, let's extract the final response and present it in a conversational manner.


Extracting the Final Response

We'll take the completion response and extract the final message generated by ChatGPT. This final message is what we want to showcase to the user.



# Extract the final response from the completion 

try: 

final_response = response.split(delimiter)[-1].strip() 

except Exception as e: 

final_response = "Sorry, I'm having trouble right now. Please try asking another question."

Presenting the Final Response

Let's present the final response in a friendly and conversational tone, making it suitable for user interaction and see what ChatGPT has to say about the price difference between the BlueWave Chromebook and the TechPro Desktop!



# Presenting the final response 
print("Chatbot's Response:")
print(final_response)

By extracting the final response, we can evaluate how well ChatGPT has addressed the user's specific query and whether it followed the chain-of-thought prompting instructions provided in the system message.



Step 7: Repeat Steps 4-6 for Different User Messages


Here, we'll repeat the process of defining user messages, obtaining completion responses from ChatGPT, and extracting final responses for different user queries. This iterative testing approach allows us to evaluate how well our chatbot performs across various scenarios and user interactions.


Define Different User Messages

First, we'll define multiple user messages representing different queries or inquiries that users might have. These messages will cover a range of topics and scenarios to thoroughly test the chatbot's capabilities.



# Define different user messages with specific queries

user_messages = [
"do you sell TVs?",

"what is the warranty period for the TechPro Ultrabook?",

"can you help me choose between the BlueWave Gaming Laptop and the PowerLite Convertible?",

"how does the TechPro Desktop compare to the BlueWave Chromebook in terms of performance?",

"I'm looking for a laptop with at least 16GB RAM. Do you have any recommendations?",

"what accessories are included with the PowerLite Convertible?"

]

Here we're essentially continuing the testing process for our chatbot, but this time, we're expanding our horizons by testing it with different user queries. So, instead of just evaluating one specific scenario, we're now exploring a variety of potential questions and inquiries that users might have.


Iterate Through User Messages

Next, we'll iterate through each user message, perform Steps 4-6 (defining a user message, obtaining a completion response, and extracting the final response), and present the results.



for user_message in user_messages:
   
    # Step 4: Define a user message with a specific query
    print("User Message:")
    print(user_message)

    # Step 5: Get a completion response from ChatGPT for the user message
    messages = [ {'role': 'system', 'content': system_message},
                 {'role': 'user', 'content': user_message} ]
    
    response = get_completion_from_messages(messages)

    # Step 6: Extract the final response from the completion
    try:
        final_response = response.split(delimiter)[-1].strip()
   
    except Exception as e:
     
        final_response = "Sorry, I'm having trouble right now. Please try asking another question."

    # Present the final response
    print("Chatbot's Response:")
    print(final_response)
    print("-------------------------------------")

Here's what's happening step by step:

  1. Defining Different User Messages: We start by defining a list of user messages, each representing a specific query or inquiry. These messages cover a range of topics to thoroughly test our chatbot's capabilities. It's like throwing a bunch of different questions at our chatbot to see how well it handles them.

  2. Iterating Through User Messages: We then loop through each user message in the list. For each message:

  • Step 4: We define the user message, just like before.

  • Step 5: We get a completion response from ChatGPT for the user message, following the same process as before.

  • Step 6: We extract the final response from the completion, again, similar to what we did previously.


The key difference here is that we're now testing our chatbot with a variety of user queries

instead of just one. This allows us to assess its performance across different scenarios and interactions. It's like giving our chatbot a comprehensive test to make sure it's ready to handle any question thrown its way!


By repeating these steps for different user messages, we can comprehensively evaluate the performance of our chatbot and ensure that it provides accurate, relevant, and helpful responses across various scenarios and inquiries.



Witness the Magic in Action!


So far we have covered the mechanics of building a functional chatbot, now it's time to add some polish and see it in action! Imagine the excitement of unwrapping a present, eagerly anticipating what's inside. Similarly, let's give our chatbot a sleek and appealing appearance to engage users effectively.


I have used the panel library to create a user-friendly interface for our chatbot. With panel, we can craft a visually appealing layout that resembles a real conversation between a user and a chatbot.


To achieve this, we'll employ components like TextInput for user input, Button to send messages, and ChatMessage to display the conversation flow between the user and the chatbot.


With these elements in place, our chatbot is ready to engage users in meaningful conversations. As users interact with the chatbot, they'll experience the smooth flow of communication, just like chatting with a knowledgeable customer support representative.






The interface is clean and intuitive, with a text input field for users to type their queries and a button to send their message. As users interact with the chatbot, their messages and the bot's responses are neatly displayed in a conversational format. It's like having a real conversation with a helpful assistant!


This is just a glimpse of what's possible with Panel and OpenAI. If you're interested in learning more about how to create such interfaces and dive deeper into the process, let us know in the comments below. We'd love to explore the steps involved in creating the interface in more detail in a future blog post.


So, stay tuned for more exciting content!


 

In wrapping up, we've journeyed into the world of customer service chatbots powered by ChatGPT. By following along, you've gained insights into the inner workings of this fascinating technology, from understanding its significance in transforming customer support to getting hands-on experience in creating your very own chatbot.


We've explored the steps involved in setting up the environment, defining the chatbot's functionality, and even spiced things up by giving it a sleek interface. As you reflect on your newfound knowledge, remember that the possibilities with chatbots are endless, and with a little creativity, you can leverage this powerful tool to enhance customer experiences, streamline operations, and propel your business forward.


Happy bot building!



If you require assistance with the implementation of such chatbots, or if you need help with related projects, please don't hesitate to reach out to us.

Recent Posts

See All
bottom of page