07 Dialogflow CX Conditional routes with expressions

Course: Learn Dialogflow CX

Lesson: Introduction to the quiz bot

Introduction to the quiz bot

[00:00] So the next thing that we are going to take a look at is the quiz bot and I will use this to explain how the conditional equations work in CX, okay, that is the conditional references. What the quiz bot allows you to do is it is going to ask the user some questions. So here it is asking which of these is used to keep track of what happened within a conversation and let me give an answer. So this is the correct answer, so it says correct and then which of these is used to tune the accuracy of your Dialogflow agent, let’s say I choose the wrong answer, then it is going to say incorrect, the correct answer is and then followed by whatever the answer is supposed to be.

And then when your Dialogflow bot cannot understand what the user said, what will be the first choice, let’s say if I choose the wrong answer, it says incorrect, the correct answer is blah blah, thanks for taking the quiz and then it also gives the user an option to start over, okay. So the first thing you have to notice in this bot is that whether or not the user answered correctly, it is still going to go to the next question, right, so that is clear and based on whether the user gave the correct or incorrect answer, it is going to give immediate feedback about whether or not the question was, the answer was right.

[01:15] And finally, one of the things that this bot is lacking is a score, it does not display a score to the user and the reason for that is that in Dialogflow ES, by the way, I have not mentioned that but this bot was built with ES, in Dialogflow ES, there is no way to calculate a score for a quiz like this without calling the webhook, okay and this particular example is not hooked up to a webhook, so it is just giving you at the end of the quiz, it just asks three questions and then it just says thanks for taking the quiz and gives the user an option to start over, so what we will do is we will try to build the same bot or you know, something very similar and also calculate the score and the best part about CX is that you can actually calculate the score without having to call the webhook.

[02:10]

Now, I just want to make something clear, it is possible in this particular scenario, I am not saying that any kind of complex bot can be built in CX without using a webhook but this logic is simple enough that you can actually build a bot in Dialogflow CX, all of it inside the console and you can still get the bot to display a score at the end and I am going to show you how in the next few lessons.

Lesson: Start quiz intent

Start quiz intent

[00:00] So we will get started by creating an intent which allows the user to start the quiz. So let’s create a new intent and call it start quiz. And you can trigger this intent by saying the word quiz or using start over. Let’s go ahead and save that. And as soon as this happens, what we want to do is we want to go to a new page. And this page is going to be Q1, which stands for question one, because we as soon as they start the quiz, we want to go to the first question.

Okay. And in the question one, we will add this following to the entry. That is the entry dialog. So I have this questions already over here. So let’s copy this and put it here. So it’s going to say which of these is used to keep track of what happened within the conversation. Okay. So pretty simple. I have not added any big logic yet, but let’s just check to see if this works. So if I were to say quiz, you can see that it’s moved on to the quiz Q1 page, and then it’s prompting you with this question.

[01:08] So we will see how to fill out the first question page in the next lesson.

Lesson: Question 1 Page

Question 1 Page

[00:00] So now that we have the Q1 entry dialogue set up to ask the question, let us consider what the user might do. They might either say, give the correct answer, in this case it is going to be context or they may give the wrong answer. The way you can handle this is you will add a transition route and this one will be called Q1.correct and this will be just the word context. Let us go ahead and save that and if they answered with the correct response, what you want to do is you want to say, you want the agent to say it was the correct answer and you want to calculate a score.

This is where we are keeping track of the score and I am going to say that I am going to create a new parameter called score and assign a value to it which is just 1, simple enough and then you also want to do one more thing which is as soon as this happens, you want to go to question number 2.

[01:07] Let us do that and then the next thing you are going to do is go to question number 2 and go to the entry dialogue and you are going to add the second question over there. Okay, let us go ahead and save it and we have to handle the other case where the user provided an incorrect answer. We will add another transition route Q1.incorrect and what I will do here is instead of typing out all the possible wrong answers, it is just far easier to use the wildcard entity to support this.

You just say wrong answer and let it click, you know, press enter and then go ahead and just mark it with the sys.any. No matter what they say except for the correct answer, it is going to fire this intent called Q1.incorrect. Okay, and then remember what I said, the agent has to give some feedback to the user.

[02:07] You will say incorrect, the correct answer is context. Okay, so that is the thing and then the parameter here has to be, score has to be set to 0 because of course they are not getting any points for this and then you are going to make sure that this also goes to the second page. Got it? Let us go ahead, just test the agent, we will say quiz and it is asking which of these is used to keep track of what happened.

I am going to say context. You can see that it is coming back with correct, which of these is used to tune the accuracy, which is the next question and it is moved to the Q2 page and then the score is 1. Now we see that it has given the correct answer and then we also see that it is moved to page, the correct page Q2. Now, it is gone to a different page.

[03:00] We cannot like test the other flow until we reset it. Let us do that and try quiz again and this time I am just going to give a wrong answer parameter and you can see that it says incorrect, the correct answer is context and it still moves to the next question and now the score has been set to 0. What we have done is whether the user provided the correct or the incorrect answer, it is moved on to the next question, it has prompted the user with the next question and based on whether the answer was right or not, we have made sure that we have assigned the value to score to be either 0 or to be 1 and then we will use this at the end to calculate how many answers the user got right.

Lesson: Question 2 Page

Question 2 Page

[00:00] So now that we have the first step of Q2 page setup, where it asks the question, let us do the intents for the Q2. And in the same way as in Q1, that is the question 1, what we will do is, we will create a new intent called Q2.correct. And in this case, the correct answer is going to be ML threshold, okay. So let us go ahead and save that. And here is where we start using the extra features that you get in Dialogflow CX, okay.

So let us just make this slightly bigger. Now what I’ll do here is, now Q2 is correct, but we do not really know whether the user got question 1 right or not, okay. So what we can do is, we can actually check for that. And the way you do that is, you go with session.params.score. You remember that we set the score to be 0 or 1, right. In case that the score is 1, let us just suppose that, okay.

[01:03] So what we have to do here is, we have to say correct, of course, as always. But what we should do in addition is that, we will have to set the score here to be 2. Because what we know is the score is already 1, that means we have to increment the score by 1 more, right. So the score, the value of the score has now got to become 2, okay. And then we will have to add a new page and we will call that Q3, okay.

So let us go over this one more time. You have Q2.correct as the intent which is firing. And then we check for this condition. We check for the condition where the session.params.score equals 1, which means that they got the first question right. And if that is true, then you have to change the score to become 2, okay. So you are incrementing it by a single point.

[02:00] Now let us go ahead and save it. So now that we have done that, you have to consider. So what happens if the user had provided the incorrect answer for the first question, right? So then their score will be 0 and we have to not increment it, which means that we keep it as it was. Now that is very interesting because what that also means is that you do not have to do anything to add that score in that case.

So what you can just do is you can just say Q2.correct, okay. And then you directly move to the next page, which is Q3. So what we have done here is we have not made any changes to the value of the score, right? Because we do not have to. We know that if they had scored a point before, then we have to increment it. If they did not, then we just leave the score as it was, which means that it was still at 0.

[03:01] Oh, sorry, I realized that I made a mistake there. So in that case, the score has to now become 1, right? Because their score before was 0 and the score has now become 1. So let me try to rephrase that. If the first question was answered correctly and we can have a check for that, then what you can do is you can create a condition here where you can add an expression to check what the score is, and then you can set it based on that.

So in the case where it was correct, you change the score to 2, you kind of update the score to 2. And if they got the first question incorrect, okay, and which means it will match this particular expression, then the score has to be updated to 1, right? The score was 0 before and now it’s 1.

[04:00] Now that I mentioned it, I think it might be a bit safer to actually add a check for that. So if the session.params.score is 0, then it’s going to turn to 1. If it was already 1, then the score is going to become 2, okay? And either way, it has to transition to the next page. And I also have to remember to say that this answer was correct, right? So let’s go ahead and save it. So with this, what we have is, let’s go to Q3 and add the last question there and save that.

So we are going to test it and we will try to make sure that the score is getting updated as we expect. So let’s say we start the quiz, the user says context, which is the correct answer. The score is now 1. And then for the next question, they answer ML threshold and see that the score has become 2.

[05:05] So let’s reset it, go to quiz, this time we say, sorry, we use the incorrect answer and then I use the correct answer. So you can see that the score is 1, just as we expected, okay? So that’s still not complete, as you might imagine, because we haven’t declared the incorrect routes for Q2, okay?

So let’s go ahead and do that. You add a new intent, Q2.incorrect, and this will be as before, wrong answer. And I’m going to select the whole thing, use the wildcard, save it. And now, as before, what we do is we have two different cases, right? Session.time.trigger.score, if it is 0, right?

[06:03] Okay, so this is an interesting one, now that we think about it a bit more. No matter what happens, if it is an incorrect intent, you are not actually going to update the score at all. So whether the score was left at 0 before or it was left at 1, that is whether the first question was answered correctly or it was answered incorrectly, if you provide an incorrect response for the second question, all you really need to do is mention that it is an incorrect answer, correct answer is ML threshold, right?

And then just simply move to the next question. I hope that makes sense because whether or not the first question was answered correctly or incorrectly, because we are not going to touch the score, we are not going to do anything to update it. In the case where the user provides an incorrect response to the second question, all it has to do is create this transition route where you see if the user’s answer was incorrect and if it is, you provide a response from the agent that it was incorrect, don’t make any changes and just transition to the Q3 page.

[07:16] So let us quickly test that also, okay, let us do quiz and let us say you have the correct answer for first one and the wrong answer for the second one and you can see that it says incorrect, the correct answer is ML threshold, moved on to the next question which is Q3 but the score is 1 which is exactly what we expect, okay.

Now, we, sorry, so we reset it, we go with quiz and this time I am going to provide a wrong answer for the first question and a wrong answer for the second question and just as we expect the score remains at 0, it has moved to page Q3, it has prompted the next question, it has given the response saying that it is incorrect.

[08:04] So till now the bot is behaving exactly as we expect, so we have to repeat the same thing which with a little more check, little more things to check in the next, in the next intent which we will go into in the next lesson.

Lesson: Question 3 Page

Question 3 Page

[00:00] So now that you understand how the intent routes are defined that is the transition routes I’m just going to quickly go ahead and do the same with question 3 Okay, and the question 3 the correct answer is supposed to be fallback intent. So let’s go ahead and save it So what you have to check now is you have to check a couple of things, right? If the session dot params dot score if it is still at zero Then you have to make sure that the score is become one.

Okay, and the agent will say correct Okay, and now then I am going to go to a new page and I’m just going to call it display score all right, so there is your display score and The score has to be updated to one if it is still zero because they have this correct, right? Let’s save it So that added one transition route Then we choose the same q3 dot correct.

[01:02] And this time we are going to check if session dot params dot score is one which means that they got one note of the last two questions, right? The agent has to say correct and this time the parameter has to become two Okay, then the same way you go to display score Save it and then Finally, we are going to do q3 dot correct the both answers the both the prior answers were correct, which means that the score would already be two Agent has to say it’s correct parameter has to be Three and It goes to display score.

Okay, so that that defines all the changes we need to make if the user got the third question, right and we also have to have a new intent for q3 dot incorrect and That is going to be It works the same way Let’s do that and save it and as you remember because we don’t have to make any updates in that case all we have to do is say it’s incorrect and correct answer is fallback intent Okay, and then we just transition over to Display score.

[02:30]

So let’s check how this works We have to the the important thing that we have to do here is to make sure that These scores that are getting computed seem to be reasonable. Okay, so let’s start the quiz I go with all correct responses the first time null threshold Fallback intent You can see that the score is 3 which is exactly what we expect. Okay, and of course I have not done anything here To show the score and all that but we will be doing that later and Let’s restart the flow quiz.

[03:10] So this time I’m going to give all incorrect answers parameter event System crash you can see that the score remains at zero. Let’s just do one more test So this time I’m going with first one is correct second one is incorrect Third one is correct Seems to be reasonable and we get a score of two as we expected now If you think about it, there are eight different paths to test right there are there’s like three Three questions each could have a binary of true or false that is correct or incorrect Which means that you have 2 to the power of 3 which is 8 total paths to check you can go ahead and check that For yourself, and I think that I have done it, right?

[04:07]

I hope that I have not made any mistake here, but even supposing I missed something here and there I think that this gives you a good picture of how this works So what what you have seen now just to recap what we have seen now is you can check the Based on which state you are in which is defined by the page right in this case is q3 and based on the users response, which you can infer from which intent is firing and Based on some other conditions you could check.

So in this case, the check is is the score still zero We could define specific action. So what I did in that case I made the score I set the score to 1 and then I moved to a new page Which is to say it goes to a new state So this is the whole idea behind adding these conditional references in Dialogflow CX because it just adds that one level more power to your part because what you can do now is on top of choosing the current state and The current intent that is whatever the user says it is going to be mapped to a specific intent You can add a third condition, right?

[05:24] You can add another check for some kind of condition not everything can be checked in the condition reference Unfortunately, you can go and take a look at this link to the syntax reference to see all the things you have available It’s not like It’s not like a programming language.

Okay, it’s there are some limitations, but it’s actually pretty powerful and Based on these three things you can choose to do some action you know display some result and then transition to a different state and all that and The combination of all these things actually makes Dialogflow CX a lot more powerful Than all the things that you could do in Dialogflow ES.

[06:05] So that is You know how you define the page Q3 and then we will wrap up this module with With just like one more video about how to get the score which is going to be the next lesson

Lesson: Display Score Page

Display Score Page

[00:00] So to wrap up, we will just do a display of the score. We already have done all the hard yards, which is we have calculated the score, made sure that we have checked all the conditions and all that. So the only thing which is left to do is we are going to, in the display score page, what we are going to do is we can say, thanks for taking the quiz, your final score is, and then you say something like $session.params.score out of three, because we know that they had answered three questions, right?

And this is going to say, let us say the score is two, you might say your final score is two out of three, okay? And the other thing that I am going to do is you might also want to transition to the start, and here you are not going to do anything other than check for true equals true. This is always going to fire, and then you can transition back to the first page, okay, start page.

[01:08] The benefit of doing that is that this allows the user to restart the quiz right away, okay? So let us go ahead and do that, test it. So you start with quiz, and I am going to give all correct answers, mlthresholdfallbackintent.

So you can see that it says, correct, thanks for taking the quiz, your final score is three out of three, and also you notice that it is moved to the start page. And one way to test this is if you were to say start over, you can see that it is gone back to the beginning of the quiz. So this time what I will do is I will just give all incorrect responses, history, event, system crash. And now, as you expect, you can see that it is saying, thanks for taking the quiz, your final score is zero out of three, and of course, it is moved to the start page one more time.

[02:06] So if I were to say quiz here, you can, so let me mix that this time, context, and the second question, I will get it right, mlthreshold, and the third one, I will get it wrong. And by the way, just for fun, let me, if I just type some garbage, because we use the sys.any, you know that this will still trigger the incorrect, the q3.incorrect. So as you expect, it says incorrect, the correct answer is fallback intent, thanks for taking the quiz, your final score is two out of three.

And you can also see that it has moved to the start page. So that wraps up this module where I talked about how you can use the conditional references, and you can make your chatbot, the Dialogflow CX chatbot even more powerful, that is you can, you get even more fine grained control over the state machine.

[03:02] And one of the things that I hope you have been thinking about is that look, it looks like you have declared a lot of intents in this, in this bot, right, it’s, if not a lot of intents, at least you will say a lot of transition routes, there are four in the page q3. And then there are three in the page q2. And then there are two in the page q1. And if you are noticing the pattern, if we had a fourth question, right, you will actually have even more checks to make, okay.

And I’m guessing that that would be like five transition routes. And then if you have one more question that will give you like six transition routes and, you know, it’s possible that you will have to come up with a lot of transition routes as you’re, as you keep increasing the number of questions, okay. So there is a good case to make for doing all the calculation just on the webhook. Because all you need to do then is, if q1 is correct, right over here, you can just set the score one to be one.

[04:05] And if q1 is incorrect, you can just set the value of score one to be zero. And in the case of the question number two, if the question two is correct, you can have another parameter called score two, and you can set its value to be one. And if it’s incorrect, you can have the same score two set to zero. And then once the whole conversation is, when you go to the display score intent or display score page, you can just send it over to your webhook and do the calculation there, right.

So of course, that means that the bot on the whole might be, in a way you can say that the simpler. So the reason I’m pointing this out is that as you’re building your bot with CX, you have to think about all these trade-offs, right. So the trade-off in a very short quiz like this, where it has only three questions, is that you can do the entire thing inside the Dialogflow CX console itself.

[05:05] You don’t have to rely on a programmer to build out the webhook logic for you. But at the same time, you don’t want to take it so far that you have a 10 question quiz and you try to do the entire thing just using these transition routes. And I’m pretty sure that if you try to do that, you will not be able to keep track of what is going on because that’s just too many transition routes to consider and too many different values.

Just as a, you know, consideration, if you think about 10 questions, that is actually 2 to the power of 10 number of paths that it could take. So that’s like, I think that’s 1024, right? So you will end up with like so many transition routes, you’ll end up like with hundreds of transition routes which you need to cover to make sure that you get the final score calculated if you want to do the entire thing inside of Dialogflow CX, which is not a good idea.

[06:00] So you definitely don’t want to do that. But this, I hope that what you took away from this series of lessons about the quiz bot is that CX allows you to do a lot of things which were not possible with Dialogflow ES, which is also why I insist that you don’t want to get jumped into CX first because, you know, it’s on the one hand, it’s very powerful, on the other hand, it’s also very complex. But you want to build as much of your bot in ES as possible and only transition to CX when your ES bot gets very complicated.

By the way, there is also another reason which is CX is, you have to pay for CX even for the very first use, okay. On the other hand, with Dialogflow ES, the trial or the free tier of Dialogflow ES is very generous, where they allow you to make like up to 180 requests per minute without being charged.

[07:02] And for a lot of bots, people who are starting a lot of bots, very unlikely that they will have a bot which has such a high throughput, you know. So there are those things to consider and ES in general is probably easier to deal with. So that’s the trade-off. So I hope that you consider all these trade-offs as you go and build your CX bots.

Lesson: Scope and the flow start page

Scope and the flow start page

[00:00] I just wanted to make one more final note about this QuizBot. And what I did is, if you remember, I had a transition route at the end of this display score where I just had true equals true and had the transition over to the start, okay. I have deleted the transition route here, and I’m just going to show you what happens in that case, okay. So let’s start with quiz. And I’m going to give all the correct answers, context, and ML threshold fallback intent.

So till now, everything is just exactly as expected. It’s giving the answer, and then it’s saying your final score is three out of three, which is right. And the page is still at display score, which means that it has not actually gone over to the start page. And you can see that the score is set as three, which is all good, right. Now, if I were to type the word start over or the word quiz, notice that it actually still goes to Q1, right.

[01:04] Even though the start page was not actually the page which had been transitioned to, if I were to use start over, it is still able to fire the intent which was in the start page. And this is because of the way scope is defined in Dialogflow CX. And by the way, that’s something that you might want to remember, okay. There is such a thing as, so taking a step back, when you provide an answer, and there is some handler for that, like an intent which will fire based on the current state, then naturally it will take that particular current page and it will give that page priority to for the handler, okay.

In other words, if you are at, let’s take the bot we already have. So let’s say that you are on page Q2, right, that is, you have come to this particular state.

[02:05] And then the bot is going to ask you a question and you are going to provide an answer, right. Now, what is it that makes sure that your answer is not accidentally triggering the intent in one of the other pages, right. If you have to give an incorrect answer in Q2, why is it not triggering Q1.incorrect? That’s because that page at that point is not having, it’s not in scope, okay.

That’s really what it means. But the start page of a given flow is always in scope, right. The Default Start Flow has a start page and no matter where you are in the conversation, if you were to say something which could potentially trigger one of the intents in the start page, it’s always going to be in scope, it’s always going to fire. It’s a lot like the non-entry candidate less, that is, context less intents you had in ES.

[03:08] If you have followed my material for Dialogflow ES, you know that I have this definition of what I call as entry intents, where those intents can fire at any point in the conversation because they are not guarded by an input context. The same logic applies for the start page in the flow that is currently active.

By the way, it only makes sense for the flow that is currently active. If you have multiple flows and one of the flows is active, the start page of a different flow is not going to be in scope, okay. There is no way that one of the intents in that could fire. But the current flow will have a current start page and that page has some intents and those are always going to be candidates to fire at any point in the conversation, okay, in CX. Right. When you build your state machine, that’s something that you need to consider.

[04:03] And one more thing you need to definitely consider is, don’t put anything in your start page which could be also firing somewhere down in the conversation. In other words, try to keep as few intents as possible in your start page, okay. And transition over to some other page as quickly as you can because unless the other page is in scope, the intents inside that page cannot actually fire, right.

So something to think about and as you get more practice with Dialogflow CX, this will also become more obvious to you. But it looks like the start page intents are prioritized over any other pages in that particular flow which means that you have to be very careful with the ambiguity in case, in cases where there is some intent in the start page which is very similar to some other intent in one of the following pages in that flow.

Leave a Reply