Python Chat Bot Tutorial - Chatbot with Deep Learning (Part 1)

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hey guys and welcome back to a brand new tutorial series where I'm gonna be showing you how to build an AI chat bot in Python now it's a very simple chat bot but it does actually use neural networks and deep learning which means it is slightly intelligent but it's also very easy to kind of get working and get testing which a lot of the other chat pod tutorials I've seen hard not that easier at least I've tried to follow along with them and I can't get all the way through so I'm hoping that you guys are gonna be able to get through this with me if you have any questions throughout the tutorials feel free to leave a comment down below maybe myself or someone else would be able to help you you also join my discord server where people are always answering questions and helping each other out so let's talk about what this chat bot is how it works and our approach to kind of doing this throughout this tutorial so essentially what I wanted to do was create a chat bot that was used for a specific purpose so not just like a general chatting bought but something that maybe could help you answer questions about your business or maybe be a QA bot that answered like trivia questions or something like that or maybe a chat bot that your customers would type too and it gives them some kind of answer so really more an application based chat bot than just like talking in general now you can also make this chat bot talking but I'm going to show you right now how you customize it and how it kind of works so this is my chapo running right now this stuff will not be in like the final version this is just kind of a testing version so I haven't gone too far with it but you can see for example it typed like hey it's gonna respond with Hello if I say like what's up I got hi there how can I help you and just to note these are things that I'm typing in are not hard-coded into the chapo which means it's kind of figuring out what I'm asking and giving me an appropriate response if I say like how's your day you can see we get two responses just cuz it wasn't sure what I was typing there and then it says good to see you again so it's really easy to fine-tune this chat bot we'll talk about that later but let's do some more examples so if I say like what is your name says I'm Tim where I say like what should I call you I'm Tim aka tech with Tim now I set this up to kind of answer like some questions about a business so I like if I ask what or when are you open so yeah let me say we are open 7 a.m. to 4 p.m. Monday to Friday and you can code kind of whatever questions and whatever responses you want into this chat bot and make it as specific to your own use as you want and that's one of the reasons I really want to show this one because it's super customizable and you guys can very easily change it and we'll show you how it works so last thing I'll say like what is on the menu I'm pretending this is like a cookie shop I know it's a ridiculous shop but you guys get the point and says cookies are on the menu if I say how much is a cookie says 18 years young because I haven't coded anything that would like take in this response and and make sense of that so anyways that's kind of how the chat bar works you can see that it's obviously not amazing and it made a few simple mistakes but there's ways to tweak this and fix this and we'll be going through and kind of understanding how this works so let's close this up for right now and let's go to the data that our chat BOTS see so that chat pod that you guys just were looking at is trained on just this little bit of information now this actually wasn't trained into there that's why I didn't work when I said how much is cookie cuz I hadn't trained the chat before that um I just added that but anyways you guys will see we have what's known as a bunch of intense and then you can see we have patterns and responses so the patterns are what likely the user would type in so for example you could see hi how are you is anyone there hello good day what's up and then the response is an appropriate response to that kind of question or whatever the user typed now you can kind of see as we go through the different examples how they work but notice that I didn't type exactly what was typed here and the chaplet still came up with an appropriate answer for most of the things that i asked within within reason if the chapel was trained on that right like some of the things when I asked these here I didn't use the exact same thing it just figured it out and kind of worked and that's what we're gonna be aiming for in this series so we have essentially a group of intense and what our chat bot is going to attempt to do is take our message in and figure out what intent we want are we greeting are we saying goodbye are we asking for the age do we want name do we want the shop do want the hours that's what it's gonna try to figure out now this is really nice because a lot of other chatbots actually generated text response but you'll notice if you use any other chat bots that are really complex they oftentimes make mistakes and what they say doesn't really make sense so with this one you know you're gonna get an output that makes some kind of sense because you're hard-coding the outputs and you can also determine kind of how the chat bot works more specifically because you can code in all of the different intents so you can say well if I want this chat bot just to be maybe about selling like my knitting or something like that then you can code whatever would make sense for a user to type to that chat bot in anything else we could just have like another intent section there should be like I don't understand what you're saying or please ask another question or something like that so with all that being said that is how the chat bot works it's pretty simple in the next few tutorials we're gonna get through it and you guys will have a working chat bot that's fully customizable but let's talk about what we need to download and install to get this working so obviously we're gonna be using Python but we need to install a few Python modules now for this tutorial we need to be working with Python version 3.6 now 3.7 currently has a bug in it where it doesn't work with the TF learn module that I want to use for this tutorial so make sure you guys are running Python 3.6 if you're if you can and you're not running anaconda or anything like that uninstall any version of python you have right now and reinstall or install python 3.6 okay if you want to see what version of python you're running just type python and it will pop up your version you can see my version is 3.7 but i'm not going to be using that version because i'm going to be using conda or anaconda which has python 3.6 for it if you're using conda to create a new environment that has python 3.6 just do conda create - n give it a name in this case i just named mine chat bot and then say python equals 3.6 I'm not gonna go in-depth with how all this stuff works I'm just trying to get you guys set up now if you're using Conda activate that environment so in this case I already have one called chat bot so I'm gonna activate it if you're not using Conda just type whatever I'm about to type in the command prompt like this okay so what we're gonna do is need to install some modules so we're gonna use pip and the first module we need to install is a NLT kay now this is natural language toolkit which is gonna give us some nice functions to do some things with our words and trim them down and all that stuff so install this I already have it installed so I'm just gonna leave that if for some reason your pip isn't working I have a tutorial on my channel it's called how to install Pi game that will show you how to fix your pip I'll leave a card to it in the top corner of the screen right now so we'll do pip install um NLT Kay now will pip install numpy this is just gonna be used for some array management and stuff like that then after that we're gonna do pip install TF learn by the way you should be hitting Enter here I'm just Knox I already have it installed and finally we're gonna pip install tensorflow like that now if any of this doesn't work leave a comment down below I'll try to answer you go on Stack Overflow try to figure out the air but all of these commands should work if you're running Python 3.6 and you have pip working correctly on your machine now that we have that it is time to go to our main Python file and just start working with a little bit of code and actually sorry before we go to that I'm just gonna talk about this intense file one last time and how you guys need to be creating this so if you're following along and you want to follow along exactly with me you can download this intent file there will be a link in the description it may not be up right away remind me if you guys don't see it and I will add it into the description or just copy out the format that I have here by typing it out so if you want to start with just like one tag or something like that that's fine just do intense make sure that it looks the same so it has this squiggly bracket colon and then there's a list like this it's just like a Python dictionary and then we have dictionaries inside of here separated by commas and those have tag pointing to whatever the tag is and then patterns pointing to a list response pointing to a list in this context underscore set pointing to nothing right now which will change later so that's you need to do we need to get this kind of structure and make sure you save this file as a dot JSON file inside of your main Python directory or whatever your script is so in this case I'm you're working in main dot PI this is inside a folder so that means that the intent so Jason needs to be beside it or in the same directory as it so that we can you know work with it and loaded in so now we're going to start doing some imports and just reading in some write JSON data so we're gonna say import and ltk we're going to say from an LT k dot stem lancaster import land castor stemmer now i just messed up the capitals on that i so we'll fix that now now this is just gonna be used to stem our words which we're gonna talk about in a little bit and we're gonna say stammer equals in this case Lancaster's stemmer like that and we'll deal with that after now we're gonna import numpy so just go import numpy we're gonna import TF learn rapport TF learn import tensorflow we're gonna import random and we're going to import JSON now what we're gonna do is use JSON to read in this file and just loop through some of it and see all of our data and how we read that into the Python script because we need to use that to train our model so what we're gonna do now is we're gonna say with open and in this case we're just gonna type the name of our file so in this case intense dot JSON when I say as file now we're gonna say data equals in this case should be json dot load and then five so now if we print out data we should be seeing like a Python dictionary that looks exactly like this so let's try that I'm just gonna CD in to my directory here and run the python script if you guys are using ideally or whatever that's fine this will work the same CD into simple chatbots and then she go python maned up high i don't think that's gonna work maybe it will okay so there we go so that worked and now you can see that we have kind of this JSON data it's all bunched up and we can start accessing some of it so notice now if i do data and I type intense because that is what we've named this right here it should just show us all of the dictionaries as a post that main dictionary so let's run this now and let's see what we get and now you can see that we're getting all of these different lists so we have tag greeting patterns all about it we can accessing this just like you would access a Python dictionary so what we need to do now is we need to get all of these patterns and figure out what group they're in so in this case we're calling these tags but it can also be just groupings we're gonna need to get the responses and we just need to load all of that in so what I'm gonna do now is just start looping through this JSON data and doing just that so we're gonna say words equals a blank list we're gonna say classes or actually will not want to class will do labels labels equals a blank list will say docs equals that and there we go so now what I'm gonna do is I'm gonna say for intent in data and then in this case we're gonna say in tense now this is gonna do is loop through all of these dictionaries for us now the way that works is because we're looking at this list because we're saying data in tense then we're saying for every single intent in here which will give us all of these dictionaries which would be really easy and nice to loop through all these different patterns and all of that so now what we're gonna do we're gonna say for in this case pattern in intent and now remember this is a dictionary so we access the patterns registered patterns we're gonna do something specific with these patterns and this is known as stemming now this is the first part where we actually start understanding some of this kind of machine learning AI stuff for our chat BOTS now what stemming does is it's gonna take each word that's in our pattern so let's look at the words here and bring it down to the root word which means that for example is anyone there well if we're looking at the word there we're gonna say they're the root word of their question mark is just there so we're gonna remove this question mark if we have something like what's up we're gonna change this to just be what instead of what's right cuz we're gonna get the root of the word now the reason we you get the root of the word is because when we're training our model and our training our bot we don't really care about the other things attached to the word we just care about the main meaning of the word so by eliminating extra characters and things that might make the word different it's making our model that much more accurate because we're gonna ignore things that could stray the model in the wrong direction you guys understand more about how this works in future tutorials now to get all the different words in our patterns and to stem them we need to do something called tokenize now what tokenized means is just get all the words in our pattern it's the same thing as just splitting it by a space and getting all these different words but NL TK has a nice thing that does it for us and we're gonna use that and we're gonna say words or I will do like W RDS equals in this case n LT k dot word underscore tokenizer I think it is let's say oh just tokenized and then we're gonna put padding now this is gonna return to us a list with all of the different words in it and now what we're gonna do is we're simply gonna put all of these tokenized words into our words list now the reason we do this is because we want to know all of the different words that are in our intense file and we'll talk about that a bit later but that's what we're gonna do so just go say words dot extend and in this case W RDS now the reason we do this is because this is already a list so rather than looping through it and appending each one in we can just extend the list which means we're going to add all of those words in so now that we've done that the next thing that we need to do is we're gonna add to our Docs the pattern of words now what we're going to do to do this we're gonna say Docs dot append and in this case we're just going to append the pattern like that and that's all we need to do for that now finally we're gonna say if intent in this case tag which or yeah intent tag which will give us like these goodbye greetings age name in let's say labels or not in labels we'll need to do my back say not in labels then we're going to say the labels dot append and in this case intent now this way we're just gonna get all of the different tags that we have so greeting goodbye age name I guess we don't really need to do it like this per se we can just say if intent tag not in labels labels not append intent I don't know why I had that in the for loop in my other thing this should just work fine though to do that for us okay sweet so that's actually all we need to do right now for loading in all of our words and our labels and getting our documents ready with all the different patterns so I'm gonna leave the tutorial here in the next video what we're gonna do is do a little bit more on this data pre-processing and then we'll start working on creating our model and our training data and how all that's gonna work for our chatbot so if you guys enjoyed please make sure you leave a like and subscribe to the channel and I will see you again in the next video
Info
Channel: Tech With Tim
Views: 669,569
Rating: undefined out of 5
Keywords: tech with tim, python chatbot tutorial, python chatbot, chatbot tutorial, chatbot tutorial python, how to create a chatbot python, ai chatbot python, deep learning chatbot, deep learning chatbot python, machine learning chat bot, how to create a chatbot with python, how to make a chatbot
Id: wypVcNIH6D4
Channel Id: undefined
Length: 16min 10sec (970 seconds)
Published: Mon May 27 2019
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.