Okay, so in this, uh, chapter, we are going to, uh, work in a another basic rack application,
but in this case with a little bit more sophisticated vector database, we are going to use the database
Deep Lake, which is a database vector database.
Uh, that is going to ask a credentials, uh, from us in order to be able to connect with it.
This is the most common way to operate in a professional way with vector databases, as you will see.
And this is a process that is going to be, uh, used whenever you use a professional vector database
pinecone, V8, Redis, whatever.
So the process you are going to see in the exercise is going to be very similar to the process you are
going to, uh, perform whenever you have to work with a professional vector database.
So the problem we are going to solve in this exercise is to test a drug application with a different
vector database and access credentials.
And later we are going to add to the external document A used by the RAC application, a data to the
external document used by the RAC applications.
Okay, so a couple of things.
We are already familiar with a basic applications.
We have done it before.
In this case we are going to go a little bit, uh, further and we are going to uh connect with a little
bit more sophisticated vector database in this case.
So.
As I was saying, we are going to use a deep Lake vector database.
Deep Lake is a database owned by the company Active Loop.
This is how you write Active Loop.
So you go to their website.
You will see how you can create a free account there, how you can get, you know, your token in order
to start using the database and how you can get the ID a the organization ID that is a requirement for
you to start using the databases, etc..
So all this you have to do in their website whatever question doubt you have regarding active loop or
vector database or whatever operations we are doing here.
Remember the usual places you can find answers chat, GPT, Google, StackOverflow, etc. etc..
Okay, so this is the process we are going to follow in this exercise.
At the beginning.
As always, we uh, load the credentials in order to use the OpenAI API.
Okay.
So as you remember we have this credentials in the dot env file.
Do you remember this was explained chapter seven.
So the first thing we are going to do is to load the deep lake credentials.
So we have our deep lake credentials in our dot m file.
Okay.
This is a confidential file that you have already created probably.
And uh, this is what, uh, every uh, software engineer uses.
Whenever we work with applications, we, we create a dot env file, and there we store all kind of
passwords, credentials, etc..
So what we are seeing here is, uh, getting the active look token and the active look or ID from the
dot m file.
The next thing we do is we, uh, name the new database, uh, we are going to create.
So in this case, uh, this is the name we are giving to the data set we are creating.
And, uh, we create, we load the dependencies.
In this case, you are already familiar with OpenAI embeddings with recursive characters, uh, text
splitter and with retrieval QA because this, uh, we have already used in previous exercises whenever
we have created a basic application.
But in this case we are using a new module called Deep Lake in order to connect long chain with this
vector store.
So as easy as that, once we have these dependencies loaded or these modules or plugins, whatever you
want to call them, what we do is, uh, pay attention to the external knowledge document we are going
to use.
So remember, a rack application is an application that provides the LM foundation model with an additional
information.
So as you know, ChatGPT for example, is trained with all the information and that is on the internet
or more or less all of all of it, but it doesn't know anything about your company, your book, your
user guide, whatever.
All the private documents you have are not included in the knowledge that ChatGPT has right now.
And the way we can provide this knowledge and we can start interacting.
Interacting with this knowledge via a LM application is uh, with a rack application, right?
So the first thing we do is we need to, uh, uh, we need to.
Tell the application what is our where is our private document.
So you can use a PDF as a private document or a text file or whatever word document, whatever kind
of file.
In this case, we are going to use a very simple document.
We are going to create a list of only two, uh, items.
The first item we are going to talk about, one, uh, curious fact about United States in this case
is, uh, the date where United States celebrates the Independence Day.
And in the same chunk of information we tell them, however, the Declaration of Independence was passed
on July the 2nd.
Right.
And it was only officially ratified on, uh, July the 4th.
So this is the special information.
We are included here.
And this is something that we are going to be asking about when we test this application.
The second piece of information we we include in this private document is okay.
The first documented European to arrive in North America was the Spaniard Juan Ponce de Leon.
Okay, so this is the external knowledge document we are going to use.
In this case, we are creating right now the external document.
But you could use, you know, a document that you have already in your company or, or personal use
or whatever.
So remember how we work in a large application, the first thing we do is to divide the document in
smaller chunks of text in order to this is the document, and we are going to divide this document in
smaller chunks of text.
In order to do that, we use the text splitter.
So the first thing we do is we configure the text splitter.
And the second we create the document chunks.
And if we print the length of this document chunks we will see that we have two chunks.
So we have divided this document into different chunks or fragments of text.
As you remember, the next operation we have to, uh, perform in a drag application is to transform
this chunks of text into numbers, what we call the embeddings.
Because as you remember, the computer works.
Much better with numbers than with letters, right?
So we convert our chunks of text into embeddings.
How do we do it?
Same thing we did in the previous exercise.
We are going to use the OpenAI embeddings module in order to build these embeddings, and we are going
to use it in the a that database we have created.
So using the Deep Lake module we are going to tell uh, our application where is our uh database and
what uh, way are we going to use in order to transform the chunks of text into a number.
So how are we going to build the embeddings?
Okay.
So in this case what you are actually doing is to create or, you know, start including data in your
vector database.
Since this is not the first time I execute this command, the the notebook is telling me, okay, this
database already exists.
Uh, it's already there.
If you are creating a new one, you are going to have a different message here telling you, okay,
your database has been created or whatever, pay attention because here we are including in the pip
install the Blake enterprise.
So this is a command that we will need in order to execute this command okay.
If you don't have the Blake Enterprise installed in your virtual environment you will need to do that.
You can do it.
You can do you can install this module from the Jupyter notebook.
And in order to do that you have to write this one.
What I have here is this commented because since I already have it, uh, install and I don't want to
install it again, it's not necessary for me.
But in your case, if it is the first time you install it, you can do this and then, uh, you know,
uh, caps and enter and you have a, you have that installed or you can do it, uh, via the terminal.
So you can write pip install, uh, Deep Lake Enterprise in the terminal.
And it will be installed in the virtual environment as well.
Remember that we use this exclamation mark in order to tell the notebook that we are going to do a terminal
operation from the notebook.
Okay.
So here we are creating the well we are entering content in the database okay.
With this.
With this one we are going to load the chunks and transform that the chunks into embeddings okay.
With this operation here.
And once we have that ready we can start.
You know testing our application.
It's as easy as that.
So we create our LM instance and we create our retrieval QA chain.
Uh, so we we give the, the the model we are using the chain type stuff as you know, is the most,
uh, popular, the most used type of chain we are going to use for retrieval QA.
You have a couple of alternatives here, but usually staff is the most common.
You can go to the documentation to look about the other 2 or 3 you have.
And then we have to tell, uh, how are we going to retrieve the information in this case we enter the
database, we have created the function as retriever and we are ready to go.
In order to start asking questions to our private document, we run the new chain.
And in this case, as you can see, we are asking, uh, the, the, the application about, uh, you
know, the actual date when the Declaration of Independence in United States was actually passed, as
we know it was July the 2nd, not July the 4th.
Uh, so then we have here an additional, uh, operation, how to add data to a vector database that
we have already created.
So let's see that you have new information you want to add to your private knowledge document.
Right.
So this is the way you can do that.
So this is the additional information we can include to our document.
And what we are going to do is we are going to add this document directly to our vector database.
In order to do that, the first thing we do is we use the text splitter to divide this information into
text chunks.
And second, we use the add documents function in order to transform the text chunks into numbers into
embeddings.
Okay.
So uh, once you have this, you can start asking questions about the new information you have added
to the vector database.
In this case, we are asking what is the largest state in the US.
And here we are saying that this is Alaska.
And in the second piece of uh, knowledge we enter here, what we are saying is that in some states
and in some cities, big cities of the United States, you have one special style of pizza, right?
So we are giving this information here.
And in the last question we are asking, tell me three states with their own style of pizza.
In this case, you can see that the application is giving us the right answer, but just in part because
it is giving us a states that have a particular pizza style.
But in this case, it is, uh, giving us five, uh, states, not three as we, uh, as we asked.
So we see that we will, uh, face, you know, this kind of, uh, not 100% accurate responses from
applications.
And we will learn ways to make them more and more accurate.
Okay.
So in this chapter we have seen how to create a basic lag application with a little bit more sophisticated
database.