E-mail Chatbot with Combined Semantic and Structured RAG Using Telegram and Pgvector

This workflow implements an intelligent email Q&A bot that allows users to interact with it via Telegram for quick inquiries about their personal emails. It combines semantic search with structured SQL queries, enabling it to understand natural language questions and accurately locate email content and time information, thereby providing precise answers. This system is particularly suitable for individuals and businesses that require efficient email management, enhancing the intelligence and convenience of email queries.

Workflow Diagram
E-mail Chatbot with Combined Semantic and Structured RAG Using Telegram and Pgvector Workflow diagram

Workflow Name

E-mail Chatbot with Combined Semantic and Structured RAG Using Telegram and Pgvector

Key Features and Highlights

This workflow develops an intelligent email Q&A chatbot that integrates semantic retrieval (based on vector search) with structured database queries (SQL) to enable efficient and intelligent querying of personal email databases. Users can interact directly via the Telegram chat interface or the built-in n8n chat. The chatbot comprehends natural language questions, automatically decomposes them, and invokes both vector search and SQL query engines to accurately locate email content and relevant temporal information, providing precise answers.

Core Problems Addressed

Traditional email search largely relies on keyword matching, which struggles to understand semantics and context, especially for queries involving time ranges and event details. By combining Pgvector’s vectorized semantic search with SQL’s structured querying, this workflow solves the challenges of semantic understanding of email content and precise temporal retrieval, enabling intelligent responses to complex email queries.

Application Scenarios

  • Rapidly querying email history for individuals or enterprises, such as finding meeting schedules, interview times, service registration dates, and other specific information
  • Conversational retrieval of email content anytime, anywhere via Telegram
  • Semantic Q&A on email data to improve email management efficiency and ease of information access
  • Acting as an intelligent assistant to help users manage and review email communication records

Main Workflow Steps

  1. Trigger Entry: Workflow is initiated by receiving a message on Telegram or via the built-in n8n chat
  2. Session Management: Generate and manage session IDs to support contextual memory
  3. Natural Language Understanding: Pass user input text to the AI Agent to parse query intent
  4. Semantic Vector Retrieval: Use the Pgvector vector database to perform semantic search on email texts and retrieve relevant email snippets
  5. Structured SQL Query: Based on email IDs from vector search results, invoke SQL query tools to obtain detailed structured email information (e.g., date, subject)
  6. Answer Generation and Formatting: The AI Agent synthesizes results from both queries to generate accurate answers and formats the response content
  7. Response Output: Segment the answer and send batch replies via Telegram messages to ensure completeness and readability

Involved Systems and Services

  • Telegram: Primary user interaction interface for receiving triggers and sending replies
  • Postgres PGVector: Stores vectorized embeddings of emails to enable semantic retrieval
  • SQL Database (Postgres): Stores structured email data supporting precise SQL queries
  • n8n LangChain Nodes: Integrate and orchestrate AI Agent, vector retrieval, and SQL query tools
  • OpenAI Model (mistral-small3.1) and Ollama Embeddings: Provide language understanding and vector embedding capabilities

Target Users and Value

  • Individual users with high email management needs who want to quickly query email history using natural language
  • Enterprise users and teams requiring efficient querying and summarization of large volumes of email communications
  • Developers and automation engineers aiming to build intelligent email Q&A assistants to enhance office automation
  • AI and data integration scenarios driving intelligent retrieval and insight extraction from email content

This workflow empowers users to intelligently query emails conversationally by leveraging advanced fusion of semantic and structured retrieval technologies, significantly enhancing the intelligence and convenience of email search.