Transcripts
1. Course Introduction: Hi, everyone. Welcome
to my project course. We are going to
create a project for RAG PDF chatbot using
Gen generative AI model. Let me introduce myself. I'm Arno Chalam. I have been
teaching a lot of courses in scutua and I have a good
experience in teaching. Let me tell my best examples. I've created programming
courses like Java C Python and also concept courses like machine
learning and generative AI. Likewise, I'm going
to teach this course. So this is our course roadmap. In the first class,
we are going to discuss about how to
set up our project. In the second class, we are
going to discuss about how to load the PDF and extract
the text from the PDF. In the third class, we
are going to discuss about how to convert
the text to embeddings. In the fourth class, we are
going to discuss about how to store the embeddings in our
created vector database. In the fifth class, we are
going to discuss about how to create the chatbot
using our Gen AI model. In the last class, we are going to discuss about our output. So this is our course roadmap. So what are all the skills you will learn when
completing this project? So first, you will know the concept behind
rack architecture. In the second, you will learn
how to process the PDF. In the third, you will
learn how to create the embeddings and store the
embeddings in the database. In the final, you
will learn how to use our Gen Gen AI model
to create the chatbot. So these are all
the skills you will learn when completing
this project. So before starting this project, you need to have a
basic Python knowledge and a laptop with
Internet connection. Okay. So now we are going to
create our Gen AI project.
2. Project Setup & Install Model: Before starting the project, let me tell the
concept behind RAG. The full form for RAG is nothing but retrieval
augmented generation. It will improve the
lodge language model and provide the exact
answers for our Kuri. So for example, you are asking
a question to hagibiti. Chargibty is an example for LLM. It will use the concept of RAG to provide the
answers for our Kuri. So that is the example. So retrival the name itself
contains the meaning. It will search and find the exact meaning for your
query from the source. So that is the retrival
and then augmentation. So it will compare the extracted text
and then user query, and then find the relationship and provides the exact answer. That is the augmentation
and generation. It will generate the
answers for your query. So this is the simple
concept of RAG. First, it will extract all
the text from the source. Augmentation, it will compare the relationship
between our costing and then extracted text. Generation, it will
generate the output. So this is the concept. Now we will create our project. So just create a new folder and open the folder
with VS code, and then create a
new Python file, app I Python file. So after that, you need to
install Oma from the website. For that, open Google
and type download Lama. After that, open
the first website, download Lama on Windows. You ought to select the version. If you want Mac,
you can select Mac. If you want Linux,
you can select Linux. If you want Windows, you can select Windows. I'm going to select Windows. So just select
Download for Windows. So after that, you can able
to see the download option. It will be two GP. You ought to wait for this. I will take some time based
on your Internet speed. After the download is completed, you can able to open
the setup EXE file. Just open the file. You can see the install
option, select that. Now, again, you have to wait for that. It will take some time. It will install all the
libraries required for this ama. So after the installation
is completed, you can able to see the
Oma from your desktop. So this is the GUI for the ma. You can also see the
lot of commands. If you want, you
can use the Codex, open code, copilot, droid. Also, you can see the settings. If you want to auto
download the updates, you can enable the option. You can also see the
models, download location. If you want, you can change so I'm going to select
the default option. So these are all the settings. If you want, you can reset. Otherwise, just go back. So this is how you can install
the ama from the website. So remember that first, you need to install this. After that only, you
can able to install any other models in local
for your system. That's it. So after that, we
are going to see how to download the Gwen model. We have to open the terminal, and then you have
to run this code. Oma run Gwen. If you run this code
for the first time, you can able to see the
size of the Gwen model. Mostly, it will be like four GB. If you run this code
for the second time, you can able to see the prompt. After that, we need to
download the second model. You have to type Oma pull
PULL NomCNO MIC, embed text. We need the model for converting
the text to embeddings. That is converting
the text to numbers because the machine cannot
able to understand the text. I need numbers. That's why we need to convert
the text to embeddings. For that only, we have
to download this model. So now we are going
to discuss about how to import the packages and how to import our model and how to set up the
local environment. So first, we are going to
import the request package. You have to type Import request. After that, we are going to
use the PyPDF package from PyPDF Import PDF reader. Using the package,
we can read the PDF. From the name itself, you can understand the
meaning PDF reader. After that, we are going to
import the package phase. You at to type
input phase, Faiss. It is a library
created by Facebook to search the embeddings
stored in the database. So it is a concept in RAG. First, learn RAG what is
RAG from the YouTube. After that, just create
this project, okay? NumPi SNP. And then we are going to
import OS package for reading the files from our
Windows or Linux or Mac. That is the importance
of OS package. After that, we need to
set up the Oma server, create Oma URL Equalt
HTTP, local host. We are going to
run our server in the local host with
the port number 11434. That is the port
used by Oma server. And then you have
to type slash API. So this is how you can set up Olama server in the local host. And then you have
to type the model, LLA model will be Gen.
We are going to use the large language model
Gen for this project. And then we are going to
use the embedding model, Embed model Equalt No MIC embed text. That is the model useful for this project to convert
the text to embeddings. Embeddings is nothing
but it is a number. The text will be
converted to number like Apple will be converted to
one, two, three, like that. So that's it. So
this is how you can import the packages and
download the model, load the model, and set
up the environment. In the next class, we
are going to discuss about the functions
useful for this project.
3. Project class 1 : Load PDF File: Okay, guys, now we
are going to discuss about how to load our PDF. Okay? It is very,
very important. We need to load our PDF. Then only we can
able to summarize the content or ask
anything from the PDF. Just create a function, load PDF of file path, how to provide the file path. Reader, we are going to use
the package PDF reader. From that package,
we can read the PDF. What are all the contents
present in the PDF we can read. Text, you have to
create a sample text. After that, you have
to create a for loop and read every
pages in the PDF. There will be a lot
of pages in the PDF. We are going to read
every pages and extract the text
from every pages. For that, you have to
create a content equal to page dot extract text of. Using the function, we can extract all the text
from the pages. That will be stored in content. Now we need to save the
content to the text. So we have to create
a if condition, if content text plus equality. That is increment
equalt content. So all the extracted text will
be stored in the content. That content will be stored
in the created variable. That's it. So this
is the concept. First, read every
pages and extract the text and store the
text in the content. At last, save the
content in the text. Also to return the output, to type written text. That's it. So this is the
code for loading the PDF. In the next class, we
are going to discuss about other functions
useful for this project.
4. Project class 2 : Split Extracted Text: Now we are going to discuss
about how to split the text. So just create a
new function, okay, split text of text, TEXT. Come on, chunk. We need to split
the text in chunks. You need to provide the size
of the chunk. I'll be 500. And then you have to
create a variable chunk equal to list. We are going to create a list
to save the splitted text. After that, we are going
to create a flop for I in range of zero comma n of text, chunk size. You need to type chunk size. So based on the chunk size, we are going to create a
far loop to split the text. The maximum split
size will be 500. Each 500 text will be
stored in the list chunks. And then we have to store
the values for that to use chunks taut upend of text. So it is a function
available in list. Up and function we
are going to use. Based on the chunk size 500, we are going to save the
500 words in the chunk. After that, you have to
return the output chunks. Okay. So that's it. It is very, very simple. Just create a function
for splitting the text. The size will be 500, 500, 500 characters will be splitted, and it will be stored
in the list chunks. In the next class, we are going to discuss about
other functions.
5. Project class 3 : Convert Text to Embeddings Part 1: Okay, now we are
going to discuss about how to get the embeddings. Okay? We need to convert
the text to embeddings. That is, we need to convert
the text to numbers. So you have to
create a function, DG embeddings. Text. From the text only, we can convert this
to embeddings. And then you have to
get the response. You have to dive a
response equal to request dot post off. We are going to use the
model nomic embed text. That is running in the
OlamaUROmaR slash embeddings. So that model can able to
extract the embeddings. So we need to provide
the request in the JSON format, key value pair. So we need to tell
the server use the model Nomic embedded text. And then you have to provide the prompt you to tell
use the prompt the text. So the text will be
converted to embeddings. So this is how you can get
the embeddings from the text. So previously, we have
created the text. Now the text will be
converted to embeddings using the model
Nomic embedded text. So that model is running
in the Oma server. So we need to tell
the Oma server, use the model nomic embed text, and prompt will be the
text because there are two models running in the server Gwen and
then Nomic embed text. We need to tell which
model the URL must use. So that's it. In the next class, we are going to discuss
about other functions.
6. Project class 4 : Convert Text to Embeddings Part 2: Okay, now we need to
get the response. So you have to type data
equality response dot JSON off. So we are just send
request to the server. The server must respond.
That is the output. So for that only, we are created the variable data equality
response dot JS on off. And then you have
to type the format. If embeddings I data written data of embeddings. It will be the response will
be in the standard format. So if you want standard format, you can use the condition
if embeddings in data, written data of embeddings. So if you want other format, you can use IF data in theta written data of data of zero from the starting index
of embeddings. So this is another format. If you want, you
can use otherwise, els, rise, value error. If there are any errors, you can use the
except sun clash. Value error of unexpected error. Data. There will be some errors, there will be some
network error like that. A errors, it will have
to provide the output. So this is the format for
returning the output. So the next class, we are going to discuss about
other functions.
7. Project class 5 : Create Vector Database: Okay, guys, now we are
going to create a database. Okay? So it is very,
very important. We need to store the
embeddings in the database. So it is a very, very
important concept in Rag. We have to save the
embeddings in the database. Then only you can able to remember what are all the
embeddings we have created. So, we are going to
create a database, create a function, D
create vector database. Of chunk. So you have to type embeddings equal to get embeddings. Of chunk in Flo, we are going to
use FarloF chunk. I chance. Dim. We need to provide the size n of Ecolte len of
embeddings of zero. Index FAI is. So it is very, very useful if we need to search the embeddings
in the database. For that, we can use
the model FAISS. It is very, very faster to search the embeddings
in the database. If you want to search any
embeddings in the database, you can use the model FAISS. From the index, we can
search very faster. So you have to type index flat two of dim Index, dot, add of Numpi dot ary Numpit. Of embeddings. Embedding. As type float 32. So we are going to use the NampiRI to store
the embeddings. Written index. Come on. So you need to check for
the indentation embeddings. That's it. We have
created a function for creating the database
for the embeddings.
8. Project class 6 : Create Faiss Search Algorithm: Previously, we have
created the database and added the index for the
embeddings in the database. Now we are going to
create a function for how to search the
embeddings in the database. You have to create a function of search of query. Come on. Index, come. Chunks. Come on, K equal to three. Such similarity, that is
the K value equal to three. So from the user Korey, we need to search the
embeddings in the database. Korey embeddings, equal to
Numbi dot RAG embeddings of query as type of float 32. So we are also converting
the user query to embeddings and storing the
embeddings in the Numpi array. And then you have to
create distances comma, indices equalt index dot search of Cory. Okay. Cory embeddings. Come. So using the similarity, we are going to
find the distance between these three
similar values. After that, we need
to get the result, result dicalt chunks. Of I for in index of zero. You are to return result. So that's it. So this
is very, very simple. You just need to convert the
user query to embeddings and search the similarity between the user embeddings and
also text embeddings. There will be some similarity
between three values. Just get the three
values as a result. In the next class,
we are going to create a prompt for our project.
9. Project class 7 : Prompt For Project: Hi, Veron, now we
are going to create a prompt for the project. Okay? We are going to ask our large language model
Gen to get the content. You have to create a function, D ask LLM of context
Kama Costin. So what are all the
context and then Costin? Costin will be
asked by the user. Now we have to
provide the prompt. Inside the prompt block, you have to type the prompt. Consider this as chargeby. You are asking the chargeabi the costing, that
will be the prompt. Answer only from the
content or context below Context will be. You have to provide the
curly bracket, context. And then costing, the costing
will be asked by the user. Okay? You just create
a variable costing. And then you have to
provide the answer. The model will
provide the answer. So just close the block, okay? So this is how you can ask
the prompt for your model. Now we need to get the
response from the model. For that you to type
response equal to request dot post of the model is
present in the OLma server. So for the server, we are sending the request. After that, the server
will provide the response. So it will generate in
the format of Jason. So we are just
telling the model, just use the model Gwen. And then use the prompt. We are given Avo how
to type prompt prompt. After that, we are not streaming the output, type stream false. That's it. After that, the server, we have to provide the
response for that. You have to return the response. Tat Jason Response. Okay. So this is
how you can request a server and then get the
response from the server. In the next class, we are going to create our main function.
10. Project class 8 : Output & Conclusion: Hi, one now, we are going to
create our main function. We just create Df
main off So first, we have to provide
the period of path, input of enter the
period of path. So after that, we
need to load the PDF. For that to create a
variable text equality. Load PDF. Just
call the function. It will load the PDF
from the file path. So after that, you
ought to create chunk. You ought to type chunks, equal to split text. We are going to call the
function split text of text. Splitting the text. Okay? And then we are going
to create a database. You have to type index, come on. We could create vector database. We are just calling
the function, and then you have to
provide the chance. And then you have to
create wild condition while all the conditions
are satisfied. User how to provide the query. How to type, enter your
query or ask your chat bot. Also, you have to
check the condition. If the user want
to exit the code, if they type exit, it will exit the loop. If you want to exit
the chatbot project, you can type exit. It will prey the loop. Otherwise, you have to
provide the result. Chatbot have to
provide the result. Result equal to search
of COE Come on. Index. Come on, chunks. After that, ototype context, double quotation slash N line, new line dot join of. The output we provide
in the new line. Answer Equal asked
LLM of context. A query. So it will provide the answer. From the context and Kori, it will provide the output. And then you have to print the output, that is the answer. At last, we need to
call the mate function. If name equal to equal to main, you need to call the main
function made of. That's it. So this is our main function. First, we need to
get the PDF path. After that, we need
to load the PDF and convert the PDF
text to embeddings. At last, we need to create a database to store
the embeddings. After that, we need to create a while loop
to ask the query. So users have to ask the
query from the chatbot. After that, if they
want to break, they can type exit. Otherwise, the chatbod
will provide the output. It will search and
provide the answer. So now we are going to
run this code, okay? You have to provide
the PDF path. Just this is our PDF. Okay? You just right
click and copy the path. And then you have to remove the double quotation and enter. You have to wait
for this, the Gwen will provide the output. So now you have to
ask your query, okay? I'm going to type what
is machine learning. From the PDF, I'm just asking
what is machine learning? I'm just checking whether it is providing the answer
correct or not. So you can see the output
generated by the Gwen model. You can also check
the sample PDF whether it is right or wrong. You can see all the content
from the PDF is right. So it is provided very
perfect answer for us. How many topics in PDF? I'm just asking some medium
level costin so mostly, if you are using small model, it cannot able to answer, okay? For that only, you need to use advanced model or
larger Gwen models. Simple answers it can give. If you want more
accurate answers, you can use the larger
models. So that's it. So this is how you
can create a per of chatbot using the model Gwen. It is very, very simple. You need to know
the concept of AG, so then only you can
able to understand this project and complete
this project. That's it.