Langchain PDF App (GUI) | Create a ChatGPT For Your PDF in Python

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
[Music] thank you good morning everyone how is it going today welcome to this amazing tutorial in which I'm going to show you exactly how to build this application that you're seeing in front of you okay let me show you real quick how it works so it's uh it has a graphical user interface of course completely coded in Python and then if you write if you drop right here a PDF I'm dropping in the Constitution of the United States it shows this text input in which you can ask a question about your PDF okay so if I ask has who has the power to veto legislation passed by Congress and I and click enter it will answer me based on my PDF but if I ask a question that has nothing to do with my PDF so for example what is the distance between the Earth and the moon for example and I click enter you will see that it has no idea because it's not information that you can find inside my PDF okay I will show you how to parse this PDF how to divide it into different chunks and also as a bonus I'm going to show you in the end how to track exactly how much money you're spending per um per request okay we're going to be building this with Lang chain and I'm going to be explaining to you a little bit about how launching works and the amazing things that you can do with it okay subscribe if you want more videos like this um I'm gonna be publishing many more videos about launching and yeah let's get right into it all right so the first thing that we're going to be doing is we're going to set up our environment okay so as you can see right here I have created my DOT end file which is the place where we're going to be storing our secret keys in this case it is my open AI API key and my DOT end of example which is the one that is going to be tracked on git because this one right here will actually have our secrets which is the actual API Keys be careful you want you're going to want to name your API environment key exactly like this if you're going to be working with openai because since we're going to be working with Lang chain it it requires your environment variable for the open AI API to be exactly this name otherwise it will not recognize it okay so it's got to be open AI lower dash API low Dash key all right there you go then we're going to have a git ignore right here and for that I'm just going to come right here and I'm going to copy a very standard git ignore so that I not that I'm well covered and then I have my app.py which is the place where we're going to start building our application okay um all right so first things first we're going to want to install our dependencies um the dependencies that we're going to need in this case are line chain first of all pi pdf2 to read our our PDF files we're going to need python.nf to enable our environment variables and we're going to need streamlit to create our graphical user interface okay this was super fast for me because I already have them installed but for you it might take a little bit longer also don't hesitate to install additional dependencies that you might need that I may have forgotten because while you're when you will be running your application you might be running into problems like oh this dependency is not installed or these are the dependency is not installed usually you all you have to do is just read the error message and it will tell you which dependency you're missing all right so there you go um and then just to just to start off with this I'm just going to I'm just going to write this very basic test right here that this line I usually just added every single time I start a Python program which basically just tests that my application is being run directly and not being imported right and then only then will it actually execute the main content of the application so there we go let's see how this looks if I run it I see Hello World all right so there we go my project is set up my API key all I have to do is come right here to my platform.openai.com I have to create a new secret key I'm going to call it PDF chat tutorial I'm just going to copy it from here I'm going to paste it right here save and then if I come right here and I say from 1.10 import load the 10th and then I do I'm gonna have to do load.nf like this and then I'm going to print I'm going to print oh I also I'm also going to need OS import OS I'm gonna I'm gonna be able to access my API key okay this is just to show you how how you will be able to access your API key and this is what langching is going to be doing in the uh behind the scenes so if I do OS Dot get environment uh get an and I ask for open AI open AI API key uh sorry this is not supposed to go inside of here this is supposed to go instead of print there you go now if I save this and I run it again you can see that my API key is being recognized and this file is not being tracked on git so that's very important to keep your secrets secret all right so there you go now that our project is set up we can start building our application um to start uh parsing your PDF files all right so let's do that now all right so now it is time to actually start creating our graphical user interface I'm just going to disable GitHub co-pilot right here just so that you can code along and there you go in order to create our graphical use interface we're going to be using streamlit so I'm going to import streamlit that I already installed and I'm going to download it as imported as St there you go and now we can actually just start setting the page configuration I'm going to say that page title page title is going to be ask your PDF and there we go now let's set a header to ask your PDF I'm just going to add a little Emoji like this there you go and then below this I'm just going to create my input where I'm going to be able to upload my PDF okay so I'm going to do St file uploader and then I'm going to say upload your PDF right here I'm going to say that the type is PDF like that all right now in order to run this I'm going to have to do up um that's just one second I'm going to have to do streamlit I'm gonna do run I'm gonna have to Target this file right here where my application lives now if I click on enter it's going to open my new application and it already looks pretty much like this um so that's pretty good uh although so far it doesn't do anything if I upload my file right here it just shows that it's been uploaded but it doesn't do anything else all right so let's just add a little bit of functionality right here um just very quick before that in case you don't know what is going on streamlit is a very streamlined it's a very very fast and convenient way of building this kind of applications with python as you can see I just added three lines of codes right here three lines of code and it already outputs this with it with a very nice title nice with nice Styles and all so there you go I mean if you want to explore other kinds of components for example right here I just used a file upload component but you can see that they have many many components for you to use so don't hesitate to take a look into that um all right so now it is time to actually start adding some logic to our application and to add our um PDF reader and parser right so let's do that now all right so now what we're going to want to do is to actually take the PDF file and read it because so far we only have the file that we have right here but we don't know what the text inside it is and in order to read it we're going to have to use one library that we imported a moment ago which is PDF PI pdf2 right so I'm going to do from pi pdf2 and I'm going to import this class that is PDF um PDF reader like this and this is the one that is going to allow me to take the text out of my PDF all right so now what I'm going to want to do is I'm going to first test that my PDF file exists so if my PDF is not null if it's not none what I'm going to do is I am going to create a new well a new PDF reader that is going to do this I'm going to PDF reader like this and I'm just going to use the new class that I imported I'm going to initialize it with my PDF file that I that the user uploaded okay so just to be clear right here we are uploading the file right here we are going to extract the text of the file okay once that we have the PDF reader we can actually Loop through the pages of the PDF reader okay because the the reader does not by default allow you to take all of the text it allows you to take the pages and then the text out of each page so first we're going to take we're going to Loop through the pages and then extract the text from there in order to do that we're going to initialize an empty string variable and then we're going to say that for each page in our PDF reader.pages this is going to create a list with all the page elements inside of my PDF reader I'm going to say that the test the text is going to concatenate with page dot extract text which is the method that is going to take the the string text out of our out of our page and then with this text what we're going to do is we're just going to write it in our in our application just to show you what is going on so far okay if I'm not mistaken and if I rerun this thing and if I upload the Constitution again I should see the Constitution text right here there you go seems to be working alright okay so there you go now what we're going to want to do afterwards is to figure out a way on how to look for information inside of this text okay so let's let's get into that all right so we have a problem now remember that we said that we wanted our language model to answer questions depending on the information that we have in the PDF right um I mean normally in something like Chad GPT which works with the GPT uh 3.5 model um normally in charge GPT you would just give it the text and then just ask the question uh underneath right so you'll send you will send it something like this paragraph and then you say what is um the I don't know just write ask something about that paragraph right and the problem right here is that this is just too much text we cannot feed this enormous quantity of text to a language model and expect it to understand it in one go so what we're going to want to do is we're going to split the text into similar sized chunks and then look inside those chunks to see which chunks contain the information corresponding to our question and then feed those chunks to the language model okay um I found this article in in the internet from this guy or girl called Penny's mindhack and it's got this very nice diagram which I upscaled for you right here so shout out to to him and it's a very nice diagram and it makes it super easy to understand what is going on right here so basically we have I mean he calls it the book but in our case it's just a PDF and we're going to extract the content and then I mean this is the complete process of what is going on in in the back of our application okay so first we take our PDF we extract the content just like we did this is the content that we have right here and then we're going to split it into chunks all right so we're going to take this text and we're going to split it so this is going to be the first chunk then this is going to be the second chunk this is going to be the third chunk Etc and then each of those chunks we're going to convert them into embeddings which are basically just a vector representations of your text what does that mean basically you can see it as a list of numbers that contains the meaning of your text and I mean that's the most basic explanation for it but it's just a v a number representation of the meaning of your text and this way these vectors are going to be stored in your knowledge base and then when the user comes here and asks a question the question is going to be embedded using the same embedding technique that we used for the text chunks and that is going to allow us to perform a semantic search which is going to find the vectors right here that are similar to our Vector of our question and this will allow us to find the chunks that actually contain the information that we need and that is those chunks that we're going to feed to our language model and it is like that that we're actually going to get an answer in the end all right so this is basically how the entire application is going to is going to work in the back while we just ask a question right here so right now what we're going to do is going to divide this text into into chunks all right and we're going to do that using Lang chain all right there we go so now what we're going to want to do is we're going to divide our text into chunks and we're going to be using this library from um this I'm sorry this function from Lang chain right so first of all I'm going to do from Lang chain thought text splitter I'm going to import character text splitter all right and it is this one right here that remember all still inside of my if PDF actually I'm going to modularize it in a moment but right here I'm going to say extract I mean split split into chunks there you go and now let's actually split it into chunks um we're going to first have to create our text splitter like this and we're going to use our character text splitter class that we imported before sorry I mentioned that it was a function before but it's actually a class and this object takes different and this object has different properties the first one is going to be the separator which is the the separator that is going to define a new line basically so I mean in our PDF a single line break is just going to be using a new line and then we have the chunk size chunk size like this and this one is going to be a thousand characters okay this basically means that of all of this text we're going to take the first thousand characters and then the second thousand characters and then the third thousand characters Etc okay and then we have another thing called chunk overlap okay and this one I'm going to set it to 200. what does the chunk cover like me overlap mean well it basically means that if you come here and your chunk size is a thousand and your thousand ends somewhere like here in like in the middle of a sentence then the next chunk is going to start 200 I mean like every time the next chunk is going to start 200 characters before this one so this is the first chunk and the second chunk is going to be something like this and the third chunk is going to be something like this so it basically just allows you to get more context into each chunk alright and not split ideas in the middle and then the length function length the length function that we're going to use to measure the length of our chunks is going to be Python's basic function length there you go and then once we have our text splitter initialized we can just run it to create our chunks like this text splitter and we're going to say that we're going to split text and we're just going to pass in the whole Corpus of text that we extracted from our PDF which is currently stored in our text variable right here and now what I'm going to do is I'm going to St write the chunks so that you can see what this looks like right so I'm saving coming back here I'm going to going to refresh the page and once again I'm going to upload my Constitution and there you go here we have that the first channel I mean here as you can see we have an array and you can see that the first chunk goes all the way to qualify I don't know but then the second one starts at section two as you can see section two so I mean it basically just makes it easier to to have context I mean this is this is the overlap in work all right so these are the 200 characters that we said that we wanted the overlap full and these are the chunks that we're going to use to create our embeddings and to look for the actual information um later all right so there you go now that we have our chunks let's just convert them into embeddings all right there we go so now as you can see we have our chunks and we're going to convert them into embeddings so basically we are right here we already had our text we already extracted our content we divided it into different chunks and now what we want to do is we want to convert each of these chunks into embeddings and turn and take this all of these embeddings and make them all of our knowledge base which is going to be a document object on which we're going to be able to run a similarity I mean a semantic search to find the chunks that are actually relevant to our question okay so let me show you how to do that um the first thing that we're going to want to do is we're going to want to import from blank chain dot embeddings dot open AI we're going to import the openai embeddings like that okay I mean you are importing them from Lang chain but they come from open AI this is just a wrapper okay um so right here what we're going to do is we're going to say um that our embeddings is going to be equal to our open AI embeddings like that I'm just going to add a comment right here um create and bad things there you go and now with these embeddings we can actually create our object that is going to be able on which we're going to be able to search right and in order to search on this document we're going to use this f a i s s files which is the Facebook AI similarity search Library okay it's basically just a library developed by Facebook that allows you to perform this this similarity search this semantic search in the knowledge base okay so we're going to have to import that one too and in order to import it we're going to say that also from Lang chain and from Vector stores we're going to import FIS there you go and now we can actually create our document on which we will be able to perform the semantic search so I'm going to say that my document is going to be vise Dot from texts because we're going to create it from chunks of texts and then we're just going to say the first argument we're going to pass is the chunks that we have here and the second one is the embeddings that we're going to use which is the embeddings from open AI okay so yeah this is looking pretty good um let me just change this name right here to knowledge knowledge base because this is actually what we have finished creating okay um we successfully created the knowledge base out of our PDF file let me just show you here in the in the diagram that we showed before where we are exactly at this time um so we already imported our PDF file we extracted the text from that video file and then we split it into several chunks to make it easier for our language model to work with them then each one of those chunks we used openai embeddings to convert them into Vector representation of those same chunks and we used Facebook's semantic search AI to build our knowledge base on which we're going to be able to look for the chunks using the embeddings that are related to the user's question okay so whenever a user asks a question we're going to come to this knowledge base that we created before and we're going to run a semantic search to find the chunks that that are closer to that question so that we can send them to the language model and use that to answer our questions okay so this is where we are so far and this means that we have successfully finished all of this part which is the all of this part now what we're going to do is we're going to start coding this part the user interaction part okay so let's get right into it right all right so what we're going to do now is basically add an input element right here okay and this is where the user is going to be able to ask and their questions okay so the idea is that I'm going to upload a PDF here and then once that PDF is properly uploaded extracted the text split into chunks and created the knowledge base from the embeddings we're going to show a an input a text input labeled ask a question about your PDF there you go and we're going to store this value from this input in a variable called user question like this now if I save it and I refresh this and I upload my file again you can see that it's running I mean that means that it's splitting and embedding and all and now I have my question right here now what I'm going to do now I'm going to say that I'm going to show user input there you go and now if we have if we have a user question what we're going to do with that user question is we're going to look in our knowledge base to see if we find any chunks that could have important information about that question okay so in order to do that we're going to create a new a new element called docs which is going to be the documents in which we're going to I mean the documents that contain important information I mean the chunks that contain information about our our question and we're going to say that we're going to search in our knowledge base we're going to go for similarity search which is a method from the fias class from the Facebook similarity search Ai and then we're going to look for users question there you go and now let's just write the docs to see what we found okay so now if I save this and I refresh this right here and I drag a constitution again there you go now you can see that it's running embedding Etc now if I come here and I re-ask a question and I press enter it's going to perform a similarity search and it's going to find the chunks that are most relevant to this question so according to the Facebook's AI in our knowledge base these are the four chunks that are more likely to contain the information that we need to respond to this question okay and now it is this docs that we are going to use to actually answer the question with our with our language model okay so let's do that now perfect so as we mentioned before what we're going to do now is we're going to use this documents right here which are the chunks that we just retrieved with the similarity search and this question right here to actually answer a question based on those chunks of text all right and Lang chain actually comes with a very convenient chain that is already built for that okay and that one is actually this one right here that they have here question answering for dogs and this is the chain that we're looking at it's called load QA chain it takes a language model as parameter we're going to be using open AI but I mean you can use any other language model if you want you can use a and an open source language model it's I mean this one takes pretty much any language model from the wrappers that they have right here I'm going to publish a crash course on online Link Chain very soon so that you can see what this I mean how this actually works but what I mean is that you have many many language models that you can work with okay but since we're going to be working with openai we're going to just copy this right here and we're going to paste it in here there we go this one since it's just our importing we're just going to put it right here like that now we have as you can see it comes in the Lang chains and question answering module and here we're going to say that this is our language model which we're going to be using and our language model is actually going to be open a i like that okay and also we're gonna have to download it from Lang chain and also I mean of course this is a open AI language model but it comes as a wrapper inside of Langston okay this doesn't mean it's like it comes from launching it's just a wrapper so from launching llms I'm going to import open AI and there we go now right here we have our llm we're passing it right here the chain type stuff that's right and then we can actually run saying that the input documents are going to be the documents that we just retrieved from from our knowledge base and the question that we're going to ask is the user question there you go there you go and now this one actually is going to is going to it's going to return our response like this response there you go um and now I suppose that we can just basically St write our response and there you go do we have any other St writes right here I think that's all right so this should work so now just um just as a reminder what we're doing here is we're catching the question from the input then we are performing a semantic search in our knowledge base which is this thing right here and the docs are the ones that the are the results from that semantic search and then we're using the language model to generate our answer and to do that we're using this blank chain rebuilt chain okay I'm just saying that we're looking at it on the docs user question then showing the response let's just save and come back here to our application refresh and then let's take our hours to Constitution here serum and now running embedding and now we can ask questions about it and it should know the answers right so now let's say what is the minimum age for someone to be elected as senator 30 years all right seems to know but if I ask something that is completely unrelated to the Constitution let's say what is the the age of Paul um Paul McCartney not even sure if that's how it's written all right the question is not related to the context there you go all right so that's already an application that's working pretty good um the thing now is that we want to know how much we're spending per question okay I'm just going to modularize this pretty quick and then let's check how much money we're spending per question let's do that all right you know what I changed my mind I'm not going to show you how to how I modularize this I suppose you can do that on yourselves I'm just going to go directly into the pricing okay and how to monitor how much you're spending per question in order to do that actually land chain comes with a very convenient monitor however it currently only works with open AI but it allows you to see exactly how much money you spend per operation in this case per question answered by the language model okay so here what I'm going to do is I'm going to import from Lang chain thought um callbacks there you go I'm going to import get open AI callback okay and just to show you real quick also here if you go back to open Ai and you come to your platform.openai and you see the usage you will see how much money you have spent per day using their apis okay here in this example I'm as I'm recording this at the same day that I'm uploading it I've spent 90 cents which is quite an expensive budget just for a few tests and also I mean keep in mind that this is way more than what you have seen me do um I have been playing around with it all day so this is definitely this doesn't account only for the test that you've seen me doing the video so what we're going to do now is we're going to take this function from Lang chain and we're going to wrap whenever we're using the open AIS language models okay so I'm going to say with this one right here as callback it's going to say CB scroll back and then inside of it I'm going to run whichever whatever I want to be tracked with um whatever I want to track the price of okay so here I'm going to track how much I'm spending for this generation for executing this chain and then inside of it I'm just going to print the Callback and that's all actually now if I save this I'm going to going to restart streamlit like this there you go if I go back to my application there you go all right refresh actually I'm going to show you this in real time let's see here you have the terminal and here you have the the the application is going to put myself um where I'm gonna move myself here and then we're going to take the Constitution again and upload it there you go and now we can actually ask whichever question we want I'm going to ask how many senators does each state have okay if I click enter we should see exactly how much money we're spending right here okay so let me just zoom a little bit here we go so you have that the answer is two senators um and that operation apparently costed uh around a thousand tokens and it costed me around two cents just for one answer okay which looks pretty good um and then if I ask another question I will get the same thing again like for example let's say how has the power of the video legislation passed by Congress um let's say the person has the power to veto legislation and also around two cents right so this is a very convenient function in order to to track your spending and yeah I mean don't forget to use it um you can probably wrap the entire program in it or entire functions in it so there you go that's how to track spending thank you all right so now you have your application that's working correctly and you know how much exactly how much you're spending per question um and we built it with a graphical user interface all of it using python and I hope that you understand uh the die I mean what's actually going on behind the scenes in this application I hope it was clear don't forget to ask me if you have any questions don't forget to subscribe if you want more videos like this and yeah thank you very much for for watching and I'll see you again next time [Music] thank you [Music]
Info
Channel: Alejandro AO - Software & Ai
Views: 90,656
Rating: undefined out of 5
Keywords: langchain, langchain tutorial, langchain pdf, langchain ai, langchain in python, chatgpt for your own pdf, chatgpt for your own data, chat pdf, LLMs, prompt engineering, Prompt Engineer, GPT-4, chatgpt for pdf files, ChatGPT for PDF, langchain openai, Text Embeddings, langchain demo, openai, vectorstorage, train gpt on documents, train gpt on your data, train openai model, train openai with own data, how to train gpt-3, langchain docs, Nicholas Renotte, Patrick Loeber, streamlit
Id: wUAUdEw5oxM
Channel Id: undefined
Length: 39min 54sec (2394 seconds)
Published: Mon May 01 2023
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.