Creating Your Own Chatbot in Python with Deep Learning Techniques
Written on
Chapter 1: Introduction to Chatbots
Artificial Intelligence is increasingly being integrated into various business workflows, facilitated by advancements in Natural Language Processing (NLP) and Machine Learning (ML). These technologies enable the creation of systems that can mimic human-like interactions, including understanding spoken and written language.
In this article, we will construct a chatbot utilizing NLP and neural networks in Python.
Section 1.1: Setting Up the Data
Before diving into coding, we need to establish a JSON file that outlines the various intents that might arise during user interactions with our chatbot. This involves creating tags representing user inquiries.
For instance:
- If a user wants to know the chatbot's name, we define an intent tagged as "name."
- If the user inquires about the chatbot's age, we tag that intent as "age."
For each tag, we need to identify multiple patterns—these are the different ways users might phrase their questions. For example, under the "name" tag, users may ask, "What’s your name?" or "Who are you?"
The chatbot will utilize these patterns as training data to learn how to respond appropriately. This means users can ask questions in various ways, and the chatbot will still understand their intent.
Note: While the chatbot will not possess advanced intelligence, it can effectively interpret a variety of queries with sufficient examples.
Within the intents JSON file, each intent includes associated responses. However, for our basic chatbot, these responses will be static and will not change dynamically based on user input.
data = {
"intents": [
{
"tag": "greeting",
"patterns": ["Hello", "How are you?", "Hi there", "Hi", "What's up"],
"responses": ["Howdy Partner!", "Hello", "How are you doing?", "Greetings!", "How do you do?"],
},
{
"tag": "age",
"patterns": ["how old are you?", "when is your birthday?", "when were you born?"],
"responses": ["I am 24 years old", "I was born in 1996", "My birthday is July 3rd and I was born in 1996", "03/07/1996"]
},
{
"tag": "date",
"patterns": ["what are you doing this weekend?", "do you want to hang out sometime?", "what are your plans for this week"],
"responses": ["I am available all week", "I don't have any plans", "I am not busy"]
},
{
"tag": "name",
"patterns": ["what's your name?", "what are you called?", "who are you?"],
"responses": ["My name is Kippi", "I'm Kippi", "Kippi"]
},
{
"tag": "goodbye",
"patterns": ["bye", "g2g", "see ya", "adios", "cya"],
"responses": ["It was nice speaking to you", "See you later", "Speak soon!"]
}
]
}
Section 1.2: Implementing the Code
To develop our chatbot, we will utilize Python libraries that are well-suited for NLP, deep learning, and array manipulation.
import json
import string
import random
import nltk
import numpy as np
from nltk.stem import WordNetLemmatizer
import tensorflow as tf
from tensorflow.keras import Sequential
from tensorflow.keras.layers import Dense, Dropout
nltk.download("punkt")
nltk.download("wordnet")
To prepare our training data, we must take the following steps:
- Construct a vocabulary from the words used in the defined patterns.
- Create a list of tags corresponding to each intent.
- Compile all patterns and associate them with their respective tags.
Let's implement this in Python:
lemmatizer = WordNetLemmatizer()
words = []
classes = []
doc_X = []
doc_y = []
for intent in data["intents"]:
for pattern in intent["patterns"]:
tokens = nltk.word_tokenize(pattern)
words.extend(tokens)
doc_X.append(pattern)
doc_y.append(intent["tag"])
if intent["tag"] not in classes:
classes.append(intent["tag"])
words = [lemmatizer.lemmatize(word.lower()) for word in words if word not in string.punctuation]
words = sorted(set(words))
classes = sorted(set(classes))
Now that we have organized our data, we can train our model. However, since neural networks require numerical inputs, we must convert our data accordingly using a technique known as the bag of words.
training = []
out_empty = [0] * len(classes)
for idx, doc in enumerate(doc_X):
bow = []
text = lemmatizer.lemmatize(doc.lower())
for word in words:
bow.append(1) if word in text else bow.append(0)output_row = list(out_empty)
output_row[classes.index(doc_y[idx])] = 1
training.append([bow, output_row])
random.shuffle(training)
training = np.array(training, dtype=object)
train_X = np.array(list(training[:, 0]))
train_y = np.array(list(training[:, 1]))
With our data prepared, we can now construct a neural network model to process our training data.
input_shape = (len(train_X[0]),)
output_shape = len(train_y[0])
epochs = 200
model = Sequential()
model.add(Dense(128, input_shape=input_shape, activation="relu"))
model.add(Dropout(0.5))
model.add(Dense(64, activation="relu"))
model.add(Dropout(0.3))
model.add(Dense(output_shape, activation="softmax"))
adam = tf.keras.optimizers.Adam(learning_rate=0.01, decay=1e-6)
model.compile(loss='categorical_crossentropy', optimizer=adam, metrics=["accuracy"])
print(model.summary())
model.fit(x=train_X, y=train_y, epochs=200, verbose=1)
In our sequential model, dropout layers are implemented to prevent overfitting, enhancing the model's performance on unseen data.
Now, we need to create features for our chatbot to utilize the trained model effectively. This involves setting up a loop for user input, cleaning the input, and predicting the corresponding intent.
while True:
message = input("")
intents = pred_class(message, words, classes)
result = get_response(intents, data)
print(result)
Let's take a look at our chatbot in action…
Chapter 2: Conclusion
You’ve learned how to develop a basic chatbot in Python leveraging deep learning and NLP techniques. While this chatbot is not sophisticated enough for professional use, it serves as a great starting point for anyone interested in conversational AI. If you create a chatbot using this guide, feel free to share your results with me on LinkedIn or Twitter.
Thank you for reading! Stay connected for more insights on Data Science, Artificial Intelligence, and Freelancing.
This video tutorial covers how to create a chatbot using PyTorch for NLP and deep learning techniques.
This tutorial provides a comprehensive guide on building a chatbot using Python and deep learning methodologies.