1
00:00:00,080 --> 00:00:05,040
Okay, well, here we are, back in cursor, one of my very favorite places to be.

2
00:00:05,280 --> 00:00:07,160
And we're going into the week.

3
00:00:07,160 --> 00:00:08,600
One directory on the left.

4
00:00:08,640 --> 00:00:11,040
Check the LM Engineering's in block caps here.

5
00:00:11,040 --> 00:00:13,320
So you're in the right project.

6
00:00:13,320 --> 00:00:17,600
And then go to day two, which is where we find ourselves right now.

7
00:00:17,800 --> 00:00:23,120
And I need to remind you before you do anything, you have to go to the top right to where it says Select

8
00:00:23,120 --> 00:00:24,240
kernel for you.

9
00:00:24,280 --> 00:00:25,280
You click on that.

10
00:00:25,280 --> 00:00:27,240
You click in the Python Environments option.

11
00:00:27,240 --> 00:00:30,240
The first option you pick the dot VM.

12
00:00:30,440 --> 00:00:35,120
That's Python 3.12 that is matching this VM right here.

13
00:00:35,440 --> 00:00:37,920
And that means your kernel is set.

14
00:00:37,960 --> 00:00:39,800
Welcome to the day two lab.

15
00:00:40,160 --> 00:00:45,880
So I just wanted to mention here before we start that there is a page of resources on my home page and

16
00:00:45,880 --> 00:00:46,920
I've linked to it here.

17
00:00:46,960 --> 00:00:48,840
I'll try and keep adding useful resources.

18
00:00:48,840 --> 00:00:50,200
So do keep that bookmarked.

19
00:00:50,240 --> 00:00:52,360
You can go there and check it out right now if you wish.

20
00:00:52,920 --> 00:00:59,880
Okay okay so we're now going to go back again to doing what we did yesterday, but do it with a little

21
00:00:59,920 --> 00:01:00,840
bit more discipline.

22
00:01:00,840 --> 00:01:05,830
And I want to tell you about this thing called the Chat Completions API.

23
00:01:06,110 --> 00:01:12,230
And this is the name of it's the simplest way that you can call an LLM, particularly a frontier LLM.

24
00:01:12,510 --> 00:01:17,990
And it's called chat completions because it's a nod to what you're actually doing.

25
00:01:17,990 --> 00:01:25,390
When you call an LLM, you're giving it a chat a conversation so far, and you're asking it to predict

26
00:01:25,390 --> 00:01:28,350
the most likely message to come next.

27
00:01:28,470 --> 00:01:31,670
And you can think of that as as like completing a chat.

28
00:01:31,710 --> 00:01:33,510
That's what the LLM thinks it's doing.

29
00:01:33,510 --> 00:01:35,830
It's it's it's like predictive text.

30
00:01:35,830 --> 00:01:39,230
It's just trying to predict the most likely words to come next.

31
00:01:39,390 --> 00:01:44,830
And as a side effect of that, it happens to be really good at answering whatever questions it's asked.

32
00:01:44,830 --> 00:01:50,670
But all it's actually trying to do is to predict the most likely next few words, or as we'll shortly

33
00:01:50,670 --> 00:01:53,190
explain, the most likely next tokens.

34
00:01:53,590 --> 00:01:57,790
Okay, so it was actually this chat completions API.

35
00:01:57,830 --> 00:02:01,750
This this approach was invented by OpenAI first and foremost.

36
00:02:01,750 --> 00:02:08,100
But it was so popular the structure and style of this type of request became so popular that it's become

37
00:02:08,100 --> 00:02:09,060
ubiquitous.

38
00:02:09,180 --> 00:02:12,660
All of the providers offer the Chat Completions API.

39
00:02:12,820 --> 00:02:17,220
It is the kind of standard way to interact with an LLM.

40
00:02:17,300 --> 00:02:19,620
And we're going to start again with OpenAI.

41
00:02:19,660 --> 00:02:23,380
I know some of you are antsy to get off OpenAI and use free models.

42
00:02:23,580 --> 00:02:25,500
Your time is coming any second now.

43
00:02:25,540 --> 00:02:29,140
Just watch this if you don't want to use OpenAI, and we'll get to you in a second.

44
00:02:29,460 --> 00:02:30,180
All right.

45
00:02:30,300 --> 00:02:37,220
So first of all, I'm going to do a repeat of last time where we use this thing called load dot EMV,

46
00:02:37,380 --> 00:02:44,860
which is going to load in the secrets in our EMV file and check that we have OpenAI key set.

47
00:02:45,020 --> 00:02:50,780
So this load EMV function is something which which makes which loads in anything you've saved in your

48
00:02:50,780 --> 00:02:51,220
EMV.

49
00:02:51,340 --> 00:02:53,580
And then we'll just check that the key is good.

50
00:02:53,620 --> 00:02:56,500
It says API key found and it looks good so far.

51
00:02:56,500 --> 00:02:58,660
And if yours doesn't say that, then you know what to do.

52
00:02:58,700 --> 00:03:04,690
Check the EMV, check everything, and look in the look in the troubleshooting and ask me if you get

53
00:03:04,690 --> 00:03:05,170
stuck.

54
00:03:05,850 --> 00:03:08,770
Okay, let's talk about end points.

55
00:03:08,810 --> 00:03:12,610
Now, I imagine most of you know exactly what an end point is.

56
00:03:12,610 --> 00:03:14,770
It's one of those words people throw around all the time.

57
00:03:14,770 --> 00:03:15,610
But you might not.

58
00:03:15,610 --> 00:03:18,450
And for people that don't know about it, you should quickly find out.

59
00:03:18,450 --> 00:03:21,690
I explain it all in the Technical Foundations guide.

60
00:03:21,730 --> 00:03:23,890
There's nothing special at endpoint.

61
00:03:23,890 --> 00:03:31,290
It's an http url which you can call to, to to to make an API request by hitting some web address.

62
00:03:31,290 --> 00:03:34,090
And that web address would be known as an endpoint.

63
00:03:34,090 --> 00:03:35,850
It's a way that you have an API.

64
00:03:36,010 --> 00:03:40,090
But if things like API's and endpoints are new to you, then read that guide.

65
00:03:40,530 --> 00:03:41,130
Okay.

66
00:03:41,530 --> 00:03:46,370
There's an endpoint in particular which might interest you, which I want to show you right now.

67
00:03:46,370 --> 00:03:50,290
And it's an endpoint, an API endpoint offered by open AI.

68
00:03:50,490 --> 00:03:54,890
So in order to call this endpoint, which I will just do, we first have to set a couple of things which

69
00:03:54,930 --> 00:03:57,290
are very common for any HTTP request.

70
00:03:57,490 --> 00:04:01,250
There's HTTP headers, the headers that go down in that request.

71
00:04:01,290 --> 00:04:05,400
You normally you probably if you know anything about web requests.

72
00:04:05,400 --> 00:04:09,600
You specify what content type, what type of thing you want to come back.

73
00:04:09,600 --> 00:04:12,680
And that's a way of saying, I want Jason to come back, please.

74
00:04:13,000 --> 00:04:20,720
And then this is a fairly standard way of, of, of sending down a secret in an HTTP message that authorizes

75
00:04:20,720 --> 00:04:27,080
you, you put in a header called authorization and the value of that header, there's the word bearer

76
00:04:27,080 --> 00:04:31,960
and then a space and then some secret that identifies you to the third party.

77
00:04:32,120 --> 00:04:36,440
And in this case we are going to stuff the OpenAI API key.

78
00:04:36,480 --> 00:04:40,240
The thing that I got just here, we're going to stuff that into the header.

79
00:04:40,720 --> 00:04:42,880
And then I've got something here called payload.

80
00:04:42,880 --> 00:04:44,960
Payload is just a chunk of JSON.

81
00:04:45,120 --> 00:04:48,960
And it's going to be a dictionary a dictionary with two keys.

82
00:04:48,960 --> 00:04:55,320
One of them is model, which is going to be GPT five nano, the tiny version of GPT five.

83
00:04:55,480 --> 00:04:58,840
The latest model, the one of the strongest on the planet right now.

84
00:04:59,240 --> 00:05:03,480
And then the second field in this dictionary has a key of messages.

85
00:05:03,880 --> 00:05:09,350
The value is, you know, this is a list of dictionaries.

86
00:05:09,350 --> 00:05:10,630
It's a list you can see there.

87
00:05:10,630 --> 00:05:11,550
It is a list.

88
00:05:11,790 --> 00:05:19,750
Each dictionary, let's put that on another line, has a key role with value user a key content.

89
00:05:19,750 --> 00:05:24,350
And the value is tell me a fun fact okay.

90
00:05:24,390 --> 00:05:25,350
Nothing special here.

91
00:05:25,350 --> 00:05:26,390
Let's look at this.

92
00:05:26,670 --> 00:05:29,710
Uh, there is the payload JSON.

93
00:05:29,710 --> 00:05:31,150
It's is just what I said.

94
00:05:31,230 --> 00:05:35,990
Model GPT five nano messages, a list of dictionaries, role user content.

95
00:05:36,030 --> 00:05:37,550
Tell me a fun fact.

96
00:05:37,710 --> 00:05:39,470
Okay, that's a chunk of JSON.

97
00:05:39,950 --> 00:05:41,270
Uh, headers and payload.

98
00:05:41,270 --> 00:05:44,430
What we're now going to do is we're going to send that to an endpoint.

99
00:05:44,750 --> 00:05:47,110
This is the endpoint API.

100
00:05:49,590 --> 00:05:50,470
V1.

101
00:05:54,110 --> 00:05:56,230
It's chat completions.

102
00:05:56,430 --> 00:06:02,190
And sometimes when you make a Post request people think of that as like creating a new resource for

103
00:06:02,190 --> 00:06:04,230
people that are like rest, rest people.

104
00:06:04,350 --> 00:06:06,550
Uh, that's that's sometimes called creating a resource.

105
00:06:06,550 --> 00:06:12,780
So you could think of this as being like chat completions create some familiar.

106
00:06:12,900 --> 00:06:15,220
Uh, so chat completions create.

107
00:06:15,220 --> 00:06:17,780
We're passing in our headers with our API key.

108
00:06:17,780 --> 00:06:21,420
And the payload is this JSON blob right here.

109
00:06:21,580 --> 00:06:23,340
So let's run that.

110
00:06:23,700 --> 00:06:26,020
And what comes back is a bunch of JSON.

111
00:06:26,020 --> 00:06:27,780
We asked for JSON back.

112
00:06:27,820 --> 00:06:28,900
Let's see what we get back.

113
00:06:28,900 --> 00:06:30,100
Let's see what kind of JSON we get.

114
00:06:30,100 --> 00:06:30,660
Here it is.

115
00:06:30,980 --> 00:06:35,460
Uh so it's a chunk of JSON as like an ID object, various stuff.

116
00:06:35,460 --> 00:06:39,020
And then it has a field called choices and Choices.

117
00:06:39,020 --> 00:06:42,060
In this response, choices is a list.

118
00:06:42,260 --> 00:06:49,380
And the first item of that choices with index zero is something which has a field message.

119
00:06:49,980 --> 00:06:53,860
And that message is itself a JSON dictionary.

120
00:06:54,020 --> 00:06:59,060
And that has something called content, which is fun fact.

121
00:06:59,100 --> 00:07:04,340
There are possible unique games, more possible unique games of chess than there are atoms in the observable

122
00:07:04,340 --> 00:07:05,300
universe.

123
00:07:05,500 --> 00:07:09,890
About ten to the power of 120 There is a fun fact.

124
00:07:09,890 --> 00:07:15,970
And so that fun fact came back in the JSON response from our call to GPT five to that endpoint, that

125
00:07:15,970 --> 00:07:17,970
URL that I just gave you.

126
00:07:17,970 --> 00:07:20,170
And so there's obviously there's another way I could do this.

127
00:07:20,170 --> 00:07:21,530
Let me get a new code thing here.

128
00:07:21,530 --> 00:07:26,330
Let's say, uh, um, we said response JSON.

129
00:07:26,330 --> 00:07:29,450
That was the JSON that we just were just looking at right up here.

130
00:07:29,770 --> 00:07:31,170
Uh, I spelt response wrong.

131
00:07:31,330 --> 00:07:34,530
That's that's what you get if you make me type Response.json.

132
00:07:34,530 --> 00:07:38,690
So we could say, okay, let's look in the choices field.

133
00:07:39,010 --> 00:07:40,690
I guess it knows what I'm doing here.

134
00:07:40,730 --> 00:07:43,010
Look at the first element in there.

135
00:07:43,010 --> 00:07:51,290
Element index zero at the field message at the field content okay.

136
00:07:51,330 --> 00:07:51,970
You see that.

137
00:07:51,970 --> 00:07:53,370
That's you know what that's going to do.

138
00:07:53,410 --> 00:07:56,610
We're just going to choices the first one message content.

139
00:07:56,730 --> 00:07:58,250
And let's see what that prints.

140
00:07:58,370 --> 00:08:03,370
It prints that that very fun fact of course that we just looked at uh, the more possible unique games

141
00:08:03,370 --> 00:08:05,370
of chess than there are atoms in the observable universe.

142
00:08:05,930 --> 00:08:07,730
Okay, but you know what?

143
00:08:07,850 --> 00:08:14,840
Uh, so so this is a perfectly good way to call OpenAI in the cloud using an HTTP endpoint.

144
00:08:14,840 --> 00:08:15,840
And it's fine.

145
00:08:15,840 --> 00:08:16,560
We could do this.

146
00:08:16,600 --> 00:08:18,120
We could we could always type this.

147
00:08:18,480 --> 00:08:25,000
But it is kind of messy fussing around with JSON, looking in dictionaries at keys and things like that.

148
00:08:25,000 --> 00:08:31,480
It's kind of hokey, and it would be a real pain if every time we wanted to call GPT or any frontier

149
00:08:31,520 --> 00:08:39,280
model, we had to stitch together these HTTP requests, call that slash chat completions with a post

150
00:08:39,320 --> 00:08:45,160
or create and then be be sort of navigating our way through JSON objects like this.

151
00:08:45,200 --> 00:08:46,360
It would be a pain.

152
00:08:46,560 --> 00:08:48,600
It would be nice if there were a better way to do it.

153
00:08:48,640 --> 00:08:50,440
Yes, yes, I know you get the joke.

154
00:08:50,480 --> 00:08:51,600
You know what I'm gonna say?

155
00:08:51,640 --> 00:08:53,240
There is a better way to do it.

156
00:08:53,440 --> 00:08:55,800
And OpenAI created this better way.

157
00:08:55,800 --> 00:09:02,840
They made a package called OpenAI, and that package is what's known as a Python client library.

158
00:09:02,840 --> 00:09:06,760
Python client library is nothing fancy at all.

159
00:09:06,800 --> 00:09:11,550
A Python client library that you often use for APIs all over the place for you.

160
00:09:11,590 --> 00:09:15,270
Name it for APIs, for sending emails, for APIs to do so much.

161
00:09:15,470 --> 00:09:22,270
Um, and these things are typically very lightweight libraries that manufacture an HTTP request to an

162
00:09:22,270 --> 00:09:22,990
endpoint.

163
00:09:23,110 --> 00:09:28,830
And with what comes back, it turns it into Python objects so that you're not messing around with this

164
00:09:28,830 --> 00:09:33,630
kind of stuff with with these, uh, ploughing your way through JSON dictionaries.

165
00:09:33,630 --> 00:09:40,430
But you can just write some nice, elegant Python code, and that is all the OpenAI library is.

166
00:09:40,550 --> 00:09:45,790
It's a Python client library that wraps a call to an HTTP endpoint.

167
00:09:45,790 --> 00:09:47,150
It's very simple.

168
00:09:47,270 --> 00:09:48,790
It's completely open source.

169
00:09:48,790 --> 00:09:50,510
You can open it, look at it, look at all the code.

170
00:09:50,550 --> 00:09:52,150
It's perfectly simple.

171
00:09:52,470 --> 00:09:56,510
Some people, the first time they come across this, think that when you're dealing with OpenAI, the

172
00:09:56,510 --> 00:10:02,590
object and the code, that somehow we're sort of running GPT and we have some some fancy code from OpenAI.

173
00:10:02,750 --> 00:10:03,710
Not at all.

174
00:10:03,750 --> 00:10:07,910
It's vanilla code that just wraps making a web request.

175
00:10:07,910 --> 00:10:11,790
And we'll we'll quickly use that code now, but it's going to be much more familiar to you.