Hi and welcome to this mini-course on AI engineering! In this mini-course you will learn the basic principles of AI engineering by using ChatGPT to automatically write blog articles on any news topic.
I will show you some cool tricks along the way, but rest assured, you don’t need to be an expert in coding or anything else to follow along. This is a beginner-friendly tutorial and we will have ample explanation for everything we do along the way. So, let’s get started!
What we’ll need
To follow along with this tutorial, you’ll need a code editor. I recommend using Visual Studio Code, but you can use any code editor you like. If you don’t have a code editor installed, you can download Visual Studio Code from here.
You’ll also need to have Python installed on your computer. If you don’t have Python installed, you can download it from here. If you’re asked whether you want to add Python to your PATH, make sure to check yes.
The last thing we’ll need is a ChatGPT account. 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!
Now that we have our API key, we can start on our automatic blog writing journey!
Project setup
So let’s take a brief test-run of how ChatGPT works when using the API with Python. After this, it will be a lot easier to create our automated blog writing script. First of all, create a folder for your project. I’ll name mine Intro_to_AI_engineering:
π Intro_to_AI_engineering
Now open the folder in VSCode. You can do this by right-clicking the folder and selecting Open with Code. If you don’t see this option, you can open VSCode and then open the folder from there.
The first thing we’ll need is to save our API key in this 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 folder by right-clicking the explorer/browser sidebar and selecting New File. Name it .env:
π Intro_to_AI_engineering
π .env
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
Making our first ChatGPT request
Now that we have everything set up and ready to go, let’s create a new Python file in the folder by right-clicking the folder in the sidebar and selecting New File. Name the file first_request.py:
π Intro_to_AI_engineering
π .env
π first_request.py
Open up this file and let’s make a request. The first thing we’ll put in this file is our imports:
import os from openai import OpenAI from dotenv import load_dotenv
The os (operating-system) library will allow us to read the API key from the .env file, used in combination with the load_dotenv import. The OpenAI import is the library we installed earlier that will allow us to interact with the ChatGPT API.
Now we’ll load the API key and set up the OpenAI object. Add this below the imports:
load_dotenv()
CLIENT = OpenAI(api_key=os.getenv('OPENAI_API_KEY'))
Calling the load_dotenv() function will read the .env file with our API key and make it so the os library now has access to this key. We then create a CLIENT object using the OpenAI class we imported and pass in an argument api_key with the value of our API key.
The os.getenv('OPENAI_API_KEY') function will read the environment variable OPENAI_API_KEY
