Custom Scripts

In this lesson, we will learn how to use Python to create a script that connects to Chat GPT.

We will use GPT-4 to create the script that allows us to chat with Chat GPT using the OpenAI API key. Even if you are not a coder, this skill can help you create code and scripts for apps or extensions on your computer.

You can follow the step-by-step instructions provided by Chat GPT to set up the environment and install the necessary software on both Mac and PC. By the end of the lesson, we will be able to create a Python script that runs on our local machine and communicates with Chat GPT using the OpenAI API.

Transcript

Okay, in this lesson, we are going to use Python, a coding language, basically, to create ourselves a little script that uses Chat GPT. And we’re going to use GPT 4 in particular to help us create this script. And this is an ideal skill if you want to create any an app, or you want to create a Chrome extension, something that runs on your own computer, then Chat GPT can help you make it a reality. Now, I can tell you, it will probably take a little bit of back and forward reporting with ChatGBT. When you run the code and it doesn’t work, then you’re going to have to tell ChatGBT why it doesn’t work. Then it will come back with a suggestion of a change and so on. It rarely will just get it all right first time. And to be fair, it’s not really because of Chat GPT. In many cases, there are variables that could apply when running on your computer, such as where the folder is or something like that that can mess the code up. But by working with Chat GPT to get the job done and helping it, and it’s helping us, you’ve got the ability to create code and create scripts without being a coder, which is pretty amazing.

And it’s perhaps one of the most amazing things about Chat GPT. And soon they’ll be bringing out, hopefully for everybody, their coding plug in as well, which is really geared towards doing that and nothing else. But we can work with this right now and create some stuff. So what I thought I’d do is I will get, I’ll put the prompt in now that will get ChatGBT to create a Python script for us and tell us how to actually install it and set up Python on our machine. We’ll make sure that it covers the Mac, which is what I’m using. And also, if you’re using a PC, we’ll do a bit of we’ll make sure that it gives instructions on how to do that as well. And then we’ll muddle our way through this. Now, I’ve already been doing this, so I know how the work that goes into it back and forth and everything, which is why I’ve pretty warned you. But me as a non coder was able to create a Python script that ran on my local machine and enabled me to chat with ChatGPT using the Open AI API. So not using this interface through my web browser.

So it’s pretty cool. It’s pretty powerful. And I think if you have ideas of apps that you want to build, this can really help you do it. So let’s get started then with this lesson right now. And we’ll get the prompting to start the ball rolling in creating a script. Okay, here we go. I would like to use Chat GP to create create a simple Python script that would allow me to chat with Chat GP using the Open AI API key. Full stop. Can you please give me clear instructions on what to do and create the code? Full stop. Please assume that I know nothing at all about coding and that I do not have anything installed on my computer, which also may be a Mac or a PC. So you will need to tell me how to install everything as well and in the correct order. Okay, so let’s just correct these little things here. The Open AI API key. Everything else I think is okay. So let’s see what we get from that. I’ll just take out the space with chat GPT, and then we’ll just click Go. And let’s see what we’ve got.

So I’ll provide you with a step by step instructions. I’ll zoom in a bit there. There we go. To set up the environment, which means basically everything that you need to create and run the code, install the necessary software, and create a Python script to chat with ChatGBT using the OpenAI API. So here we go then. This is our first instruction. And it’s basically suggesting that we’re going to be using something called homebrew, which we can use this here anyway, we can exactly use that code. And then on Windows, it’s giving you another basic way of doing this, which is going to be to just download and use Python on your own computer. So we’ll let it carry on doing this stuff. Here we go. Look at this. It’s literally giving us step by step information on what we’ve got to do. We’ve got to install basically Python, and you’ve got to get Python running on Windows or the Mac. Then we’re going to install the Open AI part of everything that it needs to run. And then we’re going to go and get our Open AI API key from our account. And I’ll show you how to do that part.

And then we’re going to actually create the Python script. And you can use TextE dit if you’re on a Mac, or you can use Notepad on Windows. And then you’re just going to save them as a. Py file when you’ve entered this code. That’s how you do it. And then looking at the code right here, it does say just there, look, the openai. Api key, and this is where you will paste in your Open AI key, which you can get from your account right here. So this is me in my playground. And then you will just literally go to create secret key, and you’ll get a big long key that you can paste into this area here. But I am going to show you this step by step because you’re going to see me doing all of this. But it does tell you there as well, replace your API key with the key, and then we’re going to save the file as chat. Gpt. P y in a directory of our choice. And then it’s going to tell us how to run the script. So that is about it. Now, it’s telling us it’s going to put it in documents.

We’ll probably put it somewhere else, and then we’ll run the actual script. So here we go then. This is the process. Let’s get busy. So the first thing that I would do on a Mac, you’re going to have to do it slightly different if you’re using a PC. But we are going to go ahead and check if we’ve got Python installed already. I know for a fact that I have. It’s very, very easy to install. All you need to do is go to here, which is Python. Org forward stroke downloads. I’m going to put the link to this underneath the training video. And then you download the latest version of Python for your device. So for me, it’s for a Mac. If you’re on Windows, then you just click Windows and then download the latest stable release just there. So that’s how you actually download Python. And then you install it in the normal way that you would anything else. Just double click it if you’re on a Mac, and then go through the install procedure and so on. So just install it like you would anything else if you downloaded it from the internet.

So going back to the instructions then, what we would need to do at this point, you can see it tells you there, you need to open Terminal. Now, what I do for that is literally just go to the top right spotlight search, and you can type in Terminal if you’re on a Mac. And then we’ll just open up the terminal and I’ll drag that in View of View just there. And there we are. So there’s our terminal like that. We’ll put it right there in the screen. And then it’s asking us to check if we’ve got Python installed, which I’ve gone ahead and already done that part. So I’ll just click there. And if you have got Python installed successfully, it will tell you the version. And for us, that is 3.11.3. I can see just down there. Now, it’s saying that we should install Homebrew, which is a package manager. It basically helps us pull in things into a terminal and into Python so that it can use them. And then just here, we’ll need to just copy this right here, this code. So I’ll copy that code now. And then once I’ve got that code, I literally would go and install it into the terminal.

So let me just do that now. We’re going to go and get the terminal again, bring it up, and now I’m going to paste in the code that it’s given me. And here it goes. So checking for pseudo access, which may request your password. Okay, well, I can do that. Let me just type in my password. I hopefully can. How do I do that? Let me just try this. Sorry, try again. There we go. So I did the right password, and then now it’s going to try and install homebrew. So press return and enter to continue or press another key. So I’ll just press continue. And there it goes. It’s now installing the first thing that it’s asked me to do, which is Homebrew. And that is it done. There we go. And it’s now saying homebrew is run entirely by unpaid volunteers. Next step, run these two commands in your terminal to add homebrew to your path. Uses json west, blah, blah, blah. Run brew to get started, and so on. Well, I don’t know what I’m doing with that, so I am just going to follow the instructions that chat gpt has given me.

And then it does tell me right there, I have to type in brew install Python 3. So let’s do that. And we’ll go brew install Python 3. And we’ll press Enter. Now I think we’ve already done it. So it will probably say that we don’t need to do it. Okay, so there’s the first issue then. We’ve gone to go ahead and do it. And now it said that ZSH command not found brew. So that’s fine. What I’ll do with that is press CTRL C. So I’m copying that that it gave me. And now I’m going to say that I was trying to run the command this, and it didn’t work, basically. So let’s just go to the very bottom of chat GPT, and we’ll tell it that it didn’t work. I used the command you told me to, which was up here, wasn’t it? There we go. Just there. Brew install Python 3. Whoops, changed that over. Python 3. But received the following error. And there we go. I apologize for the confusion. It seems like Homebrew is not installed on your Mac. Please follow the steps to install Homebrew and then install Python. So it’s saying we need to do command and space.

Type terminal, press Enter, install Homebrew by pasting this command. Waiting for the installation to finish. It might take a few minutes. We’ve got to close and reopen terminal, I’ve noticed here that it says. So maybe that’s what I’m going to do. I’m going to close off terminal right now by pressing the X and then also just quitting the app. And now I’m going to go back to it again. We’ll look at the open it up fresh. And this time we’ll try the same command. We’re going to go here, we’re going to go brew install Python 3. Whoops, I haven’t spelt it correctly. Brew, install Python 3. rew, install Python 3. And it’s still saying the same issue. So once Python is installed, you continue the rest of my steps. Now, the only thing I can think of, logically, is that it is because Python is already installed. That’s the only thing that I can think of that could be causing me the issue. So I’m going to carry on and go up the line here to see what my next move would be after I had successfully installed Python, which appears to be this one, install the necessary packages.

So now it’s saying, open terminal on a Mac or command prompt on Windows and run the following command. We have to install a package manager for Python. If it’s not already installed, yes. So we’re going to have to install this. So let’s try using that. Paste that in here. There we go. It’s looking for links. Requirement has already been satisfied in this case. It looks like it’s already in there. Okay, well, I followed the order the instructions, so that’s fine. The next thing it’s asking us to do, as you can see just there, is to install the Open AI package. So I’m going to do that, bring up Terminal, and then paste in that command, press Enter, and now it’s getting to work installing that. It’s telling me as well that there’s a new release of PIP available 23.1.2 to install RunUpgrade. Well, I’m just going to do that then. So I’ll type in what it’s told me to do to update it. And there we go. It said it successfully installed PIP 23.1.2. Fine, we’ll go with that. Now, I’ve already got an Open AI API key and I’ve showed it to you there. If you’re using ChatGBT, you’ve already got an Open AI account.

All you need to do, if we just pull that into there, is just go to your account and then you can see View API Keys. You’ll click Create New Key, call it something like Python script. Whoops, I got a thing about spelling Python wrong, haven’t I? Create special key. There’s your key. You copy it and then you press Done. And don’t share that key with anybody because it basically links up to your account and will burn through your money because it basically calls API calls. And API calls cost money with Open AI. By the way, you will probably also have to go to Billing and then enter some credit card details in order to be able to use these API keys like this. But the good thing is in Billing, if I can just go to it here, you can have usage limits. And I strongly encourage you to have usage limits on your account just in case anything went crazy wrong and it started burning through all of your money. You can have a soft limit where you get an email notification that you’ve spent that amount. And you can have a hard limit, which is where it just stops and it will just reject any further spending on your account.

So that’s very, very important. And that’s where you’re getting your API key. So now we’re going to go back to the instructions there. And it’s telling us to go ahead and now put that code into a text file and also put our API key in there. So that’s fine. I’m going to copy that code and I’m going to go to my text editor, I’m going to click Create New Document, and then I’m going to paste in the code that it’s given me right here. But I’m going to replace this part which says your API key with the API key, which is over here. So if we go to edit it, if you ever want to get it again, just go edit. Whoops, sorry. How do we do that? Okay, no. So the situation is, once you’ve created it, you cannot now access it to see it again. So you’re going to have to create a new key. There we go. Copy it, and that’s it. You won’t be seeing that key again. You can only delete it. And then we’re going to paste the code in to text edit. And there we go. We’ll paste it in right there like that.

Make sure that you’ve got it exactly right and you haven’t deleted anything like these little speech marks here at the end. And then what you’ll want to do is to save this. And we also want to make sure that it is in plain text format. I can see that this is in a. Rtf, which is no good at all. That’s rich text format. You don’t want that. So I’m going to format it as plain text. And now you can see it’s swapped to. Txt. And I’m going to file, save it as we are going to save it on my desktop, and we’re going to save it as what it asked us to save it as, which was just down here chat. Gpt. Py. So back up to here. So we’re going to go chat. Gpt. Py. And there we go. So we won’t have any. Txt applied to it. I’m just going to double check that that’s correct. It’s asked us to do chat. Gpt. Py. One thing I will do is not capitalize it as well. Little details matter when you’re doing this stuff, really does. So there we go. I’m happy with that now.

Chat. Gpt. P y, saved it to my desktop and I’ll just click Save. So that’s it. I’ve got that file now and it’s got my API key in. So that’s perfect. And it tells you next to run the script, what you need to do is navigate to where we saved that file. And in my case, that’s the desktop. And then we need to run the script by typing in this right here and pressing Enter in Terminal. So first of all, we need to go to the correct folder. And in my case, that is actually… Let’s get Terminal up again here. That is the desktop, as I’ve said. So I will go to cd desktop, which now puts me into that correct location to run the script. And then I’m going to paste in what it’s asked me down here to run the script. There it is. And there we are. We have now got the little words, Welcome to Chat GPT. So let’s ask it a question and make sure that it’s worked. So I’m actually running this to be clear, and I will try and zoom in on this. Let me see if I can zoom in.

Let’s go with View, bigger. That sounds better. There we go. We’ll go View and we’ll go a bit bigger again. There we go. So now you can see down the bottom here, it’s got Welcome to Chat GPT. Well, let’s try it out. And I’m going to say, Can you tell me where and when Prince Charles was born? And let’s see if it allows me to speak with chat. There you go. Chat GP has now responded. Prince Charles was born on November 14, 1948 at Buckingham Palace in London. And in a couple of days’ time, he’s going to be made king. So lucky guy. So that has now worked. That’s worked fully. So what we’ve done is we’ve, yes, we had to go a bit back and forward and everything else. But actually, once we’ve done what it said, the code has worked perfectly first time. And it’s enabled us to have chat GPT through our desktop file here, this terminal that is running the Python script and now communicating back and forward using our API to chat GPT. Now, this is a very simple and basic script. Now, we could take this further as well if we wanted to.

And I’ve put in here, how can we create a nice user interface for this chatbot instead of running it like in terminal and it looks a little bit complicated and not very nice. So it said to me to create a simple and nice user interface for the chatbot, you can use a Python library called Tkinter, if that’s how you pronounce it, which provides tools to create graphical user interfaces, a DUI for desktop applications. Here’s how to modify the code. And then first of all, it’s got us to install this Tkinter, but it actually comes preinstalled with Python on Windows and Mac, so we don’t need to do that part. Next up, it says to modify the existing script. So what you’ll do now is you’ll copy the code just here, and then again, make sure you’ve got the correct API key in again. So you need to get that from your existing script, copy it somewhere, and then we can paste it into this code. And then that’s it. So we would take that code now, press copy, and then we’d open up the file, which I will do again in text edit. Here it is.

And then you would paste in the code here along with your API key in that spot, which is what it’s telling you to do. Now, when you save that, you save that file, you’ve saved the new code, and then you basically do the same thing, which is just to go to Terminal and then copy that and then launch it. So here’s Terminal now. I’m going to launch that same script with the modified code and press Go. And what you can’t see is this. Just over here, it is now launched a nice little chat window that we can use. We can resize it and everything. Obviously, it’s not super fancy, but it’s better than having nothing. And now we can ask questions to chat GPT through the Open AI API. So let’s try something now and let’s just say, okay, when was the queen born and where? We’ll try that. Send. And so we press the little Send button and fingers crossed, there you go. Chatgbt has responded. Queen Elizabeth II was born on April 21st, 1926 in London, England. So there you are. So we’ve got ourselves a fancy little box. Now, I could improve that.

I could say, Move the Send button underneath. Can you please make it red colored? Can you make the background white? We could do anything we want to by just continually going back to Chat GPT and asking it to refine the code, give us the new code. We put the new code in that same file, we save it, and then we go back to Terminal and run the script. And every time we do that, we get something different, some improved version of our script. Now, if I was doing a more complicated script, you can imagine that this probably would have taken quite a long time here to get this right. But it did help us by telling us what to do after we had found an error. And in fact, that error really was caused by me because I had already installed Python and was asking it to do it again using this homebrew. So it was getting itself a bit confused there. But overall, the script was created and it worked. You can edit the script at any time you want to by asking it to make changes. So you can see there that it’s using the engine text Da Vinci 002.

Well, if we go over to the playground, which is just here in your Open AI account, that’s over here, you can see that we’ve got these different versions of Da Vinci. Well, if I wanted to use 003, then hopefully I would be able to change that code just there to 003. And also I can control the max number of tokens by changing this here as well. The max number of tokens is basically how many tokens we will use for our requests. Stops you from having a gigantic load of tokens used up that costs money from your account. And then we’ve got the temperature, which is another video all in itself. But yeah, basically, I’m pretty happy with that. It worked. When we’re finished, you can see what it says there. If we say to it quit or exit, then it will print goodbye. So let’s try that out and end the training. So I’ll say exit and it says goodbye and there we go. So there is the whole thing complete and full circle. It all works. And I hope you found that useful just to demonstrate the ability of ChatGBT to create any script that you can imagine, really.