Create a ChatGPT Bot with State using Bot Framework Composer & OpenAI Chat Completion API | Tutorial

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
let's call our chat GPT API and see how we can utilize that so we'll be calling it from The Postman so just explore how is the format like what all is in what all this parameters we need to pass along in order to create a conversation so let's copy this guy thank you so content type application slash Json authorization Bearer your API key let's initialize a variable so that we don't have to hard code in this environment open AI let's create a new environment and initialize with a value and let's save this one and when we are here in this collection we have to set our environment okay and now let's set up the body parameters that should be Json and the Border name is GPT 3.5 Turbo road user hello let's send this out and see what's response okay so chat completion from token 9 completion token total tokens 20 so assistant says hello how can I help you today how can I assist you today finish reason stop okay let's see how we can continue the conversation up to four sequence where API will start generating further two things okay given a prompt wait a second we only have this much documentation foreign [Music] tasks without any conversation okay so messages is an array and yes it is an array so let's format this thing and this time we are going to give the response that was generated by chat GPT so this time the role will be stent and content is so basically we can copy these two properties from here directly all right so now let's see what's the response okay we have to ask as a user as well and we haven't asked anything so here you see the just replies with something and we have to ask the user question let's create a conversation this time what is my name and it will tell that I do not know and just like similar to this format answer it okay because still it that the charge repeater doesn't know my name so here you see I'm sorry but as an air language model I don't have access to that information can you please tell me your name so in that case let's copy this one and let's ask another question and this time I'll say my name is ready and let's see the response hi JD how can I help you today and now what I am going to say let me first copy this response and let me ask another question and this time I'll ask what is my name so we are just checking how it remembers the state and here it says your name is JD so that's pretty cool so this is how we can utilize the chart GPT API in order to build a conversation by passing the user role and along with the assistant so basically if you want to remember any state just copy the response and put back to the input Json so why don't we build it with composer this we haven't tried yet right so let's try to build with the composer that so that we explore a new thing so it would be quicker to build as well so let's start with the empty part search GPT import this is so the first thing that I'm going to do is how to configure this configuration for the bot framework composer so we come to this page configure advanced settings and that will open up all the settings and here we are going to give for our bot basically open AI configuration so one settings that we are going to give is API key and some value and another configuration is endpoint so let's copy that from our Postman so that is my end point and this should be my API key all right so bought replies with some welcome message welcome to chat GPT bot I can remember what you said if you want to start fresh just reply with new okay so this is the command that we'll be taking from the user if user said new then we don't have to store any more conversation so we'll be starting with a new conversation all right so that is our welcome message so let's start the button see the welcome message and there it is right so we'll be implementing everything in the unknown unknown intent so a known intent is triggered when none of the intents are satisfied so this gradient intent will only be triggered once when the user joins the conversation and the activity name is conversation update activity so in this case when both parties join the conversation who are the two parties one is the bot and another one is a user so this intent will only be triggered then and this one if none of the intents get satisfied if I have like multiple intents for example let's say I have something called as agent intent and here's some utterances utterances are when how do you want this intent to get triggered so if user input says like agent health escalate talk to human so if user type something similar to this input then this particular intent will get triggered so if we have multiple intents like this then they get triggered based on the user input if none of them satisfied it always come to this unknown intent right and here we are going to start with first we have to check what's the user input to do that first we have to set a property in this case uh user okay we don't have to set because it will always be there in turn dot activity dot text so we don't have to set that up but we do have to set our input now first we have to check if if whatever user has said is new that means turn dot activity dot text so whatever user has said will always come in this property so we are going to check if that equals name okay in that case we have to reset the state so initializing our state here so there are various scope for the state so if we are using user means that is valid for the lifetime of the user conversation if you are using dialog then that will be valid only for a single dialog if you are using turn that means that will be valid only for single activity term that means if we are on this first step and you reply back to the user and then we come to the Second Step then that means that's another turn so here we just start with user Dot data let's initialize with that and for now let's say okay let's create an object we can create an object so let's copy our object from here so initially we started with this one messages so let's copy this object let's see how we are going to append everything still I haven't figured that out but let's see uh no that's an array let's create an array that will be better so user content hello so that's our first state that we want to create and this should not come here this should be part of here and initially it should always come up here Okay so so how do we check the initial state or if it is new or let's say something here how do we apply our condition or let's see what we have a composite documentation yeah just one adaptive Expressions yeah here it is so we want to track with logical expressions or this one so let's try that or [Music] um dot activity I'm not download activity or this let's user Dot is so this property will be specifying in our creating dialog so that tells us that user is new so here we are going to set this property and that will be true and once user is new then we can reset this property so basically here false so this is only one time use when user is new then after that it will always be false um so that statement wait a second this should be and condition or not and or dissatisfied or dissatisfies it should be our condition okay so now let's call our API first so there's an inbuilt functionality to call HTTP request and here we have to pass our endpoint so since we have already mentioned that endpoint in settings we can retrieve it like this settings Dot open AI dot endpoint so this is how we can retrieve that and that's an expression not sure if this that should be an expression but we can always check okay and here we have to create an object so the first property that we want to send is this model and the second property that we want to send is messages and that's an array and we already have defined the messages so we can just write an expression oh you missed where it is coming every time we cannot expand this as well messages and we want to retrieve it from our user.data because that's an array right and that is all we have to pass and along with that we have to pass as headers and headers is settings Dot open AI Dot and what is the property name so end point so API key that's our property name filters settings Dot open AI Dot API key that's authorization okay and whatever the result will be there it will be stored in this turn dot result and this one we have to set application slash Json it's not required but still we have to set and response type it will be a Json right and it is Bearer and the token so the error we have to write it here space and whatever the value all right so we are good to go let's delete this and send a response in the chat to send a response we just have to use the sender response and so the output will be present in turn dot results so let's retrieve our output and turn dot result and let's check the response so it is choices um okay turn dot results dot body I think should be body dot content let's see okay let's try that maybe content body dot content I think body should not let's start with body first body dot content dot choices and it is a array so first element Dot message and inside message content all right let's see and also let me send just turned out result as well just to see if that is coming don't Wizards just to verify if whatever we have written on the next send a response is correct okay so what's the error here enter GPT Trigger action center URL must be an expression okay so I think here equal to is not required yeah so let's restart the bot and see the response restart the conversation and let's say hi so initially it will be high only so we have to dynamically set this content so basically this value so it should be done dot activity dot text so whatever user has replied we have to set it in the content so let's restart hi okay it's bad request and what's there message role user content hi is not of type okay messages type why it is not of type array we have initialized our array right user.data and that seems to be an array okay let's print that value up here user dot data let's try something chatbot by passing the content directly as a Json like hardcore this for now so messages okay okay it should not be surrounded by it should be surrounded let's see if we can just enter I think we cannot do that you have to add double quote okay let's surround that with let's add it manually for now okay we are getting some response and see we do not get our final response so we are we have missed that so the output will come here this is the whole turned out results and the output will be part of where is that content so there is no body parameter here so there's no body property in between it just turned result content choices then we can get rid of this part we can get rid of this one and yeah that is all so let's just uh here we get some response let's see how the code comes in picture so just we haven't right right so let's try it in Postman first and instead of what is my name to get today's date in C sharp just to check how the code will be returned so it is content to get a day you can use date time dot property and this is how the examples snippet so it is three that back inverted single code and let's see how it will behave it there so the formatting is same it's just that they have added this markdown and HTML syntax so let's ask here in this how to get today's date I doubt it will format because it's simply a web chat window ah it really gives a nice nice formatting that's nice so it gives us a code piece as well let's let's have something little bigger code recursive example in C sharp okay perfect perfect it really gives a nice formatting of the code enter a number can I expand my chat window I doubt okay that's pretty cool but yeah we have to fix this piece in order to remember the state so let's try to do that and user.data [Music] um sir Dot data and that's an array is the very two Json object okay this is what we need so equals to conversation update don't use the spring interpolation but use an expression okay okay let's do that equals to uh user dot input data hope this works okay perfect perfect that worked perfectly fine and now we can see to merge them for the next situation so uh we need to set this property let's quickly do that basically append array so let's see how we can append append array report we have to find something from here collection first join returns a string that has all the items from an area and has each character and let's see if we can append that property returns a collection that has all the items from a specified collection I think this should work Union and that is what we needed okay let's try that Union and the array collection and the recollection let's try this one so we need to set our user dot input data and let's write the expression search for Union so the first array is my user.input data and my second array will be the output from this response so we got to create that one temporary property so we need to name this as turn Dot output data and this has to be an array so value must be in expression syntax error um is it throwing it here though I mean okay the error is for the other one not that so here this should be done Dot output data and let's check over here how the response will be so we need this particular object inside an array right so that is message so let's set array object equals to return dot results dot content dot choices the first element Dot message right and here we go to combine them so done Dot output data all right and let's see and in this case when it will come next time this guy will get executed so we need to put this one over here and this should not be this way the same expression that we have written should come over there paste so let's see the expression and collection so let's create a temporary variable that means done Dot new input all right in this case it will be like this comma turn dot new input right and we need to set if user has said new then we got to clear that property I think it will automatically get create overwritten by this value yes all right so let's get rid of this one and restart the bot let's see how it behaves okay finger crossed hi hello how can I access to what is my name okay value cannot be nerved perfect so I asked what is my name and it said well you cannot be null and for what it has said and value cannot be null is it user dot is name no I don't think so and user.input data let's put a strike trace for each and every property that will help us and that's gonna help us in debugging so log to console so start unknown at the start of the unknown we got a print user Dot is new this is the property that we have on our friend start new user dot his name and what is its value true okay let's copy this because we are going to reuse that and let's print it here as um stack Trace I think here we already come to know that that's the value here we need to check message the text should come here label is for structures used to identify it in a list of Trace activities okay let's get rid of this start or not and what's the value this one so here false and here we are going to check that property along with along with we are going to check for user dot input data and user dot input data okay and let's put one more here and this time turn Dot output data term dot or put data turn output send stat restroom send structures true and here also we need to say the reason being why I am not copy pasting because when I copy paste all values change basically all the sync this synchronization between this lock to console I guess there's some work so here I'm going to pop it from here user.input data and after response true so let's now debug where what is happening test in web chat restart the conversation let's say hi to the bot and here you see structures start unknown at the start of that user is new is true that's correct and hello how can I assist you today so turn output it is an array role assistant content hello how can I help you today okay that's fine next is after response and it should append that role user content comma rule assistant content so that is working pretty good next I am going to say what is my name and let's see so start unknown it is false when we come to false his name is false and user input hi assistant hello how can I assist you today that's correct and there's an issue over here ah there is no output data see here this we have missed because output data was part of this property not the other one so user.input data in turn dot new input so let's see how it behaves now hoping this should work now hi what is my name I'm going to use the same scenario that we have done in the postman I applied I apologize but as an AI language model I don't have access to personal information okay and okay my name is JD nice to meet you JD how can I assist you today to this is a good partner what is my name your name is Siri that's it's a pretty cool amazing thing now it remembers the state okay now we have to check one more scenario where I just type new then it should restart the conversation right and before restarting and this should not if it is new then okay if it is new we have to put one more condition uh is that my condition Oh wrong uh create a condition so we have to verify if turn dot activity dot text is equals to equals to name in this case what I have to do I have to reset that property and send just hi to the bot all right so set property in this case we are going to set our turn dot activity dot text and we're going to set with I let's use an expression hi okay so we are setting with this property so that when it comes there it knows what to do let's restart the bot okay so let's start with hi how can I assist you today is let's start with that remembering the state what is my name let me drop the next message my name is JD what is my name your name is Eddie okay this time I'll just say new hello how can I assist you today and let me ask again what is my name I'm sorry then here you see it forgets your name and it restarts a conversation that's pretty cool and amazing and now we have completely built the chat GPT the latest API with remembering the state that's amazing all right so how do we Implement in let's say Microsoft teams or web chat that we can do it in our one of other sessions but yeah this is what we wanted to implement and for a quick testing let's not do that let's not go to that route okay thanks everyone thanks for your time
Info
Channel: Dewiride Technologies
Views: 2,912
Rating: undefined out of 5
Keywords: chatgpt, bot framework composer, openai chat completion api, ai chatbot, chatbot tutorial, beginner tutorial, stateful bot, conversation bot, chatbot development, chatbot programming, natural language processing, nlp, microsoft bot framework, openai, bot development, chatbot, bot, open ai, gpt3, ai, ai bots, chatgpt tutorial, azure bot service, bot composer, chatgpt api, chat, chat completion api, api, build chatbot, create chatbot, create ai bots
Id: xqPrBPryPZw
Channel Id: undefined
Length: 50min 31sec (3031 seconds)
Published: Fri Mar 10 2023
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.