Introduction to PDF Querying with LangChain
Hello all, my name is Krish Naik and welcome to my YouTube channel. So guys, uh many of you had actually requested me to upload a video related to PDF query using LangChain. So suppose, let's say if you have a PDF which has a lot of text information and you want to use LangChain and OpenAI API itself to query some kind of data, but just by asking questions you will be able to get the output.
So this is where many people had actually requested and probably just in this five minutes tutorial, we'll be able to understand how you can specifically use it. Okay? Now LangChain, if I probably go and see the documentation, there is something called as document loaders. Okay? Now in this document loaders, you know, you will be able to take data from different, different sources. You know, you may be able to take data from a PDF or text file and all those things. And here you have this kind of syntaxes. We probably see that how many different types of document loaders are there, everything you'll be able to check it out over here. So please make sure that you check out this particular page itself with respect to the documentation.
Setting Up the Environment
Now, uh let's go step-by-step. Probably if you follow this particular step, uh querying the data source for the text file also becomes very, very much easy. So quickly, first of all, we go ahead and install these all basic libraries which is specifically require: LangChain, OpenAI, PyPDF2, FAISS-CPU. Now FAISS-CPU is, I'll explain you why this specific library is required. PyPDF2 is a library which will actually help you to read from the PDF file itself. And one more dependency library is for Tiktoken. Okay, so Tiktoken is also one more dependency library which will focus on creating tokens and all. Okay?
Importing Core Components and Their Roles
So once you probably install all these particular libraries, and it will probably take some time, so I've done the installation. Now I'm going to import all these libraries. Say from PyPDF2, I'm going to import PDF reader which will again be responsible for reading the PDF files. From langchain.embeddings.openai I'm using OpenAI embeddings. So OpenAI, you know, also has this embedding things, embedding vectors. So anytime whenever you have any confusion, you can just go and search it. Right? Go and just search for OpenAI embeddings. What are embeddings? OpenAI text embedding measures the relatedness of a text string. Right? Embeddings are commonly used for search, clustering, recommendation, anomaly detection, classification. So OpenAI has provided almost everything that you can probably do. So in this step, uh we have to probably use OpenAI embeddings so that whenever I'm asking any kind of question—like, let's say my PDF over here as an example I'm going to take of the Indian budget, right? So Indian budget, whatever budget is announced in this particular year, with respect to that particular PDF, I'll try to upload over here and what I will do, I'll try to ask questions within that specific PDF and you'll be able to get the answer.
Then the next thing is that I will be importing CharacterTextSplitter. Now again, if you don't know about CharacterTextSplitter, in short, whatever content I basically have inside the PDF, I'm just going to split that into considering some special characters like a new line, and I can also define how much should be the text size. You know, this is specifically done because, uh, whenever I'm using OpenAI embeddings, we have a fixed size of tokens. Okay? And this is a very important step that we really need to do.
Then the next important library is something called as FAISS. And now this is just like a vector database. Uh, you know, whenever you are trying to create an embeddings of the text data that is probably present inside your PDF, we will try to store that in the vector stores. Okay? So just a simple things over here but if any queries you specifically have, just go and search for the terms, this libraries, you know, just search for OpenAI, OpenAI buildings, OpenAI character text split, you will be able to find out all the information like how we have actually searched over here what are embeddings. Okay? So, uh, make sure that you do this, but these four libraries are specifically required. So here are all the four libraries over here that I'm going to import.
Loading and Extracting Text from the PDF
Now over here I have already executed this code of uh getting the OpenAI API key. I don't think so we require SERP API key unless until you are doing a Google search. So over here whatever API key you specifically have, in my previous video, I've already shown you how to put the API key itself.
Now, as suggested, the problem statement is that this is my budget PDF. Okay? And this particular PDF I will try to read it with the help of PDF reader. So just to execute it, PDF reader, I have to just give the path of my budget PDF over here, and then I will probably be able to, uh, read each and every page. How do I read it? For that, I will be importing two libraries from typing import Concatenate. Okay, then what I'm actually going to do, I'm going to enumerate inside the pages, of all the pages of the PDF, I'm going to extract this text and put it inside my variable content. And if content, then raw_text += content. That basically means I am putting all the content inside this particular raw_text. Okay? So once I execute it, and it will probably take time because there are so many different, different pages that are available in that particular PDF. So finally you will be able to see this is what is my raw text, and you'll be able to find out all the information over here and it's quite huge. If I probably open this, it is somewhere around 36 to 38 pages. okay?
Splitting Text and Creating Embeddings
Now the next step as we have imported the library of OpenAI embedding and character text splitter, we will go ahead and actually take this character text splitter and we'll split our entire text based on this particular separator on this chunk size, like what should be my one sentence. One sentence that is 800 chunk size, how much overlap can be done. The next sentence can have an overlap of from the previous sentence of 200 at the last. So that is there and length function is nothing but this length which is an inbuilt function.
So once I do this text splitter dot split text of raw text, then I am going to get this entire text. Okay? So this is an inbuilt function that is present inside character text splitter. Okay? So once we execute this, then this is my total number of text that I am probably able to get. Okay? Again, you can play with this chunk size. Uh, the main thing is that for each and every model that we specifically use, uh there will be a fixed token size. You should not exceed that. I can probably put this thousand also right with respect to that. Then I will go ahead and use this OpenAI embeddings which I already told you, this embeddings whatever is there. And then I will go into say that FAISS from text and this text into embedding. So that basically means what I'm actually going to do is that I'm going to basically put this entire text with respect to this particular embedding and get this entire vectors. Okay? So here is my document search. So if I probably go and execute this, okay, you will be able to see that it will be a LangChain vector stores as as suggested, right? It will just be like this text is actually getting converted into this embedding and getting stored over here.
Building the Question-Answering Chain
Okay, and later on I will try to use a tool. In LangChain also you have this question answering load_qa_chain. And from langchain.llms import OpenAI as usual. I will take this load_qa_chain, use this OpenAI object and I will say chain_type is equal to stuff, so that whenever you try to ask a question it will be able to give you the answer. So this is my chain.
Now all I have to do is that write a query. Now inside this particular PDF let me open this particular PDF so that you will be able to understand. So let's say this is my PDF over here, okay? And let's say I want to ask some specific question. Okay, so let's say over here any question you can basically ask. Okay, what is the vision for Amrit Kaal? I'll just ask this specific question. So let's let's consider this is one of the question over here.
Querying the PDF and Reviewing Results
But before this, let me go ahead and write this and take this particular stuff. So what is the vision for Amrit Kaal?
Now if I execute it, then what is happening? This document search, which has the entire embedding, it is trying to find out the similarity search for this particular query. And then this, we are doing chain dot run where I am giving my input documents and question of this query. So, our vision for Amrit Kaal includes technology-driven and knowledge-based economics. So here you can basically see all the information is basically getting picked up. So whatever things we are querying with respect to the PDF, we are able to get this. Okay?
And one more question was that how much agriculture target will be increased by, right? So, uh, I will just say, I'll just copy and search for this if you want. So over here only somewhere agriculture, related to agriculture also something I saw, uh, quite long back. Let me see, somewhere that question was there and you can ask basic questions. See, the agriculture credit target will be increased to... right? So I have written how much the agriculture target will be increased to? And if I probably execute it, you'll be able to see 20 lakhs crore, right? So this is what, 20 lakh crores is there. And suppose if I say, and what the focus will be, right? When I do this query search, uh, it will probably give the agriculture driver to grow with focus on animal husbandry, dairy and fisheries. The same thing, whatever things are specifically there.
Conclusion and Potential Applications
So I hope you are able to understand this. See at the end of the day, you take any, any, any document source, even, even pandas data frame, you can also take that also. Now if you want that specific example, please do let me know. But if you take this kind of PDFs, a, definitely you are able to do this and this is quite amazing with respect to LangChain. Now you don't have to be dependent on things. Just imagine, uh, if you have your entire financial data, right, in the form of PDFs, let's say the expenditure in the form of PDFs, right? You just need to load those PDF, chat with that, and you will be able to answer this kind of questions, right? So I hope you are able to understand this. Anyhow, I'll be sharing you this entire materials. This was it from my side. I'll see you all in the next video. Have a great day. Thank you. Take care. Bye.