Welcome back!
In this video, you'll learn to make your very first request to the OpenAI API!
Recall that we can request the use of a model from OpenAI by making a request to their API.
Depending on the model or services required, APIs have different access points for users.
These access points are called endpoints.
Endpoints are a lot like doors in a hospital.
Depending on the treatment required, patients use different doors to reach different departments.
API endpoints, like many hospital departments, may also require authentication before accessing services.
API authentication is usually in the form of providing a unique key containing a random assortment of characters.
It's important to note that many APIs, including the OpenAI API, have costs associated with using their services.
For OpenAI, these costs are dependent on the model requested and on the size of the model input and output.
A crucial part of working with APIs is navigating API documentation, which provides
details on which endpoints to use, their functionality, and how to set up authentication.
Throughout the course, we'll provide
these API details, but we also recommend checking out OpenAI's excellent API documentation.
Usage of OpenAI's API requires authentication, so to use the API and
complete the exercises in this course, you'll need to set up an account.
OpenAI often provides free trial credit to new users, which will be more than
sufficient for completing this course; however, for some countries, you may need to add credit.
Then, for authentication, you'll need to create a secret key and copy it.
DataCamp doesn't store any API keys used in this course, so you can copy it directly into the exercises.
Let's make our first API request!
There are several ways to interact with an API, but in this
course, we'll use OpenAI's own Python library, due to its friendly syntax.
We assign our API key to openai-dot-api_key, which is used to authenticate our request.
Now for the request code.
We create a request to the Completions endpoint, which is used for
completing a text prompt, by calling the create method on openai-dot-Completion.
Inside this method, we specify the model and the prompt to send to it.
We'll discuss prompts in greater detail later in the course.
Let's take a look at the API response.
The API returns a JSON response.
JSON stands for JavaScript Object Notation; it's a nested data
format that resembles a Python dictionary with keys and values.
The response has several keys: "choices" at the top, and "created", "id", "model", and others at the bottom.
We can see that the text response to our prompt is nested inside the
"choices" key; let's start to unnest this object by selecting the value from that key.
Recall that we can extract the value from a dictionary key using square brackets.
This returns another nested object, but we've gotten much closer to the text.
Notice from the square brackets at the beginning and end, that this is actually a list with a single element.
Let's extract the first element to dig further.
Ok - now we're left with a standard dictionary, and our text response is located underneath the "text" key.
Let's use dictionary subsetting once more.
There we have it - our text response as a string!
We started off with a complex object, but by applying our Python
programming fundamentals, we were able to get there one step at a time.
Time to practice making your own requests!