AI Chatbot with Document-Based RAG and PGVector - n8n Workflow

Use this comprehensive n8n workflow to build a powerful Telegram AI Chatbot leveraging Retrieval-Augmented Generation (RAG). Integrate OpenAI and PGVector for contextual answers from your documents. Find essential n8n templates here.

Workflow Preview

Ready to automate?

Download this n8n workflow template and start using it instantly.

Who is this best for?

Automation Specialists seeking advanced RAG n8n templates.
Teams needing a custom, document-aware AI assistant.
Developers implementing PostgreSQL vector storage (PGVector).
Users looking for a production-ready RAG architecture in n8n.

Overview

This sophisticated n8n workflow solves the challenge of providing highly accurate, document-contextualized responses via an AI chatbot. It is fundamentally split into two processes: Document Ingestion and AI Query Handling.

The ingestion path automatically detects new or updated files in Google Drive (using an n8n trigger), processes them, deletes old vectors, and saves new vector embeddings into a PostgreSQL PGVector store. This ensures the knowledge base is always current.

The AI Query path leverages this RAG knowledge base. When a user sends a message, the n8n agent uses the OpenAI Chat Model, PGVector tools, and chat memory to generate informed, contextual responses, making this a crucial n8n workflow for modern AI applications.

How it Works

The entire automation is a two-part n8n workflow:

1. Document Ingestion


  1. The process is initiated by a Google Drive n8n trigger (File Created or File Updated).

  2. The Loop Over Items n8n node handles each file individually.

  3. The Set File ID node extracts key metadata, which is used by the Delete Old Doc Records (Postgres n8n node) to maintain data hygiene in the PGVector store.

  4. The file is downloaded and text is extracted using the Extract Document Text n8n node.

  5. A Code n8n node (Create Chunks From Doc) prepares the text.

  6. The text is then split into manageable chunks, and contextual text is generated using an OpenAI Chat Model3 and an LLM Chain n8n node.

  7. Finally, Embeddings OpenAI1 generates vector representations, which are saved into the Postgres PGVector Store, completing the document ingestion part of this n8n workflow.

2. AI Chat Handling (RAG Query)


  1. The chat process starts with the When Executed by Another Workflow n8n trigger (designed to receive user input from an external chat application like Telegram).

  2. The core component is the RAG AI Agent n8n node, which orchestrates the query process.

  3. It utilizes the OpenAI Chat Model for reasoning and the Chat Memory (Postgres) n8n node to remember conversation history.

  4. The agent uses the Docs RAG Tool (Postgres PGVector Store) as its external knowledge base. The user query is vectorized by the Embeddings n8n node to search for relevant document chunks in the vector store before generating a final, contextual answer.

Installation Guide

To deploy this RAG n8n template, follow these steps:


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

  2. Credentials Setup: You will need credentials for the following services:

Google Drive: For the File Created and File Updated n8n trigger nodes.
PostgreSQL: Ensure your PostgreSQL instance is running PGVector and provide credentials for the Delete Old Doc Records, Postgres PGVector Store, and Chat Memory n8n nodes.
* OpenAI: Set up credentials for the OpenAI Chat Model and Embeddings OpenAI n8n nodes.

  1. PGVector Configuration: Ensure your PGVector store is properly initialized and configured in the respective n8n node settings to handle the document chunks.

  2. Connect the Chat Trigger: The When Executed by Another Workflow n8n trigger must be linked to a separate n8n workflow (e.g., a Telegram Listener) that captures user messages and executes this workflow with the user's input.

Node Details

Google Drive Trigger (File Created/Updated): Acts as the initial n8n trigger, monitoring a specific folder for new or changed documents that should be added to the knowledge base.
Delete Old Doc Records (Postgres): This essential n8n node executes a SQL query to remove existing embeddings associated with an updated file, preventing stale data.
Extract Document Text (n8n node): Converts binary files (downloaded from Google Drive) into plain text for processing and chunking.
Create Chunks From Doc (Code): A custom n8n node script used to format the extracted document text appropriately for the subsequent text splitter.
Postgres PGVector Store (n8n node): Used in two contexts: for writing new vector embeddings during ingestion, and as the RAG Tool to retrieve relevant context during a query. This is central to the RAG n8n workflow design.
Embeddings OpenAI (n8n node): Generates high-dimensional vector embeddings for document chunks and incoming chat queries.
RAG AI Agent (Langchain Agent): The primary n8n node for the chat response. It intelligently decides whether to use the internal language model or consult the external Docs RAG Tool based on the user's question and context.
OpenAI Chat Model (n8n node): Provides the language model capabilities for generation and reasoning within the n8n workflow.

Related n8n Workflows

Paid

Nodes: 19 Nodes
Updated: December 26 2025
View all
Created by

Automation & AI expert with over 7 years of experience in the digital & tech industry. Founder digital design, web & mobile development studio INSAIM.DESIGN. Focusing on startups & tech companies and helping to streamline their processes with AI and automation.

Featured*