So in this basic app, we are going to, uh, make questions to a very big document.
We have in our organization, very big private document.
We have in our organization.
Let's say we have a 10,000 pages document in our organization, and we want to know little details about
its content.
Or let's say, for example, we want to learn about the Bible.
Right?
And we want to know, tell me the story of job, you know, or tell me whatever.
Right.
So, eh, uh, we can do this with long chain in a very few lines.
And in short, what we are going to do is we are going to create a basic lag application.
If you remember, the lag technique is one of the core techniques we use in order to create a LM application.
So what are we going to do.
The problem we are going to solve is again the context window window limit.
Let's say we want to uh make questions in natural language about a big book, let's say the Bible.
We cannot do that using ChatGPT because we have a limitation with the context window.
So the solution we are going to use is we are going to create a basic lag application.
We are going to split the document into small fragments.
We are going to convert convert those fragments into numbers.
If you remember we call this fragments embeddings.
We are going to load the embeddings into a vector database.
And finally we are going to create a retrieval system using a predefined long chain chain.
So it will be five steps.
We will first load the text document.
Then we will split the document into chunks.
We will convert the chunks into embeddings.
We will load the embeddings into the vector database.
And we will use finally a chain to retrieve the data.
So as you can see here in the code, the initial part is as usual.
Uh, an invocation to the dot env files.
We are going to call the dot m file where we have our OpenAI key.
Once we have that ready, we are going to create an instance of, uh, the OpenAI LM.
As you see here, we are not going to, uh, configure any temperature.
That means that the default temperature is going to be applied.
And the default temperature, as you know, is zero.
So no, creativity is going to be a very, uh, straightforward LM, uh, model for us.
So no, no hallucinations or we are going to minimize the risk of hallucination.
So the first thing we do is load the text file, in this case, in order to load the text file or the
book, we can we can call it a book.
We are going to use what lang chain calls document loaders.
In this case it's a text loader and it's a CC.
As you see here, you apply the text loader to the path of the the file where the text file is and applying
the load function, you have the document, uh, with the article content.
Once you have that, uh, you can check the type of this new variable document and see that.
Now this variable is list.
If you remember in the previous chapter, the article was created in a string format, uh, with a different,
uh, way.
And with this way, what we are creating is a list, a type of, uh, document, a type of variable,
uh, list.
Then if we check the length, we have a length of one.
If we see, uh, a little bit, uh, deeper, we can see that this list have some interesting, uh,
parts, like, for example, metadata and this metadata part of this document, uh, what it is storing
right now is just the source where we have the path of the, of the file.
So if we, uh, check the length of the document, we know that we have just one document.
And if we check the number of characters we have in this document, we see that we have 27,000 characters.
Now we are going to use, as in the previous chapter, the text splitter, to divide this document into
different chunks.
We will use a similar procedure.
And applying that text splitter, uh, to the document, we are going to have 12 chunks in this case.
So once we have the 12 chunks remember that.
In this case, we are having chunks of text, right?
And we want to convert to transform these chunks of text in chunks of numbers.
Because as you remember, computers work much better with numbers than with a text.
So in order to convert these chunks of text into numbers, we are going to use a component called OpenAI
embeddings.
With this and with a vector database called files, we are going to do in one operation both things.
First we are going to create a vector database.
And second, in this vector database is going to be a face database that we call store embeddings.
We are going to convert the document chunks into numbers using these embeddings.
Uh, we have uh created here.
So as a result we have a vector database with the document chunks converted into numbers.
Right.
So right now as you know our computer is going to be very fast.
You know, checking this data.
The next step is going to be uh, creating a chain.
We are going to use a predefined line chain chain called retrieval QA.
And we are going to apply that in order to mix our LM with uh, our uh, vector database.
The chain type is going to be a staff, which is one of the most common types of chains.
Don't worry.
We are going to go deeper into this, uh, later in a, in a next chapter.
So now we have a question and answer answering app.
So the this retrieval QA chain has made the job for us.
And right now the only thing we have to do is to execute this chain with whatever question we want to
do.
Uh, about our document.
In this case, we can ask what is this article about?
Describe it in less than 100 words.
What is going to do this, uh, command.
So this command is going to send a, our foundation LM, uh, model in this case ChatGPT.
We are going to send to ChatGPT the, uh, what we have in our question and what we have in our vector
database.
So ChatGPT is going to, uh, search into the vector database for the pieces of content, the chunks
that respond to our question, and it's going to use that content to generate the response.
We can go forward and make as many questions to the document as we want.
So in this very simple application, as you as you have seen, we have been able to solve one very big
problem, which is, uh, making questions, uh, to big documents in our organization or in our company
and have, uh, responses in real time in Mmediately, uh, that can take us, you know, many hours
in a, in a previous, uh, in a previous stage.
Right.
So without this LM application, we will have to dedicate a lot of time, you know, in order to look
for the response we are looking for, let's say we are working with a book of 10,000 pages or a report
or whatever.
Right.
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
a lot of effort.