Introduction
In today’s fast-paced digital world, providing efficient and accurate customer support is crucial for any business. As part of my ongoing exploration of AI applications, I embarked on a project to develop an AI-powered customer support chatbot. This chatbot leverages transformer-based models for natural language understanding and generation, enabling it to interact with customers naturally and effectively.
In this blog post, I’ll walk you through the entire process of creating this chatbot, from defining the project scope to deploying the final product. Whether you’re a developer looking to implement a similar solution or a business owner curious about AI’s potential in customer support, this guide will provide basic valuable insights and practical steps.
Project Overview
The goal of this project was to create a customer support chatbot for an AI tool listing website. The chatbot needed to handle various types of queries, including:
– General inquiries (e.g. membership pricing, refund policies)
– Troubleshooting (e.g. password reset)
– Specific questions about services
– Escalation to live support for complex queries
**Ensure you have Python installed on your machine and all the necessary libraries.
Step 1: Preparing the Dataset
The first step in building our chatbot was to create a comprehensive dataset of potential questions and their corresponding answers. This dataset serves as the knowledge base for our chatbot.
Here’s a sample of what our dataset looked like:
Question | Answer | Intent |
---|---|---|
What is the membership pricing? | “Our membership pricing starts at basic price, with premium options available. You can view all membership tiers and pricing details by visiting our Membership Pricing Page. If you have any specific questions, feel free to ask.” | membership_pricing |
Details on Membership Pricing | “Free membership – can view all listing but cannot review and rate listing and cannot add to favorites, also cannot join a forum. Basic Plan which costs 1.50 – Can review, rate, add to favorites, can view discussions in general forum only (no access to start-up forum), cannot join forum or join discussions and cannot submit a post on the member blog. Premium Plan which costs 5 – access to all services. Visit Membership Pricing Page to see the full breakdown.” | membership_pricing_details |
I created a simple dataset in a CSV file, ensuring that I covered a wide range of potential customer queries.
Step 2: Loading and Processing the Dataset
With our dataset ready, the next step was to load and process it. I used Python and the Pandas library for this task.
import pandas as pd # Load your dataset from a CSV file faq_df = pd.read_csv('FAQ_dataset.csv') # Display the dataframe (optional, for checking) print(faq_df.head())
Step 3: Implementing Intent Recognition
For our chatbot to provide accurate responses, it needs to understand the intent behind user queries. I explored two approaches for intent recognition:
Approach 1: Simple Keyword Matching
Initially, I implemented a simple keyword matching method. While straightforward, this approach had limitations in understanding context and variations in user input.
def get_intent_from_input(user_input, faq_df): # Check if any question from the CSV matches the user's input (case insensitive) for index, row in faq_df.iterrows(): if row['Question'].lower() in user_input.lower(): return row['Answer'] return "Sorry, I don't understand your question." # Example usage user_input = "How can I reset my password?" response = get_intent_from_input(user_input, faq_df) print(response)
“`
NOPE, this did not meet my goal –
It only worked when the user input matched the question exactly as written in the dataset, which wasn’t ideal for real-world use.
Approach 2: NLP-Based Intent Recognition
To improve our chatbot’s understanding, I moved to an NLP-based approach using transformer models. After experimenting with TF-IDF and encountering issues with response accuracy (I was getting the same answer for different types of questions), I settled on using Sentence-BERT, which provided much better results.
Here’s how I implemented the Sentence-BERT approach:
import pandas as pd from sentence_transformers import SentenceTransformer, util # Load the dataset faq_df = pd.read_csv('FAQ_dataset.csv') # Initialize the Sentence Transformer model model = SentenceTransformer('all-MiniLM-L6-v2') # User Queries to test with user_queries = [ "How do I cancel my membership?", "I forgot my password, what do I do?", "I want to be a member, how can I subscribe?", "Can I stop receiving newsletters?", "How can I get a refund?", "I want to list my product, can you help?", "I want to place an Ad, how do I go about it?", "Which channel can I use to contact support? I need to complain about an issue?" # Encode the FAQ intents faq_embeddings = model.encode(faq_df['Intent'].tolist(), convert_to_tensor=True) def get_best_answer(user_input, faq_df, faq_embeddings): # Encode the user input user_embedding = model.encode(user_input, convert_to_tensor=True) # Calculate cosine similarity similarity_scores = util.pytorch_cos_sim(user_embedding, faq_embeddings)[0] # Get the index of the best matching intent best_match_idx = similarity_scores.argmax().item() # Retrieve the matched intent and response best_intent = faq_df.iloc[best_match_idx]['Intent'] best_response = faq_df.iloc[best_match_idx]['Answer'] return best_intent, best_response # Example usage user_input = "How do I cancel my membership?" best_intent, best_response = get_best_answer(user_input, faq_df, faq_embeddings) print(f"User Query: {user_input}") print(f"Matched Intent: {best_intent}") print(f"Response: {best_response}")
This approach significantly improved the chatbot’s ability to understand and respond to a variety of user inputs, even when they didn’t exactly match the predefined questions.
Step 4: Testing the Chatbot
To ensure our chatbot was functioning correctly, I tested it with a variety of user queries:
user_queries = [ "How do I cancel my membership?", "I forgot my password, what do I do?", "I want to be a member, how can I subscribe?", "Can I stop receiving newsletters?", "How can I get a refund?", "I want to list my product, can you help?", "I want to place an Ad, how do I go about it?", "Which channel can I use to contact support? I need to complain about an issue?" ] for user_input in user_queries: best_intent, best_response = get_best_answer(user_input, faq_df, faq_embeddings) print(f"User Query: {user_input}") print(f"Matched Intent: {best_intent}") print(f"Response: {best_response}") print("=" * 50)
The results showed that our chatbot was able to understand and respond accurately to a wide range of queries, demonstrating its effectiveness in real-world scenarios.
Sample Output
Similarity Score for ‘How do I cancel my membership?’: 0.7760460376739502
User Query: How do I cancel my membership?
Matched Intent: membership_cancellation
Response: “To cancel your membership:
Log in to your account.
Navigate to the ‘Settings’ or ‘Account Management’ section.
Click on ‘Cancel Membership’ and follow the on-screen instructions.
Alternatively, you can contact our customer support to assist with cancellation.”
==================================================
Similarity Score for ‘I forgot my password, what do I do?’: 0.7071841359138489
User Query: I forgot my password, what do I do?
Matched Intent: password_reset
Response: “To reset your password, please follow these steps:
Go to the Password Reset Page.
Enter the email address associated with your account.
Check your inbox for a reset link and follow the instructions to create a new password.
If you encounter any issues, please contact our support team.”
==================================================
Similarity Score for ‘I want to be a member, how can I subscribe?’: 0.4207305908203125
User Query: I want to be a member, how can I subscribe?
Matched Intent: membership_pricing
Response: “Our membership pricing starts at basic price, with premium options available. You can view all membership tiers and pricing details by visiting our Membership Pricing Page. If you have any specific questions, feel free to ask.”
==================================================
Similarity Score for ‘Can I stop receiving newsletters?’: 0.6970396041870117
User Query: Can I stop receiving newsletters?
Matched Intent: newsletter_opt-out
Response: “To stop receiving our newsletter:
Open any email from us.
Scroll to the bottom and click on the ‘Unsubscribe’ link.
Follow the on-screen instructions to confirm your choice.
You can also manage your email preferences directly in your account settings.”
==================================================
Similarity Score for ‘How can I get a refund?’: 0.655854344367981
User Query: How can I get a refund?
Matched Intent: refund_policy
Response: “Refunds are available according to our Refund Policy. Generally, we offer refunds within 3 days of purchase for qualifying issues. Also, Please
be aware that there is a 7 days money-back guarantee for monthly subscription and 14-days money-back guarantee for yearly subscription after which refund will not be processed. Please review the full refund policy and contact us if you believe you’re eligible for a refund.”
==================================================
Similarity Score for ‘I want to list my product, can you help?’: 0.3601275384426117
User Query: I want to list my product, can you help?
Matched Intent: ask_about_listing
Response: “If you have any specific questions about a listing on our site, you can:
Visit the listing’s page to view detailed information.
Use our Contact Form to inquire about specific tools or products.
If you’re logged in, you can also chat with a support agent for more personalized responses.”
==================================================
Similarity Score for ‘I want to place an Ad, how do I go about it?’: 0.660393476486206
User Query: I want to place an Ad, how do I go about it?
Matched Intent: advertise
Response: “We offer several advertising options, including banner ads and sponsored listings. To learn more, please visit our Advertising Page where you can find guidelines, rates, and specifications. For custom advertising solutions, feel free to reach out to our sales team.”
Step 5: Deploying the Chatbot
With our chatbot functioning well, the final step was to deploy it for use on our AI tool listing website. I chose to deploy the chatbot backend on Heroku and integrate it with our WordPress frontend.
Backend Deployment
I created a Flask API for our chatbot backend (This is a sample):
from flask import Flask, request, jsonify from flask_cors import CORS # Import other necessary libraries and functions app = Flask(__name__) CORS(app) @app.route('/chatbot', methods=['POST']) def chatbot_response(): user_message = request.json['message'] best_intent, response = get_best_answer(user_message, faq_df, faq_embeddings) return jsonify({ 'user_input': user_message, 'matched_question': best_intent, 'response': response }) if __name__ == '__main__': app.run(debug=True)
I then deployed this Flask app to Heroku, making it accessible via a URL.
Frontend Integration
On my WordPress site, I created a simple chat interface that sends user messages to our Heroku-hosted API backend and displays the responses.
Testing the Deployed Chatbot
To ensure everything was working correctly locally, I tested the deployed chatbot using curl commands:
curl -X POST http://localhost:5000/chatbot -H "Content-Type: application/json" -d "{\"message\":\"I want to submit blog post, how do i go about it?\"}"
The chatbot responded accurately, demonstrating that our deployment was successful.
Sample Results
$ curl -X POST http://localhost:5000/chatbot -H "Content-Type: application/json" -d "{\"message\":\"I want to submit blog post, how do i go about it?\"}"
{“matched_question”:”I want to submit blog post, how do i go about it?”,”response”:”\”Premium Members can contribute to our blog by submitting posts on AI-related topics:\nVisit the Submit a Post page.\nFill in your article title, content, and any supporting media.\”\nSubmit your draft for review. Once approved, your post will be published on the blog.\nMake sure to follow our content guidelines for posts.\””,”user_input”:”I want to submit blog post, how do i go about it?”}
$ curl -X POST http://localhost:5000/chatbot -H "Content-Type: application/json" -d "{\"message\":\"What is the membership pricing?\"}"
{“matched_question”:”What is the membership pricing?”,”response”:”\”Our membership pricing starts at basic price, with premium options available. You can view all membership tiers and pricing details by visiting our Membership Pricing Page. If you have any specific questions, feel free to ask.\””,”user_input”:”What is the membership pricing?”}
$ curl -X POST http://localhost:5000/chatbot -H "Content-Type: application/json" -d "{\"message\":\"I do not want to recive Newsletter anymore, I want out\"}"
{“matched_question”:”I do not want to recive Newsletter anymore, I want out”,”response”:” \”To stop receiving our newsletter:\nOpen any email from us.\nScroll to the bottom and click on the ‘Unsubscribe’ link.\nFollow the on-screen instructions to confirm your choice.\nYou can also manage your email preferences directly in your account settings.\””,”user_input”:”I do not want to recive Newsletter anymore, I want out”}
Conclusion
Building this AI-powered customer support chatbot was an exciting and educational journey. By leveraging transformer models and NLP techniques, I created a chatbot that can effectively handle a wide range of customer queries, enhancing the user experience on my AI tool listing website.
Key takeaways from this project:
1. The importance of a comprehensive and well-structured dataset
2. The superiority of advanced NLP models like Sentence-BERT over simple keyword matching
3. The need for thorough testing with diverse queries
4. The process of deploying a machine learning model as a web service
While our chatbot is now functional, there’s always room for improvement. Future enhancements could include:
– Connecting to our website’s database for real-time information retrieval
– Implementing multi-turn conversations for more complex interactions
– Adding a feedback mechanism to continuously improve the chatbot’s responses
Remember, the key to a successful chatbot lies not just in the technology, but also in understanding your users’ needs and continuously refining your solution based on real-world usage.
Have fun with similar projects!