WEBVTT

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

2
00:00:02.640 --> 00:00:06.480
In this video, we'll learn to unlock the chat capabilities from

3
00:00:06.480 --> 00:00:12.880
the OpenAI API, which underpin popular applications like ChatGPT.

4
00:00:12.880 --> 00:00:14.960
Let's get started!

5
00:00:14.960 --> 00:00:14.960


6
00:00:14.960 --> 00:00:14.960


7
00:00:14.960 --> 00:00:19.160
The Chat Completion endpoint allows us to have multi-turn conversations with a

8
00:00:19.160 --> 00:00:25.440
model, so we can build on our previous prompts depending on how the model responds.

9
00:00:25.440 --> 00:00:31.960
Additionally, chat models often perform just as well as Completions models on single-turn tasks.

10
00:00:31.960 --> 00:00:31.960


11
00:00:31.960 --> 00:00:31.960


12
00:00:31.960 --> 00:00:37.080
Compared to the Completions endpoint, Chat Completion allows for better

13
00:00:37.080 --> 00:00:43.520
customizability of the response through the use of roles, which we'll discuss in a moment.

14
00:00:43.520 --> 00:00:43.520


15
00:00:43.520 --> 00:00:43.520


16
00:00:43.520 --> 00:00:49.520
Finally, there's currently a substantial cost benefit to using chat models over completions.

17
00:00:49.520 --> 00:00:50.640


18
00:00:50.640 --> 00:00:50.640


19
00:00:50.640 --> 00:00:55.560
The cost benefit and flexibility of being able to have multi-turn conversations, means

20
00:00:55.560 --> 00:01:02.000
that developers quite often choose a chat model when building applications on the OpenAI API.

21
00:01:02.000 --> 00:01:03.200


22
00:01:03.200 --> 00:01:03.200


23
00:01:03.200 --> 00:01:06.160
Roles are at the heart of how chat models function.

24
00:01:06.160 --> 00:01:07.000


25
00:01:07.000 --> 00:01:07.000


26
00:01:07.000 --> 00:01:12.360
There are three roles: the system, the user, and the assistant.

27
00:01:12.360 --> 00:01:12.360


28
00:01:12.360 --> 00:01:12.360


29
00:01:12.360 --> 00:01:19.320
The system role allows the user to specify a message to control the behavior of the assistant.

30
00:01:19.320 --> 00:01:24.160
For example, for a customer service chatbot, we could provide a system

31
00:01:24.160 --> 00:01:29.440
message stating that the assistant is a polite and helpful customer service assistant.

32
00:01:29.440 --> 00:01:29.440


33
00:01:29.440 --> 00:01:29.440


34
00:01:29.440 --> 00:01:36.600
The user provides an instruction to the assistant, and the assistant responds.

35
00:01:36.600 --> 00:01:36.600


36
00:01:36.600 --> 00:01:36.600


37
00:01:36.600 --> 00:01:43.360
One of the interesting things about chat models, is that the user can also provide assistant messages.

38
00:01:43.360 --> 00:01:49.880
These are often utilized to provide examples to help the model better understand the user's desired response.

39
00:01:49.880 --> 00:01:49.880


40
00:01:49.880 --> 00:01:51.000


41
00:01:51.000 --> 00:01:55.040
We'll discuss multi-turn conversations in the next video; for

42
00:01:55.040 --> 00:01:59.440
now, we'll get familiar with using chat models for single-turn tasks.

43
00:01:59.440 --> 00:01:59.440


44
00:01:59.440 --> 00:02:00.520


45
00:02:00.520 --> 00:02:06.640
Making a request to the ChatCompletion endpoint is very similar to the Completions endpoint.

46
00:02:06.640 --> 00:02:08.600
Instead of calling the create method on the

47
00:02:08.600 --> 00:02:16.120
Completion class, we call it on ChatCompletion class; there's also different models for these two endpoints.

48
00:02:16.120 --> 00:02:16.120


49
00:02:16.120 --> 00:02:17.240


50
00:02:17.240 --> 00:02:20.240
The main difference is in the way that prompts are provided.

51
00:02:20.240 --> 00:02:26.720
For the Completions endpoint, the prompt is passed as a string for the model to complete.

52
00:02:26.720 --> 00:02:33.760
Due to the greater customizability of chat models through the use of roles, the prompt is provided in a different way.

53
00:02:33.760 --> 00:02:33.760


54
00:02:33.760 --> 00:02:34.880


55
00:02:34.880 --> 00:02:42.640
The prompt is set up by creating a list of dictionaries, where each dictionary provides content to one of the roles.

56
00:02:42.640 --> 00:02:42.640


57
00:02:42.640 --> 00:02:42.640


58
00:02:42.640 --> 00:02:50.320
The messages often start with the system role followed by alternating user and assistant messages.

59
00:02:50.320 --> 00:02:50.320


60
00:02:50.320 --> 00:02:50.320


61
00:02:50.320 --> 00:02:57.440
The system role here instructs the assistant to act as a data science tutor that speaks concisely.

62
00:02:57.440 --> 00:02:57.440


63
00:02:57.440 --> 00:02:58.560


64
00:02:58.560 --> 00:03:02.360
Let's add these messages into the request code and print the response.

65
00:03:02.360 --> 00:03:02.360


66
00:03:02.360 --> 00:03:03.520


67
00:03:03.520 --> 00:03:07.720
Like the Completions endpoint, we receive a JSON response,

68
00:03:07.720 --> 00:03:11.720
where the assistant's text response is nested inside the choices key.

69
00:03:11.720 --> 00:03:11.720


70
00:03:11.720 --> 00:03:11.720


71
00:03:11.720 --> 00:03:17.440
To extract the text, we use very similar dictionary and list subsetting as we

72
00:03:17.440 --> 00:03:22.280
did for the Completions endpoint, except instead of the message being attached

73
00:03:22.280 --> 00:03:28.080
to a key called text, it is nested inside another dictionary inside the message key.

74
00:03:28.080 --> 00:03:28.080


75
00:03:28.080 --> 00:03:28.080


76
00:03:28.080 --> 00:03:32.320
To extract the text from the choices key at the top,

77
00:03:32.320 --> 00:03:33.160


78
00:03:33.160 --> 00:03:34.400
we extract the value

79
00:03:34.400 --> 00:03:39.440
from the key with square brackets, which returns a list with a single element.

80
00:03:39.440 --> 00:03:39.440


81
00:03:39.440 --> 00:03:39.440


82
00:03:39.440 --> 00:03:43.800
Next, we'll subset the first element from that list,

83
00:03:43.800 --> 00:03:43.800


84
00:03:43.800 --> 00:03:46.440
which returns a nested dictionary.

85
00:03:46.440 --> 00:03:46.440


86
00:03:46.440 --> 00:03:46.440


87
00:03:46.440 --> 00:03:54.960
Finally, we can access the content by subsetting the values from first the message key, and then the content key.

88
00:03:54.960 --> 00:03:54.960


89
00:03:54.960 --> 00:03:54.960


90
00:03:54.960 --> 00:04:03.840
We can see that the assistant stayed true to the system message - only using two sentences in its concise explanation.

91
00:04:03.840 --> 00:04:03.840


92
00:04:03.840 --> 00:04:04.800


93
00:04:04.800 --> 00:04:13.480
In the next video, you'll learn how to extend this to multi-turn tasks, but for now, time for some practice!

