[n8n] Naive RAG Chatbot

$0

This template builds a naive Retrieval-Augmented Generation (RAG) based Artificial Intelligence (AI) chatbot. This chatbot automatically ingests and processes documents from a specified Google Drive folder, then stores them in a Supabase vector database. Finally, it uses an AI agent to answer user questions about those documents through a chat interface.

Create a RAG chatbot with documents from Google Drive, OpenAI, and Supabase

This template builds a naive Retrieval-Augmented Generation (RAG) based Artificial Intelligence (AI) chatbot. This chatbot automatically ingests and processes documents from a specified Google Drive folder, then stores them in a Supabase vector database. Finally, it uses an AI agent to answer user questions about those documents through a chat interface.

Who is this for?

  • Developers building AI-powered support bots.
  • Teams wanting to create an internal knowledge chatbot to answer employee questions.
  • n8n users who want to build a question-answering system based on their own document set.
  • AI enthusiasts who want to explore the RAG architecture to combine Large Language Models (LLMs) with private data.

Key Features

  • Automatic Document Ingestion: Automatically triggers when a new file is added to the specified Google Drive folder.
  • RAG Architecture: Implements a complete RAG pipeline to provide context-aware answers based on your documents.
  • Vector Storage: Uses Supabase and its pgvector extension for efficient storage and retrieval of document embeddings.
  • Conversational Memory: Remembers past interactions within a conversation using Postgres chat memory, allowing for contextual follow-up conversations.
  • AI Agent: Uses an n8n LangChain agent to intelligently use the knowledge base (retriever tool) to answer questions.
  • Ready-to-use Chat Interface: Includes a chat trigger so you can deploy and interact with your chatbot immediately.

How it works

This template is divided into two main workflows:

  1. Workflow 1: Document Ingestion
    1. A Google Drive Trigger node monitors a specific folder for new files.
    2. When a file is added, it is downloaded and its text content is extracted.
    3. The extracted text is split into smaller, more manageable chunks.
    4. An OpenAI Embeddings node converts these text chunks into numerical vectors.
    5. These vectors, along with their metadata, are inserted into a specified table in your Supabase vector store.
  2. Workflow 2: Chat Interaction
    1. The Chat Trigger receives the user’s message and session ID.
    2. The RAG Supervisor Agent receives the user’s question.
    3. This agent uses the Supabase Vector Store node as a tool to search for relevant document snippets based on the user’s query.
    4. The OpenAI Chat Model generates a response based on the user’s question, the retrieved context from the documents, and the conversation history (managed by Postgres Chat Memory).
    5. The final answer is sent back to the user through the chat interface.

Requirements

  • An n8n instance.
  • An OpenAI account and API key.
  • A Google account for Google Drive.
  • A Supabase account, including:
    • A Supabase project.
    • A Postgres database (which comes with Supabase) to store chat history.

Step-by-step setup guide

  1. Set up Supabase:
    • Create a new project in Supabase.Go to the SQL Editor in your project and select Quickstarts => Langchain.Paste the entire following SQL script into the editor and press Run. This script will enable pgvector, create the documents table, and create a function for searching.
-- Enable the pgvector extension to work with embedding vectors
create extension vector;

-- Create a table to store your documents
create table documents (
  id bigserial primary key,
  content text, -- corresponds to Document.pageContent
  metadata jsonb, -- corresponds to Document.metadata
  embedding vector(1536) -- 1536 works for OpenAI embeddings, change if needed
);

-- Create a function to search for documents
create function match_documents (
  query_embedding vector(1536),
  match_count int default null,
  filter jsonb DEFAULT '{}'
) returns table (
  id bigint,
  content text,
  metadata jsonb,
  similarity float
)
language plpgsql
as $$
#variable_conflict use_column
begin
  return query
  select
    id,
    content,
    metadata,
    1 - (documents.embedding <=> query_embedding) as similarity
  from documents
  where metadata @> filter
  order by documents.embedding <=> query_embedding
  limit match_count;
end;
$$;
  1. Set up Google Drive:
  2. Set up n8n credentials:
    • Add credentials for OpenAI, Google Drive, and the Supabase API to your n8n instance.
    • Add separate Postgres credentials using the database connection details from your Supabase project.
  3. Configure the template nodes:
    • Google Drive File Created (Trigger): Paste your Google Drive folder ID into the Folder To Watch field.
    • Insert Content into Supabase Vectorstore: Select your Supabase credentials and enter the name of the table you created (documents).
    • OpenAI Embeddings & OpenAI Chat Model: Select your OpenAI credentials.
    • Supabase Vector Store (in the chat workflow): Select your Supabase credentials, enter the table name (documents), and the name of the search function (match_documents).
    • Postgres Chat Memory: Select your Postgres credentials and configure the table name to store chat history (e.g., chat_history).
  4. Activate the workflow: Save and activate the workflow.
  5. First Run (Ingestion): Add a document to your specified Google Drive folder to trigger the ingestion part of the workflow and populate your vector store.
  6. Chat: Use the chat interface to start asking questions about your documents.

How to customize the workflow

  • Change the LLM: Swap the OpenAI Chat Model with other supported models like Anthropic Claude or Google Gemini.
  • Use other vector stores: Replace the Supabase node with other options like Pinecone, Weaviate, or Qdrant. You will need to adjust the ingestion process accordingly.
  • Optimize text splitting: Adjust the Chunk Size and Chunk Overlap in the Character Text Splitter node to optimize performance and context relevance for your specific documents.
  • Adjust the agent’s prompt: Modify the system message in the RAG Supervisor AI Agent node to change the chatbot’s personality, instructions, or constraints.

FAQ – Frequently Asked Questions

1. Who is this workflow intended for?
This workflow is designed for users who have a basic understanding of n8n and are capable of troubleshooting issues on their own. If you’re familiar with optimizing prompts and handling minor issues, this product is a great fit for you.


2. How is the workflow installed and used?
The workflow comes pre-configured by default, which means you can import and run it immediately. However, to achieve optimal performance for your specific use case or business needs, you may need to customize and optimize the prompts.


3. What should I keep in mind during testing?
During testing, we recommend using low-cost models (such as mini or flash) and generating low-resolution images to save on costs. The primary goal is to ensure the workflow operates reliably before making any further optimizations. Note that the low-cost models may cause error to the workflow.


4. What are the default and alternative AI models?
By default, the workflow uses the GPT-4o model due to its stability and excellent ability to return data in the required JSON format. If you encounter any issues, you can try switching to ChatGPT-4o. Note that some other models (like Gemini Flash) may not return results in JSON format or support tool calls, which could cause the workflow to malfunction.


5. How do I troubleshoot if the workflow fails to run?
Please try the following steps:

  • Run the workflow in an incognito window with all plugins disabled.
  • Try using a different browser (for example, switch from Chrome to Safari).
  • Test on another computer or in a different network environment/ server.
    Keep in mind that issues can stem from various sources, including limitations of the AI model, your self-hosted n8n server, the n8n platform itself, or even your local device/ network/ server settings.

6. How can I submit feedback or report a bug?
You can contact us to submit your suggestions, comments, or bug reports related to the workflow and documentation. Every piece of feedback is carefully reviewed to address bugs or incorporate quality improvements in future versions.


7. Is technical support included after purchase?
At present, purchasing the workflow provides you with the file only, without any technical support. In the future, we plan to offer additional support packages, including tutorial videos, technical consulting, and customization services based on customer needs.


8. Can I share or resell the workflow?
Please do not share or resell the workflow without obtaining prior permission from us. The product is protected by copyright, and unauthorized sharing or resale is strictly prohibited.


9. How do I submit feedback on my purchasing experience?
If you have any comments or suggestions regarding your purchasing experience, please send us a message. Your input is valuable to us and will help improve our services and product quality.


10. What is the refund policy?
Due to the nature of the workflow product, our shop does not currently offer refunds for purchases. In the future, we plan to sell our products on platforms that support refund policies. However, please note that the prices on those platforms will be significantly higher compared to purchasing directly from our shop.


If you have any further questions or need additional information, please feel free to contact us through our contact form.

Truly,
AI Automation Pro

Review Your Cart
0
Add Coupon Code
Subtotal