Hello guys.
So we are going to continue the discussion with respect to our end to end project, uh, gen AI project,
which is nothing but Q&A conversation with PDF including chat history.
Okay, now um, we will be implementing this completely step by step.
First of all, let's go ahead and start importing all other libraries.
Already we have discussed about this chat history, how to include that inside the chain in our previous
module.
And that module was nothing.
But, uh, it was something related to conversational queue and a chat bot.
So a detailed video in the previous module has been already uploaded.
Now this will be completely an end to end project.
So first of all we will go ahead and import Streamlit as St.
Now this Streamlit that we are specifically going to use.
Other than that I will be also importing some of the important libraries.
So first of all, let me just go ahead and import two important libraries.
One is called as create History aware Retriever and create retriever chain.
Okay.
So first of all this will basically be used to create the retriever chain.
And uh if I want to add history along with the history prompt I'll be using this Create history aware
retriever.
Right.
Uh, this is just like a retriever.
This function will be just used to create a retriever with chat history functionalities.
Along.
Along with that I will be using this create stuffed document chain so that we can combine the entire
document and send it to the context.
Then we are importing our vector store db that is chroma.
You can also go with files.
It is up to you.
Uh, then uh let's go ahead and import our chat history chat message history.
So that will be available inside your Langton underscore community dot chat underscore message underscore
history already.
Uh, all these libraries has already been installed in this requirement dot requirements.txt.
So please make sure that you have this requirement dot txt.
And this is how the entire folder structure needs to be created.
Like right?
I have all the projects over here.
This is my requirement dot txt with respect to all the files right?
So please make sure that uh, so you don't have to even worry about any installation at arch.
Okay.
Then uh, you have this entire, uh, base chart message history over here.
So I will also go ahead and import this.
Okay.
Then, uh, coming to the long chain underscore code or prompts, that is chat prompt template and message
placeholder that also we will be using it okay.
This message placeholder is to define the uh session key.
Uh and what kind of session key I'm actually storing all the conversation hit okay.
And this is my base chat message history, which we have already discussed.
Then, uh, we will also be importing, uh, a couple of libraries, which is called as Chat Grok.
So let's go ahead and uh, import this something called as chat grok over here.
So chat grok will be specifically used uh, with respect to um, the OpenAI API key that I'm actually
going to use.
So sorry, not OpenAI grok API key that I'm actually going to use.
Uh, and here I'm actually using grok.
Uh, the model that is nothing but Google Gamma two.
Okay.
Along with this I will also go ahead and import Huggingface embeddings.
So here is my Huggingface embeddings so that I will be able to import all my embeddings itself in order
to import it.
Uh, quickly I will go ahead and import two libraries that is import OS.
Okay.
And uh, I will say from dot env import load underscore dot env.
Okay.
And I will go ahead and initialize this load underscore dot env.
And let me quickly go ahead and initialize this h.f underscore token which is present in my environment.
Variable right.
H.f underscore token.
And this I have already shown you how to properly load it.
Then, uh, I will go ahead and create my embeddings over here.
So this is my embeddings that I'm actually going to use hugging face embedding.
And the model name is this one right.
All mini lm l6 v2 okay.
So this is my model that we are specifically going to use.
Perfect.
Till here everything looks good enough.
Uh, and now let us go ahead and see some more libraries that will be requiring.
Okay.
Uh, so I will be importing some three important libraries that I'm actually two important libraries
that I'm actually going to use over here.
One is recursive character text splitter and pi PDF loader.
Right.
This is basically to load the PDF itself.
Okay.
So uh, I think almost all the libraries has been, uh, taken place and has been, uh, imported.
One final, uh, library that I want to import is with respect to the runnable history.
Okay.
So here we are going to use this runnable with his message history.
The reason why we are using this is that we will be able to create our conversational queue and a chatbot
with the help of, uh, the chat history that we have.
Right.
So here what we will be doing is that, uh, we will be using this to create our retriever, uh, when
our, when we are creating, our retriever will be making sure that when we create that specific agent
to run, the retriever will also be using this library to probably include the chat history.
I'll be talking about more of this when we go ahead.
Okay.
Now, quickly, uh, let's go ahead and do this.
And, uh, here I have already imported all the libraries.
So first of all, we will go ahead and set up our Streamlit app.
Okay.
I will write s t dot title.
And here I'm going to basically say conversational conversational rag with PDF uploads and chat history.
Okay.
And chat history.
So everything is basically going to get included over here.
Then I will go ahead and write SD Dot write.
And here let's go ahead and include this.
I will say upload PDFs okay.
And chat with the content okay.
Chat with the content.
Perfect.
Now, uh, let me go ahead and input the grok grok API key okay.
And here I'm just going to go ahead and create my API key which I will be writing s t dot text underscore
input.
And enter your grok API key.
And here specifically I am going to do one thing.
I will just go ahead and write my type as password.
Okay.
Type as password.
This will basically be my API key over here.
Let's see.
This looks fine or not okay I forgot to give comma.
Okay so this is for my API key.
And as usual I will go ahead and see check if grok API key is.
Is provided okay so I'll write.
Hey if API underscore key is equal to or double colon I will just go ahead.
And uh if the API key is basically given I will just go ahead and initialize my LLM model.
Right.
So here I will go ahead and say chat.
Grok the grok API key that I really want to give over here will be initialized to my API key.
API key okay.
The second parameter that I want to give is model underscore name.
Now with respect to this particular model, uh, I will be using our Gamma2 model, uh, for the same
purpose.
So let's go ahead and go to my grok cloud.
Okay.
Grok cloud or I will just go ahead and click on this okay.
I can use any of this model.
It is up to you.
So I will use gamma two nine be it okay.
So gamma two.
Gamma.
Two.
Nine.
Be it.
Okay, I'll use this specific model.
Perfect.
This will basically be my model name.
So till here, everything looks absolutely fine and absolutely good itself.
Uh, I think we should proceed, uh, in developing this particular entire application.
So.
So here, uh, as soon as I make sure that I initialize my LM model, I also make to probably create
my session ID.
So I will go ahead and write session ID is equal to SD dot text underscore input.
And here I'm going to basically give my session ID.
Okay.
And here I'm going to basically go ahead and write.
My value is equal to some session okay.
You can put anything as you want but I will just keep it to default okay.
Default default underscore session okay.
So this will basically be my session ID okay.
Now this is for my chat interface.
Right.
So this is my chat interface.
What we are specifically using.
Okay.
In the next uh, what we are basically going to do is that I'm also going to state fully manage the
chat history.
I have to manage it with the help of session states.
Okay.
Now here I will say if I'll create a variable called a store.
Okay.
If store not in session, state not in session, you'll be understanding where we will specifically
be using the store.
If you remember in the Get session history in conversational Q&A when we discussed about.
Right.
So if I go ahead and write not in store dot session underscore state, I am just going to go ahead and
write s t dot session.
Session underscore state dot store.
We'll go ahead and initialize to this empty dictionary.
And inside this only we'll be having all the key value pairs.
Right.
Uh like of the messages of the I message content, human message content, all those things.
Now I'll go ahead and create my uploaded files.
So this will be upload button right.
I will say hey SD dot file uploader okay.
And I'll say hey choose choose a PDF file okay a PDF file file okay.
And here I will say hey type is equal to PDF okay.
And I will also say accept multiple files.
I'll keep it to false because I just want to try with a single PDF.
But if you want to try with multiple PDF, just go ahead and give this as true.
Okay, perfect.
Uh, so everything looks good till here.
So I will go ahead and process my uploaded files.
Okay.
So here what I will do is that I will go ahead and write if uploaded underscore files.
If the file is basically uploaded I will go ahead and create a document okay.
This will basically be my document because in PDF I may have multiple files.
Right?
I may have multiple pages.
So here I'm basically going to write documents and I'll say hey for uploaded files.
So or uploaded underscore files in sorry file in uploaded files.
So let's say even though I make this as true, let's make this as true I can have an option to probably
select multiple files also.
Okay, it is up to you whether you want to go with single or multiple, but I will write a generic code
where I'll say that hey, for each and every file that is basically uploaded, we'll take that file.
And first of all, we'll store that file file in our local folder because see when we upload it right.
There is no specific, uh URL.
Everything will be in the form of memory.
So it is necessary that we create some temporary, uh, pdf file in our local.
Right.
So here I will go ahead and say temp uh, instead of writing temp temp temp pdf.
Okay I'll go and write temp PDF.
And this is a F string that I will be using and I'll upload I'll create this with the name of temp dot
pdf.
Okay.
Now what we will do we will open this PDF I'll say with open temp PDF comma.
I'll send right bytes mode okay.
I'll open this PDF and I'll write as file.
So once I open this I will go ahead and write file dot write.
And here I'm just going to go ahead and create my uploaded file dot get value.
Okay.
So finally if you go ahead and see file underscore name is equal to uploaded file dot name okay.
Whatever name will be there I will also take give that particular file name if I want okay.
All the information from that.
So in short what I'm doing is that I'm opening that file that I have uploaded, I'm reading all the
values and I'm also reading the name.
Okay.
Now finally, in order to open this file and get that content, what I will do, I'll create a loader
and I'll write, hey, let's go ahead and use this py pdf loader which will be useful in call loading
your files.
So here I'm going to write temp dir okay.
Oh sorry temp PDF.
It should not be Dir.
It should be PDF.
Whatever PDF file is there we are going to load.
And then I have docs where I'll specifically write loader dot load.
Okay.
Uh so all these things is specifically there.
Now, what I will do, I will go ahead and write documents.
Dot extend.
Okay.
And here I'm going to go ahead and create my docs okay.
So I'm saying hey let's go ahead and append all the docs over here right now.
Uh, what we are basically going to do is that, uh, we are going to split and create embeddings for
the documents.
So here I will just go and copy and paste it.
So instead of using OpenAI embeddings I'm going to use embeddings, whatever embeddings we have discussed
over here.
So let me just go ahead.
But I think this is common code right.
Split and create embeddings for documents.
First of all we'll use recursive character text later.
I'm using a chunk size of 5000 chunk overlap of.
Let's make it to 500 at least.
Okay.
Then you have this text splitter dot split documents of documents.
Then I have the splits.
Then I'm using chroma to probably split all the documents and store it in my embedding vectors.
Right.
And finally we are converting this into a retriever.
Okay.
Now, uh, what I am actually going to do after this is that I am going to go ahead and I will just
go ahead and create my new prompt.
Okay.
Now this new prompt will have context.
So I'll say contextualize underscore cue system prompt.
This is basically my system prompt that I really want to create.
Okay.
And here I will be writing this one.
I'll say hey, given a chat history and the latest user question okay.
Question okay.
Along with this I will just put some information over here.
I can also write all this like this.
Right.
So here you can see I'm saying hey, given a chat history and the latest user question which might reference
context in the chat history formula to standalone application question which can be understood without
the chat history, do not answer the question, just reformulate it if needed and otherwise return this.
So this is a kind of a system problem that we are specifically giving to our LM model.
And for this we will be creating a prompt template.
So for this I will just go ahead and like like this.
So I'll say contextualize Q prompt where I'm using chat prompt template dot from underscore message
where I'll give my system which will be assigning to this particular system prompt.
So this is the set of instruction that I'm giving to my LM model.
Then I am going to create a placeholder which has this chart underscore history.
So in my session I will go ahead and create this chart underscore history.
Just in some time I will go ahead and create it.
Don't worry.
Then I have this human which respect to any input that I specifically give.
Okay.
Now after this what I will do, I will since I have created this placeholder for the chart history,
I can go ahead and create my history underscore aware underscore retriever.
Right.
So that is where I am going to create my retriever.
Right.
So and this retriever will be nothing.
But uh it will be a retriever with memory.
Okay.
So here I'm going to use this.
I'll go ahead and call this create history retriever uh, aware retriever here I'm going to give three
information LM retriever along with this whatever prompt that I have actually designed over here, this
prompt and this chat history will be referring to that.
So this basically means this retriever has a powerful feature of this particular chat history where
it will be able to store all the information.
Okay.
So yes, uh, this was about this chat aware history retriever okay.
Now finally what I will do, I will go ahead and create my answer question prompt okay.
Answer question prompt.
And let me just go ahead and write this particular prompt because this prompt will be based on the context
okay.
So here I have created a system prompt okay.
Quickly.
And this system prompt says that hey you are an assistant for the question answer task.
Use the following pieces of retrieve context to answer the question.
If you don't know the answer, say that you don't know.
Use three sentence maximum and keep the answer concise.
New line.
And here we are passing this particular context.
And this context will be replaced by the stuff document chain.
Right.
So this is my system prompt.
Now I will go ahead and create this particular prompt for this kind of question.
I will say queue a prompt and from message I'll be using this again.
I'm using this placeholder.
This and message that I'm actually giving is input okay.
So uh, almost uh, this is specifically done.
Okay.
And it is up to you.
Like how you really want to create this and all.
Okay.
But at the end of the day, what I will be doing is that, uh, I will make sure that I will integrate
this particular prompt with this history aware retrieval.
Okay.
So now in the next step, what I will say, I will say, hey, let's go ahead and create our question
answer chain okay.
And I'll say create stop document chain.
So here I'm going to use this here I'm going to give two parameters.
One is uh lm and one is QA prompt.
Right.
Because this creates a document chain will be replacing, uh, the entire documents over here in the
context.
So the two information is basically given LM and QA prompt.
Okay.
Then finally I'll go ahead and create my wrap chain which will be nothing but create a retrieval chain
okay.
And inside this retrieval chain I will be using a history aware retrieval.
And then you'll also be using this question answer chain.
So in short I have combined this history aware retrieval with the recent QA prompt that I've actually
used.
Okay.
So this basically gets my rag underscore chain.
So guys now let's go ahead and uh you know we have already created this rack chain.
But as you all far remember, you know in a conversational Q&A history or chat bot module, we had also
discussed something about creating a get session history function.
So here we are just going to go ahead and write get session history, get session history.
And we will create the specific function.
And the function.
Main aim will be that whenever we give any session ID okay it should return back our base chat history.
Message history.
Right.
So here what we'll do.
We'll write if s t dot session or sorry.
Or I'll just go ahead and write if session underscore id not in s t dot session underscore id state
dot store.
Because inside this only we'll go ahead and check it.
And this is where we are going to probably put all our session ID automatically get stored inside this.
Right?
So I'll say PT dot session state dot store, dot store.
And here I'm going to give my session ID which will be equal to our chat message history and this chat
message history.
What it will do.
All the conversation that is basically happening with this particular session ID, it will get stored
over here, and from here it will go to this particular, uh, you know, the session ID itself.
It will get saved over there.
Then finally we go ahead and return this s t dot.
Session state dot store.
And here I'm going to basically use my session underscore ID.
Perfect.
So this is the same store that we created on top right.
So the same right which is a key value pairs.
So yes.
Uh, once this is done this is the function that we have actually created.
Now I'm going to go ahead and create my conversational rag chain.
So I'm going to write conversational rag underscore chain which will be equal to runnable message history.
And here I will be giving my information like rack chain.
Then I have my function which is called as get session history.
The third thing that I'm actually going to give is my input key message or input message key.
As you know what is the input that I'm giving.
So I'm giving basically input right.
And along with this you should also know which is my history message key.
History message key is nothing but chat history.
So automatically this basically comes to know that where it needs to save it, uh, all the chat session.
And finally your output message key which will be assigned to your answer.
Right.
Your output message key means whichever response you will be getting that will be stored in the specific
output.
Okay.
So till here, everything looks good.
Uh, now let's start and write our code as soon as we get our input.
Uh, that is the next thing that we really need to write.
But I think till here, everything looks good.
So this is my uploaded file.
Uh, with respect to the uploaded file, we are able to see each and every thing.
This is good enough, and we have our system prompt everything as such.
Okay.
I think one condition I may have missed somewhere.
Okay.
Uh, one, let me just go ahead and verify this here.
I've given my API key here.
I have checked the condition.
Here is my uploaded file with respect to uploaded file.
I have my text splitter then.
Okay.
Yeah.
So this context splitter needs to be created all inside this if condition itself okay.
So this is one of the changes that I will do I'll go ahead and press shift tab okay.
So please make sure that you do these changes.
It needs to be inside this right if condition.
Because once I need to have the documents over here right then only I should be able to go ahead and
create all this chain.
If I do not have the documents, then it will be of no use.
Okay?
So please make sure that you make these changes.
Okay?
Just go over here and press select from here.
This this entire the from the text editor code.
It should be inside this if if block okay.
Perfect.
Until here.
Everything looks good enough.
And, uh, now, uh, the next thing that I am actually going to do is that after creating this conversational
Q&A chatbot is that let's go ahead and create my user input.
So I will say, hey, this is my user input.
Whatever question I'm looking for.
And I'm saying, hey, if user underscore input, I'll say create my session history.
When my session history will be nothing but get session history.
And here I'm going to give my session ID right.
Whatever session ID I have the default session.
That is what I'm actually going to give over here.
And now I can probably go ahead and call my conversational rag Jane this one.
Right.
So I'll go inside this I will right dot invoke here input will be one key.
And this configuration will be with for my session ID so that automatically once I use this config right
inside my conversation Q&A, it knows right where I need to save all my session information, like all
the chat right inside this chat history variable.
This will be my input.
This will be my, uh, chat history where all the information will be saved based on the session ID,
right?
Since that is the reason we are using this.
Uh, say get session history now, Now, along with this, uh, I will quickly go ahead and display
this.
All the three information, like what is the store?
Right then, uh, if you see over here session history, it should be session history.
Okay.
I will be displaying this store.
I'll be displaying the answer and I'll be displaying my message.
Okay.
And here, uh, instead of getting the response, I will use store dot success over here so that it
shows in green color.
Okay.
so yes, this was it, uh, all the information that I've actually coded in front of you and if the
API key is not given.
Right.
So let me just go back over here, okay.
And let me right else if when the API key is not given, I will give a warning saying that hey, please
enter your.
Please enter the grok API key.
Okay I will go ahead and execute this.
Now let me just go ahead and open this quickly.
Uh, let me go back to this specific folder.
So here I will write CD dot dot and the folder over here that I'm actually using if you see.
Right.
Uh which is the folder.
So folder is nothing but 4.1 rag Q&A.
right?
So I will go ahead and write 4.1.
CD 4.1 rag Q&A conversation I will clear the screen and next go ahead and run this Streamlit run app.py.
Before that I will open my env file and copy this key so that it will be useful.
Let's go ahead and press this.
Enter.
And here you have this entire website okay.
So it is loading.
Let's see I think 1 or 2 errors usually comes when I'm we are basically writing the code from here itself.
Right.
So let's go back over here and see whether we are getting any warning.
Resume and download is procured.
Okay.
So it is first for the first time.
It is just going to take some time okay okay.
Password is not a valid input text okay.
Let's see where is password.
I'll go ahead and search for.
So it should be double s okay.
Perfect.
Let's now see.
Always rerun.
Now I think it should see for the first time.
It will take time because it needs to download the hugging face embedding technique.
Okay.
So please uh please make sure that you be patience for that.
Okay.
Now I have my environment key.
Uh, then I will go ahead and paste it over here.
Press enter.
Okay.
I think it should work.
It has got entered now here also it is going to probably take some amount of time.
No, I don't think so.
It will take time because only for the first time when the hugging face embedding is basically required.
At that time it will take.
So now uh, here you can see it is running the embedding technique of hugging face is basically getting
applied, you know, and, uh, here you have it.
Right.
So here, now let me just go ahead and ask about what is Transformers.
Okay.
If you use OpenAI embedding it will be much more faster.
So here uh, I have searched for what is transformer.
And uh, here you can see input to chat prompt template missing chat history.
Uh expected chat underscore history.
Received input.
Chat history.
Okay, I think this is some bit of mistakes that has been happened over here.
Uh, we will just go ahead and check it out.
Let's see which line it is.
Basically saying, uh, input chat history chat underscore history is over here.
Here chat underscore history is basically over here.
and uh, down what I've used here also chart underscore history is there uh here okay.
So here I have to use chart underscore history.
So it's fine.
Some smaller mistakes will happen.
Now it is just going to give me the output okay.
So I have to make sure that all these things are correct.
Right.
So one more error.
Um response of answer.
Okay.
Start.
Success.
Assistant.
Okay, I've given two parameters over here.
Right.
Take two positional arguments, but three is given.
So let me do one thing.
Instead of writing success, I'll just go ahead and write SD dot write for now.
Okay.
Now it should basically give me the output.
And here you can see in the default session it is basically getting displayed.
Uh transformers are type of neural network.
Uh they use self-attention to weigh the importance.
And all this information is basically there.
Okay.
Um, fine.
Let's go ahead and ask about this.
What is attention?
Uh, or I'll just say provide a detailed summary of Transformers with attention is all you need.
I'll go ahead and search for this.
Let's see whether I'll be able to get the answer or not.
So here is my entire output.
This paper attention is all you need to introduce the transformer architecture with all the information
you can see over here and in the chat history.
Also you are able to see this.
You can also get the detailed summary I can say provider, detail summary, provider, detail summary,
provider detailed summary of the conversation we had.
Okay.
So here you can see the output.
I hope you're enjoying this videos right.
Because at the end of the day I know I'm creating this amazing modules.
This can be integrated wherever you want, right?
So here you can see once your conversation started, you asked me to ask about Transformers.
I provided a concise explanation.
I also outlined the advantages of transformer in emphasizing their palatability ability to handle.
You can upload any PDF and you can start working, right.
So I hope, uh, you understood this particular video.
I hope you were able to understand the entire implementation.
So yeah, this was it for my side.
I hope you liked this particular video.
I'll see you in the next video.
Thank you.
Take care.
Bye bye.