Deploy a powerful Retrieval-Augmented Generation (RAG) system using this advanced n8n workflow. Automatically ingest Gmail emails into PGVector and use an OpenAI agent for smart, conversational querying.
Download this n8n workflow template and start using it instantly.
Professionals or organizations dealing with high volumes of email who need quick, semantic search capabilities.
Users looking for practical n8n templates integrating LangChain components.
Developers building custom AI assistants who require a robust RAG knowledge base for external data.
Anyone interested in leveraging the power of an n8n workflow to automate knowledge management using vector databases.
Managing high volumes of email often results in crucial information being buried and difficult to retrieve through standard keyword searches. This innovative n8n workflow solves this by implementing a powerful Retrieval-Augmented Generation (RAG) system.
The n8n workflow operates in two phases: ingestion and retrieval. First, it uses an n8n trigger watching Gmail to automatically extract and vectorize new emails, storing them in a dedicated PGVector table. Second, it utilizes an AI agent powered by an OpenAI Chat Model. When a user sends a query via the chat n8n trigger, the agent uses its tool—the PGVector knowledge base—to find semantically relevant email context. This allows users to ask complex questions like, 'What was the delivery timeline mentioned in the email from Vendor X last Tuesday?' The final response is generated using both the user's query and the context retrieved from the database, demonstrating the powerful capabilities of this n8n template for knowledge retrieval.
This n8n workflow is divided into two interdependent automation processes: the Data Ingestion Pipeline and the Conversational Query Agent.
Enhanced Default Data Loader n8n node, combining the sender, date, subject, and text body into a single document. This document is then processed by the Recursive Character Text Splitter2 to break down large emails into manageable chunks (2000 characters with 200 overlap), suitable for vectorization.Embeddings OpenAI3 n8n node generates vector representations (embeddings) for each text chunk. These embeddings, along with the original text and custom metadata (e.g., datasource: gmail), are immediately inserted into the emailsvectorhistory table using the Postgres PGVector Store3 n8n node. This completes the real-time update of the RAG knowledge base.OpenAI Chat Model n8n node provides the reasoning and conversational capabilities.Postgres PGVector Store n8n node configured in retrieve-as-tool mode. When the agent determines that the user's query requires knowledge retrieval (e.g., 'What did John send me?'), it calls this tool, which performs a similarity search (topK: 5) against the emailsvector_history database using embeddings provided by the secondary Embeddings OpenAI n8n node.To deploy this comprehensive n8n workflow template, follow these steps:
OpenAI Chat Model and both Embeddings OpenAI n8n nodes. Update the existing 'OpenAi account' credentials or create a new one.Gmail Trigger and Get Mail Data n8n nodes to allow access to your mailbox.pgvector extension enabled. Configure the credentials for both Postgres PGVector Store n8n nodes, ensuring they point to the same database and schema. The table name emailsvectorhistory must exist or be created.Gmail Trigger to start indexing new emails.When chat message received n8n trigger to generate the public webhook URL needed for the conversational interface.New RAG Agent n8n node and the jsonData expression in the Enhanced Default Data Loader to ensure the structure meets your specific needs. This complete n8n template is now ready for use.This advanced n8n workflow utilizes specialized nodes to create a robust RAG system:
Gmail Trigger
Function: Acts as the initial n8n trigger, monitoring the configured Gmail account for new messages every minute.
Key Configuration: Polling interval set to 'everyMinute'.
Get Mail Data
Function: Retrieves the full body content and necessary metadata of the email identified by the trigger.
Key Configuration: Retrieves the messageId dynamically using the expression: = {{ $json.id }}.
Enhanced Default Data Loader
Function: Structures the raw email data into a consistent document format, making it searchable. It pulls critical fields like subject, sender, date, and text body using an expression.
Key Configuration: Uses jsonMode: expressionData to construct a detailed input string. Adds custom metadata: datasource: gmail and createdat.
Postgres PGVector Store3 (Insert Mode)
Function: The destination for newly embedded email chunks, facilitating the creation of the knowledge base.
Key Configuration: mode: insert, tableName: emailsvectorhistory.
Embeddings OpenAI3
Function: Responsible for converting the text chunks from the splitter into numerical vector embeddings for storage in PGVector.
Key Configuration: Requires OpenAI API credentials.
When chat message received
Function: Serves as the conversational n8n trigger, initializing the AI agent path when an external chat client sends a message.
New RAG Agent
Function: The core orchestrator. It decides whether to use the vector search tool or rely solely on the language model's internal knowledge based on the user's input.
Key Configuration: Defined systemMessage provides guidance and context for the AI assistant.
Postgres PGVector Store (Retrieve-as-Tool Mode)
Function: Configured as a callable tool for the agent. It performs semantic similarity search (RAG) against the email data.
* Key Configuration: mode: retrieve-as-tool, toolName: emailsvectorsearch, topK: 5 (retrieves the 5 most relevant chunks). This node is crucial for the RAG functionality of the n8n workflow.
Use this powerful n8n workflow to instantly aggregate real-time crypto news from multiple RSS feeds and generate market sentiment summaries using the GPT-4o model, delivered directly via Telegram.

Use this secure n8n workflow to deploy an AI agent that fetches real-time Gate.io spot market data (price, order book, candles) and delivers it instantly via Telegram. Leverage the power of an n8n node orchestration.

Create a real-time crypto market analyst using this sophisticated n8n workflow. Integrate GPT-4o-mini with multiple CoinMarketCap APIs, triggered effortlessly by a Telegram n8n trigger.

Automate critical error response using this powerful n8n workflow. Instantly receive Slack alerts and create Jira tickets when production errors are detected via webhook.

Implement essential devops monitoring using this reliable n8n workflow. Get instant, detailed WhatsApp alerts for failed n8n workflows, ensuring rapid response to system errors. A must-have n8n template for production environments.








































