WEBVTT

1
00:00:00.000 --> 00:00:02.560
Welcome back!

2
00:00:02.560 --> 00:00:08.840
In this video, you'll learn to make your very first request to the OpenAI API!

3
00:00:08.840 --> 00:00:08.880


4
00:00:08.880 --> 00:00:08.880


5
00:00:08.880 --> 00:00:15.760
Recall that we can request the use of a model from OpenAI by making a request to their API.

6
00:00:15.760 --> 00:00:15.760


7
00:00:15.760 --> 00:00:15.760


8
00:00:15.760 --> 00:00:21.600
Depending on the model or services required, APIs have different access points for users.

9
00:00:21.600 --> 00:00:21.600


10
00:00:21.600 --> 00:00:22.320


11
00:00:22.320 --> 00:00:24.880
These access points are called endpoints.

12
00:00:24.880 --> 00:00:25.760


13
00:00:25.760 --> 00:00:25.760


14
00:00:25.760 --> 00:00:29.120
Endpoints are a lot like doors in a hospital.

15
00:00:29.120 --> 00:00:34.720
Depending on the treatment required, patients use different doors to reach different departments.

16
00:00:34.720 --> 00:00:34.720


17
00:00:34.720 --> 00:00:35.680


18
00:00:35.680 --> 00:00:43.800
API endpoints, like many hospital departments, may also require authentication before accessing services.

19
00:00:43.800 --> 00:00:43.800


20
00:00:43.800 --> 00:00:43.840


21
00:00:43.840 --> 00:00:50.920
API authentication is usually in the form of providing a unique key containing a random assortment of characters.

22
00:00:50.920 --> 00:00:50.920


23
00:00:50.920 --> 00:00:51.920


24
00:00:51.920 --> 00:01:00.000
It's important to note that many APIs, including the OpenAI API, have costs associated with using their services.

25
00:01:00.000 --> 00:01:00.000


26
00:01:00.000 --> 00:01:00.840


27
00:01:00.840 --> 00:01:07.720
For OpenAI, these costs are dependent on the model requested and on the size of the model input and output.

28
00:01:07.720 --> 00:01:09.000


29
00:01:09.000 --> 00:01:09.000


30
00:01:09.000 --> 00:01:15.160
A crucial part of working with APIs is navigating API documentation, which provides

31
00:01:15.160 --> 00:01:22.240
details on which endpoints to use, their functionality, and how to set up authentication.

32
00:01:22.240 --> 00:01:22.240


33
00:01:22.240 --> 00:01:22.240


34
00:01:22.240 --> 00:01:24.080
Throughout the course, we'll provide

35
00:01:24.080 --> 00:01:30.440
these API details, but we also recommend checking out OpenAI's excellent API documentation.

36
00:01:30.440 --> 00:01:30.440


37
00:01:30.440 --> 00:01:31.560


38
00:01:31.560 --> 00:01:37.320
Usage of OpenAI's API requires authentication, so to use the API and

39
00:01:37.320 --> 00:01:42.880
complete the exercises in this course, you'll need to set up an account.

40
00:01:42.880 --> 00:01:42.880


41
00:01:42.880 --> 00:01:42.880


42
00:01:42.880 --> 00:01:47.800
OpenAI often provides free trial credit to new users, which will be more than

43
00:01:47.800 --> 00:01:53.240
sufficient for completing this course; however, for some countries, you may need to add credit.

44
00:01:53.240 --> 00:01:53.240


45
00:01:53.240 --> 00:01:53.240


46
00:01:53.240 --> 00:01:58.920
Then, for authentication, you'll need to create a secret key and copy it.

47
00:01:58.920 --> 00:02:06.920
DataCamp doesn't store any API keys used in this course, so you can copy it directly into the exercises.

48
00:02:06.920 --> 00:02:07.720


49
00:02:07.720 --> 00:02:07.720


50
00:02:07.720 --> 00:02:10.040
Let's make our first API request!

51
00:02:10.040 --> 00:02:10.040


52
00:02:10.040 --> 00:02:10.040


53
00:02:10.040 --> 00:02:14.560
There are several ways to interact with an API, but in this

54
00:02:14.560 --> 00:02:20.080
course, we'll use OpenAI's own Python library, due to its friendly syntax.

55
00:02:20.080 --> 00:02:21.000


56
00:02:21.000 --> 00:02:21.000


57
00:02:21.000 --> 00:02:27.600
We assign our API key to openai-dot-api_key, which is used to authenticate our request.

58
00:02:27.600 --> 00:02:27.600


59
00:02:27.600 --> 00:02:27.600


60
00:02:27.600 --> 00:02:30.480
Now for the request code.

61
00:02:30.480 --> 00:02:34.320
We create a request to the Completions endpoint, which is used for

62
00:02:34.320 --> 00:02:40.080
completing a text prompt, by calling the create method on openai-dot-Completion.

63
00:02:40.080 --> 00:02:41.160


64
00:02:41.160 --> 00:02:41.160


65
00:02:41.160 --> 00:02:45.440
Inside this method, we specify the model and the prompt to send to it.

66
00:02:45.440 --> 00:02:50.200
We'll discuss prompts in greater detail later in the course.

67
00:02:50.200 --> 00:02:50.200


68
00:02:50.200 --> 00:02:50.200


69
00:02:50.200 --> 00:02:52.480
Let's take a look at the API response.

70
00:02:52.480 --> 00:02:52.480


71
00:02:52.480 --> 00:02:53.400


72
00:02:53.400 --> 00:02:56.200
The API returns a JSON response.

73
00:02:56.200 --> 00:03:01.960
JSON stands for JavaScript Object Notation; it's a nested data

74
00:03:01.960 --> 00:03:05.960
format that resembles a Python dictionary with keys and values.

75
00:03:05.960 --> 00:03:13.880
The response has several keys: "choices" at the top, and "created", "id", "model", and others at the bottom.

76
00:03:13.880 --> 00:03:13.880


77
00:03:13.880 --> 00:03:13.880


78
00:03:13.880 --> 00:03:18.760
We can see that the text response to our prompt is nested inside the

79
00:03:18.760 --> 00:03:24.800
"choices" key; let's start to unnest this object by selecting the value from that key.

80
00:03:24.800 --> 00:03:24.800


81
00:03:24.800 --> 00:03:24.800


82
00:03:24.800 --> 00:03:30.400
Recall that we can extract the value from a dictionary key using square brackets.

83
00:03:30.400 --> 00:03:30.400


84
00:03:30.400 --> 00:03:30.400


85
00:03:30.400 --> 00:03:35.960
This returns another nested object, but we've gotten much closer to the text.

86
00:03:35.960 --> 00:03:42.560
Notice from the square brackets at the beginning and end, that this is actually a list with a single element.

87
00:03:42.560 --> 00:03:42.560


88
00:03:42.560 --> 00:03:42.560


89
00:03:42.560 --> 00:03:46.520
Let's extract the first element to dig further.

90
00:03:46.520 --> 00:03:46.520


91
00:03:46.520 --> 00:03:46.520


92
00:03:46.520 --> 00:03:54.400
Ok - now we're left with a standard dictionary, and our text response is located underneath the "text" key.

93
00:03:54.400 --> 00:03:54.400


94
00:03:54.400 --> 00:03:54.400


95
00:03:54.400 --> 00:03:57.680
Let's use dictionary subsetting once more.

96
00:03:57.680 --> 00:03:57.680


97
00:03:57.680 --> 00:03:57.680


98
00:03:57.680 --> 00:04:01.680
There we have it - our text response as a string!

99
00:04:01.680 --> 00:04:06.560
We started off with a complex object, but by applying our Python

100
00:04:06.560 --> 00:04:10.720
programming fundamentals, we were able to get there one step at a time.

101
00:04:10.720 --> 00:04:10.720


102
00:04:10.720 --> 00:04:10.720


103
00:04:10.720 --> 00:04:16.120
Time to practice making your own requests!

