Post 2010 this is going to be the decade of e-commerce Ben Pandey runs a traditional Eatery in the city of Mumbai Mr Pandey wants to improve his income by listing it in platforms like zwigy and somato he thought this will fix his fortune but he gets dejected both platforms rejected his application a budding data professional decides to Step up and help his Uncle Ben Pandey Peter is a go-gadget he sat on a mission he decides to build a chat board for his Uncle Ben's Eatery in fact he succeeded in building one the first small step
towards a giant lead [Music] thank you I am going to take you along Peter's journey of building this chatbot step by step we will first configure our chatbot in dialogflow then we will write a back-end server in Python using fast API web serving framework this will interact with mySQL database so we will be placing orders retrieving order information from this database and once all of this is done we will integrate this chatbot to our food website this will be truly end-to-end project which will not only help you learn a lot of diverse skills required in
building a chat board it will also give you a solid project that you can put on your resume first on your emails and get ready for The ride review the problem statement we have this food website Pandey fresh and delicious foods and here if someone comes on the website it will show all the items so right now it is selling only nine items these are all yummy items chole Samosa mango let's see and so on and a person can get an idea on the price location they can even call to inquire but they don't have
any technology as such and what we want to do is we want to build a chatbot and Integrate it on this particular website so that you know the chatbot appears on the bottom right and a person can say New Order or track order so now let's say if I'm saying new order it will show me all the items which are available and I can then say give me two bav bhaji one pizza all the junk food and it will kind of review whatever you have added in the cards so far then you can add let's
say one mango let's see you can even remove an item you can say well you know these Are too much uh I want to remove let's say pizza in the chat Port you look at it chatbot is smart it is remembering the context and it is having this list where you can add items and remove items eventually when you say that's it it knows that you're completing an order and it will say okay we have placed your order this is the bill and so on so we are going to implement this and this will insert
a record through our backend in our mySQL database and it will handle All this conversation I am calling this a Mira chatbot you know all these companies have fancy names Ino Julie uh Alexa all these chatbot names I came up with this name Mira uh so Meera will be the chatbot that we are going to build now you can have a different scenario where a person might say I want to track an existing order after placing an order what happens is in the kitchen Chef will prepare it then it will be delivered so here see
order number 41 is in transit Meaning order has started from the restaurant and it is on the way previously it was uh in progress the previous order so it can give you all that status when you say okay Pani Puri I did not understand it will tell you that we do not support these items we can only order from all these nine items so it will have all these uh fancy features okay so first thing we are doing now is we are scoping down our work when I worked in Industry at Bloomberg and Nvidia business
managers would come up with big projects and they will have so many features that they would want to implement but when you start any project in the first phase you don't Implement all the features you have to scope it down so here is a huge list of requirement you come up with the initial set of features that you're going to implement and this is called scoping your work okay in our case we have variety of features when it comes To New Order one can place a new order you can also support paying through a chatbot
for that order the example that I showed you previously in the chatbot it was cash on delivery but you know a business user will say okay why don't you support credit card or debit card payment in your chatbot you can also have tracking features you can track by order ID Track by phone number Track by customer name you can have other features as well such as offers and Store hours and so on now you will review these features with business by you I mean the data scientists or ml Engineers who are working on this project
so there will be engineering team or data scientist team there are a business team they will have conversation they will do negotiation they will say hey this feature will take one month this feature will take five days uh which one you want to pick and business will come up with their most Desired items okay so they will say Okay I want to implement these two items placing a new order and tracking the order and that decision is dependent on two factors one is how important is that feature second thing is feasibility uh there is a
term called feasibility impact analysis let's say this feature is very important supporting payment through a chatbot business will say yes this is very important but data scientists will say well it will take Two months then that feature has a high impact but low visibility on the other hand you can have a feature which is very high feasibility but low impact for example store hours implementing store hours is a piece of cake you can implement it in half a day maybe but it doesn't have too much impact okay so you want to Target those features which
has high impact high visibility and after a closed room discussion Between business stakeholders and data scientists they decided these are the two features which we should Implement in our first round so what we had initially was all these features if you put it in a flat vertical list this is called your backlog folks when you're preparing for data science or NLP career make sure you don't learn only technical things you need to understand the fundamentals Behind project management agile methodology stakeholder management and so on backlog is a popular term when it comes to Agile way
of building projects backlog is nothing but all your wish list all the feature you want to build then after you had that discussion which I already talked about you came up with these two features and they are called MVP minimum viable product which is your phase one in phase one I want these two Features and you can use tools such as jira notion Asana etc for doing this kind of project management right now I just showed you all this in my slide but when I was at Bloomberg we were using jira there are companies which
is using a notion for example in my own company right now we use notion as a project management tool I have used Asana as well these are all awesome tools and they help you more manage your project they help you define Your backlog your MVP and so on and then once your MVP is defined your data science team will work only on these two they will not look at all these they will keep this in mind because these features they will Implement in phase two phase three and so on but their immediate Target is these
two features once these two features are out now you will have minimum viable product which is your first version which you can deploy you can test with users users Will give you some feedback after the feedback some of your backlog items will change because business is dynamic it's always changing it's not like okay my manager told me to implement this 10 feature and I will keep on implementing this for next one yet no as you deploy a MVP in production your current backlog will change based on the feedback you're getting from People based on changing
demands from the business so in phase two then you can pull some more features From backlog so you'll continue adding or deleting items from backlog and you will pull the features from backlog to here okay so read online about agile methodologies scrum way of doing project development there is a great website called scrumtraining series.com if you want to learn about scrum all right so now we have these two features that we are going to implement in this particular video so in throughout the video we are building this particular MVP so now let's think about solution
design and architecture when you get a project and when you have a scope of work clearly defined see so far we have clearly defined scope of work I want to build a chatbot put it on my website chatbot should support two features placing a new order and tracking order bet order ID as a data science team you know exactly what you're building now comes the phase where you will Define the technical architecture or the Solution usually in the companies uh when I was working in an industry and even in my company what we do is
we get together in a conference room and there will be team of data scientists who will brainstorm the solution uh let's say if you call a student if you're learning or even if you're working as a data scientist you have a great consultant right now who is available to you for free all the time and that consultant is chat Deputy yes chat GPT can give you a Complete solution design here I am saying I have a fast food business I want to build this our chatbot and I want two features New Order and track order
okay chat GPT will give you a complete design step by step and the suggestion that it is giving is actually pretty good okay it's not like bluffing so it is saying you need to First use dialog flow it's a NLP platform it will understand input and generate responses then you need to use node.js For backend you can have MySQL as a database uh you can use express.js as an interface and web interface okay it is giving you one possible solution that you can use in our case we are not going to use node.js Etc because
we like Python and we would like to do this in Python and fast API uh so we will use that but in chat GPT I'm pretty sure if you say I don't want to use node.js I want to use Python show me an alternative approach it will tell you That particular Pro so I would say let's try it out okay so while you're watching the video maybe pause it and just try it out and see the solution steps it has given it's pretty darn amazing okay once you get all these steps you can also say
can you tell me step by step process of implementing this in dialog flow because dialog flow is just one component so then it will say okay these are the steps and by the way don't worry about these steps I'm just showing to You so that tomorrow if you work on any NLP or machine learning project you can use chat GPT or to come up with all these steps and it will tell you okay Define intent Define fulfillment and so on so let's go over this process step by step first thing we did is we Define
scope of work we had new order tracking order billing query store hours and we said we'll Implement only two things New Order and tracking order second one is chat board platform selection see chat GPT gave dialog flow but when I was talking to my friend he told me why do you use Amazon Alex then I did Google Google told me rasa and dialogflow and five other platforms I am now confused I don't know what to use so you have to do some research to figure out which platform is the best one for you and there
are no general answers there are specific answers for your specific need dialoflow might be better for a need of some other team rasa might be better so It's like dependent on the individual situation therefore you as a NLP engineer or a data scientist need to learn how to pick a right platform for your given project or a problem then we are going to obviously in this project use dialogflow I will tell you how I pick dialogflow and why I didn't use rasa in dialogflow we have to set up something called intent and fulfillment we'll go
into the details okay don't worry I know these are all jargons this Is all actually very easy I will tell you later on then we will build back-end and we will do it in Python and fast API on this channel there is a fast API tutorial which you can watch and get your fundamentals clear but yeah that's what we are doing we are not using node.js Etc and then in the end we will integrate this chat board into our website all right now how did we pick up dialogflow as a platform for this particular project
so see there are two Options whenever you are building any chatbot you can use our readymade framework such as dialogflow rasa Etc or you can go for custom implementation where you can use things like open AI llms hugging face Bloom foundational model and so on in our case what we want to do is we want to get some help from the export and once again that expert is chat GPD so in chatgpt you can say okay which platform is better initially I also asked questions should I use open Ai and build the boat it says
no doesn't make sense that we use open AI so now I'm saying okay dialogue flow and rasa which one is better and it will create a comparison table between both the platforms and when I look at it what I find is dialog flow gives lot of features out of the box so you have to do less work see here my business is facing problem I want to build a chatbot fast and release it soon so time to Market is my first priority although Rasa gives lot of customization time to Market is not as high as
dialogflow time to Market means how much time you take to ship your product which is chatbot in this case to Market how much time you take to complete this project that is time to Market and it's a very important parameter training data since dialogflow is a Google's platform it has vast amount of training data here you have to do some manual training a dialogflow is cloud-based so directly you can upload It to Cloud uh here rasa you have to build it and you have to kind of host it on your own okay uh so let's
see other differences Integrations uh once again dialogflow provides better integration so if you read this carefully by that I'm not gonna go into all these one by one you can read this and you will realize yourself that dialog flow is better for our use case uh we did not use open ai's llm Etc because our problem is not like a generative AI type Of problem our problem is user is placing an order he's asking for tracking Etc it's like very specific actions and for that dialog flow is the best then I got some confidence that
dialog flow is something I should use but I still want to do more research just to get a confirmation that I am making a right choice so naturally I would think which companies use dialog flow if dialogflow is not used by uh other companies then it means it would Have some problem so this is what I do I I always make sure the technology I'm using is mature it is stable it is used in the industry okay then I am not doing experiments for that company okay here I ask this question and it immediately tells
me Domino's is using dialogflow Ticketmaster Air Asia there are so many companies which are using dialog flow after reading that after confirming these companies are using it I am now damn sure that I have Confidence on dialogue flow I can go ahead and use it for my project all right let's move on to the next step which is I'm saying this graph folks so many people are watching my videos they are not subscribed if you can subscribe to this channel it will mean a lot to me it might be a small thing for you but
it's gonna make a huge difference in your life all right now let's do our dialog flow setup I type dialogflow in Google it is taking me to this Particular website and here uh it has information on two different versions of dialogflow which is CX and Es all right and now I am again confused which one is better for my needs okay and these two versions are actually a lot different uh in the way they work and they have this featured uh list and when I compared it I did not get much idea so what I
did is again I went to my consultant friend chat GPT I said look I want to build this chatbot I want to have just two Features my budget is limited you need to tell your conditions your situation to chargpt can you suggest me which is better and it is saying dialogflow yes is better because number one cost cost of es is much lesser compared to CX if you look at CX uh their pricing is I think it's it's bit costly per I think of 10 chat session they charge something like two dollar which is kind
of costly so I will go with es and you can just compare other uh factors why GPD saying You should use ESN not CX okay so once you say I have dialogflow es ready to go you can just click on go to console or you can just type directly dialogflow.cloud.com you will have to login using your Google credential if you don't have a Google account you need to create it and once you create that account see let me open this in uh in cognitive mode It will say sign in using Google so here you can
sign in and then you will come here okay now there is this concept of agent so I have already built this chat board so it is showing this but let me create a new one so here I will say create a new agent so new agent is essentially your new chatbot okay and here my chatbot name is mira mira chatbot for food delivery I know it's a long name But whatever and then I can just say create so when you create this it will use uh Google a cloud actually so this project is already on
the cloud you are not building locally by the way you are already building this particular project on the cloud so it already created a Google Cloud project so if you go to settings here uh this is the project ID on Google Cloud if you have used Google Cloud before you Will get what I'm talking about but anyways my agent or my kind of basic uh framework for my chatbot is ready and if you look at intents it has two intents so before I go here let's understand what exactly is the intent let's understand intense by
going over some examples I use this bank called PNC Bank here in us and when I call them for any help usually they will give me pre-canned list of options such as you want to Check your payment recent activity low star stolen card this is not new I think you must be experiencing these things for your banking needs and these are short of like a fixed pre-can option that you can choose from and these five are nothing but the intent okay so let's say if you have an NLP chatbot and it says how can I
help you you might say I want to make a payment then it will ask you a few other questions make a payment here is an Intent when a person is coming to a chatbot they might have different intent or intention such as they want to make a payment they want to check their balance they want to report a stolen card or lost card these are intent so it's a fancy word folks but actually you know what this is It's like a main function it's like what do you want to do so you have this natural
language English from that you want to derive or you want to map that sentence with specific Action that your back end should take okay so that is your intent in another conversation you might say can you check my recent activity here checking recent activity is an intent whatever I have outlined here these five things these are intent in our case you will say Okay Place me a new order so placing New Order is an intent then you can say I want to add two pav Bhajan one pizza so when we design the backend You will
get more better understanding adding these items into your order is also an intent okay then you can say I want to add mango let's see that is intent so these two are same intent this is a different intent this is like a pure beginning a new order whereas you are adding items to existing order and when you say that's all you mean complete my order so that is your intention I want you to complete my order and tell me how much money you are Going to charge me so that is order complete intent I hope
it is clear you can pause this video If you're not getting it it's really a simple concept then boot will say okay I place your order in scenario two you might also say hey remove Vada power from my order so removing Vodafone okay that means you are intending order remove okay so order removes kind of like a function or intention uh you might say hey please get rid of pizza you might use a Different word okay you can say remove it up how get rid of pizza I don't want Biryani all of these mean the
same intent which is order removed so in dialog flow we will map this we will map different English sentences that you can use for a specific intent so we'll say these 10 sentences mean this specific intent and don't worry it's a NLP tool so it's not like you have to specify all possible options it will figure out using its NLP capability Then you can say Okay track my order okay here the intent is track order okay so it's just a mapping between what user says and what action will be taken by a chat board or
your backend now let's understand the concept of entity folks if you are following the NLP playlist you already know what entities are but let's look at entities in our uh context so here when you say add to pav bhaji and one pizza to the order let's say if you make This call to a backend python backend python backend got an idea that all right you want to add few items to the order but what are those items here the items are pav bhaji and Pisa and the other thing that it wants to know is what
is the quantity of these items so quantities two and one so here these two things are needed so that you can call your python function you might have this function add to order where you need two piece of information name of the item The quantity so intent alone is not sufficient intent is what order at so using order ad maybe you call this function in Python back and add to order and don't worry we'll add all this it will be very clear folks if you're confused right now just hold on I will make things very
very clear no rocket science here just think intent order add means some python backend function we are calling which is add to order and as an argument it needs this Information how do you get that information you get that through entities so here power budgie is one entity which is called food item two and one these are another quantity which is called number it's a system generated entity in dialog flow number you can also Define custom entity you can call it okay these are quantity of food Dash quantity okay so I think it's now clear
let me give you one more example which I have discussed in our NLP tutorial Playlist before you have this particular sentence there are various entities present here okay what are those entities if you remember from NLP playlist just pause this video and make a guess I want to test your knowledge all right I guess you made a right guess here Elon Musk is an entity it's a person entity Tesla is another entity which is a company then 40 could be another entity which is a number Or you have this statement for example Michael Bloomberg founded
Bloomberg so Michael Bloomberg is a person and Bloomberg is a company name we used to use this a lot whenever we discuss this a name entity disambiguation problem it is called NAD where you have Bloomberg founded Bloomberg so word is same Bloomberg but first one means a person second one means the entity we can say Pandey ji founded Pandey Pandey is the name of the restaurant but the owner is Pandey so so that's a personal entity this one is a business or a company entity yeah so this is a playlist I was talking about if
you go to YouTube another playlist uh here in this particular video see uh we have a lot of videos where we discuss entities but uh in this particular video we did name entity recognization and dialog flow is internally using some of these techniques to recognize those entities Once you have these entities you can call python backend function very easy okay in the Second Use case of tracking order the 9992 number 9922 number that you give is a order ID entity and then you're taking uh these two and calling some python backend function saying track order
and you're giving this order ID so you are kind of getting a sense of this whole chatbot development process you have a dialog which is a conversation from conversation you are Extracting two things intent entity an intent is mapped to some python function ndte is nothing but the argument to that python function all right by default you get two intents in dialog flow so I clicked on intents and I'm seeing these two okay what are these exactly let me check welcome intent here there are couple of sections there is contacts events training phrases and tax
response let's look at training phrases First here it says what are the possible prompts that are accepted as part of that intent so when here on the top when I say hey yeah it is saying greetings how can I assist and you see it matched the intent default welcome intent so whenever I say here it will look into the current list of intents and based on the training phrases that you have provided it will Match that and this response that you're saying greetings how can I assist is coming from here you see so in the
tax response you have this four possible options and randomly it will give you different tax responses so let me try another one I will say howdy okay and it will say good day what can I do so you can see that here see so you understand I am typing some text in chatbot it is matching it with all The intent so I have two intense default welcome and default uh fallback and it is matching it with the welcome intent and whatever is the response for the welcome intent it will give you that in our chat
board we want to start a conversation by saying choose from New Order and track order so I will change the default welcome intent and I will keep this particular thing same you can say hi hello to begin the conversation but in the response I will remove all these and I will say this in the second response is just a variation by the way so every time you don't want to say the same thing that's why you are giving three different variations so user get different experience all the time okay when I hit save sometimes it
will take some time so you need to wait a little bit now when I say hey yeah see it is giving you one of these Responses what can I do you can say New Order or track order and by the way all these responses I copied from the text file which you can download by clicking on uh the link which you have in a video description I have given a zip folder with all the assets and if you expand that folder it will be zip file and if you extract that zip file you will get
this particular directory in that go to dialogflow assets opentraining phrases Dot txt and that file has all the three Text which I just copy pasted okay that is for default welcome intent okay now this looks good what I want to do next is let's go back click here and check fallback intent in the fallback intent uh the purpose of this intent is if chatbot doesn't understand what you're saying it will give one of these responses so if you say what is the Temperature today okay that's not the purpose of this chat but the chatbot doesn't
know how to handle that it will say sorry could you say again and that is coming from here see one of these responses it will randomly pick from one of these responses so what you want to do here is I want to delete all of these okay so let me delete and in the default fallback intent I want to say this so in the fallback intent I want to say I did not understand what you said please choose from New Order or track order and when you're placing new order by the way make sure you're
you're placing order from only these items because remember our menu is limited we have only nine items food items so we want to say this to user so that they are kind of guided in their conversation I will hit save and now when I say what is the temperature it will say I didn't understand look I don't know how to Handle that temperature question but I can take your order so you can say the New Order track order and you know whatever okay so my default intent and my welcome intent looks good now let's create
our first intent which is create an intent which is New Order so it is New Order so when you're placing a new order uh what kind of uh text you will give okay so let's try it so first you will start your conversation in your chat About saying hi it will tell you new order or track order what do you think user will say user will either say New Order or track order they might say New Order okay so you want to have this as a training phrase here so you can click on ADD training
phrases and add this as a new order now what else user might say okay they can say place a new order maybe okay they might say let me just copy yeah I I Just put two things here New Order and place New Order if you are looking for more training responses uh what you can do is you can go to chat GPT and you can say say this in five different ways and chat GPT will help you generate those training phrases and you if you have more training phrases it's going to help you okay so
it is advice that you have uh as many training phrases as you can okay uh now let's try this thing so I Will say hi and it will say New Order I will say New Order okay I have seen that chadji uh not charging dialogue flow sometimes takes us time so there is a delay here uh it is kind of training the agent uh so once it is trained maybe I will just wait for few seconds and then we'll we'll try okay let's try again so see now it worked it is saying New Order so
here it matches the intent so here you are typing whatever you will Type in uh your chatbot then it is matching the intent here and if you have any response it will give you a response here so what do you want to give in a response so let's add that so here in the text response I want to say this okay and once again you have this file folks so feel free to copy paste things from the file and then once you learn of course you can have your own responses So here when someone says
New Order I want to say starting a new order I I'm meaning the chatbot and you will guide people so it's it's important that while building a chat board you do some guiding you have to give them Clues on what they should type next that way the conversation will be very clear so now we have new order so let me start all over again and by the way uh I will talk about context later but let's see hi okay and now it is saying New Order so you'll say New Order now it is saying see
this thing you can start a new order by saying things like I want two pizzas one mangola C also specify quantity because you want user to specify quantity sometimes they say I want pizzas but how many peas are if you call a pizza shop they will tell you right so same thing should happen here you need to Clearly say specify quantity for each of these items okay so once again these are the two uh alternate Responses uh so at any time it will just kind of randomly say see here now it is saying starting a
new order all right this looks good now let's create the tagging order I only have the board build so I just copy copy paste the name here just to save the time okay so track order so tracking order will be our new intent and here in the tracking order what are the training phrases so once again I'm just going to copy paste from Here so you will say one of these three things okay so check this status of my order that could be one thing then the other one is track order and track existing order
once again if you want to add more training faces you can say to charge gpts how about this and it will generate five different variations so maybe you can include one of these variations I'm feeling lazy right now so I'll just say done and when Someone says I want to track my order what would be your response as a chat board you'll ask for a tracking ID correct okay so let's let's see let's start again so I will say hey it is saying New Order or track order when I say track order see it is
okay once again it it is taking I've seen this it is taking time so let's wait for few seconds a few moments later all right after some delay it is working now when you set track Order it is matching the track order intent but what should be your response your response should be what is your tracking ID give me your tracking ID things like that so let's add that in the text response here you can say what is the tracking ID or lesser order ID uh other things you can say I can just copy paste
from the other chatbot which I have already Built I'm sure please enter your order IDs things like that once again you can use chat GPT for this training purpose all right now let's test the whole flow again you are saying hi it is saying New Order track order you're saying track order now enter your order ID and you will say whatever 4560 whatever now this one it is not recognizing so we will look into this a little later but at least now my intent classification is Done for these two use cases it is able to
recognize if I'm talking about tracking the order on New Order okay so once again uh you can say fresh order and see it is recognizing it is New Order you're talking about new order you can say track my order please and okay sometimes I know it behaves weird it depends on adding more training phrases but it will look good overall And it is doing this intent classification so the question might be how does it work exactly whatever you're typing here uh it doesn't have to be exactly the same thing that you gave as a training
phrase so there is some kind of machine learning here which dialog flow uses so let me go over that what happens is um when you give some training phrases so for New Order I gave three training phrases New Order Place New Order and Fresh order Okay Google's dialog flow will generate more training phrases internally so these are the three I gave but internally by using ml model it will generate more training phrases and it will train an agent so that tomorrow if someone types something different it can still recognize same thing if you say tracking
order let's say these are the three training phases I give internally it will which create more training phrases and it will Train the agent once the agent is trained when you give any new text in the chat board on the top right when I say order status please the ml model for intent classification will kick in and it will try to classify this particular text in one of these indents so you are essentially doing intent classification here and it will say you know what this is track order intent For a different use case it will
say this is a new order intent so this way it can do intent classification using its internal model and if you want to know more about it you can just go to chatgpt and you can say what classification model does dialog flow uses for intent classification and it will give you some information okay so see here it is using irm like intent recognization model uh and here it uses Some of the annual natural language understanding techniques for doing that so you can do some reading on this one but so far uh things are looking good
as far as our basic basic workflow is concerned now let's improve uh some of our intents and add new ones let's look at the flow of the conversation so if you go back to our presentation okay you will notice that at some point we had this different Scenarios so the first scenario that we had was this how can I help New Order what would you like to order uh we covered up till this point now user will say I want to power bhaji and add pizza so let's handle this particular dialogue and for that I
will create a new intent called order dot add so you are adding items to order okay so we did first level classification New Order track order now if you have a new order you are adding Items to the order and how do you add that I will have to give different training expiration okay and those training expressions are user might say give me two plates of show layer and one cheese piece I know you're getting water in your mouth but ah now let's check this what is happening here is 2 and 1 it is already
recognizing them as a entity which is very good so 2 and 1 are called system entity sys DOT number these are default Entities for example email phone number these are all generic entities which dialog flow can recognize on its own okay let's give another training phrase what do you do where by the way when you place an order online what kind of statement you say just add all those statements here I like to order two plates of chole one cheese pizza three mango let's see now in this case it did not recognize this one as
a CIS number but that's okay you can double click and You can just say sis number see these are all numbers so let's recognize this also as a CIS number and here we want this to be one single entity so here it says number one and two I don't want that so you will just left Mouse click highlight it and just say sis number and you can also type this this DOT number see this is how you assign entities within the statement so I have these two uh training phrases let's hit save and let's create
an Entity for food item because see the quantity it is already recognizing but food item is something that we have to Define on our own for that I will go to entities I will create the new entity I will call it food item and I will Define synonyms I will do also fuzzy matching by the way fuzzy matching means if it is Loosely matching then also you know match it so see for Z matching is a Type of parameters which will approximately match and here the entity that you want to add is let's say I
only have those entities here so I'm just gonna copy paste just to save some item uh time on recording so how would you say you can say you can also say Mumbai maybe Mumbai foreign you can just say chole you can say C double actually you can say chole Puri instead of you can also say Puri chole okay variety of synonyms that you can give all right so I'm not going to waste the time here and I will just add it and I'll be back after few seconds okay there was some manual work but finally
I have defined all my synonyms once again you can use chat GPT for synonym let's say you want synonyms for ravados I give me synonyms for our Dosa And it will give you that so you can just copy paste these things into here and just hit this okay now our food item entity is defined let's go back to intent and in the order add let's start assigning that entity so here just left Mouse click assign that a food item so I will just say this is food item cheese pizza is also a food item mango
let's see is also a food item this is how you assign and it is same thing here You have to do this in every training phase if you do your setup in a proper way your chat board will work very smoothly so you have to be careful and not mess it up here sometimes people assign a wrong entity and they see weird results and they are like oh this is not working well it's a setup issue I added all the training phrases you can see there are two Page worth of training phrases here and I
assign the food items here So here you'll just click on it for example like click and then when you assign that entity it will do the work so many times when you're adding a training phase it will not automatically recognize those two so the way you do it is same method highlight it then just select food item then highlight the whole item safe food item this is how you assign entities in your statements if you notice I also put some additional Statements such as additionally I would like to add to Mango nursing because in the
conversation people will say order me to chole bhature and one Samosa and then after some time they say Okay add to pizza or additionally I need mango let's see I have added those kind of statements here as well in the same intent you can go to chat GPT you can say this is my phrase and say this in five different ways and you can use all of these training phrases as well it Will be some manual work but trust me it is very well worth it and all of these training phrases are available in the
file that I'm going to give you so all you have to do is copy paste and maybe you add a couple of more training phrases as well on your own then hit save and now we are all set how do you test it well you have the agent here so let's start a conversation you'll say hi New Order and then just say give me two And one plate of samosa and now see what happened is it first of all recognized that as order dot add intent so that part worked pretty well also in terms of
parameter see food item whatever Dosa and Samosa were recognized as a food item and two and one were number after doing this you can say hey add one mango let's see two and see once again order dot add one Mangola C so mangoes is recognized as a food item and one is a number you can see the entire response uh in terms of your Json API response this is something we'll be using when we write our fast API backend but here you can see food atom is Mangalore C this is one and so on so
this is working as expected now what can happen is the conversations are not perfect they will say hey add pizzas well but how many pizzas they didn't specify it okay And here see the chatbot is not smart enough it should say how many pizzas you want okay or specify food quantity for this you have to make both of these a required parameter okay so here go down here and just make them require parameter and if they are not specifying any parameter you need to have a prompt you can say please clearly specify a food quantity
And you can just give example uh you know I want two pizzas or three chole but or you can say it using word you don't have to specify the number all the time so that is the prompt for the food item you can say please specify food item from this list and that list is the list we can get it from the default intent I think okay Uh not not default I think in new DOT order we supplied that list so you can say please specify it from this list now when say hey add pizzas
I previously forgot to save it so whenever you are adding something make sure you hit the save now when I say hey add pizzas it will say please specify the food quantity and without that it's not gonna work you can say hey I want three Samosa but let's say I've forgot To write samosa I want 3 what okay if I want three Samosa okay somehow it's recognizing this as a food item Pizza by default I don't know why it is doing that but let's take another example I want two plate of Pani Puri so we
don't have panipur in our food menu so now it is saying please specify the food items from this list because it did not get the food item and because of the prompt that you have it is giving That guidance to the user so far it looks good now let's discuss something very important called context let's say you have conversation like this new order or track order and directly you say remove whatever your chat board will be confused what are you talking remove whatever from where so human conversations will always have a context context means whatever
I previously said you need to have that in memory and then when I say new thing you Need to relate that to what I said previously so here here remove what a pound doesn't make any sense but if the same conversation is like this see New Order what would you like to have what about to Biryani what else now when I say hey remove whatever that makes sense because you have a clear context so we need to define the context in dialog flow here when I start a new order your context is New Order you
can Define it in any possible way I'm going to give the name ongoing order this context name you can whatever it can be ABC whatever name you want to give I will say it is an ongoing order context when you start a new tracking that is an ongoing tracking so in that ongoing order you can have other intents such as you might add things to your order you might remove things from your order when you have ongoing tracking you might give multiple Tracking IDs but let's say if your context is ongoing tracking and if you
say hey remove it upon that doesn't make sense board will be confused so whenever you say statement like hey remove word upon one word upon to Biryani it has to be in this kind of context okay so we already defined a new order and Order dot add intent so that is that is this is order dot add intent but this has to be in the context ongoing order Remove should also be in the context ongoing order so let's define these things here in in our dialog flow in the way I do that is whenever you
are starting a new order in the context you have input context and output contacts so in the output context you will say ongoing order see it's just a simple text you type in and this Phi means conversation turns lifespan for how many iterations meaning when I say hey how are you uh hello Whatever that is one conversation done for example this is one conversation done okay boat end user that is bad that is one this is another conversation term this is third conversion term Phi is good enough if you want to edit it you can
just go and say seven okay I'll just keep this as a default value ongoing order so that is the context whenever you say new order for example look at this whenever you say New Order you are starting that new context that's why it Is in output context but when I come to this intent okay adding an order my ongoing order context is already set so it will be input as well as output context so let's let's do that uh here you can hit save and then go to your order ad and in both input and
output context it will be ongoing so ongoing order and ongoing order okay because after you say air three Biryani you can say just add two mango let's see you can keep on adding one by one that's Why it is in the output as well and when you do that you will see that context appearing on the right hand side so let's do that so I'll say hey then I will say New Order when I say New Order the context is ongoing order now I will say I want two pizzas and the context is still ongoing
order okay so you can keep on adding that now let's add uh the order dot remove intent so here I will say order dot removed because you might Potentially remove and now now we will use a different uh different convention which is whenever you are adding any intent which also has a context so here there will be ongoing order context okay input and output both so whenever you have context in your intent I will say context colon ongoing order this is just a naming convention I am using okay nothing Nothing fancy here as such and
the same thing I will now change order dot add and I will say context ongoing order okay whenever you see this uh rotating uh cogwheel you have to wait because it's kind of training and that was the reason some some of the times these things don't work you have to wait till the this rotation is complete okay so I have now total four intents these two have context set in it okay what's gonna Happen in a remove context let's add some training phrases once again to save the time I will use my I'll just copy
paste things from here uh so let's say this and now you'll realize that it is auto recognizing this in Samosa as one of the entities so I will do I will add all of this and we'll be back after some time I also assigned the entities most of it was assigned already but if it doesn't then make sure you are just highlighting And assigning it in the response you can say removed Dollar food item hit save and I'll show you what happens due to this when you say remove Pizza it will say pizza remote okay
so I can say remove Samosa for example and it will say remove Samosa so that is what happens when you give this kind of text response what I did is just to save time on recording I already added the other Intents which was tracking order for example right so here uh the context I have defined it as ongoing tracking which will be set whenever you are placing a tag order see output context is ongoing tracking then here I think in the tax response you should say what is your order ID so I have added all
of those things here and here in the file you will see all those things so make sure you are using this file adding all the responses and the intent In an appropriate way and looks like my chatbot is kind of ready we have still not hooked this with a back end so it will not do a complete end-to-end work but at least we can now check the whole flow so I will say hi it is saying New Order or track order which is good then I will say New Order okay I want to show laboratory
and one mango let's see and it is the matching the indent also in the food item in numbers you're Getting the right quantities oh add one Samosa as well and once again it is matching that one samosa so that is accurate please remove mango Lassie we want to check if it is matching the see now sometimes people can say remove mango let's say they might make a spelling error so instead of 2s I'm saying one Lassie okay that did not quite match actually Let's see yes so mango let's say it did not recognize that so
let's see what's going on so in our entities food items uh we have mango lassi so I've seen this sometimes it doesn't work uh you have to do more training and it might work so mango let's see synonym is specified let's check our intent remove intent do we have that kind of training phrase here remove Pizza from my order we don't have just remove I'll just add that remove some also and I'll just say save it will require a lot of training iterations folks so you have to be patient Supply as many training phrases as
you can this is still spinning will wait till that stops okay now let's try it remove mango let's see okay please can you remove mango let's see this is still ongoing order it did not Match the food item why is it not matching so this is removed from also remove angle let's see uh okay looks like folks that could be a delay I have seen this or there is a delay but we are going to assume this is all setup okay working it and then you can say that's it so it will match the order
complete intent which I have created already by the way so see order complete just to save time so right now if you're Practicing make sure you are adding this particular intent order complete where the input context is ongoing order and see when the people they finish the order what would they say place an order uh no thank you uh because uh when you're adding any item let me show you that part here what you will say is in the text response you will say anything else right when you call someone for food order they'll say
okay two Pisa one Mangular C they'll say anything else you'll say yeah too okay uh that's the reason you added anything else and in the other intent now you have to say uh let's see where it is order complete you will say place an order no thank you let me just show you here so you will start with New Order okay you will say two pizzas N1 mango lesson now you're saying anything else do you want anything else you say nope That's it and it will match order complete indent so it will say so here
awesome your order is placed say awesome your order is placed here is your order ID okay so we have kind of rough skeleton ready if you want to test it you will go to Integrations and here click on web demo you'll just say enable after enabling you'll click on this and this kind of chat board will come up you will say hi and you'll send new order you will say To Pizza one let's see anything else you'll say yes [Music] okay see I did not say how many what about that's why it's saying yes one
word upon you have to say one two anything else uh nope that's it and now see awesome you are already placed so the skeleton is working now let's work on integrating our backend And how do you do that well you do it through something called fulfillment so here in order add if you scroll down you will see this fulfillment section and here I am going to say enable web hook for this intent what is webhook when you add anything into your order you want to call a backend okay so let me hit save here and
then we will go to fulfillment here in the Fulfillment you can have Google cloud function if you have deployed your Backend as a Google Cloud function you can use that but I'm going to use a web hook in the web hook you have to specify the URL where your backend server is running okay now you will need some knowledge of fast API for this so just go to YouTube and just say code Basics fast API and you will find this tutorial if you don't know first API I suggest you watch it so that your fundamentals
are clear I Will still go over some of the fundamentals here in this video but that is what we are using for the Fulfillment all right so our overall architecture is going to be dialogflow will recognize the intent and the entities then we will call our fast API backend server that will be a python code which will save or retrieve records from my SQL database as a prerequisite for building backend we need to set up our database first we are using MySQL I have installed MySQL Workbench if you don't know about MySQL Etc go to
YouTube say code Basics MySQL tutorial you'll find my tutorial so it will tell you how to install and just the basics of it we are going to import the database for our food business and the way you do that is you go to server data import from self-contained file now the folder that you have downloaded it has this database subfolder which Will have this particular SQL file so just copy the path of that SQL file here okay I'm going to use that and just say hit import and let's see if we need to select any
other options yeah this looks good and that will import the database you hit the refresh icon and you will see this database here right click and say set or as default schema Now in terms of tables we have food item table if you click on this particular icon you will see all the items that we have which is see Power version Etc these are item IDs and these are the price let's say six dollar seven dollar Etc then you can go to orders table click here and we already have some existing orders in this table
so the way order works is let's say Someone ordered this 40 so 40 order ID has what so 40 order ID has item number one and item number one is pav bhaji actually so this person ordered how many quantity two and then the second ID he has is three which is pizza so he ordered one pizza okay so e order to pav bhaji and one pizza so that is one order and the second order is this 41. so you can kind of Understand the design here and the total price is for this one so one
pound boy G is six dollars so two will be 12 so you see 12 here then similarly item number four is let's say mangola C price for one mango list is five and since you order three five into three it will be fifteen dollars pretty straightforward database design here and the third table is order tracking where you will see in what status the Order is when someone places an order it will be in progress then when the order is ready it will be ready status and then when order gets delivered let's assume it or swiggy
guy comes picks up the order and then when they are delivering it to your home in between the order is in in transit status so there are various status see for example this order is in in transit and when order gets delivered it will be in delivered status So that's the basic setup we have for the database you can go through the columns Etc just to understand database design if you click on the second icon see use the primary key Etc I hope you have some basic SQL knowledge if you don't once again go to
YouTube look for any MySQL tutorial you can say code Basics MySQL tutorial just get your fundamentals clear otherwise see it's pretty in due to anyways so as you work on uh this particular tutorial you will Get more understanding now let's do our backend setup we are going to use pycharm as an editor if you don't have pycham installed just Google pychon Community Edition and install it for Windows or Mac whatever OS you have so this is a community version you should click on this download and then when you launch it it will ask you to
open an existing project okay so here maybe you can open the or the code that we I have already given it to you or I think that Better idea is you create a new project okay so what you will do is you will go to let's say C directory let's say I have this code folder okay and I have uh now created a new folder called food chatbot let's let's create that folder and then you just say open and from open you will select that directory C code and then food chatbot and okay so you're
opening that particular folder here so here in This folder we already see this file main dot Pi so which is good enough here first thing we are going to do is install couple of modules so those modules are fast API first of all so let me show you how to install it probably you know the way to install it in Python which is PIP install use a pip install fast API all this is how you will install fast API and uvcon which is something we will use to run the fast API backend so first run
that Uh and if you are looking for some documentation you can go to Google Just say fast API tutorial you will find this particular link and they have a very nice page you can see all the help here people install fast API all when you do that it will install two things the first API package itself in uvicon which is something we'll use to run the backend server the other thing you want to use is my SQL connector so my SQL let's see what is the name my SQL it is Called MySQL I have by
the way requirement.txt file uh just in case so here the requirement.txt file says that MySQL connector python so let me copy paste that okay so these are the two modules you're installing I already have is installed that's why you see this message you can also use my requirement.txt file which you have downloaded here once you've put that file let's say I copy this file Here uh this is another way of installing those two modules I think you should have some Basics clear for python you should know how to do these things so here let me
go to this particular folder it's called food and once I'm in that folder I can also do since I have requirement.txt file what this will do is essentially it will just install these two modules which are Specified so either you install manually or through requirement.txt file all right my installation part is over now for first API we will set up the basic server once again you can go to YouTube and look at my video or if you don't want to go through the whole video Let's copy paste this code and run our first server okay
so that is in main dot pi so this is the code that we wrote We imported fast API class from the module And here we are just writing a basic routine that is the entry point for our backend server so just say save and then go to your console so here I will run it from terminal here okay and the way you do that is by saying uvcon main app reload and when you do that it will run a server on this particular address 127001 colon 8000 and when I go and look at that server
here in my See it says hello world by the way localhost and 12700.1 they both are same thing and when I hit this particular URL it will go to This Server because this server is running on 8000 port and it will print this particular message you can just change the message if you want okay so my basic uh fast API backend is all set up basically the skeleton is set up now let's add our code and in terms of our code what we're going to do is We'll first tackle the track order okay so track
order is you give a order ID and it will tell you which status uh the order is in uh we looked at the database already we have two orders 1441 so we will say Okay 40 then it should return deliver 41 it should return in transit so we'll tackle that piece first because that's relatively easier so let's go to our dialog flow and set up to the Fulfillment okay so I will go here Uh let me just manage it here and go to fulfillment just say enable the webhook and here you are going to specify
the URL what is our URL our URL is this it is http okay see we are seeing an error fulfillment URL must be https so https is a secure HTTP protocol what we are running right now is HTTP so That's not gonna work and how do you now set up https so what I did is I asked this question to chat GPT and chagibit told me that I need to install something called ngrok okay so just go to Google and just say ngrok install I'll tell you what it does it's a simple tool so you
go to here then for Windows I have Windows machine just download this file so when you download this file obviously it is a ZIP file when you extract it all It will uh give you this dot exe file so this dot exe file I am going to go into my directory and copy so in the food chat board I will copy that file here this file will create something called an secure tunnel now what is secure tunnel let me just show it so that you get an idea so first of all you need to open
a Command prompt so I will open a Windows command prompt by the way so this is Windows command prompt okay and you will go to see code 4 and here you see ngraph.exe so ngrok http 8000. our server is running on 8000 port and the protocol that we are using right now is http so let's see what happens when I do this when I do this It will give me uh let me just make yeah it will give me mapping of this HTTP to https what I mean by this is I can access the same
server using this link it's like an alias it is creating another link but this particular link now is a secure link okay and you will do https okay no connection made okay it says bad gateway maybe I should do it again so let's do it oh yeah I made a mistake it should be 8 000. there was a Typo here it should be 8000 I said 800. see now you are seeing the same message so what is the difference now now you are calling it using https previously you are calling it using http okay so
at least this requirement is cleared out I can go and copy paste this in my dialog flow so in dialogflow we'll be using this particular URL as our web hook um auth all these things you can keep it empty and just hit save now to write the Implementation for track order handling I went to my best friend Mr C yes chat GPT and I say that I want to write python code using fast repair backend I have two intense order add order remove let's write function for it and chat GPD does pretty good job with
writing these uh handlers okay so all we are going to do is copy paste this code of course we'll understand what this code is we'll copy paste uh and it's folks it see it's completely okay to use chat GPT as your Helper because it helps you save time you might not know the syntax you might not know that to get intent you have to go to query result and all these parameters but chatgpt knows it so you can use that gpt's knowledge and expertise here and it is completely okay to use it to write code
in fact if you don't use it to write code you will be behind because you will spend lot of time figuring like syntax related things out So here I copy pasted that routine okay and now see here chat GPT kind of generates different type of code so this time it generated different code but previously when I was using it it generated Discord so I'm just using that and and both of the codes are fine okay so our entry point Main entry point in the server is handle request so this request that you're getting will be
what dialog flow is sending now you'll you'll be like okay where do I see dialog for Flow request well here you can go to dialogflow okay and if you type any intent let's say you say uh hey and if you say track order in diagnostic info you will see that request so let's see this request okay this request has response ID query result then it will have intent things like that so query text is this and where is query result uh okay I think we need to let me do this So here in the track
order I will go and I will say fulfillment enable web hook call for fulfillment then only it will go and hit the backend so just say save here just say hi and just say track order and it regenerate a request so what is the request the request looks something like this it has Intent which is intent and display name will have the exit intent so if you look at our back end that's what we are doing so we have a payload which has a query result element if you see here see this is the payload
this is the whole payload query result is sub element under query result you have intent and then display name so under query result you have intended display name so you get intent so what is your intent tractor or order Okay now what are the parameters you need to get parameters too so this is query result and parameters so query result parameters right now parameters are empty because we are giving that parameter in the next conversation turn so this will have the parameters which you will copy here and you will also have the output context the
whole output context you will grab it in this particular element now you can say if Intent is equal to a track order then you need to fulfill fulfill means you need to say I received your response and the way you do that is by the way again syntaxial sugar you can always use chat GPT to figure out how it should look like and chat GPT is telling me you can return it like this okay return whatever message and I'm going to use what chat GPT gave me previously which is both both of these things are
valid By the way you can say received whatever intent let's say you received track order correct so I will say received track order and I will just put this thing so that you know that this is the internet is Reading in the back end just say hit save uh this is just a skeleton just to figure out the connection that request is going to backend and response is correctly coming back in my uh dialog Flow so let's go to dialogflow and test this out really quick Here track order has fulfillment enable previously we were saying
what is the order ID Etc uh you know what I should probably enable uh this fulfillment on the other one because here when you say track order we are already asking please enter your order ID for this we don't need to go to backend if you remember see the conversion starts like hey what do you want to do now you say track Order then dialog flow itself will say what is the order ID so for this you don't need to go to backend you need to go to backend to process that order ID and if
you remember we have the second intent I think this is the intent where we should enable web hook track order context ongoing tracking here you will be accepting that order ID and you will be processing it so let's say user says ID is 453. at that time You need to enable the web hook so what will happen when you say enable webhook it will go it will call this particular URL when you call this URL our request will come here then you will recognize what is the intent so right now the intent is not track
order but it is this one what is the intent name once again copy paste from here so this is the intent that you will get and upon receiving that you are saying Do this thing okay now let me just uh and exit the Zen mode I'm in the Zen mode by the way I was meditating and since we run that that thing remember we ran that uh uicon main app reload what happens now is since you said a Reload whenever you make any code change and when you say Ctrl s to save it it will
restart that server so my server is already running I don't need to run that particular Command again so now everything is set we can in fact do our testing so let's do it I will say hey okay then track order okay what is my order ID I will say 41. and see now it is saying received so at least it is hitting the back end hurray we checked the end-to-end connection it is receiving this particular intent in the back end so that part is very well tested now but obviously we don't want To send that
dummy message we want to send the actual status so we have to do something here okay what are we going to do so let's me let me go to my zen mode I want to meditate again uh it's a focus mode here Let's uh write another function for handling order so we'll say track order and what should that function receive well I can receive order ID directly but what I'll do is I will write a generic function which receives these three things intent parameter and output contacts so that you kind of get everything in one
shot so let me get parameters first I think parameters is enough parameters this parameters is a dictionary by the way so I will use Python hint type hint and just say dictionary I don't need Intent or output context because intent is already classified here you know it is tracking order so you don't need to send that and this is it now you will get the order ID from here so let me do this thing here what I will say is called a call track order and Supply parameters and from track order I want to send
back the response so where is my Order ID so I am receiving parameters how do I get order ID from parameter let's go back to dialog flow and here let's check this diagnostic info and in the request you will see that order is under query result parameters and order order ID and it's a dictionary so we only get query result parameters into parameters So within parameters if I say order ID so my order ID is this this is how you get an order ID Once you have the order you need to Now call our database
and from database you need to retrieve that information once again I'm going to use my best friend Mr C and I said hey I have mySQL database which has this particular table it has a column called status primary key order ID write a python code that can get me a status and look at this it first tells me hey dude you need to install MySQL connector python which already we Already did and it gave me this beautiful code I checked it the code works like a charm so we will just use this particular code here
and we are going to create a separate file by the way I can see I can I can put all my database handling in the same file but it's always a good idea to create separate files so I will create a new file what should we call it um DB helper maybe foreign API helper you will say API underscore Helper and so on and here I am going to just copy paste all this code copy code you will see chat GPT improves your productivity by a whole lot okay and you've got this thing here and
I'll just change the credential Etc so you know my local it's my localhost my root password and my database is called Pandey G itray if you check in Our MySQL see Pandey E3 that's why I have that and this function I can remove this get order status because we already established a connection through this Global variable uh and then here we are running this function called select get order status which takes order ID as an input and here we are running a query you are executing it Fetch one cursor close I think this looks perfect
see now I can call this function here so we'll first import import DB Helper and from DB helper DB helper will say get order status this will be our status here so you can say status is this and what should we do next so we got the order status now if there is a database error Etc you can Still get none as a status so if you have ordered status then your message should look like uh okay let me create this fulfillment text variable and my message is going to look something like this I'm just
using copy paste to save time on recording folks but it will be the order status for order ID this is order underscore status we are using python format strings here and otherwise you can say be found error or something sum message which Accurately conveys to user that we had difficulty finding the order that you give and in the Fulfillment text now I can just say this hit Ctrl s save it will restart the server Let's test the whole scenario again from our dialog flow I'll go here I will usually I have this habit of resetting
context so that the previous chat session just clears out I will start all over again I'll say Hey Okay track my order 41 is my order and we got some error so let's see what happened okay if you're getting any error you can go to fulfillment status and looks like there was some error failed to pass web hook Json expected message object but got null so I will go to my backend first and I don't see any problem here and here also there is no issue okay I think the problem here is Although we are
returning Json response from here but look at this from our main function we are not returning that so whatever response we get here we need to store it in response here and we return response or we just you know just to make it easier we just save this and just say hit save it will restart the server and come back here just say again 41 And it will say the order is in transit hooray but it says 41.0 well we can handle that that's probably okay here you can just say end order something like that
correct so if you do end order it will not do that dot zero message this one so 41 42. no order found with order id42 which is correct because if you look at our table we don't have anything in 42 let's say 40 40 is delivered so you give 40 as an order ID And it will say it is delivered no it is not saying that okay what happened let's see okay my SQL connection is not available I think I need to close my connection probably that is why this is happening there are some issue
with my DB so let's see if I'm closing so here I'm closing the connection and when I close the connection uh it is not available yeah so I I don't Need to close the connection as such because I will have that connection open and I I want to perform this query so again let's restart this server and let's try this out again now you will give different texts so instead of just giving okay let's do all all of this again so I will say reset contacts I will say hey it will say New Order track
order okay track order what is your order ID I will tell here Is my order ID 56. no order found with 56 so you notice two things I did not give exit text see I said here is my order ID equal to 56. now I have here is my order number you see I don't have the exit tax but dialog flows NLP classification is able to detect that this means this particular intent so it is matching this intensity it doesn't have to be same training phrase that you Gave during training uh so 56 is not
found when you say 40 say it is saying delivered hooray so see we made a very good progress and we already have a track order intent implemented now let's move on to the other intent the question is for which other intents we need to have this web hook calling we already implemented ongoing tracking track order doesn't need any fulfillment let's look at New Order New Order also doesn't need any fulfillment because when you create a new order here it is saying create a new order place a new order Etc okay so see this intent gets
generated when you say let me reset the context here when you say hey and when you say new order at that time this new DOT order see this intense gets generated now to handle this intent you don't need any back end because you're only just giving a fixed response so for this particular Intent fix response from dialogflow is enough you don't need to call fast API now as a next section person will say to pizzas and one mango let's see at this time let's see what we need to do we are not committing the order
to database yet we are asking user do you need anything else and for that we use this this other indent so let's let's go there so order dot add so we are invoking this intent we are taking food item quantity and we Are storing it in some temporary buffer so you are saying okay anything else anything else and you're saving it in temporary buffer okay so what is that buffer by the way we need to think about it where do we store that data because user might have converges and you'll say oh no added one
Dosa or remove one mango let's see dialogflow doesn't have any facility where you can store all this temporary information you have to go to our Backend so we need to enable web hook for sure so which I have already done here and in the back end we need to have some temporary variable where we store all that information now one idea is you can store that information in order table so you can have a transient order like the order which is not complete you can say this is in progress order so you can have a
status column saying that in progress order or completed order and by The way that that status is different here okay this is a delivery status here this is the order status like user is still placing an order so I have decided to use just a back-end variable because I don't think we need to go to database in unnecessary complicated things uh so think about it if we have a python dictionary so let's say I have a dictionary here which stores session ID so every chat conversation you are doing is one Session so you say hi
Place New Order two Pizza this is the Bill thank you close the chat that is one session so if you have a dictionary where you have a session okay so this could be session id1 and here you store that order you say Okay pizzas too Samosa let's say one and when user says complete the order okay and by the way you can have a different session here so other user who Is talking to your chat board might have a different session ID and they say Okay I want to put sholay order okay just one chole
and that's it you can have this kind of session IDs in this common dictionary which we will call in progress order you can give any name okay so let's say you create this dictionary called in progress orders where you have all these things stored and when user says now I'm done please finish the order at that time you will Take that order from this dictionary and store it in database I think that approach is going to work pause the video If you're not getting it it's really easy just just do some thinking uh you will
find it pretty easy now where do we commit so when user say add this add that he will be invoking this ongoing order intent but when they say place an order done that's it you are Invoking order complete intent at that time when this in time gets inward so let me enable the Fulfillment for this particular thing when this intent get uh invoked at that moment I will come here I will take that order and insert that record in a database okay so that's the overall design so let's create this particular variable it will be
a global variable And here I will handle all the other intents so what is the other intent that we have the other intent is order complete okay actually we have one more which is intent is equal to which is the other one this one order dot add and then order dot remove also so you will do all your handling Uh so you are realizing there are a lot of ifas condition and I'm thinking what could be a better way to handle I don't want to have this big if else condition I just want to have
a mapping see this is a mapping if it is track order call track order function if it is order add then you will call some function call add to order we'll Define that function but essentially you have intent and for each intent you are calling a unique Function and luckily for all the functions parameters are same so it's gonna work if you create maybe a dictionary so let's say if I create this kind of dictionary here intent Handler dictionary where you have intent in the function you are calling is also called like a routing table
you are routing this call to here this call to there Etc I think that will work and that will avoid the need of this big and ugly if Else condition and then what you can do is see you have intent correct so when you pass an intend into a dictionary place the order add intent it will give you a function so this will be replaced by this function correct so let's say if you replace this by this function how do you call this function value add parameters and session ID you need to add parameters in
session it So let me just add parameter session I will talk about it later uh you need to just give parameters and that add to our order is nothing but when you supply intend into that dictionary you get one of these functions and you call that function by calling this so I hope that thing is very clear to you let's now Define our add to order function so Define add to order And here we have parameters dictionary and by the way uh I will for now Ctrl slash I will just hide this uh and just
handle these two or where do you get your food item so first you need to get food item and quantities so how do you get that let's look at dialog flow once again and look at the diagnostic info so food items are under parameters under parameters if you say food item you get That and number you get the number so let's get those two here so food items numbers you got it now what if someone is not supplying equal number of arguments here if someone says add pizza and two mango let's see now we want
the parameters to be same here okay but here what's happening is you have two uh items in the list here whereas here you have only one one item so I'm going to just say If Lan of food items is not equal to line of quantities then you you need to specify the quantities clear we don't want to handle it we want to keep things simple and say hey specify quantities clearly otherwise I will say received what what did we receive Items and quantities in the back end once again I'm just testing the flow and then
we'll implement it and after that what you do is you return this Ctrl s and now let's test the add to order scenario so here I will just reset everything start all over again hey New order two pizzas and one mangola 6. see it works received Pizza mangula C and this in the back end so it came all the way traveled all the way till here and it is returning the response but if you don't give similar quantities so I will say add to Mango let's see and shoulder but I'm not specifying how many it
will say sorry I didn't understand can you please specify food items and quantities clearly so This will not work but if I do two level today that will work see this is this 2 2 so it's 2 and 2 so it is passing the language so here it says two it is converting it into a number so far we are returning just the fixed response now let's think how we can aggregate our order by aggregate what I mean is a user might say initially I need two Pizza and one Samosa let's say and in the
second statement when chatbot Says do you need anything else they will say oh well add one mango lassi they can give their a wish or the items one by one so what we need to do is when they say this we will be making a call to our add to order function correct so here in this function we need to create a dictionary somehow which says Pizza is two and Samosa is let's say one And when you give one more item to add it should update this dictionary and it should add mangola Cinder you need
to remember it needs to remember the old item so your dictionary food item dictionary will look like something like this and when they say no that's it place an order at that time we'll take this and commit it into our database so let's not worry about saving it into database let's Think how we can form this dictionary and all these orders might be coming in subsequent calls to this add to order function one thing we already discussed is we have this in progress order uh dictionary so we can use that dictionary here and we can
append to that now going over architecture what will happen is you might have multiple chat boards see you might have multiple sessions when we Deploy this into production there might be 100 users talking to these chat boards and there are multiple instances but these will call our backend server and our backend server also might have multiple instances running let's say we deploy our server uh in AWS or Google Cloud and we decide to run two instance or three instance of the server each of these servers might be handling multiple chat requests so you need to
remember Session ID so per chat request let's say this chat will have one session ID this will have a different session ID and based on that you need to create this kind of dictionary so in this dictionary the key will be session ID and for that session total order that users have placed so let's say uh session id1 is this particular conversation okay let's say this is session id1 here they first say pizza and Samosa Then there's a pizza Samosa and mango let's see so in that Session One Pizza Samosa and mangola soup okay this
kind of a thing needs to happen so initially uh when they say add to order in our session ID we'll get this when they say add mango lassi at that time we will add it so you need to update the dictionary associated with that same session ID I hope this is making sense now if you think about it it's really a Simple a computer science architectural principle that we are following so the first question that we need to ask is how do you get the SNL because that's the key if you look at dialog flow
once again and if you look at the payload that payload should have a session ID so in this dialog flow request if you go to Output context see there is a session added here so after sessions whatever this string is it is session ID We already get output context correct if you look at our code we already have let me remove or let me just comment this part and output context so from this output context which looks something like this I want to get a session ID how do you get that well once again I
asked chat GPT and if you do if you know about regular expressions in Python you should be able To figure it out your logic should be any string that appears between slash sessions and slash context that is your session ID so I asked that question GPT let's see where is that question yes I asked that question and it gave me this code and this is the right code so I tested this in regex101.com by the way if you don't know about regular Expressions I Highly suggest that you develop that skill because as a python engineer
or as a data scientist you need to have good knowledge on regular expression uh so I have a tutorial if you go to YouTube core Basics regex you will find this it's a 20-25 minute video but I go over all the fundamentals of regular expression so here using this website I tested it that when you have this kind of uh pattern it will extract the number Between sessions and contacts so see it is extracting that number here if I change that number and if I say okay that number is let's say this number is 456.
see it will retrieve four five six so let's use this we are going to copy paste this code okay and I have already by the way created a function so I will just copy paste the whole function here and that function is called extracting session ID from the Session stream so you give a session string and you let me import the re module here then from that string you can get the extracted string now I want to keep a separate file for these functions because we might add more functions like this in the future so
what I'd like to do is I like to have this generic helper file so here you can say this is a generic helper dot pi and I will move this function there so let me move this function here maybe this one you can move it at the top and import this particular file here so you say import generic helper and when you want to extract that session string you will just say generic helper Dot extract session ID From the Assassin string now what is the session string well it is in the output context okay and
in the output context you need to uh take the first elements output context by the way is the you see it's a list in that list take the first one and just say name so I'll take the first one and I will say name and that will give me the Session ID which I can pass it to all my functions so this session ID I am going to pass it to all my functions okay so now I have the session ID if you want to test these functions individually what I do is if unders underscore
name is equal to under underscore mean you can write a unit test also but let's let's test this function so In here let's give this particular string and let's see what happens so here I am going to give the string okay and I'm expecting it should return this string 7 F 23 and the long long string whatever it is so print and you can just right click and run it okay this code is not working as expected because it is returning sessions and context both you don't want that But actually let's see if that is
the code that we had yes the extracted text should be match dot group one so I don't know if we did that match dot group one do we have that let's see actually should be group one not the whole string because in the if you know the fundamentals of regular expression the first one will be the whole match and group one will be whatever is in the bracket so that is what we want see now you're getting this string 7f if You change the string here okay so if I say one two three unless if
that is my string session string so it will return me one two three so now you have tested this function you can rely that this function works okay and then you're using that function here 2x take the session ID okay so now I got session ID what do I do next so let's go back to here and here in the add to order function I am going To add the session ID string parameters so this is the parameter I am adding and here I need to do something with that not here actually here because this
is the error condition so we are handling that anyway all right now let's look at our structure first you need to form this dictionary that is the first task then you need to put it in the in progress order Dictionary how do you form this dictionary because what you have here is two separate lists so you have one list which contains the food items the other list can stay contains the numbers you want to create this kind of dictionary once again I asked my dear friend Chad GPT that I have this kind of tool list
how do I convert this dictionary and it gave me this particular python code so Folks by watching this video today I am walking you through my process of how I build end-to-end applications and you are realizing I use chat GPT as a productivity tool of course I I try to understand that code so it's not I am blindly copying because chat GPD makes mistakes but it saves me lot of time in finding the syntax you know structuring the code and so on therefore I am a big fan of this tool I encourage all of you
to use it you'll realize it will save you a lot Of time so here if you're two lists like this and if you run this code it will create this dictionary if you don't believe chat GPT well you can test it in Python and the way you do that is you go to python console and in Python console you can type that and see print food addict And see it does work actually so I will just go ahead and use that code here so I will just say if Lan not equal to this then the
food dictionary I'm using just that code okay so zip and then zip will create the pairs and dict will create the dictionary out of it so this will be the new food dictionary why do I call it new food dictionary because your existing uh dictionary might Already have these two so say a user said I need two P's and one Samosa initially so you already have this dictionary okay so let's say you already have this particular dictionary and in the new call that you received when user said oh well add one mango let's see at
that time you are going to get only this dictionary so you got only this dictionary now you need to append this dictionary into this One so that is our next step how do you take the session ID you have a session ID here you have a session ID along with session ID you have this dictionary and this dictionary is this new food dictionary that is a new incoming order first check if this session ID exists in the in progress order and if it exists then append but if it doesn't exist then you Have to enter
a key enter as a new key okay these are just basic dictionary fundamentals that I'm talking about so here you will say if session ID in in progress order then do something otherwise let's say session ID is not in the in progress order let's say you don't have this big deal then use what you do is you you take this session ID and just create the key value pair so let's do that first I Will handle that scenario first I will say take the session ID and that is my new food deck but if it
already exists let's say if this is the scenario if it already exists such as this as an id1 unless you have something if it already exists then now you need to merge these two dictionaries okay so how do you merge this first let me get The old one so this is the old one okay I will call it current for dict that is your old one in this you need to append the new one so how do you do that once again chat GPD chat GPT is telling that if you have these two dictionaries if
you want to update dictionary one you just say dict1 dot update dick2 and the addict one will be having all of these So this is the way you kind of merge two dictionaries so let's go back to python code and say current food dot update update it with new food deck and then you need to put it here back here I need to verify it will if it will automatically update because it's the same object so it should but just to be on a safe side I'll just put it here by the way you can
verify in your code if it does then you don't need this Particular line anyways now I took care of that logic where if this guy comes in then we are taking this here and we are updating it here perfect now I have my this thing so actually my goal is accomplished now I need to send this message back to my user saying that uh this thing is already updated okay so how do you do that for this particular order You want to return a message to a user saying that so far you have two pizza
and one mango let's see you have this dictionary how do you convert it into this message where you are saying two Pisa one mangula C so you are essentially taking the value of the key in a dictionary first then the key then end then this and then mango let's see if you're just one recipes only you'll say you have two P's and that's it you will Not have and Etc once again I use my friend chat GPD I'm saying I have this kind of dictionary write a Python program that outputs this see you want to
convert this dictionary into this kind of text and once again uh chat GPD gave me the answer I think this has little mistake probably you need a list here for using list comprehension so I did some minor tweak and I figured it out so let's add that particular Function in our generic helper so the generic helper we are going to add this new function called get string from food dictionary and that food dictionary that we're taking uh we're going to call that food addict which is a type hint of debt and here we will use
whatever chat cptq plus I made some trick here so that is this and let's test this function so here print let's say get Str from footbrict let's Say you have Samosa to foreign it is putting the value here first then this then chole and then this okay perfect so now let's use this function here so we're going to use this function from generic helper and create that string so here I think we will call that order string order string is generic helper And it is not a new food dictator just to be safe and secure
I will just use whatever the in progress ordered dictionary is giving me back when I do in progress order session ID stationary is this it will return me this dictionary here which I am passing it to that function and this function anyway is converting that dictionary to this string and we we don't want to just set to so Much of actually we want to say so far you have you know that kind of string so here let me change this to so far you have this order do you need anything else perfect I think it
is looking good I mean if it is not then it will give error and we'll figure it out so you do control as to save it and let's see where we are yes it restarted it okay so now we'll go back To mgrok first by the way because it might time out hey so yeah one thing about ngrok you see restart and grocked if you have not created any account or anything this session will end after some time so remember to restart it so I'll do Ctrl C to end that session and then I will
just restart it's the ngrok http 800. and it restarted this session and my new string is this So just highlight and then write Mouse click so it will copy it and you will put it back into fulfillment I mean when you are in production you don't have to do it but since this is in development we are still building we have to do it if you don't want to go through all of this you have to go to and rock and I think register for an account that is one way to solve it all right
now I think we are good okay Okay let's try this I will say hey okay I will say New Order what do you want to order two pizzas and one mango let's see now see this is what happens so far you have two P seven mango let's see why because it created this particular order let me print the whole dictionary here so that you get an idea so I will print it here printed with star so that there is some kind of Visual indication uh review print so you get a feeling of what's going on
here so Ctrl s it is restarting and I will go back here reset everything I'll start over say hi then New Order then I will say two Pisa one mango let's see now look at this C star so in progress and oh actually you know What I want to print the session ID as well never mind we'll have to do it again so reset contacts do it all over again hi New Order two pizza and one mango let's see see this is the session ID this is the session ID that I was talking about session
IDC session ID then you have pizza and mangola C now when I say chole it should append to This particular dictionary so let's say chole let's add some yummy chole only and it will work why did it work well remember in the entities you said and if you look at the dictionary now see I have chole but today here and if some user from some other session makes other order it will it will maintain that okay so looks like our ordering looks good now yes so I will just remove this print Rest because we just
added it to confirm few things now we are going to look at a very interesting part which is saving the order in a database once you have ordered all these you will say nope that's it or complete my order because chatbot is saying so far you have two Pizza one mango let's see two solo but today do you need anything else and then you will say no that's it that's it that's all I had and at that time The intent that is going to be triggered is this order complete context ongoing order in our backend
we have that particular intent mapped here to a function called complete order and right now that function is empty so let's write some code for for that in this function you are getting a session ID so using that session ID you need to Find a key in our in progress order dictionary because our in progress order dictionary looks something like this and if it's session id1 then this is the order you are saving to database if it is session id2 this is the order you are saying to your database so first check if session ID
in in progress order if it is not in uh then it means you have some problem so in the Fulfillment text you will say you know what I'm having trouble finding Your order you need to probably place your order again because I we don't have that order due to some error or something but let's say if you find that okay then your order the actual food order will be this and I'll just call it order so this order will get you let's say pizza mangolis Etc now you have to insert this record in a database
and Just to make things easier uh we should probably call a function called save to database let's let's create a new function called save to database which looks something like this save to DB order which is a dictionary all right now take a pause and let's think what we need to do in this particular function let's look at our database Or in our database we have things like this we want to insert a next order in the orders table so first thing we need is order ID so what will be the order very well what
we can do is just say whatever is my Max order ID add one to it that will be my next ID so max order ID is 41 so my next order ID will be 41 plus 1 which is 42 and to get Max order ID you know the function so select Max from orders select next Order ID from orders and it gives 41. okay so let's let's write a function just to get the next available order ID and for that also I use chat chat GPD for that and it gave me this function so I
copy pasted this function make sure you don't include cnx.close that's why I see blindly copying from chat GPT is never going to be beneficial you have to look at the code you have to think you need to have those fundamentals I know That you can't close a connection like this in the middle of the function so I removed it and I created this new function let me add this new function here I will call it what should we call it get next order ID Maybe or get next available order order ID and that function I'm
not going to write the whole function because I pretty much copy paste and moved to the code from Chat GPT but just to let you know City selecting the Max and whatever Max you're getting it is saying that Max plus one but let's say if you don't get any result let's say your order table is empty then return one okay so now we get let's say the order ID what do you do next next you need to insert a record here so let's say if I am ordering two Pizza first thing you need to do
is using Pizza string find out the item ID so see Pizza string Item ID is three okay so from this derive the item ID that is your first step okay get Item ID which will be this once you have that and now you have two pizza so see two pizzas is your input so what did you do from Pizza you derive the item id which is three Then in the step two I think step one is really get the max ID so I'm that is Step Zero so I'm skipping that now get the price get
the price of two visa because this is the I price for One Visa you have to get the price for two visa which will be the get the price and multiply it by the quantity which is two and then you insert it here so your order ID item id quantity and total price now luckily we have stored Procedure in our MySQL table called get price for item and if you uh look at any item see Pisa here it will give you the that pizza price and then we have second store procedure get total order price
so that will be or I think we should be looking at this insert order item so here see when you say insert order item you can give food item which is pizza Then quantity and then order ID and the price etcetera it will retrieve on its own so that this is good actually so using insert order item store procedure we can insert those items here but this stored procedure is taking one food item at a time we have multiple food items so we need to run some kind of for Loop here okay so go back
and in the save to database function we have order so this order Order can be Pizza 2 so lay one it could be this kind of dictionary we need to insert these items one by one and the way you do that is you write a for Loop you will say for food item quantity in order dot items when you do this on a dictionary you will get food item and the quantity and Then you will insert so now in our DB helper we need to pop we need to add one method called insert order item
let's say we have a method called insert order item where you can supply food item you can supply quantity and you supply next order ID then this will work so how to get next order ID by the way we have that function already so D we offer Dot get next order ID and that is your next order ID so that part is taken care of but we need to write this function insert order item so I'll go here I'll create a new function here death insert order item and that method is taking three parameters these
three okay once again I use chat dpd and say that I have this kind of store procedure just Write me a function and it it wrote me a nice beautiful function which I am going to copy paste here see it is calling the insert order item store procedure this is how you call it and if the order is not successfully reply uh placed you roll back and you do it uh you know printing Etc okay so this thing is done now let's come back to this function so here now that you have insert order item
I want To check the return code here because it might return -1 which is not good anyway if it returns minus one then you know if let's say you have any database error I will return minus one from here too and we'll order we will handle this thing in save to database function so we will do that but for now let's say everything worked okay at that time Uh at that time we are pretty much done and we can return that that order ID that order ID back to this function so here this will be
order ID okay so let's say I saved my record in a database I can say hey your order is placed uh here is your order ID okay or here is your order total actually order total is also is something you need to mention but let's first handle order ID Okay order ID by the way can be minus 1 which indicates The database error in that case you can say if order ID is equal to minus 1. then you can return some message saying that sorry I could not reply I could not place your order try
it again but if it plays the order then you will say awesome I have placed your order this is the order ID okay and you are also going to say order total so how do you get order total where for order total wouldn't it be Nice if you have a function in our DB helper which says okay get total order price and you just give order ID luckily we have the stored procedure for such a function so here if you say get actually get price for the item and get total price yes okay so for
order ID 40 let's say total price is 12 and 8. 20 so if I say 40 here said is giving me 20. so we already have a stored procedure which we can call Okay so I will say get total order price so I talked to chat GPT and I said I have a store procedure called get total order price where I give order ID can you write a python code for me and it's not very surprising it showed a nice python code which looks something like this let me just uh show that code it to
you so that code looks something like this you take order ID you select Uh this is where you're calling that user defined function by the way it's not a store procedure it's a user defined function that I'm calling and very straightforward function it will return the total order price and once again you can use that if under the underscore name is equal to main to test that particular function okay I got the order total what do I do next now okay I have ordered total all of my Fulfillment text looks good I can return that
okay I can return it perfect but we need to do one more thing when an order is placed let's say this order is placed we need to remove this record from our in progress order otherwise it will mess things up and to remove that we can use this function called Del so if you do that it will remove That that whole thing from our dictionary okay now let's go back to our database and let's think one more time if we are completing everything so let's say incoming order came in you placed let's say it was
pizza you you inserted two two records order ID will be same 42 so 40 to 42 and the pizza and should I go to the record okay so that's it but we have another table called order tracking so here also we need to insert a record remember when order is placed The order should also go into order tracking and we can just say this is in progress Etc so let's do that one last piece and that will come in save to DB function by the way because save to DB is the function which kind of
saves everything to our database and here it will be my wish that my DB helper has a routine call insert order tracking Where I insert my next order ID and let's say the status I want to insert is in progress so I want to have this function which takes order ID in progress and all it will do is it will insert less order ID and this is the status progress or status correct and I used uh chat GPT once again to kind of write that function so I'm just gonna copy paste folks I know it's
lot of coding so I want to save your Time you are given all this code so take your time to digest this this is a long tutorial I understand folks but we are building end to end serious project here so obviously you need to help patients Whoever has patience and completes this till the end is the person who is going to get maximum benefit out of this okay so now inserted so the order is inserted into order tracking table as well Looks like we have done most of the things uh that could be an error
I don't know so I will just restart my server and I will now do the true end-to-end test so let's try everything once again I will say hi Place me New Order what do you want to order two pizzas and one mango let's see it it has that in our in progress order Dictionary you will say nope that's it and see it placed an order the order ID is 42 the total is 21 let's verify it in our database so right now database has only two orders see 40 and 41 1441 in both the tables
now let's run it again 42 see this is 42 folks this is what we ordered just now and same thing here 42 in progress see and the total is 16 and 5 21. so our Chatbot is saying 21 which is correct and you placed I think two Pizza one sholay whatever perfect so now if this is like end to end tested you can also Place New Order and test that scenario where you know you are adding more and more items so let's say add two mangola c and or you can say add one word up
so two mango let's see two shoulders and you can say now complete the order And you will say we have placed your order 43 see order number 43 should have three items let's go back here 43 you see it has three items amazing let me go over that session ID logic so that there is no confusion left in production there will be multiple chat windows and the server the first API backend that we are writing there could be multiple servers that we can deploy to our cloud provider it could be AWS gcp Etc and one
server will be Receiving requests from multiple chat Windows opening at the same time so let's say server one receives two chat windows at the same time the first person says add five solar battery in the back end we will add session ID for that session and we will say chole file order then they can say oh can you also add one word upon and in that same session ID we will add a Vada at the same time other person might Start a chat say New Order now in this in New Order we don't need to
do anything but when they say I need two P's and one plate Samosa we will add session ID 2 in our dictionary and we'll add two pizza and one samosa so just pause this video and just visualize this picture you your understanding will be very very clear chat window there is a session ID and the order for that session is stored as a value for that key in the dictionary Now this person will say complete my order so we'll save the record in database and we will remove the session IDC session ID one is gone
here see it was there before but when this says complete at that time we remove the session ID and this person can continue having a conversation and that was the reason in our code you noticed we deleted that particular key so if you See that delete code that we wrote uh where is it yes see this delete line we wrote because once the order is saved to a database we want to remove it from the dictionary now let's move on to our last piece which is the remove okay so we have handled most of uh
the intentions and now the only intention that is remaining is order dot remove okay so let me comment this code and we are going to write this remove from order function This function will be actually simple uh when you're having a conversation say add to whatever one mango let's say this happens in real life person might say oh you know what please remove mango or say I don't want to order that they just change their mind while they are on the call same thing in NLP chatbot they will remove some items at any point of
time so what we need to do is let's say for session id2 right now it is word upon mangulasi and if that person says remove Mango let's see what do we need to do step by step step one is locate this session ID record okay so that's session ID let's say if it is session id2 that record will be this once you locate it step number two is get the key from the dictionary get the value from the dictionary actually value from debt which is actually this so like locate the session record will be this
whole thing and then when you locate the Value from the deck it will be this dictionary and step number three will be remove the four items that you are asking to remove now in the request you will receive an array of food items that you want to remove you will receive so let's say whatever you might uh receive multiple items so let's say in your order there is pizza as well okay I'm just adding one more item to make it clear and if the person says remove Vada pav And pizza now what we need to
do is we need to either through this array and we need to also enter through the keys in this particular dictionary and remove those key value pairs so that is the simple logic that we are going to follow here so let's go back to the python code and first thing obviously we are going to check if whether this session ID exists or not okay so you will say if session ID not in in Progress order see if the if it is not there then there is some error condition and you can tell to the user
that looks some error happen buddy sorry you need to place a new order but let's say if session ID is there then you need to stationary there is meaning let's say this is a session ID so you need to locate this order so this order is what in progress order in bracket session ID so let's do that so In progress order okay and session ID this will give you this particular record so let's store this record in a variable called current order so this current order once again is this particular dictionary and you want to
get from the request the items which are being removed let's say Vodafone and pizza is being removed let me add pizza Here just to make this picture complete so that food items will be coming from parameters okay and the parameters it will be food item we will see in the dialog flow how this works but the parameter name is is essentially food items so now you are going through all those four items so let's say I'm going through Vada power and Pisa so first I get what about and that whatever you look into this Particular
dictionary which is your current order so you will say if item first you want to check see let's say Vada power might not be there because of some error someone said let's say those are here well you're trying to remove don'tsa from this order which doesn't have the answer that's an error condition so you need to handle it uh the way we are going to handle it is we'll say if item not in current order do something We'll worry about that later but let's say if it is in current items okay you will delete from
that current order you will delete it you will delete what item that's it so you needed it from the item and then you will say Okay item is removed blah blah blah but there could be scenarios where as we saw in if condition item is not there so you want to kind of track which items are removed and which items do not exist So for that I will create an array called remove items so whenever item gets removed I want to track which items got removed okay so I will just put that in one list
and the items which are not removed which we did not find will say no such items so no such items no such items dot append item when we are done with this for Loop we know which items were removed from the order which items did not exist and the way we Can utilize this information is we can say if Lan of removed item is greater than zero meaning we removed some items then we will say my fulfillment text is equal to its form string we will say V remote XYZ items from your order that message
will send back to the user so that we can display it in our chat window if length of No Such item let's say you did not find some items so here in our case if we say remove Vada Point Pisa unless if We said not what about pizza but let's say now it will be able to remove whatever so the message should say removed from order and the other one is no item or let's say not found in your order you want to just be very very clear so here you are saying which items were
removed here you are saying which items so if length of No Such items is greater Than zero then from current order your current order let's say does not have these items this is the message that we want to pass and if length of current order dot keys is equal to 0 which means your order is essentially empty let's say a current order here due to some reason I don't know what could be that reason unless it's blank it means you have an empty order So you want to display that as well well after sorry after
removing it after removing the item let's say your order is blank okay so let's say you had only this thing one thing whatever and you remove it upon so your order is blank okay uh we will also have as condition your order does is not blank so you want to display which items are remaining in the order so what items are remaining what whatever is your current order this Current order those are the items remaining right so how do you convert this current uh order into a string format so we already wrote this function called
get string from foot dick so we'll use we will just use that function few minutes back so the same function we are going to use here and say your uh okay and this is generic helper so from generic helper call this function get the information on which items are Removed and what what is left now so this this is the current order and we'll say here is what is left in your order so these are the only things which are left in your order and after that we can return the response okay I'm doing some
copy paste to save again time on my recording hope you understand all right my function I think looks pretty good we Kept track of removed items no such items and Json response is being returned so let's test this thing end to end so the server is already restarted I'm going to go to my dialog flow so let's locate the direct flow here you'll reset all the contacts you will say hi we are forming a new order here so you will say New Order I need you can say two mangle of Finch Oliver today so see
Two mango let's see and cholesterol uh wait it did not queue the ah see it did not give the response saying that if so far you have this and whenever that happens I go to diagnostic info see fulfillment status is again that same and grock error because if you look at your ngrok the session has expired so you need to restart it it will generate a new string I know it's a pain to kind of copy paste the new string Anytime if you don't want to go through the pane just create a new account on
Android so right click I will go to fulfillment and save it it should work now all right I will say hi okay new order [Music] two mango let's see and two Chola battery now see I got this response which means my back end is okay You can also remove items now so let's say I have two mango let's see two children please I don't want to eat too much oily okay so it says removed it says remote today but it doesn't say what items are left okay it here is what is left it did not
say that so why it did not say that it should say here is what is left let me see okay maybe we can taste this whole thing again so let me put some stress here so we came all The way here and we have this removed items from the current order after that agnostic info oh yeah because I did not enable the Fulfillment yes that's the reason so order dot remove you need to enable the Fulfillment here so go here see the web Hook was not enabled and it was just giving me this default response
let me remove this response so that it's not confusing and let's try this again New Order I hope you are getting lot of water in your mouth while working on this project uh so you have done a lot of hard work so maybe order some of these things and just enjoy give your mind a break okay you have two mangoes let's see left now you can say that's it that's it please place an order awesome we placed an order order number 44. if you go to the database you will find A record order number 44
should be there 44 is there hooray I need to pay 10 at the time of delivery uh and let's say you wait you're very hungry you're waiting for 15 minute order is not there yet you will say hey track order what is the order ID 44 or 43 44 is not available okay why did I get that okay let's check so here track order takes one position argument but two given got it so remember we are passing two arguments so One we need to pass session ID as well although we are not using it but
it's a string I think that was the error so you can hit save restart the server you can once again say track order 44 and it is in progress at some point when the order is dispatched someone from the back end the other system will change this order from in progress to let's say in transit okay that's gonna happen through some other system and when that happens and When now you tracked order number 44 it will say it is in transit okay it is saying it is in progress maybe it is little slow I already
applied applied okay I see it is going on because I have an as open connection actually so the order is not committed because my connection is still open in my back end so I need to exit I need to just I think I can just restart the server and when you restart the server here Let's see see this came it in transit and now when you check 44 it should say in transit so this is working good end to end now let's go to Integrations and through integration see it provides variety of Integrations where you
can integrate your chat board to twilio a Via Way a variety of platforms slack you name it there's a web demo by the way if you click on it if it says enable button just click on that and when you click on This thing you'll get this kind of window and here see hey what do you want to do new order this is now full-fledged chatbot two pizzas and it plays the order sorry I did not understand oh yeah see TWP I had a typo two pizzas one rava dancer and it will add that order
uh hey please remove I don't want rawatosa I want to add Masala dosa actually I change my mind add one masala dosa see we need to specify quantity and now I have two Pizza one masala dosa do you need anything else nope your order is placed 45 okay and you can also track the order what is your order ID 45 okay you can also say my order ID is 41 you can use different type of language and it will still work so my chat board is ready uh as a Next Step we are going to
integrate this chat Board into a website let's now create a website very quickly once again using chat GPT I went to chat GPD and said create me a website for Pandey Eatery the website should have visual look and feel and it should have only following items so it created me a layout which looks pretty good like very basic structure then I said create HTML and CSS code for it and you can give any specific guideline let's say you want orange Orange theme or red or yellow theme whatever you like it will generate a basic HTML
and CSS score it will not generate images by the way so immediately is something you have to get on your own I took all of this code I gave a lot of instructions I'm not going to go over all of them because this is not a web development tutorial essentially but I just want to highlight that using chat GPT even if you don't Know web programming you can still create a website and I have given the code of that in the front-end directory so if you have downloaded our main folder you would see front-end directory
so that front-end directory has all these files so I give couple of images so I created these images On My Own by the way uh using variety of tools and then I also got some Banner from the subscription that I have I have put I I Use PowerPoint and created all these logos all of this was done pretty quickly and food is a directory which has the food website okay okay so this is I think something different but home.html is the main HTML page and this is called uh your cascading uh style sheet you know
it defines the visual look and feel of the website now when you double click on this particular website you will see Uh the website comes up and this particular thing comes up at the bottom okay so this is this is what comes up when you say hi it will say New Order or track order and the way we got this chat window in our website is using this iframe control so this I frame controller you can just copy paste in it into your website code so here I have my HTML code so I copy pasted
it and all it is doing is it is going to through some Cloud function Https console.dilockflow.com and it is giving this specific ID for that chatbot I also created by the way this special class called chat board and put it in my style sheet and I used chat GPT for it so if you know how to install chat GPT you can create website easily uh and now that I have a website I can test the whole thing end to end on the website itself so here on the website I will say Place me a new
order It will say what order I will say one biryani and anything else no please [Music] you know complete the order complete the order and the order is completed see it's working there is a website there is a chat board and it is working end to end you can say track order and 46. all right so This is complete our website is ready uh once again I did not go through a lot of HTML and CSS fundamentals here because this is not a web development tutorial it is an NLP tutorial uh you will find all
the code in the front-end directory so when you download this you will you will have three type of directories front-end back-end and dialogflow assets so front end will have all the website code backend has our obviously our python code our database has database Dump and this assets have the training phrases so hooray good job website is complete and we have a chat board integrated into our website now now comes the most interesting part of this entire tutorial which is exercise folks without practicing you're not going to learn it so I am assuming so far you
have practice along with me and I'm going to give you two exercises the first one is there is an issue with incomplete order and this will happen in Real life too when you deploy the first version of your chat board it's not going to be perfect there will be issues which will be reported by Beta users or even in production you will find issues so you have to go through the iteration to make your chat board perfect here what is happening is let's say you added two Pizza in your order correct so right now is
saying you have two Pizza in your order but someone let's say by mistake without completing the order they are Saying New Order you see at this point body is asking you do you need anything else you should say no please complete the order but you are not saying that you are saying New Order and you have started New Order whereas the previous order of two Pisa is incomplete okay now let's say I want rawa Dosa this time so two rava dosa okay look at that that's a problem I only ordered two robot Dosa because the
New Order forget everything that I said before but it is still remembering that you order two Pizza in the previous iteration this is a problem whenever someone says New Order you need to clean your cash you need to clean your in progress order dictionary so that is one exercise that you have to work on the second exercise is actually simple the only two option New Order track order add a third option called store hours and when someone says give me store Hours you should return a fixed response you don't need to call back in just
from dialogflow text response you can return this folks this is going to be very easy okay so please don't complain you can do this very very easily once you have done both of these exercises write a nice LinkedIn post where you are summarizing your work you're saying I did this project here are my key learnings what are the things you need to take care when building a chat board such as Developing intents going through the flow going through that whole cycle talk about MVP scope of work you should talk about non-technical things in this LinkedIn
post and then post this on LinkedIn see LinkedIn is super important folks because it helps you build online credibility in the post you can give credits if you like this video obviously and you can also just give a tag like we have on LinkedIn we have a company so you can just say hashtag code Basics you Can put my name my co-partner Hammond's name and doing that activity on LinkedIn can actually help you get a job because there is a person who got a job by doing this so what we do is on code Basics
dot Io if you go we conduct this resume project challenges and in that challenge we give your data set and you build analytics dashboard and so on and we evaluate their entries and these people have a habit of writing LinkedIn posts so Irish summer was the First winner he wrote this LinkedIn post and actually he got a job as a data analyst in integ enterprising just because of this post because when he became winner I posted separately saying that uh you know Aaron won this Etc and he got an interview call but if you don't
consider that whole resume project challenge in general if you're learning something post it on LinkedIn because other people are going to notice your word they are going to notice your English return skill iron did a nice presentation by the way see this is an amazing presentation that he made and here he shared all his insights maybe for this project you can make a nice presentation in PowerPoint or whatever presentation tool you like and present your Insight because presentation skills are very important for a data scientist or even NLP engineer so by doing that you are
getting a chance to practice those soft skills and you're showcasing Those skills to the world so folks this is actually very important okay do not ignore this you have watched this entire video you have spent so much hard work on this so far so please finish that hard work by completing those two simple exercises and also finishing uh this particular aspect of the exercise which is posting on LinkedIn see he's he's tagging my company he's saying thanks whatever you can do all of that another thing I want to mention is on core Basic.io resume a
project challenge we are going to post new challenge on dialogue flow only so folks if you have not please register it's free by the way this whole project challenge is free you don't have to pay a single uh penny for this make a login to codebasic.io and whenever new challenge comes out we send email to those folks that challenge will be based on whatever you learned from today's tutorial and whoever wins we give them Prizes to okay so all that information is here go to codebasic.io resume your projects you will find that make sure you
participate in that upcoming challenge it will be on building chatbot using dialog flow and we will take a different use case all together but whatever you learn from this video is going to be useful to you [Music] thank you