Question and Answer Generation App using LLM: Elevate Your Exam Prep with Generative AI ๐Ÿ“š๐Ÿค–

AI Anytime ยท Beginner ยท๐Ÿง  Large Language Models ยท2y ago

Key Takeaways

This video demonstrates the creation of a question and answer generation app using large language models, specifically OpenAI's GPT-3.5 Turbo model, and various tools such as FastAPI, LangChain, and Vector stores. The app streamlines exam preparation by generating practice questions and answers from uploaded PDF files.

Full Transcript

hello everyone welcome to AI anytime Channel and in today's video we are going to work on a very interesting project okay so so far we have seen that how we can you know retrieve information from document or different kind of data formats like you know your audio video images or any other knowledge basis or Knowledge Management that you have that's what we have done so far you know to to build a conversational interface or a search interface and then we you know ask a query and then we uh we are expecting some output or the response from a large language model that's what we have done so far so we have focused on information retrieval now in this video we are going to you know develop an application you know that that will help us you know generate question answers pair okay so you have a document and I want to generate some questions and respective answers or only the questions depends you know what we are really looking at and this video is weird this video is going to be really helpful for you know a segment of users for example teachers professors in Academia the students and anyone who is preparing for some kind of exams right so they have their study materials reference guides Etc they can you know upload this on the project that we are going to build you can see it on screen as well and then you can generate generate QA pair that's it you will have an Excel sheet or something like that you know so I'm going to do this in a controlled environment on a very small scale but you can extend this further number of documents or how do you want to do it right you can do pretty much everything you want to do with this and this is also going to help if you are looking to you know uh you know generate or I'll not say generator you want to prepare or create a collection of data sets you know to fine-tune a large language model now you have your own code basis if you have your own uh documents and you want to generate QA pair out of it so you can fine tune a large language model this will also help you do that so we are going to you know generate the QA pair in a CSV or an Excel format and then you can convert this in an alpaca format and you can fine tune a large language model lamba 2 for example so you can see the screen here you can it says question answer generator and I will give you this entire template it's just a bootstrap template if you don't know bootstrap it's a it's like a it's been used to develop web websites web apps Etc it's uh I think we are we are using bootstrap 5.3 here the latest one but you can use any other libraries or Frameworks for example react or angular respectively you can also use those if you are comfortable with you can see it says question answer generator here you have an option to upload your PDF file so when I click on upload PDF file I'm gonna upload an sdg PDF now when I upload a hdg PDF let me first so what I mean by SVG PDF so SVG PDF is a document that I have it's it's called sustainable development goals yeah so United Nations united nation has a STG which where defined you know uh this all of this goals 17 goals that you see it starts from no poverty to partnership for the goals and if we have this document you know by the United Nation and you know we are going to just generate some question answer pairs now if you want to if you are working in an ID company or a company which is focused towards sustainability how can when you use this document to generate a question answer for your sustainability module you know we have we go through all these trainings right how can you generate something like this a question answer pair even with not this document you have some other documents for example if you have uh a chapter from your curriculum and as a teacher you want to generate a question answer pair as a student you want to generate a question answer period of summarize answers for your study notes you can do that as well you know after going through this video so this is the document you can see it says around I don't know 24 pages or something now I have set a limit over there I'll show you why are we setting a limit well let me just show you okay so you can upload a PDF but we are only going to consider five or six pages you know we cannot gonna pass all this all together with two large language model because we are going to use open AI API in this case just to build this uh demo project guys you know you can look at large language model I tried large language model open source large language models I tried llama 2 and MPT but I was not getting the accuracy or the kind of responses I was waiting so I was not getting that so I just thought I will create this with open AI because you should have a meaningful output so if you see this upload your PDF file here we have uploaded the PDF file and once I click on generate keyword pair now what it does you can see it over here it will it's load it's buffering right so it's working in the back end all the chains we are using Lankan chain I will explain that when I'm writing The Code by the way so we are reading language and QA Generation chain and it it works with uh it works on top of your document with the help of open AI uh GPD 3.5 turbo model and you will see the uh output very soon here because it has Pages it has to go through all the pages first generate the question list then respective answers through retrieval qhen or something and then merge it together that's what we are doing here guys so this is the application that we are going to build you can see question answer generator let me just show you the term you can see in the terminal it's generating right for all the question and the respective answers all the questions and the respective answers question nine how can we achieve sustainable or something it was right uh let me just go up you know sustainable consumption and production patterns and the answer for it that's what we are going to do in this video guys okay so it will take a little time you know it takes time you can see you can track it in terminal the number of questions that we're generating I have said uh token limits because you know it's it's cost me a bit so I just have put a token limit over there and you can see it's generating it has generated 14 questions the 15th question is going on and now you can see over three over here now this is the document and here we have an option to download the question answer generation period now here I have a single question a single answer and that answer I have used to summarize chain you know I have used summarize chain in line chain a framework for generative AI applications now if you don't want to summarize answer you can just don't use some right changes use lldm chain in that case also when I'm writing this code because I just wanted to create a very neat and clean demo you know the project uh today so you can see the PDF you can see the PDF viewer we have in the left hand side uh on the we are reading HTML PDF we were here you can see the PDF the PDF that you have uploaded and here we have an option to You Know download the Excel or CSV so you can see it's downloaded here qa3csv now when I click on that you can find out all of your queries so we have 15 questions and we have 15 answers and I can read all this you can see the first question is what is the name of the plan created by leaders from 193 countries you know in 2015. So the plan created by leaders from 193 countries in 2015 is called the sustainable development of hdg right how many goals are included so 17 goals are included what are the goal of the undp so the goal of undp is to fulfill the sustainable development goals by the year 2030 The undp Works in nearly 170 countries the people were you know the countries were participated and then we have questions like how can we achieve sustainable consumption and production patterns and you have all that in a very summarized and concise way so you can see right the questions and the answers now we have this data it's of course with 15 rows you cannot find in your large language model better to have an embedding approach in that case but you can generate more questions in number of question and answer pairs you know and you can do whatever you want to do with that you use it in your Academia you use it in your study you use it in your at your workplace you use it to create a curriculum uh where you have question answer in in the last where people have to go through this question and answer to complete the module you use this for fine tuning a Live Language model by creating n number of question answer pairs so you can see it uh very simple neat and clean UI that we have built on bootstrap so this is what we are going to build in this video guys okay we'll write everything you know on the back end we're gonna use fast API of one of my favorite framework uh fast API where I'm going to use fast API you know for backend to create this completely API based and then we have bootstrap here this this will be available on a GitHub repository I'll give the link in the description or you can go to AI any times glitter so without any further delay now let's build this application guys okay uh so now let's start writing some code and this will be a little lengthy guys so if you don't want to follow along along you can you know just go to GitHub and download the code you know but I will suggest that you know you should spend a little time to uh write the code with me maybe you know to understand a bit now I'm going to create a file name called app.pi when I click enter you can see this is the folder structure that I have pretty much if you have worked with fast API flask whatever that you will know this kind of folder structure so we have a static we will keep all our static files now if you see within a static we have two folders Docs and output docs the file that we are uploading will get stored in Docs the PDF file output the CSV that we are generating will get stored in output templates pretty much self-explanatory the index.html the index we are not going to write this HTML file because it will the video will be too lengthy then I'll I'll just explain maybe that what are we doing with this and I'll just write the backend code and this entire project folder will be available on the GitHub repository now let's jump in and start writing some code here guys so app.pi the first thing that we need is fast API so from Fast API you know and these are the requirements txt you know you need language and open AI Pi PDF tick token Etc AIO files and python multi-part you're uploading a file uh on Fast API you need that uvcon as a server you know to run this fast API file that you have ginger to register templating module in fast API right and then Pi pdf2 that's what you need so that will be given so from Fast API import fast API that's what we need first thing first API then we need form and then we need a request so let me just do request we also need response you know if you have worked with fast API I just like Fast API you know if you want to learn anything in Python backend if you want to focus on microservices Etc uh you know you can learn fast API it's really good now I'm gonna use depends http exception to handle some exceptions and then need the status another download send out right so okay this is what we need uh from Fast AP and a couple of other things quickly so from fast APA dot responses we need redirect response because we might redirect it to localhost 8000 right that Swagger specification that we have you know doc read docs redoc Etc so from responses import redirect response and guys if you are not interested in building this application like I don't need it in a fast API just give me the python file just just take the function that I'm gonna write okay you can just put that in a script.pi or something like that and just run it locally and you will get it if you're not interested in the web part of it okay so just ignore that so from Fast API dot responses import redirect response we need static files we need ninja to wasted Etc right so from Fast API dot uh static files I'm using tab 9 as a coding assistant and you can also use it if you want you can also pay ten dollar to get GitHub Co pilot you know now I need from Fast API dot templating so templating and I need Ginger to templates so import Ginger to templates uh I also need encoders so from Fast API dot encoders okay and just need the Json jsonable encoder or something here we go just enable encoder we are done with the fast API thing you guys okay now the line chain uh thanks to highs and Chase for creating this beautiful framework but there's a lot of bugs in production when it comes to Lang chain but I still love Lang chain uh chat models we're gonna use chat models and I'm gonna use import chat open AI so let's get this chat open AI that's what we're gonna need so we need chat open AI from linechain.chat models and we need couple of chains so from line chain dot chains uh import Let's uh get the QA Generation chain so it has a QA Generation chain let me just show you what I'm talking about QA Generation chain now let me also you can see that we are in this let me just activate my conda activate lunch and environment I use Anaconda I have a launcher environment where I have installed all my lunch and like generative AI based thingy let me show you QA generation so I'm gonna write QA Generation chain line chain it has a keyword Generation chain now if you come over come over here you can see QA generation GPD cache read the docs this is a chain that you can also look at it it's extremely powerful guys you know to generate question answer pair you know but anyway that's fine now let's come back on the screen so from Langston dot chains import keyword generation change let's also have retrieval what will we do without retrieval chain guys so retriever I hope I'm writing it correctly in a retrieval key way this is okay retrieval keyword is done I don't know why I feel that I have done a typo here r i e v a l or something okay retrieval keyword now we also need some right chain so let's do some line chain dot uh chains or something what was that line chain dot chains uh dot summarize so change dot summarize and import because we are going to get a summary response for that question that you're gonna have okay so we only return a summarize answer okay not a detailed answer just to save some token time there okay so load summarize the chain we are I think done with the chains also guys so line chain is done okay when I say chain I mean we need Vector stores and also let's write the code there so from line chain dot uh so let's have text splitter and import we're gonna have token text filter in this case I don't know why it's not suggesting me okay okay let's have to open text splitter then we need doc store so from lunch excuse me from Lane chain uh dot doc store dot document I'm gonna use document where I can customize that I can have a complete control on this document module and if I'm using document and I will explain that when I'm writing that particular function why you should use document it really help you control the function you can define a lot of quads within it so from Langston dot doc store dot document import document now let's have some loader so we need Pi pdfloader because only one except Pi PDF here PDF here so from LinkedIn Dot document underscore loaders or something okay so loaders I'm going to have Pi pdf2 or something okay Pi PDF loader I have to install Pi pdf2 so Pi PDF loader I'm gonna have a prompt template so from link chain dot uh prompts weddings you can use chroma as well and now that I'm using fast you know just to build this demos but you can use queue brand you can use Millworks you can use chroma you can use in other numbers of vector stores so from LinkedIn dot Vector stores import uh fast my Facebook uh Facebook it's developed by Facebook from LinkedIn dot Vector stores input fast and okay couple of utilities import EOS then I'm gonna have import Json I'm gonna have import time and I have import UV cone I'm gonna have import AIO files for fast API what else we need CSV so import CSV to save the file and we need PDF reader so let's have from PI PDF just to show that you know PDF reader something like that okay PDF uh reader okay so from PI PDF to import PDF reader here we go now let me just run this guy and see if I'm getting any error on my imports so I'm gonna do here I'm gonna say okay all right python average you know simple something like that okay I didn't get any error it means my all of my imports are successfully imported wow we have done 80 percent of our task okay well this is like 21 lines that are the Imports that we have right but it's it's okay uh let's let's start writing the code guys here so no app because you want to Define that fast API app so we have to initialize a fast API app guys if so this is how we initialize an app when we are using fast API same goes with flask also right so app fast API and you can do you know we can do a lot of documentation within it okay it has a lot of input arguments that you can pass it over here you can see you can pass a summary you can pass a you know a title description version etc etc right so let's let's don't do that so what I'm gonna do here is I'm gonna mount something so app dot Mount because we have to mount the static files so we have a static file over here you can see a folder not file the static folder so what I'm going to do here app.mount and within this I'm gonna write okay slash static okay slash static and then my static files that we have used on top so it's static files okay so it's static files and within that you have to pass the directory name so my directory is nothing so let me just use directory and my directory is static so I'm just going to pass static and you know just I'm gonna give a name here so name and let me just put static okay so I don't know why it's Online extra guys okay let me just see app dot Mount directory static okay you have to close this here okay after directory static so now what I'm gonna do here so static name static this we have to close this and now it's okay now app.mount and then we have to also do it same for templates because we're gonna rely on Ginger to templates the module within fast API so what I'm going to do here templates no templating by the way templates and you can keep any variable if you want I'm gonna use Ninja 2 templates and within this my directory again so directory and my directory name is templates if you are naming something else please make sure that you have to Define all this that's it so we are done with the mounting and templating Etc now let's do OS dot in one okay environment I'm gonna pass and open a API key here and of course you can do it from EnV as well but I'm going to delete this key after this video so a oppos dot in one open AI API key let me just paste my key here okay that's it and you can also pass this API quickly through create after creating an EnV file or you just do a set or export whatever depending on what kind of operating system you are on now I'm gonna write a function first so let's write a function called count PDF pages so I'm going to do count PDF pages and we'll see it later if you want to use this function or not okay so what what it does what this is going to do is it will count your number of pages okay so count PDF Pages I'm gonna just do a try except here first so try and within Triumph right PDF of variable I'm gonna use PDF reader so PDF reader which is just let's define our PDF path so let me just write PDF path here and we'll see how gonna use the PDF part later on then return length of that PDF so just checking the length of that PDF so return length PDF dot pages that's it okay so this will return now let's have an accept exception e guys so accept exception as e or something okay so l e error or whatever okay then print error okay so print error I can just do something like this okay print error and just do write e that's it and just return nothing here okay there's any error then don't return anything now this is our account PDF Pages guys now what I'm going to do here I'm gonna write uh the function for you know uh file processing so let's do that so what I'm going to do here I'm gonna write Define file processing so let me just do file processing so five processing and within this I'm gonna pass my file path so file path and so there will be a file path as an input parameter and here we're gonna write all of our Logic for uh with the help of line chain so let's do that so what I'm going to do first is I'm going to load that file path so I'm going to use loader so loader I'm gonna use Pi PDF loader so let's use Pi PDF loader and if you have directories then if you have one to do it multiple files you can use any other as well depending on you have CSV to your CSV loader okay so then we have data pretty much launching thingy you can get all this in the launching documentation and many of my previous videos okay so loader upload it's loading that file now let's create an empty string here the question Jane there'll be nothing inside this so let's just do that question again now I'm going to say for paid in data okay for page in data I'm gonna say question Jane the variable that we have defined the empty one that which has nothing right now question Jane then you have to keep adding it so question Jane and I'm gonna use page Dot Page content or whatever it is okay page underscore page content okay so this is okay uh now this this is what question J now what we're gonna do is okay let's have a variable called splitter we're gonna split the question Jane So split question let's Define a variable I'm gonna use token text splitter you would have heard about text splitter recursive character text splitter character text splitter then I'm gonna use token text splitter I'm looking at the token level of it token text splitter and I'm gonna use GPD 3.5 turbo here if you can deploy on lure as well and you can use that model as well if you want 3.5 for on Azure you cannot do 0.5 it's GPT 35 turbo GPT hyphen 35 turbo so GPD 3.5 uh turbo here on open AI so Turbo and the model is done let's have a chunk size guys so chunk size keep a bigger chunk size you know so I'm gonna keep a 10 000 chunk size and then I'm gonna have a chunk overlap let's keep our chunk overlap of 200 little lesser and just to quickly build this splitter question token text splitter and now I'm gonna have Chung's underscore one more variable let's call it uh it's gonna hold the chunks of the generated questions so question the chunks question I'm gonna write a splitter question so let's utilize that variable on top so splitter question dot split text and now I'm not getting that function it's pretty text I'm just gonna pass that question Jane inside this that we have defined on line number 43 that's it question then here we go so question now let's have a variable called document uh let's call it document question okay and in document question I'm gonna have I'm gonna use that document okay inside a list so document and Page content let me think page content sticks a text Page content let's give T and then you can write 40 in that chunks 40 in that chunks okay this is the right suggestion so let me just do that now this was for question guys okay the splitter question that we have we have to do it something similar for answer Gene as well the answer generation the splitter for that so let's let me just do write that as well or maybe I'll just copy this on top to save some time so let me just copy this here I'm gonna come back paste it here I'm just gonna replace quests with ants that's it so splitter and Jane now here what we are doing here you can see we have GPT 3.5 turbo same then we have chunk overlap and then we have you know a just for answer generation now we have to do write the similar thing like document answer generation where you have document question then you have to write document and searching so let's now let's do that guys so what I'm gonna do here so let's do document it should not be questioned so let's call it answer gen or something document answer gen I'm gonna use splitter uh answer Gene okay that's right uh gen dot split the same thing that we had earlier for question generation split text I'm gonna just just pass the document question then inside this so let me just see and I'm gonna pass in document question okay so this is on document answer generally is done now let's return the both the theme Here guys for question and for answer so return document question and then return document answer then that's it that's it that's that's what we need okay so this function is completed is the first function you can see the first function is file processing we are processing that file the uploaded file and it's been done I'm getting that sunlight I'm not able to see it properly but anyway now let's define an llm pipeline it's a very sunny day here guys today so Define llm Pipeline and within this I'm going to pass my file path by the way so file path and I'm gonna write the prompts and everything within this pipeline so the first thing that I have to do I have to call that function on top okay so let me just do document question or something whatever you name it guys the variables okay document question and then we have document or document and then I have answer gin or whatever okay and then I use file processing so let's use that so file processing function and it accepts a file path the same file path remains so file path let's keep passing this file path by now and okay now let's write something right so llm question so I'm gonna do here llm question uh pipeline so llm question Pipeline and it's click it's a variable I'm gonna use chat open AI from line chain so let's use that that we have used on top so let me just see if we have imported it if you can say this is what we have imported on line number six and that's what we're gonna call here chat open Ai and let's pass some arguments within it the first thing is temperature as you want to generate a question answer pair I will not have too much of creativity so I like to set the temperature on a lower scale so let's do that so temperature first and I'm gonna do temperature equals 0.3 or something and then we can do 0 also if you want let's use the model here my model name is nothing but GPD 3.5 turbo so GPD 3.5 Turbo okay and now you need some prompt template guys so what I'm gonna do here I'm going to go on my GitHub gist so I'm just gonna go on my GitHub and on GitHub I'm gonna go inside that gist your GIFs and I need couple of prompt templates I already have some prompt templates I'll just copy this for now so prompt template I'll explain that prompt by the way okay so let me just come here on Chrome template and if you see here what I'm doing in this prompt I am saying you are an expert at creating questions I'm telling this to large language model GPS 3.5 turbo that you want to export at creating questions based on coding materials and documentation your goal is to prepare a code or Progressive for a different experiment I will make some changes and then exam encoding test you do this by asking questions about the text below now here the text that we're gonna pass create questions that will prepare the coders or programmers for their test make sure not to lose any important information and then then your question starts coming in so in this prompt I'm gonna make some changes so not for coding you are an export at creating questions based on study materials let's call it study materials and documentation uh let me call it DOC not document study materials and reference reference guides reference guide and your goal is to prepare a student or teacher or something okay so let me just write teacher for their exams and tests you do this by asking questions about the text below create question that will prepare the quota for this prepared will prepare the user so let me just prepare the student or teacher for their test okay this is the first prompt now let's we also have to do the similar for you know uh refined prompt I'll show you what I'm what I mean by refine prompts let me just see it on my screen so prompt template so now let's use that prompt template so for that we're gonna use uh this prompt where it is go from template you can see the line number 12 so let's let's use that guys you know very simple so I'm gonna Define a prompt question let's keep it like a I'm not gonna make this any changes okay so prompt questions so in this prompt questions I'm gonna use from template from line chain from template and I'm gonna pass templates equals template equal prompt template the variable that we have defined on top you can see line number 80 of with the prompt and let's pass an input variable for now because that X that that looks for text that when I use that that's going to be your input variable that will vary so variable and your input variable remains uh it's not input variable it input variables so your input variables is nothing but a text here we go so we have this you know prompt questions and we are working with prompt questions now let's do a refine template then I'm going to copy these guys here okay so or I can just copy it from my gist again I have for refine template I have done this to save time guys you know while creating this video so let's use this refine template quickly so what I'm going to do here I'm going to come here on this write a refine template and in this creating practice question based on study materials and reference guides let me just replace this Ctrl C and I'm going to replace this with this your goal is to help a student or teacher for their exams and let me call this exams by the way here exams coder and for coding test student or teacher for their exams and test we have received some practice questions to a certain extent uh existing and so this is this is correct we have to optional refine the existing question right now this is fine now this looks for existing answer and textbook therefore you're calling it refine template now it's look for existing answer as well so let me do that given the new context we find the original questions in English if the question is not helpful this is fine okay so let's define this refined template guys here so let me call it refine prompt questions and in this defined prompt questions I'm gonna again use prompt template here so in this from template we're gonna pass few things the first thing is input variables and input variables v a r i b l uh variables input variables so let me just do v a r i a b l e s in input variables I'm going to pass existing answer and this existing answer I'm gonna also have text here so let me just do text and after this I'm gonna pass template and in this template I'm gonna use refine template the variable that we have defined on line number 97 okay so this with the prompt guys so we are okay with our prompt also now okay so we have written couple of prompts and you can see we are you know we are okay with the prompt now so let's let's now Define uh a chain again some right chain with this prompt that we have defined in the LM so I'm gonna call it quish uh Jane chain or you can give any variable name so question chain I'm gonna use load underscore summary chain and in this load underscore summary chain I'm gonna pass my llm my llm is nothing but the that we have defined on line number 75 so that that's goes LM question pipeline the first thing that I'm gonna next is chain type it's have stuff or something or let's have a refine for now chain type a lot of other things guys you know it depends we explore on lunch and documentation now chain type then we're gonna use refine here then let's keep let's keep verbose equal to true I want to see that uh in terminal so excuse me it's a Boolean value so verbose equals true let's have question two then we have question prompt so let's pass the question prompt and this is nothing but the this one 9 number 92 prompt questions from questions and then we have question from then we have refine prompt so refined prompt is excuse me refine prompt is nothing but the line number 112 refined prompt questions here we go we are done with our chain now we're gonna use this chain to summarize that guys you know that's why we're using this load some right chain now let's do two okay now we are going good so now we're gonna have is let's run this chain so the first thing is question quiz Jane so let me just use question chain dot run I'm gonna use run here and you can pass that in document question okay document question now we're gonna create embeddings of it so embeddings and embedding is going to be open AI embeddings let me just use it quickly by using ad I embedding zero zero two and we need a vector store so I already have installed Vector store here so let's do that so Vector store and in this Vector store you're gonna have fast so fast Dot from underscore documents so let me just do that fast dot uh and from underscore documents you can see I have a power code but anyway that's fine we are doing it live locally so there's no problem you know so let me just turn on this okay fast from document I'm gonna pass my embeddings so I'm just gonna pass in this embedding when I we are using fast if you want to use chroma nothing will change you just have to install chroma DB PP install chroma DB this word will get changed from flash to chroma that's simple so chroma dot under from underscore document I'm using fast I'm facing some issues with chroma nowadays I'm I'm not using it so Vector store fast Dot from now but fast is not that good guys to be honest you know so I'll suggest you to use a lot of others maybe Millwork for example depends what you want to use okay so Vector stores fast Dot from documents embeddings and that also depends if you want an in-memory Vector store on any Vector database if you have large file or something right so full latency and all so now what I'm gonna also do is llm answer gen and you know in this answer Gene I'm gonna use again chat open Ai and let's pass couple of things here quickly okay uh and I'm I'm making mistakes here guys so a lot of brain fed movement here so if you see line number 129 just passing embeddings but where is that document that we're gonna use let me see no you're gonna pass here fast so let me just to add comma and here I'm gonna use Excuse me document Quest answer then we're gonna use answer we need answer change so document answer gen and emitting now it looks good so excuse me so LM answer Jane and here I'm gonna use the temperature let's lower the temperature even more for temperature excuse me and let's keep it 0.1 as temperature and you can do in the right model again GPT 3.5 Turbo GPT iPhone 3.5 hyphen Turbo this is our answer Jane pipeline guys okay chat open AI now let's have a Quizlet so quest list and then I'm just doing question split you can I split that question by slash n the next line right pretty much python thingy here quest list and questioner split and now let's have a filtered questions list okay so filtered quest list and you're gonna pass element element for element in question what is quest list I don't know why it says Q is it should be q u right I have defined keep if element dot ends with so let's and let's put a check I want all the question to end with a question mark so let's do that one I don't know if that's the right logic but that's what I'm gonna use okay because most of the time this due to some tokens or whatever right it stops in between I don't want to I don't want that to be happen so we can what we can do is we can put a logic here elements dot ends with or something if there is anything ends with let's look for a question mark okay ends with and I'm gonna have or element or element ends with DOT so I it should I it can also end with DOT let me do an ALT D first okay so element dot ends with and then it goes dot okay so here we have some good validation checks you know and now let's have our final answer Generation chain so answer Generation chain and in this answer Generation chain I'm gonna use retrieval QA Dot from underscore chain type so let's use a retrieval chain type excuse me what happened and chain type ISS type is so type okay retrieval QA from chain type the first is your llm and your llm is nothing but the line number 31 llm answer gen chain type stuff excuse me it's better to have refine chain type on top when we use refine you know when we're using some right chain which that's that generates better responses guys so chain type stuff and let's have a retriever where we pass we're gonna pass our you know our embedding thing is there so retriever Vector store dot as underscore retriever isn't it and stamp line is bad okay retriever I don't know if some of the tabernian guys will reach out to me and say Hey you are doing negative something right reviews or something but I don't care okay so vectors underscore store as retriever okay now let's return this guy so what I'm gonna do is return answer Generation chain and then filtered question list list that's it so we are done with uh our uh this guy is l m also okay I have power back okay so I have my power back also so now what I'm gonna do is let's write something or will not write uh a lot of this thingy so what I'm gonna do I'm gonna copy a few of the things from my gisha repository so now let's do that so let me open my gist so I'm just gonna go on my GitHub again and the gist I need one more gist so that's the CSV function excuse me I don't know what I did not sponsor you just okay so I just need gate CSV because we are going to write this in a CSV file the answer that we're gonna generate the question answer so I'll just need that function so what this function is doing it has it takes again a file path and then you just answer Generation chain where you are just using that llm pipeline so let me just go on top and we are utilizing this llm pipeline function here within this get CSV function and our base folder is nothing where we are going to save that CSD file which is static and within that then we have output that's what that's where we're going to save our output file then we are first checking if that's not there then make that directory that's what this piece of code does and then we are going to save that file with the base folder plus keyword or CSV and then with a little bit of file processing in Python with open adding that file writing it with CSV writer which comes with default in Python and I'm just checking that each line for it for each question in question list putting that answer together and just writing it in a question answer column that's what we are doing here guys with return after file now this is this is done so we have written the code now we have written all the functions now the only thing we have to do we have to write the first API code so let's do that quickly couple of functions so first API the first thing that we're gonna do is app get in this we're going to use redirect response you know so diff uh let's use an async function so async Dev index and here I'm gonna just pass request so request excuse me it's not capital I don't know why it's giving me okay request and request is nothing but it's gonna be request and uh async okay hold on and now here I'm just going to use template so return template so templates Dot template response and just pass index HTML that's it so index HTML and I'm gonna also do a dictionary whether I'm going to pass this request to request and this request is nothing but that's the request okay so the first function is done that's an index function to load index.html file the next function we're gonna use a post right so we're going to post the file we're going to upload a file so we're going to write a post method here so app.post and in this course our endpoint will be nothing but that we will call as upload so let's call it upload and let's again have an async function that's why most of the people uses fast API now async Dev and let's call it chat for example you can also do search or something so async Dev chat and here we have to define the structure of our data you know so that will be again request this is okay so request request now you're gonna have a PDF file so let's name it PDF file and this PDF file is nothing but it's gonna have a bytes value so bytes and then the file pretty much self-explanatory in fast AP if you work with fast API then excuse I think I make some mistake fine name and this file name is going to be a string so let's call it HDR and it's gonna take that as a form so form input and three dot that's it okay we are done with this guys this uh parameters in this chat function and our endpoint is nothing but the upload okay the first thing is we're gonna have a base folder that's where we're going to have our file so base folder let's call it static again okay that's called docs that's the folder is base folder Docs and you can also use the same code from here so let's use the same code if not OS path so I'm just going to paste this if if not OS bar e directory base folder then make that folder that's what we are doing here as well you know that make that folder now let's have a PDF file name so PDF file name and this PDF file name is OS okay this is correct base folder and then gonna have a file name here so let's just do a file name that's it so now this variable has the PDF file name you know you can use that file now so let's Now do async with AIO files to we're gonna open that file so open and of course PDF finder self explanatory right that let's say python is so fun to learn as file or F whatever and then just have an await method here so of it F dot right away is the keyword in Python so await F dot right and what I'm gonna do here is I'm gonna pass that PDF file so let me just see okay you have to pass this PDF file that we have passing in the argument and not the file name so just let's do PDF file okay so async with AIO files we're okay with it now let's do a response data so response data and this just enable encoder I'm gonna encode that Json that we're gonna get we're gonna have Json dot dumps let's dump that file response data this is I don't know why it's responded it's also responded I'm gonna write this message here so let me just remove response date excuse me dumps and within that dumps is gonna have a dictionary you're gonna have a message and this message is like success or something so success excuse me success and then PDF file name PDF file name okay that's it so we are okay with it I don't know why did I have an extra okay then we have ah it's not DOT okay PDF file name message success okay cool so we are done with this response data and now just let's call it result equals to response and just passing the respondent I'm gonna just return the result that's it okay so just return the result and one last function we need an analyze or something let's have let's define an endpoint again app dot post app dot post and here I'm gonna write and analyze analyze here an endpoint I'm gonna have async div and let's call it again uh and uh let's pass again request so request excuse me then you're gonna have request request and then PDF file name and form and that's it that's how we Define a form and here we go right that's it and within this I'm just going to write an output file so output underscore file and our output is like use the function okay get CSV the function name I guess let me just go and talk get CSV because we have to download that CSV so I'll put file get CSV passing file name output excuse me I don't know what happened output this will be output underscore file and in this output underscore file we're gonna have our output file okay so we are okay with it and just again result so just again response to return race we have completed our code guys so we written the code each and every line for most of the functions like we have taken couple of functions and the prompt from the GitHub gist that link will be given to you in the description as well if you want the gist separately now now let's run this and see what else let me explain that quickly what I have done here so I'll explain from Top I have imported all the libraries and we are using fast API here okay we have app equals first API we are mounting it our static folder and the templates in templates we have index.html which is going to you know uh where that's where we are showing the results right taking our input and showing the result and giving option to download the file now we have counting it we didn't divide it anywhere but we can we can use it in the last function then we are having file processor file processing loading through Linkedin modules you know using some for both question and answers okay yeah the model and then we have llm pipeline uh in llm pipeline we are defining all the prompt templates refined templates we are using embeddings creating pipelines retrieval QA chain then we have a CSV get CSV function that will help you download the csvs then we have couple of other functions async functions you know in fast API with couple of endpoints like upload and analyze this is what we have written now let's do one thing let's run it and see if we are able to generate the question answers and we were able to download it guys so let me just come down okay everything looks nice so now let's run this so let's open the terminal here and in terminal what we're going to do we are not going to do python appetite again we can do it but we have to set up that UV corn in if name so what you can also do let's do uvcon run ubicon app app is your file name an app video class name so you become app hyphen hyphen reload that's what I'm gonna do here so let's now let's do that it says application startup complete now what we're gonna do is we're gonna just uh so now let's do one thing what I'm gonna do here I'm going to upload localhost 8000 so let's do localhost 8000 and in this localhost 8000 let's run this guys so now you can see we have something called question answer generator where the same thing that we use we have seen in the you have seen in beginning of this video now here we have upload your PDF file so let me just go home and uh dot desktop my project question answer I'm gonna use this sdg now what it does let me open this and you can see all of your chains are being called the change that we have created it's a finished sale for each and you're seeing a question what is the name of plan created by leaders the question you can see question answer and you can track it in your terminal we have verbose equals to true you know in this chain and we are seeing all the question and answer you can take any other document you can try it out so let's see you know once we get the final uh response what is the goal for education in the sdg one of the questions what progress has been made in achieving under gender equality so we have used QA Generation chain of course with other chains like load Samurai chain and retrieval keyway Etc right that's that's what we have used okay in this video so you have used retrieval keyword chain and we are using GPD 3.5 turbo model okay so the logic is that there can be different Logic for this guy you know you first put a prompt that will get all the questions maybe that can be a logic and then start feeding that in a loop to retrieve the information that also is one way of you know performing this uh experiment okay but here in this case we have relied on chains so uh you can see it has generated more than 17 question it seems now okay what are some ways individuals can so support and help achieve and you can see all of our output have been generated and here the PDF that we uploaded Maybe right now what I I think I have to make some changes in the JavaScript on the front end I think it's loading only once the results are being generated but this will be a separate function maybe but we will see that so what we have done we have a file option you know where you can upload a file and then we have this sdgs HD PDF and here you can download the file okay so once you click on download files I'll just click on download So once I click on this you can see qa4.csv now when you click on qa4.csv it opens a CSV file and you can see in the CSV file question and answer what is the name of the plan created by leaders from 193 countries something something 17 questions and 17 respective answers for it fantastic right if you click on one of these questions like what are some ways individuals can show and help achieve the stgs and you can read the answers there are several ways individuals can show support and help achieve the sdgs educate yourself spread awareness volunteer Etc fantastic so this is the result that we have you know uh generated with the help of this app question answer generator now you can take the same app you can download it from clone it rather you know from GitHub repository you can change your API key you can also use Azure open AI here you know to perform this experimentation you can also create data sets question answer pair in a CSV file you can put some instruction also if you want later on with the help of python you know some Logics and you can create a good data set to fine tune a Live Language model you can use it in your Academy as a teacher as a professor as a student you know if you are doing it okay control everything through this application you can extend this further this is what I wanted to do in this video guys to be honest some of you requested to have a question answer generation app you know I think few of my subscribers reach out to me to build something

Original Description

Are you tired of spending hours creating practice questions and answers for your exam preparation? Look no further! This in-depth tutorial introduces an incredible Generative AI-powered solution to streamline your study routine. Building an Automated Question and Answer Generation App, using OpenAI's advanced Large Language Model (LLM), ChatGPT. With the power of FastAPI on the backend, this app offers a seamless and user-friendly experience. Watch as I guide you through the entire process: ๐Ÿ”ธ Uploading your study material in PDF format ๐Ÿ”ธ Exploring how LangChain's cutting-edge language processing capabilities handle the heavy lifting ๐Ÿ”ธ Witnessing the magic of ChatGPT as it generates a CSV file containing a comprehensive set of questions and their accurate answers GitHub: https://github.com/AIAnytime/Question-Answer-Generation-App Langchain Docs: https://docs.langchain.com/docs/ Don't forget to like and subscribe to stay updated on the latest Gen AI-powered learning techniques! #ai #generativeai #python
Watch on YouTube โ†— (saves to browser)
Sign in to unlock AI tutor explanation ยท โšก30

Playlist

Uploads from AI Anytime ยท AI Anytime ยท 0 of 60

โ† Previous Next โ†’
1 Spelling and Grammar Checking Streamlit App: Building Docker Image
Spelling and Grammar Checking Streamlit App: Building Docker Image
AI Anytime
2 Spelling and Grammar Checking Streamlit App: Docker Image and Docker Hub
Spelling and Grammar Checking Streamlit App: Docker Image and Docker Hub
AI Anytime
3 Image Caption Generator: Google Colab and Hugging Face
Image Caption Generator: Google Colab and Hugging Face
AI Anytime
4 Low Code/No Code AI Platform Teachable Machine: Brain MRI Image Classification
Low Code/No Code AI Platform Teachable Machine: Brain MRI Image Classification
AI Anytime
5 Low Code/No Code AI Platform Teachable Machine: Testing the Model
Low Code/No Code AI Platform Teachable Machine: Testing the Model
AI Anytime
6 Low Code/No Code AI Platform: Streamlit App for Brain MRI Image Classification
Low Code/No Code AI Platform: Streamlit App for Brain MRI Image Classification
AI Anytime
7 Readme Generator Streamlit App using ChatGPT
Readme Generator Streamlit App using ChatGPT
AI Anytime
8 Generate Minutes of Meeting (MoM) from Video using ChatGPT: AI as an API
Generate Minutes of Meeting (MoM) from Video using ChatGPT: AI as an API
AI Anytime
9 The Great AI Showdown: ChatGPT vs ChatSonic ๐Ÿ”ฅ
The Great AI Showdown: ChatGPT vs ChatSonic ๐Ÿ”ฅ
AI Anytime
10 Generating Transcripts and News Article with Whisper, GPT-3.5, ChatGPT and Streamlit
Generating Transcripts and News Article with Whisper, GPT-3.5, ChatGPT and Streamlit
AI Anytime
11 Toxicity Classifier using Machine Learning and NLP
Toxicity Classifier using Machine Learning and NLP
AI Anytime
12 Toxicity Classifier API using FastAPI
Toxicity Classifier API using FastAPI
AI Anytime
13 Toxicity Classifier Streamlit App
Toxicity Classifier Streamlit App
AI Anytime
14 Low-Code Insurance Prediction with PyCaret and Streamlit
Low-Code Insurance Prediction with PyCaret and Streamlit
AI Anytime
15 Deploy Streamlit Python Application for Free
Deploy Streamlit Python Application for Free
AI Anytime
16 GPT3 Powered Text Analytics App
GPT3 Powered Text Analytics App
AI Anytime
17 AI Image Generation Streamlit App
AI Image Generation Streamlit App
AI Anytime
18 Streamlit and txtai: Building an Abstractive Summarization App in Python
Streamlit and txtai: Building an Abstractive Summarization App in Python
AI Anytime
19 Building a Topic Modeling and Labeling app with Streamlit
Building a Topic Modeling and Labeling app with Streamlit
AI Anytime
20 The Art of AI: Exploring Midjourney, Dall-E, and Lexica
The Art of AI: Exploring Midjourney, Dall-E, and Lexica
AI Anytime
21 Exploring the latest Large Language Models (LLaMA and Alpaca)
Exploring the latest Large Language Models (LLaMA and Alpaca)
AI Anytime
22 Comparing LLMs like GPT-X, LLaMA, and Alpaca: Analyzing the Perplexity Score
Comparing LLMs like GPT-X, LLaMA, and Alpaca: Analyzing the Perplexity Score
AI Anytime
23 GPT-3 powered Q&A App using Langchain, GPT-Index, and Gradio
GPT-3 powered Q&A App using Langchain, GPT-Index, and Gradio
AI Anytime
24 All things #ai . Latest and greatest in AI. #tech #python #chatgpt #youtubeshorts #shorts #gpt3
All things #ai . Latest and greatest in AI. #tech #python #chatgpt #youtubeshorts #shorts #gpt3
AI Anytime
25 Text-to-Video Generation using a Generative AI Model
Text-to-Video Generation using a Generative AI Model
AI Anytime
26 #ai brand name generator. #artificialintelligence #tech #shorts #youtubeshorts #youtube #chatgpt
#ai brand name generator. #artificialintelligence #tech #shorts #youtubeshorts #youtube #chatgpt
AI Anytime
27 Talking AGI with Sam Altman: A Deepfake Showcase
Talking AGI with Sam Altman: A Deepfake Showcase
AI Anytime
28 A conversation with ChatGPT creator Sam Altman. #tech #technology #ai #shorts #viral
A conversation with ChatGPT creator Sam Altman. #tech #technology #ai #shorts #viral
AI Anytime
29 Get to Know Anthropic's Claude: The Ultimate ChatGPT Competitor
Get to Know Anthropic's Claude: The Ultimate ChatGPT Competitor
AI Anytime
30 #shorts #chatgpt #python #datascience #tech #coding
#shorts #chatgpt #python #datascience #tech #coding
AI Anytime
31 Recipe Generator App from Cooking Videos using Whisper and ChatGPT
Recipe Generator App from Cooking Videos using Whisper and ChatGPT
AI Anytime
32 Segment Anything Model by Meta AI: An Image Segmentation Model
Segment Anything Model by Meta AI: An Image Segmentation Model
AI Anytime
33 One of the best #ai #books based on #tensorflow. #tech #coding #shorts #chatgpt #machinelearning
One of the best #ai #books based on #tensorflow. #tech #coding #shorts #chatgpt #machinelearning
AI Anytime
34 Music Generation using Mubert #ai . #music #shorts #youtubeshorts #chatgpt #generativeai
Music Generation using Mubert #ai . #music #shorts #youtubeshorts #chatgpt #generativeai
AI Anytime
35 Image to Text Prompt: Reverse Engineering AI Image Generation
Image to Text Prompt: Reverse Engineering AI Image Generation
AI Anytime
36 Image Generation for #ramadan using #ai. #midjourney #chatgpt #shorts #youtubeshorts #islam
Image Generation for #ramadan using #ai. #midjourney #chatgpt #shorts #youtubeshorts #islam
AI Anytime
37 How to build an AI-ready organization: Cultivating a Data-Driven Culture
How to build an AI-ready organization: Cultivating a Data-Driven Culture
AI Anytime
38 Midjourney: Generate AI-powered Images
Midjourney: Generate AI-powered Images
AI Anytime
39 Getting Started with Graphs: A Beginner's Guide (Part 1 of GNN Series)
Getting Started with Graphs: A Beginner's Guide (Part 1 of GNN Series)
AI Anytime
40 Build India's First ChatGPT like App for Politics: BJP-GPT
Build India's First ChatGPT like App for Politics: BJP-GPT
AI Anytime
41 Meet BJP-GPT.... @AIAnytime  #bjp #news #shorts #tech #chatgpt #ai #youtubeshorts #coding #video
Meet BJP-GPT.... @AIAnytime #bjp #news #shorts #tech #chatgpt #ai #youtubeshorts #coding #video
AI Anytime
42 ChatPDF... #chatgpt  for PDF files. #ai #generativeai #shorts #youtubeshorts #coding #tech #ai
ChatPDF... #chatgpt for PDF files. #ai #generativeai #shorts #youtubeshorts #coding #tech #ai
AI Anytime
43 Free AI Image Generation #ai #chatgpt #coding #tech #shorts #youtubeshorts #shortvideo #generativeai
Free AI Image Generation #ai #chatgpt #coding #tech #shorts #youtubeshorts #shortvideo #generativeai
AI Anytime
44 Transform old photos into Vibrant Memories with Deoldify AI: Build a Streamlit App
Transform old photos into Vibrant Memories with Deoldify AI: Build a Streamlit App
AI Anytime
45 Open Assistant: The Real Open-sourced LLM
Open Assistant: The Real Open-sourced LLM
AI Anytime
46 Thanks to @YannicKilcherand team for the open sourced LLM Open Assistant. #ai #shorts #tech
Thanks to @YannicKilcherand team for the open sourced LLM Open Assistant. #ai #shorts #tech
AI Anytime
47 Search Engine for AI generated images. #ai #tech #technology #generativeai #chatgpt  #shorts #video
Search Engine for AI generated images. #ai #tech #technology #generativeai #chatgpt #shorts #video
AI Anytime
48 Generative AI Video Platform "Synthesia" #shorts #youtubeshorts #ai #tech #chatgpt #generativeai
Generative AI Video Platform "Synthesia" #shorts #youtubeshorts #ai #tech #chatgpt #generativeai
AI Anytime
49 Text to speech Voice AI platform. #shorts #youtubeshorts #ai #tech #technology #python #coding
Text to speech Voice AI platform. #shorts #youtubeshorts #ai #tech #technology #python #coding
AI Anytime
50 Create Amazing Videos with ChatGPT and Pictory: Free AI-powered Video Creation
Create Amazing Videos with ChatGPT and Pictory: Free AI-powered Video Creation
AI Anytime
51 Want to create beautiful video using #chatgpt and #pictory ? Watch the tutorial on channel. #ai
Want to create beautiful video using #chatgpt and #pictory ? Watch the tutorial on channel. #ai
AI Anytime
52 Animate your photos using AI. Bring old family photos to life. #ai #tech #shorts #shortvideo #coding
Animate your photos using AI. Bring old family photos to life. #ai #tech #shorts #shortvideo #coding
AI Anytime
53 Create a PDF Search and Summarization Tool in less than 100 Lines of Code: GPT-Index and Streamlit
Create a PDF Search and Summarization Tool in less than 100 Lines of Code: GPT-Index and Streamlit
AI Anytime
54 Text to Video Generation using Videocrafter: Intuitive Math behind Latent Diffusion Model
Text to Video Generation using Videocrafter: Intuitive Math behind Latent Diffusion Model
AI Anytime
55 Gamma AI: Create presentation PPT easily with #ai . #chatgpt #shorts #shortvideo #tech #coding
Gamma AI: Create presentation PPT easily with #ai . #chatgpt #shorts #shortvideo #tech #coding
AI Anytime
56 Tripnotes: Free AI tools for your trip planning. #ai #chatgpt #shorts #youtubeshorts #video
Tripnotes: Free AI tools for your trip planning. #ai #chatgpt #shorts #youtubeshorts #video
AI Anytime
57 Meet Bark (New Text to Speech Model): Clone Any Voice to Generate Music and Speech
Meet Bark (New Text to Speech Model): Clone Any Voice to Generate Music and Speech
AI Anytime
58 Fliki: The free AI video creation tool. #ai #shorts #shortvideo #youtubeshorts #chatgpt #tech #news
Fliki: The free AI video creation tool. #ai #shorts #shortvideo #youtubeshorts #chatgpt #tech #news
AI Anytime
59 Ask Anything Tool: Chat with Your Video using ChatGPT, MiniGPT4, and StableLM
Ask Anything Tool: Chat with Your Video using ChatGPT, MiniGPT4, and StableLM
AI Anytime
60 HuggingChat: Open Source ChatGPT (Interface and Model)
HuggingChat: Open Source ChatGPT (Interface and Model)
AI Anytime

This video teaches you how to build a question and answer generation app using large language models and various tools, and how to fine-tune the model for better performance. The app can help streamline exam preparation by generating practice questions and answers from uploaded PDF files.

Key Takeaways
  1. Upload a PDF file to the app
  2. Define a prompt template for the LLM
  3. Use the LLM to generate question and answer pairs
  4. Fine-tune the LLM for better performance
  5. Download the generated question and answer pairs in a CSV file
๐Ÿ’ก The use of large language models and fine-tuning can significantly improve the accuracy and effectiveness of question and answer generation, making it a valuable tool for exam preparation and other applications.
๐Ÿ”’ Pro feature: Ask AI to explain this lesson โ†’

Related Reads

๐Ÿ“ฐ
The Research Assistant in the Room
Learn how to build a Research Assistant like Omnist in two weeks with a team of one, leveraging AI and ML concepts
Dev.to ยท Thomas Lee
๐Ÿ“ฐ
AI Mastery: Why Learning AI Is One of the Best Skills Today
Learning AI skills can boost productivity and competitiveness in today's digital world
Dev.to AI
๐Ÿ“ฐ
Top AI Papers on Hugging Face - 2026-07-22
Explore top AI papers on Hugging Face, including video grounding and code agents, to stay updated on the latest advancements in AI research
Dev.to ยท Y Hร nh Nhan
๐Ÿ“ฐ
The Sophistication Trap: Why the Smarter AI Technique Keeps Losing
Smaller AI models can outperform larger ones due to overfitting and complexity, and understanding this phenomenon can inform better AI development strategies
Medium ยท Deep Learning
Up next
5 Levels of AI Agents - From Simple LLM Calls to Multi-Agent Systems
Dave Ebbelaar (LLM Eng)
Watch โ†’