1
00:00:03,670 --> 00:00:05,020
What are chains?

2
00:00:05,020 --> 00:00:13,780
And, uh, how do we use chains in large chain chains are sequences of operations.

3
00:00:13,780 --> 00:00:15,970
We have two kinds of chains.

4
00:00:16,540 --> 00:00:23,470
We have the predefined chains that long chain offers us, you know, to use.

5
00:00:23,530 --> 00:00:28,330
And we have the customized chains we build ourselves.

6
00:00:29,560 --> 00:00:30,520
So.

7
00:00:32,170 --> 00:00:34,450
In this chapter.

8
00:00:34,510 --> 00:00:41,590
I don't want to spend too much time because we are going to deal with a lot of things, and I do not

9
00:00:41,590 --> 00:00:43,600
want to make it very long.

10
00:00:43,990 --> 00:00:53,110
My purpose here is that you understand the main concepts, and that after this lesson you are able to

11
00:00:53,110 --> 00:00:59,770
experiment, experiment and play around with the code and also with the land change documentation.

12
00:01:00,750 --> 00:01:07,470
So normally a chain, a customised, a customised, a customised chain like the ones we are going to

13
00:01:07,470 --> 00:01:07,770
build.

14
00:01:07,770 --> 00:01:16,350
In this exercise a combines a language model, a prompt and usually another component, right?

15
00:01:16,350 --> 00:01:25,140
So the concept of chain is instead of doing things separately one by one, we are going to build one

16
00:01:25,140 --> 00:01:29,370
chain that includes all of the different steps.

17
00:01:29,790 --> 00:01:35,730
So in this chapter we are going to build three types of chains.

18
00:01:36,060 --> 00:01:46,800
And in every case we are going to do it in the traditional way and in the new way land chain is testing.

19
00:01:46,800 --> 00:01:56,100
So in the in the last weeks, land chain is testing a new way of doing things with the framework, and

20
00:01:56,100 --> 00:02:00,180
they call it the long chain expression language.

21
00:02:00,180 --> 00:02:06,570
The main purpose of the long chain expression language, the new way of doing things in long chain.

22
00:02:06,570 --> 00:02:09,509
The main purpose is to simplify things.

23
00:02:09,960 --> 00:02:16,320
In my opinion, maybe this goal is not achieved yet.

24
00:02:16,320 --> 00:02:23,370
So in my opinion, right now, long chain expression language is not more simple than the previous way

25
00:02:23,370 --> 00:02:24,450
of doing things.

26
00:02:24,570 --> 00:02:27,870
Long chain is accepting both ways.

27
00:02:27,870 --> 00:02:31,470
So right now we can work one way and the other.

28
00:02:31,650 --> 00:02:41,850
The important thing for me is to a communicate you that we have both ways and that you are able in this

29
00:02:41,850 --> 00:02:48,060
and this exercise and in many others to compare both ways and to experiment with both ways.

30
00:02:48,180 --> 00:02:58,920
So the most, uh, probable thing is that long chain is going to go further and further into the second

31
00:02:58,920 --> 00:03:00,120
new way, right.

32
00:03:00,120 --> 00:03:01,500
The chain expression language.

33
00:03:01,500 --> 00:03:07,890
But this is still, you know, something that is not a 100% sure.

34
00:03:07,890 --> 00:03:14,160
So, you know, it's important, in my opinion, right now to know both ways.

35
00:03:14,160 --> 00:03:18,210
And then let's see what long chain is going to do next.

36
00:03:18,930 --> 00:03:26,250
So the first thing we are going to do in the code here in the in the right side of the screen, as always,

37
00:03:26,250 --> 00:03:31,770
is to connect with our dot m file where we have the credentials and the password.

38
00:03:31,770 --> 00:03:35,610
So we can connect in this case with the API of OpenAI.

39
00:03:35,640 --> 00:03:40,380
So we can use ChatGPT and the tools of OpenAI.

40
00:03:40,380 --> 00:03:50,460
And we will start building a very simple chain, uh, using a module called LM chain from the chains

41
00:03:50,460 --> 00:03:51,900
library of long chain.

42
00:03:52,050 --> 00:03:52,260
Right.

43
00:03:52,260 --> 00:03:58,980
So we are going to import chat OpenAI because we are going to work with a chat model, then chat prompt

44
00:03:58,980 --> 00:04:01,050
template and then LM chain.

45
00:04:01,050 --> 00:04:04,620
The first thing we do is we create an instance of the chat.

46
00:04:05,380 --> 00:04:11,410
And we create a prompt template, in this case a chat prompt template.

47
00:04:11,440 --> 00:04:19,450
What we are going to do in this exercise is to ask ChatGPT for a good name for a company that does something,

48
00:04:19,450 --> 00:04:21,700
what is the company doing?

49
00:04:21,700 --> 00:04:29,620
That is the user input we are going to get so the user can tell us, I want to do, I don't know whatever

50
00:04:29,620 --> 00:04:31,060
product of service, right?

51
00:04:31,060 --> 00:04:38,740
So depending on the product and service the user provide us, ChatGPT is going to invent a name for

52
00:04:38,740 --> 00:04:39,940
the company, right?

53
00:04:40,090 --> 00:04:45,160
So we have the chat model and we have the template.

54
00:04:45,790 --> 00:04:53,800
Until now, we will combine the input of the user with the prompt template and we will create the final

55
00:04:53,800 --> 00:04:54,340
prompt.

56
00:04:54,340 --> 00:05:01,060
And with this final prompt we will, uh, execute, you know, the call to the chat model.

57
00:05:01,060 --> 00:05:01,420
Right.

58
00:05:01,420 --> 00:05:05,020
So remember we do elm parentheses prompt.

59
00:05:05,020 --> 00:05:09,070
And we immediately have the response from the from ChatGPT.

60
00:05:09,070 --> 00:05:09,670
Right.

61
00:05:09,670 --> 00:05:14,290
In this case what we are doing is we are creating a chain.

62
00:05:14,740 --> 00:05:20,500
So instead of calling step by step we are going to call the chain.

63
00:05:20,500 --> 00:05:21,040
Right.

64
00:05:21,040 --> 00:05:31,360
So with this uh function the function dot run, we are we just to, we just have to introduce the user

65
00:05:31,360 --> 00:05:32,230
input.

66
00:05:32,230 --> 00:05:40,810
And the chain uh, understands that this is the user input that has to be in the prompt template and

67
00:05:40,810 --> 00:05:43,480
that has to use the chat model.

68
00:05:43,480 --> 00:05:43,900
Right.

69
00:05:43,900 --> 00:05:51,550
Because in the chain, as you can see here we are uh, defining the language model and the prompt template

70
00:05:51,550 --> 00:05:52,960
we are going to use.

71
00:05:52,960 --> 00:05:53,620
Okay.

72
00:05:54,160 --> 00:05:57,070
So very easy very simple.

73
00:05:57,070 --> 00:05:57,850
This chain.

74
00:05:58,450 --> 00:06:05,320
How do we do this thing with the new, uh, long chain expression language.

75
00:06:06,980 --> 00:06:09,320
We do it this way.

76
00:06:09,320 --> 00:06:12,560
So instead of this way.

77
00:06:13,960 --> 00:06:16,360
We are using this other way.

78
00:06:16,360 --> 00:06:20,770
So this line is called pipe.

79
00:06:20,980 --> 00:06:27,730
And what the pipes are doing are, uh, composing, you know, the chain.

80
00:06:27,730 --> 00:06:32,980
So these pipes are telling, okay, use this prompt template with this model language.

81
00:06:32,980 --> 00:06:35,740
And in this case we have added an output position.

82
00:06:36,160 --> 00:06:36,850
Okay.

83
00:06:37,030 --> 00:06:42,700
Instead of using dot run we are going to use dot invoke.

84
00:06:42,700 --> 00:06:49,570
And you will see that the expression is a little bit different here, but very similar in the way of

85
00:06:49,570 --> 00:06:56,020
doing things in the uh, old way and the new way, at least in concept.

86
00:06:56,020 --> 00:06:56,680
Right.

87
00:06:57,940 --> 00:07:00,430
What about the sequential chain?

88
00:07:01,470 --> 00:07:07,560
So the important thing now is to understand the concept of what is a sequential chain.

89
00:07:07,560 --> 00:07:14,070
So a sequential chain executes a number of chains one after the other.

90
00:07:14,070 --> 00:07:14,610
Right.

91
00:07:14,610 --> 00:07:25,590
So in this case we have designed a simple exercise where we get a text from the user and we translate

92
00:07:25,590 --> 00:07:27,570
this text to Spanish.

93
00:07:27,750 --> 00:07:34,470
And once we have this text translated then we summarize with the next chain.

94
00:07:34,470 --> 00:07:36,300
We summarize this text.

95
00:07:36,300 --> 00:07:38,370
And with the next chain we do another thing.

96
00:07:38,370 --> 00:07:40,140
And with the next thing we do another thing.

97
00:07:40,140 --> 00:07:48,600
And instead of calling each of these chains one after the other, we create one final sequential chain

98
00:07:48,600 --> 00:07:53,340
that is going to do all the chains one by one.

99
00:07:53,340 --> 00:08:02,070
Okay, so as you can see, this requires a new module that we have, uh, imported here, which is the

100
00:08:02,070 --> 00:08:04,320
sequential chain module.

101
00:08:04,320 --> 00:08:15,720
And it has a little bit different, uh, way of expressing things than the ways we are familiar.

102
00:08:15,720 --> 00:08:22,920
So we, we see here this, uh, item chains we see here this item output variable.

103
00:08:22,920 --> 00:08:28,020
So the input variable is just the variable of the first chain in order etc..

104
00:08:28,050 --> 00:08:28,620
Right.

105
00:08:28,830 --> 00:08:33,240
But I think the concept is very is very easy to get.

106
00:08:33,240 --> 00:08:40,200
And the code is just a question of practice experiment and go to launching documentation if you have

107
00:08:40,200 --> 00:08:41,370
any problem or doubt.

108
00:08:41,370 --> 00:08:50,880
So since we have included variables true we can see what, uh, this uh, model language is doing in

109
00:08:50,880 --> 00:08:52,260
the, in the background.

110
00:08:52,260 --> 00:08:57,120
And as you can see here, what you see, you know, the results.

111
00:08:57,120 --> 00:09:00,870
So this is the, the text that we include.

112
00:09:00,870 --> 00:09:03,240
And here we have the Spanish translation.

113
00:09:03,240 --> 00:09:06,660
Here we have the summary, the follow up message etc. etc..

114
00:09:07,320 --> 00:09:13,620
So the new way of doing of doing this thing with the long chain expression language, we have included

115
00:09:13,620 --> 00:09:16,350
here a couple of options.

116
00:09:16,350 --> 00:09:24,660
The first one is with the simpler syntax, uh, we can, uh, use in this way.

117
00:09:24,660 --> 00:09:30,570
We just have the output and then we have a second option, a little bit more sophisticated that, uh,

118
00:09:30,570 --> 00:09:34,470
gets that gets us input and output.

119
00:09:34,470 --> 00:09:35,130
Okay.

120
00:09:35,130 --> 00:09:44,400
So as you can see, we make use of the pipe a couple of different ways, you know, to include the variables

121
00:09:44,400 --> 00:09:48,330
the and the prompts using invoke instead of review.

122
00:09:48,540 --> 00:09:52,020
Uh, look at this way of entering.

123
00:09:52,020 --> 00:09:59,910
You know, the the input, the user input instead of the, the one we, we are used to and see also

124
00:09:59,910 --> 00:10:07,680
here they are a little bit more advanced way of doing things with runnable pass through and see how

125
00:10:07,680 --> 00:10:09,630
it is used in the example.

126
00:10:09,630 --> 00:10:09,990
Okay.

127
00:10:09,990 --> 00:10:17,400
So I'm going to leave you with the code which is a working code for you to start experimenting, start

128
00:10:17,400 --> 00:10:23,910
trying, and also to go to the launching documentation where you will find more documentation about

129
00:10:24,030 --> 00:10:25,950
this sequential chain.

130
00:10:25,950 --> 00:10:27,720
If you are one day interested.

131
00:10:27,720 --> 00:10:33,870
In my experience, the sequential chain and also the router chain are.

132
00:10:34,470 --> 00:10:39,840
Seldom used, so they are not very frequently used.

133
00:10:40,470 --> 00:10:46,680
A but if you find a case where they can be useful for you, you know where to find it.

134
00:10:46,800 --> 00:10:47,490
Right.

135
00:10:47,490 --> 00:10:53,610
So the router chain is a different concept.

136
00:10:53,610 --> 00:10:59,130
So here I think is going to be very easy for you to understand with this example.

137
00:10:59,130 --> 00:11:04,620
What we are trying to do is we are trying to tell ChatGPT that.

138
00:11:05,520 --> 00:11:13,620
It has four hats he can use so he can be a rock, rock and roll expert.

139
00:11:13,620 --> 00:11:19,680
He can be a politics expert, he can be a history expert, or he can be a sports expert.

140
00:11:19,680 --> 00:11:25,980
So we are defining here how to behave in each of these scenarios.

141
00:11:25,980 --> 00:11:36,930
And what we are trying to do is to get ChatGPT to decide which of these hats he has to use in order

142
00:11:36,930 --> 00:11:39,480
to respond to our question.

143
00:11:39,480 --> 00:11:49,920
So if we ask this LLM application about a rock and roll topic, he's going to use the rock and roll

144
00:11:49,920 --> 00:11:52,320
hat in order to respond.

145
00:11:52,320 --> 00:12:02,640
If we make a history question, then ChatGPT will, uh, decide to use this other roll in order to answer

146
00:12:02,640 --> 00:12:03,630
this question.

147
00:12:03,630 --> 00:12:08,220
Okay, so I think the concept is the most important thing to get.

148
00:12:08,220 --> 00:12:10,800
That's the purpose of the outer chain.

149
00:12:10,800 --> 00:12:14,790
So the router or I think is the is the key here.

150
00:12:14,790 --> 00:12:18,120
And here you have how to do it in code.

151
00:12:18,870 --> 00:12:27,540
So the I think the most uh complex thing you will see in the code is this for probably it is going to

152
00:12:27,540 --> 00:12:34,770
be easier to understand if you replace p info with the word item.

153
00:12:34,770 --> 00:12:38,550
So what this is doing is this is looping.

154
00:12:40,060 --> 00:12:40,690
Through.

155
00:12:41,430 --> 00:12:45,660
This, uh, prompt enforce uh, variable.

156
00:12:45,660 --> 00:12:46,200
Right.

157
00:12:46,200 --> 00:12:49,800
And this p info is in fact.

158
00:12:50,780 --> 00:12:51,860
One item.

159
00:12:51,860 --> 00:12:58,490
Okay, so for each of these items, you have to do this loop in order to create the destination chains

160
00:12:58,490 --> 00:12:59,150
okay.

161
00:12:59,480 --> 00:13:06,140
Little bit cumbersome, but I think having the code and the possibility to experiment will be the best

162
00:13:06,140 --> 00:13:08,600
thing for you to master this other way.

163
00:13:08,600 --> 00:13:16,610
And here you have also the a way to do it with long chain expression language.

164
00:13:16,610 --> 00:13:17,240
Okay.

165
00:13:17,240 --> 00:13:24,050
So remember in this case we are going to make a lot of pipes okay.

166
00:13:24,050 --> 00:13:30,080
We are going to use a lot of pipes in the different operations we do okay.

167
00:13:30,080 --> 00:13:35,990
So for me the most important thing in this lessons is to instead of going, you know, detail by detail

168
00:13:35,990 --> 00:13:43,160
is that you understand the concept that, you know, here you have the working code and that you can

169
00:13:43,160 --> 00:13:49,640
refer to the long chain documentation whenever you want to experiment, no more, etc., etc..

170
00:13:49,640 --> 00:13:50,030
Okay.

171
00:13:50,030 --> 00:13:54,110
So this is the important goal for this chapter.

