All right, here we are, our super bass project rag.
I went and got a coffee.
I came back and it's done.
It takes a few minutes.
So let's just look around this navigation as we're looking into our project.
Of course, the way you get here if you ever get lost is if you're on your projects page, you simply
come into rag and back.
We are here again.
And looking at the nav here, we can be looking at tables.
We can looking at something called SQL, SQL which you probably know is how you, you you ask questions,
you query your database, the database itself, uh, and then a bunch of other stuff.
If we look at the database itself, loading tables, we have no tables.
It's an empty database.
Okay.
Back.
We go back we go to the project overview.
Here we are.
And now is the one thing I told you I was going to do something that's kind of Cody.
And so for those of you that are engineers this is going to be like, sure, this is easy for those
of you that aren't this is going to be like, Holy cow, what is this?
And and I'm here to tell you that it honestly, it's very cookie cutter stuff.
Whilst you do need to run some code at this point, which you will find in the in the course resources
tell you exactly what to do.
This is a very pedestrian activity.
And here's the thing.
You could ask ChatGPT to write this for you with some instructions, and it would do it in a heartbeat.
And in fact, as like a just a dark secret that's just between you and me.
Don't tell anybody else.
But I got ChatGPT to generate this for me because I hate SQL.
I always seem to make mistakes with it, so I didn't want to do that.
I just asked ChatGPT to generate it.
I told it exactly what it needed to do and it did it right away.
You can use that or code or whatever you like to use, but when you're ready, I need you now to go
to the SQL editor, which is where you get to run code should you wish, by pasting it in there and
pressing that run button down there.
And that's what we're going to do next.
So the reason we're doing this is because the tables that come with the super base database aren't,
by default, set up to be exactly the format that Nam would like.
And it's not like there's a there's a sort of out of the box way to say, hey, make me the tables and
expects.
So we have to create the database in that particular format so that this will work well.
And so if I paste in this SQL, this is the SQL that you should be able to copy and paste also from
the resources.
And let me just quickly say at a high level, because there is one janky thing about this one janky
thing.
Uh, so this basically says, I want you to create a table called knowledgebase that is going to be
the database table.
It's going to create.
And I want every row on that table to have an ID content, which is going to be the text that we just
built in metadata, which is going to be the metadata including the category and then the vector.
The embedding vector.
And this is the janky thing that I'm going to come back to.
And then I need to make a single a function in the database called a match documents.
And this is a function that expects this is a function.
It's going to want to call when it wants to retrieve relevant context.
And as I say, if you go with like like some database setups, this will all be automatically built
and will be exactly what Nan is expecting.
And we only had to do this because we're going pro with Supabase.
So we're creating this function, match documents.
We're saying that it should return some information.
and this is what the function needs to look like.
And basically it's because when you when you do the natural calculation in the base table with this,
this Postgres function, it gives something slightly different to what Nam would like.
And you have to take one minus that calculation in order to get just the right number for super base.
So this stuff doesn't really matter.
If you wanted to generate this for you, you would just simply say the ChatGPT hey, can you make me
something?
I can run in super base in Postgres, which would allow me to set up a table called Knowledge Base that
would be in the right format with content and metadata, and a vector that Nam would expect, and it
will generate something just like this, as it did for me.
Okay, now let me explain the one janky thing.
So here's the thing.
People that are relatively new to Rag sometimes think that the the magic in rag is something to do with
what's in the vector database.
These vectors and and whilst it's true that magic is in the vectors, the vector database, it's just
an efficient database that can deal with with, with similarity calculations.
The thing that's doing the magic is the embedding model.
It's the LLM that's able to take text and turn it into a vector, and that's outside Postgres.
We're about to set that up in N810, and that's going to come up with a vector and pass it in.
And what Postgres and Superbase is able to do well is store that vector.
But but this database doesn't actually create the vector.
And that means that there is one important thing that needs to be set up when you create the database.
And it comes down to this, this idea of, of dimensions, it's the question is how many dimensions
are the vectors going to have.
Because this database needs to be set up to expect that.
And that is a is a is a feature of the embedding model that we choose.
As I say, you can imagine one that just comes up with three numbers that you can imagine.
Some come up with thousands.
And so you have to pick a model.
And then that you have to make sure that the number of dimensions that the database is expecting matches
your embedding model.
And in our case, we're going to pick a really standard typical first choice, which is to use a model
that called called OpenAI embedding small.
And it's a great model and everyone uses it, and it happens to have 1536 dimensions.
And that number one, five, three, six needs to go in these two places here.
And that is a bit janky.
I do agree that seems like what.
So if you put one, five, three, seven one off, you're going to get a weird error at some point about
mismatching dimensions.
And that's no good.
So that has to be right now OpenAI embeddings model does have a charge associated with it, but it's
really tiny.
It's really tiny for huge numbers of documents.
And so it's super tiny for 60 documents.
Nonetheless, I know that that OpenAI needs an upfront payment and you might not want to use that.
Gemini has a great embeddings model two, and if you know what you're doing with open source, of course
the open source models are free.
And if you do choose to go in that different direction, the only thing you need to know is once you've
picked your embedding model, you may have to come back and rerun this with a different number.
In here you can run this, this, this recreates the table if it already exists.
You can run this as many times as you want.
Just make sure that the last time you run it that the number of dimensions that you specify here matches
the embedding model that you pick.
And if you stick with the plan and you pick OpenAI's model, then 1536 is the perfect number to have
here.
So I appreciate that.
It's a bit weird to to to have it like this.
It seems like a bit of voodoo, but but uh, yeah, you need to make sure that the dimensions in your
embedding model matches the number of dimensions that you tell super base it needs to create in this
database table.
All right, that's a long explanation.
Time to run this.
Ah, but wait, there is one more thing we need to do.
Just before we run this script.
We have to just tell Super Base that we're using vector related stuff with this database.
And the way we do that is you go over here on the left to the database section, and you go to extensions
right here.
And you see all these extensions here, type vector.
And this comes up.
And then you turn that on and here we go.
Enable extension.
Let it think.
And it's done.
Extension vector is now enabled.
And now come back to the SQL editor.
Here we go.
You see the arrow I got down there because I didn't do it yet.
Uh, and now we should be able to run and get a better result.
Okay, here we go.
Run!
Running.
It says success.
No rows returned.
That's what we wanted.
We didn't want any rows returned.
And now to check that it's really been successful.
Like it says we go to database.
It says it said for a moment it said no tables.
But there it is.
There is our knowledge base database we've set up.
It has an ID, it has content that's going to be the stuff.
It has metadata and it has embedding, which is our vector.
This is where it's going to go.
So we've done it.
We've set up a super base database.
It's now time for us to go to back to N and to hook it all up and then populate our database, do the
load part of ETL.