How to set up Supabase + Postgres for RAG Agents with memory

Rory Ridgers walks through setting up Supabase and Postgres in n8n to store chat history and document embeddings for RAG agents. He creates a Postgres table for messages, configures a vector store with OpenAI embeddings, uploads files through a chat widget with a 1,000-character chunk size, and queries the stored documents from an AI agent.
Chapters
What's up guys, in this video I want to show you a really, really quick and easy way that you can get Postgres and Supabase's vector store up and running so you can use it in your AI agents. Specifically, this is going to be used for RAG agents, so when you need to post data to a vector store and then also retrieve data from that vector store, it's just the easiest way for the AI agent to handle large quantities of data. So to begin with, we're in NaN, and all we're going to do just to test this out, we're going to add a chat message node. We can worry about that in a minute. Then we're going to add an AI agent step. Really simple, we're just going to add in our OpenAI credentials. So obviously this is hooked up to GPT-4 Mini in this case, and you're probably all familiar with this. But now what we need to do is when we go to memory, we want to add a Postgres chat memory. And obviously I've got this set up, but I'm going to show you how to set up a new account. And then we also want to set up— if we type in Supabase You can see we get this Supabase Vector Store. We're just going to click on that, and again, I've got my credentials set up. I'm going to show you how to set up a new account here. Okay, so we've got our Postgres chat memory, and then we have our Supabase Vector Store. And then to do this, obviously we're going to have to go over to Supabase, so I'll show you inside there. So when you go over to Supabase, you're obviously going to have to click sign in, and then you'll be taken to this page after you've created your account to create a new workspace on Supabase. So you can keep all of this pretty much blank. You can do the project name. I'm just going to say something like na10test. And then you can keep this all the same. The database password, you're gonna have to put something here, and you have to remember this because you're gonna have to input it into your Postgres credentials in a second. So make sure you do a password that you remember. Okay, so I've just done my password. You can change your region if you like, doesn't really matter. But we're just going to hit Create New Project, and then we should be taken to another screen. You can see at the moment it's saying setting up project, and this does take a second to to set up our project. So we're just going to let this run and come back to it in a minute. While this is running, I'll quickly introduce myself. My name is Rory Ridgers. I help business owners to integrate AI agent systems into their businesses. So if that's something you might be interested in, then I've left a link to my website in the description. You go ahead and book up a call, we can talk about some ways to integrate AI agents into your business. I also have a platform called Commonus where I post all of my AI agent templates that you see in these videos for completely free. You guys can go on there, download these templates, and then you can also post your own templates on there for absolutely free. So if you want to check that out, there'll be a link to that in the description. You can browse all the templates on there. So this has been running for a minute, it should almost be done. By the way, if you don't really know what Postgres is, you kind of heard of it, don't really know what it is, basically all you need to know is it is an open-source framework for storing data, and you can do it on platforms like Supabase. They make it really, really easy to set up and get started with Postgres. So basically, in our scenario, all that's going to be happening is, as we have Postgres chat memory as our memory, every time we talk to our AI agent, a copy of that chat will get saved to a table inside of Supabase. And then your AI agent can really, really easily query that data in that table. It just makes it really, really simple. Okay, so you can see the project status is set to green now, which means we're good to go. Our account has been set up. All we're going to do to set up our Postgres account is we just head over to connect here. You can have a look around at everything here. We're just going to head over to connect, change this to psgl, and then we're going to scroll down. We're going to open this one up, and you can see we get these different credentials here. So if we go back over to NAM, let's add a new credential, and then we have all of these different data points that we need to fill out. So host, database, user, password, maximum number of connections, SSL and port. The things that we're mainly going to be focusing on are port, password, user, database, and host. And if you have a look at this little strip of text here, we got all of it here. So the H is going to be the host. So what we do is we highlight that, copy it, come over to an8n, paste it into there. The database, let's have a look if we scroll along. Database is just going to be Postgres, so we're going to leave that as it is. User labeled with U. We just copy that, paste it into the user, and then password. This is going to be the password that you made a minute ago, so make sure you've remembered that. Put that into here. Okay, so I put my password in. Now we're going to scroll down. We're going to leave maximum number of connections as it is. We're going to leave SSL as it is. And then the port, by default it's 5432, but if we come over to NAN, you can see port labeled with P, it is 6543. So we're just going to copy that come back over to NAN, paste that into there, and then let's hit save and see if it works. Okay, then as you can see, connection tested successfully, so perfect. Let's come out of that. And then the table name, this is just going to be the name of the table that we want to save these messages to, so I've just done NAN_chat_histories. And then that's pretty much all you need to do. The session ID is going to be from the connected chat trigger node, so this chat trigger node here. And to test this out, we're quickly going to disconnect this one here. And then if we open the chat and just say something like, hey, you can see it's used this tool. We got the little tick, which means it's worked and it's used it twice because it saved the hey. And then it's also saved the hello, how can I assist you today? And then if we come over to our Supabase account, we can see we're in the correct table. And you can see here, this is the conversation we just had. So hey, and then hello, how can I assist you today? So really, really simple. That's literally all we needed to do, and now that is set up perfectly. So that is going to save all of our messages into that table on Supabase, which of course makes it really, really easy and accessible to get access to these chat histories for whatever reason. So now what we're going to do is we're going to connect up the Supabase Vector Store node. So let's just connect this back up quickly, and then we're gonna— and then we're gonna create a new credential here. So I've already got one set up. Let's hit create new credential. You can see here we have to put in a host and a service role secret. So to find that, what we do is we just come back over to Supabase, go to project settings. If we go to data API, you see we get this project URL. Just hit copy on that, come back over, put that into here. And the service role secret is going to be this API key here. So I'm going to reveal it and copy it and then paste it over. Okay, so I've copied it. Let's paste it into here, hit save. And then, yeah, credentials saved successfully. Perfect. So you can see the operation mode we've set it to. There's a couple things you can do. We set it to retrieve documents and then as tool for AI Agent. And then name, let's give this his name. Let's just say, uh, documents. And then the description, our valuable documents we share in chat. Let's just say something like that. And then the table name, we don't actually have a table for this yet. So what we're going to do is we're going to go to docs, and then if we scroll down, we see this link here. So quick start for setting up your vector store, hit that, and then we get this bit here. And this is an SQL command. So what we're going to do, copy that, come back over to Supabase, go to SQL editor, paste that into there, hit run, and then we should see success, no rows returned, which is actually a good thing. Basically what we did there is we just made a new table for our documents to go in for a vector store. So if we go over to table editor, you can see now we have this new documents table that wasn't there before. If we go back over to NAN, you should see table name. So yeah, documents, we can hit that. And then basically all the documents that we upload via this node on NAN will just simply go into that documents table on Supabase. So we can come out of that. One more thing we need to do is we need to add an embedding model. And really, really simple, we can just add the OpenAI Text Embedding 3 small model. So really, really simple. You should already have your OpenAI credentials set up if you've got this node here. So really, really simple. And I know what you're saying now— there's no documents in this table, what are we retrieving from there? So I'm going to show you how to also upload documents to your vector store on Supabase via NAN. So if we quickly disable this here and then we start a new little workflow here just so I can show you sort of what we're doing. If we hit an add, let's go to Supabase and then Supabase Vector Store. And this time we want to add documents to the vector store. Got our Supabase account hooked up. Operation is insert documents. The table name is going to be so documents. And then the embedded batch size, you can kind of do this to whatever you want. And then embeddings again, and we're going to use the OpenAI node. Bring that over here. And then document, so we're just going to do the default data loader. And basically what this means is just going to auto-detect a document when you upload one, just makes life a lot easier. So the type of data, we can say binary because it's probably not going to be JSON unless you're doing JSON, obviously. But if you're just doing files, you can do binary mode. Load all input data, we're going to do that. And then the data format is going to be automatically detect MIME type because it just means if we upload different types of documents, so for example CSVs, PDFs, JSON files, uh, DOCX files, uh, this node will just automatically detect the extension and just convert it in any way that it needs. So if you're just uploading one specific file type, maybe select that from here, but if not, just do automatically detect. So we're going to hit that, and then we have to add a text splitter. So we're just going to do character text splitter here, and this is basically just what the vector store is going to chop up your documents in when it gets uploaded, because it has to chop it up to be able to vectorize it. So for the separator, I've just done 1,000. Chunk size is 1,000. Chunk overlap is 200. So we're going to come out of that, and that kind of varies depending on the size of the documents that you're uploading, but you can just kind of leave it at that. That's kind of just the default settings. So to test this out, what we're going to do, just double-click on this, hit Add Field, and then allow file upload. So we're actually able to upload files via this little chat widget on NAMM. So open the chat and then let's try to upload a file. So I'm just uploading this PDF that I've got on prompt engineering. I'm just going to say— you can say anything to it, you don't actually have to say upload this file or anything. It just needs a text input as well. For some reason it's just sort of a thing with NAMM, you can't upload just files, you have to upload a text sort of message with that file. It's weird. So you can just say something like, hey, and we should see this run. And then, okay, yeah, it's giving us all of this stuff back. Don't really want all of this, but it doesn't really matter. If we come back over to Supabase and then the documents, you can see here, this is what we've got. Perfect. So this is the content from that PDF that I uploaded. Um, you can read through all of this if you want, it should all be there. But yeah, that's just the content from the PDF that I uploaded. And I mean, it did that literally instantly. So what we're going to do now is we're going to come back over to this other workflow and we're going to see if it can actually retrieve that data from that PDF. So let's remove that, hook this back up to here, and then let's say something like, what data do I have in my vector store? So I've said, what data do I have in my vector store? So you have to be quite specific with it, like saying, what data do I have in my actual vector store, rather than just saying, uh, what files do I have uploaded? Because we haven't actually got a prompt for this AI agent. I'm I'm just doing this with default prompts, so it doesn't really matter for these small little showcases. We can kind of be just more specific in our user prompt. But you can see it said to us, could you please provide more context or specify which vector store? So maybe we do actually need to give it a little bit more context here. We're going to say, search my Supabase vector store for content. Okay, so actually had to quickly add a system prompt to the AI agent. Let's just saying use the Supabase vector store tool to retrieve data from my vector store. So really, really simple. Just because it wasn't getting that, I had to use the tool. But no worries at all, obviously it's a really, really simple prompt. And I've just said to it, what data do I have in my vector store? And this is what it's given us. So this doesn't really make sense because the PDF had pictures in it and stuff, which made it make a little bit more sense. But you can see here, this is the exact data that is in our Supabase vector store here, which is absolutely perfect. So Now you should be able to upload things to your vector store on Supabase, retrieve things from that same vector store, and then also save your chat histories to a table on Supabase as well. So really, really cool. So I do hope you enjoyed the video, guys, and you learned something new. As I said previously in the video, if you were looking to work for me so we can implement some AI agent systems into your business, then I've left a link in the description to my website. You can go ahead and book up a call or just fill out the contact form and I'll get back to you. But yeah, that is it for this video. You should now be well equipped to create your own RAG AI agents and upload things to vector stores, retrieve things from vector stores, and also store your chat history in a Supabase table. So thanks for watching. I'll see you in the next video.
More from Rory

Dynamic Workflows are crazy. This is how to actually use them (beginner's guide)










