From the course: OpenAI API for Python Developers

Configure the project: Set up an API key

From the course: OpenAI API for Python Developers

Configure the project: Set up an API key

- [Instructor] Now we want to configure the project with OpenAI. So you see that I have here added a few spots that you need to complete with code. First we want to load the environment variable and set up an API key and also define here the language model that we want to interact with. So let's find examples and instructions from the documentation. And actually we're going to check out here an example of how to set up a starter project. We're going to select from here, generate, and this first example, and you want to make sure that you select the Python library. So what you want to do first is to add to the scope the OpenAI library, then create a new object. Finally, define a prompt. But before you do that you want to set up an API key to be authorized to send requests. So you're going to do that from the left, select API keys, and from here you're going to be able to create your new secret key. I'm going to select this. So defining a name is optional, so I'm just going to click, Create secret key. And from here you're going to read an important message, which is to save these secret key, somewhere safe and accessible. Because after closing this box, it's no longer be visible so you can make sure that you copy it first and in the case you lose it or if it is compromised, you're going to need to create a new one. So I'm going to make sure again that I copy it to my clipboard and then click Done. Let's go back to our project and find instructions as to how to set up the key. So you have different options. You can run this script in the terminal to set this one up. So I'm going to do that first, or you can also add it to a .inf file. That I'm going to do next. So I'm going to show you both ways. I'm going to first copy this and then go to the terminal, and then run this. And I think that I missed just one quote at the end, so just make sure that you add it. And finally what you're going to do is echo this one, OpenAI API key, and you're going to see that this is properly set up. Perfect, so next what we're going to do is take this one and this time what I'm going to do is add it to my .inf file. You're going to find this file for you and you see that I already have one. So I'm just going to replace with the right API key and make sure that I can send request API calls. And once this is done, you can just delete this one or you can keep it. But what you need to do is to make sure that you set up the API key on the .inf file. Let's go back to Main.PY and make sure that we load the environment variables. And after that we're going to set up the API key with OpenAI.APIKey. Then we can access with this. And actually I'm going to change, it's going to be os.getenv like this. And next I'm going to define also the language model. I'm going to define this with a constant, like so. And let's go check out again the documentations to see which language model we're going to use for the next example. We are going to find this under API reference and here under models. And actually right here you can find it also here, text generation. And that's going to be actually not this one, but a little bit lower under Legacy because this one, the very last update for completion was July of the year 2023. So that is something important to keep in mind, which is to always keep an eye on the latest release to keep your project current and up to date. But for now, we're going to use this example with this language model, which is GPT-3.5 Turbo instructs. So we're going to add it to our project, replace like this. And next I'm going to also define an example of a prompt just for our next first example, and I'm just going to write this as a test prompt and I'm going to ask the language model to say, say this is a test, that's going to be the first test. Say this is a test. What we're going to do is start our project. I'm going to go here and make sure that we clear and we're going to run it with Python three, main.py, and we're going to start with the menu. Here we go. So we're going to have the options to ask a question or exit the program, and you're going to see that for now, we just have the options to exit. So I'm going to add an L statement and that's going to be the next steps. What we want is to define a prompt to be able to interact with the language model.

Contents