Transcript of How to Build a Local AI Agent With n8n (NO CODE!)
Video Transcript:
In this video, I'll show you how to build a local AI agent in just a few minutes using N8N. We'll be using N8N to build the agent and Postgres to act as our vector search database and to store our conversation history. This means you'll be able to retrieve information from something like a CSV file, Word document, or a PDF and bring that into your agent. All of this is completely free and you don't need something like an OpenAI account or any other paid service. You can do all of this on your own machine. But first, let's have a quick look at what we'll be building. Here we have an instance of N8N that's running on my own machine. We're using Olama to run the AI model on our own machine. We're using Postgres to store our conversation memory and we're also using Postgres as our vector database. This agent is also able to retrieve real-time information from the web using a web search tool. I will show you how to set all of this up step by step in this video, but first let's run this. So let's open up the chat and let's just zoom into our agent a bit. So let's start by saying "Hello, how are you?" And our agent says "I'm doing well, thank you. How about you? Is there something I can help you with?" Now I've already uploaded a document into my knowledge base containing questions and answers related to a fictitious race to run. So I'm going to ask "What are the current specials according to my knowledge base?" Now do take note you can upload any documents that you want. So these could be study notes, invoices, whatever. And I will show you how to do all of that in this video. We can also ask our agent to retrieve information online. So let's ask it "What is the latest news from OpenAI?" and it's calling the web search tool. And after a few seconds we get this information back. Or let's try another example. Let's say "What is the current weather in New York?" So we can run this. The web search tool is being called. And now we're getting back to weather in New York. How cool is that? We're not limited to these tools only. We can click on the tool node and here you will find a lot of different tools offered by N8N. You've got integration with things like Airtable, Gmail, and all sorts of other platforms. So the sky really is the limit here. But for this demo I think these two tools are enough. So let's dive in and let's build this. Before we begin I do want to mention that N8N already provides a self-hosted AI starter kit. I will leave a link in the description to this article, but we will be deviating from this quite a bit. In the N8N template they use two different databases to deal with vector search and conversation history. I prefer keeping things simple, and for most of my projects in the real world I use Postgres. The first thing we need to do is install Olama. Olama is an app that you install on your machine and it allows you to run these large language models locally. So go to olama.com and click on download and then download and install Olama for your operating system. After installing Olama you can verify that everything was set up correctly by opening up your command prompt or terminal. Then simply enter Olama and you should see this list of commands. Then all you have to do is download a few models. If you click on this model page we'll be downloading two different models. We'll use lama 3.2 as our chat model and we'll download Nomic embed text which will vectorize the documents for our database. Let's start with lama 3.2. This is a tiny model and I've been able to run this model on super old hardware so I'm pretty sure you can run this on a potato. It's only about two gig big so what you have to do is click on this command then simply add that command in your terminal and press enter. The first time you run this it will download the model and afterwards you'll be able to test it by sending a message like hello. If you get a response back everything was set up correctly. Now I'm just going to exit out of this by typing front slash by and let's download our second model. So back in the list of models let's download Nomic embed text. So I'll click on this let's copy this command and let's run this in the terminal as well. Awesome so we are now done with setting up olama. So the next thing we have to do is set up our database. This is where our conversations will be stored as well as the content of our documentation like our CSV files or PDFs. Now since we want to run everything locally we will use docker to run our database. So simply go to docker.com then click on download docker desktop and download and install docker for your operating system. After starting docker desktop you should see a screen similar to this. So currently we don't have any containers running. Now to set up our Postgres container simply open up your command prompt or terminal again and enter the following command. You can find this command in the description of this video. Basically all we're doing here is saying docker needs to run this container called Postgres vector and then we have to set a few environment variables. The first one is the username for the database which I'll simply call my user but of course if you want to change it this is where you have to do that. Then we also have to export the password which I've set to my password and finally we also have to set the database name which I'm simply going to call my database. Then we're telling the container on which port to run so you don't have to change anything here and then finally we're providing the name of the image that needs to be copied. You don't have to change this either. This image simply creates a Postgres database with a vector extension automatically applied. So let's run this command and this will now download all the dependencies for Postgres and create a container for you and if we go back to docker desktop we can see that our database is currently running and finally all we have to do now is set up n8n and this couldn't be any easier. The only dependency we have is to install node.js so go to node.js.org and then download and install node for your operating system. Then open up your command prompt and run the following command npx n8n start and this will now download n8n and automatically start it and we can access n8n from this URL. So you can simply copy this or control click to open it up in your browser and welcome to n8n. This is probably one of the easiest ways to build AI agents in 2025. Now you won't see anything on this dashboard so what we'll have to do first is create our workflow. So click on create workflow and let's give this a name like my AI agent. Now let's start by adding our very first node. This is called a trigger. Now triggers could be anything this could be an event from another application for example when you receive an email you might want to start this workflow automatically or if you add an entry to a database table it should trigger this workflow as well. You can also run these workflows on a schedule you can call it from a web call from a form submission etc. We want to chat to our agent so let's add the on chat message node. So this will bring up all the settings for this node which we don't have to change at all. Let's go back to the canvas and then let's add our second node and this is called an action or processing node. Here we can do things like transform data or call actions in other applications. Obviously we want to build an AI agent so let's click on the advance AI option and then let's add an AI agent. Right now we can see all the settings for our agent and the only thing I want to change here is to add an option and let's add the system message. We can use the system message to change things like the behavior of the agent we can set its persona and specify any rules that it needs to follow. For now let's simply say you are a helpful assistant. In fact let's add a rule section and for the first rule let's say respond in a natural and friendly manner. That's all rules we have to add for now. Now if we go back to the canvas we can see that this agent can take in a few inputs. We need to assign a chat model and optionally we can assign things like memory and tools. If I click on this plus button it shows all the different LLM providers. Now in this list there is this olamocat model option. Now do take note that we're actually not going to use this node for the whole tutorial and I will show you the reason for that. But for now let's simply add this node then under credentials click on create new credential and you might notice that if you try to save this you'll get this error message. If this happens to you simply replace localhost with 127.0.0.1 and try to save again. This time the connection is successful and I can now see all the models that I've downloaded on my machine. We will be using llama 3.2 and the 3 billion parameter model. Great we can already chat to this agent by opening up chat and it's a hello. And look at that we get a successful response back and this didn't cost us a cent. Next let's have a look at this memory node. The memory node is used to store our conversation history. Let me show you something. If I said my name is Leon our agent says nice to meet you. Leon is something I can help you with. But watch what happens when I ask it what is my name. Suddenly the agent doesn't know what my name is. That is because at the moment the agent has no way of keeping track of our conversation history. So every message we send is seen as a new and standalone message. So what we can do is add memory and what a lot of tutorials will do is to add this simple memory node which stores the conversation in local memory. But there is a limitation with this as well. So let's add that node and let's test this in the chat. So I'm going to start a new conversation and it's a my name is Leon. So this is great nice to meet you and let's ask what is my name. And this time the agent is able to retrieve our name because it's got access to this memory database. The limitation with a simple memory node though is that if we start our N8N server and started it up again. So it's running again. We can then open up the chat and ask what is my name. And although we're simply adding to the same conversation the agent all of a sudden does not know my name again. So the problem with this node is that you will lose your conversation history if your N8N instance had to restart. So a better solution is to use a persisted database like the Postgres database that we set up earlier. So all we have to do now is click on add. Let's select Postgres chat memory. Then let's click on credentials create new credentials and for the host we can leave it on localized. For the database name we have to use the values that we created earlier. So mine was my database and for the user I had that as my user and for the password I had that as my password. And scrolling down we don't have to change anything. And the port that we set was indeed 5432. Let's save this. We can see the connection to our Postgres database was successful and if this isn't working for you then you need to ensure that this database is up and running in Docker desktop. So I can see the status of my database is currently running. Cool then that's all we have to do on this node. Let's go back to the canvas and let's click on open chat. I'm going to start a new conversation. Let's say my name is Leon. Then let's ask it what is and it says your name is Leon. And let's see what happens if we terminate our N8N instance and let's start it up again. Cool and now that N8N is back up and running let's ask what is my name. And great the agent was able to recall this information. Right now let's have a look at adding tools to this agent. When we click on this add tool button you will find plenty of integrations with external systems. So for example you could search for Gmail and this will allow your agent to read and send emails. And there are plenty of other integrations as well. I personally like to assign a calculator tool as sort of the default tool because these LLMs are very bad at doing math. Now let's have a look at adding our custom knowledge base. Click on tool and under vector stores add Postgres PG vector store node. We've already set up the credentials to Postgres in the memory step so we don't have to do anything else. We're simply reusing the same credentials. Then under operation mode let's select retrieve documents and for the name let's call this knowledge underscore base. And in the description we need to tell the agent when to use this tool. So let's say use this tool when asked questions from the custom knowledge base. And for the table name we can give any name that we want. I'm actually going to call this one n8n tutorial. And then under limit we can specify the amount of documents that will be returned from the database. Let's just leave that at 4 for now. Then let's go back to the canvas and let's add our embedding model. Let's click on embeddings. Let's click on embeddings ollama. And for the embedding model let's use the gnomic embed text model which we downloaded earlier. The database is empty at the moment as we haven't uploaded anything to the knowledge base yet. But irrespective watch what happens when we try to run this now. So let's just say hello. And because we haven't asked it to search up any information the vector store wasn't called. So now let's ask retrieve the current specials from the knowledge base. This time it is calling the knowledge base but then we received this error message. And this is a very common issue that people seem to run into even when using the n8n starter kit. Now thankfully the fix is really simple. We will simply replace this ollama node with something else. So let's delete this node. And under chat model let's add the openai chat model node instead. Now please don't worry we will not be using openai and this will not cost you anything. Click on credentials. Click on create new credential. And then in this base url we're actually going to replace the endpoint from openai to the endpoint to ollama instead. Remember to add this front slash v1 at the end. And believe it or not that's all we have to do. This openai node is simply acting as a wrapper around ollama and allows us to call the vector store. Now in the model list we will actually see the models that's on our own machine. Let's select llama 3.2, the 3 billion parameter model. So I'm just going to rename this node to ollama and I'm actually going to rename this node as well to memory. And then for our tools I'm going to rename this node to knowledge base just to keep things clean. And another tip for keeping things organized is to add this sticky node. And we can actually place this behind these nodes like so. That looks way better. And if we double click on this node we can change the text. I'm simply going to remove all of this and let's also change the color to something like green. Cool. And in fact while we're doing all of this let's add one more tool. So let's click on tools and let's add SERP API. And this will allow our agent to go online and perform a google search. So in this drop down click on create new credential. Here we have to provide an API key. And in order to add that key go to SERP API and register for your account. Then copy your key from this dashboard and add it to n8n. Then click on save. And that's all we have to do for this. Our agent now has access to real-time data. We will have a look at populating the database in a second but first let's give this a spin. Let's say hello. Cool. We get a response back. Then let's say what is the current weather in New York. And we can see in the flow that our SERP API tool was indeed called. And we're getting the correct response in the chat. So how do we add documents to our knowledge base. Well that's super easy. Let's go back to our dashboard. Let's create a new workflow. And let's call this add documents. Now there are many ways of getting data into your database. The simplest is to add a new trigger. And let's add this on form submission trigger. Let's give a form a title like add documents. And for the first field let's call this file. Let's change the type to file. And I'm going to disable multiple files. And let's enable required field. All right. Let's go back to the canvas. Let's click on test workflow. And now n8n is giving us this form. I'm going to select a file. So I've got this Q&A document for this restaurant called Oak and Barrel. And I'm going to submit this. Now you could load anything. This could be a PDF document, a CSV file, whatever you want. After running this node we can see the output is this file. This binary file. So what we want to do with that file is to add it to our vector database. So I'm going to search for Postgres. And let's select Postgres PG vector store. And for the action let's select add documents to vector store. Simply select the credentials that you created earlier. And for the operation mode select insert documents. Then for the table name let's rename this to n8n tutorial. And I think that's all we have to do. Make sure that the name that you provide here for the table name is the same as the table name that we created in this agent. So if we go to knowledge base we call the table n8n tutorial as well. Cool. So let's go back to add documents. And now let's add our embedding model. So under embeddings let's select embeddings olama. And from this list of models let's select gnomic embed text. Cool. Then for document let's select default data loader. And here you need to change the type of data from JSON to binary. And we'll leave mode and data format on the default values. And finally let's add a text splitter. Let's add the recursive character text splitter. And I'll just leave these on the default values. Cool. Now let's test this workflow. I'm going to select that file. Let's submit this. And just like that our data has been loaded into the database. So let's go back to our agent. So let's click on my agent. And now let's ask a question related to our database. Like what are the current specials as per my knowledge base. And looking at the workflow the knowledge base was indeed called. And we get the correct response back. If you found this video useful then please hit the like button and subscribe to my channel for more innate content. Also check out this other video where I show you how to build an AI agent that's got vision and voice. I'll see you in the next one.
How to Build a Local AI Agent With n8n (NO CODE!)
Channel: Leon van Zyl
Share transcript:
Want to generate another YouTube transcript?
Enter a YouTube URL below to generate a new transcript.