How To Build AI Chatbot – RAG Chatbot with N8N (Step-by-step)

In this step-by-step guide, I’ll show you how to build your own custom knowledge base AI chatbot—also known as a RAG (Retrieval-Augmented Generation) chatbot—without writing a single line of code. We’ll use the powerful no-code automation platform, N8N, to create a chatbot that you can embed directly on your WordPress site.

By the end of this tutorial, you will have a chatbot that:

  • Answers queries only from your provided data.
  • Learns automatically when you add new files to a Google Drive folder.
  • Logs all conversations to a Google Sheet.
  • Is seamlessly embedded on your WordPress website.

What You’ll Need Before Starting

  1. An N8N account (Cloud or Self-Hosted).
  2. A Pinecone account (for the vector database).
  3. An OpenAI account (with billing enabled for API access).
  4. A Google Account (for Drive and Sheets).

Part 1: Building the “Data Pump” Workflow

This first workflow automatically takes any document you upload to a specific Google Drive folder and sends it to your Pinecone vector database (your chatbot’s “brain”).

Step 1: Create a New Workflow

In your N8N workspace, create a new workflow and name it “Push Data to Vector DB”.

Step 2: Set Up the Google Drive Trigger

  1. Add a Google Drive Trigger node.
  2. Set the trigger to “On Changes Involving a Specific Folder”.
  3. Connect your Google account (if you haven’t, you’ll need to set up credentials—see our N8N beginner tutorial for help).
  4. Select the folder you want to use (e.g., “N8N Chatbot Tutorial”).
  5. Set it to watch for “File Created” events.

This node will now activate your workflow anytime a new file is added to that folder.

Step 3: Download the File from Google Drive

  1. Add a Google Drive node (the regular one, not the trigger).
  2. Set the operation to “Download a File”.
  3. Pass the File ID from the trigger node into this node. You can do this by dragging the fileId field from the trigger node into the “File ID” field in this node.

Step 4: Configure the Vector Database with Pinecone

  1. In your Pinecone dashboard, create a new index. Name it chatbot-tutorial and select text-embedding-3-small as the model. Note the dimension value (e.g., 512).
  2. Go to the “API Keys” section and create a new key.
  3. Back in N8N, add a Pinecone Vector Store node and select “Add Documents to Vector Store”.
  4. Create new credentials using your Pinecone API Key and select the index you just created.
  5. Crucial: In the node’s options, set the Namespace to chatbot-tutorial and the Dimension to match your index (e.g., 512).

Step 5: Set Up the AI Embeddings

  1. Add an OpenAI Embeddings node.
  2. Connect your OpenAI account by generating an API key in your OpenAI platform settings and adding it to N8N.
  3. Set the model to text-embedding-3-small.

Step 6: Connect the Data Flow

  1. Add a Default Data Loader node between the Google Drive node and the Pinecone node. Set it to “Binary” to format the file data correctly.
  2. Connect the nodes as shown below. Your final workflow should look like this: Google Drive Trigger → Google Drive (Download) → Default Data Loader → OpenAI Embeddings → Pinecone Vector Store

Step 7: Test the Data Workflow

Upload a .txt file with some sample content (e.g., information about your business services) to your designated Google Drive folder. Activate and execute the workflow. If successful, you should see the documents processed and uploaded to your Pinecone index. You can check the record count in your Pinecone dashboard to confirm.


Part 2: Building the Main Chatbot Workflow

Now, let’s build the brain that will query this knowledge base and talk to your users.

Step 1: Create a New Workflow

Create a new workflow and name it “Chatbot Tutorial”.

Step 2: Add the Chat Trigger

  1. Add a Chat Trigger node. This node creates the public interface for your chatbot.
  2. Keep the default settings for now.

Step 3: Build the AI Agent

  1. Add an AI Agent node and connect it to the Chat Trigger.
  2. System Message: This is critical for guiding the chatbot’s behavior. Use a prompt like: “You are a friendly, helpful, and professional AI assistant for the WebSense Pro knowledge base. Your name is WebSense Pro Assistant. Answer the user’s question based only on the provided context. If the answer is not in the context, politely decline to answer. Do not hallucinate or make up information.”
  3. Chat Model: Add an OpenAI Chat Model node connected to the AI Agent. Select your OpenAI account and choose a model like gpt-4o-mini for a good balance of speed and cost.
  4. Memory: Add a Simple Memory node and connect it to the AI Agent. Set the context window to 20 so it remembers the last 20 messages of the conversation.

Step 4: Give the AI Agent Access to Your Knowledge Base

  1. Add a Vector Store Question Answer Tool node to the AI Agent.
  2. In the tool’s description, write: “Return data related to WebSense Pro services.” This helps the AI decide when to use this tool.
  3. Now, connect the same Pinecone Vector Store and OpenAI Embeddings nodes you used in the first workflow. Ensure the Namespace and Dimensions are set correctly here as well.
  4. Connect another OpenAI Chat Model node to the tool for its processing.

Your AI Agent setup should look like this inside the node:

  • Input: From Chat Trigger
  • Model: OpenAI Chat Model
  • Memory: Simple Memory
  • Tool: Vector Store QA Tool (connected to Pinecone & Embeddings)

Step 5: Test Your Chatbot

Save and activate the workflow. Click “Open Chat” in the Chat Trigger node and ask a question based on the data you uploaded earlier (e.g., “What services do you offer?”). If it answers correctly, you’re on the right track!


Part 3: Adding Chat Logging to Google Sheets

Let’s keep a record of all conversations.

Step 1: Add the Google Sheets Node

  1. After the AI Agent node, add a Google Sheets node and set the operation to “Append or Update Row in Sheet”.
  2. Connect your Google account and select (or create) a spreadsheet with a sheet named “Chats”.
  3. Map the columns:
    • Timestamp: {{ $now }} (or use a function to get the current time)
    • User Input: {{ $node["Chat Trigger"].json["message"] }}
    • Bot Response: {{ $node["AI Agent"].json["response"] }}

Step 2: Ensure Smooth Chat Flow

Adding a node after the AI Agent can break the chat memory. To fix this:

  1. Add a Set node after the Google Sheets node.
  2. Create a new variable called text.
  3. Set its value to the bot’s response from the AI Agent node (e.g., {{ $node["AI Agent"].json["response"] }}). This passes the correct response back to the chat interface.

Your final workflow order should be: Chat Trigger → AI Agent → Google Sheets → Set → (Output back to chat).


Part 4: Embedding on Your WordPress Website

Step 1: Get Your Public Webhook URL

  1. Double-click on your Chat Trigger node.
  2. Click on “Make chat publicly available” and copy the generated URL.

Step 2: Add the Code to WordPress

  1. In your WordPress dashboard, go to Appearance > Theme File Editor.
  2. Select your active theme and edit the functions.php file.
  3. Add the following code snippet to the end of the file, replacing YOUR_WEBHOOK_URL with the URL you copied: function add_n8n_chatbot() { ?> <script> window.n8nChatSettings = { webhookUrl: 'YOUR_WEBHOOK_URL', welcomeMessage: 'Hi! I am the WebSense Pro assistant. How can I help you?', }; (function() { var script = document.createElement('script'); script.src = 'https://chat.n8n.io/webchat.js'; document.head.appendChild(script); })(); </script> <?php } add_action('wp_footer', 'add_n8n_chatbot');
  4. Update the file.

That’s it! Your custom AI chatbot should now be live on your website, ready to answer questions based solely on your data.

Conclusion:

You’ve just built a powerful, no-code AI chatbot with a custom knowledge base. This system is incredibly flexible. To update your chatbot’s knowledge, simply upload a new document to your Google Drive folder and run the first workflow.

5/5 - (5 votes)

About

Leave a Comment

Your email address will not be published. Required fields are marked *