So in this basic app we are going to summarize a long article.
As you know, one of the main problems of the foundation LMS is that they are limited by what we call
the context window.
And this means that, for example, the free version of ChatGPT cannot handle a text of more than 4097
tokens.
So what if we want to summarize a document longer than that limit?
We are going to use long chain to help the foundation Elm summarize a document that exceeds the content,
the context window limit.
And we are going to do that in four simple steps.
First we are going to load the document.
Then we are going to check its token count.
Then we are going to split it into smaller parts.
And finally we are going to use a predefined line change chain to send the parts, the chunks of the
article to ChatGPT and get a summary of the whole document.
So here we have the code we are going to use for that.
In the initial part of the Jupyter notebook, you will see what we normally have in every notebook.
This is just an invocation a call of our dot env file.
So we have the dot m file in the same folder.
We have this Jupyter notebook, and we are calling this file in order to load the OpenAI API key.
Once we have that A, we are a loading the OpenAI component in order to create an instance of our realm,
and we are going to load the text file.
So we have a text file in a folder called data.
And we are going to load this file and save it in a variable called article which is a variable of type
string.
If we print the first characters of this article, we will see the title and the first paragraph.
Once we have that ready, we can, uh.
Take the number of tokens that this article has with the function.
This is a default function in launching a.
To check the actual number of tokens in this article, and we see that this is more than 6000 tokens,
which as you know, is over the context window limit we have.
So how can we solve this problem?
We are going to solve this problem using a text splitter and a chain.
The text splitter we are going to use from Lang chain is called recursive character text splitter.
And the first thing we do is we configure this text splitter.
So we are going to tell that we want to separate, you know the text by paragraphs.
The the size of our chunk is going to be over 5000.
And the size of the overlap is going to be over 300.
Once we have this configuration we can create our article chunks variable.
Applying to this text splitter, we have configured the function at the default function create documents.
So this is going to uh divide the article in different chunks.
And as you can see, if we print the length of the article chunks, we see that we have eight chunks
of text.
And uh, right now what we are going to use a chain called load summarize chain.
This is a predefined chain in in long chain that is going to help us to create a chain that includes
our LM and uh, the chain tab, which is going to be the summarize uh, summarizing uh, change type.
So once we have the chain configured we apply that chain, uh, to the article chunks.
We have uh, build before.
And once we have that, if we run this, uh, by ChatGPT, we are going to produce the summary of the
whole article.
So basically what we have done with this chain and the execution of this chain, uh, applying the article
chunks, we have sent the eight chunks of the article to the to ChatGPT, and ChatGPT has been receiving
these eight, uh, chunks and has been creating a summary for all of them.
So what what it does is it does a summary of the first one, a summary of the second one, etc., for
the eight chunks.
And then it does a summary of the eight summaries.
So as you see in a very simple application, we have solved one of the main problems that the foundation
LMS has.