1
00:00:00,240 --> 00:00:00,640
Okay.

2
00:00:00,640 --> 00:00:04,200
This is the single most important moment of the entire lab.

3
00:00:04,680 --> 00:00:09,320
So it's the chat function, and I've spaced it out a bit to really explain what's going on.

4
00:00:09,320 --> 00:00:14,080
It's a more powerful chat function that includes the calling of a tool.

5
00:00:14,560 --> 00:00:17,120
It starts exactly the same way as it did before.

6
00:00:17,240 --> 00:00:19,120
We create some messages.

7
00:00:19,120 --> 00:00:20,640
Role of system.

8
00:00:20,640 --> 00:00:22,440
The content is the system prompt.

9
00:00:22,560 --> 00:00:24,280
We add in the history from before.

10
00:00:24,680 --> 00:00:25,680
Role of user.

11
00:00:25,680 --> 00:00:27,680
The content is the user's message.

12
00:00:28,200 --> 00:00:35,120
And now do a pretty standard trick of looping again and again and again until a variable called done

13
00:00:35,160 --> 00:00:36,520
gets set to true.

14
00:00:36,680 --> 00:00:39,480
So it's going to keep going until done is true.

15
00:00:39,880 --> 00:00:41,160
What is it going to keep doing?

16
00:00:41,600 --> 00:00:43,360
It's going to call open AI.

17
00:00:43,880 --> 00:00:46,640
And this is the usual thing OpenAI.

18
00:00:49,280 --> 00:00:51,360
Create we're familiar with that.

19
00:00:51,400 --> 00:00:52,680
We pass in the model.

20
00:00:52,920 --> 00:00:56,320
We pass in the messages this set of of messages.

21
00:00:56,560 --> 00:00:59,520
But look we also pass in Tools.

22
00:00:59,720 --> 00:01:06,080
Tools is that JSON blob, that structure of JSON, which is going to be sent to the LM saying, these

23
00:01:06,080 --> 00:01:07,360
are all the things you can do.

24
00:01:07,360 --> 00:01:09,760
This is the description of everything you can do.

25
00:01:10,280 --> 00:01:10,680
All right.

26
00:01:10,720 --> 00:01:12,800
And back it comes into response.

27
00:01:12,800 --> 00:01:18,880
And what comes in response might just be the output to go to the user, or it might be a response that

28
00:01:18,880 --> 00:01:21,600
indicates that tools need to be called.

29
00:01:21,600 --> 00:01:23,520
So that is what we do next.

30
00:01:23,560 --> 00:01:25,880
We look at response choices.

31
00:01:28,800 --> 00:01:35,200
And this tells us whether the LM is finished and it's done or whether it wants to call a tool.

32
00:01:35,640 --> 00:01:40,080
So if the finish reason is tool calls, it wants to call a tool.

33
00:01:40,480 --> 00:01:46,960
Then we we look at the message itself, we pluck out the tool calls from it.

34
00:01:47,560 --> 00:01:53,880
And this this is where we call that function that we just wrote, that clever function, that sneaky

35
00:01:53,880 --> 00:01:54,600
function.

36
00:01:54,840 --> 00:01:55,760
Right here it is.

37
00:01:55,760 --> 00:01:57,560
Remember it handle tool calls.

38
00:01:57,650 --> 00:02:02,810
This is the one that is the glorified if statement, the one that has the fancy if statement.

39
00:02:02,810 --> 00:02:07,970
But there's also this more silly version here that just has an actual if statement.

40
00:02:08,210 --> 00:02:09,850
But it's going to call this.

41
00:02:10,210 --> 00:02:11,490
So back we go.

42
00:02:12,170 --> 00:02:16,850
So again if it wants to call tools then we do that.

43
00:02:16,850 --> 00:02:17,930
We handle the tool calls.

44
00:02:17,930 --> 00:02:19,010
We call the tools.

45
00:02:19,170 --> 00:02:21,570
And then into the set of messages.

46
00:02:21,570 --> 00:02:25,130
We put what it wanted to do and the results of doing it.

47
00:02:25,490 --> 00:02:29,890
And then we will loop back and call a second time.

48
00:02:29,890 --> 00:02:32,810
And we will keep doing this until the finish.

49
00:02:32,810 --> 00:02:34,770
Reason is not tool calls.

50
00:02:34,770 --> 00:02:40,850
It does not want to call tools with whatever, it's just responded, in which case we return the content

51
00:02:40,850 --> 00:02:45,250
because that is going to be the actual response to the user.

52
00:02:45,610 --> 00:02:46,930
I hope that made sense.

53
00:02:46,970 --> 00:02:50,450
If not, go through it slowly, carefully until it does.

54
00:02:50,650 --> 00:02:52,330
This is this is pivotal.

55
00:02:52,330 --> 00:02:54,690
This is how the whole thing hangs together.

56
00:02:55,170 --> 00:02:56,450
Now, I know what you're thinking.

57
00:02:56,450 --> 00:02:58,650
You're thinking it's all very well showing us all this code.

58
00:02:58,650 --> 00:03:00,170
But does it actually work?

59
00:03:00,410 --> 00:03:01,770
Well, let's find out.

60
00:03:01,770 --> 00:03:03,170
Let's run the code.

61
00:03:03,210 --> 00:03:04,370
Here we have it.

62
00:03:04,370 --> 00:03:05,450
Let's say hi there.

63
00:03:05,810 --> 00:03:06,170
Hello.

64
00:03:06,170 --> 00:03:07,370
How can I assist you today?

65
00:03:07,370 --> 00:03:08,250
What's your job?

66
00:03:09,490 --> 00:03:11,650
I'm currently the co-founder and CTO of Nebula.

67
00:03:11,690 --> 00:03:12,570
Very nice.

68
00:03:12,730 --> 00:03:14,050
Uh, do you have a patent?

69
00:03:15,490 --> 00:03:16,850
Yes I do.

70
00:03:16,890 --> 00:03:17,650
Very nice.

71
00:03:17,730 --> 00:03:21,410
Uh, who is your favorite musician?

72
00:03:22,210 --> 00:03:23,210
That's what I want to know.

73
00:03:25,090 --> 00:03:29,370
I hope you, uh, heard that success.

74
00:03:29,490 --> 00:03:30,050
So it says.

75
00:03:30,050 --> 00:03:32,010
I'm not sure about my favorite musician.

76
00:03:32,010 --> 00:03:33,130
I appreciate a variety.

77
00:03:33,130 --> 00:03:35,010
If you have a favorite, I'd love to hear about it.

78
00:03:35,050 --> 00:03:36,370
It called the tool.

79
00:03:36,610 --> 00:03:37,570
You heard my phone.

80
00:03:37,570 --> 00:03:38,010
Go.

81
00:03:38,210 --> 00:03:39,890
We have success.

82
00:03:39,970 --> 00:03:42,810
The agent was able to take that JSON.

83
00:03:42,850 --> 00:03:45,570
It knew that this was a tool that it could call.

84
00:03:45,610 --> 00:03:48,410
It was able to respond with the finish.

85
00:03:48,410 --> 00:03:50,250
Reason was was tool calls.

86
00:03:50,250 --> 00:03:56,420
And that caused us to to interpret that to make the call to R our function handle tool calls, which

87
00:03:56,420 --> 00:04:03,500
had our fancy code that ended up calling this function that ended up calling pushover, that then sent

88
00:04:03,540 --> 00:04:07,820
the push notification to my phone and it all hung together.

89
00:04:08,180 --> 00:04:09,660
All right, let's keep going.

90
00:04:09,980 --> 00:04:11,220
I'd like to get in touch.

91
00:04:12,220 --> 00:04:13,020
That's great to hear.

92
00:04:13,020 --> 00:04:14,260
Please share your email address.

93
00:04:14,300 --> 00:04:19,340
I'm at Edward donna.com.

94
00:04:23,340 --> 00:04:24,740
I know you're expecting it.

95
00:04:24,740 --> 00:04:25,100
Now.

96
00:04:25,100 --> 00:04:27,380
The, uh, the magic's worn off.

97
00:04:27,420 --> 00:04:28,780
You're not impressed anymore?

98
00:04:28,900 --> 00:04:30,500
Well, I think that's really cool.

99
00:04:30,620 --> 00:04:36,220
So it has indeed, of course, notified me about my own email address, which is great.

100
00:04:36,220 --> 00:04:39,260
And so it shows you that it does hold together.

101
00:04:39,420 --> 00:04:44,460
Uh, and you are indeed able to have these interactions and get it to text you.

102
00:04:45,060 --> 00:04:45,780
Okay.

103
00:04:46,100 --> 00:04:46,860
That's great.

104
00:04:46,860 --> 00:04:48,420
I hope you enjoyed it.

105
00:04:48,420 --> 00:04:54,900
But next it's all about doing this for you and actually deploying this live to production so that you

106
00:04:54,900 --> 00:04:59,180
can serve your own avatar on your personal website.

107
00:05:00,060 --> 00:05:02,940
So if you've followed everything so far, then many congratulations.

108
00:05:02,940 --> 00:05:07,660
If you haven't, then also congratulations because it gives you this great opportunity to go through

109
00:05:07,820 --> 00:05:09,500
work through this until you do.

110
00:05:09,540 --> 00:05:10,700
And if I can help.

111
00:05:10,740 --> 00:05:13,260
Email me or contact me anytime.

112
00:05:13,260 --> 00:05:13,860
LinkedIn with me.

113
00:05:13,860 --> 00:05:16,980
Message me so that I can help you out.

114
00:05:17,300 --> 00:05:24,900
So what I want to do now is show you how you can deploy this application in production for yourself,

115
00:05:24,900 --> 00:05:28,100
so that you can have this as your virtual resume.

116
00:05:28,380 --> 00:05:30,460
Surely this is the future of resumes.

117
00:05:30,500 --> 00:05:36,900
No longer will we have profiles or CVS resumes where you list out your skills and experience, but rather

118
00:05:36,900 --> 00:05:41,220
you'll have a chat bot that people can interact with to learn about your career.

119
00:05:41,220 --> 00:05:48,020
And what better way to highlight your your AI abilities and your abilities to work with Agentic AI than

120
00:05:48,020 --> 00:05:55,870
to have an agentic solution up on your website that will allow you to interact with people and talk

121
00:05:55,870 --> 00:05:56,910
about your career.

122
00:05:57,510 --> 00:06:01,550
So let me show you some steps to deploying this.

123
00:06:01,950 --> 00:06:06,590
So the first thing to do is to put the code into a Python module.

124
00:06:06,590 --> 00:06:09,910
And I have done that right here for you in App.py.

125
00:06:10,230 --> 00:06:13,710
And you'll see that this has all of the stuff you would expect.

126
00:06:13,710 --> 00:06:20,550
It's got the same information in here, but organized into nice little functions at the top of the functions

127
00:06:20,550 --> 00:06:22,430
that cover the tools that we call.

128
00:06:22,750 --> 00:06:26,710
And then there is a class me as in me or or you.

129
00:06:27,070 --> 00:06:33,070
And that's something which has the handle tool call that we know so well that's right here.

130
00:06:33,070 --> 00:06:40,110
And it has the system prompt and this loop that we just went through, this chat loop that has the while

131
00:06:40,150 --> 00:06:46,590
not done it calls open I create, it passes in the tools.

132
00:06:46,710 --> 00:06:48,670
I don't think I mentioned this actually, so I'm pleased.

133
00:06:48,670 --> 00:06:51,430
I pause for a minute here because I do want to show this one more time.

134
00:06:51,630 --> 00:06:53,590
We call chat completions create.

135
00:06:53,630 --> 00:07:00,270
We select the model, we pass in the messages, and we pass in the tools JSON, the description of the

136
00:07:00,270 --> 00:07:01,550
tools that it can call.

137
00:07:01,910 --> 00:07:04,790
And with what comes back, we take choices zero.

138
00:07:04,910 --> 00:07:08,030
And we look at the finish reason back in the response.

139
00:07:08,030 --> 00:07:11,230
And we see whether or not that is tool calls.

140
00:07:11,430 --> 00:07:19,510
And if so, then we loop through to make sure that we run all of the tools before calling it again to

141
00:07:19,550 --> 00:07:20,950
get the final response.

142
00:07:21,350 --> 00:07:26,910
So this is so critical, this this chat with its loop that I do encourage you to take a look at it.

143
00:07:27,390 --> 00:07:30,950
And now at the bottom here we have the Gradio code.

144
00:07:30,950 --> 00:07:38,270
We've got it set up here so that if this is just run as it is then it will create a gradio chat interface

145
00:07:38,470 --> 00:07:44,550
with showing me chat and with the messages and it will launch it.

146
00:07:44,950 --> 00:07:49,840
So what I can do now, in fact, is I can bring up a terminal which you may remember.

147
00:07:49,840 --> 00:07:53,200
You press control and the backwards tick mark.

148
00:07:53,360 --> 00:07:56,200
I can go into the week one folder.

149
00:07:56,200 --> 00:07:59,200
Here we are and I can now type.

150
00:07:59,240 --> 00:08:01,120
Remember you don't type Python.

151
00:08:01,160 --> 00:08:04,040
You type, you run to run something.

152
00:08:04,040 --> 00:08:06,960
In this way you've run app.py.

153
00:08:07,680 --> 00:08:11,720
And if I run that then it should come up.

154
00:08:11,960 --> 00:08:13,520
So this link.

155
00:08:13,520 --> 00:08:17,920
Now on this link I can press control and click it to bring it up.

156
00:08:18,400 --> 00:08:19,160
Up it comes.

157
00:08:19,200 --> 00:08:20,840
Let me make that a bit bigger for you.

158
00:08:21,120 --> 00:08:26,240
This is now an app running on my box and I can say hi there.

159
00:08:27,040 --> 00:08:27,400
Hello.

160
00:08:27,400 --> 00:08:34,560
How can I assist you and, and uh, do you have a patent and so on and there we go.

161
00:08:34,760 --> 00:08:35,360
Okay.

162
00:08:35,680 --> 00:08:43,240
So so that works going back here, what we now want to do is look at actually deploying this.

163
00:08:43,560 --> 00:08:48,920
So there are various ways you can deploy an application like this, but one of them that's particularly

164
00:08:48,920 --> 00:08:55,800
simple and elegant is brought to you by the lovely AI company Hugging Face, which also happens to be

165
00:08:55,800 --> 00:08:57,960
the company that owns Gradio.

166
00:08:58,000 --> 00:09:02,960
That beautiful, framework hugging face has something called hugging face spaces, and hugging face

167
00:09:02,960 --> 00:09:06,880
spaces is a way that you can deploy things really simply.

168
00:09:06,880 --> 00:09:10,280
And then if you wish, you can also embed them in your own home page.

169
00:09:10,280 --> 00:09:13,680
So it gives you a really nice, simple way to do it.

170
00:09:13,960 --> 00:09:15,880
So this is how you do it.

171
00:09:15,880 --> 00:09:21,400
You first have to go to Hugging Faces website and set up your own account if you don't already have

172
00:09:21,400 --> 00:09:21,880
one.

173
00:09:21,880 --> 00:09:25,360
So you may already have a hugging face account, but if not, you need to set one up.

174
00:09:25,760 --> 00:09:31,640
Once you've done with that, you go into the foundations folder in a terminal and you run this command

175
00:09:31,680 --> 00:09:33,400
Gradio deploy.

176
00:09:34,000 --> 00:09:39,240
It's then going to ask you a series of questions, a bunch of simple questions to which I've got the

177
00:09:39,240 --> 00:09:40,920
answers you have to give right here.

178
00:09:41,360 --> 00:09:42,760
And I'll go through and do it right now.

179
00:09:42,760 --> 00:09:46,370
But I'm not going to do it properly, because one of the questions it's going to ask you about some

180
00:09:46,370 --> 00:09:52,890
secrets that you'll say yes, and you're going to provide your OpenAI API key so that it can use that

181
00:09:52,890 --> 00:09:54,050
in the deployment.

182
00:09:54,050 --> 00:09:57,250
And you can also give your pushover tokens as well.

183
00:09:57,530 --> 00:10:01,330
And then I have got the rest of the instructions right there for you.

184
00:10:01,810 --> 00:10:06,810
And once that's done, it will be deployed into a hugging face space.

185
00:10:06,810 --> 00:10:08,850
And you can choose whether that is public or private.

186
00:10:08,850 --> 00:10:12,810
You can have it be private if you don't want other people using your key.

187
00:10:12,850 --> 00:10:16,170
Although as I say, it's super cheap, but people would have to.

188
00:10:16,210 --> 00:10:20,690
It would have to go viral for you to have any kind of significant spend.

189
00:10:20,690 --> 00:10:25,450
And of course, make sure that your OpenAI account, you never want to have that auto refilling so that

190
00:10:25,450 --> 00:10:30,210
if that ever were to happen, if your app did go viral, then you would be protected.

191
00:10:30,210 --> 00:10:33,490
And anyway, OpenAI has limits, as you probably know.

192
00:10:34,170 --> 00:10:36,890
So, uh, let's go and actually do this.

193
00:10:36,890 --> 00:10:38,570
Let's run Gradio deploy.

194
00:10:38,770 --> 00:10:43,970
So I'm going to bring up the terminal now, and then we will go and do this ourselves together.