OpenAI Setup - Instruction
Signing Up
To start using OpenAI APIs, you’ll have to sign up for an account. If you already have a ChatGPT account, the same account can be used here, and you can skip to the next section to create an API Key.
If you don’t have an account yet, go to platform.openai. Then, click Sign up. You’ll be taken to the sign-up page as shown below.
Select your preferred way of signing up. For example, after you select log in with Google, you’ll be taken to the screen below.
Fill in your details. If you’re curious why they’re asking for your birthday, maybe they want to be thoughtful and greet you on yours :] or you can read their article about that here. Once you’ve filled your details in, click Agree.
You’ll be directed back to the platform overview, as shown in the image below.
Nice! Now, you have an account on their platform.
Creating an API Key
Since you want to access OpenAI via APIs and not via their ChatGPT interface, you’ll have to create a way to authenticate yourself. This is via API keys.
To generate one, starting from the overview page, then click Dashboard. On the dashboard page, like below, click API keys.
If you just started your account, you may still need to verify your phone before you can generate API keys. Go ahead and click Start Verification.
You’ll see this screen below.
Enter your phone number, and go through the verification steps. At the time of writing, you might get free credits if you haven’t used your phone number before! Once you’ve successfully finished verifying, you’ll see this screen below.
You can choose to name your key as you wish. For example, name it “bootcamp”. The name will allow you to identify your keys later if you have multiple. Key naming is useful, for example, if someone accidentally or maliciously obtained your key and you want to stop them from using it so you won’t be billed. Speaking of accidentally getting API keys, in this lesson you’ll also learn how to securely store it.
OpenAI makes sure that you have control over your keys. For the Project field, select the “Default Project” for now. This would be useful if you’re managing multiple projects and want to share your project with teammates but you don’t want all of them to see the keys from other projects.
Continuing with the theme of “controlling your keys”, the Permission field allows you to create Restricted keys and Read-Only keys. If you want to generate text, you’ll need write access to the /v1/chat/completions
endpoint, at least. To see the full list of controls, see the image below. For simplicity, select all permissions for now.
Next, click Create secret key. You’ll see the screen below.
Since OpenAI won’t show you this key again, remember to save it somewhere else, like in your preferred text editor or notes app.
Good job! Now you have an API key.
Securing your API Key in Python
Now that you have an API key, you’ll want to securely use it in your Python Environment. For this lesson, you’ll use Jupyter Lab.
Jupyter Lab
JupyterLabs and Jupyter Notebooks are similar, and you might be familiar with Jupyter Notebooks from previous courses. In fact, the Jupyter Lab opens Jupyter Notebook’s ipynb
files. The additional advantage of using Lab is that it integrates your file system into the interface, allowing you to navigate across multiple files and folders within the lab itself. This is advantageous if you have multiple Jupyter notebooks or if you want to see the other files in the same folder to use in the notebook.
If you don’t have Jupyter Lab yet, you’ll have to install it. Follow the instructions in here to obtain it.
If you already have it, run it using jupyter lab
in your Terminal, make sure to not close the terminal window where you started it. It should open your browser, and you should see a screen like this:
Then, navigate to the m3-tgo-materials/01-introduction/Starter
folder, which you can download from the materials repo of this module. Next, open lesson1.ipynb
.
Securing Your Key
If you’re working with version control like Git, you want to avoid adding your API keys to the repository. This is because other people will be able to see them. The same rules apply when using a public repository like GitHub. If someone used your key, your account could be billed and you don’t want that. Just look at this article about searching for other people’s OpenAI API Keys in GitHub.
You might be wondering how you then use the keys without storing them in your repository. The way to do this is to provide the key from outside the repository when you run your notebook. You can do it in several ways, but in this tutorial you’ll use an environment variable.
Go ahead and switch to your browser. Hopefully, the lesson1.ipynb
is still open in JupyterLab. Add the following code:
# 1
import os
# 2
KEY = os.environ["OPENAI_API_KEY"]
# 3
print(KEY)
Here’s what you did.
- You imported the
os
module, which contains the access to environment variables. - You assigned the environment variable called
OPENAI_API_KEY
to the variableKEY
. - You print the variable
KEY
.
Try running the notebook by pressing Shift+Enter or Run -> Run Selected Cell in the menu bar. You’ll notice that you encountered an error like below.
This happens if you don’t define the environment variable before running it. Now, stop JupyterLab by going back to the Terminal where you started it and use your platform-specific command to perform the stop, e.g. in MacOS, it’s Control-C or just close that Terminal. Now, you want to start JupyterLab with the environment variable.
In your Terminal, switch using cd to the directory of lesson1.ipynb
. Then, type OPENAI_API_KEY=<your key> jupyter lab
. Remember to use your own API Key here, and press Enter. This should launch JupyterLab in the current directory. Open lesson1.ipynb
and run the cell again. You should be able to see your key like below.
Congratulations! Now, you have one secure way to use your API Key.
Billing and Usage
In this lesson, you’re reminded to secure your API key multiple times. To further understand the financial impact of someone stealing or borrowing your API key, you need to understand how usage and billing works.
Go to Usage. You’ll see a screen like below.
This is a real example of a project that uses the API. You can see that it can get expensive.
In order to use the API, you also need to set up Billing since you can’t use it for free. To do so, go to Billing and click Payment Method. You should add one if you didn’t get any free credits or if you think you need more than the free credits you got when signing up.
Now you have a pretty good understanding of what using the API looks like on your wallet. :] To further understand the billing, you should learn about the available models and what tokens are.
Pricing
Now, to understand how the calculation for the bill is done, you should understand that OpenAI bills on usage. What does that mean? Go to Pricing to find out.
You’ll see the pricing for GPT-4o and GPT-4o-mini, or whatever models are in production at the time you’re learning this, on this page.
You’ll also see that the pricing is stated like this, $5.00 / 1M input tokens
. Usage means that OpenAI measures “something” and charges you depending on how much of that “something” you use. In this case, that “something” is called a token.
Understanding Tokens
On their website, they state that tokens are like pieces of words, for example, 1,000 tokens is about 750 words. One of the easiest ways to understand how tokens are formed is to go to OpenAI’s tokenizer and enter a phrase.
Look at the image above, it colors each token differently so you can see them. Notice how tokens can vary from 1 character like 3 or -, to two like ‘s or er, to even more characters like important or different. The scope of this lesson won’t go so deep into tokenization, the process of converting inputs like text into tokens etc. Fortunately, there are multiple resources available online if you’re interested to find out more about it.
Available Models
Back to understanding pricing, OpenAI provides different models, and each is priced differently.
Go to the Models Overview in OpenAI.
You’ll see the list of models available like above. For text based models, the most important details are summarized below.
Context length means how many tokens it can support for the input. Notice also that the price for both input and output tokens is different. It means that you also get billed for both how much input you send to the API and how much output the model produces.
OpenAI also has non-text models like below.
There’s more. OpenAI also provides fine-tuning for their models, which means you can provide training data for GPT-4o or other text-based models and generate a fine-tuned model based on your data. The pricing for these is different, too, but more expensive than the base models that are used when training. Embedding models are also provided, which means you can convert your data into embeddings, but you’ll not go deep into that in this module.
That was a lot of information to take in, well done!