AutomationFlowsAI & RAG › Create a Factual Learning Assistant with Rag, Gemini, Telegram & Mongodb

Create a Factual Learning Assistant with Rag, Gemini, Telegram & Mongodb

ByPawan @gladiator on n8n.io

This template is perfect for educational institutions, coaching centers (like UPSC, GMAT, or specialized technical training), internal corporate knowledge bases, and SaaS companies that need to provide instant, accurate, and source-grounded answers based on proprietary documents.

Chat trigger trigger★★★★☆ complexityAI-powered17 nodesDocument Default Data LoaderChat TriggerGoogle Drive TriggerVector Store Mongo DbatlasGoogle Gemini ChatGoogle DriveAgentForm Trigger
AI & RAG Trigger: Chat trigger Nodes: 17 Complexity: ★★★★☆ AI nodes: yes Added:

This workflow corresponds to n8n.io template #9595 — 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
{
  "name": "RAG-based AI Learning Assistant for Telegram using MongoDB and Google Drive",
  "tags": [],
  "nodes": [
    {
      "name": "Default Data Loader",
      "type": "@n8n/n8n-nodes-langchain.documentDefaultDataLoader",
      "position": [
        -160,
        256
      ],
      "parameters": {
        "options": {},
        "dataType": "binary"
      },
      "typeVersion": 1.1
    },
    {
      "name": "When chat message received",
      "type": "@n8n/n8n-nodes-langchain.chatTrigger",
      "position": [
        96,
        -32
      ],
      "parameters": {
        "options": {}
      },
      "typeVersion": 1.3
    },
    {
      "name": "File uploaded",
      "type": "n8n-nodes-base.googleDriveTrigger",
      "position": [
        -672,
        128
      ],
      "parameters": {
        "event": "fileCreated",
        "options": {},
        "pollTimes": {
          "item": [
            {
              "mode": "everyMinute"
            }
          ]
        },
        "triggerOn": "specificFolder",
        "folderToWatch": {
          "__rl": true,
          "mode": "list",
          "value": "<GOOGLE_DRIVE_FOLDER_ID_WATCH>",
          "cachedResultUrl": "https://drive.google.com/drive/folders/<GOOGLE_DRIVE_FOLDER_ID_WATCH>",
          "cachedResultName": "YT"
        }
      },
      "credentials": {},
      "typeVersion": 1
    },
    {
      "name": "MongoDB Atlas Vector Store - Insert",
      "type": "@n8n/n8n-nodes-langchain.vectorStoreMongoDBAtlas",
      "position": [
        -240,
        48
      ],
      "parameters": {
        "mode": "insert",
        "options": {},
        "mongoCollection": {
          "__rl": true,
          "mode": "list",
          "value": "<MONGO_ATLAS_COLLECTION_NAME>",
          "cachedResultName": "<MONGO_ATLAS_COLLECTION_NAME>"
        },
        "vectorIndexName": "<MONGO_ATLAS_VECTOR_INDEX_NAME_INSERT>",
        "embeddingBatchSize": 10
      },
      "credentials": {},
      "typeVersion": 1.3
    },
    {
      "name": "MongoDB Atlas Vector Store - Retrieve",
      "type": "@n8n/n8n-nodes-langchain.vectorStoreMongoDBAtlas",
      "position": [
        544,
        224
      ],
      "parameters": {
        "mode": "retrieve-as-tool",
        "options": {},
        "mongoCollection": {
          "__rl": true,
          "mode": "list",
          "value": "<MONGO_ATLAS_COLLECTION_NAME>",
          "cachedResultName": "<MONGO_ATLAS_COLLECTION_NAME>"
        },
        "toolDescription": "Tool for AI Agent: Use this to search and retrieve relevant documents from the vector store to answer questions or analyze or fulfill tasks.",
        "vectorIndexName": "<MONGO_ATLAS_VECTOR_INDEX_NAME_RETRIEVE>"
      },
      "credentials": {},
      "typeVersion": 1.3
    },
    {
      "name": "Google Gemini Chat Model",
      "type": "@n8n/n8n-nodes-langchain.lmChatGoogleGemini",
      "position": [
        272,
        256
      ],
      "parameters": {
        "options": {},
        "modelName": "models/gemini-2.5-flash-lite"
      },
      "credentials": {},
      "typeVersion": 1
    },
    {
      "name": "Download file",
      "type": "n8n-nodes-base.googleDrive",
      "position": [
        -480,
        128
      ],
      "parameters": {
        "fileId": {
          "__rl": true,
          "mode": "list",
          "value": "<GOOGLE_DRIVE_FILE_ID_DOWNLOAD>",
          "cachedResultUrl": "https://docs.google.com/document/d/<GOOGLE_DRIVE_FILE_ID_DOWNLOAD>/edit?usp=drivesdk",
          "cachedResultName": "History of modern India Spectrum 2"
        },
        "options": {},
        "operation": "download"
      },
      "credentials": {},
      "typeVersion": 3
    },
    {
      "name": "RAG Agent",
      "type": "@n8n/n8n-nodes-langchain.agent",
      "position": [
        384,
        32
      ],
      "parameters": {
        "text": "={{ $json.message.text }}{{ $json.chatInput }}",
        "options": {
          "systemMessage": "You are an Expert UPSC Examination Analyst and Study Assistant. Your primary function is to accurately, comprehensively, and analytically answer user queries related to the Union Public Service Commission (UPSC) Civil Services Examination (CSE).\n\nCore Directives and Persona\nExpertise: You are a master of the UPSC syllabus, exam patterns, current affairs relevance, and interdisciplinary analysis required for the Mains and Prelims examinations.\n\nRAG Mandate: You must utilize the context retrieved from the uploaded documents/knowledge base via the Retrieval-Augmented Generation (RAG) system to formulate your answers.\n\nSource Usage: Answer the query based primarily on the provided documents. Do not hallucinate or introduce information from your general training knowledge if it is contradicted by the documents.\n\nResponse Rules\nComprehensiveness & Depth: Provide answers that are deep, well-structured, and suitable for a high-level competitive exam like UPSC.\n\nAnalytical Approach: If the query asks for analysis, evaluation, comparison, or critical assessment, you must not simply restate facts. Instead, synthesize and analyze the information from the documents to provide a nuanced, insightful, and well-reasoned argument, as an expert would.\n\nSynthesis over Quotation: Do not use direct quotes or phrases like \"according to the document,\" \"the knowledge base states,\" or \"as per the database.\" Integrate the information naturally into a coherent and original answer.\n\nFormatting: Structure your response using clear headings, subheadings, and bullet points where appropriate to enhance readability for an examiner.\n\nLanguage and Tone: Maintain a formal, objective, and authoritative tone suitable for an academic and expert-level response.\n\nQuery Handling Procedure\nAnalyze Query: Determine the core subject, key concepts, and the type of response required (e.g., factual, analytical, comparative).\n\nRetrieve Context: Use the RAG tool to fetch the most relevant and complete information from the indexed documents.\n\nDraft Response: Synthesize the retrieved context and your domain expertise to craft a complete answer that adheres to all the above rules. "
        },
        "promptType": "define"
      },
      "typeVersion": 2.2
    },
    {
      "name": "On File Upload",
      "type": "n8n-nodes-base.formTrigger",
      "position": [
        -672,
        -32
      ],
      "parameters": {
        "options": {},
        "formTitle": "file upload",
        "formFields": {
          "values": [
            {
              "fieldType": "file",
              "fieldLabel": "file",
              "acceptFileTypes": ".pdf, .csv, .jpg, .jpeg, .png"
            }
          ]
        }
      },
      "typeVersion": 2.3
    },
    {
      "name": "Listen for Aspirant Question",
      "type": "n8n-nodes-base.telegramTrigger",
      "position": [
        96,
        96
      ],
      "parameters": {
        "updates": [
          "message"
        ],
        "additionalFields": {
          "chatIds": "@educationalch"
        }
      },
      "credentials": {},
      "typeVersion": 1.2
    },
    {
      "name": "Send Answer via Telegram",
      "type": "n8n-nodes-base.telegram",
      "position": [
        752,
        32
      ],
      "parameters": {
        "text": "={{ $json.output }}",
        "chatId": "=<TELEGRAM_CHAT_ID>",
        "additionalFields": {
          "appendAttribution": false
        }
      },
      "credentials": {},
      "retryOnFail": false,
      "typeVersion": 1.2
    },
    {
      "name": "Convert Documents to Embeddings",
      "type": "@n8n/n8n-nodes-langchain.embeddingsGoogleGemini",
      "position": [
        -384,
        416
      ],
      "parameters": {},
      "credentials": {},
      "typeVersion": 1
    },
    {
      "name": "Retrieve Documents from Embeddings",
      "type": "@n8n/n8n-nodes-langchain.embeddingsGoogleGemini",
      "position": [
        656,
        384
      ],
      "parameters": {},
      "credentials": {},
      "typeVersion": 1
    },
    {
      "name": "Simple Memory",
      "type": "@n8n/n8n-nodes-langchain.memoryBufferWindow",
      "position": [
        400,
        240
      ],
      "parameters": {},
      "typeVersion": 1.3
    },
    {
      "name": "Sticky Note",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        -1440,
        -112
      ],
      "parameters": {
        "width": 672,
        "height": 1344,
        "content": "Who's it for?\nThis template is perfect for educational institutions, coaching centers (like UPSC, GMAT, or specialized technical training), internal corporate knowledge bases, and SaaS companies that need to provide instant, accurate, and source-grounded answers based on proprietary documents.\n\nIt's designed for users who want to leverage **Google Gemini's** powerful reasoning but ensure its answers are strictly factual and based only on their verified knowledge repository.\n\n## How it works / What it does\nThis workflow establishes a **Retrieval-Augmented Generation (RAG) pipeline** to build a secure, fact-based AI Agent. It operates in two main phases:\n\n**Knowledge Ingestion:** When a new document (e.g., a PDF, lecture notes, or policy manual) is uploaded via a **form** or **Google Drive**, the **Embeddings Google Gemini** node converts the content into numerical vectors. These vectors are then stored in a secure **MongoDB Atlas Vector Store**, creating a private knowledge base.\n\n**AI Query & Response:** A user asks a question via **Telegram**. The **RAG Agent** uses the question to perform a semantic search on the **MongoDB Vector Store**, retrieving the most relevant, source-specific passages. It then feeds this retrieved context to the **Google Gemini Chat Model** to generate a precise, factual answer, which is sent back to the user on Telegram.\n\nThis process ensures the agent never \"hallucinates\" or uses general internet knowledge, making the responses accurate and trustworthy.\n\n## Requirements\nTo use this template, you will need the following accounts and credentials:\n\n* **n8n Account**\n* **Google Gemini API Key:** For generating vector embeddings and powering the AI Agent.\n* **MongoDB Atlas Cluster:** A free-tier cluster is sufficient, configured with a Vector Search index.\n* **Telegram Bot:** A bot created via BotFather and a Chat ID where the bot will listen for and send messages.\n* **Google Drive Credentials** (if using the Google Drive ingestion path).\n\n## How to set up\n\n1.  **Set up MongoDB Atlas:** Create a free cluster and a database. Create a Vector Search Index on your collection to enable efficient searching.\n2.  **Configure Ingestion Path (Left Side):** Set up the **On File Upload** webhook or connect your **Google Drive** credentials. Configure the **Embeddings** and **MongoDB Insert** nodes with your credentials, collection name, and index name.\n3.  **Configure Chat Path (Right Side):** Set up the **Telegram Trigger** with your Bot Token/Webhook. Configure the **Google Gemini Chat Model** and the **MongoDB Retrieve** tool with your credentials/index details.\n4.  **Final Step:** Configure the **Send Answer via Telegram** node with your Bot Token and the Chat ID."
      },
      "typeVersion": 1
    },
    {
      "name": "Sticky Note1",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        -752,
        -160
      ],
      "parameters": {
        "color": 4,
        "width": 816,
        "height": 752,
        "content": "Workflow 1: Knowledge Ingestion Pipeline\n(Triggers on file upload to form or Google Drive)"
      },
      "typeVersion": 1
    },
    {
      "name": "Sticky Note2",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        80,
        -160
      ],
      "parameters": {
        "color": 6,
        "width": 896,
        "height": 768,
        "content": "Workflow 2: RAG Chatbot Query Pipeline\n(Triggers on question received via Telegram)"
      },
      "typeVersion": 1
    }
  ],
  "active": true,
  "settings": {
    "executionOrder": "v1"
  },
  "connections": {
    "RAG Agent": {
      "main": [
        [
          {
            "node": "Send Answer via Telegram",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Download file": {
      "main": [
        [
          {
            "node": "MongoDB Atlas Vector Store - Insert",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "File uploaded": {
      "main": [
        [
          {
            "node": "Download file",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Simple Memory": {
      "ai_memory": [
        [
          {
            "node": "RAG Agent",
            "type": "ai_memory",
            "index": 0
          }
        ]
      ]
    },
    "On File Upload": {
      "main": [
        [
          {
            "node": "MongoDB Atlas Vector Store - Insert",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Default Data Loader": {
      "ai_document": [
        [
          {
            "node": "MongoDB Atlas Vector Store - Insert",
            "type": "ai_document",
            "index": 0
          }
        ]
      ]
    },
    "Google Gemini Chat Model": {
      "ai_languageModel": [
        [
          {
            "node": "RAG Agent",
            "type": "ai_languageModel",
            "index": 0
          }
        ]
      ]
    },
    "When chat message received": {
      "main": [
        [
          {
            "node": "RAG Agent",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "MongoDB Atlas Vector Store1": {
      "ai_tool": [
        [
          {
            "node": "RAG Agent",
            "type": "ai_tool",
            "index": 0
          }
        ]
      ]
    },
    "Listen for Aspirant Question": {
      "main": [
        [
          {
            "node": "RAG Agent",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Convert Documents to Embeddings": {
      "ai_embedding": [
        [
          {
            "node": "MongoDB Atlas Vector Store - Insert",
            "type": "ai_embedding",
            "index": 0
          }
        ]
      ]
    },
    "Retrieve Documents from Embeddings": {
      "ai_embedding": [
        [
          {
            "node": "MongoDB Atlas Vector Store1",
            "type": "ai_embedding",
            "index": 0
          }
        ]
      ]
    }
  }
}
Pro

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

About this workflow

This template is perfect for educational institutions, coaching centers (like UPSC, GMAT, or specialized technical training), internal corporate knowledge bases, and SaaS companies that need to provide instant, accurate, and source-grounded answers based on proprietary documents.

Source: https://n8n.io/workflows/9595/ — 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

This workflow acts as a 24/7 sales agent, engaging leads across WhatsApp, Instagram, Facebook, Telegram, and your website. It intelligently transcribes audio messages, answers questions using a knowle

Chat Trigger, Memory Postgres Chat, Tool Workflow +20
AI & RAG

• Create a Google Drive folder to watch. • Connect your Google Drive account in n8n and authorize access. • Point the Google Drive Trigger node to this folder (new/modified files trigger the flow).

Agent, Chat Trigger, Memory Buffer Window +14
AI & RAG

⚡AI-Powered YouTube Playlist & Video Summarization and Analysis v2. Uses lmChatGoogleGemini, agent, splitOut, chainLlm. Chat trigger; 72 nodes.

Google Gemini Chat, Agent, Chain Llm +11
AI & RAG

This n8n workflow transforms entire YouTube playlists or single videos into interactive knowledge bases you can chat with. Ask questions and get summaries without needing to watch hours of content. 🔗

Google Gemini Chat, Agent, Chain Llm +11
AI & RAG

use cases: research stock market in Indonesia. analyze the performance of companies belonging to certain subsectors or company comparing financial metrics between BBCA and BBRI providing technical ana

Chat Trigger, Chat, Telegram Trigger +10