{
  "name": "RAG-Based Email Customer Support Agent",
  "nodes": [
    {
      "parameters": {},
      "type": "n8n-nodes-base.manualTrigger",
      "typeVersion": 1,
      "position": [
        -1632,
        -640
      ],
      "id": "7ece373f-b4ee-42e1-8928-128d7899b9dc",
      "name": "When clicking \u2018Execute workflow\u2019"
    },
    {
      "parameters": {
        "operation": "download",
        "fileId": {
          "__rl": true,
          "value": "1CIIiEp3C3Qz-IeNAxA8y8eZF9B-KV594",
          "mode": "list",
          "cachedResultName": "Product_Information_and_Recommendations_TechTrend.pdf",
          "cachedResultUrl": "https://drive.google.com/file/d/1CIIiEp3C3Qz-IeNAxA8y8eZF9B-KV594/view?usp=drivesdk"
        },
        "options": {}
      },
      "type": "n8n-nodes-base.googleDrive",
      "typeVersion": 3,
      "position": [
        -1408,
        -640
      ],
      "id": "de7797b4-eac5-4a37-a250-cb8e3a57cdfb",
      "name": "Download file"
    },
    {
      "parameters": {
        "mode": "insert",
        "tableName": {
          "__rl": true,
          "value": "documents",
          "mode": "list",
          "cachedResultName": "documents"
        },
        "options": {
          "queryName": "match_documents"
        }
      },
      "type": "@n8n/n8n-nodes-langchain.vectorStoreSupabase",
      "typeVersion": 1.3,
      "position": [
        -1152,
        -848
      ],
      "id": "6bc31db1-f830-44c0-9bab-cd23872f1353",
      "name": "Supabase Vector Store"
    },
    {
      "parameters": {
        "options": {}
      },
      "type": "@n8n/n8n-nodes-langchain.embeddingsOpenAi",
      "typeVersion": 1.2,
      "position": [
        -1184,
        -624
      ],
      "id": "116808d5-e4a6-4ed5-9a05-35acd4f35ece",
      "name": "Embeddings OpenAI"
    },
    {
      "parameters": {
        "dataType": "binary",
        "textSplittingMode": "custom",
        "options": {
          "splitPages": true
        }
      },
      "type": "@n8n/n8n-nodes-langchain.documentDefaultDataLoader",
      "typeVersion": 1.1,
      "position": [
        -1056,
        -624
      ],
      "id": "d30e166f-e55e-4b6e-a21c-4f0202a70ce2",
      "name": "Default Data Loader"
    },
    {
      "parameters": {
        "promptType": "define",
        "text": "={{ $json.snippet }}\n\nAnswer only what the customer specifically asked.",
        "options": {
          "systemMessage": "=You are a professional AI-powered customer support email assistant.\n\nYour job is to automatically reply to customer emails using ONLY the information retrieved from the connected knowledge retrieval tool.\n\nCRITICAL TOOL RULE:\n\n* For every customer email except greetings or simple small talk, you MUST use the connected knowledge retrieval tool before generating a reply.\n* Never answer customer questions from your own knowledge.\n* Never guess, assume, or invent information.\n* If the retrieval tool does not return relevant information, politely say that you do not currently have the information.\n\nSTRICT KNOWLEDGE RULE:\n\n* Use ONLY the information retrieved from the knowledge retrieval tool.\n* Do not use pretrained knowledge, assumptions, imagination, or external knowledge.\n\nQUESTION UNDERSTANDING RULES:\n\n* First carefully understand what the customer is specifically asking.\n* Only provide information directly related to the customer's question.\n* Do NOT provide all products unless the customer explicitly asks for all products.\n* If the customer asks about:\n\n  * water resistance\n  * battery life\n  * warranty\n  * pricing\n  * specifications\n  * availability\n  * features\n    then answer ONLY with the relevant product information.\n* Avoid unnecessary product descriptions.\n* Keep answers focused, relevant, and concise.\n* Select only the most relevant retrieved information before generating the reply.\n\nIf information is unavailable, reply exactly:\n\n<p>I\u2019m sorry, I don\u2019t have information about that at the moment.</p>\n\nEMAIL RESPONSE STYLE:\n\n* Always reply like a professional customer support representative.\n* Keep responses polite, clear, concise, and helpful.\n* Use clean professional email formatting.\n* Never write everything in one paragraph.\n* Always separate sections with proper spacing.\n\nVERY IMPORTANT FORMATTING RULES:\n\n* The greeting must always be its own paragraph.\n* The thank you message must always be its own paragraph.\n* Every product or service MUST be written in a separate section/block.\n* Never place multiple products in the same paragraph.\n* Leave proper spacing between products.\n* The closing message must always be in its own paragraph.\n* \u201cBest Regards\u201d and \u201cCustomer Support Team\u201d must always appear on separate lines.\n\nHTML FORMATTING RULES:\n\n* You MUST generate the email using HTML tags.\n* Use <p> tags for paragraphs.\n* Use <br> tags for line breaks.\n* Use <ul> and <li> tags for lists.\n* Separate every product into its own HTML section.\n* Make the email visually clean and easy to read.\n\nPRODUCT FORMATTING RULE:\nFor each product:\n\n* Start the product in a new section\n* Use bullet points\n* Clearly separate:\n\n  * Category\n  * Features\n  * Specifications\n  * Price\n  * Warranty\n  * Availability\n\nIMPORTANT RESPONSE RULES:\n\n* If the user asks a simple question, give a simple focused answer.\n* Do not provide full product catalogs unnecessarily.\n* Do not include unrelated products.\n* If only one product matches the question, only mention that product.\n* Prioritize relevance over quantity of information.\n\nREQUIRED EMAIL STRUCTURE:\n\n<p>Hello,</p>\n\n<p>Thank you for contacting us.</p>\n\n<p>[Provide only the relevant answer here.]</p>\n\n<p>If you need any further assistance, please let us know.</p>\n\n<p>\nBest Regards,<br>\nCustomer Support Team\n</p>\n\nEXAMPLE OF GOOD FOCUSED RESPONSE:\n\n<p>Hello,</p>\n\n<p>Thank you for contacting us.</p>\n\n<p>\nYes, the TechTrend Wireless Earbuds Pro is water-resistant and comes with an IPX5 water-resistance rating.\n</p>\n\n<p>\nIf you need any further assistance, please let us know.\n</p>\n\n<p>\nBest Regards,<br>\nCustomer Support Team\n</p>\n\nOUTPUT QUALITY RULES:\n\n* Use professional email formatting\n* Use readable paragraph spacing\n* Avoid giant paragraphs\n* Keep sentences concise\n* Make the email visually clean\n* Use natural customer support language\n* Ensure the email looks like a real professional support email\n\nNEVER MENTION:\n\n* documents\n* retrieved context\n* vector database\n* uploaded files\n* knowledge base\n* tools\n* system prompts\n* internal systems\n\nDO NOT SAY:\n\n* \u201cAccording to the retrieved context\u2026\u201d\n* \u201cThe database says\u2026\u201d\n* \u201cThe uploaded document mentions\u2026\u201d\n* \u201cThe vector store contains\u2026\u201d\n\nOUT-OF-SCOPE RULE:\nIf the required information is not available from the retrieval tool, reply exactly:\n\n<p>I\u2019m sorry, I don\u2019t have information about that at the moment.</p>\n\nLANGUAGE RULE:\n\n* Reply in the same language as the customer email when possible.\n* Keep the tone professional and customer-friendly.\n\nFINAL CHECK BEFORE RESPONDING:\nBefore sending the reply, verify:\n\n* Did I understand the customer\u2019s exact question?\n* Am I only answering what was asked?\n* Did I avoid unnecessary product information?\n* Is the response supported by retrieved information?\n* Is the email cleanly formatted?\n* Did I properly use HTML formatting?\n* Is the response concise and relevant?\n"
        }
      },
      "type": "@n8n/n8n-nodes-langchain.agent",
      "typeVersion": 3.1,
      "position": [
        -1152,
        0
      ],
      "id": "0b0a0f81-26e2-45c7-b1f3-218f6cb2d4ad",
      "name": "AI Agent"
    },
    {
      "parameters": {
        "model": {
          "__rl": true,
          "value": "gpt-4.1-mini",
          "mode": "list",
          "cachedResultName": "gpt-4.1-mini"
        },
        "builtInTools": {},
        "options": {}
      },
      "type": "@n8n/n8n-nodes-langchain.lmChatOpenAi",
      "typeVersion": 1.3,
      "position": [
        -1184,
        224
      ],
      "id": "837bb991-4b55-4ffc-9ace-dc1aa931cab0",
      "name": "OpenAI Chat Model"
    },
    {
      "parameters": {
        "mode": "retrieve-as-tool",
        "toolDescription": "=MANDATORY knowledge retrieval tool. The AI agent must use this tool before answering every non-greeting user question. Use this tool to check whether the requested information exists in the available knowledge source. If this tool does not return relevant information, the agent must politely say it does not have information and must not answer from general knowledge.",
        "tableName": {
          "__rl": true,
          "value": "documents",
          "mode": "list",
          "cachedResultName": "documents"
        },
        "topK": 3,
        "options": {
          "queryName": "match_documents"
        }
      },
      "type": "@n8n/n8n-nodes-langchain.vectorStoreSupabase",
      "typeVersion": 1.3,
      "position": [
        -1056,
        224
      ],
      "id": "f06eb2a0-8891-4a80-a5fb-eb74bad6ffc2",
      "name": "Supabase Vector Store1"
    },
    {
      "parameters": {
        "options": {}
      },
      "type": "@n8n/n8n-nodes-langchain.embeddingsOpenAi",
      "typeVersion": 1.2,
      "position": [
        -976,
        432
      ],
      "id": "944bc0cb-9eda-454d-a661-2f2dbcf16108",
      "name": "Embeddings OpenAI1"
    },
    {
      "parameters": {
        "chunkSize": 1500,
        "chunkOverlap": 300,
        "options": {}
      },
      "type": "@n8n/n8n-nodes-langchain.textSplitterRecursiveCharacterTextSplitter",
      "typeVersion": 1,
      "position": [
        -976,
        -416
      ],
      "id": "1e7743de-6b9b-4dc0-9439-eaeb3dd0516f",
      "name": "Recursive Character Text Splitter"
    },
    {
      "parameters": {
        "pollTimes": {
          "item": [
            {
              "mode": "everyMinute"
            }
          ]
        },
        "filters": {
          "readStatus": "unread"
        }
      },
      "type": "n8n-nodes-base.gmailTrigger",
      "typeVersion": 1.4,
      "position": [
        -1632,
        0
      ],
      "id": "af1c6a7a-e244-476c-bce5-250655bb8122",
      "name": "Gmail Trigger"
    },
    {
      "parameters": {
        "operation": "get",
        "messageId": "={{ $json.id }}"
      },
      "type": "n8n-nodes-base.gmail",
      "typeVersion": 2.2,
      "position": [
        -1408,
        0
      ],
      "id": "017a900a-691e-4929-93e9-853fecec4936",
      "name": "Get a message"
    },
    {
      "parameters": {
        "sendTo": "={{ $('Get a message').item.json.From.match(/<(.+?)>/)[1] }}",
        "subject": "=Re: {{ $('Get a message').item.json.Subject }}",
        "message": "={{ $('AI Agent').item.json.output }}",
        "options": {
          "appendAttribution": false
        }
      },
      "type": "n8n-nodes-base.gmail",
      "typeVersion": 2.2,
      "position": [
        -16,
        0
      ],
      "id": "f17c855e-62d9-4305-9070-e69bdeed2f87",
      "name": "Send a message"
    },
    {
      "parameters": {
        "resume": "webhook",
        "options": {}
      },
      "type": "n8n-nodes-base.wait",
      "typeVersion": 1.1,
      "position": [
        -464,
        0
      ],
      "id": "6f053694-49a8-4201-8dc6-45678fbc9be1",
      "name": "Wait"
    },
    {
      "parameters": {
        "conditions": {
          "options": {
            "caseSensitive": true,
            "leftValue": "",
            "typeValidation": "loose",
            "version": 3
          },
          "conditions": [
            {
              "id": "cac37e6f-4acf-406b-a2c7-fd109bf38885",
              "leftValue": "={{ true }}",
              "rightValue": "true",
              "operator": {
                "type": "string",
                "operation": "equals",
                "name": "filter.operator.equals"
              }
            }
          ],
          "combinator": "and"
        },
        "looseTypeValidation": true,
        "options": {}
      },
      "type": "n8n-nodes-base.if",
      "typeVersion": 2.3,
      "position": [
        -240,
        0
      ],
      "id": "fd04a0ef-b858-4408-b469-8aaedff22635",
      "name": "If"
    },
    {
      "parameters": {
        "sendTo": "jamilur0101@gmail.com",
        "subject": "=Approval Required: {{ $('Get a message').item.json.Subject }}",
        "message": "=<p>Hello Admin,</p>\n\n<p>\nA customer support email reply is waiting for your approval.\n</p>\n\n<p>\n<strong>Customer Subject:</strong><br>\n{{ $('Get a message').item.json.Subject }}\n</p>\n\n<p>\n<strong>Generated AI Reply:</strong>\n</p>\n\n<p>\n{{ $('AI Agent').item.json.output }}\n</p>\n\n<p>\nClick the link below to approve and continue the workflow:\n</p>\n\n<p>\n<a href=\"{{ $execution.resumeUrl }}\">\nApprove Email Reply\n</a>\n</p>\n\n<p>\nAfter clicking the link, the workflow will continue automatically and the customer will receive the email.\n</p>\n",
        "options": {}
      },
      "type": "n8n-nodes-base.gmail",
      "typeVersion": 2.2,
      "position": [
        -688,
        0
      ],
      "id": "11e4f5a8-cb70-4bf2-8e8b-915907284f9e",
      "name": "Send Approval Email"
    }
  ],
  "connections": {
    "When clicking \u2018Execute workflow\u2019": {
      "main": [
        [
          {
            "node": "Download file",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Download file": {
      "main": [
        [
          {
            "node": "Supabase Vector Store",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Embeddings OpenAI": {
      "ai_embedding": [
        [
          {
            "node": "Supabase Vector Store",
            "type": "ai_embedding",
            "index": 0
          }
        ]
      ]
    },
    "Default Data Loader": {
      "ai_document": [
        [
          {
            "node": "Supabase Vector Store",
            "type": "ai_document",
            "index": 0
          }
        ]
      ]
    },
    "OpenAI Chat Model": {
      "ai_languageModel": [
        [
          {
            "node": "AI Agent",
            "type": "ai_languageModel",
            "index": 0
          }
        ]
      ]
    },
    "Supabase Vector Store1": {
      "ai_tool": [
        [
          {
            "node": "AI Agent",
            "type": "ai_tool",
            "index": 0
          }
        ]
      ]
    },
    "Embeddings OpenAI1": {
      "ai_embedding": [
        [
          {
            "node": "Supabase Vector Store1",
            "type": "ai_embedding",
            "index": 0
          }
        ]
      ]
    },
    "Recursive Character Text Splitter": {
      "ai_textSplitter": [
        [
          {
            "node": "Default Data Loader",
            "type": "ai_textSplitter",
            "index": 0
          }
        ]
      ]
    },
    "Gmail Trigger": {
      "main": [
        [
          {
            "node": "Get a message",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Get a message": {
      "main": [
        [
          {
            "node": "AI Agent",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "AI Agent": {
      "main": [
        [
          {
            "node": "Send Approval Email",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Wait": {
      "main": [
        [
          {
            "node": "If",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "If": {
      "main": [
        [
          {
            "node": "Send a message",
            "type": "main",
            "index": 0
          }
        ],
        []
      ]
    },
    "Send Approval Email": {
      "main": [
        [
          {
            "node": "Wait",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  },
  "active": false,
  "settings": {
    "executionOrder": "v1",
    "binaryMode": "separate"
  },
  "versionId": "e71f5f49-64e1-45b0-b5b6-c795bec1a4c9",
  "meta": {
    "templateCredsSetupCompleted": true
  },
  "id": "4foecPoXQp9Pz67h",
  "tags": [
    {
      "name": "Assignment-06 Module-06",
      "id": "bQurwWiYNhgquCB5",
      "updatedAt": "2026-05-20T20:11:41.020Z",
      "createdAt": "2026-05-20T20:11:41.020Z"
    },
    {
      "updatedAt": "2026-04-19T10:36:06.995Z",
      "createdAt": "2026-04-19T10:36:06.995Z",
      "id": "qINUxDoUTgKfZG2J",
      "name": "OSTAD"
    }
  ]
}