RAG Chatbot with Vector DB and External LLMs - n8n Workflow

Learn how to build a scalable Retrieval-Augmented Generation (RAG) solution using n8n workflow templates. This n8n automation integrates Supabase for vector storage, TogetherAI for embeddings, and OpenRouter for cost-effective LLM access.

Workflow Preview

Ready to automate?

Download this n8n workflow template and start using it instantly.

Who is this best for?

AI developers needing robust RAG knowledge base solutions.
Users looking for advanced n8n templates for generative AI applications.
Engineers utilizing Supabase for vector storage in their n8n workflows.
Automation specialists seeking to integrate external LLMs like TogetherAI and OpenRouter.

Overview

This sophisticated n8n workflow template demonstrates a complete RAG pipeline, divided into a one-time training phase and a continuous runtime phase. The challenge solved is enabling an AI model to answer questions based strictly on a specific, large corpus of proprietary data (a Google Doc). By using TogetherAI for generating embeddings and storing these vectors in a dedicated Supabase database table, the n8n automation significantly improves answer accuracy and reduces reliance on the LLM's general knowledge base.

The resulting chatbot is triggered by a simple chat message, which then utilizes the optimized knowledge base to retrieve context before generating a highly relevant answer via an OpenRouter connection. This complex n8n workflow showcases how powerful, multi-service integrations can be achieved efficiently using various specialized n8n node types.

How it Works

The entire process is split into two distinct operational flows within this n8n workflow:

Phase 1: Knowledge Base Setup (Run Once)


  1. Data Extraction: The flow starts by using the Google Docs n8n node to fetch the specified training content document.

  2. Chunking: A Code n8n node programmatically splits the large text content into manageable 1000-character chunks, suitable for embedding generation.

  3. Embedding Generation: An HTTP Request n8n node sends each chunk to the TogetherAI embeddings API (BAAI/bge-large-en-v1.5) to generate a vector representation.

  4. Vector Storage: The Supabase n8n node then saves both the original text chunk and its new vector embedding into the designated embed table in the database.

Phase 2: Runtime Chatbot Operation


  1. Trigger: The When chat message received n8n trigger starts the process when a user sends a message to the public webhook chat interface.

  2. Query Embedding: An HTTP Request n8n node sends the user's message to TogetherAI to generate a vector embedding for the query.

  3. Context Retrieval (RAG): A subsequent HTTP Request n8n node connects to Supabase, calling the matchembeddings1 RPC function. This function uses the query embedding to find the top 5 most semantically similar chunks from the stored knowledge base.

  4. Aggregation: The Aggregate n8n node merges these 5 retrieved text chunks into a single, comprehensive context block.

  5. LLM Processing: The Basic LLM Chain n8n node constructs a prompt using the aggregated context and the user's original question. This prompt strictly instructs the AI to answer only based on the provided context.

  6. Response Generation: The LLM Chain utilizes the OpenRouter Chat Model n8n node (configured to use the qwen/qwen3-8b:free model) to process the final prompt and generate the detailed, contextualized response.

Installation Guide

To deploy this expert n8n workflow template, follow these steps:


  1. Import: Copy the provided JSON code and import it directly into your n8n instance.

  2. Supabase Setup: Configure your Supabase database connection (n8n credentials). Ensure you have a table named embed and the necessary vector search RPC function (matchembeddings1) set up for similarity search.

  3. API Credentials: Set up HTTP Bearer Token credentials for the TogetherAI API (for both embedding generation) and the OpenRouter API (for the chat model).

  4. Google Docs: Configure a Google Service Account credential for the Google Docs n8n node and update the document URL to point to your training data source.

  5. Initial Training Run: Crucially, manually execute the initial flow path (starting from 'Content for the Training' and ending at 'Save the embedding in DB') just once. This action indexes your data and makes the knowledge base ready for the RAG chatbot to function.

  6. Activate Chat Trigger: Ensure the When chat message received n8n trigger node is active to enable the runtime chatbot.

Node Details

Content for the Training (Google Docs n8n node): Fetches the raw text content from the specified Google Doc URL, serving as the knowledge source for the RAG system. Authentication uses a Service Account.
Splitting into Chunks (Code n8n node): Custom JavaScript code that divides the fetched document into 1000-character segments. This preparation is vital for creating accurate and manageable embeddings.
Embedding Uploaded document / Embend User Message (HTTP Request n8n node): Calls the TogetherAI API endpoint to convert text (either knowledge base chunks or user input) into numerical vector embeddings using the BAAI/bge-large-en-v1.5 model. Requires Together AI API credentials.
Save the embedding in DB (Supabase n8n node): Handles the persistent storage, saving the text chunk and its corresponding vector embedding into the Supabase embed table, completing the knowledge base setup for the n8n workflow.
When chat message received (Chat Trigger n8n trigger): The entry point for the RAG system, responding to new incoming chat messages via a public webhook.
Search Embeddings (HTTP Request n8n node): Executes a query against Supabase, utilizing a custom RPC function to perform vector similarity search, retrieving the top 5 relevant document chunks.
Aggregate (Aggregate n8n node): Combines the disparate retrieved chunks into a single context payload, ready to be injected into the LLM prompt.
Basic LLM Chain (chainLlm n8n node): Defines the core RAG logic and prompting strategy. It creates a robust instruction set for the model, ensuring the answer is grounded in the retrieved context.


  • OpenRouter Chat Model (lmChatOpenRouter n8n node): The language model provider used by this n8n workflow. It connects to the OpenRouter platform, specifically utilizing the qwen/qwen3-8b:free model to generate the final human-readable response.

Related n8n Workflows

Free

Nodes: 10 Nodes
Updated: December 26 2025
View all
Created by
iamvaar
iamvaar

Featured*