Chatbot for Email History Search using RAG and PGVector - n8n Workflow

Deploy an advanced n8n workflow to chat with your email history via Telegram. This RAG system uses an n8n AI Agent, PGVector, structured SQL tools, and a local LLM (Mistral via Ollama).

Workflow Preview

Ready to automate?

Download this n8n workflow template and start using it instantly.

Who is this best for?

n8n Automation Specialists seeking complex RAG implementations.
Developers wanting to integrate PGVector and local LLMs (like Mistral via Ollama) for semantic search.
Businesses needing a conversational interface to query their historical email data.
Users looking for advanced examples of the n8n agent node using multiple tools.

Overview

This sophisticated n8n workflow solves the challenge of querying unstructured personal data (like emails) by combining semantic and structured search techniques, known as Dual RAG. The core of this automation is a powerful n8n AI Agent that utilizes two distinct data sources:


  1. Vector Search (Semantic RAG): Using the n8n node for Postgres PGVector Store, the agent can perform conceptual or contextual searches against email embeddings, powered by an Embeddings Ollama n8n node.

  2. Structured Search (SQL RAG): For precise queries involving dates, recipients, or subjects, the agent calls an external n8n workflow (referenced as 'Call the SQL composer Workflow') that translates natural language into database queries.

By leveraging the n8n trigger capabilities of Telegram, this entire system is deployed as a conversational bot, providing instant, highly accurate answers based on historical email communication. This demonstrates how a comprehensive n8n workflow can manage complex information retrieval.

How it Works

The n8n workflow initiates when a message is received via either the Telegram Trigger or the When chat message received n8n trigger (for manual chat execution or API use).


  1. Input Handling: The Generate session id n8n node standardizes the incoming message and creates a unique session key for conversation memory.

  2. AI Orchestration: The AI Agent n8n node receives the user query. It is configured with system instructions detailing the email database schema and critical time handling rules. It uses the OpenAI Chat Model (pointing to a local Mistral instance) and Simple Memory to maintain context.

  3. Tool Selection: Based on the query, the AI Agent decides whether to use the Postgres PGVector Store n8n node for semantic RAG or the Call the SQL composer Workflow n8n node for structured SQL RAG.

  4. Response Generation: After retrieving relevant data from the tools, the AI Agent synthesizes the final response.

  5. Output Routing: A conditional Came from Telegram? n8n node checks the origin.

  6. Telegram Response Formatting (Conditional): If the input came from Telegram, the long text response is processed by a Code n8n node (Split text into chunks) to ensure messages adhere to size limits. It then loops through these chunks (Loop Over Items), escapes Markdown characters (Escape Markdown n8n node) to prevent formatting errors in Telegram's MarkdownV2 parser, and finally uses the Respond on Telegram in batches n8n node to send the answer piece by piece. If the input came from the n8n chat, the Beautify chat response n8n node prepares the output for display within the n8n interface.

Installation Guide

To use this n8n workflow, follow these steps:


  1. Import: Copy the provided JSON data and paste it into your n8n instance using the 'New' -> 'Import from JSON' function.

  2. External Workflow: This n8n workflow requires a linked sub-workflow. You must download and install the related 'Translate questions about e-mails into SQL queries and run them' n8n template and connect it in the Call the SQL composer Workflow n8n node.

  3. Credentials Setup:

Telegram: Set up Telegram API credentials for the Telegram Trigger n8n node and the Respond on Telegram in batches n8n node, specifying your bot token and allowing access for your specific chat ID.
Postgres/PGVector: Configure credentials for the Postgres PGVector Store n8n node to connect to your email embeddings database.
* LLM/Embeddings: Configure the Ollama API credentials for both the OpenAI Chat Model (for the Mistral LLM) and the Embeddings Ollama n8n node.

  1. Activation: Ensure the Telegram Trigger n8n trigger is active and initialized (listening for messages).

  2. Test: Send a test message to your Telegram bot or use the internal chat n8n trigger to verify the RAG system functionality. This complex n8n template is ready for deployment.

Node Details

This n8n workflow utilizes several specialized n8n node types:

Telegram Trigger: This specialized n8n trigger starts the workflow upon receiving a message update in Telegram, capturing the chat and message IDs.
AI Agent: The central orchestration n8n node, which interprets the user query, manages conversation state via memory, and decides which tool (emailsvectorsearch or emailsqlsearch) to execute based on its system prompt.
OpenAI Chat Model: Despite the name, this n8n node is configured here to use a local LLM (mistral-small3.1:latest) accessed via an Ollama endpoint, acting as the brain for the AI Agent.
Postgres PGVector Store: Configured as the emailsvectorsearch tool, this n8n node performs semantic retrieval against the embedded email content (RAG). It uses the 'retrieve-as-tool' mode.
Embeddings Ollama: Provides the necessary embedding model (nomic-embed-text:latest) used by the PGVector n8n node for vector similarity calculation.
Call the SQL composer Workflow: Configured as the emailsqlsearch tool, this powerful n8n node links to a separate n8n workflow template responsible for translating complex questions into database queries.
Split text into chunks (Code): A custom Code n8n node designed to handle large AI responses by splitting them at word boundaries into manageable chunks (max 500 characters) before sending them to Telegram.
Escape Markdown (Code): Another crucial Code n8n node that sanitizes the output string, escaping special characters (like underscores, dots, and brackets) to ensure correct rendering under Telegram's strict MarkdownV2 rules before using the response n8n node.

Related n8n Workflows

Free

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

Software Engineering | Die-hard maker

Featured*