📱

Get Our Mobile App

Take your business learning on the go!

Download on the App StoreGet it on Google Play

How to Build a WhatsApp RAG AI Agent with N8N (Free Template)

Rajeevdaz21:27

Transcription

In this video, we're going to build a WhatsApp rag AI agent. That means we'll connect WhatsApp with an AI model that can not only chat but also fetch accurate answers from our own knowledge base using retrieval augmented generation. Let me show you a quick demo on WhatsApp. I asked, "Cash on delivery available?" Now our workflow has triggered. It takes the information from our knowledge base and we got the response. "Yes, cash on delivery is available for selected locations." Now, let's ask another question. "I need a winter jacket." It fetches the information and gives the response with some details of the product and price. Next, let's ask, "Please share website link to order." And here we go. "You can order from our website, amazon.com." When we ask a query that's not available in the knowledge base, it replies, "One of our team members will assist you on this." So, let's get started.

First things first, we need an N8N instance. I always suggest railway.com as the best platform to host it. Their infrastructure and user interface make it easy for anyone to install and manage N8N in just a few clicks. The plans are cheaper than most other platforms starting at just $5 a month, which includes great resources like 8 GB of RAM and eight virtual CPUs. That's perfect for beginners or intermediate users. And if you ever want to scale, you can upgrade to higher plans without any hassle. If you use the link in my video description, you'll get a $1 credit as a bonus, which you can use to install and test it for free.

To install NAT, just go to the railway dashboard, tap on new, then deploy a template, and search for N8. You'll see multiple templates. The first one is the most popular, but for now, I'm choosing this minimal one, N8N with Postgress. Tap on deploy to install. Once that's done, just tap here and you'll get the N8N dashboard link. Anytime a new version is released, just return and hit redeploy to update everything. It's that simple. I've added the link in the video description. Feel free to check it out.

Okay, let's start creating our workflow. First, let's add the WhatsApp trigger. Tap on the plus icon. Search for WhatsApp, then select WhatsApp Business Cloud. Set the trigger as on message. Now we have to connect our credentials. This is not just a WhatsApp account. This is a WhatsApp business cloud account. Even if you don't have a WhatsApp business cloud account, you can simply create one. There are many YouTube tutorials available on this. So you can check them out here. We need the client ID and client secret to connect our WhatsApp account. For that, go to business.fas.com and log in with your Facebook account. Once done, tap on settings. Go to the app section and here you'll see the app you created for setting up WhatsApp business cloud. Tap on the three dots and select open in app dashboard. This will redirect us to developers.fas.com. Once it loads, tap on the drop-down in app settings and select basic. Here you can see the app ID and app secret which are the same as the client ID and client secret. Let's copy the app ID and paste it here. Now go back, tap on show to reveal the app secret. Copy it and paste it here. Tap on save and our account is connected successfully. Just close this. And here we have to do one important setting. Click on add option. This will enable something like receive message status updates. By default, the node setting is going to trigger whenever a message becomes delivered or seen, which may cause errors in our workflow. To avoid that, just delete it and keep this empty. Let's execute the step. It's now listening for a test event. Now, just go to WhatsApp. And I'm messaging the WhatsApp number connected with my WhatsApp business cloud account. Let's send "hi." And back to the N8N dashboard. Here we go. It triggered our workflow. Here is the text message we sent. Back to canvas.

Now let's add an AI agent node. Change the user message to define below. In the prompt column, just drag and drop the text body string like this. We're not setting a system message right now. We'll do that later. Next, let's add a chat model. Here we can choose any chat model. I'm using open AI. Let's keep the model as GPT4.1 mini. Back to the canvas. Now let's execute the step. It's done. Let's open the node. And here is the reply. "Hello, how can I assist you today?"

Next, we have to add a WhatsApp node. To send the reply back to the user. For that, just tap on the plus icon, search WhatsApp, and select WhatsApp Business Cloud. Set the action as send message. Here the credentials required for the WhatsApp trigger and the send message node are entirely different. So first we have to set up the credentials for the send message node. Here we need the access token and business account ID. To get that, just go back to developer.fas.com. On the left side, just tap on the WhatsApp drop-down button and select API setup. First, we have to select the WhatsApp number on which we want to set up our AI agent. Just copy the WhatsApp business account ID. Go back to the N8N dashboard and paste it here. Next, we need the access token. Here, we can generate a temporary access token, which can only be used for a short period of time or just for development purposes. To get a permanent access token, just go to business.fas.com. Again, tap on system users, then generate a token. Select the app and set the expiration as 60 days or never expire. I am selecting never. Then click on next. Here we have to assign the permissions. Click here. Scroll down to the bottom and choose these three options. Click on generate token. We have to verify our account by entering the OTP we receive on our phone number. Once done, you will get the permanent access token. Just copy it. Go back to N8N and simply paste it here. Hit save. And now our account has been connected successfully. Close this. Next, we have to select the sender phone number. Done. In the recipient phone number column, we have to set an expression. For that, just select the WhatsApp trigger. Then drag and drop the ID string. This will dynamically add the user's phone number who messages us. In the text body column, we have to add the AI agent output. Just drag and drop the output string like this. Now, let's execute the step. And it's done. Let's check WhatsApp. And here we go. We got the reply.

Okay, now we've created the AI agent part. Right now, it's only responding based on the AI's general knowledge. But the real power comes when we connect our own knowledge base. That's when it becomes a rag AI agent. For this knowledge base, we can connect something as simple as a Google sheet or a Google doc. But the best practice is to use a vector database. So what's a vector? Think of it as turning text into numbers that represent meaning. For example, the words "car" and "automobile" would have vectors that are very close to each other because they mean almost the same thing. By storing your knowledge as vectors, the AI can search and match information much more accurately instead of just looking for exact keywords. It's not a complicated process. You can do it in just 5 minutes by following these simple steps.

Here I have created a Google sheet that contains some information suitable for an e-commerce business. I've added some general FAQs like "what's the return policy?", "Do you provide free shipping?", and "is cash on delivery available?". I've also created another sheet with product information such as product name, category, size, price, and description. Now, we are going to convert this data into vectors.

First, let's add a manual trigger. Then, add the Google sheet node to pull the data from there. Set the action as get rows in sheet. Select the document. Next, we have to select the sheet. So first, we are going to vectorize the frequently asked questions data sheet. Later, we'll do the same for the product info as well. Now let's execute the step. It's done and we got 21 items here. Next, we're adding a code node to get the output structure suitable for vectorizing the data. Here you just have to paste the script which I will provide in a blog post. Now let's execute the step and it's done. Here is the output structure suitable for vectorizing the data and this code node will also handle our next product info sheet as well.

Now we're ready to convert this data into vectors and for that we're using Superbase. Superbase is an open-source alternative to Firebase. It lets us store and manage our data and it also comes with a built-in vector database which makes it perfect for building our rag AI agent. The good part is it has a free plan that we can use to get started. First, we have to set up our credentials. For that, go to superbase.com, create an account and it will ask you to create an organization. Simply enter a name and in the plan section, select the free plan. Then hit create organization. Next, we need to create a new project. Just put a name. Create a strong password which we'll require later for setting up the AI agent chat memory. And always choose the region closest to your users. I'm using Southeast Asia. That's it. Now, just tap on create new project.

Next, we have to take the host name and service role secret. For that, go to the project settings, then data API, and here you can see the project URL. This is your host name. Simply copy it. Go to the N8N dashboard and paste it here. For the service role secret, go to API keys. Here you'll see the service role secret. Tap on reveal. Copy it. Go back to the dashboard and paste it here. Hit save. And our account is connected successfully. Now, we have to create a new table to store these vectors. For that, just go to the N8N documentation and scroll down to find quick start for setting up your vector store. Open it and scroll a bit to find the SQL script. We just have to copy this. Then go to Superbase, open the SQL editor and paste the copied SQL script. You don't have to change anything. Just run the script. Once you get an output like this, it will create a new table named documents. And here you can see the newly created table. Currently, this is empty and we are going to save the vector data here.

Now go back to the N8N dashboard. Refresh the tables list and you'll be able to see the newly created table. Just select documents. Next, let's keep the embedding batch size as the default value 200. Tap on add option and set the query name as match documents. Now tap on the plus icon and select default data loader. Keep everything as default. It's just a simple way to feed our data into the vectorization part. Next, go to the superbase node again and we have to add an embedding model. An embedding model converts our data into numbers called vectors. So AI can understand and compare it much better than raw data. Here we have different models available. I'm choosing the OpenAI embedding model which is really cheap. You can check the pricing on the Open AI website. Let's choose the model as text embedding 3 small. Back to the canvas. Now we're ready to convert it into vectors. Let's execute the step. It's processing and done. Let's open the node. We got 21 items here. Let's check the superbase table. And here we go. Here is our data and here are the vectors.

Next, we have to vectorize the product info as well. For that, in the Google sheet node, just select the second sheet, product info. Let's execute the step. It's done. And we got 15 items here. Next, let's execute the code node. As I mentioned before, the code node will also handle our product info sheet to make the structure suitable for vectorizing the data. It's done. Back to the canvas. And now let's execute the superbase node to convert our data into vectors. It's processing and done. We got 15 items here. Let's check the superbase table. Refresh the page. And here we go. Here is the product info converted into vectors.

Now we've completed the vectorizing part. Next, all we need to do is connect the superbase vector store with the AI agent so it can communicate with the knowledge base and answer based on that. Let's give the tool description as "Call this tool to fetch general FAQs and product info." We have to choose the same table documents. Next, we can set the number of top results to fetch from the vector store related to the user query. By default, it's four, but we can change it if needed. Now, click on add option and select query name. At the time of vectorizing the data, we used the query name match document. So we need to use the same here to fetch the data from our vector store. Next, we have to connect an embedding model. I'm using the same OpenAI embedding model. Once that's done, back to the canvas, let's unpin the data and activate the workflow.

Now to check, let's send a message. "Hi." And we got the response. Now let's ask a question related to our knowledge base. Let's ask, "Free shipping available?" And we got the response. "Free shipping is available for orders above $50." We can see here it took the data from the knowledge base. The message also contains some extra text like "If you have any other questions or need more details, feel free to ask." So we can adjust the response and tone by setting a system message. For that, just open the AI agent node, click on add option and select system message. Here, let's add some instructions. "You are a helpful WhatsApp support agent. Your function is to respond to user queries by retrieving data from the Superbase tool. Keep the answers short and relevant. At any point, if the answer isn't available in the retrieved data, just reply, 'One of our team members will assist you on this.'" Back to Canvas and simply save this.

Now, let's ask a product question. "I need a yoga mat." And we got the response. "The yoga mat is eco-friendly with an anti-slip grip along with some more details. Then it is priced at $25." And here in the Google sheet, we can see the product price and description. Currently, our AI agent doesn't have memory, so it can't remember previous messages. For example, here it asked, "Would you like to know how to place an order?" If we reply "yes," it won't respond properly because it cannot recall the earlier messages. To fix that, let's add a memory tool. Tap on the plus icon and you'll see multiple options available. We could use the simple memory, but I suggest using Redis or Postgress. Since we've already set up Superbase, we can just use the Postgress chat memory option.

First, we have to set up the credentials. For that, go to Superbase. On the top, you'll see an option "connect." Click on that and scroll down to the second option, transaction pooler. Click on view parameters. And here you'll be able to see the details. Copy the host name and paste it here. Next, copy the port and paste it. The third one is the database name. It's the same, so we don't need to change it. Next, copy the user and paste it here. For the password, enter the one we set during project creation. The rest of the settings can be left as default. Hit save. And now our account is connected successfully. Simply close this. And here, select define below. Next, we have to set the key. For this, we'll use an expression to get the user's phone number. Open the previous execution. Take the last one and click copy to editor. Now open the Postgress chat memory node and select WhatsApp trigger. Simply drag and drop the ID string into the key column. This will be unique for every user messaging us. Next, keep the table name as default. Here it will create a new table called N8N chat histories. The next option is context window length. This defines how many past interactions or previous messages the model receives as context. By default, it is five, which means it will take the last five messages as context. You can increase this number if needed. Back to the canvas. Let's unpin the data and save the changes.

Now, let's send a message. "Hi." We got the response. "Hello, how can I assist you today?" Now, let me open the execution to show you how the memory works. Here we can see it called the chat memory tool to save the messages. When we check in Superbase, we can see there is a new table named N8N chat history. And here are the saved messages.

Okay, now it's time to do the final test. Let's ask a general question. "Cash on delivery available?" We got the response. "Yes, cash on delivery is available for selected locations." And here we can see this is included in our knowledge base. Now let me show you a live execution so you can understand how it takes the data from Superbase and answers accordingly. First, I'll deactivate the workflow and then execute it. Now it's listening for a test event. I'll send a message from my phone. It's processing the message, retrieving the data from Superbase and it replied. Let's open WhatsApp. I asked, "I need a yoga mat." And I got the response. "We have an eco-friendly yoga mat with an anti-slip grip. The price is $25." It also asked, "Would you like to know about payment options or delivery details?"

Now, let's activate the workflow to test it further. Let's say "yes." And we got the response for the yoga mat. "Payment options include credit cards, debit cards, UPI, and cash on delivery. The delivery usually takes 3 to five working days and you'll receive a tracking link via email or SMS once it's shipped. You can return products within 7 days of delivery." And yes, it gives a bit more information. We can adjust the response by adding more instructions in the AI agent system message. Now, let's ask "how to order?" And we got the response. "To order, visit our website or app along with some more details." Let's ask "what's the website link?" And here it is. "You can order from our website amazon.com" which is already available in our knowledge base.

Finally, let's ask a question that is not available in the knowledge base. "Affiliate program available?" And now it replied, "One of our team members will assist you on this" because in the system message we had added that instruction. So that's how it works. Now you can modify this and add more functionalities. For example, when the AI gets confused with an answer, we can call another tool like Telegram or WhatsApp to notify one of the team members so they can join and answer the user queries. And if you need any paid customization on this, you can contact me through the email provided so we can discuss it further. So that's all for today. I will include the workflow JSON file in a blog post and the link is given in the video description. I hope you enjoyed this video. If you have any doubts, feel free to let me know in the comments. We will be uploading more AI and automation videos in the coming days. So, don't forget to like and subscribe.