Create your own AI app with GPT-4
Build custom apps with the GPT-4 AI model. Learn how to create an Open AI API key and then call the API from within a python application.
GPT-4 Beginner Series:
- Start using GPT-4, the world’s most intelligent publicly available AI
- → Create your own AI app with GPT-4
Contents
- Prerequisites
- Github Repo
- Create an Open AI API key
- Add your API key to an environment variable
- Use your API key in a python app
- Pricing and Usage
- What's next?
Prerequisites
- Basic knowledge of the terminal / command prompt (how to set environment variables on your system and run programs)
- To complete this tutorial, you must have python installed: https://www.python.org/downloads/
- You will need a text editor to write code. I use VSCode: https://code.visualstudio.com/download
Github Repo
All code for this tutorial is available on Github: https://github.com/ATLucas/aidev/tree/main/tutorials/openai_api_basics
Create an Open AI API key
- Navigate in a browser to https://openai.com/
- Click Log in
- Select API
- Select API Keys from the left side menu
- Click Create new secret key
- Give your key a name and click Create secret key
- Copy your key somewhere safe and click Done
Add your API key to an environment variable
- Open a terminal
- Open/create your .bash_profile:
$ nano ~/.bash_profile
- Add this line to the file:
export OPENAI_API_KEY=[ENTER YOUR KEY HERE]
- Type CTRL+X to exit the nano editor and save the file by typing ENTER
- Source your .bash_profile with the new env var added:
$ source ~/.bash_profile
- Verify the env var is set:
$ echo $OPENAI_API_KEY
[YOUR API KEY]
Use your API key in a python app
- Install the openai python module
$ python3 -m pip install openai
- Clone the repo containing the tutorial code
$ git clone git@github.com:ATLucas/aidev.git
- Change to the tutorial directory
$ cd aidev/tutorials/openai_api_basics
- Run the command line app
$ python3 main.py
- (Had to use GPT-3-turbo for this example due to Open AI outages after dev day)
- The code for communicating with ChatGPT in this tutorial was based on the Open AI API python quickstart: https://platform.openai.com/docs/quickstart?context=python
Pricing and Usage
- While the cost to run this app is small, know that you will be charged for usage according to the API pricing guide (likely just pennies for a short conversation). GPT-4 costs quite a bit more than GPT-3-turbo. It can be 10 to 30 times as much, depending on the model you choose.
- Pricing guide: https://openai.com/pricing
- Check your API usage: https://platform.openai.com/usage
What’s next?
- Try-out different models by adjusting the MODELS variable at the top of the python script. Model names can be found here: https://platform.openai.com/docs/models/continuous-model-upgrades
- Note that as of the writing of this article, the GPT-4 models were temporarily removed, likely due to the Open AI servers going down in the aftermath of dev day.
- Try modifying the SYSTEM_PROMPT variable and see how it impacts your conversation. For example, tell GPT that it should act like your favorite movie character.
- In the next article, we’ll take a look at the new assistants API for creating GPT-4-turbo agents…assuming Open AI can get GPT-4 back up-and-running sometime soon…
UPDATE
Turns-out I just didn’t have payments setup properly in the Open AI account I was using, and that’s why I didn’t have access to the GPT-4 models anymore.
In any case, go to Settings in the left side panel, click to drop-down and select Billing. Then add a payment method and fund your account. The GPT-4 model will be immediately available, both in the Open AI platform playground and via the API itself. If you have a Plus account, you should also be able to use the GPT-4 turbo model preview.