WEBVTT

1
00:00:00.000 --> 00:00:03.160
Great work so far!

2
00:00:03.160 --> 00:00:11.160
In this video, we'll begin to unleash the full potential of chat models by creating multi-turn conversations.

3
00:00:11.160 --> 00:00:11.160


4
00:00:11.160 --> 00:00:11.160


5
00:00:11.160 --> 00:00:16.840
Recall that messages are sent to the Chat Completions endpoint as a list of dictionaries,

6
00:00:16.840 --> 00:00:23.960
where each dictionary provides content to a specific role from either system, user, or assistant.

7
00:00:23.960 --> 00:00:23.960


8
00:00:23.960 --> 00:00:23.960


9
00:00:23.960 --> 00:00:30.040
For single turn tasks, no content is sent to the assistant role - the model relies only on

10
00:00:30.040 --> 00:00:35.840
its existing knowledge, the behaviors sent to the system role, and the instruction from the user.

11
00:00:35.840 --> 00:00:35.840


12
00:00:35.840 --> 00:00:36.720


13
00:00:36.720 --> 00:00:40.680
Providing assistant messages can be a simple way for developers to steer

14
00:00:40.680 --> 00:00:46.560
the model in the right direction without having to surface anything to end-users.

15
00:00:46.560 --> 00:00:46.560


16
00:00:46.560 --> 00:00:46.560


17
00:00:46.560 --> 00:00:52.200
If we created a data science tutor application, we could provide a few examples of data

18
00:00:52.200 --> 00:00:57.680
science questions and answers that would be sent to the API along with the user's question.

19
00:00:57.680 --> 00:00:58.800


20
00:00:58.800 --> 00:00:58.800


21
00:00:58.800 --> 00:01:03.560
Let's improve our data science tutor by providing an example.

22
00:01:03.560 --> 00:01:03.560


23
00:01:03.560 --> 00:01:03.560


24
00:01:03.560 --> 00:01:08.320
Between the system message outlining the assistant's behavior and the user's question

25
00:01:08.320 --> 00:01:15.440
to answer, we'll add a user and assistant message to serve as an ideal example for the model.

26
00:01:15.440 --> 00:01:15.440


27
00:01:15.440 --> 00:01:15.440


28
00:01:15.440 --> 00:01:18.240
We add a user message containing a question on a similar

29
00:01:18.240 --> 00:01:18.800
topic

30
00:01:18.800 --> 00:01:19.440


31
00:01:19.440 --> 00:01:22.760
and a response that we consider ideally written for our use case.

32
00:01:22.760 --> 00:01:22.760


33
00:01:22.760 --> 00:01:23.720


34
00:01:23.720 --> 00:01:30.200
The model now not only has its pre-existing understanding, but also a working example to guide its response.

35
00:01:30.200 --> 00:01:30.200


36
00:01:30.200 --> 00:01:31.400


37
00:01:31.400 --> 00:01:34.200
With an example to work with, the assistant provides a

38
00:01:34.200 --> 00:01:38.160
succinct and accurate response that's more in-line with our expectations.

39
00:01:38.160 --> 00:01:39.080


40
00:01:39.080 --> 00:01:39.080


41
00:01:39.080 --> 00:01:44.760
Another common use for providing assistant messages is to store responses.

42
00:01:44.760 --> 00:01:49.200
Storing responses means that we can create a conversation history,

43
00:01:49.200 --> 00:01:54.040
which we can feed into the model to have back-and-forth conversations.

44
00:01:54.040 --> 00:01:58.680
This is exactly what goes on underneath AI chatbots like ChatGPT!

45
00:01:58.680 --> 00:01:58.680


46
00:01:58.680 --> 00:01:58.680


47
00:01:58.680 --> 00:02:03.840
To code a conversation, we'll need to create a system so that when a

48
00:02:03.840 --> 00:02:08.040
user message is sent, and an assistant response is generated,

49
00:02:08.040 --> 00:02:08.040


50
00:02:08.040 --> 00:02:10.360
they are fed back into the messages

51
00:02:10.360 --> 00:02:10.360


52
00:02:10.360 --> 00:02:14.160
and stored to be sent with the next user message.

53
00:02:14.160 --> 00:02:14.160


54
00:02:14.160 --> 00:02:14.160


55
00:02:14.160 --> 00:02:16.760
Then, when a new user message is provided,

56
00:02:16.760 --> 00:02:16.760


57
00:02:16.760 --> 00:02:20.880
the model has the context from the conversation history to draw from.

58
00:02:20.880 --> 00:02:20.880


59
00:02:20.880 --> 00:02:21.960


60
00:02:21.960 --> 00:02:27.600
This means that if we introduce ourselves in the first user message, then ask the model what our

61
00:02:27.600 --> 00:02:33.560
name is in the second, it should return the correct answer, as it has access to the conversation history.

62
00:02:33.560 --> 00:02:33.560


63
00:02:33.560 --> 00:02:34.400


64
00:02:34.400 --> 00:02:37.120
Let's start to code this in Python.

65
00:02:37.120 --> 00:02:37.120


66
00:02:37.120 --> 00:02:37.120


67
00:02:37.120 --> 00:02:40.320
We start by defining some base messages, which can

68
00:02:40.320 --> 00:02:44.280
include the system message and any other developer-written examples.

69
00:02:44.280 --> 00:02:44.280


70
00:02:44.280 --> 00:02:44.280


71
00:02:44.280 --> 00:02:47.240
Then we can define a list of questions.

72
00:02:47.240 --> 00:02:52.480
Here we ask why Python is popular and then ask it to summarize the

73
00:02:52.480 --> 00:02:58.120
response provided in one sentence, which requires context on the previous response.

74
00:02:58.120 --> 00:02:58.120


75
00:02:58.120 --> 00:02:58.120


76
00:02:58.120 --> 00:03:05.040
Because we want a response for each question, we start by looping over the user_qs list.

77
00:03:05.040 --> 00:03:05.040


78
00:03:05.040 --> 00:03:05.040


79
00:03:05.040 --> 00:03:08.640
Next, to prepare the user message string for

80
00:03:08.640 --> 00:03:11.720
the API, we need to create a dictionary

81
00:03:11.720 --> 00:03:11.720


82
00:03:11.720 --> 00:03:15.600
and add it to the list of messages using the list append method.

83
00:03:15.600 --> 00:03:15.600


84
00:03:15.600 --> 00:03:15.600


85
00:03:15.600 --> 00:03:21.520
We can now send the messages to the Chat Completion endpoint and store the response.

86
00:03:21.520 --> 00:03:21.520


87
00:03:21.520 --> 00:03:21.520


88
00:03:21.520 --> 00:03:28.560
We extract the assistant's message by subsetting from the API response, converting to a

89
00:03:28.560 --> 00:03:34.920
dictionary so it's in the correct format, then adding it to the messages list for the next iteration.

90
00:03:34.920 --> 00:03:34.920


91
00:03:34.920 --> 00:03:34.920


92
00:03:34.920 --> 00:03:39.960
Finally, we'll add two print statements so the output is a

93
00:03:39.960 --> 00:03:43.760
conversation between the user and assistant written as a script.

94
00:03:43.760 --> 00:03:43.760


95
00:03:43.760 --> 00:03:43.760


96
00:03:43.760 --> 00:03:49.000
We can see that we were successfully able to provide a follow-up correction to

97
00:03:49.000 --> 00:03:53.880
the model's response without having to repeat our question or the model's response.

98
00:03:53.880 --> 00:03:53.880


99
00:03:53.880 --> 00:03:53.880


100
00:03:53.880 --> 00:03:58.320
You've learned about the key functionality underpinning many

101
00:03:58.320 --> 00:04:05.160
AI-powered chatbots and assistants - time to begin creating your own!

