1
00:00:03,580 --> 00:00:13,330
So in this basic app, we are going to, uh, make questions to a very big document.

2
00:00:13,330 --> 00:00:17,650
We have in our organization, very big private document.

3
00:00:17,650 --> 00:00:19,600
We have in our organization.

4
00:00:19,600 --> 00:00:28,930
Let's say we have a 10,000 pages document in our organization, and we want to know little details about

5
00:00:28,930 --> 00:00:29,710
its content.

6
00:00:29,710 --> 00:00:33,280
Or let's say, for example, we want to learn about the Bible.

7
00:00:33,280 --> 00:00:33,850
Right?

8
00:00:33,850 --> 00:00:39,040
And we want to know, tell me the story of job, you know, or tell me whatever.

9
00:00:39,040 --> 00:00:39,640
Right.

10
00:00:39,640 --> 00:00:46,210
So, eh, uh, we can do this with long chain in a very few lines.

11
00:00:46,210 --> 00:00:52,420
And in short, what we are going to do is we are going to create a basic lag application.

12
00:00:52,420 --> 00:00:59,230
If you remember, the lag technique is one of the core techniques we use in order to create a LM application.

13
00:00:59,230 --> 00:01:01,360
So what are we going to do.

14
00:01:01,360 --> 00:01:05,680
The problem we are going to solve is again the context window window limit.

15
00:01:05,680 --> 00:01:12,280
Let's say we want to uh make questions in natural language about a big book, let's say the Bible.

16
00:01:12,280 --> 00:01:18,640
We cannot do that using ChatGPT because we have a limitation with the context window.

17
00:01:18,790 --> 00:01:24,670
So the solution we are going to use is we are going to create a basic lag application.

18
00:01:24,670 --> 00:01:27,820
We are going to split the document into small fragments.

19
00:01:27,820 --> 00:01:31,630
We are going to convert convert those fragments into numbers.

20
00:01:31,630 --> 00:01:34,540
If you remember we call this fragments embeddings.

21
00:01:34,540 --> 00:01:37,660
We are going to load the embeddings into a vector database.

22
00:01:37,660 --> 00:01:43,930
And finally we are going to create a retrieval system using a predefined long chain chain.

23
00:01:43,930 --> 00:01:46,600
So it will be five steps.

24
00:01:46,600 --> 00:01:49,360
We will first load the text document.

25
00:01:49,660 --> 00:01:53,050
Then we will split the document into chunks.

26
00:01:53,050 --> 00:01:55,720
We will convert the chunks into embeddings.

27
00:01:55,720 --> 00:01:59,020
We will load the embeddings into the vector database.

28
00:01:59,020 --> 00:02:04,000
And we will use finally a chain to retrieve the data.

29
00:02:04,000 --> 00:02:09,039
So as you can see here in the code, the initial part is as usual.

30
00:02:09,039 --> 00:02:12,730
Uh, an invocation to the dot env files.

31
00:02:12,730 --> 00:02:17,620
We are going to call the dot m file where we have our OpenAI key.

32
00:02:18,040 --> 00:02:26,260
Once we have that ready, we are going to create an instance of, uh, the OpenAI LM.

33
00:02:26,500 --> 00:02:32,110
As you see here, we are not going to, uh, configure any temperature.

34
00:02:32,110 --> 00:02:35,410
That means that the default temperature is going to be applied.

35
00:02:35,410 --> 00:02:38,680
And the default temperature, as you know, is zero.

36
00:02:38,680 --> 00:02:47,080
So no, creativity is going to be a very, uh, straightforward LM, uh, model for us.

37
00:02:47,080 --> 00:02:53,560
So no, no hallucinations or we are going to minimize the risk of hallucination.

38
00:02:53,560 --> 00:03:00,130
So the first thing we do is load the text file, in this case, in order to load the text file or the

39
00:03:00,130 --> 00:03:02,860
book, we can we can call it a book.

40
00:03:02,860 --> 00:03:06,880
We are going to use what lang chain calls document loaders.

41
00:03:06,880 --> 00:03:09,910
In this case it's a text loader and it's a CC.

42
00:03:09,910 --> 00:03:19,930
As you see here, you apply the text loader to the path of the the file where the text file is and applying

43
00:03:19,930 --> 00:03:26,320
the load function, you have the document, uh, with the article content.

44
00:03:26,320 --> 00:03:33,250
Once you have that, uh, you can check the type of this new variable document and see that.

45
00:03:33,250 --> 00:03:35,380
Now this variable is list.

46
00:03:35,380 --> 00:03:42,760
If you remember in the previous chapter, the article was created in a string format, uh, with a different,

47
00:03:42,820 --> 00:03:44,050
uh, way.

48
00:03:44,050 --> 00:03:52,960
And with this way, what we are creating is a list, a type of, uh, document, a type of variable,

49
00:03:52,990 --> 00:03:53,950
uh, list.

50
00:03:53,950 --> 00:03:58,300
Then if we check the length, we have a length of one.

51
00:03:58,300 --> 00:04:07,270
If we see, uh, a little bit, uh, deeper, we can see that this list have some interesting, uh,

52
00:04:07,270 --> 00:04:16,360
parts, like, for example, metadata and this metadata part of this document, uh, what it is storing

53
00:04:16,360 --> 00:04:21,940
right now is just the source where we have the path of the, of the file.

54
00:04:22,360 --> 00:04:28,390
So if we, uh, check the length of the document, we know that we have just one document.

55
00:04:28,390 --> 00:04:37,390
And if we check the number of characters we have in this document, we see that we have 27,000 characters.

56
00:04:38,620 --> 00:04:46,480
Now we are going to use, as in the previous chapter, the text splitter, to divide this document into

57
00:04:46,480 --> 00:04:47,560
different chunks.

58
00:04:47,560 --> 00:04:49,900
We will use a similar procedure.

59
00:04:49,900 --> 00:04:59,530
And applying that text splitter, uh, to the document, we are going to have 12 chunks in this case.

60
00:04:59,530 --> 00:05:03,010
So once we have the 12 chunks remember that.

61
00:05:03,220 --> 00:05:06,760
In this case, we are having chunks of text, right?

62
00:05:06,760 --> 00:05:12,580
And we want to convert to transform these chunks of text in chunks of numbers.

63
00:05:12,580 --> 00:05:20,680
Because as you remember, computers work much better with numbers than with a text.

64
00:05:20,680 --> 00:05:31,060
So in order to convert these chunks of text into numbers, we are going to use a component called OpenAI

65
00:05:31,060 --> 00:05:32,170
embeddings.

66
00:05:32,800 --> 00:05:42,430
With this and with a vector database called files, we are going to do in one operation both things.

67
00:05:42,430 --> 00:05:46,270
First we are going to create a vector database.

68
00:05:46,270 --> 00:05:53,020
And second, in this vector database is going to be a face database that we call store embeddings.

69
00:05:53,020 --> 00:06:00,670
We are going to convert the document chunks into numbers using these embeddings.

70
00:06:00,670 --> 00:06:03,520
Uh, we have uh created here.

71
00:06:03,520 --> 00:06:12,250
So as a result we have a vector database with the document chunks converted into numbers.

72
00:06:12,250 --> 00:06:12,610
Right.

73
00:06:12,610 --> 00:06:17,560
So right now as you know our computer is going to be very fast.

74
00:06:17,560 --> 00:06:19,810
You know, checking this data.

75
00:06:20,170 --> 00:06:24,220
The next step is going to be uh, creating a chain.

76
00:06:24,220 --> 00:06:29,560
We are going to use a predefined line chain chain called retrieval QA.

77
00:06:29,740 --> 00:06:40,000
And we are going to apply that in order to mix our LM with uh, our uh, vector database.

78
00:06:40,000 --> 00:06:44,890
The chain type is going to be a staff, which is one of the most common types of chains.

79
00:06:44,890 --> 00:06:45,400
Don't worry.

80
00:06:45,400 --> 00:06:50,710
We are going to go deeper into this, uh, later in a, in a next chapter.

81
00:06:50,710 --> 00:06:56,230
So now we have a question and answer answering app.

82
00:06:56,230 --> 00:07:03,400
So the this retrieval QA chain has made the job for us.

83
00:07:03,400 --> 00:07:12,670
And right now the only thing we have to do is to execute this chain with whatever question we want to

84
00:07:12,670 --> 00:07:13,240
do.

85
00:07:13,240 --> 00:07:15,490
Uh, about our document.

86
00:07:15,490 --> 00:07:18,400
In this case, we can ask what is this article about?

87
00:07:18,400 --> 00:07:21,310
Describe it in less than 100 words.

88
00:07:21,310 --> 00:07:24,430
What is going to do this, uh, command.

89
00:07:24,430 --> 00:07:33,370
So this command is going to send a, our foundation LM, uh, model in this case ChatGPT.

90
00:07:33,460 --> 00:07:43,990
We are going to send to ChatGPT the, uh, what we have in our question and what we have in our vector

91
00:07:43,990 --> 00:07:44,980
database.

92
00:07:44,980 --> 00:07:56,020
So ChatGPT is going to, uh, search into the vector database for the pieces of content, the chunks

93
00:07:56,020 --> 00:08:02,920
that respond to our question, and it's going to use that content to generate the response.

94
00:08:03,100 --> 00:08:08,500
We can go forward and make as many questions to the document as we want.

95
00:08:08,500 --> 00:08:18,580
So in this very simple application, as you as you have seen, we have been able to solve one very big

96
00:08:18,580 --> 00:08:28,180
problem, which is, uh, making questions, uh, to big documents in our organization or in our company

97
00:08:28,180 --> 00:08:37,179
and have, uh, responses in real time in Mmediately, uh, that can take us, you know, many hours

98
00:08:37,179 --> 00:08:40,870
in a, in a previous, uh, in a previous stage.

99
00:08:40,870 --> 00:08:41,169
Right.

100
00:08:41,169 --> 00:08:47,830
So without this LM application, we will have to dedicate a lot of time, you know, in order to look

101
00:08:47,830 --> 00:08:55,780
for the response we are looking for, let's say we are working with a book of 10,000 pages or a report

102
00:08:55,780 --> 00:08:56,770
or whatever.

103
00:08:56,770 --> 00:08:57,250
Right.

104
00:08:57,250 --> 00:09:04,690
So you see that Lang Chain is giving us a lot of power that is going to help us save a lot of time and

105
00:09:04,690 --> 00:09:06,070
a lot of effort.

