AutomationFlowsAI & RAG › Gmail RAG Assistant with OpenAI and PGVector

Gmail RAG Assistant with OpenAI and PGVector

Original n8n title: Real-time Email RAG Assistant with Gmail, Openai Gpt, and Pgvector

ByZain Ali @zain104 on n8n.io

This workflow is ideal for: Professionals Project managers Sales and support teams Anyone managing high volumes of Gmail messages

Event trigger★★★☆☆ complexityAI-powered13 nodesGmail TriggerGmailText Splitter Recursive Character Text SplitterOpenAI EmbeddingsDocument Default Data LoaderVector Store PgvectorOpenAI ChatAgent
AI & RAG Trigger: Event Nodes: 13 Complexity: ★★★☆☆ AI nodes: yes Added:

This workflow corresponds to n8n.io template #5908 — we link there as the canonical source.

This workflow follows the Agent → Chat Trigger recipe pattern — see all workflows that pair these two integrations.

The workflow JSON

Copy or download the full n8n JSON below. Paste it into a new n8n workflow, add your credentials, activate. Full import guide →

Download .json
{
  "id": "6m6Tp8bUVb0t6lwo",
  "name": "Email RAG for new Emails",
  "tags": [
    {
      "id": "PNmVCFjr9sq0xind",
      "name": "RAG",
      "createdAt": "2025-07-12T09:30:51.202Z",
      "updatedAt": "2025-07-12T09:30:51.202Z"
    },
    {
      "id": "3Jr6wxDndYE0FaUt",
      "name": "email",
      "createdAt": "2025-07-12T10:08:56.337Z",
      "updatedAt": "2025-07-12T10:08:56.337Z"
    }
  ],
  "nodes": [
    {
      "id": "a896498a-dc44-4d77-8268-7f7001510974",
      "name": "Gmail Trigger",
      "type": "n8n-nodes-base.gmailTrigger",
      "position": [
        740,
        340
      ],
      "parameters": {
        "filters": {},
        "pollTimes": {
          "item": [
            {
              "mode": "everyMinute"
            }
          ]
        }
      },
      "typeVersion": 1.2
    },
    {
      "id": "7fa7e6b4-ea5e-4716-be0e-58b96458ec0d",
      "name": "Get Mail Data",
      "type": "n8n-nodes-base.gmail",
      "position": [
        960,
        340
      ],
      "parameters": {
        "simple": false,
        "options": {},
        "messageId": "={{ $json.id }}",
        "operation": "get"
      },
      "typeVersion": 2.1
    },
    {
      "id": "bce4a290-a71c-4277-9381-3cf86867341e",
      "name": "Recursive Character Text Splitter2",
      "type": "@n8n/n8n-nodes-langchain.textSplitterRecursiveCharacterTextSplitter",
      "position": [
        1460,
        700
      ],
      "parameters": {
        "options": {
          "splitCode": "markdown"
        },
        "chunkSize": 2000,
        "chunkOverlap": 200
      },
      "typeVersion": 1
    },
    {
      "id": "71ae9619-51ae-4c9a-8697-031725d037af",
      "name": "Embeddings OpenAI3",
      "type": "@n8n/n8n-nodes-langchain.embeddingsOpenAi",
      "position": [
        1080,
        620
      ],
      "parameters": {
        "options": {}
      },
      "credentials": {
        "openAiApi": {
          "name": "<your credential>"
        }
      },
      "typeVersion": 1.1
    },
    {
      "id": "9e7bd487-f657-4522-9f29-e937f604fff0",
      "name": "Enhanced Default Data Loader",
      "type": "@n8n/n8n-nodes-langchain.documentDefaultDataLoader",
      "position": [
        1380,
        540
      ],
      "parameters": {
        "options": {
          "metadata": {
            "metadataValues": [
              {
                "name": "=data_source",
                "value": "=gmail"
              },
              {
                "name": "=created_at",
                "value": "={{ $json.date }}"
              }
            ]
          }
        },
        "jsonData": "=Email details:\nDate: {{ $json.headers.date }}\n\nFrom Email: {{ $json.from.value[0].address }}\nFrom Name:  {{ $json.from.value[0].name }}\n\nTo Email: {{ $json.to.value[0].address }}\n\nEmail Subject:\n{{ $json.subject }}\n\nEmail body:\n{{ $json.text }}",
        "jsonMode": "expressionData"
      },
      "typeVersion": 1
    },
    {
      "id": "251eaeab-ec58-48ad-b51c-556c739ba777",
      "name": "Postgres PGVector Store3",
      "type": "@n8n/n8n-nodes-langchain.vectorStorePGVector",
      "position": [
        1220,
        340
      ],
      "parameters": {
        "mode": "insert",
        "options": {},
        "tableName": "emails_vector_history"
      },
      "typeVersion": 1.1
    },
    {
      "id": "c0930431-754d-4f87-bf44-6415a816aa7d",
      "name": "Sticky Note2",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        680,
        120
      ],
      "parameters": {
        "color": 4,
        "width": 1200,
        "height": 740,
        "content": "## Watch Trigger (Email) - New Email\n## Get new Email -> Extract the text -> Add to Vector Store"
      },
      "typeVersion": 1
    },
    {
      "id": "c6abb6a2-bc2a-4b2c-98c8-4de628c2776d",
      "name": "OpenAI Chat Model",
      "type": "@n8n/n8n-nodes-langchain.lmChatOpenAi",
      "position": [
        -60,
        540
      ],
      "parameters": {
        "options": {}
      },
      "credentials": {
        "openAiApi": {
          "name": "<your credential>"
        }
      },
      "typeVersion": 1
    },
    {
      "id": "fa5ca8d4-5997-4e77-8ab3-f0efe560042d",
      "name": "Sticky Note",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        -380,
        120
      ],
      "parameters": {
        "width": 902,
        "height": 928,
        "content": "## RAG AI Agent\n\nThis powerful n8n workflow creates an intelligent email search and query system using RAG (Retrieval-Augmented Generation) technology. The system automatically ingests Gmail emails into a vector database and provides conversational AI-powered search capabilities through a chat interface"
      },
      "typeVersion": 1
    },
    {
      "id": "99153412-5aa8-4ab0-918e-172802fc8590",
      "name": "New RAG Agent",
      "type": "@n8n/n8n-nodes-langchain.agent",
      "position": [
        60,
        320
      ],
      "parameters": {
        "options": {
          "systemMessage": "=You are a helpful assistant that will get data from RAG and send a good response to user.\n\nToday date is this if user ask for dated or latest data: {{ $now }}"
        }
      },
      "typeVersion": 1.7
    },
    {
      "id": "00e80343-3f3c-4fed-93d9-213435012df7",
      "name": "Postgres PGVector Store",
      "type": "@n8n/n8n-nodes-langchain.vectorStorePGVector",
      "position": [
        240,
        640
      ],
      "parameters": {
        "mode": "retrieve-as-tool",
        "topK": 5,
        "options": {},
        "toolName": "emails_vector_search",
        "tableName": "emails_vector_history",
        "toolDescription": "Call this tool to perform a vector embeddings search in my e-mail database. For time-specific queries:\n1. ALWAYS include the time frame in your query (e.g., \"interviews scheduled after April 27, 2025\" or \"interviews for next week April 28-May 4, 2025\")\n2. For future events, explicitly mention \"future\" or \"upcoming\" in your query"
      },
      "typeVersion": 1.1
    },
    {
      "id": "2c39ec49-ac2b-42bc-b176-7f15a712c6b7",
      "name": "Embeddings OpenAI",
      "type": "@n8n/n8n-nodes-langchain.embeddingsOpenAi",
      "position": [
        200,
        800
      ],
      "parameters": {
        "options": {}
      },
      "credentials": {
        "openAiApi": {
          "name": "<your credential>"
        }
      },
      "typeVersion": 1.1
    },
    {
      "id": "e569097d-79b8-4648-86d5-67298c2a25d6",
      "name": "When chat message received",
      "type": "@n8n/n8n-nodes-langchain.chatTrigger",
      "position": [
        -220,
        320
      ],
      "parameters": {
        "options": {}
      },
      "typeVersion": 1.1
    }
  ],
  "active": false,
  "settings": {
    "executionOrder": "v1"
  },
  "versionId": "13c464b8-66a1-4503-b853-4b9ae2365146",
  "connections": {
    "Get Mail Data": {
      "main": [
        [
          {
            "node": "Postgres PGVector Store3",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Gmail Trigger": {
      "main": [
        [
          {
            "node": "Get Mail Data",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "New RAG Agent": {
      "main": [
        []
      ]
    },
    "Embeddings OpenAI": {
      "ai_embedding": [
        [
          {
            "node": "Postgres PGVector Store",
            "type": "ai_embedding",
            "index": 0
          }
        ]
      ]
    },
    "OpenAI Chat Model": {
      "ai_languageModel": [
        [
          {
            "node": "New RAG Agent",
            "type": "ai_languageModel",
            "index": 0
          }
        ]
      ]
    },
    "Embeddings OpenAI3": {
      "ai_embedding": [
        [
          {
            "node": "Postgres PGVector Store3",
            "type": "ai_embedding",
            "index": 0
          }
        ]
      ]
    },
    "Postgres PGVector Store": {
      "ai_tool": [
        [
          {
            "node": "New RAG Agent",
            "type": "ai_tool",
            "index": 0
          }
        ]
      ]
    },
    "When chat message received": {
      "main": [
        [
          {
            "node": "New RAG Agent",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Enhanced Default Data Loader": {
      "ai_document": [
        [
          {
            "node": "Postgres PGVector Store3",
            "type": "ai_document",
            "index": 0
          }
        ]
      ]
    },
    "Recursive Character Text Splitter2": {
      "ai_textSplitter": [
        [
          {
            "node": "Enhanced Default Data Loader",
            "type": "ai_textSplitter",
            "index": 0
          }
        ]
      ]
    }
  }
}

Credentials you'll need

Each integration node will prompt for credentials when you import. We strip credential IDs before publishing — you'll add your own.

Pro

For the full experience including quality scoring and batch install features for each workflow upgrade to Pro

About this workflow

This workflow is ideal for: Professionals Project managers Sales and support teams Anyone managing high volumes of Gmail messages

Source: https://n8n.io/workflows/5908/ — original creator credit. Request a take-down →

More AI & RAG workflows → · Browse all categories →

Related workflows

Workflows that share integrations, category, or trigger type with this one. All free to copy and import.

AI & RAG

Order and Delivery Support. Uses lmChatOpenAi, documentDefaultDataLoader, embeddingsOpenAi, toolVectorStore. Event-driven trigger; 29 nodes.

OpenAI Chat, Document Default Data Loader, OpenAI Embeddings +15
AI & RAG

This workflow implements a complete Retrieval-Augmented Generation (RAG) knowledge assistant with built-in document ingestion, conversational AI, and automated analytics using n8n, OpenAI, and Pinecon

Form Trigger, Data Table, Text Splitter Recursive Character Text Splitter +8
AI & RAG

This powerful AI automation add-on upgrades your Telegram Bot Starter Template by integrating a fully functional AI chatbot and a context-aware AI agent that answers user questions using your internal

OpenAI Chat, Document Default Data Loader, OpenAI Embeddings +10
AI & RAG

<h2>📧 Analyze, classify, and summarize emails using RAG (automatic taxonomy learning)</h2>

OpenAI Chat, Gmail Trigger, Google Sheets +7
AI & RAG

HR & IT Helpdesk Chatbot with Audio Transcription. Uses stickyNote, manualTrigger, httpRequest, extractFromFile. Event-driven trigger; 27 nodes.

HTTP Request, Vector Store Pgvector, OpenAI Embeddings +9