From the course: AI-Powered Software Development: Coding, Testing, and System Design

The basics of generative AI API integration

- [Instructor] Alright, well so far in this course we've been taking a look at how to use generative AI to write code for us. And what we're gonna do in this section is we're gonna flip things around and see how to write code that uses generative AI. Now, the first thing that I want to do before we get started and get into the details is talk a little bit about the basic idea of having our programs interact with generative AI on our behalf. If we open up ChatGPT and enter in some sort of prompt. So for example, let's say something like, tell me three fun facts about coffee. Well, this is just one way of getting output from ChatGPT, and this is the most human friendly way of doing so. So what we're seeing here in the browser is what's known as the user interface or UI. And the problem with this is that this is designed for humans to use, to be able to look at and then point and click and type keys on their keyboard. And that's really not the easiest thing for computer programs to do. So in other words, when we want to have a computer program that interacts with something like ChatGPT on our behalf, instead of the UI, we use something called the API. That stands for application programming interface. Essentially, this is the programmatic equivalent of the user interface. For users you have the user interface, for programs you have the API. So the first thing that you're gonna wanna do here is go to platform.openai.com and you'll probably need to set up an account there, but I'll just let you do that on your own. It should be pretty straightforward. And once you've done that, you're gonna wanna go to the dashboard link and then click on this API keys link. What we're gonna do here is we're gonna generate an API key, which is essentially like a password that our program can use to communicate with the OpenAI API. So what we're gonna do here is we're gonna click this create new secret key button and then we're going to enter a name for this key. I'm gonna call this AI powered software development just to basically remind myself what this key is for and I'm gonna click create secret key. Now, the first thing that I'll tell you now that we've created the secret key is that you really want to be very careful with this. Treat this like you treat any password because it will give whoever has this key full access to interact with OpenAI on your behalf. And that could mean costing you a lot of money if it gets into the wrong hands. So we're gonna click copy here and what I'm gonna recommend for you is that you go back to your project. And by the way, I got rid of all of the files from the previous sections of this course because I felt like these demonstrations would be a little bit easier if we were starting from a clean slate. And so what I'm gonna do is I'm going to start off here by creating a new file and I'm gonna call this file .env. Now, this kind of file is used generally for storing things like API keys and allowing our program to use them, but not making them accessible to anyone else. We don't usually wanna have these API keys in our code because then when we commit our code to say GitHub, anyone who can see that repo, especially if it's a public repo, that's everybody, is gonna be able to access our API key. And that is not a good thing. So what we're gonna do in here is we're gonna say Open AI API key equals, and then we're gonna paste our OpenAI API key. It's quite a long string and I guess I should mention here that you may wonder why I'm showing this API key to you. That's just because I want you to know what it's supposed to look like. And I'm going to delete this API key as soon as I'm done recording this course. And by the way, in case you're wondering how to do the same thing, if you go back to here, we're gonna say done. You can delete your API key just by clicking that little trashcan icon next to it. And that will make it so that that API key no longer works when trying to access the OpenAI API. Back to our program here. Now that we have the OpenAI API key in our .env file, there's two important steps. The first is we're going to create a new file called .gitignore. And actually, if you're using the GIT repo that I gave to you here, you can just click on the existing .gitignore file. Didn't realize that was already there. And then we're going to say .env and add that to this .gitignore file, which means that the contents of this .env file that we created will not be committed to the repo. So this is going to be missing this file in our GitHub repo, which in this case is actually what we want. So that's the first thing is creating that .gitignore file. The next thing that we're gonna do is we're gonna need to install the OpenAI library into our project. And you can do that by running pip install openai. Assuming of course that you have Python installed locally. And once that's installed, that should allow you to run any of the code that we're going to write in the next few videos. So that's the basic setup and the basic concept behind writing code that will use generative AI on our behalf.

Contents