So guys, in this video we are going to create one amazing LM project which is nothing but PDF query
with Langshan and Cassandra DB.
Uh, Cassandra DB will be probably creating in a platform which is called as data stacks.
So if you have probably heard about this particular platform, which is called as data stacks, which
will actually help you to create Cassandra DB in the cloud itself, and why this platform is quite amazing
because from this you will be able to perform vector search.
And whenever we talk about this kind of documents, or if you want to really create a Q&A applications
from huge PDFs itself, vector search is the thing that you really need to implement.
Now, before I go ahead, let's first of all understand the entire architecture.
We will be solving this entirely step by step.
What are the steps specifically that will be taken to probably complete this specific project that we
really need to understand?
So let's begin with the architecture.
Initially.
Let's say you have a specific PDF.
This PDF can be of any size and any number of pages.
First of all, we will read the documents and understand.
Here we are going to use Lang chain as I said, because Lang Chain has some amazing, amazing functionalities
which will actually help you to perform all the necessary tasks to create this specific application.
Now first we will go ahead and read the documents that is specifically the PDF and the first step.
Usually when we, whenever we work with this kind of data set is with respect to some kind of transformation
we really need to do.
So after reading this documents, we will convert this into various test chunks.
That basically means will split the data set into some kind of packets.
Right.
So this text chunk will be of some specific size based on the tokens that we are probably going to use.
So over here you can see some example reading the document.
And then we have divided this into some chunks.
Then we will convert all this chunk into text embeddings.
Now for here we will be specifically using OpenAI embeddings okay OpenAI embeddings actually helps you
to convert text into vectors.
Now why you specifically require these vectors?
I hope you have heard about text embedding techniques in machine learning right there.
We have specifically used bag of words, TF, IDF, and many more things that is already present in
my YouTube channel.
We have also used word to work, average word to work.
What are the main aim of all these techniques?
To convert text into vectors?
Because once we probably convert into vectors, we can perform various tasks like classification algorithms
like similarity search algorithm, and many more.
So that is the reason we will specifically be using OpenAI embeddings, which will be responsible in
converting a text into vectors itself.
Now, once we convert every text into vectors, we will also say this as text embeddings.
Once we get this embeddings, what we are specifically going to do now this will be quite amazing because
understand if we have a huge PDF document, right?
So definitely the vector size will keep on increasing.
So it is better we store entirely all these vectors into some kind of database.
And for this we are going to use Cassandra db.
So in short, what we are basically going to do is that we will take all the specific vectors and save
it in a vector database here.
Currently we are going to use Cassandra db.
Now what exactly is Cassandra db?
So in order to understand about Cassandra DB, I have opened the entire documentation page over here.
Cassandra.
Aperture.
Apache Cassandra is an open source NoSQL database, and it can definitely be used for saving massive
amount of data, so it manages massive amount of data fast without losing sleep.
Right?
So again, understand this is a no sequel database and for vectors kind of thing, we definitely have
to save it.
In this kind of database itself.
Many bigger companies are basically using this Cassandra DB for this specific purpose.
So if you really want to read more about Apache Cassandra, you can probably see over here.
Apache Cassandra is an open source NoSQL distributed database trusted by thousands of companies for
scalability and high availability.
This is the most important point for scalability and high availability without compromising performance.
Linear scalability and proven fault tolerance on commodity hardware or cloud infrastructure make it
as a making it as a perfect platform for mission critical data.
Now how we are going to probably create this specific database.
For that, we will be using this data stacks platform, wherein it will actually help you to create
this vector Cassandra DB, so that you can store entirely all these vectors into this specific DB.
And at any point of time, if a person is trying to query from this particular DB, you will be able
to get that specific response from that right.
And the most similar response that you will be able to get it.
Now that is the next step.
What we are basically going to do, all this vectors, we are going to save it in some kind of vector
database.
As I said, we are going to use Cassandra DB, or we can also say Astra db, and this will be created
in this Datastax platform, uh, wherein you can actually perform vector search.
Now the next thing is that after you probably save entirely all your vectors in, in the database itself,
then a human, whenever, uh, human tries to query anything that is related to that particular PDF
document, it is going to probably apply similarity search along with text embeddings, and it is going
to get that specific response.
So this is the entire architecture that we are specifically going to perform in this specific project.
All the steps will be shown step by step.
Everything will be explained in an amazing way along with the code and along with the explanation.
Now let's go ahead and start our specific project for this PDF query with Launching and Cassandra DB.
So guys now let's go ahead and implement this specific project.
I will be going step by step.
I will also be showing you how you can create the Cassandra DB, uh, specifically in the Datastax,
uh, platform itself.
Uh, we'll be seeing step by step all the comment regarding this code and all is given over here.
I will also be providing you the code in the description of this particular video.
So first of all, uh, what exactly we are doing, we are going to query PDF with Astra DB and Lang
chain.
Uh, it is basically powered and uh, understand it is powered by vector search.
So first of all you need to understand what exactly is vector search.
So there is an amazing documentation that is given in the data stacks documentation itself.
So vector search enhances machine learning models by allowing similarity.
Comparison of the embeddings.
Embeddings basically means whatever text is basically converted into the vectors.
That is basically embedding, right?
And over there you can definitely apply multiple algorithms, right.
Machine learning algorithms on the fly.
Right?
As a capability of Astra DB vector search supports various large language models.
So large language models can be is very is supported in an amazing way.
In this the integration is very much smooth and easy right.
Since this LLM are stateless, they rely on vector database like Astra DB to store the embeddings.
See, understand.
Because when we say stateless that basically means what.
Suppose if we have a embeddings, once we lose it we cannot again query it.
Right.
So it is definitely require a database to probably store all these things.
And what you can do after that you can query any number of time.
So let us go step by step and let us see okay.
So first of all we need to create a database on Astra DB.
So I will probably click this specific link.
Everything is basically given over here.
For this we will be going to Astra dot data sex.com.
Right.
So first of all it will probably ask you to sign in right.
And here you can probably sign it with your GitHub or with your Google account.
So here I'm going to go ahead and sign it with GitHub.
And probably once I probably sign in over here you will be able to see that, uh, I will be providing
you the link along with everything in the, uh, code itself.
Right.
So it will be very much easy for you.
So once you go to astra.datastax.com, the next step is basically to create a database.
Right.
So this database, uh what kind of database we are going to probably create.
It will be serverless vector.
And this is specifically a Cassandra DB.
Okay.
So here I will probably give my database name.
Let's say I want to do PDF query.
Right.
So this will basically be my PDF query DB okay.
This will basically be my database name.
You can give anything as you want.
And here I'll basically be giving lang chain underscore db a key space name.
It should be unique.
The provider that you can specifically use.
You have multiple like Amazon Web Services, Microsoft Azure.
But here I'm going to probably use Google Cloud, which is the default that is selected.
In the next step, we will go ahead and select the country region, which is by default US East one.
So as soon as you probably fill all this details and as you know that we are specifically going to use
this vector database itself, because at the end of the day, the algorithms that we are probably going
to apply it will be easy with respect to this kind of database, right?
So finally we will go ahead and create the database.
Now once we create the database you will be able to see that my database is basically created over here.
Right.
So this is what is my database that looks like right.
PDF query underscore DV.
Now if I probably go to my dashboard I have already created this kind of database a lot.
So let me consider one database which I have already created.
And over here some important information that you really need to take.
First of all I will go and click on connect okay.
So when I probably click on connect what some information you will definitely require one is generate
token right.
And the other one is the db id.
So db id is basically present over here right.
The token is basically present over here.
Now I'll talk about where this specific information will be required okay.
Okay.
So here I will go with respect to my code.
Now let's start our coding.
Initially we will be requiring some of the important libraries like cashier dataset, Lang Chen, OpenAI
and tick token.
So here I will go ahead and execute it, and I will go ahead and install all this specific libraries.
So it will probably take some time.
Right.
I've already done that installation.
So for me it has happened very much quickly.
Now the next thing is that as we know that we are specifically going to use Cassandra DB.
So in long chain you have all these libraries which will actually help you to connect with Cassandra
DB and perform all the necessary tasks like text and meetings or creating vectors and probably storing
it in the database itself.
So here I'm going to probably import all these libraries from Langton dot vector stores.
Dot Cassandra I'm going to import Cassandra.
Along with this I'm also going to use this vector store index wrapper.
It is going to wrap all those particular vectors in one specific package so that it can be used quickly.
Then I'm also going to import OpenAI, because OpenAI is the thing that we really need to use.
Along with this, we are also going to use OpenAI embeddings, which will be responsible for converting
your text into vectors.
Along with this, if you want some kind of data set from hugging face, you can also use this.
And one more important library that we are going to use is cashew.
Now cashew actually helps you to, uh, probably integrate with the Astra DB right in Lang chain.
And it will also help you to initialize the DB connection.
So all these libraries we are going to use, I'm going to execute this step by step.
You're going to probably see.
And this is the first step in installing the libraries and initializing all the libraries that we are
specifically going to use.
Along with this, what we are going to also import is one PDF which is called as pi pdf two.
This will actually help you to read any PDF, uh read the text inside the PDF itself.
So this is one amazing library to probably use.
Okay, so here I have basically used pip install Pypdf2.
So let me just go ahead and execute it.
And inside this you will be able to see it shows a requirement.
Already satisfied because I have already installed over here Then from Pi PDF you are going to use PDF
reader because this will be the functionality that will be used in order to read the document.
Here is the document that I have specifically taken.
So this is one budget speech PDF.
So this is the Indian budget that is probably of 2023.
It's a big document with somewhere around 461 KB file.
It has around 30 pages.
So I'm going to specifically read this PDF and then convert it into vector, store it in the database
itself, and then query from the database anything that you have any information about that particular
PDF.
Now let's go ahead with the setup okay.
Now with respect to the setup you require three important information.
One is the Astra DB application token, one is the Astra db ID okay.
So where you can probably get this two information.
So go to your vector database.
Vector database in the data stacks.
So here is what you have specifically logged in okay as I said inside your DB just go and click on connect
here.
You need to click on Generate Token.
As soon as you probably click on Generate Token then you will be getting some code which looks like
this.
This token you will specifically go getting.
So this will be probably found in your token JSON file.
So it will probably show you a JSON file which will have this information.
Okay.
The first information that you have over here is the Astra DB application token.
So here you can probably see it starts with Astra CS.
So what you need to do just click on the generate token and you will be able to see it.
This is the first information you just need to copy and paste it over here.
The second information is Astra db ID right?
Astra db id is nothing but this specific information.
That is your database ID where do you get it?
You just need to copy it from here.
So this is the information with respect to your Astra db ID.
So this two information, once you do it you paste it over here I've already pasted it.
And then you can also see that I've also used some open API key.
And this specific API key.
Don't use this only because I made some changes.
I've already executed the code also.
Okay, so I'm going to take this three information.
This two information is basically used to connect to your Astra DB right.
Which has a Cassandra DB hosted over there in the cloud.
Right.
And the other information is basically to use the open AI API features.
Right.
So all this information is basically there.
I'm going to probably execute this.
And then we will go ahead and read our budget speech PDF.
So this is the first step.
According to this we are reading the specific document.
Before that we have initialized everything with respect to this okay.
So once we specifically do this I will probably be reading this now after reading, as I said, we are
going to divide all our content into some kind of chunks, right?
So here is what chunks we are basically going to do.
Now first of all, I will read all the raw text.
So for this I'm going to use from type extension using concatenate I'm going to read from each and every
pages.
I will extract all the text.
So here you can probably see for I comma page in enumerate pdf reader dot pages page dot extract text
will basically take out all the text from those pages, and it will concatenate in this particular variable
that is raw underscore text.
So once I probably execute this, what will happen is that you will be able to get all the text inside
this particular variable.
So you can probably see over here raw underscore text has all the entire text.
So this is the entire text from that specific PDF.
Right.
Slashing basically means new line.
So this step is basically done.
Just imagine before if you did not had this specific library, it was very difficult to read a PDF.
Right.
And we have actually done this just with writing 4 to 5 lines of code.
Now the next step is that we will initialize the connection to your database.
I have all my database information right, like our token ID and the database id I'm going to use that
cash.
You cashier is basically used as a library over there for initializing of this particular database.
So cashier dot init.
Here I'll be giving one parameter which is called as token which will be nothing but Astra db application
token.
And then your database ID, which is nothing but Azure db ID, right.
So I've taken this two information.
I will execute this.
You will get some kind of warnings.
So don't worry about the warnings.
It is just like showing you some kind of warnings okay with respect to some drivers issues and all.
But this will basically get executed.
And now I have uh basically initialized my DB itself.
Right now we are going to create the long chain embeddings Lnm objects for later use.
So for that I'm going to use I'm going to initialize OpenAI with my OpenAI key and embeddings.
Also OpenAI embeddings with my OpenAI key.
So I have my LLM.
I have my embeddings okay.
Now is the main step I need to create my long chain vector store.
So over here this is what we are basically going to create now right.
And for that you know we have initialized Kassandra.
Right.
We have we have imported Kassandra.
Now, what we will do is that in this Kassandra, we will provide three important information.
What is the kind of embeddings we are going to use?
What is the table name inside this particular database session?
None.
Keyspace none.
So this is the default parameters we have specifically used.
QA mini demo is my table name okay.
Just like a question answer table name and what kind of embeddings we are going to specifically use.
That basically means whenever we store any we whenever we push any data in my Cassandra DB, in my Astro
DB itself, what it is going to do, it is going to probably convert all the text using this embeddings
into vectors.
Right?
And this is the embeddings that we have initialized over here.
So here is the next step.
We will go ahead and execute this.
So this is my Astra vector store.
But still I have not probably converted my text into vectors only when when I'm pushing my data inside
my DB.
That time, this entire embeddings will, uh, probably convert that particular data into vectors.
Then what we are specifically going to do is that we will take this data and we will try to, uh, will
take this entire data, will convert into checks, uh, chunks and will also do the text embedding,
right.
Text embedding while inserting.
Right.
So here you first of all, we are dividing the data or the entire data entire document into text chunk.
So for this we are using character text splitter which is basically present in lang chain dot text splitter.
We need to split the text using character text splitter.
It should not increase the token size.
So here I have given character text splitter.
I'm saying use the separator slash in use chunk size some chunk size of 800 characters.
Chunk overlap can be 200.
And how much is the length with respect to that specific length?
You can probably provide it over here.
Right.
And once I probably do this you can see text or dot split text here.
You will be able to get all the text.
And if I see the top 50 text, you can probably see that I'll be able to see all the top 50 text over
here.
Right.
All the data itself.
This is amazing.
Right.
And this is basically from the PDF.
Right.
All the data all the data.
Right.
It is basically taking the top 50.
Right.
And understand the token size is basically over here as the chunk size is somewhere around 800.
Okay.
Now this is done.
We have the text.
I'm going to just use top 50 and probably store it in the vector database to see if everything is working
fine.
Now how to add the specific text.
Now what will happen when I add this text inside my Cassandra db c extra vector store?
What is this?
This is basically initialized with respect to the Cassandra library, right?
So here you will be able to see that I have used embedding.
So now when I'm inserting inside the Cassandra DB what it is going to do, it is going to apply this
specific embeddings also.
So that is the reason you will be able to see that when we write extra underscore vector underscore
store dot add underscore text.
And I'm taking the top 50 top 50 texts over there.
This will also perform embeddings.
So that basically means if I see over here it is going to perform this task and it is going to insert
in the Astra DB which is having the Cassandra over there.
Right.
So it is going to do this both the steps with respect to this particular code.
So we are going to add this text.
And then we are also going to wrap wrap this entire inside a wrapper okay.
So these are the information.
This is the index that we will be getting with respect to those texts.
So once I probably execute it, you'll be seeing that in the same database.
It is going to insert all this headlines okay.
Now finally let's go ahead and test it.
That basically means I have my vectors inside my database.
Now it's time that we just query and we ask some kind of questions.
Now I have read this entire PDF guys.
I could find out some of the question like what is the current GBD, how much agriculture target will
be increased and all.
So I will take this particular example.
And let's say I'm writing first question is equal to true.
While true if first question I'm just say that input okay.
It will just ask like what kind of question you want to type.
Else uh, it is just asking you to uh, put more questions itself.
If I write quit it is going to break.
Otherwise it is going to continue.
Now see, this is the most important.
As soon as I give my first question, it will go ahead with vast, uh, extra vector index and it will
query whatever query text we are specifically using and the LLM models that we have specifically initialized.
And after that we will be getting the answer.
Along with this we'll also be providing some information right like for doc score or similarity score
score like some other information also.
Right.
So let's go ahead and execute it.
And as I said I'm going to use this cushion okay.
How much is the agriculture target to be increased and what focus it will be?
Okay.
So I'm going to paste it over here.
I'm going to press enter.
So as soon as I press enter you can see that it is now taking the information.
See this.
Um you can probably see over here we are querying this particular DB right.
And it is going to give me the top four results okay.
So here you can see that agriculture credit target will be increased to 20 lakh crore with the focus
on animal husbandry dairy and fisheries.
Right.
Why it is giving only this much data?
Because I have told that.
Take the 84 characters or 84 words.
84 characters, text till there and probably give the results right.
If I increase this, it will give you more result.
Along with this, you can probably see that it is giving me stop quick queries.
That is the four query.
Uh Hyderabad will be supported as center of excellence.
Some more information but the most suitable answer that you have specifically got is this one right?
And this is what probably if you go ahead and search in the PDF, if you give the same question, you
will be able to see the same answer right along with this.
Probably if I want to probably see what is the current GDP, if this information is present over there,
it will also be giving you that specific answer.
It will just do the similarity search right.
So here you can the current GBD is estimated to be 7%.
Isn't it amazing?
Now you can probably take any huge data because at the end of the day you are specifically using DB,
right?
And finally, if you want to quit it, I will just go ahead and write quit.
And this is basically quit, right?
So in short, we have performed each and every step.
Now this is what it is happening.
Whenever a human is giving a text query, text embeddings will happen.
And based on that similarity search.
And then you will be probably get the output right.
And this is the entire steps we have basically done step by step.
Now a amazing shout out to uh datastax Astra DB, because you can also create your own free account
vector search, which is super important, not just understand any type of Q&A applications.
You can definitely develop with the help of vector search.
Right.
And uh, that is where this Datastax Astra DB uh, is basically used, right?
And internally it is specifically using Cassandra DB.