1 00:00:00,700 --> 00:00:04,230 In the previous video, we talked about simple chains. 2 00:00:04,900 --> 00:00:09,450 They are easy to create and use, but they are limited in what they can do. 3 00:00:11,500 --> 00:00:16,750 For more complex tasks, it is often necessary to use sequential chains. 4 00:00:17,680 --> 00:00:21,430 In this video, we will discuss sequential chains. 5 00:00:24,130 --> 00:00:27,140 With sequential chains, you can make a 6 00:00:27,150 --> 00:00:29,700 series of calls to one or more LLMs. 7 00:00:29,710 --> 00:00:33,520 You can take the output from one chain 8 00:00:33,530 --> 00:00:36,780 and use it as input to another chain. 9 00:00:39,030 --> 00:00:41,400 There are two types of sequential chains, 10 00:00:41,970 --> 00:00:47,240 simple sequential chains and the more general form of sequential chains. 11 00:00:48,270 --> 00:00:54,680 The simple sequential chain represents a series of chains where each individual 12 00:00:54,690 --> 00:01:01,660 chain has a single input and a single output, and the output of one step is 13 00:01:01,670 --> 00:01:03,620 used as input to the next. 14 00:01:05,390 --> 00:01:08,860 For example, you could create a simple 15 00:01:08,870 --> 00:01:16,120 sequential chain that first asks a question to an LLM, then takes the answer 16 00:01:16,130 --> 00:01:22,960 from the first LLM and uses it as input to a second LLM to get a more detailed answer. 17 00:01:23,910 --> 00:01:27,140 Let's see how to create and use sequential chains. 18 00:01:28,110 --> 00:01:36,280 I am importing all the required libraries and from lengthchain .chains, I am also 19 00:01:36,290 --> 00:01:39,620 importing the SimpleSequentialChain class. 20 00:01:40,750 --> 00:01:45,100 I am defining the first LLM which will 21 00:01:45,110 --> 00:01:47,040 use GPT 3 .5 turbo. 22 00:01:48,010 --> 00:01:54,180 Shut open AI, model name equals GPT 3 .5 23 00:01:54,190 --> 00:02:01,310 turbo and the temperature equals 1 .2. 24 00:02:06,730 --> 00:02:10,180 And the prompt template1 equals 25 00:02:10,190 --> 00:02:23,510 promptTemplate .fromTemplate and the argument template equals, you are an 26 00:02:23,520 --> 00:02:29,530 experienced scientist and Python programmer, write a function that implements the 27 00:02:29,540 --> 00:02:35,830 concept of and concept in curly braces, concept is the dynamic part of the prompt. 28 00:02:37,410 --> 00:02:47,720 I am creating the first chain, chain1 equals LLMchain of LLM equals LLM1 and 29 00:02:47,730 --> 00:02:51,260 prompt equals promptTemplate1. 30 00:02:55,840 --> 00:03:00,570 I am defining a second LLM which will use 31 00:03:00,580 --> 00:03:11,880 the GPT 4 turbo model, GPT 4 turbo preview. 32 00:03:13,050 --> 00:03:17,300 Note that at this moment, GPT 4 turbo is 33 00:03:17,310 --> 00:03:19,500 available only to paid plans. 34 00:03:20,590 --> 00:03:23,940 Use another model if you use the free plan. 35 00:03:25,210 --> 00:03:29,380 I will use a lower temperature for a more precise response. 36 00:03:30,370 --> 00:03:45,680 I am defining the second prompt template, promptTemplate .fromTemplate and the 37 00:03:45,690 --> 00:03:48,560 argument will be template equals and the prompt. 38 00:03:50,650 --> 00:03:54,380 Given the Python function and function in 39 00:03:54,390 --> 00:03:58,080 curly braces, describe it as detailed as possible. 40 00:03:59,730 --> 00:04:02,120 Maybe it is a good idea to switch the 41 00:04:02,130 --> 00:04:03,260 temperatures around. 42 00:04:04,070 --> 00:04:11,480 I will use 0 .5 for the first LLM and 1 43 00:04:11,490 --> 00:04:13,980 .2 for the second one. 44 00:04:13,990 --> 00:04:19,680 I want a precise and exact function, but 45 00:04:19,690 --> 00:04:21,580 a creative description of it. 46 00:04:24,950 --> 00:04:27,440 And the second chain, chain2 equals 47 00:04:27,450 --> 00:04:36,660 LLMchain of LLM equals LLM2 and prompt equals promptTemplate2. 48 00:04:39,650 --> 00:04:45,700 Now, I am defining a sequential chain using the two chains above. 49 00:04:47,490 --> 00:04:55,600 The second chain takes the output which is a function from the first chain as input. 50 00:05:00,270 --> 00:05:11,440 Overall chain equals simple sequential chain of chains equals and the list of chains. 51 00:05:13,710 --> 00:05:17,860 And I am also adding verbose equals to. 52 00:05:20,830 --> 00:05:24,500 I have mistyped two keywords, exprompt 53 00:05:24,510 --> 00:05:26,680 and verbose. 54 00:05:27,510 --> 00:05:30,820 Sorry, I am running the overall chain 55 00:05:30,830 --> 00:05:37,060 specifying only the input variable for the first chain, output equals 56 00:05:37,070 --> 00:05:43,560 overallchain .invoke and a data science concept, linear regression. 57 00:05:46,210 --> 00:05:55,080 Take a look at the debug information that is being displayed. 58 00:05:55,790 --> 00:05:59,900 You can see each prompt template and how the chains are invoked. 59 00:06:43,890 --> 00:06:48,160 Now you know what chains are and what they can do. 60 00:06:48,810 --> 00:06:55,460 You also learned that there are two types of chains, simple chains and sequential chains. 61 00:06:55,470 --> 00:07:01,880 The type of chain you use will depend on the task you are trying to accomplish. 62 00:07:02,710 --> 00:07:07,960 If you are trying to do a simple task, a simple chain might be enough, but if you 63 00:07:07,970 --> 00:07:12,720 are trying to do a complex task, a sequential chain is what you need.