Difference between revisions of "Python-for-Automation/C3/Building-and-Training-a-Chatbot/English"

From Script | Spoken-Tutorial
Jump to: navigation, search
(Created page with " {| border="1" |- || '''Visual Cue''' || '''Narration''' |- |- style="border:1pt solid #000000;padding:0.176cm;" || Show Slide: '''Welcome''' || Hello and welcome to the Spok...")
 
Line 5: Line 5:
 
|| '''Narration'''
 
|| '''Narration'''
 
|-
 
|-
|- style="border:1pt solid #000000;padding:0.176cm;"
+
 
 
|| Show Slide:
 
|| Show Slide:
  
 
'''Welcome'''
 
'''Welcome'''
 
|| Hello and welcome to the Spoken Tutorial on '''“Building and Training a ChatBot”'''  
 
|| Hello and welcome to the Spoken Tutorial on '''“Building and Training a ChatBot”'''  
|- style="border:1pt solid #000000;padding:0.176cm;"
+
|-  
 
|| Show Slide:
 
|| Show Slide:
  
 
'''Learning Objectives'''
 
'''Learning Objectives'''
 
|| In this tutorial, we will learn to  
 
|| In this tutorial, we will learn to  
* <div style="margin-left:1.27cm;margin-right:0cm;">Create an auto-updatable Q n A database for a chatbot</div>
+
* Create an auto-updatable Q n A database for a chatbot
* <div style="margin-left:1.27cm;margin-right:0cm;">Compare input questions to database and find similarities and</div>
+
* Compare input questions to database and find similarities and
* <div style="margin-left:1.27cm;margin-right:0cm;">Teach the chatbot to learn an answer that it does not know</div>
+
* Teach the chatbot to learn an answer that it does not know
 
|-
 
|-
 
|| '''Show slide:'''
 
|| '''Show slide:'''
Line 23: Line 23:
 
System Requirements
 
System Requirements
 
|| To record this tutorial, I am using
 
|| To record this tutorial, I am using
*<div style="margin-left:1.27cm;margin-right:0cm;">'''Ubuntu Linux OS version 22.04'''</div>
+
*'''Ubuntu Linux OS version 22.04'''
*<div style="margin-left:1.27cm;margin-right:0cm;">'''Python 3.12.3'''</div>
+
*'''Python 3.12.3'''
  
 
|-
 
|-
Line 33: Line 33:
 
[https://www.spoken-tutorial.org/ https://spoken-tutorial.org]
 
[https://www.spoken-tutorial.org/ https://spoken-tutorial.org]
 
|| To follow this tutorial:
 
|| To follow this tutorial:
* <div style="margin-left:1.27cm;margin-right:0cm;">You must have basic knowledge of using Linux Terminal and Python</div>
+
* You must have basic knowledge of using Linux Terminal and Python
* <div style="margin-left:1.27cm;margin-right:0cm;">For pre-requisite Linux and Python Tutorials, please visit this website</div>
+
* For pre-requisite Linux and Python Tutorials, please visit this website
* <div style="margin-left:1.27cm;margin-right:0cm;">Python libraries required for automation must be installed</div>
+
* Python libraries required for automation must be installed
 
|-
 
|-
 
|| '''Show slide:'''
 
|| '''Show slide:'''
Line 41: Line 41:
 
Code files
 
Code files
 
||
 
||
* <div style="margin-left:1.27cm;margin-right:0cm;">The files used in this tutorial are provided in the '''Code files''' link.</div>
+
* The files used in this tutorial are provided in the '''Code files''' link.
* <div style="margin-left:1.27cm;margin-right:0cm;">Please download and extract the files.</div>
+
* Please download and extract the files.
* <div style="margin-left:1.27cm;margin-right:0cm;">Make a copy and then use them while practicing.</div>
+
* Make a copy and then use them while practicing.
 
|-
 
|-
 
|| '''Show Slide:'''
 
|| '''Show Slide:'''
Line 49: Line 49:
 
About Chatbot
 
About Chatbot
 
||
 
||
* <div style="margin-left:1.27cm;margin-right:0cm;">A chatbot is a software application designed to simulate conversations</div>
+
* A chatbot is a software application designed to simulate conversations
* <div style="margin-left:1.27cm;margin-right:0cm;">Typically they interact with human users through text or speech</div>
+
* Typically they interact with human users through text or speech
 
|-
 
|-
 
|| '''Show Slide:'''
 
|| '''Show Slide:'''
Line 56: Line 56:
 
Building and Training a Chatbot
 
Building and Training a Chatbot
 
|| In this tutorial we will build a Chatbot and an auto-updatable database.
 
|| In this tutorial we will build a Chatbot and an auto-updatable database.
* <div style="margin-left:1.27cm;margin-right:0cm;">When the user asks a question, the bot will search for an answer in the database</div>
+
* When the user asks a question, the bot will search for an answer in the database
* <div style="margin-left:1.27cm;margin-right:0cm;">If the question is not found, the user’s answer is saved to the database</div>
+
* If the question is not found, the user’s answer is saved to the database
 
|-
 
|-
 
|| '''Show Slide:'''
 
|| '''Show Slide:'''
Line 63: Line 63:
 
Chatbot - Libraries
 
Chatbot - Libraries
 
|| The Python libraries required for this tutorial are:
 
|| The Python libraries required for this tutorial are:
* <div style="margin-left:1.27cm;margin-right:0cm;">'''Json '''module''' '''which parses '''JSON '''strings with key/value pairs</div>
+
* '''Json '''module''' '''which parses '''JSON '''strings with key/value pairs
* <div style="margin-left:1.27cm;margin-right:0cm;">'''difflib '''module is used to compare sequences</div>
+
* '''difflib '''module is used to compare sequences
 
|-
 
|-
 
|| '''Narration'''
 
|| '''Narration'''
Line 96: Line 96:
  
 
'''Point to chatbot.py and QnA_base.json'''
 
'''Point to chatbot.py and QnA_base.json'''
|| <span style="color:#252525;">'''chatbot</span>.py '''is the '''python '''code which is linked to the '''json '''file to fetch data.
+
|| '''chatbot.py '''is the '''python '''code which is linked to the '''json '''file to fetch data.
  
 
Note that the '''json''' file and '''python '''code file should be saved in the same location.
 
Note that the '''json''' file and '''python '''code file should be saved in the same location.
Line 108: Line 108:
 
|| Now, we will go through the '''python''' code in the text editor.
 
|| Now, we will go through the '''python''' code in the text editor.
  
<span style="background-color:#ffffff;">It allows the use of new </span><span style="background-color:#ffffff;">'''Python '''</span><span style="background-color:#ffffff;">features in modules if there are transitions</span> between versions.
+
It allows the use of new '''Python '''features in modules if there are transitions between versions.
  
 
First we need to import the '''json '''module for encoding and decoding '''JSON '''data.
 
First we need to import the '''json '''module for encoding and decoding '''JSON '''data.
Line 115: Line 115:
  
 
from difflib import get_close_matches
 
from difflib import get_close_matches
|| '''Get <span style="color:#252525;">underscore </span>close <span style="color:#252525;">underscore </span>matches''' is a function from the '''difflib '''library.
+
|| '''Get underscore close underscore matches''' is a function from the '''difflib '''library.
  
 
It is used to find matches for a string in a list of strings based on similarity.
 
It is used to find matches for a string in a list of strings based on similarity.
Line 177: Line 177:
 
We set it as '''0.6'''.
 
We set it as '''0.6'''.
  
Now the Bot answers the question with a 60 percent match in the '''JSON '''file<span style="color:#ff0000;">.</span>
+
Now the Bot answers the question with a 60 percent match in the '''JSON '''file.
 
|-
 
|-
 
|| '''Highlight:'''
 
|| '''Highlight:'''
Line 215: Line 215:
  
 
database = loadData(r"QnA_base.json")
 
database = loadData(r"QnA_base.json")
|| We first call the '''loadData '''function to load the '''JSON '''file<span style="color:#ff0000;">.</span>
+
|| We first call the '''loadData '''function to load the '''JSON '''file<span style="color:#ff0000;">.
 
|-
 
|-
 
|| '''Highlight:'''
 
|| '''Highlight:'''
Line 247: Line 247:
  
 
chatbotDatabase["questions"].append({"questions": userInput, "answer": newResponse})
 
chatbotDatabase["questions"].append({"questions": userInput, "answer": newResponse})
| style="border-top:none;border-bottom:1pt solid #000000;border-left:none;border-right:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.176cm;padding-right:0.176cm;" | '''newResponse''' is then appended to the '''JSON '''file using the append function.
+
|| '''newResponse''' is then appended to the '''JSON '''file using the append function.
  
 
If the user enters '''skip''', we move on to the next iteration of the while loop.
 
If the user enters '''skip''', we move on to the next iteration of the while loop.
Line 258: Line 258:
  
 
print("Great! I've learned something new.")
 
print("Great! I've learned something new.")
|| We call '''saveData''' to overwrite the '''JSON '''file<span style="color:#ff0000;">''' '''</span>with the new answer.
+
|| We call '''saveData''' to overwrite the '''JSON '''file<span style="color:#ff0000;">''' '''with the new answer.
  
 
The bot displays a message after successful updation in the database.
 
The bot displays a message after successful updation in the database.
Line 442: Line 442:
  
 
In this tutorial, we have learnt to
 
In this tutorial, we have learnt to
* <div style="margin-left:1.27cm;margin-right:0cm;">Create an auto-updatable QnA database for a chatbot</div>
+
* Create an auto-updatable QnA database for a chatbot
* <div style="margin-left:1.27cm;margin-right:0cm;">Compare input questions to the database and find similarities</div>
+
* Compare input questions to the database and find similarities
* <div style="margin-left:1.27cm;margin-right:0cm;">Teach the chatbot to learn an answer that it doesn't know</div>
+
* Teach the chatbot to learn an answer that it doesn't know
 
|-
 
|-
 
|| '''Show slide:'''
 
|| '''Show slide:'''
Line 450: Line 450:
 
Assignment
 
Assignment
 
|| As an assignment, please do the following:
 
|| As an assignment, please do the following:
# <div style="margin-left:1.27cm;margin-right:0cm;">Consider a product of your choice, for example a Samsung phone.</div>
+
# Consider a product of your choice, for example a Samsung phone.
# <div style="margin-left:1.27cm;margin-right:0cm;">Create a Customer Service chatbot that can answer customer queries.</div>
+
# Create a Customer Service chatbot that can answer customer queries.
 
|-
 
|-
 
|| '''Assignment- '''Customer Service chatbot
 
|| '''Assignment- '''Customer Service chatbot
Line 465: Line 465:
 
|| '''Show Slide:'''
 
|| '''Show Slide:'''
  
<div style="color:#252525;">Spoken Tutorial Workshops</div>
+
Spoken Tutorial Workshops
 
|| The '''Spoken Tutorial Project''' team conducts workshops and gives certificates.
 
|| The '''Spoken Tutorial Project''' team conducts workshops and gives certificates.
  
 
For more details, please write to us.
 
For more details, please write to us.
 
|-
 
|-
|| <span style="color:#252525;">'''Show Slide: '''</span><span style="color:#252525;">Answers for THIS Spoken Tutorial</span>
+
|| '''Show Slide: '''Answers for THIS Spoken Tutorial
 
|| Please post your timed queries in this forum.
 
|| Please post your timed queries in this forum.
 
|-
 
|-
|| <div style="color:#252525;">'''Show Slide:'''</div>
+
|| '''Show Slide:'''
  
<div style="color:#252525;">FOSSEE Forum</div>
+
FOSSEE Forum
 
|| For any general or technical questions on''' Python for Automation''', visit the '''FOSSEE forum''' and post your question.
 
|| For any general or technical questions on''' Python for Automation''', visit the '''FOSSEE forum''' and post your question.
 
|-
 
|-

Revision as of 14:52, 18 November 2024

Visual Cue Narration
Show Slide:

Welcome

Hello and welcome to the Spoken Tutorial on “Building and Training a ChatBot”
Show Slide:

Learning Objectives

In this tutorial, we will learn to
  • Create an auto-updatable Q n A database for a chatbot
  • Compare input questions to database and find similarities and
  • Teach the chatbot to learn an answer that it does not know
Show slide:

System Requirements

To record this tutorial, I am using
  • Ubuntu Linux OS version 22.04
  • Python 3.12.3
Show slide:

Prerequisite

https://spoken-tutorial.org

To follow this tutorial:
  • You must have basic knowledge of using Linux Terminal and Python
  • For pre-requisite Linux and Python Tutorials, please visit this website
  • Python libraries required for automation must be installed
Show slide:

Code files

  • The files used in this tutorial are provided in the Code files link.
  • Please download and extract the files.
  • Make a copy and then use them while practicing.
Show Slide:

About Chatbot

  • A chatbot is a software application designed to simulate conversations
  • Typically they interact with human users through text or speech
Show Slide:

Building and Training a Chatbot

In this tutorial we will build a Chatbot and an auto-updatable database.
  • When the user asks a question, the bot will search for an answer in the database
  • If the question is not found, the user’s answer is saved to the database
Show Slide:

Chatbot - Libraries

The Python libraries required for this tutorial are:
  • Json module which parses JSON strings with key/value pairs
  • difflib module is used to compare sequences
Narration Do you know how a chatbot works?

Let me show you a small demo.

Show a demo on terminal

Type python3 chatbot.py

Let me type “Hello” and press Enter.

Can you see a response?

Again let me type “How are you?” and press Enter

You can see the chatbot is giving appropriate replies.

Narration Let us see how it has been implemented in Python.
Open downloads folder I have created the source files required for this demonstration.
Open QnA_base.json file QnA_base.json file acts as the database for the ChatBot.

It has question-answer pairs stored in a dictionary format.

Switch tab to Downloads folder

Point to chatbot.py and QnA_base.json

chatbot.py is the python code which is linked to the json file to fetch data.

Note that the json file and python code file should be saved in the same location.

Highlight the lines:

from __future__ import annotations

import json

Now, we will go through the python code in the text editor.

It allows the use of new Python features in modules if there are transitions between versions.

First we need to import the json module for encoding and decoding JSON data.

Highlight the lines:

from difflib import get_close_matches

Get underscore close underscore matches is a function from the difflib library.

It is used to find matches for a string in a list of strings based on similarity.

Highlight:

def loadData(file_path: str) -> dict:

loadData loads the JSON file.
Highlight:

with open(file_path, 'r') as file:

Next we open the JSON file in read mode.
Highlight:

return json.load(file)

json dot load function is used to read the JSON data in the file.
Highlight:

def saveData(file_path: str, data: dict):

saveData function writes new data into our Chatbot’s database.
Highlight:

with open(file_path, 'w') as file:

Next we open the JSON file in write mode.
Highlight:

json.dump(data, file, indent=2)

Json dot dump writes the content of the JSON file into the dictionary data.
Highlight:

indent=2

We can also add an indent value of 2 for readability in the JSON file.
Highlight: findClosestMatch function finds the best match for a user’s question in a list of questions.
Highlight:

query

Here, query is the variable that stores the user’s question.
Highlight:

questionsp

questions is a list of all questions from the JSON file.
Highlight: Get_close_matches function is used to find a match to query in the list questions.
Highlight:

cutoff=0.6

Here, cutoff is used to find the percentage of similarity that is acceptable.

We set it as 0.6.

Now the Bot answers the question with a 60 percent match in the JSON file.

Highlight:

return matches[0] if matches else None

Then, we return the best match.

If no match is found then None is returned.

Highlight:

def fetchAnswer(query: str, chatbotDatabase: dict) -> str | None:

fetchAnswer function retrieves the answer for a given question from the JSON file.
Highlight:

query

Here, query is the question asked by the user for which the answer is retrieved.
Highlight:

database

database is the dictionary containing the Bot’s database.
Highlight: We iterate through each question-answer pair in database.

If the question matches the input then we return the corresponding answer.

Highlight:

def runChatbot():

runChatbot function acts as the main function to implement the chatbot.
Highlight:

database = loadData(r"QnA_base.json")

We first call the loadData function to load the JSON file.
Highlight: We print a greeting, then start an infinite loop to interact with the user.
Highlight:

lower()

lower function converts the user input to lowercase.

This prevents the case sensitivity of strings while typing the answer by the user.

Highlight: The loop will only break if the user enters quit.
Highlight: findClosestMatch is used to find a match for the user’s input in the database.
Highlight: If a match is found, we can call fetchAnswer and print the answer.
Highlight: If no match is found, we prompt the user to give an answer to the question.
Highlight: The answer that the user enters as input is saved in a variable named newResponse.
Highlight:

if newResponse.lower() != "skip":

chatbotDatabase["questions"].append({"questions": userInput, "answer": newResponse})

newResponse is then appended to the JSON file using the append function.

If the user enters skip, we move on to the next iteration of the while loop.

Now the bot will prompt the user for a question again.

Highlight:

saveData(r"QnA_base.json", chatbotDatabase)

print("Great! I've learned something new.")

We call saveData to overwrite the JSON file with the new answer.

The bot displays a message after successful updation in the database.

Highlight:

runChatbot()

Finally, we call the runChatbot function.
Press ctrl+s Save the code as chatbot.py in the Downloads folder.
Running the code:

Open terminal -

Press Ctrl + Alt + T

Open the terminal by pressing Control, Alt and T keys simultaneously.

Type in terminal:

source Automation/bin/activate

We will open the virtual environment we created for the Automation series.

Type source space Automation forward slash bin forward slash activate.

Then press enter.

Type in terminal:

> cd Downloads

> Python3 chatbot.py

In the terminal, type cd Downloads and press enter.

Next type python3 chatbot.py and press enter.

Type in terminal:

Hello

The chatbot prints a greeting message and prompts the user.

Here I am going to type Hello and press Enter

Highlight:

Hey there

The bot answers: Hey there.
Type in terminal:

What is the capital of Denmark?

Let us ask: What is the capital of Denmark?
Highlight:

Copenhagen

Show the JSON file and highlight the Q and A

The bot answers Copenhagen because this answer is already in its database,

We will check the JSON file now. It is working as we expected.

Type in terminal:

In what year was the first international modern Olympiad held?

Now type: In what year was the first modern Olympiad held?
Highlight:

Hmm, I don't have an answer for that. Can you update me?

The bot does not know the answer to this and hence it asks Can you teach me?

The chatbot is waiting for our input.

Type in terminal:

1896

Let us enter the answer: 1896

The bot responds with a message to confirm that the answer has been added.

Show:

QnA_base.json file

Check the json file.

We have received an alert: This file has been changed by another program

Click on reload Let us reload this file.
Highlight: Now we can see that the new question and answer have been appended to the file.

Switch back to the terminal again.

Show terminal and type:

What is the capital of India?

Now let us ask: What is the capital of India?
Highlight:

Copenhagen

The Bot gives us the wrong answer. Why does this happen?
Type in terminal:

quit

First let us terminate the program by typing quit.
Show file:

chatbot.py

Switch to the source code and check the function get_close_matches.

We set the cutoff value or the similarity percentage to 60 percent.

Highlight in QnA_base.json:

what is the capital of

The program matches the words 'what is the capital of'.

This forms a 60 percent match with an existing question in the database.

The accuracy rate is significantly low, as is evident.

Type:

Change 0.6 to 1.0

This problem can be fixed by changing the cutoff value to something higher.

Let us change it to 1 point 0.

Now the bot will only match questions with 100 percent similarity in the database.

Save chatbot.py:

Type in terminal:

python3 chatbot.py

Save the source code and switch to the terminal.

Rerun the code by typing the command python3 chatbot.py

Type in terminal:

What is the capital of India?

We will type the question: What is the capital of India? again.
Highlight:

Hmm, I don't have an answer for that. Can you teach me?

This time the bot does not know the answer.

This means that the Chatbot is treating the question as a new one.

Enter the answer New Delhi

This will automatically be saved in our QnA_base.json file

In this way, we can build a chatbot for our specific purpose.
Type in terminal:

quit

deactivate

Type quit to terminate the program.

Then, type deactivate and press Enter.This will allow you to exit the virtual environment.

Show slide:

Summary

This brings us to the end of the tutorial. Let us summarize.

In this tutorial, we have learnt to

  • Create an auto-updatable QnA database for a chatbot
  • Compare input questions to the database and find similarities
  • Teach the chatbot to learn an answer that it doesn't know
Show slide:

Assignment

As an assignment, please do the following:
  1. Consider a product of your choice, for example a Samsung phone.
  2. Create a Customer Service chatbot that can answer customer queries.
Assignment- Customer Service chatbot

assignment_chatbot.jpg

Try to create a Customer Service chatbot as shown.
Show slide:

About the Spoken Tutorial Project

The video at the following link summarizes the Spoken Tutorial Project.Please download and watch it
Show Slide:

Spoken Tutorial Workshops

The Spoken Tutorial Project team conducts workshops and gives certificates.

For more details, please write to us.

Show Slide: Answers for THIS Spoken Tutorial Please post your timed queries in this forum.
Show Slide:

FOSSEE Forum

For any general or technical questions on Python for Automation, visit the FOSSEE forum and post your question.
Show slide:

Acknowledgement

The Spoken Tutorial Project was established by the Ministry of Education, Government of India.
Show slide:

Thank You

This is Jasmine Tresa Jose, a FOSSEE Semester Long Intern 2024, IIT Bombay signing off.

Thanks for joining.

Contributors and Content Editors

Madhurig, Nirmala Venkat