Natural Language Understanding in Python | Rasa NLU Quickstart

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hey guys what's up my name is Gabe and this is games with Gabe in today's tutorial what I'm gonna be doing is something a little bit different this is something called natural language understanding if you haven't heard of it I'm gonna go for a brief overview of what it is and sort of just the concept behind it before we actually delve into using this library rasa Nou and actually learning how to use it but essentially what we're gonna be doing is guessing what users want to do based on some text input that they give us and then figuring out what their intent is and choosing an appropriate response so essentially what we're gonna be making is a chatbot I'm going to take you through Rossa and I'll use first tutorial basically and just sort of explain everything that they're explaining and then I'm gonna go a little bit further and show a couple extra steps we're going to modify their first tutorial a little bit and add a whole new intent and capability to our little chat ba-bye so that it can suggest us some movies when a user asks for any good movies so that's what we will be doing in today's tutorial let's look at how it works behind the scenes first before we actually go and try and code it all right so what is natural language understanding well when most people hear natural language understanding what they think of is something like Google or Alexa where you have somebody speak and say something this is called an utterance and then this utterance goes and it they use some sort of AI to change that speech to text so then you get a sentence that looks like something somebody said do you know of any good movies and then what the AI then does is it uses another library to break up this sentence into its parts of speech and remove any unnecessary words like of and do and any which can be useful but for the most part most people will not be using and that breaks up into the most important components which are things like the nouns the verbs and the adjectives and then it uses another machine learning model to identify what the intent of that user was by matching it up against some training data and then in this case it identified an intent find a movie this is in essence what natural language under standing is in our case we're gonna take out this component we are not going to do an e speech-to-text yet and then what we're going to do is for this breaking up of everything we're actually gonna be using not Spacey that's one Python library but we're going to be using Karis which is another machine learning library that will be working behind the scenes we won't have to worry about any of this part this is all done internally by the library that we will end up using which basically matches this against a bunch of training data we will be using Raza and LU which uses this you can configure it to use different libraries like Spacey which is another Python library to do things like this where you break up the speech into different parts of the speech and everything but instead what we're gonna be doing is just be using the Raza Nou which does all that stuff for us and we can just focus on giving it training data and coming up with a intent and a system to respond to user input and everything and so basically what that means is machine learning models the way they work on the inside is they transform all that data into a set of matrices which are just a bunch of numbers so you have one big Maitre see this is your input data which you've multiplied against another maître see this is your weight and then you have another possibly sometimes people will leave this out this is your biases so you have your input you have your weights your biases and this gives you your output data and then what it does is this output data in our case is going to be a bunch of different intents so it could be stuff like find a movie find movie or it could be something like grief or greet angry and you could just have a bunch of different intents and then what the AI is going to do is it's going to assign a weight to each of these to say oh I think this is zero point nine percent so that means ninety percent probability they're doing this this is like a zero point zero five and this is like a 0.15 and these will all sum up to one which means we'll have a hundred percentage in here but it will choose the best response and then you can set a threshold where if it's below like zero point eight that's not confident enough for you so if it's below zero point eight you have a fallback method and it will just go to some fallback mechanism no matter what if it's not confident enough and so in that case and normally what you would do is if it's just not recognizing a phrase that you would like to recognize you would add in more training data and what the machine learning model does is it uses all the training data to help it come up with these weights and these biases and then it uses the training data to confirm that it is guessing accurately by matching randomized data it'll use half of the training data to come up with to train it and then half of the data to verify that it's working so you want to ensure you have enough training data is what I'm trying to explain here and we're going to see what all this actually looks like in practicality once we start looking at the rasa Lu library so without further ado let's look at that library and see how we can use it to our advantage in this case and build a simple chat box the very first thing that we're gonna want to do is you're going to want to create a folder that will contain all this I'm just in my Python scripts folder I'm gonna say ctrl shift and to create a new folder in here and then I'm gonna call this rasa and öyou movies because that's basically what we're gonna be doing then you're going to want to go into here this is where everything is going to be happening so then in Visual Studio code I'm gonna go in and open up rasa and öyou movies hit select folder and then we're gonna open up a new terminal inside of here and to install Hossa and all you if you have PIP what you can do is you could say pip 3 install and then Raza and this will go through all the requirements and I already have it installed so does that very quickly it may take a little bit longer in your case next what we're gonna want to do is we're gonna say Raza in it - - no - pumped and what this was gonna do is it's going to set up a basic project for us to take a look at and see how everything works so we get a bunch of folders appearing over here on the left hand side and you see it's doing this training which is the thing I was talking about and it's actually really cool watching all this stuff - they put a lot of work into this library and it really shows it's even improved in the couple years since I've last taken a look at it or a few months I think but it's really cool ugly so what I want to do is take a look through all of this stuff in the left hand side sort of explain what all of it is in it dot PI that's just a basic Python file that make sure that we have access to all of our other Python files and everything actions dot pi so let's take a look inside here it has nothing in it but if we read up here it says this file contains custom actions would compete which can be used to run custom Python code so this is basically like if you were using a web server to connect to your training model and then you were using that to dispatch commands to this action stop I fold ER so basically the way that Lhasa NLU expects to be used is as a web server that you connect to the web server dispatches commands to some other Python file on that web server and this is where those commands would be next what we're going to take a look at is config ymo this basically sets up the configuration for your whole file so you can say it says we're using English and the pipeline is supervised embeddings this is basically the catch-all for just most of the basic I know you components you can change this pipeline to use other things and then it also has the policy so this is where it's saying it's using Carris and this is like the policy for what training it's going to be using you can also use in this config file i believe there's more configurations like how long you can track history and stuff that might be another one credentials not lion-o so this is for things like containing connecting to external services so this is where it would be running our web server basically and then you can see that it has other web servers you can connect to so if you want to put this on slack matter most socket IO and they sort of give you a little indications of what you would have to do in order to do that you would have to set up a little bit extra to but is basically where you would keep all that information we won't be worrying about this one too for now let's take a look inside this data folder and then inside of NLU MD so this is a markdown file which contains the training data so like I said this has quite a bit inside of it but it all goes under some blanket intent so this is saying hey these are all the intents that we want matchup to greet and so it looks at hey hello hi good morning all these different things and it uses these to try and guess if somebody is trying to greet so then you can do variations of this or different greetings and it will try and guess and say hey I think it's trained to greet the person and then they have the other intents goodbye affirm deny mood greet mood and happy so on so forth now let's take a look at domain IML this is where it has the global so this is all the intense we could have these are all the actions that our AI can take this is all just defined inside this global domain don't lie a metal file and then these are all the responses and so as you can see these responses are very rigid you can't customize these it's selecting from a pool of responses selecting and very a varying the responses and everything which is once what you do once you get into a more advanced type of stuff now this also has like session expiration time which means it will expire after 60 seconds and carryover slots to new session means this this natural language understanding library holds history so it holds the history of your conversations and when it says this it's saying hey Kerry the history of however much history I said hold on to the next session so that when the user logs back in the AI has not forgotten about that stuff and you can customize that to those different things inside this file as well endpoints dot yml so this is where you would connect to things like Facebook and everything we won't be worrying about this once again because we are just going to be focusing on local versions of this right now and then models / timestamp so this file right here is your model this is your natural language understanding model this is what contains all the data for things like the weight the biases so that the AI knows what to do with everything one more thing I want to mention inside of this you can see that all the intents or this is the NLU MD by the way the training data one more thing I want to mention is that these are all very rigid just like simple phrases and stuff you can customize it it's like hey name and you attach this to some name and what this basically does is it will expect some variable name here and then it's going to match it up and give it to you in a variable called name and you can do customizations like that and stuff that's getting into more advanced knowledge too and then you can also set up reg access to so if you want to do like match a number and you want to match it like just one or more numbers that you could do that so that you can match patterns like zip codes and stuff but we won't be worrying about that right now because it's just getting a little bit more advanced to what I want to do I just want to short show the basics of Ross and audio and how it works if you guys want me to I can go into more depth on how to do stuff like that in future tutorials so with all this sort of under a belt let's see how this all works real quick I'm gonna go and say Ross Michelle because this will actually give us a little bit of an interactive model to check out how it works and stuff so if we do this then it's loading the BA and I can say hey and then it chooses its response and it says hey how are you and I say I'm doing bad it will choose the greet affirm and so then it sends me a picture and I can say yeah that helped me if I spoke y'all right but and you see that it miss recognized that intent I made of it doesn't know what to do after that so it just says bye after this it's like okay it helped and we can actually see that too if we go into the stories file which I don't think I showed this one is also important so if you notice it has these different paths and so happy path and it says basically the user greets what do you do you utter a greeting if the users mood is great than you utter a happy greeting sad path one says ok the user greets you utter greeting the mood is unhappy then you utter it's your up and then you say did that help and if it's an affirmation than you utter happy and then sad pattu is greet and so basically you're just defining like conversation pass that the a I could take depending on what sequence of events happens so if we try this again and I say hey all right it says hey how are you nice a bad and then it says it's gonna send me a picture and I'm gonna say yes and then it will utter great carry on okay and then if I say bye then it's just gonna say goodbye that's basically you can hit ctrl C to listo that's what the stories file is for we're not gonna worry too much about this - this is for if you want to stay within Raza and ohyou um definitely look into it because I think staying within the package is great and it will be much better than what I'm about to show you but I do want to show you how you can expose it's that you can use it with other libraries and stuff of that nature if you want to first things first in order to expose it to ourselves we have to do is we have to go into these models and I'm gonna open up 7-zip because it's the easiest way I could extract all this stuff so I'm gonna copy this path and then paste it into here hit enter and we're gonna go into models and right here I'm going to hit extract say okay go into here hit extract say okay and then we have all of that file extracted so all the data is finally extract I'm gonna delete this and I'm gonna delete this as well move this up and I'm going to delete the dot tar and I'm going to rename this test so if we go into here we see this is actually what the training moment looks like it has this Nou folder with all this stuff and the metadata and then it has the core which is just the policies and stuff for connecting to the actual API and everything what we're gonna do is we're basically just gonna export expose the model and just use that to our advantage so let's go and create a new file inside of here I'm gonna call this test up hi there's gonna be a few libraries we have to import from Raza that will help us expose this so I'm gonna say from la salida utils import print success from Raza I know you don't model import interpreter and then from Raza I know you utils import JSON to string then what I'm going to do is I'm gonna set up the pass let me say Rossum model path equals and we know this is under model slash test slash Nou relative to this Python folder it's inside here and you want to do the file to this MOU folder because that's what this will need in order to work so just whatever the name of your folder is then I'm gonna say interpreter equals interpreter dot load Ross model path and then what we're going to do is we're going to create this function so def defying Rossa output text and this is just as a message equals whatever that text is so we're gonna cast it to a string and then we're gonna strip everything off of it and then result is interpreter dot parse message this is basically what Rosso is doing behind the scenes and then it does more stuff with this result we're going to stop it here just that we can have more control over it I'm gonna create a main function real quick and then create a simple wire loop that takes user input sung as a user input in equals that and then while the user input while the user put input is not in the just a few different phrases so like while they're not and exit and I'm actually a lower case this just so that we make sure we get it so while it's not an exit or quit or queue we will continue to loop and then we will say our else equals Rossa output so we'll call that file with our user input then we'll print what we get just that we can see what it's returning and then we can say user in equals input and we'll do the double arrows once again and it will continue to loop until the user is done don't forget to call main Athlon function that's important next we'll say python test stop by and it's going to run this you're going to see a bunch of warnings and stuff that's okay you can ignore those there's a way to disable those too but then we get to this point I'm gonna say hey and then what you see is this big blob of text and says intent name and it says greet so it's saying worth doing a greeting what's the confidence 97% so it says they're greeting you with a 97 percent confident entities this is stuff that you could capture within the greeting like names and stuff so that's where it would appear intent ranking so then this is just the rest of the intent and what confidence it has for each of these so as you can see greeting has 0.97 so that's the highest then like practically zero practically zero and practically zero and practically zero for the rest of the greetings so this basically gives us a way to see what our intents are I'm gonna hit exit and then let's comment that out then I'm gonna say print the result and then I'm gonna say intent and name just that we can see this will just print out what the intent was so then let's do this one more time we'll run Python test okay I'm gonna type in hey make sure that's our out not result okay run this one more time and we'll type in hey and then it tells us that's a greet if I type in who are you it says it's a bot challenge so we get this way that we can expose this stuff so let's modify this a little bit so that we can actually have a movie I'm gonna do the movie part first because it's a little bit easier so we'll say utter good movie and then this will be movies equals dark night and Inception and Lord of the Rings and you could actually have this pulling from like IMDB or something if you have some other library We Bought a Zoo don't judge me on my movie choices - these are good movies but you could basically have these pulling from like some IMDB ratings or something and then just have it throw out whatever it wants so I'm gonna say I think you might like and then we're gonna pull a random movie from there so we'll say movies random dot R and range and we're getting port that in just a second zero to the length of the movies and then we're gonna say plus I liked that movie so basically you should get pull one of these random movies insert it and then give it to the user up here let's say import random just that we have that library available to us when we pull one of these and this actually might need to be minus one not completely sure on that but we'll go with it for now now we need to modify our row so model and retrain it so this is something you would do regardless if you want to add new and tense everything so first let's go to MOU and inside of here I'm gonna go hashtag hashtag and then I'm gonna say intent : this is gonna be any movies so basically this is the user saying is there any good movies and I'm gonna say they could ask any good movies and we basically just want to give it like four or five six the more the better because more intense you have better it is and it's really great if you can get actual put the test your models just that you can see what types of things they type in and then go based off of that I'm looking for a movie period so we're gonna just sort of use these four phrases to train it a little bit and then we'll have a story which we won't be using for our thing but it's good to have any movies and basically what we're gonna do actually we're gonna say this is a find a movie challenge or story I'm sorry and then we'll say any movies if the user it greets any movies then we will utter good movie and so then we will go into our domain which has all of our things first of all we want to add in any movies into here and that should not be an equal match be a - then we will go down here in utter good movie which I believe is the syntax I used yep utter good movie and any movies and if we go to nou any movies then under responses we will create our other movie so say utter good movie and I'm just gonna say text so this is basically where you define it a good movie is da because we have no way to choose it in here I mean technically well this is just a yml file so yeah you would have to connect this to an actions folder and everything to get more variations they do have other ways you can do it without that which we can took a look take a look at in a future tutorial as well I just wanted to get the bare bones site you know how to use this next thing we have to do is we have to train the model so we say Ross a train I'm gonna give it a no you will just get do is its get take all that new data we've given it and then it's gonna put it into the model and train it so you see it's going through everything again retraining it and the Nou at the end is just basically going to put it in something called Nou go into here and if we go up to models we have a new training model with Nou as the prefix now if we say Rossa shell I know you just to make sure it works we'll wait for this to start off real quick then we can say any good movies and you'll notice that it gives us the whole thing and so it says any movies with a 99% confidence so we know this is working perfectly fine next step you need to do is just like we did before a tract it from here using 7-zip which I closed out for some reason so we will go up here we'll say extract and I'm gonna rename this to test 2 then we will go back into our testify file and I'm going to change this from test to test tube then we're gonna say inside of here if the are out intent name equals any movies then utter good move II I spelled this wrong there we go and then we're gonna hash tag this out to comment out and let's run Python test PI real quick um and it says we have an error oh we forgot a plus there I'm sorry there we go run that one more time I'm gonna say hey do you know of a good movie which if you remember we did not program that in explicitly but it matches it to the movie intent as I think you might like we Pontius ooh I like that movie and then I can say good movie which we also did not put in there but it recognizes the intent and so this is where the real power of natural language understanding comes in and that's why it's so important to have good training data because if you have good training data it can match it up down here and there like I said you can change this training data so that it has it can take in different names you could do um was and then like movie a good movie and then what it does is it starts to match this which is random words and stuff you can pull from a database so that it's only selecting specific words or you can pull and just from a regex pattern and then it just says like oh i think they're talking about a movie there and then you can go and use that data so you can really customize this it is really powerful I would suggest you take a look at their website because their website is really good I will leave a link in the description to their website to the tutorial I use I suggest you check out the future tutorials if you guys want me to go into more depth on ain't up there tutorial on how to do things like that then ask me I started building a text adventure game in this using some of those more advanced concepts and I did it this way where I was just using predefined messages and stuff but I would love to take look at this more and see how to use it with an actual web server and everything because this is just a really cool feature a really cool library that you can use and get up and running with very quickly so I hope you guys really tried this out and see how it works further and show me what you come up with if you guys like this please hit like and subscribe and I will see you next time thanks [Music]
Info
Channel: GamesWithGabe
Views: 1,748
Rating: 5 out of 5
Keywords: gameswithgabe, games with gabe, Natural Language Understanding, NLU, Rasa NLU, chatbot, how to code chat bot in python, nlu in python, how to code natural language understanding in python, how to use rasa nlu, how to code chat bots, python
Id: 5UTOC8eSvP8
Channel Id: undefined
Length: 24min 44sec (1484 seconds)
Published: Mon Feb 10 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.