1
00:00:03,080 --> 00:00:13,460
In this exercise, we are going to explore the use of OpenAI functions, uh, in a long chain application,

2
00:00:13,580 --> 00:00:22,310
and also the use of OpenAI functions out of long chain using directly the OpenAI API.

3
00:00:22,430 --> 00:00:29,690
So, as you will see, using OpenAI function functions is extremely powerful.

4
00:00:29,690 --> 00:00:36,260
It allows us a universe of possibilities and applications.

5
00:00:36,260 --> 00:00:47,330
But in the current status of long chain, this approach may have glitches, may have, uh, situations

6
00:00:47,330 --> 00:00:50,750
where uh, you break the application.

7
00:00:50,750 --> 00:00:59,360
So it is possible that at the time you are watching this in this, this video, this glitch is fixed.

8
00:00:59,450 --> 00:01:06,260
But by November 2023 it is still not so.

9
00:01:06,260 --> 00:01:11,870
The long chain approach has a some areas where it is going to fail.

10
00:01:11,870 --> 00:01:21,200
So here we are going to show you how it works, where it fails and how you can fix it using directly

11
00:01:21,200 --> 00:01:23,570
the OpenAI uh API.

12
00:01:24,730 --> 00:01:34,690
So in this, uh, chapter, we are going to design an LM application capable of receiving and managing

13
00:01:34,690 --> 00:01:37,150
Chinese food orders.

14
00:01:37,510 --> 00:01:42,340
So as you see in the code, this is a long exercise.

15
00:01:42,340 --> 00:01:50,890
We are not going into the deep details, but we will go, uh, enough for you to see what is happening,

16
00:01:50,890 --> 00:01:52,480
how it is working.

17
00:01:52,480 --> 00:01:57,760
And with the working code, you will be able to experiment and try by yourself.

18
00:01:57,760 --> 00:02:03,970
And then with the documentation, you can complete whatever doubt or question you may have.

19
00:02:04,270 --> 00:02:09,250
But in this, uh, exercise, you are going to see that.

20
00:02:11,410 --> 00:02:15,820
Connecting OpenAI functions to our LM applications.

21
00:02:16,120 --> 00:02:19,360
I mean, the sky is the limit, as you will see.

22
00:02:19,540 --> 00:02:24,760
So we will use for this to solve this, uh, request.

23
00:02:24,940 --> 00:02:29,980
The ChatGPT capability to work with functions with OpenAI functions.

24
00:02:30,010 --> 00:02:37,510
First, we will design a solution with long chain and study a use case where the solution may fail.

25
00:02:37,510 --> 00:02:44,380
And then we will design a solution using the OpenAI API that solves the previous problem.

26
00:02:44,380 --> 00:02:48,460
So as you can see, the process is long.

27
00:02:48,460 --> 00:02:50,500
So let's go step by step.

28
00:02:50,890 --> 00:02:58,060
First, as always, we connect with our dot m file to get the the credentials in order to communicate

29
00:02:58,060 --> 00:03:00,130
with the OpenAI API.

30
00:03:01,430 --> 00:03:09,740
And then so do you remember what we did when we were with agents in Lansing?

31
00:03:10,280 --> 00:03:16,790
Uh, do you remember this agent that, uh, was, uh, deciding between different tools?

32
00:03:16,790 --> 00:03:17,390
Right.

33
00:03:17,390 --> 00:03:26,630
So if you remember a first, we, uh, imported the tools, and then instead of working directly with

34
00:03:26,630 --> 00:03:33,020
the tools, we needed to provide a description of the tools to the LM.

35
00:03:33,020 --> 00:03:38,030
So the LM was able to understand what.

36
00:03:38,690 --> 00:03:42,380
Every tool does right what each tool does like.

37
00:03:42,380 --> 00:03:44,990
Okay, this is a tool for searching Google.

38
00:03:44,990 --> 00:03:47,660
This is a tool for math operations.

39
00:03:47,660 --> 00:03:49,760
These are tools for this or that right.

40
00:03:49,760 --> 00:03:55,550
So with the functions we need to uh take the same approach.

41
00:03:55,700 --> 00:03:59,600
So we will have the function definition.

42
00:03:59,720 --> 00:04:04,520
We will define functions in in Python are as as usual.

43
00:04:04,520 --> 00:04:15,830
But we also have to provide an explanation or a description of what each of these function does to chat

44
00:04:15,830 --> 00:04:16,459
GPT.

45
00:04:16,610 --> 00:04:19,040
So this is what we do here.

46
00:04:19,040 --> 00:04:25,310
So in this exercise in this application we are going to make use of two functions.

47
00:04:25,790 --> 00:04:28,700
The first function is get item info.

48
00:04:29,150 --> 00:04:36,080
This is a function that we are going to use to start the conversation with the customers.

49
00:04:36,080 --> 00:04:39,110
Remember we are a Chinese restaurant, right?

50
00:04:39,110 --> 00:04:40,370
Chinese food restaurant.

51
00:04:40,490 --> 00:04:47,360
So the customers are going to start a conversation with us and they are going to ask us, okay, what

52
00:04:47,360 --> 00:04:48,590
do you have in the menu?

53
00:04:48,590 --> 00:04:54,860
Or how much is this, uh, Chinese rice, uh, this whatever.

54
00:04:54,860 --> 00:04:55,160
Right.

55
00:04:55,160 --> 00:04:59,450
So this is the function that is going to be in charge of that.

56
00:04:59,450 --> 00:05:08,390
So we need to describe this function in this particular way for ChatGPT to be able to understand it,

57
00:05:08,840 --> 00:05:16,190
the name of the function, the description, what the function does and then the parameters they, they,

58
00:05:16,190 --> 00:05:16,790
they have.

59
00:05:16,790 --> 00:05:23,570
And in this case you will see that the most important information regarding the parameters is the item

60
00:05:23,570 --> 00:05:23,870
name.

61
00:05:23,870 --> 00:05:25,670
So the product, the name of the product.

62
00:05:25,670 --> 00:05:26,150
Right.

63
00:05:26,150 --> 00:05:27,890
So we have the name of the product.

64
00:05:27,890 --> 00:05:30,710
And then we have uh like like.

65
00:05:31,380 --> 00:05:33,900
Like a like that.

66
00:05:34,350 --> 00:05:39,960
The second function we have is going to manage a the orders.

67
00:05:39,960 --> 00:05:48,210
So once you know the menu items and once you know how many of each of them you want, you are going

68
00:05:48,210 --> 00:05:49,920
to make the order.

69
00:05:49,920 --> 00:05:50,160
Right?

70
00:05:50,160 --> 00:05:54,480
Okay I want to Chinese rice this whatever.

71
00:05:54,480 --> 00:05:55,080
Right.

72
00:05:55,080 --> 00:06:01,320
So this is the function that is going to tell you okay I got your order.

73
00:06:01,560 --> 00:06:04,680
Your total is going to be $45.

74
00:06:04,680 --> 00:06:12,480
And we are going to send the the order to your place in 30 minutes.

75
00:06:12,480 --> 00:06:13,260
Right.

76
00:06:13,410 --> 00:06:20,280
So you see that in the description of this, uh, second function we describe the function.

77
00:06:20,280 --> 00:06:29,400
And then we, we, we, we say, okay, you have a number of required, uh, parameters item name.

78
00:06:29,400 --> 00:06:35,190
So the name of the product, the quantity, how many of the product do you want and the address we are

79
00:06:35,190 --> 00:06:36,840
going to send the the order.

80
00:06:36,840 --> 00:06:37,410
Right.

81
00:06:37,590 --> 00:06:40,050
So this is the first step.

82
00:06:40,050 --> 00:06:45,150
We need to describe the functions in a way that ChatGPT can understand.

83
00:06:45,150 --> 00:06:45,540
Right.

84
00:06:45,540 --> 00:06:52,860
So just with this function description ChatGPT is able to start operating.

85
00:06:53,280 --> 00:07:03,480
So if you don't have the function configuration, ChatGPT will not be able to proceed and execute the

86
00:07:03,480 --> 00:07:13,950
function, but he will be able to identify which function is the right one for your particular question.

87
00:07:13,950 --> 00:07:21,780
So just with these two function descriptions, ChatGPT is going to be able to say, okay, I need to

88
00:07:21,780 --> 00:07:26,190
use this function, or I need to use this function depending of the question we make.

89
00:07:26,190 --> 00:07:26,880
Let's see.

90
00:07:26,880 --> 00:07:27,810
Let's see that.

91
00:07:28,230 --> 00:07:29,250
So.

92
00:07:30,240 --> 00:07:35,640
Let's see first how we use OpenAI functions with long chain.

93
00:07:36,810 --> 00:07:40,410
In order to do that, we are going to create a chain.

94
00:07:40,530 --> 00:07:44,520
That long chain has predefined for us.

95
00:07:44,700 --> 00:07:50,190
In order to work with OpenAI functions, this is the chain create OpenAI function chain write.

96
00:07:50,190 --> 00:07:53,220
This is the module we are going to use to create this chain.

97
00:07:53,220 --> 00:07:57,270
So in this exercise we create a language model.

98
00:07:57,270 --> 00:07:59,730
We create a prompt template.

99
00:07:59,730 --> 00:08:05,040
And we create a chain with the language model and the prompt uh.

100
00:08:06,960 --> 00:08:16,980
On the prompt template and see here that in this case, this create OpenAI function chain has a special

101
00:08:16,980 --> 00:08:19,170
argument called functions.

102
00:08:19,170 --> 00:08:23,130
So this argument functions is equal to.

103
00:08:24,420 --> 00:08:26,940
These variable functions we have defined here.

104
00:08:26,940 --> 00:08:30,510
We can call this variable whatever we want if we want.

105
00:08:30,510 --> 00:08:38,460
If we call it functions we don't need to write here functions equal to functions because that is assumed.

106
00:08:38,460 --> 00:08:45,720
But if we call this my functions or whatever I don't know like like my car.

107
00:08:47,170 --> 00:08:49,510
Here we are going to say functions equal.

108
00:08:49,510 --> 00:08:49,900
My car.

109
00:08:49,930 --> 00:08:50,320
Okay.

110
00:08:50,320 --> 00:08:51,160
You get it.

111
00:08:51,310 --> 00:08:52,300
So.

112
00:08:53,460 --> 00:08:56,670
When we execute this chain.

113
00:08:57,590 --> 00:08:59,480
And we enter this user input.

114
00:08:59,480 --> 00:08:59,780
Okay.

115
00:08:59,780 --> 00:09:02,780
How much does the job say cost?

116
00:09:02,780 --> 00:09:03,470
Okay.

117
00:09:04,520 --> 00:09:09,770
Since we have here verbose true, we can see what is going on under the hood.

118
00:09:10,130 --> 00:09:13,880
And you see that eh.

119
00:09:14,690 --> 00:09:21,620
The the LM has here the prompt, you know, like okay, I am a chatbot having a conversation with a

120
00:09:21,620 --> 00:09:22,010
human.

121
00:09:22,010 --> 00:09:23,780
This is what the human is telling me.

122
00:09:23,780 --> 00:09:25,790
And this is the conversation history.

123
00:09:25,790 --> 00:09:34,520
And this is the response that right now ChatGPT can provide, which is okay.

124
00:09:34,520 --> 00:09:44,090
For this question, I will need to use the function, get item info, and I will need to pass this argument

125
00:09:44,090 --> 00:09:45,260
to this function.

126
00:09:46,400 --> 00:09:47,660
So how?

127
00:09:48,580 --> 00:09:54,310
Thus ChatGPT know it because it has the function description.

128
00:09:54,580 --> 00:10:02,260
So when we make a question, a ChatGPT is going to say it's going to come here and say, okay, is this

129
00:10:02,260 --> 00:10:06,460
function right for this or is this function right for this?

130
00:10:06,460 --> 00:10:06,760
Right.

131
00:10:06,760 --> 00:10:14,200
Do you remember how long chain operated when we were working with agents and different tools is the

132
00:10:14,200 --> 00:10:15,520
same process, right.

133
00:10:15,520 --> 00:10:20,980
So when we are when we were defining an agent with different tools, we make a question and then the

134
00:10:20,980 --> 00:10:26,200
agent is able to identify which of the tools he needs to to use.

135
00:10:26,200 --> 00:10:32,560
Because we have described the tools right, we have provided ChatGPT with the description of each tool.

136
00:10:32,560 --> 00:10:34,120
So here is the same.

137
00:10:34,480 --> 00:10:43,960
Since ChatGPT knows the function description, he knows what function is the right one to to to to create

138
00:10:43,960 --> 00:10:46,300
a response and how.

139
00:10:47,190 --> 00:10:50,070
He is going to configure this function.

140
00:10:50,070 --> 00:10:55,170
So what are the parameters that he's going to use to execute that function.

141
00:10:55,170 --> 00:10:55,590
Right.

142
00:10:55,590 --> 00:10:59,940
So this is what a ChatGPT is doing here.

143
00:11:00,660 --> 00:11:11,610
So if we continue our conversation and say, I want to order two chop suey to this address, right?

144
00:11:11,610 --> 00:11:16,140
So once I have the information, I say, yeah, okay.

145
00:11:16,140 --> 00:11:21,660
I like we don't have the information yet because the function wasn't run right.

146
00:11:21,660 --> 00:11:29,370
But in case we had it and we follow the conversation with a second question like this, then ChatGPT

147
00:11:29,370 --> 00:11:37,020
is going to identify that in order to respond to this second question, it needs to use the second function,

148
00:11:37,020 --> 00:11:38,790
the function place order.

149
00:11:38,790 --> 00:11:43,170
And he needs to execute this function with this argument.

150
00:11:43,170 --> 00:11:45,210
So the item name is chop suey.

151
00:11:45,240 --> 00:11:49,800
The quantity is two and the address is 321 street.

152
00:11:51,030 --> 00:11:58,590
Okay, so see how powerful it is and the use of OpenAI functions.

153
00:11:58,590 --> 00:12:00,120
What is the problem?

154
00:12:00,750 --> 00:12:05,910
So don't worry, because now we are going to use this with the function definition.

155
00:12:05,910 --> 00:12:08,100
So you are going to see this working.

156
00:12:08,100 --> 00:12:08,970
But.

157
00:12:09,700 --> 00:12:15,910
First of all, I wanted to show you what is the problem with long chain and OpenAI functions.

158
00:12:16,120 --> 00:12:17,980
So if the user.

159
00:12:18,750 --> 00:12:25,680
Behaves as expected if the user makes questions, in this case about Chinese food.

160
00:12:25,710 --> 00:12:27,870
No problem, it is going to work.

161
00:12:28,260 --> 00:12:32,880
But the problem comes when the user goes out of the script.

162
00:12:33,210 --> 00:12:33,930
Right.

163
00:12:33,930 --> 00:12:40,410
So let's say that the user asks how old did my grandfather get?

164
00:12:40,440 --> 00:12:41,910
How well is my grandfather?

165
00:12:42,930 --> 00:12:44,010
So.

166
00:12:44,810 --> 00:12:51,500
In this case, the application is going to break and it's going to show an error message and it's going

167
00:12:51,500 --> 00:12:52,220
to stop.

168
00:12:52,550 --> 00:12:54,380
So this is a problem.

169
00:12:54,380 --> 00:13:00,680
And if you have this application in production, your customer is going to have a bad customer experience.

170
00:13:00,680 --> 00:13:05,300
You don't want a an application to break.

171
00:13:05,300 --> 00:13:13,430
In this case you want to tell the user, I don't have this information or I only I am only allowed to

172
00:13:13,430 --> 00:13:16,670
speak about Chinese food or whatever you don't want to break.

173
00:13:17,000 --> 00:13:23,390
You don't want your application to break if a the user goes out out of script, right?

174
00:13:23,390 --> 00:13:28,820
So how probably long chain is going to solve this.

175
00:13:29,300 --> 00:13:38,210
But if long chain doesn't solve it, you have the option to use OpenAI API directly.

176
00:13:39,430 --> 00:13:48,730
So here you you see how to create a chat model with the OpenAI API.

177
00:13:49,150 --> 00:13:49,480
Right.

178
00:13:49,480 --> 00:13:54,370
So here we are defining a chat model.

179
00:13:54,370 --> 00:14:04,150
So a different a way a different chat a and we are including here the functions okay.

180
00:14:04,150 --> 00:14:08,830
So this chat is aware of the function descriptions we have provide.

181
00:14:09,250 --> 00:14:15,490
So if we make the same questions to this chat we are going to have the same answers.

182
00:14:15,490 --> 00:14:20,140
Except if we go out of script.

183
00:14:21,060 --> 00:14:24,030
This application is not going to break.

184
00:14:26,150 --> 00:14:28,580
So this is a interesting.

185
00:14:28,580 --> 00:14:30,410
And if we.

186
00:14:32,300 --> 00:14:43,460
If we fill this application with the two elements that are a out, which is first the database.

187
00:14:43,460 --> 00:14:49,160
So we need to know what are the items we have in the menu, what are the prices ingredients.

188
00:14:49,730 --> 00:14:52,340
And second the function definition.

189
00:14:52,460 --> 00:14:55,850
Here we are not going to use a real database.

190
00:14:55,850 --> 00:15:01,040
We we don't want to connect with too much work for this exercise.

191
00:15:01,040 --> 00:15:07,940
So instead of doing that we are going to fake a database with a Python dictionary.

192
00:15:07,940 --> 00:15:08,570
Right.

193
00:15:08,900 --> 00:15:14,060
But for the application is going to be like this is a database, right.

194
00:15:14,630 --> 00:15:19,070
So once we have that we can create the functions.

195
00:15:19,070 --> 00:15:27,530
And this functions a that are going to use the fake database as you, as you see here are going to uh

196
00:15:27,530 --> 00:15:29,060
behave as we expect.

197
00:15:29,060 --> 00:15:38,630
So the get item info function is going to give us information about a product and the place order is

198
00:15:38,630 --> 00:15:42,890
going to, uh, manage, uh, the order.

199
00:15:42,890 --> 00:15:43,580
Right.

200
00:15:43,670 --> 00:15:53,720
So if we, uh, use these uh, functions with the new, uh, chat we have created with the OpenAI API,

201
00:15:53,720 --> 00:16:00,410
we can see that we are able to manage the whole order process.

202
00:16:00,410 --> 00:16:04,880
You can see that we can tell, okay, your order was placed successfully.

203
00:16:04,880 --> 00:16:08,930
Your order ID is one, and your total price is $30.

204
00:16:08,930 --> 00:16:12,470
Because you have asked for two jobs.

205
00:16:12,470 --> 00:16:17,120
We and we know that chopsuey.

206
00:16:18,450 --> 00:16:20,580
Is $15 each.

207
00:16:20,580 --> 00:16:24,990
So since you have two units, it's going to be $30.

208
00:16:25,290 --> 00:16:25,890
All right.

209
00:16:25,890 --> 00:16:33,120
So here you have a working code for this, uh, application.

210
00:16:33,120 --> 00:16:42,330
You can also improve this app making it dynamic so the LM can continue the conversation after the order

211
00:16:42,330 --> 00:16:43,740
has been placed.

212
00:16:43,770 --> 00:16:44,220
Right.

213
00:16:44,220 --> 00:16:52,140
So here you have a little bit more detail a code in order to achieve that.

214
00:16:52,140 --> 00:16:54,360
And then you can try.

215
00:16:55,210 --> 00:17:04,900
And see that if a type of food is not in the menu, for example, the chatbot is going is going to respond

216
00:17:05,050 --> 00:17:07,690
appropriately in an appropriate way.

217
00:17:07,690 --> 00:17:16,030
And also, if you ask an off topic question, the chatbot is also going to act to respond in a in a

218
00:17:16,030 --> 00:17:19,030
in a appropriate way as well.

219
00:17:19,030 --> 00:17:19,510
Okay.

220
00:17:19,510 --> 00:17:25,450
So just a stay with the main concepts.

221
00:17:25,960 --> 00:17:29,890
Uh, very important to understand that.

222
00:17:30,470 --> 00:17:40,310
The use of OpenAI functions with LM applications opens the door to a whole universe of possibilities.

223
00:17:40,610 --> 00:17:44,300
So very important for you to understand right now.

224
00:17:44,300 --> 00:17:47,600
What is the status of long chain about this?

225
00:17:47,630 --> 00:17:53,990
What is the alternative of using directly the OpenAI API?

226
00:17:53,990 --> 00:18:00,680
And as always, to understand that you can go to the launching documentation and in this case to the

227
00:18:00,680 --> 00:18:07,670
OpenAI API documentation in order to explore more, to experiment more, and to know more about this

228
00:18:07,970 --> 00:18:11,990
a depending of your particular necessities.

229
00:18:11,990 --> 00:18:21,230
But this is absolutely one of the most interesting and powerful, uh, ways we have with LM applications.

