How To Create A Chat Bot Response System With AI in Python Tutorial (OpenAI API)

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
in today's video we're going to be creating a chat bot that uses artificial intelligence provided by the open AI API and it's going to look like this so if we run the program we're going to have a small prompt here that says you and here we can type in something such as how are you doing and if we tap on enter it's going to make an API call and it's going to give us a response what do you think about bananas and you can make these prompts as complicated as you want and the bot will always be able to understand and give you something back in context I actually said it should pretend to be a skater dude so at the end of each sentence it says yeah and it does scatter things and let's say what are you up to and just chilling and skating because that's the model we provided it as you can see maybe here you'll have a quick Glimpse you will pretend to be a skater dude that ends every response with yeah and then you give it an example of how it should react and it will take care of the rest in the prompt so I think it's a really cool project and we can get started immediately by creating a new empty python project in pycharm or you can use any code editor you want I will be using python 3.11 for this and it's going to look like this this is the intro screen to my new project we don't need any of this of course it's just an empty main.pi file for us to write our code the first thing we're going to do to get this out of the way is open up the terminal and we're going to type in PIP install open AI since they have their own package for handling all of this and then we can close that terminal and the very first thing we need to do is import open Ai and we need to provide it an API key so I'm going to have a secret file called hidden and to do this we will just go into our folder right click new file and we'll just call it hidden.txt now inside here you need to put your API key and we don't have an API key at the moment so what you need to do is go to openai.com create a profile so that you can actually use the AI and I'm going to leave a link in the description box Down Below on where you have to go because once you are in openai.com you need to click on your account and you need to click on view API keys and here you can create a new secret key but once again I'm going to leave this in the description box down below so you can just tap on the link and you'll end up here if you have an account otherwise you do need to sign up and generate your new secret key now of course you can't use my key it's hidden this is not the full key and will not work if you copy it so you're going to have to generate your own to actually be able to use it so right now what I did is replace that text in the hidden.txt file with my API key of course you cannot see that but I just did that off camera so we can continue with the project now that we have our API key so we need to provide open AI with our API key and to do that we're going to type in with open hidden.txt as file and we're going to type in open AI dot API key and that's going to equal the file dot read so this will read the one line that we have in hidden.txt and use that as the API key for openai DOT API key now as a security practice it's not recommended that you store your API keys in a format such as a text file but for the purpose of this video we're going to keep it like that because it's nice and simple and if this is your own project I mean you can easily just skip all of this and say open AI dot API key and just paste it in directly here you don't have to create that file it was actually just mostly for my video so I wouldn't give it away but next we can create our first function which gets an API response so he will type in Def get API response and it's going to take a prompt of type string and it's going to return to us either a string or none depending on what we provided maybe there's going to be an error and if there's an error I want this to return none but I want it to display an error so I want it to be an optional type that it returns then the first thing in here we need to create is the text of type string and none or I mean of type optional and it's going to equal none initially because this can either end up being a string or it might also end up being none now before we continue with this program let's go back to open AI because inside here they have this place which is a playground or this section of their website that's considered a playground where you can play around with the capabilities of this AI before actually putting it in your code and we're going to be using the text DaVinci 003 model and it has lots of presets this is where you can learn how it works you can see examples q a you have a chat bot down here and if you tap on chat it's going to give you an example on how it works we can say something such as hello again and if you tap on submit it's going to generate the next response so at the beginning it provided the prompt of what's going on then it gave some context such as how it should respond and what it should do next and then it actually was able to generate that for us so if you have this open you can just tap on view code and you can just copy this response down here and paste it in your project and it should work right away so it's pretty simple to use this API I'm going to write it by hand because if you're watching this video maybe it makes more sense to understand what each line does also very briefly I want to mention that if you hover over any of these it will give you a small window telling you exactly what it does so I'm not going to explain all of these in detail that will be more of a personal project for you but I will explain them very briefly so here we're going to start off with a try block and it's going to take a response or we're going to create a response of type dictionary and that's going to equal open AI completion dot create and here we're going to create the request which gives us a response first we need to specify a model and the model is going to be text DaVinci zero zero three for the prompts we're just going to insert the prompts then we have the temperature which is a float and I'm going to set that to 0.9 and the higher the temperature the more random your Bot is going to be then there's one called Max tokens and we're going to set that to 150 for now so it's a rather short response it's going to give us if we go back to the API you can see you can play around with all of these and 150 is pretty much the absolute minimum and you can put that up to 4000 and you'll get very long responses but I'm going to leave this at 150 because tokens will eventually cost you money so it's good to keep into account that you want to use less tokens in some scenarios if you are new to open AI during the first three months they're going to give you 18 dollars of credits which are plenty for playing around with this so that's what we're using at the moment I think I used only a few cents in the past few days testing this so it's pretty nice that we have that for free so we only want to use 150 tokens per response then we have top p which is going to be one and that's actually a default value for that and the documentation describes this as an alternative to the temperature then there is the frequency penalty which we're going to provide the value of 0 4 and that reduces the verbatim line repetitiveness then we have a presence penalty which will be 0.6 and this presence penalty determines how often the AI is going to talk about new subjects if you have a very low presence penalty it's probably going to repeat itself so it's good to have it somewhere up there and most importantly we need to provide a stop and this will be an array of these stops so we have first a space and human followed by a space and AI so these are stop Words which means once it runs into these it knows that it shouldn't generate any more responses and again writing this out by hand might be annoying so you are encouraged to go to this playground and play around with the values here so you can just view the code and copy it from here and just to get rid of the error at the bottom we're going to accept exception as e and we're going to print error if there's an error and E it would be good to test that this response actually works so we're going to print the response down here and we're going to try to use this I mean we're not returning anything yet so this will still be highlighted but right below we can type in get API response and we need to provide a prompt and you will type in prompts is going to equal and we will go back here and we will view the code and as you can see the initial prompted provided in here is what we wrote inside this API so I'm just going to copy that just for this example to see if it works it's a very long string the following is a conversation with an AI assistant it's literally everything inside here on one single line with new lines as well and we're just going to insert the prompts then we're going to tap on run and it ended up giving us this Json response which is really good as you can see we have choices and then we have all of this data here and what we really care about is the AI section where it says I'm here to help you with whatever you need what would you like to do today that was the next line generated after we provided this prompt I'm going to show you how we can make our own prompt so we don't have to copy and paste this but this was just to see that everything was working and if I'm not mistaken I guess you didn't actually need this entire line we can just delete it and we can say hello and I think that would still work but I'm not sure and it still did work so I said hello and it said hi there how can I help you so you didn't have to copy all of that you could literally just insert hello if you want what's important is that we know it's giving us a response so everything's working pretty nicely but inside our get API response we don't really care about the Json we just want to get the response of the AI so we're not going to print that and we're going to replace that line of code with a variable called choices of type dictionary which is going to equal response dot get and we want to get choices at the index of zero and to explain what that is these are the choices from the Json as you can see the first element in this dictionary or on this Json is choices so we need to access that part of it and then we need to access the first element inside choices at the index of zero which is this dictionary here and right below that we can say text is equal to choices dot gets and what we want to get is the text we want to be able to retrieve this section of the Json so that's what we're doing with these two lines of code at the very bottom outside of the accept block we're going to return the text so this is the full function that we have in get API response it just tries to make this request if anything goes wrong it prints an error and returns none so now we can try this again but this time we need to print the get API response because once we insert something into it it's going to give us a response back so we can say hello there and if we run this we should get some text back saying how are you today I hope you're doing well it's nice to meet you so we got the section we wanted although we do have two empty lines which we will take care of in just a moment but everything's working quite nicely and I'm going to type in if name is equal to Main and just put that inside there just for now so we have a nice entry point and it looks quite clean but next we're going to create a function that adds each message that we create to an array and it also is going to add each message that the bot creates to that array so we kind of have a message history so we're going to create this function called update list and it's going to take a message of type string whether that comes from the bot or from us and we also want to have the prompt list that we're going to pass in and this is going to be a list of type string then all it's going to do is append the message to that list so it's a very simple function and we can move on to the next function because with that list we need to be able to create a prompt since we're going to have an array of messages or a list of messages we're going to want to consolidate that into a single string and that's where this function is going to come into play So Def create prompt and it's going to take a message of type string followed by the prompt list which is a list of type string and it's going to return to us a string then we're going to have a processed message so I'm going to call this P message of type string and that's going to equal a formatted string with a new line followed by human and we need to insert the message and it's very important we provide the new line the slash n because that's how the bot is currently designed each time we have a new line followed by human or a new line followed by the bot this gives the bot further context on how to proceed with answering the questions but with that we can update the list with the processed message and the prompt list then the prompt itself of type string is going to equal quotation marks followed by join and we want to join everything inside the prompt list then we're going to return the prompt now the final function we need to create has to do with actually giving us the response from the bot in a clean format so here we'll type in Def get bots response and it's going to still take a message of type string and the prompt list of type list of strings so I'm just going to copy and paste that inside and it's going to return to us a string now first we want to get the prompt so the prompt of type string is going to equal create prompt it's going to take a message and it's going to take the prompt list than the bot response of type string is going to equal get API response followed by the prompt so now we're slowly chaining this logic but it's also important that we check that the bot response is not none so if there is a bot response that means we can use update list and we can insert the bot response to our prompt list so we're inserting elements into our history into our chat history and we also need to get the position of the AI response so here we'll create a variable called position of type integer and that's going to equal botresponse dot find and what we want to find is backslash n AI colon and a space we're finding this because we only want to print what comes after that so the bot response is going to equal the bot response at the index of position plus 5. colon so everything Beyond this because right now when it finds this this is going to be at the index of zero so we'll find this here and we want to go plus five so that everything after that is going to be printed nicely else we're going to print the bot response which is going to equal or not print but we're going to assign this value to the bot response that says something went wrong because we got none back which means it could be a poor API call or just didn't have the correct response we were looking for and we're going to return the bot response no matter what happens so we will always have a functioning chatbot now we just need to put this together so we need to create our main function we have all the functionality that's required we just need to glue it together so def main that's not main def Main and here we're going to create the prompt list this famous prompt list that we've been inserting everywhere we're finally going to create that which will be of list of type string and here we need to provide the training data I'm going to copy and paste in what I had from earlier what I wrote initially was you will pretend to be a skater dude that ends every response with yeah and then you need to give some examples such as human what time is it AI it is 12 yeah so some very basic context and you can change this with whatever prompt you want you can say you will pretend to be a fighter pilot dude that ends every response with you or you can say you can pretend to be a sheep or you can be a potato you can tell it to do whatever it has to do and you can make it quite descriptive and then give it some examples after and it will do its best to follow that prompt but that was my basic prompt example that I was going to use for this so that's how I'm going to keep it and all we have to do is type in while true the user input which is us of type string is going to equal input and we just need to say U because that's the user who's typing inside and we need to get the response now so response is going to equal get bot response and it will take the user input as the message and the prompt list as the prompt list then all we have to do is print the formatted string of bots followed by response and instead of using this silly code down here we can just pass in Main and that's all we need to do to create this chatbot now if we tap on run we're going to get the U prompt and we can say hey there how are you mate and we're going to wait for the bot to respond and it will say hey there I'm doing great yeah and it's working perfectly fine one thing you can do to understand better what the prompt list is is just print the prompt list after each iteration on while true so if we run main again and say yup you'll see when it generates a response all of the information that we have all of the chat history is going to be inside here so I mean right now we just set the app as human and AI said yup yeah and that was also appended to the list which means this is growing in context and you as your prompt gets bigger and bigger it has more context on what you were talking about with it as you go through the chatbot well we can actually change this to something a bit more fun we can say you are a potato and will answer as a potato and here we can type in something such as I have no idea I'm a Potato and we need to escape that then we can remove this prompt list print and we can just run the program as normally now we're talking with a potato so what is your favorite food we're asking a potato obviously french fries teach me science sorry I'm a potato and have no knowledge of science or anything else why not because I'm a Potato potatoes don't have the capacity to learn or understand things like science so it was that simple and I say that simple after maybe 20 minutes of tutorial of course but it was that simple to create AI today using the open AI API to create your very own chatbot but with that being done that's actually everything I wanted to cover in today's video if you did enjoy this video and you do enjoy python do consider giving this video a like and possibly even subscribing because I do post a lot of python content on my channel but I guess with all that being said as always thanks for watching and I'll see you in the next video
Info
Channel: Indently
Views: 15,458
Rating: undefined out of 5
Keywords: pyton, pyhton, pythn
Id: qkzhSZAwD6A
Channel Id: undefined
Length: 20min 46sec (1246 seconds)
Published: Mon Feb 20 2023
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.