Welcome to part two of this tutorial series. Now that we have our meme data all set up and ready to go, it’s time to look at integrating ChatGPT into our project and generating the text for our memes.
Getting a ChatGPT API Key
The first thing we’ll need is a ChatGPT account. If you already have an account and API key, skip ahead past this next portion. If this is your first time using it, don’t worry, you will get a bunch of free credits after signing up for your first account so you should be able to follow along without having to pay any money.
Go to https://platform.openai.com/ and log in. If you already use OpenAI and already have an account set up with a free or paid API key, you can use that. If you don’t have an account just log in with your Google account. It will ask you something simple, like to fill in your birthday, and ta-da, you have an account!
When you log in on a brand new account you will see something like this (navigate to the dashboard if you land on a different page):

Find API keys in the left sidebar and click on it. If this is a new account it will ask you to verify your phone in order to create a new API key:

The reason they do this is to prevent bots from creating loads of free accounts and abusing their system. Just give them a phone number and they will send you a verification code to enter. You will also get a bunch of free credits from them to follow along with this tutorial, so it’s a win-win!
Find the green button to + Create new secret key in the top right corner and click on it:

In the next window, you can leave everything as is. You don’t need to give it a name or select a project. You can do these things if you want to, but I’ll just create a nameless general key for now by accepting everything as is and clicking the green Create secret key button:

You will now see your new API key:

So make sure you press the Copy button and save it somewhere safe, maybe a password manager. You won’t be able to see this key again, though you can always generate a new one if you lose it. Make sure not to share your key as anyone with your key can use your credits!
Adding the ChatGPT to our project
We’ll need to save our API key in our project. We don’t want to hardcode it into our scripts, as that would be a security risk. Instead, we’ll save it in a separate file and read it from there. This way, we can also easily share our code without sharing our API key.
Create a new file in the root folder and name it .env:
π Meme_Gen
π fonts
π ARIAL.TTF
π ComicSansMS3.ttf
π impact.ttf
π output
π templates
πΌ (all the images in here...)
π .env --> create this file
π load_meme_data.py
π meme_data.json
The file has no name but only the extension .env and will store our API key. Open the file and paste your API key in there:
OPENAI_API_KEY=sk-loadsoflettersandnumbers
Make sure to replace sk-loadsoflettersandnumbers with your actual API key. Save and close this file. If you ever share your code, make sure to exclude this file from the shared code.
The next thing we’ll need to do is install two libraries for Python. Open a terminal in VSCode by clicking Terminal in the top menu and selecting New Terminal, or using the shortcut [Ctrl + `]. In the terminal, type the following command and press Enter:
pip install openai --upgrade
This will either install or update (if you already have it) the OpenAI Python library. This library will allow us to easily interact with the ChatGPT API.
The next library we need is called python-dotenv. This library will allow us to read the API key from the .env file we created. Install it by typing the following command in the terminal and pressing Enter:
pip install python-dotenv
Setting up the ChatGPT logic
Time to rock ‘n roll! Let’s create a new Python script called get_meme.py in our project folder:
π Meme_Gen
π fonts
π ARIAL.TTF
π ComicSansMS3.ttf
π impact.ttf
π output
π templates
πΌ (all the images in here...)
π .env
π get_meme.py --> create this file
π load_meme_data.py
π meme_data.json
The meme generation logic will not be too hard but will involve several steps, so let’s build a simplified version first that just calls ChatGPT, and then we can keep adding stuff on top of it until we have what we need.
Inside of get_meme.py, let’s start by importing the necessary libraries and loading our API key:
import os from dotenv import load_dotenv from openai import OpenAI load_dotenv()
The openai library will allow us to interact with the ChatGPT API easily, and dotenv will allow us to read our API key from the .env file. Calling the load_dotenv() function will read the .env file and store the API keys contained inside it as environment variables. The os is imported as it has the ability to read these environment variables.
Let’s continue:
CLIENT = OpenAI(api_key=os.getenv("OPENAI_API_KEY"))
SYSTEM_MESSAGE = {
"role": "system",
"content": [
{
"type": "text",
"text": "You are a cat. Reply to whatever the user says in whatever manner you see fit, but remember to be a cat.",
}
],
}
Here, we create a CLIENT object that will allow us to interact with the ChatGPT API. We pass in the api_key parameter by using the os library’s .getenv method to read the environment variable (which the load_env() function loaded from the .env file).
We also create a SYSTEM_MESSAGE object that will be used to set the initial context for the conversation with ChatGPT. The object structure is a bit odd, but looking past that it basically just says that this message is
