How to Build an AI Chatbot: RAG with LangChain & Streamlit. LLM chatbot tutorial for beginners

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hi everyone welcome to a product Builders where we dive into the world of generative AI today we are unveiling something truly exciting a step by-step tutorial on building your own chap PDF which we have love lovingly named document first I'll walk you through how drag works and crucial Concepts suround it and then I'll walk you through the entire code base and the last part is bonus where I'll show you how you can deploy the app on streamlet so it can be shared with the public so this is what we have built today document Genie get stent insights from your document this is basically a chatbot built using rack framework leveraging generative a model Gemini Pro it basically process your PDF documents by breaking down into manageable chunks creating a searchable vctl store and you know generating aurent answers to your queries how it works is basically you have to enter your Google API key upload your document on on the sidebar and ask a question about any document you have let's start by doing this so to obtain your Google API key you have to just click on this link so you'll end up on Google AI studio and you can create your API Co key over here just copy paste your API key and save it in a very safe place enter your API key click enter and then browse any file you wish to so I'm I'm using a file called AI tools so this file basically has a lot of information how to use AI tools this is the guide I've created by myself for example how to use canva AI how to use notion Ai and some other AI tools so I'll be uploading this document and click on submit and process so once the document is processed it gives you a validation clue saying done and now ask a question related to document so I'll be asking for example how to use canva AI yeah yes so it gave me a very concise answer whatever is in the document so that's the advantage of rag framework right the retrieved information is presented to the llm as an additional context along with the prom this allows the llm to you know access relevant facts and relationships while generating the response so feel free to ask if you have any questions about specific apps aspects or applications now that we are done looking at how the app works let's jump into the code first let's understand why are we importing so many libraries right for that to understand you have to understand what is rack framework um and how it works so rack is a very important concept to understanding and for building llm based applications so as you've seen most of the time llms hallucinate and rag helps avoid that hallucination and ensure that llms doesn't make up any facts llms are usually trained on a data set and are not updated frequently so rag allows s us to access those knowledge that llm might not have been trained on and it also has domain specific expertise for example Enterprises have propr data and rack can adapt to that specific areas by using relevant knowledge based databases most of the time you're unsure how llms are generating the answers right and rag helps you to understand how the llms arrived at a particular answer by referencing retrieved sources so in a nutshell retrieve argumented generation is a way to boost the capabilities of llms by combining them with external knowledge databas essentially it teaches llms to find relevant information outside their limited memory before generating an answer so let's understand how it works it basically consists of three parts retrieval argumentation and generation in the retrieval part the user asks a question or proms the llm and rack searches an external database like documents databases or the apis for related information this could be anything from articles to product manuals or any historical records and then there comes the argumentation part this retrieved information is used to enrich The Prompt given to the llm this is something called context and then this adds factual context and helps the llm understand the question better and then there's the generation part the llm armed with the prompt and the context generates its response this response should be more accurate factual and relevant to the question than without the retrieved information so so retriever is basically acts like a search engine sources all the external knowledge database Wikipedia company documents using something called semantic search it understand the user questions and retrieves relevant passages and then the llm itself it could be gbd3 for or Gemini models it takes the user prom along with the retri information as context T and generates the final response so let's also understand what are embeddings right usually machines do not understand text they only understand numbers both the user query and the documents in the knowledge database are converted into mathematical representation called emings imagine each text piece in a point in a high dimensional Vector space and then you know these Vector databases find similar text which like close up and then we store the embeddings in the vector databases the retriever has sufficient algorithms to find document whose embeddings are closest to the quar embeddings and this ensures the retrieved information the context aligns with the user in so that's how rag works I have included each of the you know libraries which we would be using for example um so the documents to read a PDF we are using 5 PDF and we chunk the PDF so we are using a library called recursive character text splitter it's provided by longchain and then we convert the text into embeddings and we are using a library called Google generative Ai embeddings and then those embeddings are saved in a vector database basic function provides some prompt templates and that's what we are using here and then to generate the response we are using chat Google RI you could use any model over here gem Pro gem Ultra so this is my GI up profile basically you have um four files for this application license it's a standard MIT license and then you have read me section giving a brief overview of how the app works and the requirements. text where you have all the libraries and you know the actual code for the application and to build that application we'll be using lanon lanon is a framework designed to facilitate the development of of application using large language models it aims to make it easier to build complex application that involve processing understanding and generating human like text uh to make this possible we have to uh import several libraries I already explained what libraries we require for this so let me start uh by importing them I've already installed all the libraries let's dive into the code our first step is setting up our stream page as I've already explained we'll be using using streamlet to deployer op and also to build a web interface uh what is streamlet streamlit is an open source python library that enables developers to create beautiful interactive web application just using python so let's start setting up the page we can set it up using s. set page config you can set up the page title document gie what is why so we set up um the page title document Genia I've selected the lay out as wide and we can also have something called markdown text so basically describing what your app does so we'll use st. markdown stream L for example let's say what the document does so this section includes a brief markdown on how the document GD works we and then we start setting up our API keys there are two ways how you can um you know this application using the API key you can enter your API key and save it as an environment variable but here I would be asking the user to enter their API key so that the cost is incurred by the user and not me so let's start by saying API key ask the user to input thing text input enter Google API key equal to password is API key input so here we are prompting user to enter their API key next we have our core functions we'll start with get PDF text what basically does is it reads and extract text from uploaded PDF so let's get started PDF text dos example let's say text initialize the text and then say write a for Loop for PDF in PDF do PDF Pages the text don't forget the indentation and the next step is to you know write a function for extracting the chunks from the documents so what this does is basically splits the documents into manageable chunks for processing the embeding so we'll start by writing get text chunks so we'll use something called text splitter and we have to use the library by long chain which is recursive character splitter and we keep the Chun size to 100,000 the Chun so what are we doing here we splitting um the documents using text splitter recursive character text splitter keeping the CH size 2,000 and overlap 2,000 and then we split the text and once we have the text CH we have to convert this into embeddings and store the Vector database so we'll start defining a function called get Vector so you have your text chunks Pi key so what do we have in this particular function is basically we are taking text CHS and API as parameters and then first we convert this text J using embeddings so we are using Google Genera embeddings over here um and then once we have the embeddings we store in a vector database so face is the library over here so so we passing the text chain and the embar so for now we we'll be storing the vector database in the local storage uh so we are done with reading a document extracting the information out of it converting them into Texs and converting those text into embeddings and all storing in the vector database and now we have to um you know get the pass the information to llm so this is where the real magic happens so write the function def get conversation chain we have to mention the promt template here so we are using prompt template from L chain again so we have to pass over the promt here for example I would say so we'll enter the prompt saying here answer the question as detailed as possible um from the provided context we can say make sure to provide all the details so we could also instruct the llm saying if answer is not available in the answer don't provide wrong answer so that's basically it and we'll pass over the context we'll pass it in a new line we pass over the answer so this is the instruction to The Prompt template and then we have to specify the model and the model will be using over here is Gemini Pro you could use anything specific to yours and then we say ROM use the prom template template to input variable the input variable will be context the problem we are providing is answer the question as detailed as possible Pro from the provided context we also instructing the llm saying that do not provide an answer if you're not if you're unsure and then we have to pass over the model so I using chat Google Genera the model here I'm using is Gemini Pro I'm also setting up the temperature you could set the temperature as per your wish and also passing over the Google API key as we set the prom template we have to pass in the input variable that is basically the context and the question provided by the user and then we are using a function uh load QA chain basically question and answering now that we are done with the conversational chain we have to write a function to process the user question search with most relevant documents based on the question and generate a response using the conversation EI chain so let's begin by defining the function for example the function would be taking two inut which is the user question and the API key and we have to get the embeddings user question so let me start explaining this part of the code very clearly so the user input is taking two parameters the user question and the AP key so this function purpose is to basically um process the user question search for the most relevant question and generate a response using the conversational AI chain which I mentioned before and then the you know the embedding part here is where we convert the text into numerical representation that captures the semantic understanding of the words the particular model we are using is it's already the embedding model um pre-built by Google and then we load the face Index this index basically contains all the embeddings and the document chunks which we processed earlier and then we put do the similarity search this line performs a similarity search in the face index for the embeddings corresponding to the user question the search returns document Changs that are more similar this function basically retrieves a conversational chain which is a sequence of processing steps defined by the input documents we are using Gemini Pro over here you could use any model here and then we generate the response and at last we are displaying the information the generated response is displayed in the Stream L application U this allows the user to see the answer to the question directly in the web interface now let's start building the UI interface for streamlet stream late you we start by defining the header Define main the document so in this part of code we Define the main function also for the stream UI interface as you see in the interface we have two input function where they enter the Google API which we already defined about but we also have something called ask a question from the PDF file so we also have drag and drop files here so we have to have all this information in the streamlet UI so we begin by setting the header and we ask the user to ask a question and so on the sidebar section we have something called file uploader so this is where the user uploads the file and we have sd. spinner basically this is giving a visual clue for the user that their documents have been processed and we use um all the necessary function which I mentioned about getting the text chunks and storing them in the vector databas and processing them and we show finally a success message done and and I think that's the end of the code so we build the entire application in just 89 minutes so I hope this application provides a deeper understanding of how rag works now that you have seen how the application works and written the code you can also deploy this app on stream lead Community Cloud so you have this beautiful portfolio project build but uh you would like to show this in on your resume or you know to share it with your friends or potential employes you can go and host it on streamlit community Cloud so that it's accessible by everyone and everyone can use it uh so in order to depl your app on streamlit uh just head over to streamlet create an account and once you end up on this dashboard you have to click on new app and paste a GitHub URL over here so this is my GitHub URL just pasted it and you can also um edit this domain if you wish to you just say deploy thank you everyone for watching the video if you would like to see more such tutorials please do subscribe I'll have the link to the giup report repository so you can go through the entire code base thank you so much
Info
Channel: AI Product Builders
Views: 8,788
Rating: undefined out of 5
Keywords: datascience, generativeai, dataanaylst, ai, aiproduct, chatbot, llms, aitutorial, python, nocode, langchain, streamlit, rag
Id: SkY2u4UUr6M
Channel Id: undefined
Length: 17min 32sec (1052 seconds)
Published: Sun Feb 18 2024
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.