A lot of generative AI tutorials assume you have a basic understanding of common development tools or practices, but if you’re me, you really need a guide that starts from the very beginning of “I have nothing, I know nothing, I don’t know where the hell this code snippet should go”. I’m going to walk you through my exact setup for how I went from “my computer is turned on” to “I can send prompts to an LLM”.
In this tutorial, we will:
- Get our tools and software all set up
- Create an OpenAI API key
- Use the API
- Troubleshooting
Why Use the OpenAI API Instead of ChatGPT?
My sign is Taurus, which means I’m stubborn, I like money, and I’m deeply suspicious of everything (that last one may have nothing to do with the stars). Here are some advantages of using the API:
- Cost-effective: Instead of paying $20 or however a month for ChatGPT’s paid plan, you can buy credits and manage your own usage in a pay-as-you-go style system.
- Privacy: When using the API, OpenAI doesn’t train on your data unless you explicitly opt-in to let them. ChatGPT also lets you opt-out of training on your data, but if the product is free, then YOU are the product.
- Flexibility: You can choose which model(s) you want for a task, whether it’s easy or complex. You can adjust a variety of parameters with greater control than the ChatGPT interface.
- Hands-on AI experience: There’s something great about using the API directly and getting into the “nitty gritty” as a non-technical person, right?
Tools
We are not doing this old school in a terminal – let’s use some tools to make this a lot easier. Here’s my personal setup:
- Computer: Laptop with Windows 11
- Programming Language: Python
- Very popular language and super common in the generative AI world. I’m currently auditing Meta’s Python course on Coursera.
- Don’t know any Python? Totally fine, we’re just using the API and not doing anything crazy yet.
- IDE: VS Code – set up for Python development
- I grew up flipping through MSDN CD subscription binders to find the Visual Studio installation CD for my dad’s office computers, so VS Code is familiar and lightweight.
- What’s an IDE? Integrated Development Environment. It gives you a lot of tools and features that will make your software development foray easier.
- Notebook: Jupyter and a regular physical journal
- Jupyter Notebooks let you test out code and write notes, so they’re very useful for playing around with the OpenAI API. You can open a Jupyter Notebook inside of VS Code.
- I really enjoy physically writing with a fountain pen on paper, so I still take handwritten notes as I work through my thoughts.
- Pen: TWSBI Eco
- Ink: Iroshizuku – variety of colors
Setting Up Your IDE (Python)
VS Code’s Quick Start Guide covers a lot of these steps, but I’ll give you the bare minimum to start using the OpenAI API. You can use a different IDE, but this is what I did.
- Install the latest version of Python.
- Install VS Code.
- Open VS Code. On the left panel, click Extensions (block icon).
- Search for and install the Python extension.
- Search for and install the Jupyter extension.
- In the Terminal at the bottom of the IDE, type:
- pip install openai
- Press the Enter key. The OpenAI Python library is now installed.
- When I first used the “pip” command, I was so confused. How did it know to just go grab the correct library and install it? I don’t fully understand it, but it’s like you ask your computer to grab a book (openai) from the library (pip) to read (install).
You may not know how to use any of this stuff or what it means, but now we are ready to use the OpenAI API! Kind of.
Create an OpenAI API Key
Minimize VS Code and open https://platform.openai.com/ in your preferred browser. You can either use your existing ChatGPT credentials to log in or create a new account. OpenAI may ask you some questions to create an organization, but you’ll eventually find yourself at the OpenAI Platform Dashboard.
To create an API key:
- On the Dashboard page, click API Keys.
- Click Create a new secret key with the default options.
- You can name your key whatever you want!
- Click Create secret key, and a pop-up appears with your new key.
- This is the only time OpenAI will show you the full key, so copy it and paste it somewhere safe (like a password database).
- Click Done.
Yay! Now you have a key, but as my mom says, “No money, no honey.” In this case, “No money, no promptin’.” You can delete and create keys whenever, but remember never share your key publicly because you’re the one paying for the usage.
Note: You may have some free credits on your account. I had some, but they had long expired. I don’t know how you get free credits.
To buy credits:
- Go to the Settings page of your organization.
- Click Billing. On the Billing page, you can add a payment method and buy some sweet, sweet credits.
When I added my payment method, I turned off auto recharge because this is for my personal use and testing. If I exceed the $10 that I’ve spent, I can always manually buy more credits. If you’re building an app that needs to be on and available, you may want to auto recharge.
Keeping Your Key Private: Environment Variables
If you create a program or are using your OpenAI API key during a demonstration, how do you keep others from seeing your key? We’re going to use an environment variable! An environment variable is a value set outside of the program. It can reduce the need to modify or re-release code when a configuration changes.
For example, if you reference your API key multiple times, you would only need to change the environment variable rather than tracking down every instance in your code.
To set up an environment variable in Windows 11:
- Enter “environment variable” in the taskbar Search field.
- Click Edit the system environment variables. The System Properties pop-up appears.
- Click Environment Variables.
- In the Environment Variables pop-up, click New under either User variables or System variables.
- I chose to put my key under User variables, just in case anyone else ends up using my machine and happens to be working with the OpenAI API.
- Enter the following:
- Variable name: OPENAI_API_KEY
- Variable value: [copy and paste your key that you saved from OpenAI]
- Click Ok. OPEN_API_KEY now appears as an environment variable.
Now, you don’t have to copy and paste your API key everywhere – you can instead just call for your environment variable of OPEN_API_KEY! Okay, but now how do we actually do that?
Use Your OpenAI API Key
Back to VS Code! We’re going to create a new Jupyter Notebook and plop in some code.
To create a new Jupyter Notebook:
- Click File, then New File.
- Click or search for Jupyter Notebook. A new .ipynb file opens in VS Code.
By default, a code cell appears first. You can hover above or below the cell to add a Code or Markdown cell. But a code cell is exactly what we need! There are dozens, if not hundreds, of code samples you can copy and paste to get started. OpenAI’s official documentation gives you so many options for various scenarios, so that’s a great starting point.
For our uses today, copy and paste this into your Jupyter Notebook code cell:
import os
from openai import OpenAI
client = OpenAI(
api_key=os.environ.get("OPENAI_API_KEY"),
)
response = client.responses.create(
model="gpt-3.5-turbo",
input="Write one sentence about prompt engineering.",
)
print(response.output_text)
Click Run All at the top and you should get a single sentence response in the Notebook! From here, you can now start experimenting with your own prompts, use different models, and expand beyond just text.
It’s Not Working
Let’s troubleshoot a couple scenarios that I ran into.
- My program can’t find my API key
- Double-check that your environment variable is using the correct key value.
- Double-check that you’re referencing the correct environment variable name (“OPENAI_API_KEY” versus “OPENAIAPIKEY”).
- I’m trying to use a different model, but the error says I’m not verified.
- OpenAI only allows verified organizations to use their newer models. You can go through the verification process through the OpenAI Platform Settings page.
- You will have to submit photos of a valid ID and verify your identity through a facial recognition system (this means having a weird selfie video moment). If this gives you the ick, then you’ll be limited on what models you can use.
- Literally nothing is working.
- Go through the Tools and setup section again to make sure you have Python, Jupyter, and the OpenAI library installed.
Leave a Reply