Video43 min watch

NEW: Complete RAG Setup In n8n & Supabase Vector Store!🤖 OpenAI/HuggingFace Embeddings PG Vector

In this video

  1. 0:00Introduction to Complete RAG Setup in n8n & Supabase
  2. 0:33What is RAG (Retrieval Augmented Generation)?
  3. 2:00Tools Overview: n8n and Supabase Vector Store
  4. 3:21Embedding Models: OpenAI and HuggingFace
  5. 6:00Creating Supabase Table for Vector Storage
  6. 9:04n8n Workflow Setup with Airtable
  7. 11:20OpenAI Embedding Costs
  8. 13:00Testing Embeddings in Supabase Vector Store
  9. 14:40Q&A Chain Setup in n8n
  10. 18:00Adding Functions in Supabase for Vector Store
  11. 21:00Testing Q&A Chain with OpenAI and Supabase
  12. 25:00Recap of Complete RAG Setup Process in n8n & Supabase
  13. 27:30Closing Remarks and Additional Resources

Links from the video

Transcript

6,516 words

What's going on everybody Josh pook here and in today's video this is a really good one I am going to show you exactly how you can set up your very own rag retrieval augmented generation framework for your AI Bots for your AI knowledge base whatever the case may be using n8n and super base let's dive right into it [Music] all right guys so quick background if you're not familiar with what a rag actually is well here's some different diagrams of them

But just to sum it up I why don't I just let perplexedly explain it um so retrieval augmented generation rag is an advanced AI framework designed to enhance the accuracy and reliability of responses generated by llms it achieves this by integrating external knowledge sources into the generative process here's a detailed explanation of what a reg is and how it works so reg is a technique that combines with the capabilities of traditional informational retrieval systems with uh generative llms the primary goal

Is to optimize the output of the llms essentially um this is the three stages retrieval phase generation phase um a few benefits of it enhanced accuracy current information improve trust cost effective which is actually um I this is I actually almost thought that it would maybe be more uh expensive cuz you're paying for um embedding and then the generation but since it's being embedded if you're embedding um a text it's actually going to be a lot less um embedding costs are

Way less when you're using for example like open AI API I'll show you the cost in just a second you could also use um open source models but um but then yeah once it's embedded it's it's actually going to cost you a lot less so anyways applications of a red chat Bots customer service content generation there's so many different ones but um this is kind of a brief summary of a rag if you're watching this lot most a lot of you

Probably already know what a rag is but you're going to see in just a second so uh I'm going to lay some of the foundational information of what tools I'm going to be using and you know kind of what you should know and all this stuff at the start and then uh in the next maybe minute or two I'll be diving into actually starting how to set this up first off um just so you guys know I'm using super base if

You don't know superbase I'm hosting it on coolify I did a video a couple days ago showing you how to self host on kify your super Bas um you can also use their Cloud so if you go to superbase where is super base um if you go to super base.com you can go here you can sign up on their cloud or you can start your own project you can either host it on coolify or host it however you want right so

We're using super base technically this same process could you could use really any any Vector um database as well as yeah you could really use any Vector database you could use Zeb uh you could use um different ones right we're going to be using super base okay next is the embedding models so if you don't know how Rags work basically okay so uh I'm I'm not an expert like a complete expert here there's probably there's definitely people that can explain a

Lot better but essentially say if I I make a query to the llm through whatever means you know whatever um and you can we're doing this in n8n but this applies to whether you're building an app or whatever the case may be then it's basically going to we want to take that c uh query and we want to embed it all right now I'm not going to do a deep dive in like embeddings and explaining how it all works and vectors

And and all this stuff and the different dimensions and stuff like that um but maybe in more uh in future videos as I'm continuing to learn more about it you know watching a lot of uh you know kind of more longer in-depth videos of people explaining these things but it's very interesting how these actual llms work and uh neural Nets and all this stuff but essentially all you really have to know is that it's being embedded okay so and what that's

Going to look like just to show you as an example um it's going to take like for example a piece of content like this where it's text and it's going to like make it into see this embedding um thing so each one of these is is an embedding um and you'll you we'll get into more of that in a second it will make a bit more sense if you if you're confused but anyways there's a couple different options for embedding models

For the sake of ease here I just used open AI embedding model but um and we're using the text embedding three small all right so page per dollars performance on MTB eval which is over here which we'll get into in just a second Max input you can I'll link this um their embeddings page uh Down Below in the description and yeah it will kind of give you the gist of their embeddings here and then over here on hugging face leaderboard for

The mte leaderboard and I'm not going to go into depth of explaining this leaderboard and explaining like um I'm not going to go super in- depth into embeddings and explaining all this right now but the main thing is these are just the leaderboards of the top embeddings on hugging face basically on their specific testing criteria and there's different um you know tests here um that you can see there's different ones right and basically yeah this is just like they're currently like

Um out of box they're filtered by the overall score so you depending on your specific use case like if you're building an app if you're doing this if you're doing retrieval Etc these different scores um some of them may be better for your specific use case you know we have the different embedding Dimensions here Max tokens Etc so I'm not going to go in depth into explaining how which one to choose for your specific use case if you really want to

Go in depth on that um there's definitely videos more videos I I'll maybe talk about this more in depth in future videos but I'm just letting you guys know like if CU some like this whole rag thing it you can go down multiple different rabbit holes and and if you're new to this you can go about you know learning U more and more stuff about it so I'm just kind of letting you guys know like um I'm going to show you

How to do it but then if you want to learn more about it and go down these rabbit holes and really get a good understanding I'm just kind of pointing you in those directions but um yeah so that is yeah so you could you'll see the different models here we have like quen um voyage and yeah I would talk to chat GPT or talk to perplexity or Claude and maybe ask certain questions about this based on your specific use case if

You really want to go down like that that side of things but for the sake of ease in this video we're just going to be using like I said the open AI embedding okay so now that we got the super BAS B out of the way we got the embeddings out of the way um also too just so you guys know super base uh the way it works is it's it's um it's basically open source and it has a bunch of

Different open source projects kind of under the the under the hood uh one of them obvious what it's built on is postgress and then PG Vector as an extension so this is actually the GitHub um of PG Vector this is what what we're going to be using to do the Vector um to vectorize or put them into a the text into a vector the embeddings uh into the vector um database within super base so um if you want to learn more

About PG Vector uh and you know you you can check out the GitHub here so I'll link that down below as well okay but now let's actually get into building this thing out so it really doesn't matter how you're getting the data right I could do thing something right here where I'm scraping a website getting data I could do really anything for sake of ease or just for what my use case is here we're doing a web hook to start this

Trigger off and it's getting a so the web Hook is actually just um over here in an air table base when I uh when I basically um switch a single select to the the status of super base it's going to send you'll see here um well okay you can't I'll I'll test it in a second but it's going to to send that to the web hook which is going to include the record ID and then I'm doing and get record air

Table node which is literally just getting the record ID and then what I'm doing is I'm putting okay so what we're doing in this is we're putting a transcript um which is the from the air table field I'm just putting that into a set node just because then when I go over to superbase Vector store um the operation mode is insert document and table name this is my table name query name um and let me just see here oh it's because

And I think it's document data loader yeah load all data input Json so we're loading the Json so yeah the reason I put a set note here is because we're putting the set node and we're loading the Json from the vector store so just so so how this kind of makes sense here so if you're building it out what you're going to do you're just going to click type super base I'll show you what you're what you do here so uh

Super base Vector store and then insert documents so we need to insert this document now like I said you could put a Google drive here this could be like your company Sops this could be really anything right but yeah like I said I'm just using air table you you can go wild with whatever you want whatever your heart desire tires um and then from that you're adding an embedding so the embedding you have multiple different options like you could use hugging

Face which are a lot of Open Source models um there's mistol there's open AI which we're using there's old Lama more open source azour a bunch right so for us we're just going to be using open Ai and you have three options here we are using text embedding small reason I'm using small is because a is the older one large is like here I'll show you the pricing let me show you the pricing if does it show it here I think

It should show it here um open AI embedding pricing okay as that loads um I'll just go back here okay the reason I'm using this is because it's the second cheapest and like it's substantially cheaper than the large model it's like this one is I believe 13 cents per thousand tokens and this one is 2 cents per thousand tokens this model is highly efficient and provides a significant upgrade over its predecessor um at Ada so Ada is oh Ada I think

Is even more expensive it seems but anyways um yeah so small seems to be the best um just for what I was looking for uh and just for ease of use um now I didn't go I didn't want to you know look into right now at least you know different models over here but yeah you can do it with the hug and face and I'll make videos in future showing you how to do it with the hug and face it's hugging

Face it's very simple though you just do hugging face and then you would put the model name right here which you would get like if I go here to this model right here Salesforce sfr embedding right and that's what you would put okay but yeah uh I'll do more videos in the future about that okay from that point once you have the embedding model which for us like I said is open Ai and we're using small then what we're going to

Do is the document so document loaders you have a couple options you have GitHub document loader um which you would just need to add your credentials for we're not going to be doing that we're going to be using the default data loader and you do still have a few options you can use Json binary um and I believe let's see Json so you can use binary CSV docs ePub Json PDF text and then Json on load specific I swear there was

Some other stuff here let me just see well anyways we're loading Json so that's what we're doing and then once you do that you're going to add a text splitter right here or token splitter and just so you know what we do here is we go boom and the text Splitters there's character text splitter recursive character Tech Splitters we're using a token splitter so split into chunks by tokens just add that back and just so you can see our chunk size

Is 512 and our chunk overlap is 100 okay from that point on that once this goes this is going to insert the vector uh the document or the text into the um super base as a vector now before you actually do that we're going to have to obviously we're going to actually have to build out the table in super base so I'll show you that in just a second and just so you know after as well this right here I use

A super base update row just because um you'll see here so this is update row this is in the table name here build manually and just so you guys know how to do something like this um I'm going select conditions content and then I'm bringing over the content Json variable from this super base Vector output um that's basically going to match it up and then I'm just adding like the video title the other stuff from my air table just so if

I look in like if you see here if I look in the uh superbase table I have other information here too like publish date all this other stuff description not that you need you really you don't even need that but I just wanted it there just because uh it looks nicer it's easier for me like if I'm looking at it to see what is what Etc so okay now that we're done well we did a bit of the n8n stuff we're

Just going to show you what you need to do in super base so let's just pretend I have a new table well not let's not just pretend I'm going to create a new table right here so you can do this either like in the command line in the SQL editor we're just going to do it in the visual Builder um um I'll do this here to keep it simple for you guys so let's just go like in I don't know Instagram

Whatever that's the name of our table so you can either enable row level security which um yeah I mean it's recommended so I just keep it on in which case though we're also going to need to add a um a condition or whatever you call it a policy or something um so I'll show you that in a second columns so yeah ID created at um we can leave that for now yeah that's pretty much all we need for now um okay

And yeah we're going to need to add a policy so add row level policy and create policy and I'll show you what the policy is let me just check okay so we are keeping it as permissive you can just put the policy name as whatever and you could do insert you technically could do all I'll just do all from this but like if you want to limit it you could just do insert or actually update to so I'm just going to

Do all and I believe it's just true here let me just save this okay so I think that should be it so I added a row level policy if you just saw I literally just put true at the bottom and then I think I maybe did one other thing but um let's just test that out real quick going to duplicate this and run a test real quick just because I want to make sure everything works before we move on here okay

So I'm just going to put this to Instagram uh query name match documents that's fine just going to add this right here and we just okay we don't need to update it and now I'm just going to test this and I'm triggering it on my other screen right here with air table so if this works it should okay so it didn't work content oh okay it's because we need to add the cont okay so this is where the next part comes

In okay so it worked though because I didn't get an issue with the um the like the the what do you call it the like the policy the uh permissions there we go yeah I didn't get an issue with permissions so that means we're allowed to do it it's just that it doesn't have the necessary Fields so to get the necessary Fields I'm just going to go new query so I'm going to SQL editor and new query and I have these

Copy and pasted so this is going to be very easy for you guys and I'll literally put this in the description down below so you don't have to come up with your own queries um so I'm just going to alter the table of Instagram and we're going to add the column of edding okay so they actually I'll tell you what I'm doing here but we're going to have to do one more thing before I do this so add column and embedding

And it's the type is going to be vector and this number right here is going to depend on um your like what embedding model you're using but for open AI it's 1536 okay and then add column and you just look at what dimensions the model outputs on so open AI 1536 different models are different though and then add column metadata that's going to be Json B and then add colum uh content and this going to be a text all right so

We're just going to copy that paste that in here and we're going to run that okay success so if I go to the table go to Instagram you'll see here we have okay so don't actually run it yet don't run it yet because I need to tell you to do something first but for me you'll see we have those two fields we had before we have our Vector embedding field so you see how it's a vector type and we have a

Json metadata field Json b and a text uh field for Content okay now one thing I'll say too is if you unless I'm missing something cuz yes you can insert columns here but um from what I oh okay never mind okay so you can literally okay you don't even okay so I'll tell you what the issue I had before was so I was having a big issue earlier it was very um I was getting a lot running into a lot of

Issues reason reason being is if we go over to databases and then you go over to extensions one thing you need to do before any of this uh is you need to enable the PG Vector extension it's called Vector right here okay so if you scroll the way down or if you go f contrl f and you can search for vector and you'll be able to find it and you need to enable it now once you click enable it's going to

Like ask you where you wanted to enable it to what uh schema Now the default schema it shows you can see here schema it will show extensions all right it will just show there so I didn't know any better at the start and I just clicked okay then I built out the whole table with this under the schema of extensions and then I was running into all these issues and then I I kind of thought it may have been that so

I tried to disable it so I could re enable it and maybe change the schema and I even tried changing the schema in the SQL letter I was running into some issues but long story short I just had to like delete the fun the functions and all these other stuff and then disable vector and then reenable it and then put it onto public so long story short just so you don't mess up and run into issues once you enable the extension

Put it into public re also the once you enable it into public if you go to like functions you'll see it adds all these different functions array to Vector Co cosine distance all these things are from PG vector and it's it's freaking awesome it's awesome this is like really really cool stuff here guys um yeah like I hope you guys are getting some value from this I hope I'm explaining it in a way where you're following along and it's somewhat making

Sense um even if you're a bit new and yeah if you guys I mean you hopefully you're seeing the true value of once you get something like this set up and you can actually use it and there's many many use cases whether you're using it for your own personal knowledge base your companies building different customer service Bots AI Bots you name it this is the type of stuff we do for our business as well as our clients as well so anyways

Um and you're going to get much better output um okay so okay so what I was saying now is yeah I will still leave this uh SQL um this SQL uh query down below so you could run the SQL but actually I just found out if if you install PG Vector correctly you can actually just go here and you will see the vector type under uh other data types so you could just go like this and add embeddings man uh just

By typing it without doing any queries SQL queries or anything like that you literally just click save and you'd be good to go what when you when I didn't have the uh Vector extension enabled correctly I couldn't see this under type it wasn't letting me do this through the interface I could only do it through running SQL queries okay so anyways so yeah you can do it through either like the the user interface if you really just don't want to run

Queries but I like I said I'll leave it that these queries in the description down below anyways and you can just run these regardless um now the one thing is let me see I don't know if I go to Vector here um Define I don't know where you put the number but um yeah maybe if you're if you uh I don't know if it gives you a spot to put the number since I haven't done this let's just see test save

Okay well if you're running into issues doing it since I haven't tried it through the manual way then use the commands down below but it looks like now with the extension uh set up properly you possibly can do it just through the interface okay so what let's just cover where we are at now so we have our table we have the basic stuff we have the embedding we have the mated data we have the content we have PG Vector enabled on

Our super base we have the correct um RLS uh policy setup just to allow us to update and add documents in and we have this flow setup so now that I have all that set up let me just test it into our new table and I'm just going to trigger on my right screen here if everything goes well it should add the document in so look let's just zoom in here we had the trigger boom boom boom we got five items

Here everything ran correctly basically it chunked it into five so let's just see here if I go to super base which is right here okay look at that look at that we got all this stuff we got the embeddings we got the metadata okay and we have the this is the original content as you can see it's chunked into five different vectors with the uh corresponding embeddings for this text okay and uh yeah so now that we have that that's great

Okay we're also going to need and let me just um we're also going to need another there's a lot there's a few different ways you can do this for the sake of this video I'm just going to keep it very simple and show you what the mechanics of this are and then like if you wanted to um you could do this in a separate workflow and you can like put tools and like make it run from the workflow or um or

You can or you can just change this trigger to something out like there's a lot of different options but basically simp just to keep being simple what we're going to do is we're right here we're just adding another trigger um for for testing purposes I wouldn't use this in a in an actual production um uh you know workflow but this is uh just te uh click to test workflow and essentially we're just using a Q&A chain so question and answer retrieval

Under AI nodes okay once you do that I mean the trigger could be anything right but um yeah we're just using like you could use a chat I'm using uh this and then just because I'm using this for testing purposes I'm defining the prompt you could take it from previous node depending on what your previous node is okay then we're using the same model um well we're using actually different model we're using CU that's a embedding model this is a generation

Model we're using open AI chat GPT 40 and then for the retriever we're using a vector okay so let me just show you from scratch how this would look so here so like this and then retriever you have different options contextual compression retriever Vector store retriever multi-query retriever workflow retriever so if you were running it through a different workflow you put the the ID here um but we're going to do a vector store retriever we're going to keep the limit as

Four and then we're going to add our Vector store of super base okay and you got obviously need to connect your superbase account like that goes without saying and um yeah so we're retrieving documents so you can select your table we're doing Instagram here and then our embeddings is the same embedding which is open Ai embeddings and we're using this one right here XT embeddings three small okay I'm just going to go like this delete this and I'm just going to

Duplicate this copy paste where' that go paste he where is that going okay we're just going to add another trigger click manual trigger node let's just go like this for simple sake there we go and then model is open AI [Music] dpt4 okay sorry this looks a bit messy guys I'm just rebuilding it just so you guys can see here once I'm done this I'm going to delete these uh other things here but so click Q&A chain open AI model GPT

40 Vector store retriever super base store okay so we got that right so we have this is all built out what we're going to need though is we're going to need to add a function into our super base okay so there's different ways you can do this too like I'm just going to show you the basics the uh super base also has Edge functions um I'm not going to cover that this video but basically we're just going to go to uh

Database functions so this where I was showing you that there's those uh vectors and PG Vector uh functions once you add that extension to your public schema make sure it's your public schema once again okay this is the the this is the one that we're going to add match documents so edit function um let me just show you this is what I use to create the function and let me just see if I can do this with in here table Vector

Okay from my knowledge I don't know if specifically if you can do it just within I I I personally didn't do it within the the user interface like here create function I use the SQL query reason being is because I couldn't find the type uh for um you'll see here this is a table for the return type and I could not see table I just see different things here so for the sake of this video and if uh if you're following

Along we'll be using the SQL query and I'll leave this in the description down below as well and let me just change this to Instagram okay going to copy this and we are going to go here SQL query going to go new query and to paste this in and I'm actually going to change this to let me just see here I'm going to pick a different name let's just see go up so whatever your query name is here we're actually going

To change this to actually um match documents IG going to change that to that okay and then I am going to go back here match documents IG so this is whatever your query name is um match documents IG match documents IG match documents IG okay so let's run this so it should have worked let's just double check database functions match documents IG so right here now I'm just going to read some new stuff in here under that and then we're going

To going to test everything out so delete this go here and I'm going to test workflow I'm triggering it over on my right screen with a table so it should send the document over here okay we got the those embeddings now let me just check okay embeddings are here now let me come over here and the uh the question that we're asking is let's define this s uh me zoom in a bit give me her um what and believe this video

Is on what can that and chat DPT make animations explain indepth please okay that's just going to be our query because I see that that's what the video was about and let me just see so go ahead and test so that worked let's play this okay it looked like it worked we got no issues it's loading executing node everything goes well here we should get a output and it should basically have information okay at gbt itself cannot directly recreate animations because

Is text based langage model okay so basically this is is direct information from the video right so this is pretty pretty cool I'm not going to read this whole thing and like I would specifically tell it like hey I don't want you to format it like this way and I would obviously do a lot better prompt I'm just showing you the basics of setting up a rag so what we did just to recap we set our triggers up we put the

Uh document into our Vector base we set our Vector base up correctly with the right policies to ensure that we have the right permissions we set up um we added PG Vector as an extension the right way to our public schema okay we um what else did we do we added we added these columns all right make sure that this number right here is what you're embedding model outputs for the dimensions okay and then we ran this SQL query for the

Uh to set up a function that is and yeah you can and yeah make sure oh yeah also to make sure the function number right here is the output of your Dimensions as well for the embeddings the number and yeah there's different ways you can also like uh query similarity and stuff like that I'm not going to get into that too much in this video this is just one like this is I'm going to give you the this copy and paste

Down Below in the description so you can start using this and if you just cop like if you're new to this and you just copy and paste everything I do like with the model with the embedding model with super base Etc it will work all right and yeah if you need to watchh this back and whatever and customize it a bit for your specific embedding model for your specific use case it should be good as well you're just going to have

To change those specific things I mentioned all right re-watch this video but yeah I pretty much just gave you everything you really need know to set up a simple rag system within NAD you don't need to know how to code you don't need to use the command line vs code and do all this you can just do this within n8n a no code drag and drop Builder um and this is like some technically more advanced stuff like not even really this

Is like not super Advanced but it's just like a bit it's way more advanced than the average person using AI or doing this so if you're doing stuff like this you're already headed the curve if you're able to set this up and actually leverage it in in a good way in a really good way so yeah guys um this is definitely uh yeah I definitely think this is a very value packed video and this is something extremely valuable so if you

Actually got value from it and um and yeah if you actually genuinely got value from this then I would definitely appreciate you know subscribing if you're new to this channel we upload videos every single day on AI automation nn make.com business growth marketing you name it uh I run a company executive stride I'm the founder and we help business owners grow and accelerate their stride if you want more information about that um you can check out some of the links in

The description if you're new to this Channel and you haven't joined our free stride Community strid community.com Link for that will be in the description down below we have about 750 to 800 members in our free Discord and free Facebook group so definitely check that out also too guys also too guys if you haven't already checked out my book client acquisition code This Book Is Awesome um super Super Value packed to show you how to get clients on autopilot leveraging AI

So if you're in B2B you're a SAS owner if you're a founder of literally anything B2B if you need more appointments on your calendar definitely check out that book it's literally like the cheap one of the cheapest Solutions um to get your desired result it's super Super Value packed so if feel like you're on a budget definitely check out that book I Port my heart blood blood sweat tears and more importantly my heart my school into that book it took me

So long to write um and then other than that guys if you want help growing your business you can check out executiv stride. comom apply we help our clients in many different ways set up systems automations uh we help them set up infrastructure AI infrastructure AI cool callers AI appointment sets we help our clients book appointments on autopilot leveraging this runp paid ads run paid traffic scale the 10 20 50 100K a month whatever you know depending on where you're at

So that sounds like something that you want some help with if you already run a business and uh you just need some some uh some you know uh some some uh if you just need some pour some gas on that fire and really just you know uh blow up like a rocket ship then um yeah this is your call just book a call down below and you'll speak to myself and my team and see if we can uh if it's a

Good fit or not other than that guys um yeah I will see you in tomorrow's video hope you got this if you hope you got some value out of this if you have any questions let me know the comments down below keep hustling keep grinding and of course guys accelerate your stride take care

Latest from the blog

View all