chatbot

Dissecting ChatGPT – Is this new AI tool a game changer?
Hudaiba Soomro
| December 14, 2022

ChatGPT is being hailed across the globe for disrupting major jobs and businesses. In this blog, we see how much of that hype is fair. 

After raging headlines like “Google is done” and “The college essay is dead”, ChatGPT is busy churning sonnets and limericks about its downtime caused due to heavy traffic. The news spreading like wildfire around town is that it will bring an end to jobs from insurance agents to court reporters. Let’s dive in and assess how much of the hype is true. 

 

chatgpt
ChatGPT – Data Science Dojo

Did ChatGPT kill the essay? 

 

OpenAI’s latest release large learning model (LLM), ChatGPT claims to provide natural and conversational communication. It also claims to assist with providing advice, information, performing writing and coding tasks, and admitting mistakes. Naturally, people across the globe have been bombarding the bot with requests to check how great it really is. 

 

Let’s consider the “death of the college essay“. The first read will show well-written essays to subjects on nearly anything. Consider, for example, the academic essay on theories of nationalism being hailed as a “solid A- essay”. However, a closer look shows that this AI tool works by using existing templates and so, college essays are churned out as per five-paragraph formulas.  

 

chatgpt essay
ChatGPT essay

 

These academic essays also lack the sophistication provided by critical thinking skills. They reproduce existing content online and refashion it to fit a specific template. In style, they are dreadfully dull, lacking stylistic human expressions.

Similarly, ChatGPT’s poetic output conveys a similar emulation of formulas being rewired, with technical obeyance of rhyme scheme, while a lack of ingenuity is evident.  

 

An obvious conclusion, then, appears that, while great at reorganizing text to fit templates, is deeply unaware of what it means. This comes as no surprise to those familiar with even a rudimentary understanding of natural language processing and its applications.

The function of large learning models (LLM) is far from epistemological and is rather based on identifying patterns and replicating them.  

 

Chatgpt Sonnet
ChatGPT sonnet

 

 

Here, it should be noted that AI tools such as it can be used as tools for humans to perform routinized, well-formulated tasks such as producing well-structured poetry or college essays. However, they lack the essential key insights provided via human intelligence regardless of the field of study. 

 

Is ChatGPT a source of information or misinformation? 

 

A feature that allows ChatGPT’s performance across a range of writing tasks is its ability to fast-fetch information. Because of its ability to fetch information immediately, it is being hailed as the end of Google. However, a few considerations regarding the differences between large learning models and search engines are important. 

For example, search engines work by hunting the web for all weblinks that are related to the search query. Their selling point here is accuracy, as they only connect you to other sources. ChatGPT, however, can provide responses to nearly any nonsensical queries.

Consider, for example, a user’s search query on designing “an electron app that is hosted on a remote server to give a desktop user notification.” As a response to this query, it came up with a completely fake method, revealing ChatGPT’s susceptibility to being a source of misinformation. 

 

chatgpt answer
ChatGPT answer

 

This tool would only admit to mistakes if prompted to do so via further inquiry, making it a rather risky tool. Opposed to this, an SEO engine would provide accurate information from original sources. This ranks the practical utility of an SEO engine far above it. This settles the debate on whether ChatGPT is to replace Google any time soon.  

 

Furthermore, ChatGPT’s ability to construct nonsensical ideas and arguments about nearly anything can make it unsafe for a first onlooker. Only a trained eye will then be capable of nitpicking factually plausible ideas from the mere fictional constructs. Here, again, the relevance of human ingenuity and intelligence is needed to ensure tools like this, are used in meaningful ways.  

 

ChatGPT’s release a signal to rethink education 

 

ChatGPT’s advances are, however, relevant in considering the value of human creative output rethinking conventional education and training at schools that rely on memorizing and reproducing routine tasks. For circumstances where these tasks or skills are deemed essential, it’s simple to enforce testing practices that prevent access to such sources.  

 

At the same time, it’s an unfair stretch to suggest that ChatGPT means the end of optimized search engines like Google and creative human tasks such as writing. At best, it can be used to assist humans in their projects, be it their daily tasks or work-related queries.

It is, at the end of the day, only a mere tool that can be integrated in a plethora of human initiatives.  

 

Final words 

With limitations ranging from verbosity in communication, inaccurate information, and an obvious lack of sophisticated opinions, ChatGPT’s performance doesn’t quite meet the hype. Similarly, instead of offering natural conversations, ChatGPT has offered boring and dull essays, even when it comes to imitating a writer’s style.

At the same time, it is a tool that can be used by trained experts to perform certain routine tasks including writing, coding, and information fetching more easily. 

 

Umair Hasan
| September 26, 2022

In this tutorial, you will learn how to create an attractive voice-controlled chatbot application with a small amount of coding in python. To build our application we’ll first create a good-looking user interface through the built-in Tkinter library in Python and then we will create some small functions to achieve our task. 

 

Here is a sneak peek of what we are going to create. 

 

Voice controlled chatbot
Voice controlled chatbot using coding in Python – Data Science Dojo

Before kicking off, I hope you already have a brief idea about web scraping, if not then read the following article talking about Python web scraping 

 

PRO-TIP: Join our 5-day instructor-led Python for Data Science training to enhance your deep learning

 

Pre-requirements for building a voice chatbot

Make sure that you are using Python 3.8+ and the following libraries are installed on it 

  • Pyttsx3 (pyttsx3 is a text-to-speech conversion library in Python) 
  • SpeechRecognition (Library for performing speech recognition) 
  • Requests (The requests module allows you to send HTTP requests using Python) 
  • Bs4 (Beautiful Soup is a library that is used to scrape information from web pages) 
  • pyAudio (With PyAudio, you can easily use Python to play and record audio) 

 

If you are still facing installation errors or incompatibility errors, then you can try downloading specific versions of the above libraries as they are tested and working currently in the application. 

 

  • Python 3.10 
  • pyttsx3==2.90 
  • SpeechRecognition==3.8.1 
  • requests==2.28.1
  • beautifulsoup4==4.11.1 
  • beautifulsoup4==4.11.1 

 

Now that we have set everything it is time to get started. Open a fresh new py file and name it VoiceChatbot.py. Import the following relevant libraries on the top of the file. 

 

  • from tkinter import * 
  • import time
  • import datetime
  • import pyttsx3
  • import speech_recognition as sr
  • from threading import Thread
  • import requests
  • from bs4 import BeautifulSoup 

 

The code is divided into the GUI section, which uses the Tkinter library of python and 7 different functions. We will start by declaring some global variables and initializing instances for text-to-speech and Tkinter. Then we start creating the windows and frames of the user interface. 

 

The user interface 

This part of the code loads images initializes global variables, and instances and then it creates a root window that displays different frames. The program starts when the user clicks the first window bearing the background image. 

 

if __name__ == “__main__”: 

 

#Global Variables 

loading = None
query = None
flag = True
flag2 = True

   

#initalizng text to speech and setting properties 

engine = pyttsx3.init() # Windows voices = engine.getProperty('voices') engine.setProperty('voice', voices[1].id) rate = engine.getProperty('rate') engine.setProperty('rate', rate-10) 

 

#loading images 

    img1= PhotoImage(file='chatbot-image.png') 
    img2= PhotoImage(file='button-green.png') 
    img3= PhotoImage(file='icon.png') 
    img4= PhotoImage(file='terminal.png') 
    background_image=PhotoImage(file="last.png") 
    front_image = PhotoImage(file="front2.png") 

 

#creating root window 

    root=Tk() 
    root.title("Intelligent Chatbot") 
    root.geometry('1360x690+-5+0')
    root.configure(background='white') 

 

#Placing frame on root window and placing widgets on the frame 

    f = Frame(root,width = 1360, height = 690) 
    f.place(x=0,y=0) 
    f.tkraise() 

 

#first window which acts as a button containing the background image 

    okVar = IntVar() 
    btnOK = Button(f, image=front_image,command=lambda: okVar.set(1)) 
    btnOK.place(x=0,y=0) 
    f.wait_variable(okVar) 
    f.destroy()     
    background_label = Label(root, image=background_image) 
    background_label.place(x=0, y=0) 

 

#Frame that displays gif image 

    frames = [PhotoImage(file='chatgif.gif',format = 'gif -index %i' %(i)) for i in range(20)] 
    canvas = Canvas(root, width = 800, height = 596) 
    canvas.place(x=10,y=10) 
    canvas.create_image(0, 0, image=img1, anchor=NW) 

 

#Question button which calls ‘takecommand’ function 

    question_button = Button(root,image=img2, bd=0, command=takecommand) 
    question_button.place(x=200,y=625) 

 

#Right Terminal with vertical scroll 

    frame=Frame(root,width=500,height=596) 
    frame.place(x=825,y=10) 
    canvas2=Canvas(frame,bg='#FFFFFF',width=500,height=596,scrollregion=(0,0,500,900)) 
    vbar=Scrollbar(frame,orient=VERTICAL) 
    vbar.pack(side=RIGHT,fill=Y) 
    vbar.config(command=canvas2.yview) 
    canvas2.config(width=500,height=596, background="black") 
    canvas2.config(yscrollcommand=vbar.set) 
    canvas2.pack(side=LEFT,expand=True,fill=BOTH) 
    canvas2.create_image(0,0, image=img4, anchor="nw") 
    task = Thread(target=main_window) 
    task.start() 
    root.mainloop() 

 

The main window functions 

This is the first function that is called inside a thread. It first calls the wishme function to wish the user. Then it checks whether the query variable is empty or not. If the query variable is empty, then it checks the contents of the query variable. If there is a shutdown or quit or stop word in query, then it calls the shutdown function, and the program exits. Else, it calls the web_scraping function. This function calls another function with the name wishme. 

 

def main_window(): 
    global query 
    wishme() 
    while True: 
        if query != None: 
            if 'shutdown' in query or 'quit' in query or 'stop' in query or 'goodbye' in query: 
                shut_down() 
                break 
            else: 
                web_scraping(query) 
                query = None 

 

The wish me function 

This function checks the current time and greets users according to the hour of the day and it also updates the canvas. The contents in the text variable are passed to the ‘speak’ function. The ‘transition’ function is also invoked at the same time in order to show the movement effect of the bot image, while the bot is speaking. This synchronization is achieved through threads, which is why these functions are called inside threads. 

 

def wishme(): 
    hour = datetime.datetime.now().hour 
    if 0 <= hour < 12: 
        text = "Good Morning sir. I am Jarvis. How can I Serve you?" 
    elif 12 <= hour < 18: 
        text = "Good Afternoon sir. I am Jarvis. How can I Serve you?" 
    else: 
        text = "Good Evening sir. I am Jarvis. How can I Serve you?" 
    canvas2.create_text(10,10,anchor =NW , text=text,font=('Candara Light', -25,'bold italic'), fill="white",width=350) 
    p1=Thread(target=speak,args=(text,)) 
    p1.start() 
    p2 = Thread(target=transition) 
    p2.start() 

 

The speak function 

This function converts text to speech using pyttsx3 engine. 

def speak(text): 
    global flag 
    engine.say(text) 
    engine.runAndWait() 
    flag=False 

 

The transition functions 

The transition function is used to create the GIF image effect, by looping over images and updating them on canvas. The frames variable contains a list of ordered image names.  

 

def transition(): 
    global img1 
    global flag 
    global flag2 
    global frames 
    global canvas 
    local_flag = False 
    for k in range(0,5000): 
        for frame in frames: 
            if flag == False: 
                canvas.create_image(0, 0, image=img1, anchor=NW) 
                canvas.update() 
                flag = True 
                return 
            else: 
                canvas.create_image(0, 0, image=frame, anchor=NW) 
                canvas.update() 
                time.sleep(0.1) 

 

The web scraping function 

This function is the heart of this application. The question asked by the user is then searched on google using the ‘requests’ library of python. The ‘beautifulsoap’ library extracts the HTML content of the page and checks for answers in four particular divs. If the webpage does not contain any of the four divs, then it searches for answers on Wikipedia links, however, if that is also not successful, then the bot apologizes.  

 

def web_scraping(qs): 
    global flag2 
    global loading 
    URL = 'https://www.google.com/search?q=' + qs 
    print(URL) 
    page = requests.get(URL) 
    soup = BeautifulSoup(page.content, 'html.parser') 
    div0 = soup.find_all('div',class_="kvKEAb") 
    div1 = soup.find_all("div", class_="Ap5OSd") 
    div2 = soup.find_all("div", class_="nGphre") 
    div3  = soup.find_all("div", class_="BNeawe iBp4i AP7Wnd") 

    links = soup.findAll("a") 
    all_links = [] 
    for link in links: 
       link_href = link.get('href') 
       if "url?q=" in link_href and not "webcache" in link_href: 
           all_links.append((link.get('href').split("?q=")[1].split("&sa=U")[0])) 

    flag= False 
    for link in all_links: 
       if 'https://en.wikipedia.org/wiki/' in link: 
           wiki = link 
           flag = True 
           break
    if len(div0)!=0: 
        answer = div0[0].text 
    elif len(div1) != 0: 
       answer = div1[0].text+"\n"+div1[0].find_next_sibling("div").text 
    elif len(div2) != 0: 
       answer = div2[0].find_next("span").text+"\n"+div2[0].find_next("div",class_="kCrYT").text 
    elif len(div3)!=0: 
        answer = div3[1].text 
    elif flag==True: 
       page2 = requests.get(wiki) 
       soup = BeautifulSoup(page2.text, 'html.parser') 
       title = soup.select("#firstHeading")[0].text
       paragraphs = soup.select("p") 
       for para in paragraphs: 
           if bool(para.text.strip()): 
               answer = title + "\n" + para.text 
               break 
    else: 
        answer = "Sorry. I could not find the desired results"
    canvas2.create_text(10, 225, anchor=NW, text=answer, font=('Candara Light', -25,'bold italic'),fill="white", width=350) 
    flag2 = False 
    loading.destroy()
    p1=Thread(target=speak,args=(answer,)) 
    p1.start() 
    p2 = Thread(target=transition) 
    p2.start() 

 

The take command function 

This function is invoked when the user clicks the green button to ask any question. The speech recognition library listens for 5 seconds and converts the audio input to text using google recognize API. 

 

def takecommand(): 
    global loading 
    global flag 
    global flag2 
    global canvas2 
    global query 
    global img4 
    if flag2 == False: 
        canvas2.delete("all") 
        canvas2.create_image(0,0, image=img4, anchor="nw")  
    speak("I am listening.") 
    flag= True 
    r = sr.Recognizer() 
    r.dynamic_energy_threshold = True 
    r.dynamic_energy_adjustment_ratio = 1.5 
    #r.energy_threshold = 4000 
    with sr.Microphone() as source: 
        print("Listening...") 
        #r.pause_threshold = 1 
        audio = r.listen(source,timeout=5,phrase_time_limit=5) 
        #audio = r.listen(source) 
 
    try: 
        print("Recognizing..") 
        query = r.recognize_google(audio, language='en-in') 
        print(f"user Said :{query}\n") 
        query = query.lower() 
        canvas2.create_text(490, 120, anchor=NE, justify = RIGHT ,text=query, font=('fixedsys', -30),fill="white", width=350) 
        global img3 
        loading = Label(root, image=img3, bd=0) 
        loading.place(x=900, y=622) 
 
    except Exception as e: 
        print(e) 
        speak("Say that again please") 
        return "None"

 

The shutdown function 

This function farewells the user and destroys the root window in order to exit the program. 

def shut_down(): 
    p1=Thread(target=speak,args=("Shutting down. Thankyou For Using Our Sevice. Take Care, Good Bye.",)) 
    p1.start() 
    p2 = Thread(target=transition) 
    p2.start() 
    time.sleep(7) 
   root.destroy()

 

Conclusion 

It is time to wrap up, I hope you enjoyed our little application. This is the power of Python, you can create small attractive applications in no time with a little amount of code. Keep following us for more cool python projects! 

 

Code - CTA

 

In the second article of this chatbot series, learn how to build a rule-based chatbot and discuss the business applications of them.

Chatbots have become extremely popular in recent years and their use in the industry has skyrocketed. They have found a strong foothold in almost every task that requires text-based public dealing. They have become so critical in the support industry, for example, that almost 25% of all customer service operations are expected to use them by 2020.

In the first part of A Beginners Guide to Chatbotswe discussed what chatbots were, their rise to popularity and their use-cases in the industry. We also saw how the technology has evolved over the past 50 years.

In this second part of the series, we’ll be taking you through how to build a simple Rule-based chatbot in Python. Before we start with the tutorial, we need to understand the different types of chatbots and how they work.

Types of chatbots

Chatbots can be classified into two different types, based on how they are built:

Rule-based Chatbots

Rule-based chatbots are pretty straight forward. They are provided with a database of responses and are given a set of rules that help them match out an appropriate response from the provided database. They cannot generate their own answers but with an extensive database of answers and smartly designed rules, they can be very productive and useful.

The simplest form of Rule-based Chatbots have one-to-one tables of inputs and their responses. These bots are extremely limited and can only respond to queries if they are an exact match with the inputs defined in their database.

AI-based chatbots

With the rise in the use of machine learning in recent years, a new approach to building chatbots has emerged. Using artificial intelligence, it has become possible to create extremely intuitive and precise chatbots tailored to specific purposes.

Unlike their rule-based kin, AI based chatbots are based on complex machine learning models that enable them to self-learn.

Now that we’re familiar with how chatbots work, we’ll be looking at the libraries that will be used to build our simple Rule-based Chatbot.

Natural Language Toolkit (NLTK)

Natural Language Toolkit is a Python library that makes it easy to process human language data. It provides easy-to-use interfaces to many language-based resources such as the Open Multilingual Wordnet, as well as access to a variety of text-processing libraries.

Regular Expression (RegEx) in Python

regular expression is a special sequence of characters that helps you search for and find patterns of words/sentences/sequence of letters in sets of strings, using a specialized syntax. They are widely used for text searching and matching in UNIX.

Python includes support for regular expression through the re package.

Want to upgrade your Python abilities? Checkout Data Science Dojo’s Introduction to Python for Data Science.

Building a chatbot

This very simple rule based chatbot will work by searching for specific keywords in inputs given by a user. The keywords will be used to understand what action the user wants to take (user’s intent). Once the intent is identified, the bot will then pick out a response appropriate to the intent.

 

Keyword intent

The list of keywords the bot will be searching for and the dictionary of responses will be built up manually based on the specific use case for the chatbot.

We’ll be designing a very simple chatbot for a Bank. The bot will be able to respond to greetings (Hi, Hello etc.) and will be able to answer questions about the bank’s hours of operation.

A flow of how the chatbot would process inputs is shown below;

 

Python chatbot process
Flow of how the chatbot will process

We will be following the steps below to build our chatbot

  1. Importing Dependencies
  2. Building the Keyword List
  3. Building a dictionary of Intents
  4. Defining a dictionary of responses
  5. Matching Intents and Generating Responses

Importing dependencies

The first thing we’ll need to do is import the packages/libraries we’ll be using. re is the package that handles regular expression in Python. We’ll also be using WordNet from NLTK. WordNet is a lexical database that defines semantical relationships between words. We’ll be using WordNet to build up a dictionary of synonyms to our keywords. This will help us expand our list of keywords without manually having to introduce every possible word a user could use.

# Importing modules
import re
from nltk.corpus import wordnet

Building a list of keywords

Once we have imported our libraries, we’ll need to build up a list of keywords that our chatbot will look for. This list can be as exhaustive as you want. The more keywords you have, the better your chatbot will perform.

As discussed previously, we’ll be using WordNet to build up a dictionary of synonyms to our keywords. For details about how WordNet is structured, visit their website.

Code:

# Building a list of Keywords
list_words=['hello','timings']
list_syn={}
for word in list_words:
    synonyms=[]
    for syn in wordnet.synsets(word):
        for lem in syn.lemmas():
            # Remove any special characters from synonym strings
            lem_name = re.sub('[^a-zA-Z0-9 \n\.]', ' ', lem.name())
            synonyms.append(lem_name)
    list_syn[word]=set(synonyms)
print (list_syn)

Output:

hello
{'hello', 'howdy', 'hi', 'hullo', 'how do you do'}
timings
{'time', 'clock', 'timing'}

Here, we first defined a list of words list_words that we will be using as our keywords. We used WordNet to expand our initial list with synonyms of the keywords. This list of keywords is stored in list_syn.

New keywords can simply be added to list_words. The chatbot will automatically pull their synonyms and add them to the keywords dictionary. You can also edit list_syn directly if you want to add specific words or phrases that you know your users will use.

Building a dictionary of intents

Once our keywords list is complete, we need to build up a dictionary that matches our keywords to intents. We also need to reformat the keywords in a special syntax that makes them visible to Regular Expression’s search function.

Code:

# Building dictionary of Intents & Keywords
keywords={}
keywords_dict={}
# Defining a new key in the keywords dictionary
keywords['greet']=[]
# Populating the values in the keywords dictionary with synonyms of keywords formatted with RegEx metacharacters 
for synonym in list(list_syn['hello']):
    keywords['greet'].append('.*\\b'+synonym+'\\b.*')

# Defining a new key in the keywords dictionary
keywords['timings']=[]
# Populating the values in the keywords dictionary with synonyms of keywords formatted with RegEx metacharacters 
for synonym in list(list_syn['timings']):
    keywords['timings'].append('.*\\b'+synonym+'\\b.*')
for intent, keys in keywords.items():
    # Joining the values in the keywords dictionary with the OR (|) operator updating them in keywords_dict dictionary
    keywords_dict[intent]=re.compile('|'.join(keys))
print (keywords_dict)

Output:

{'greet': re.compile('.*\\bhello\\b.*|.*\\bhowdy\\b.*|.*\\bhi\\b.*|.*\\bhullo\\b.*|.*\\bhow-do-you-do\\b.*'), 'timings': re.compile('.*\\btime\\b.*|.*\\bclock\\b.*|.*\\btiming\\b.*')}

The updated and formatted dictionary is stored in keywords_dict. The intent is the key and the string of keywords is the value of the dictionary.

Let’s look at one key-value pair of the keywords_dict dictionary to understand the syntax of Regular Expression;

{'greet': re.compile('.*\\bhullo\\b.*|.*\\bhow-do-you-do\\b.*|.*\\bhowdy\\b.*|.*\\bhello\\b.*|.*\\bhi\\b.*')

Regular Expression uses specific patterns of special Meta-Characters to search for strings or sets of strings in an expression.

Since we need our chatbot to search for specific words in larger input strings we use the following sequences of meta-characters:

.*\\bhullo\\b.*

In this specific sequence, the keyword (hullo) is encased between a \b sequence. This tells the RegEx Search function that the search parameter is the keyword (hullo).

The first sequence \bhullo\b is encased between a period-star .* sequence. This sequence tells the RegEx Search function to search the entire input string from beginning to end for the search parameter (hullo).

In the dictionary, multiple such sequences are separated by the OR | operator. This operator tells the search function to look for any of the mentioned keywords in the input string.

More details about Regular Expression and its syntax can be found here.

You can add as many key-value pairs to the dictionary as you want to increase the functionality of the chatbot.

Defining responses

The next step is defining responses for each intent type. This part is very straightforward. The responses are described in another dictionary with the intent being the key.

We’ve also added a fallback intent and its response. This is a fail-safe response in case the chatbot is unable to extract any relevant keywords from the user input.

Code:

# Building a dictionary of responses
responses={
    'greet':'Hello! How can I help you?',
    'timings':'We are open from 9AM to 5PM, Monday to Friday. We are closed on weekends and public holidays.',
    'fallback':'I dont quite understand. Could you repeat that?',
}

Matching intents and generating responses

Now that we have the back-end of the chatbot completed, we’ll move on to taking input from the user and searching the input string for our keywords.

We use the RegEx Search function to search the user input for keywords stored in the value field of the keywords_dict dictionary.  If you recall, the values in the keywords_dict dictionary were formatted with special sequences of meta-characters. RegEx’s search function uses those sequences to compare the patterns of characters in the keywords with patterns of characters in the input string.

If a match is found, the current intent gets selected and is used as the key to the responses dictionary to select the correct response.

Code:

print ("Welcome to MyBank. How may I help you?")
# While loop to run the chatbot indefinetely
while (True):  
    # Takes the user input and converts all characters to lowercase
    user_input = input().lower()
    # Defining the Chatbot's exit condition
    if user_input == 'quit': 
        print ("Thank you for visiting.")
        break    
    matched_intent = None 
    for intent,pattern in keywords_dict.items():
        # Using the regular expression search function to look for keywords in user input
        if re.search(pattern, user_input): 
            # if a keyword matches, select the corresponding intent from the keywords_dict dictionary
            matched_intent=intent  
    # The fallback intent is selected by default
    key='fallback' 
    if matched_intent in responses:
        # If a keyword matches, the fallback intent is replaced by the matched intent as the key for the responses dictionary
        key = matched_intent
    # The chatbot prints the response that matches the selected intent
    print (responses[key]) 

The chatbot picked the greeting from the first user input (‘Hi’) and responded according to the matched intent. The same happened when it located the word (‘time’) in the second user input. The third user input (‘How can I open a bank account’) didn’t have any keywords that present in Bankbot’s database and so it went to its fallback intent.

You can add as many keywords/phrases/sentences and intents as you want to make sure your chatbot is robust when talking to an actual human.

Conclusion

This blog was a hands-on introduction to building a very simple rule-based chatbot in python. We only worked with 2 intents in this tutorial for simplicity. You can easily expand the functionality of this chatbot by adding more keywords, intents and responses.

As we saw, building a rule-based chatbot is a laborious process. In a business environment, a chatbot could be required to have a lot more intent depending on the tasks it is supposed to undertake.

In such a situation, rule-based chatbots become very impractical as maintaining a rule base would become extremely complex. In addition, the chatbot would severely be limited in terms of its conversational capabilities as it is near impossible to describe exactly how a user will interact with the bot.

AI-based Chatbots are a much more practical solution for real-world scenarios. In the next blog in the series, we’ll be looking at how to build a simple AI-based Chatbot in Python.

Do you want to learn more about machine learning and it’s applications? Check out Data Science Dojo’s online data science certificate program!

Usman Shahid
| September 15, 2020

Explore Google DialogFlow, a conversational AI Platform and use it to build a smart, contextually aware Chatbot.

Chatbots have become extremely popular in recent years and their use in the e-commerce industry has skyrocketed. They have found a strong foothold in almost every task that requires text-based public dealing. They have become so critical with customer support, for example that almost 25% of all customer service operations are expected to use them by the end of 2020.

Building a comprehensive and production-ready chatbot from scratch, however, is an almost impossible task. Tech companies like Google and Amazon have been able to achieve this feat after spending years and billions of dollars in research, something that not everyone with a use for a chatbot can afford.

Luckily, almost every player in the tech market (including Google and Amazon) allows businesses to purchase their technology platforms to design customized chatbots for their own use. These platforms have pre-trained language models and easy-to-use interfaces that make it extremely easy for new users to set up and deploy customized chatbots in no time.

In the previous blogs in our series on chatbots, we talked about how to build AI and rule based chatbots in Python. In this blog, we’ll be taking you through how to build a simple AI chatbot using Google’s DialogFlow:

Intro to Google DialogFlow

DialogFlow is a natural language understanding platform (based on Google’s AI) that makes it easy to design and integrate a conversational user interface into your mobile app, web application, device, bot, interactive voice response system, and so on. Using DialogFlow, you can provide new and engaging ways for users to interact with your product.

Fundamentals of DialogFlow

We’re going to run through some of the basics of DialogFlow just so that you understand the vernacular when we build our chatbot.

PRO TIP: Join our data science bootcamp program today to enhance your NLP skills!

Agents

An Agent is what DialogFlow calls your chatbot. A DialogFlow Agent is a trained generative machine learning model that understands natural language flows and the nuances of human conversations. DialogFlow translates input text during a conversation to structured data that your apps and services can understand.

Intents

Intents are the starting point of a conversation in DialogFlow. When a user starts a conversation with a chatbot, DialogFlow matches the input to the best intent available.

A chatbot can have as many intents as required depending on the level of conversational detail a user wants the bot to have. Each intent has the following parameters:

  • Training Phrases: These are examples of phrases your chatbot might receive as inputs. When a user input matches one of the phrases in the intent, that specific intent is called. Since all DialogFlow agents use machine learning, you don’t have to define every possible phrase your users might use. DialogFlow automatically learns and expands this list as users interact with your bot.
  • Parameters: These are input variables extracted from a user input when a specific intent is called. For example, a user might say: “I want to schedule a haircut appointment on Saturday.” In this situation, “haircut appointment” and “Saturday” could be the possible parameters DialogFlow would extract from the input. Each parameter has a type, like a data type in normal programming, called an Entity. You need to define what parameters you would be expecting in each intent. Parameters can be set to “required”. If a required parameter is not present in the input, DialogFlow will specifically ask the user for it.
  • Responses: These are the responses DialogFlow returns to the users when an Intent is matched. They may provide answers, ask the user for more information or serve as conversation terminators.

Entities

Entities are information types of intent parameters which control how data from an input is extracted. They can be thought of as data types used in programming languages. DialogFlow includes many pre-defined entity types corresponding to common information types such as dates, times, days, colors, email addresses etc.

You can also define custom entity types for information that may be specific to your use case. In the example shared above, the “appointment type” would be an example of a custom entity.

Contexts

DialogFlow uses contexts to keep track of where users are in a conversation. During the flow of a conversation, multiple intents may need to be called. DialogFlow uses contexts to carry a conversation between them. To make an intent follow on from another intent, you would create an output context from the first intent and place the same context in the input context field of the second intent.

In the example shared above, the conversation might have flowed in a different way.

In this specific conversation, the agent is performing 2 different tasks: authentication and booking.

When the user initiates the conversation, the Authentication Intent is called that verifies the user’s membership number. Once that has been verified, the Authentication Intent activates the Authentication Context and the Booking Intent is called.

In this situation, the Booking Intent knows that the user is allowed to book appointments because the Authentication Context is active. You can create and use as many contexts as you want in a conversation for your use case.

Conversation in a DialogFlow hatbot

A conversation with a DialogFlow Agent flows in the following way:

Chatbot Conversation Text
An example of DialogFlow Chatbot Conversation

Building a chatbot

In this tutorial, we’ll be building a simple customer services agent for a bank. The chatbot (named BankBot) will be able to:

  1. Answer Static Pre-Defined Queries
  2. Set up an appointment with a Customer Services Agent

Creating a new DialogFlow agent

It’s extremely easy to get started with DialogFlow. The first thing you’ll need to do is log in to DialogFlow. To do that, go to https://dialogflow.cloud.google.com and login with your Google Account (or create one if you don’t have it).

Once you’re logged in, click on ‘Create Agent’ and give it a name.

INterface
DialogFlow Interface

1. Answering static defined series

To keep things simple, we’ll be focusing on training BankBot to respond to one static query initially; responding to when a user asks the Bank’s operational timings. For this we will teach BankBot a few phrases that it might receive as inputs and their corresponding responses.

Creating an intent

The first thing we’ll do is create a new Intent. That can be done by clicking on the ‘+’ sign next to the ‘Intents’ tab on the left side panel. This intent will specifically be for answering queries about our bank’s working hours. Once on the ‘Create Intent’ Screen (as shown below), fill in the ‘Intent Name’ field.

Text Fields
Intent Name Fields

Training phrases

Once the intent is created, we need to teach BankBot what phrases to look for. A list of sample phrases needs to be entered under ‘Training Phrases’. We don’t need to enter every possible phrase as BankBot will keep on learning from the inputs it receives thanks to Google’s machine learning.

Training Phrases
Adding Training Phrases

Responses

After the training phrases, we need to tell BankBot how to respond if this intent is matched. Go ahead and type in your response in the ‘Responses’ field.

responses | Data Science Dojo

DialogFlow allows you to customize your responses based on the platform (Google Assistant, Facebook Messenger, Kik, Slack etc.) you will be deploying your chatbot on.

Once you’re happy with the response, go ahead and save the Intent by clicking on the Save button at the top.

Training Phrases
Training Phrases with Actions and Parameters

Testing the intent

Once you’ve saved your intent, you can see how its working right within DialogFlow.

To test BankBot, type in any user query in the text box labeled ‘Try it Now’.

Testing Phase
Testing the Intent Example

2. Setting an appointment

Getting BankBot to set an appointment is mostly the same as answering static queries, with one extra step. To book an appointment, BankBot will need to know the date and time the user wants the appointment for. This can be done by teaching BankBot to extract this information from the user query – or to ask the user for this information in-case it is not provide in the initial query.

Creating an intent

This process will be the same as how we created an intent in the previous example.

Training phrases

This will also be same as in the previous example except for one important difference.  In this situation, there are 3 distinct ways in which the user can structure his initial query:

  1. Asking for an appointment without mentioning the date or time in the initial query.
  2. Asking for an appointment with just the date mentioned in the initial query.
  3. Asking for an appointment with both the date and time mentioned in the initial query.

We’ll need to make sure to add examples of all 3 cases in our Training Phrases.  We don’t need to enter every possible phrase as BankBot will keep on learning from the inputs it receives.

Training Phrases
Adding Training Phrases

Parameters

BankBot will need additional information (the date and time) to book an appointment for the user. This can be done by defining the date and time as ‘Parameters’ in the Intent.

For every defined parameter, DialogFlow requires the following information:

  1. Required: If the parameter is set to ‘Required’, DialogFlow will prompt the user for information if it has not been provided in the original query.
  2. Parameter Name: Name of the parameter.
  3. Entity: The type of data/information that will be stored in the parameter.
  4. Value: The variable name that will be used to reference the value of this parameter in ‘Responses.’
  5. Prompts: The response to be used in-case the parameter has not been provided in the original query.
Actions parameters dialog box
Adding Actions and Parameters

DialogFlow automatically extracts any parameters it finds in user inputs (notice that the time and date information in the training phrases has automatically been color-coded according to the parameters).

Responses

After the training phrases, we need to tell BankBot how to respond if this intent is matched. Go ahead and type in your response in the ‘Responses’ field.

responses dialog box
Adding Text Responses

DialogFlow allows you to customize your responses based on the platform (Google Assistant, Facebook Messenger, Kik, Slack etc.) you will be deploying your chatbot on.
Once you’re happy with the response, go ahead and save the Intent by clicking on the Save button at the top.

Testing the intent

Once you’ve saved your intent, you can see how its working right within DialogFlow.

To test BankBot, type in any user query in the text box labeled ‘Try it Now’

Example 1: All parameters present in the initial query.

Text box
An Example of Testing an Intent

Example 2: When complete information is not present in the initial query.

Some Text
DialogFlow Chatbot Conversation Example

Conclusion

DialogFlow has made it exceptionally easy to build extremely functional and fully customizable chatbots with little effort. The purpose of this tutorial was to give you an introduction to building chatbots and to help you get familiar with the foundational concepts of the platform.

Other Conversational AI tools use almost the same concepts as were discussed, so these should be transferable to any platform.

Related Topics

Web Development
Top
Statistics
Software Testing
Programming Language
Podcasts
Natural Language
Machine Learning
Hypothesis Testing
High-Tech
Events
Discussions
Demos
Data Visualization
Data Security
Data Science
Data Mining
Data Engineering
Data Analytics
Conferences

Up for a Weekly Dose of Data Science?

Subscribe to our weekly newsletter & stay up-to-date with current data science news, blogs, and resources.