OpenAI GPT-4 Requests with Python

Let's look at how to make a request to GPT-4 using Python in a Jupyter notebook.

To follow along, you'll need an API key from OpenAI. If you don't have one, visit platform.openai.com to obtain one.

In the Jupyter Notebook, the first step is to install the OpenAI package:


pip install openai

Next, add a cell and import the openai package. It's recommended to use an environment variable to store your API key.


import openai

In this example, we'll use the dotenv package to load the API key from an .env file:


# second cell
from dotenv import load_dotenv
import os
# third cell
load_dotenv()
openai.api_key = os.getenv('OPENAI_API_KEY')

Making a Request

Here's the basic syntax for making a request with the OpenAI package:


openai.ChatCompletion.create(
model='gpt-3.5-turbo', # or 'gpt-4'
messages=[
{"role": "user", "content": "tell me a joke"}
] # A list of messages
)

The messages parameter is a list of dictionaries, even if there is only a single message. Each movie must contain a role and content

The role can be one of the following:

  • ’system’: Sets an overall system directive.
  • ’user’: Represents the user (or developer) asking a question or making a request.
  • ’assistant’: Shows the model the desired response format.

Now, let's add a new notebook cell and create a request to ask GPT-4 about OpenAI:


res = openai.ChatCompletion.create(
model='gpt-4',
messages=[
{
'role': 'user',
'content': 'Tell me about OpenAI'
}
]
)

Accessing the Response

The response object contains a lot of information:

code screen

What we're interested in is the assistant's reply, which is nested inside of choices which is a list.

To access the content of the response message, use the following code:


res['choices'][0]['message']['content']

This will print the assistant's response, providing the information about OpenAI that we asked the API for.

Transcript

00:00 In this video, I'll show you how easy it is to make a request to GPT-4 using Python. So I'm working in a notebook, and the first thing we have to do is install the OpenAI package. So pip install OpenAI. I've already done this. It will take a moment for you, most likely.

00:16 And then we have to import OpenAI and provide our API key. So if you haven't gotten an API key yet, you'll need to go to platform.openai.com to get your own API key.

00:28 Once you have it, all we do is on the OpenAI package, we set OpenAI.APIKey equal to whatever your API key is. Although it's a much better idea to use an environment variable, which is what I'm going to do.

00:43 There we go. I'm using a package called.env. It's specifically a function load.env. I have a file called.env with my environment variable in it, and then it loads it. I get access to it, and I set OpenAI.APIKey equal to my API key. Okay, so now it's time to make a request.

01:02 This is the basic syntax for making a request with the OpenAI package. So the first thing you'll notice is that we call OpenAI.chatCompletion.create. We specify a model. If you don't have access to GPT-4, GPT-3.5 Turbo is your best bet. And then we have to specify a list of messages.

01:20 So this list of messages follows a particular format. I'll talk more about it in a moment. Let me just show you an example. Let's do an OpenAI.chatCompletion.create. I'll set my model here to be GPT-4 because I have access to it for now.

01:37 And then I provide a list of messages, even if it's just a single message. Each message has to follow this format. It must be an object or in Python a dictionary that has a role, which can be set to system, user, or assistant. You can use the system role to set an overall system directive.

01:55 The user role is what you'll most commonly use anytime you're asking or requesting anything from the model. And we can also use role set to assistant if we're trying to show the model what we want it to give us back. And then most importantly, we have to provide content, which is the content of the message.

02:13 So let's start with a very simple, single little message. Role will be user. Again, that refers to me or the developer, the user here talking to the assistant. And then our content, let's ask the model to tell us about OpenAI. All right. And I'm going to save this response into a variable. I'll call it res, and I'll run that cell.

02:32 When it finishes, here's our response variable. You'll notice there's quite a bit of information in here, but the actual content we want is nested inside of choices, which is a list. We want the first choice, and then we want message, and then finally, we want the content of that message.

02:50 Notice this message has a role set to assistant. It's the assistant's response. And finally, I'll just print this, and we'll see our nice response we got back, a little bit of information about OpenAI.

More Tips