Practice Exams:

AI-102 Microsoft Azure AI – Create a bot by using the Bot Framework SDK part 1

  1. Overview of the Bot Framework SDK

So in this section of the course, we’re talking about the Bot Framework SDK and we’re going to talk about Conversation Flow as part of that. Now, in the last section we talked about this Q and A maker. And as you saw, it’s basically a knowledge base, right? The Q and A maker actually extracts the information from your website website, from a PDF, from an Excel spreadsheet or another source, builds that database up, people can interface with it using a natural human language and it basically is a no code way of creating a bot. And that is good for a lot of purposes.

You can put this on your website where you’d have your FAQ section or the original Contact US method before they get put into an email queue or a phone queue. But the bot framework is obviously a more advanced form of building a bot. Now, these bots can then actually perform actions. So instead of just basically answering simplistic questions, you can build a bot that actually has some logic and will actually record those actions and perform tasks such as orders. So you could create a pizza ordering bot and what you’ll end up doing, and this is where we come into Conversation Flow, is we’re going to be taking the person you’re talking to through a prearranged number of steps.

So maybe there are, I’m going to say, seven steps to ordering a pizza. And so you’ve pre programmed your pizza order bot to have those seven steps. And we’re going to see some sample code in a second here of programming this. It’s called a waterfall method and you’re basically taking people through seven consecutive steps and they basically have to answer those questions and they’ll get to a result similar to when you’re creating a website. You are basically allowing people to click on this link that clicks on that link, that clicks on that link to get them to a particular destination. So that’s the bot framework.

Now the bot framework we saw with the Q and A maker, it can interact with certain channels. Obviously with the access to code, you can have basically any channel that you can program into. You’re going to have different types of bots that can have free form text or clicking on buttons, something pre programmed, something that can be interrupted, et cetera. And again, it basically integrates all of your net framework, your other cognitive services. You can have speech and image and text analysis and sentiment. You can obviously do quite complicated bots using the Brought Framework SDK.

Now, for some of the videos in this section, we’re going to be relying on the Microsoft repository for bot builder samples. I’ll put a link to this in this video, but we can see in this repository, we’re going to get a number of samples. There are you can see it’s numbered up to 45 and it does cover multiple languages net, JavaScript, Python and Java. And we’re going to be able to see some fairly simplistic bots all the way to more advanced bots. And so as we talk about certain types of styles of creating, like we said just a second ago, waterfalls, et cetera, we’re going to look at some demos based out of this repository. So go to that link, attach to this video, and we’ll continue on in the rest of this section.

  1. Our first Framework Bot – EchoBot

So we’re going to begin with the beginning, which is that we’re going to look at this Echobot. And so we can click right off the Bot Builder samples repository. Read me here, I can click right on python. It will take me into the python tree. If you want to look at another language, you can. And this repository is broken up into the bot, the deployments and then the app. Let’s look at the bot first. The bot is very simple. If we look at Echobot dot Py, you can see that it’s not even 20 lines of code, including a couple comments. It uses this module called Bot Builder Core and Bot Builder Schema to import all of the modules that it needs. And it’s called Echobot. And there’s two basically methods.

One is when a member is added to the bot, it just says hello, it’s going to send that back. And on the other is whenever it receives a message on message activity, it’s going to send a message that says Echo and then it just sends back whatever it receives. So it’s a pure Echo bot. We’ll see that in a second. If we go back up, we can go into the deployment. Now in this video, we’re going to test this bot entirely on our local machine. And you don’t have to deploy it into Azure just to test it, but as soon as you want to use the Azure servers to do the logic, whether that’s a development environment or staging environment production, then you’re going to need to deploy this.

And so we’ll see the resources required to run this on Azure servers and we can see that in this particular repository, you got the choice of creating a new resource group or deploying this into an existing resource group. If we look at the template here, the Arm template, we can see that there are effectively four resources. This Microsoft web server of forms is the App Service plan. And as you can see, it’s creating a Linux app service plan. The Microsoft websites is the web app. So it deploys a web app into the app service plan. Scrolling down, it’s got the configuration. So the web app is going to need some configuration, including the default documents, et cetera.

And at the bottom, the last and fourth resource is the bot service. So that is we created a bot service going back here for our Q and E bot. We’re going to end up creating a new bot service for our Echo bot. In this particular case, so far, hopefully that’s pretty straightforward. The last element, of course, is the app itself, which we’re going to call from a command line. We can see that it does call the Bot Builder Core to get the bot framework SDK. It’s going to instantiate let’s go down here, the Echobot. So the Echobot is what’s going to be running within Azure. And then it just has this way of handling incoming requests, any kind of messages that are coming from it, make sure it’s all authenticated and it’s able to basically send and receive messages.

So whatever the user posts as a message, it’s going to send to the bot and then leave. This is like a middleware. All right, so this is, like I said, one of the simplest bots you’re going to see. We can see there are instructions for deploying this within python. And so I’m going to use my local python here. Instead of using Pycharm, I’m going to switch to Anaconda. We’re going to clone the repository, we’re going to get into the Echobot folder, install some requirements. Now, the only requirement, there’s only one, and it is the bot builder integration Aio. So one single module needs to be installed using Pip. And then we’re going to run the bot. And then basically it’s going to create a local host URL here.

We can see the example of it and we’re going to need this software called Framework emulator that we can integrate with our web front end. All right, so let’s start by cloning the code. Now, I’m going to use Anaconda here to clone the code. Now this is going to get all the samples, not just the one that we’re looking for, so then we can navigate to that particular sample. All right. Now at this point, we’re going to want to activate the environment that we want to work in. In this case, I’m going to say conda create name bots enter. I can see Konda activate bots. All right, so we’ve got a separate environment for this thing here. Now we can install the requirements.

For this particular example, we’re going to need Pip, all right, pip’s installed. Now we can use Pip to install the bot framework, all right? So that should be everything installed in terms of the requirements. All right? So before we run it, we’re going to have to get this bot emulator, which is a desktop application, allows us to front end basically to talk to our web app, which is talking to our bot. So it’ll take us to this link and it says the latest release. If you scroll down, you can see different options, including Linux, Mac, Windows and even the source code if you just want to compile it yourself. I downloaded the Windows setup. Now I went ahead and installed it and started it up.

So now we can go back to start our application. So if we go back to, in my case, Anaconda, we can start app dot pi. And when we start it, we’re going to just see it’s going to have running on localhost port 3878-3978, we can go to the bot emulator, type Open bot and this 3978, we can paste again from the instructions, paste the URL into the Open bot dialog and just say Connect. And so now we have a chat opened with our bot running locally. Now, remember, the bot did say hello and welcome as a new person added to the chat. And so what we’re expecting is whatever message that we send is going to be echoed back to us.

So I can type Hi there and the echo comes back Hi there, and I can type hello and it comes back hello. So we can see that our bot, our echo bot in this case, is operating in less than 20 lines of code. Exactly what we expect now. We’re still a long way from having conversation flow and logic and asking questions and keeping session state, for instance. But you can sort of see the basics of coded bots that we can control. Definitely recommend that you download this sample repository, the bot running on your local machine. We don’t have to deploy it into Azure just yet. Get it running on your local, play with it and you can sort of see the basics of how bot framework, and in this case, the bot builder and bot emulator help us to develop our own bots.

  1. And our second Bot – WelcomeBot

So next up we can look at the concept of session state. So if we look at sample number three, welcome user going to here on Python under bot, we can look at the welcome user bot. Now, the welcome user bot imports a lot more from bot builder Core and bot builder Schema libraries, including something called user state. We also basically have created a data model which I’ll show you in a second. And this is the welcome user bot. Now we can see here that right away we are sort of relying on this thing called user state. And you’ll see in this bot that the bot itself knows the first time it sends you a message.

So if we scroll down a little bit, it’s going to say that you are seeing this message because this is your first message. So clearly we are understanding that there’s a welcome that happens and we’ll never say welcome again. So once they say welcome, once we set the variable in the user state to true and that is remembered for this user. So we can see that there’s some echoing. You said if they say hi, there’s a response, if they say intro, it’s a different response. There is this concept of cards where the user is going to have a pretty standard set of inputs, get an overview, ask a question, learn how to deploy.

Let’s go back up here. We can see the well, we’ll see in a second that they’ll be given those options that were basically dealing with cards. And so you can imagine maybe in a real world context, if you had a flow chart or whether you were using actual index cards, you can build out a flow chart. And so each card is its own self contained thing. It has a title, a text, what gets displayed and a link. In this particular case, action is open URL. So when the user chooses something, it opens a URL. So this is a specific type of card that you can build in the bot framework. And I mentioned there’s a data. So let’s go back up to welcome user.

There’s a data model for the user state. It’s extremely simple. The only thing we care about is whether we said hello to the user or not. Did. Welcome user. True or false? The default is false, right? All right, so we’ve already got this downloaded from our last video and so we should be able to let’s just check the requirements. Requirements exactly the same. So we should be able to just navigate to the correct directory and run this app instead of the other. And then the bot emulator will connect to the same URL with a new bot. So let’s do that. All right, so we can go into the three sample which is welcome User and we should be able to pythonapp Py and have it run that bot instead.

We can see the URL as as before, then we switch over to the bot framework, open the bot, we can reuse the URL, say Connect. And now we have got a series of messages from there this welcome bot. Hi there, user. You’re seeing this message. And if I say hi, you are seeing this message because this is your first message ever into this bot. And so now the user state, the welcome state has been sent to true. And so the next time we send that message, then it’s just echoing back what we said because the user state is true. Okay, we want to receive the intro. We see the intro.

Now we have this concept of cards. All right, so that’s the second concept in this particular code. And we have this intro card. And instead of having the user typing, you can see three buttons. And so if you want to say, get an overview. Now this action was Open URL, and it’s trying to over this URL for the bot service. The other Ask a Question takes you to stack overflow and deployment takes you to Microsoft Docs as well. So this is a card and these are actions that we’ve designated for this card. And in this particular case, they’re using the Open URL. But we can obviously spur other conversation and make a fairly complex set of conversations based on these button clicks.

  1. Using Bot Dialogs

So the next concept we have here is the concept of dialogues. And we’re going to show you what’s known as a waterfall dialogue, which is a dialog that follows a very specific script. So I’m going to go into the multi term prompts sample. Now, the bot here is actually very simplistic. We can see here that there is, well, it’s about 50 lines, but there’s not a lot of logic here, right? It’s maintaining state and it’s really just calling out to this dialog helper anytime it receives a message. So there’s an external helper that’s doing the messaging work. Okay, so there’s a set up here. And then there’s the work, which is only a couple of lines of code. So we’re going to get out of there.

There is a data model. So we are keeping this user state in this example as well. In this case, it is keeping track of the name, the preferred method of transport, the age, and a picture of the person that we’re talking to. So four pieces of data relating to the user that is having the conversation. All right, now let’s look at the dialogues. So this is the dialogue helper that he was calling out to. Now again, it’s importing the library bot builder dialogues. It has a number of modules called waterfall dialogue. And the real work here is you can see that there’s this waterfall dialogue. We’re using the add dialogue method to build this. And you can see there are seven steps.

There’s the transport step, confirming their name, asking their age, asking their picture, doing a final confirmation, and another summary. And then we can see basically these things are happening in a particular order. And each of these steps, like the transport step, is going to say, please enter your mode of transport. And it’s giving them three choices, please enter your name. And it’s asking for it’s a text prompt. So for text to come back, it is now going to confirm. It’s going to say thank you. Do you like to give your age? You can say yes or no. If you said yes, then please enter your age. And the value must be between zero and 150. And then we can see the other steps.

I’m not going to go through each of them.We’ll demo this in a second. Right, so you’ve got a basically a fairly lengthy dialogue helper that has the logic of this, including methods for each of these. All right, so this is pretty straightforward. Check the requirements. Now we have additional requirements besides the Aio Http module, we’ve also got dialogues and AI. So I’m going to have to run the Pip install before running the app. So let’s go into the fifth sample. And we’re going to Pip install from the requirements, which include a couple of more requirements. We can see them being installed here, so that installed.

And then we can just run the application using Python, which will create the bot as a web app then we can switch to the emulator. Since the URL remains the same, we can just reuse the same URL. And now we are chatting with our bot here so it doesn’t say anything to us to welcome us. We can just say hi. And now we’re entering into this dialog. So the first question is please enter your motor transport. And again you have three links that you can choose and it’s just going to type the choice. Please enter your name. My name is Scott. Would you like to give your age, yes or no? Sure. Please enter your age. I’m going to pretend that I am 18. I am not.

Please attach a profile picture or any type or type any message to skip. So I guess we could have an Http something or I can say any message so I didn’t attach any message if that’s okay. Sure. And it says My name is Scott, my age is 18. So it has this session state and as you can see it’s following this conversation in a pattern. So now we can start to expand our imagination into sort of a chatbot which again can follow a script and lead to an action. And now we’re getting a lot closer to what may be a call center would be able to do, which is just follow a particular pattern of asking a series of questions and then getting to the point where we’re able to call an external API to order the pizza if this is what we’re doing.