1
00:00:03,120 --> 00:00:08,250
So in this basic app we are going to summarize a long article.

2
00:00:08,400 --> 00:00:16,379
As you know, one of the main problems of the foundation LMS is that they are limited by what we call

3
00:00:16,379 --> 00:00:17,820
the context window.

4
00:00:17,820 --> 00:00:27,270
And this means that, for example, the free version of ChatGPT cannot handle a text of more than 4097

5
00:00:27,270 --> 00:00:27,990
tokens.

6
00:00:28,320 --> 00:00:34,230
So what if we want to summarize a document longer than that limit?

7
00:00:35,190 --> 00:00:43,800
We are going to use long chain to help the foundation Elm summarize a document that exceeds the content,

8
00:00:43,800 --> 00:00:45,210
the context window limit.

9
00:00:45,210 --> 00:00:49,080
And we are going to do that in four simple steps.

10
00:00:49,140 --> 00:00:51,450
First we are going to load the document.

11
00:00:51,570 --> 00:00:54,840
Then we are going to check its token count.

12
00:00:54,840 --> 00:00:58,290
Then we are going to split it into smaller parts.

13
00:00:58,290 --> 00:01:04,769
And finally we are going to use a predefined line change chain to send the parts, the chunks of the

14
00:01:04,769 --> 00:01:09,300
article to ChatGPT and get a summary of the whole document.

15
00:01:09,420 --> 00:01:13,050
So here we have the code we are going to use for that.

16
00:01:13,050 --> 00:01:21,060
In the initial part of the Jupyter notebook, you will see what we normally have in every notebook.

17
00:01:21,060 --> 00:01:26,460
This is just an invocation a call of our dot env file.

18
00:01:26,460 --> 00:01:29,730
So we have the dot m file in the same folder.

19
00:01:29,730 --> 00:01:37,890
We have this Jupyter notebook, and we are calling this file in order to load the OpenAI API key.

20
00:01:38,600 --> 00:01:49,250
Once we have that A, we are a loading the OpenAI component in order to create an instance of our realm,

21
00:01:49,250 --> 00:01:52,280
and we are going to load the text file.

22
00:01:52,370 --> 00:01:57,470
So we have a text file in a folder called data.

23
00:01:57,650 --> 00:02:08,960
And we are going to load this file and save it in a variable called article which is a variable of type

24
00:02:08,960 --> 00:02:09,680
string.

25
00:02:10,009 --> 00:02:15,620
If we print the first characters of this article, we will see the title and the first paragraph.

26
00:02:16,220 --> 00:02:20,780
Once we have that ready, we can, uh.

27
00:02:21,930 --> 00:02:26,640
Take the number of tokens that this article has with the function.

28
00:02:26,640 --> 00:02:30,570
This is a default function in launching a.

29
00:02:30,570 --> 00:02:39,840
To check the actual number of tokens in this article, and we see that this is more than 6000 tokens,

30
00:02:39,840 --> 00:02:45,600
which as you know, is over the context window limit we have.

31
00:02:46,020 --> 00:02:48,870
So how can we solve this problem?

32
00:02:48,870 --> 00:02:53,910
We are going to solve this problem using a text splitter and a chain.

33
00:02:54,240 --> 00:03:00,630
The text splitter we are going to use from Lang chain is called recursive character text splitter.

34
00:03:00,630 --> 00:03:05,310
And the first thing we do is we configure this text splitter.

35
00:03:05,310 --> 00:03:11,820
So we are going to tell that we want to separate, you know the text by paragraphs.

36
00:03:11,820 --> 00:03:15,870
The the size of our chunk is going to be over 5000.

37
00:03:15,870 --> 00:03:21,450
And the size of the overlap is going to be over 300.

38
00:03:21,450 --> 00:03:28,080
Once we have this configuration we can create our article chunks variable.

39
00:03:28,080 --> 00:03:35,700
Applying to this text splitter, we have configured the function at the default function create documents.

40
00:03:35,700 --> 00:03:41,850
So this is going to uh divide the article in different chunks.

41
00:03:41,850 --> 00:03:50,370
And as you can see, if we print the length of the article chunks, we see that we have eight chunks

42
00:03:50,370 --> 00:03:51,360
of text.

43
00:03:51,990 --> 00:03:58,860
And uh, right now what we are going to use a chain called load summarize chain.

44
00:03:58,860 --> 00:04:09,300
This is a predefined chain in in long chain that is going to help us to create a chain that includes

45
00:04:09,300 --> 00:04:18,269
our LM and uh, the chain tab, which is going to be the summarize uh, summarizing uh, change type.

46
00:04:18,269 --> 00:04:25,680
So once we have the chain configured we apply that chain, uh, to the article chunks.

47
00:04:25,680 --> 00:04:29,430
We have uh, build before.

48
00:04:29,430 --> 00:04:39,210
And once we have that, if we run this, uh, by ChatGPT, we are going to produce the summary of the

49
00:04:39,210 --> 00:04:40,260
whole article.

50
00:04:40,260 --> 00:04:47,070
So basically what we have done with this chain and the execution of this chain, uh, applying the article

51
00:04:47,070 --> 00:04:55,680
chunks, we have sent the eight chunks of the article to the to ChatGPT, and ChatGPT has been receiving

52
00:04:55,680 --> 00:05:02,460
these eight, uh, chunks and has been creating a summary for all of them.

53
00:05:02,460 --> 00:05:08,130
So what what it does is it does a summary of the first one, a summary of the second one, etc., for

54
00:05:08,130 --> 00:05:08,820
the eight chunks.

55
00:05:08,820 --> 00:05:12,120
And then it does a summary of the eight summaries.

56
00:05:12,570 --> 00:05:21,720
So as you see in a very simple application, we have solved one of the main problems that the foundation

57
00:05:21,720 --> 00:05:23,040
LMS has.

