04 Dialogflow CX Entities and Parameters
Course: Learn Dialogflow CX
Lesson: Planets Bot: Introduction
[00:00] So in the next set of videos, what we are going to do is translate the PlanetsBot which I have already created for the ES and I am going to translate that to Dialogflow CX. Now, the PlanetsBot was actually the first chatbot that I explained in my step by step Dialogflow course, which is actually like my recommendation for people who are just getting started with Dialogflow ES, okay, and though this was the first bot I created in ES, I do not recommend people use this as the first thing they learn in the CX course because I think that it is more important that you understand how state machines work and that is why I created a different course or a different, you can say a mini course is like the beginners tutorial for Dialogflow CX and after you understood the pages and how to transition between pages, then you are actually ready to build the PlanetsBot in CX.
[01:05]
What the PlanetsBot does as an overview is that there are going to be, we have the nine planets, of course, it is now disputed whether Pluto is a planet or not for the purpose of this course, let us just assume it is and then I have three attributes of these planets. The first one is radius, the second one is volume and the third one is mass, that is weight of the planet, okay, and the bot is supposed to answer these questions.
If you ask it for the volume of Mars, for example, it should be able to provide an answer and if you ask for its, okay, notice that as soon as the user asks what is the volume of Mars and it gives some answer, it, when the user changes the attribute from volume to color, it has to be able to respond to that. What has happened here is that it is able to infer that the user is asking about the planet Mars, okay, and the other thing I just realized is that the three attributes that I have declared, I think they are volume, color and mass, I think it is probably not radius.
[02:18] Either way, that is, it is not a big thing, that is just a minor detail, you can choose whatever attribute you want, it is just for the sake of explaining the course. But the point here is that we want to sort of mimic or, you know, imitate this behavior in the, in using Dialogflow CX, that is the goal here.
Lesson: Entity Definitions
[00:00] As you learn about the how to build the PlanetsBot in Dialogflow CX, you are going to learn two core concepts in CX. The first one is going to be entitiesBoth Dialogflow ES and Dialogflow CX support entities and th... More and you will actually notice that entities in CX are not that different from entities in ES, but the way you invoke the entities is actually quite different. In other words, if you look at entities and parameters as a single concept, which is I do, I actually think of them as similar things.
The first part of this building the PlanetsBot is going to explain how entities slash parameters work and the second part of the PlanetsBot is going to explain how webhooksYou can use webhooks to add custom business logic in both Di... More can be used to actually get the values for the specific things the user is asking for.
[01:03] For the first part, let’s just take a look at entities. You go to this manage tab here and go to entity types. Let’s create a new entity and I’m going to call it planet and I’m not going to have any of these checkboxes actually check because the first one is talking about entities without synonyms. In this case, we do have synonyms, so I’m not going to use it. They are not regular expression entities.
I don’t want it to automatically add entities and there is no fuzzy matching. I’m just going to straight away fill out all the values. There’s Mercury, Venus and for now, I’m just going to have these three planets. I’ll fill out the rest later. For now, this is just for the sake of explanation. Let’s go ahead and save it and I’m also going to create another entity and this one is going to be called attribute.
[02:02] In the same way, these things don’t need to be checked for this one and the attributes I’m going to have are going to be color for sure. I know that that’s definitely being used in the previous bot and I’m going to use volume and then I’ll finally use, let’s go for mass, that is the weight and let’s add weight as a synonym here.
Okay, so these are the two entities I’m going to use for this bot, planet and attribute. Now, if we go to the intentsBoth Dialogflow ES and Dialogflow CX have the concept of int... More and actually let’s go directly to the build. What I’ll add here is a new transition route and I’m going to add a new intent. This one we’ll just call get planet attribute.
[03:01] This is where the user is asking for an attribute of a planet. Let’s say the user says, what is the color of earth? The first thing you notice is that it’s not actually doing the annotation. I think that this is just like for it, it’s taking a while for the training and all that to kick in. But if I were to just highlight this and I can scroll down and I can find the attribute there.
Okay. And in the same way, I’ll highlight earth and I’ll find the planet there. I will save it. And once this has been saved. You’re going to have, like I said, there are going to be two parts to this building the planet bot. The first part is going to talk about only parameters and we are not going to be actually using webhooks. And it is good to remember, if you went through the step-by-step Dialogflow course, you know that if you want to actually do something which involves this kind of logic.
[04:06] In this case, the logic is you’re going to do a database lookup. You do need to use a webhook in that case. In CX, there has been a few updates which could make it possible to implement somewhat minimal logic inside the console itself without calling the webhook. And we will take a look at that later in the course. But for the most part, the same concepts still hold in that if you want to do any kind of business logic, you just start with the assumption that you will need to create a webhook.
And in some cases, you might be able to use just the built-in conditions and parameters and all that to avoid it. Although, like I said, just start with the assumption that you do need a webhook. And maybe later on, if you find that you don’t have to call a webhook, you can maybe do it in another way. That’s different.
[05:01] But again, let’s not get too worried about that. At the moment, all I’m trying to say is that because we haven’t yet created a webhook, the agent is not going to give an answer. Instead, it is going to echo the user, echo back to the user what it understood. Now, that’s important because even to just echo back to the user what the agent understood, it should have actually extracted the value of the entity.
Right. So the way you do that with something called intent parameters is you can say simply you asked for, you put the dollar sign intent dot params dot, and then you provide the name of the entity. OK, so here it will be you ask for dollar intent dot params dot attribute of dollar intent dot params dot planet. So what you’re really saying here is if the user were to say, OK, what is the here, what is the color of earth?
[06:05] It’s going to extract the value. It’s actually going to extract the value of color and it’s going to extract it because we actually told it that the attribute has to be extracted. Right. That is, think of it like this. When it outputs this information, it’s actually outputting the value. OK, so in this case, it’s actually going to say if you were to ask what is the color of earth?
It’s supposed to say you asked for color of earth. OK, so let’s save it and let’s test it. What is the color of earth? And you can see that just as we expected it is. Let’s make it a little better. So you can see that it’s asking you, it’s responding. You asked for color of earth. Now, if you were to say instead, what is the volume of Mercury?
[07:08] OK, you can see that it comes back with you asked for volume of Mercury. So till now, it’s working exactly like we expected. It is just echoing the information back to the user based on what specific entity they are looking for. And in the next lesson, we’ll look at how to improve what we already have.
Lesson: Original vs Resolved value
[00:00] In this video, what I am going to do is explain the concept of resolved and original values for the intent parameters which were extracted by Dialogflow CX. Now, I also went ahead and added all the planet values for all the nine values, you can see that over here. And what I am going to do now is when the user asks for get planet attribute, I am going to actually go into this response and in the parentheses, okay, I am going to have, so what I will do is I will have dot, dot, so here if you look at it, it says intent dot parameterBoth ES and CX support the concept of parameters. If entitie... More, params dot parameter id dot original.
So the original is what the user typed, resolved is the, the key of that, of that entity and I think it will become clearer once I type it out. Here what we will have is you ask for intent dot parameters dot original, then inside the parentheses, I will put resolved of intent dot params dot planet, okay, so what I will do here is the same way I just put this intent dot params dot resolved here with the planet as the entity and here I will put dot original, okay, and let us go ahead and save that.
[01:40]
Now, if I were to test it, you can say what is the color of earth and notice that it says you ask for color and in parentheses, it puts color of earth and you might be wondering then like, okay, so what is the big thing, nothing has changed, but what has changed is if you say what is the weight of, let us say I put Mercury, notice that now it is changed from you ask for weight and then in parentheses, it changes it to mass because the original thing that the user typed was the word weight, but the resolved value was the value mass, right.
[02:25] And in the same way, I made it uppercase of Mercury and the resolved value is lowercase Mercury.
Now, you know, what you use as the resolved value, whether it should be uppercase or lowercase is really up to you, I just prefer the lowercase convention and the important thing is to be consistent. If you choose lowercase, make everything lowercase, if you choose uppercase, make everything uppercase as much as possible because that will prevent some confusion down the line when you are doing string comparisons and all that, if you just use a single convention.
[03:00] But the point, the overall point of course is that by looking at the resolved and the original, you should be able to find out what the user typed versus what the value key is supposed to be. Now, in case you are not very clear about what I said, I just want to go over to the entity types and go into attribute and when I say key, I mean this, whatever is coming, appearing in this column, okay, because that is sort of the key for this value which you have declared and notice that the key is mass when the value is weight.
That is why when we had this test, okay, I think it goes away, but that is why when we did the testing of that agent, when the user says what is the weight of Mercury, it is able to find the original value as weight, but also the resolved value as being the mass of the planet. Now, why do we need the key?
[04:01] I think that that should be fairly straightforward. You could refer to the same value of the entity in many different ways, including for example, abbreviations and all that and having a key like this will help you normalize the value for whatever the user is interested in. Even if the user were to say weight, you can normalize it and understand that what they really want is mass, okay.
Lesson: Intent vs Session parameters
[00:00] So one of the big changes that is introduced in Dialogflow CX is this concept of intent, form and session parameters. Now the reason that this is such a big concept is because this actually the way that Dialogflow CX has introduced these parameters, it solves a lot of problems which were there, which were present in Dialogflow ES and notably the big problem that they solved is the fact that carrying over users input, whatever information that they had provided through the whole conversation was actually very tricky.
You could either use a long contextContexts are used in Dialogflow ES to manage the state of th... More lifespan and have that cause a lot of unexpected behavior because there were too many candidate intents, that was one way of handling it or you could create like slot filling and of course slot filling is as I mentioned before is very bad because it just causes a lot of, it’s just very hard if it gets into a loop.
[01:05] Slot filling is a great concept but a very bad feature. But the thing is with CX what Dialogflow has done is that they have created these three types of parameters which are essentially meant to handle these different scenarios as well as provide you with multiple ways to get, not just get in user information but also carry them over through the whole conversation.
So I just want to point that out because what we are going to do in the next set of videos is we are going to look at the difference between the intent parameters and session parameters for the PlanetsBot. We will not need to use form parameters because the form parameters is what replaces slot filling and we will take a look at form parameters with an entirely different type of bot and it will be its own self-contained bot, we will take a look at that later in the course.
[02:03] But for now for the next set of videos I am just going to go over intent and session parameters.
Lesson: Session Parameters Intro
[00:00] So when the agent responds with this response, you ask for intent.params.attribute.original of you know blah blah and all that, what is happening is that for that particular intent, you know you start over here and you end over this, this is the whole intent. There are some parameters which are being set and those are, you can reference them or invoke them using this $intent.params. you know name of entity, right.
But what you really want to do is you want to be able to carry this information over through your entire flow. Okay, and what the session parameters allows you to do is precisely that, that is as soon as some information of an entity is set when you’re running the bot during the runtime, it will copy that value into the corresponding session parameter.
[01:00] And you don’t even have to explicitly declare something as a session parameter, just by having an intent parameter collect a value, you automatically create the session parameter. So what I mean by that is, let’s say that the, we invoke the session parameter here, session.params.attribute of $session.params.planet is, and of course I’m not going to fill out this information because actually getting this information here would require a webhook call and we don’t, we haven’t yet built it.
So what this does is when this intent fires, and when the intent parameter called attribute is set as at runtime, it automatically creates a session parameter of the same name, and also copies the value over into that session parameter.
[02:03] So to test it, let’s go and try this. What is the mass, let me try this, what is the weight of, let’s say Neptune. Okay, let me say, let’s say we do that. And you can see that the first response is you ask for, you know, the one we already had, but notice that there’s a second response coming back from the agent. And that actually says the mass of Neptune is blah, blah, blah.
And notice that it has changed that from weight to mass, which means that the session parameter always uses the resolved value and not the original value. And you can see the same thing with Neptune also. And the other thing that you have to see is that it’s now, if you see the parameters over here, you can see that it’s not, I don’t think it’s actually explicitly noting that this is an intent versus a session parameter. But I think that what you see in the box is all of these are session parameters.
[03:03] That’s what I understand. So you now have a session parameter, which is called attribute, has a value of mass. And then you have another session parameter called planet, which has a value of Neptune. Okay, so why does this matter? I’ll explain what happens next. Okay, so remember that in the explanation of what the bot is supposed to do, I mentioned that as soon as the user over here, when the user asks for something and the bot comes back with an answer, they can change the attribute without mentioning the name of the planet.
And the bot is able to infer what planet the user is actually talking about. So we will do the same thing. And we will be able to get that information. And for that, we will be using session parameters. And I’ll explain how to do that in the next video.
Lesson: Using session params
[00:00] So now let’s take a look at how to use session parameters and do something a little more useful. So in the get planet attribute, what I’m going to do is I’m going to create a new page whenever this fires. And I’m just going to call it follow up, because this page should be able to serve all the follow up questions coming from the user. So now I’m going to go into the follow up page. And here, let’s add another intent. This is a new intent.
And this one will be changes the attribute that is the we know what planet it is, but the user is changing the attribute. So we can say, what is its color. So remember that here it’s now you can also see that it’s doing annotation automatically. Remember that here we know the planet name, and we are changing the attribute. And even though I type the word color here, they could have as well changed that to something else, because you know, that’s how Dialogflow works, right?
[01:06] It’s going to extract this attribute value. And what I was going to say is that the word it’s over here, it actually represents the planet name, right? That is, when the user types, what is the let’s say, the weight of Neptune, as we did before, that means they’re talking about Neptune, and the page has transitioned to this follow up page.
And then if they were to say, what is its color, we know, of course, that the word it’s is referring to Neptune, right? Now, let’s first save this, right? Now, if you came here, you know that we have, okay, so I’m going to do a few things here where I intentionally make some mistakes, so that it’s much clearer what is actually going on. What I’ll do is I’ll create three different responses, okay?
[02:00] So the first thing I’ll say is, the attribute is, and then I’m going to say dollar intent dot params dot attribute dot, let’s go for result, okay, just to keep it consistent and make it simpler. And then I’ll have a second response, which says the planet is dollar intent dot params dot planet dot result. Okay? Then I’ll also have a third response, which will be, you asked for the, but this time, I’m going to use session parameters, okay?
Session dot params dot attribute of dollar session dot params dot planet. It’s just going to echo everything to the user, back to the user, but we are going to use the various intent and session parameters just to see what actually happens.
[03:06] Okay? So let’s go to the test agent now and do the same thing. What is the weight of Neptune? Okay, I’ll just change this to Saturn. Of course, the first time it will be fine, right? Because it’s going to say, ask for the weight of Saturn, the mass of Saturn is blah, blah, right? And this is all good. You can also see that it has gone to the follow up page.
But now if I say, and what is its color, okay, notice what happens. So the first thing is the attribute is color is the correct information because it is an intent parameter and we are still in the scopeDialogflow CX scope helps you decide what set of actions CX ... More for that particular intent. That is the intent is called changes attribute and the user actually typed in an attribute in that intent. That’s why it fired. So there is a value for intent dot params dot attribute dot result.
[04:04] But notice that it just says the planet is, and it’s not able to actually get any information for this because at this point in the conversation, this intent, because it never collected a parameter called planet, it’s not going to actually have any value. So it’s just going to, you know, it just thinks it’s some text. It’s not, it’s not able to dialogue for CX, it’s not able to identify this as an actual parameter. Okay. But finally, the last step, you see, we said you asked for the attribute of planet and now it’s able to, it’s able to not only get color as the attribute, it’s also able to get Saturn as the planet.
And these two happen to be the last values which were set for this particular parameter. Right. Now that, that is very important. And the reason that’s very important is because if I were to just change it again, what is it’s, what is the other thing, mass, there’s color, and then I think there’s volume, right?
[05:08] So you say, what is its volume? You can see that the attribute is volume is correct. The planet is unknown, but it’s now changed to, you asked for the volume of Saturn. So Saturn is still there. It’s still the same session parameter and we have not overwritten that value in any way. But you can see that because we have now asked for the volume, it has overwritten the value of attribute with, yeah, it is overwritten volume as the value for the attribute you are looking for.
Okay. So I think that that should make it very clear what session parameters are doing here. And I’m going to, in the next lesson, I’m going to add another intent where the user is able to change the planet. But of course, by now you understand that the behavior is going to be pretty much exactly the same and there’s not much to explain in the next lesson. So I’ll just go through that a little more quickly.
Lesson: Keeping track of session parameters
[00:00] So now I am in the follow up page and I have added one more intent called changes planet and if you were to go into the intent, you can see that this is just changing the planet name, it is asking what about the different planet name okay and save that and notice that I have three responses in the fulfillment, it is trying to echo the attribute, the planet and the plan attribute of planet based on the session parameter. Now if you were to go and test the agent, let us start with what is the mass of Venus, so you can see that it comes back with you ask for and mass of Venus is and it is transitioned over to the follow up page.
Now if I were to say what about let us say Jupiter, so notice that it could not resolve the attribute is you know $intent.params.attribute.resolve because in this particular intent, they use it is not talking about any planet but it was able to resolve that the planet is Jupiter and it was also able to collect that value from the session parameter where it is going to be able to say you ask for the mass of Jupiter.
[01:15] Now because of the way session parameters work and it holds the last updated value for that particular parameter or entity whichever way you want to look at it, you can just keep continuously doing the same thing. So for example and what is its volume, so here we know that it is the last planet we have is Jupiter, so we are asking for the volume of Jupiter, you can see that it says you ask for the volume of Jupiter and what about earth, so now it should come back with volume of earth, see that it says you ask for the volume of earth, what is its mass.
[02:02]
So this time we are talking about the mass of earth, so you can see that it says you ask for the mass of earth and notice that the other the intent parameters are not always getting captured properly which is again what we expect because it was because a specific intent fired and that intent never collected the particular parameter for the intent, the session parameter is still being maintained just as you might expect. So with this what we have effectively done is once the user provides any information during your conversation flow, you can see that a copy of that entity’s value that parameter is going to be stored in the session and as long as that is the case, you can always reference that session parameter by name and get its value by using the syntax $session.params. whatever the name of the entity would be, you can just keep using that and you can just keep moving forward in the conversation without losing the information the user provided.
[03:12]
Now I will contrast this with what happened in Dialogflow ES where you actually the same thing usually was done using the context and the value could only be invoked as long as the context lifespan was active.
Now what this led people to do is they set the context lifespan to be very high so that they could keep referencing back the information they collected from the user in the previous step. Unfortunately, a high context lifespan can interfere with your agent’s behavior in completely other ways because it will create this scenario where if you are familiar with candidate intents in ES and I really hope you are because it’s sort of like a foundational concept if you want to build complex conversations in ES.
[04:03] So if you understand what candidate intents were, you know that a long, a large context lifespan corresponded to a lot of candidate intents which was bad because it was quite possible to confuse and throw off Dialogflow ES and once the conversation went off track it was even worse because now you actually had to create new intents to deal with the conversation going off track and it just led to a lot of the fine-grained management of intents and Dialogflow ES usually required a lot of intents and it was just not a simple process.
CX actually simplifies it a lot and that is why in fact that’s the big reason why CX has been created and you can see over here when we just moved from asking from one particular attribute to the changing the planet, changing the attribute, we just kept changing it back and forth and it was still able to do the, it was still able to provide the correct answer right.
[05:11] At least we don’t, we didn’t fetch the value but we still know that Dialogflow understood what was being asked because they have designed Dialogflow CX in such a way that it’s much much easier to use the session parameters and keep track of all the values that the user has input till now in the conversation.
Note: This is my old website and is in maintenance mode. I am publishing new articles only on my new website.
If you are not sure where to start on my new website, I recommend the following article:
Is Dialogflow still relevant in the era of Large Language Models?