 Hey everybody David Shapiro here with part two of my getting started with Python and GPT-3 from scratch video series Before we get started, please go ahead and like and subscribe this video That will help me work towards getting monetized on YouTube, which who knows maybe one day I'll be able to do this full-time, but also if you like My videos my content you find them helpful. Please consider hopping over to my patreon link in the description or comments And consider supporting me directly That will go much further towards helping me to do this full-time You see how I churn out content I'm happy to keep doing this and the more support I get the better it'll be so without further ado Let's jump back over to our our project our learning So this is our repo This is where we left off. We did a hello world a read me and And so on and so forth. Now what I've done is I added a I added git ignore and what git ignore does is I've added open AI API key because last time I said whatever you do don't ever store your API key publicly or in a git repo and so what get ignore does and this is what it looks like locally is It tells git to ignore that File you can do directories. You can do wildcards as well So you could do like star.json to ignore all json files. That's fine So but if I look in my repo my key is here So you should have last time you should have created this text file and populated it and Get in and get your hello world to work. So let's take a quick look at hello world All this does is opens a file sets the API or it first we declare a function that opens a file reads it and returns That file contents and then we have a function that will do a DaVinci completion and then we will Will we get we pass it a prompt that says write a list of famous American actors We get the response from GPT three and then we print it out So I'm assuming you did your homework and you you've tested this so Python Hello world So it'll take just a second and then it'll spit out Tom Hanks, Leonardo DiCaprio, Jennifer Lawrence Brad Pitt. Excellent. So it works now This was all statically coded. So what are we going to do? We need to do something more useful So what we're going to do is we're going to hop over to the playground and we're going to do some prompt engineering So let's just say we want to do our first chat bot Um, so we'll say the following is a conversation between user and Jax Jax is a sentient sentient machine with the goal of I don't want to say something evil like taking over the world. Let's say Jax is a sentient machine with the goal of I don't know world peace That's fine Okay, so let's say user Hey Jax, what are you doing today? And then let's see what happens if we just hit enter That says Jax. I'm working on my plans for world bees. Not bad. So what's the first thing that you notice? There's a space here. That's not necessarily a bad thing, but we will have to contend with that space If we when we go to automate this in code, that's like, okay user, okay How are you going to achieve that? And we say what's next? I'm still working out the details, but I'm confident I can make a difference user So you're not that smart, are you? I'm being real sassy here. Oh Okay, all right, so you can see that this works So now what do we do? How do we actually take this into? And and do this with code. So let me show you So the first thing we do is we copy down This prompt this initial prompt and you see how sent how how how simple it is So what we'll do is we'll copy that we'll say we'll save this into a new file Do a couple new lines and then we'll do block and I'll show you what we do with the block in just a minute So we'll save this as prompt chat So now we have this which is just a copy paste of what we did in the playground And it's like, okay. Well, how do we accumulate these this chat? How do we what do we do here? I'm glad you asked so we'll get we'll start a new file and then we'll do file F alt F a for all file save as and we'll go to all types and We'll say chat dot pie. So now we've got a new We've got a new Python script that we're working on This will be in the repo. So you will have been able to just clone this down But what I recommend that you do is that you ignore the existing file You can look at it for reference, but that you follow along and do this coding by hand So first what we'll do is we'll just copy the existing code that we have that works There is no shame in recycling Your old code Just remember that if you have errors in your old code, you're copying those errors. I do that all the time Okay, so we start here. We've got our completion It uses DaVinci And we say prompt right a list of famous American actors All right. So here's your first big python lesson We want to have an open-ended chat. So there's a few things that we need to do First we get rid of the prompt not using that So we'll say while true So while true, this is what's called a this is what's called a loop There's a few kinds of loops the two primary loops that you'll use are a while loop And so if you do while true, it's just always true. So this is an infinite loop. So while true What are we going to do? Well, first we need user input and we'll say user input equals input And we'll just say user user And it's that simple. So this is a built-in Construct a built-in function for python that allows you to just take in console user Console input from the user Great. Okay. Now what? Ah, well, we forgot a step. What do we where do we put this? Do we just pass this? Do we just do gpt3 completion user input? What do you think's going to happen? Nothing because let let's say that you see something that says like That you get this variable Here, let me just show you actually python User input equals input user This is user. Um Hey jacks, are you alive? Okay, now what? So then we say print user input Hey jacks, are you alive? So if we just print this or if we just send this up to gpt3 It's not going to do anything for us It kind of intuits that uh, that it's a machine or whatever, but there's no other framing, right? So it it it if we however Um Go back to our prompt right here We say user Hey jacks Are you alive? Of course I am I'm a sentient machine with a goal of world peace. See a little bit different. All right. So how do we do this? I'm glad you asked So we say so we're going to say no, that's not what we're going to do. We're not going to just send up our base Input we're going to say conversation equals list So what you can do is this is called instantiating a variable or declaring a variable So now conversation we're saying conversation is an empty list So we're going to accumulate it as a list of As a list of text and what this does is this instantiates another variable a text variable or a string variable As um as an input Okay, great. So then what do we do with it? Well, how do you add something to a list? So first you do Conversation dot append and then we don't want just We don't want just this We want this to look so hey jacks are you alive? But if you look here, I had this little bit so it tells you who was speaking, right? You see that in every text message every chat. You got to know who's speaking So we want to have both the user and hey jacks. Are you alive? So what we do here is we do We'll copy This bit so that'll be that'll be hard coded And then we'll do percent s And so basically that's a placeholder for Populating a string so percent s means populate a string here And then we're going to do percent user input and so this says all right when you when you store this variable Populate this bit with this variable and so then what happens is In memory, it'll look just like this Great. Okay. Now what? How do we convert How do we convert this conversation into something usable? So that we can actually send it up to gpt3. So let me let me go ahead and just Um, I'll I'll follow along here in in the python interpreter here. This is a little big So we'll say Okay, so Conversation equals list. Okay, cool. You can do you can follow along in your in your own interpreter um, and we'll say Conversation dot Append user Hey jacks. Are you alive? Okay, so that's what it'll look like and then we do print conversation And you see how it's it's wrapped in brackets. That's how you know that it's a list Okay, so then then what let's imagine that jacks gave us a response. Actually, we can just go ahead and copy jacks's response So we're we're basically simulating what's going to be happening in the background So then we'll do conversation dot append and then add the uh The single quote and do shift insert. So the reason that I do shift insert instead of control v Is it sometimes your terminal session? Doesn't really like control v And shift insert is just a little more reliable um So of course I am Okay, so now if we do print conversation You see that user. Hey jacks are you live jacks? Of course I am One advantage of a list is that it is always in the order that you add it that you created in and so Lists are uh, what is that? It's not immutable. It may be immutable. No I can't remember the exact term right now. But anyways lists are always going to be in the order in which you created them Okay, so Now what so like conversation. So let's say print Um conversation and then if we do bracket zero, so that's index zero That'll just print the first part And the second part How do we how do we get this up into gpt3? um What we need to do is we need to convert this list into a full text block so that you can see as it gets bigger and gets accumulated We will have um, we'll have a Something that we can take from a list we can accumulate the conversation We can save it locally But then we also need to be able to convert it back into text to send it in a gpt3 prompt because gpt3 doesn't understand python lists It only understands text. So what we do here is we do um single uh single quote Slash backslash n so that's for new line and then we do dot join So this is this is a method that is built into the string type in python that allows you to um It's basically a list comprehension. And so what we'll do is do uh conversation um, and so what we'll do is we'll do uh text equals Uh backslash in join conversation So one new line and then we're going to put a new line between every bit of conversation. So then if we do print text It puts it back into one text block great So we'll just copy that code because that's exactly what we need Um text and join Excuse me. Um, okay, great. So now we've got the text block that we want to put here Um, so basically what it's going to look like when we're done Copy that real quick. We're going to say it's going to look like this And if we put this into um Up into gpt3 we can test how it's going to look the user. Um Um Yes, but how do you know you're a you're alive? There you go. Okay. So you can see this is this is coming right along But we're still we're still a couple steps shy. Let's do a control zeta and do that So we've got this little token here And it's like, okay. Well, what do we do next? All right, so we will use We'll say prompt equals Open file. So this open file is the function that I wrote here And we're going to say open prompt underscore chat dot text. That's this right here Um, and so we just pass it and what this passes back it just passes. Um, it says return in file read So it'll read the file for us and it'll pass it back as is But what we want to do is we want to replace this block with the text Um, you know, so basically we'll do that. We'll we'll do functionally the same That we did just a minute ago. Whoopsies That's not what I meant to do There we go Okay, so we'll save that. Um, we'll we'll we'll we'll functionally replace that block so it'll look like this, right? So we'll do a virtual control v to paste in instead of having this this placeholder We'll actually paste in our um our our text block. So we do dot replace And then we'll say what are we going to replace? We're going to look for block And we're going to replace it with text And so this variable right here Is this one and so you see how text is used in multiple places. That's bad form So what we're going to do is we're going to rename this and and we'll call this text block Um, because that's a little bit more specific about what what it is that it is so this is the conversation This is the text block of the conversation. So we'll Do that And this will make the prompt is going to look just like this Now there's one last step missing so, uh, fortunately, uh, DaVinci, um, is This one is aligned well enough so that it knows once it sees a couple of messages back and forth It knows what format to follow. However, we don't want to rely on that So what we're going to do Is we're going to add just a little bit to the end of the prompt. So we'll say prompt equals prompt plus We'll do one more new line jacks And so basically what that'll make it look like is this So, uh, it we're basically we're basically priming the next line Because one thing that can happen this this is doing pretty well not to do this But one thing that can happen is if you don't have this here, it might like Uh, continue the user's side See how what it did here is if I took out the um the question mark It added in the question mark for me and then continued with jacks, but let's say you didn't want that Um, and you just say jacks See it's a little bit different So you got to be very careful about the text that you pass back. Okay, so we we, uh, we We load our prompt. We populate it with the text block of the conversation and we say, okay jacks, you're up So now what now we do, um response Actually here, we've already got that code here So response equals gpt3 completion prompt. So this is going to be jacks's answer Um, and so what we'll do is We will do print jacks And then we'll do comma response So print allows you to print multiple variables at once So we're basically printing two string variables And when you put a comma between it, it basically just acts like a space It won't work if you do this because it's like, I don't know how to how to view these two things So we just do jacks comma response great, so But don't we need to accumulate jacks's end of the conversation in the text block and the in the list? Absolutely, because if we don't it's just going to talk be talking to itself and our It will we're only right now. We're only recording our side of the conversation Um, okay, so then what we'll do is we'll do conversation dot append And we'll make this look exactly like this one Except we'll change this to jacks And we'll change user input To response There you go All right, so we're just about done and ready for testing One thing that is important to note here you see how it added a couple new lines So for whatever reason gpt3 will sometimes do this where it'll it'll add new lines Between stuff and we don't want our our our text message to get all scattered. We want it to look Like this So how do we do that? I've got that I've got that covered right here with this little function. I think I mentioned it in the previous video So it always will cut out any excess space around it Now there's one last thing sometimes What happens is Is gpt3 will just it'll have the whole conversation on its own, right? It'll just it'll it'll try and fill in the the position for the user and jacks And so what we want to do is we want it to stop if if gpt3 ever generates these tokens user or jacks and so what we do is we'll add those Right here so jacks and then User I could have just typed it out. I guess It's five characters. I should have typed it out. Okay. That's fine So this will basically tell gpt3 just in case Do not have this whole conversation for me Because what I can do is I can say If we change this to imagine A conversation between user and jacks What'll happen is it'll do the conversation for me Well, sometimes it will Okay This prompt isn't causing it to do that, but sometimes it will I wonder if it'll do it if we if we turn the temperature up There we go See how it completed it for me So sometimes it's like, you know, it'll kind of make up its own mind about what to do We'll turn that back down to 0.7 Um, okay. I think we're ready for testing So we'll come here Once you're in python you hit control z And then enter and that'll that'll allow you to exit out of your python loop All right, cls to clear the screen. We'll do python chat Can't open No, such file. Oh, right chat.py. I forgot to do the completion All right user Hey jacks, what are you up to? I'm up to my usual goal of world peace great Uh, okay How are you doing that? By promoting understanding and cooperation between different nations and groups Do you have any links? I need evidence. I'm being real difficult Here's a link jacks.org Completely made that up. That's great. Okay. Well, jacks is trying his best Um, okay, so we see that our chatbot is indeed working But we are not seeing anything going on in the background So what if we want to do like debug, right? What if we want to see what's going on in the background? We'll come back to that next time, but I think we've done enough This episode. So thanks for watching. Once again like and subscribe Uh, and then also please consider hopping over to patreon to support me directly Um, that will really turn this up to 11. Thanks for watching and take care