Skip to content

RAG BOT Ahmer

A multilingual Telegram chatbot that provides intelligent customer service using Retrieval-Augmented Generation (RAG) technology. The bot can communicate in both English and Luganda, automatically detects the user's preferred language, maintains conversation history, and learns user behavior patterns to provide personalized responses.

Purpose

No business context provided yet — add a context.md to enrich this documentation.

How It Works

  1. User Interaction: A user sends a message to the Telegram bot
  2. User Profile Check: The system checks if the user exists in the database
  3. Profile Creation: If new user, creates a profile with default settings
  4. Language Detection: Analyzes the incoming message to determine if it's in English or Luganda
  5. Translation: If the message is in Luganda, translates it to English for processing
  6. Text Embedding: Converts the user's query into a vector embedding for similarity search
  7. Knowledge Retrieval: Searches the vector database for the top 3 most relevant documents
  8. Context Assembly: Combines user query, conversation history, user preferences, and retrieved knowledge
  9. AI Response Generation: Uses a language model to generate a contextual response
  10. Profile Update: Updates the user's profile with new conversation data, needs, and learned behaviors
  11. Response Delivery: Sends the response back to the user via Telegram

The workflow also includes a separate one-time setup process for embedding knowledge base documents into the vector store.

Workflow Diagram

graph TD
    A[Telegram Trigger] --> B[Check User Profile]
    B --> C{User Exists?}
    C -->|No| D[Execute SQL Query - Create Profile]
    C -->|Yes| E[Merge]
    D --> E
    E --> F[Language Detection]
    F --> G[Code in JavaScript - Parse Language]
    G --> H{Language is Luganda?}
    H -->|Yes| I[Translate Luganda to English]
    H -->|No| J[Merge1]
    I --> J
    J --> K{Language is English?}
    K -->|Yes| L[Sent Input for Embedding In English]
    K -->|No| M[Sent Input Embedding for Luganda]
    L --> N[Merge2]
    M --> N
    N --> O[Generate Embedding via API]
    O --> P[Format Embedding Array]
    P --> Q[Vector Search Top 3]
    Q --> R[Execute SQL Query2 - Get User Profile]
    R --> S[Inject prompt to model]
    S --> T[Response Agent]
    T --> U[Pass user history]
    U --> V[Update Database]
    V --> W[Send a text message]

    X[Manual Trigger] --> Y[Execute SQL Query1 - Get Documents]
    Y --> Z[Postgres Vector Store]
    AA[Document Loader] --> Z
    BB[HuggingFace Embeddings] --> Z
    CC[Recursive Text Splitter] --> AA

Trigger

  • Primary: Telegram webhook trigger that activates when users send messages to the bot
  • Secondary: Manual trigger for one-time document embedding setup

Nodes Used

Node Type Purpose
Telegram Trigger Receives incoming messages from Telegram users
Postgres Database operations for user profiles and knowledge base
HTTP Request Calls external APIs for language detection, translation, and embeddings
Code (JavaScript) Data processing, formatting, and business logic
If Conditional routing based on user existence and language
Merge Combines data streams from different paths
Document Loader Loads documents for vector embedding
Recursive Text Splitter Splits documents into chunks for embedding
HuggingFace Embeddings Generates vector embeddings for documents
Postgres Vector Store Stores and retrieves document embeddings
Telegram Sends responses back to users

External Services & Credentials Required

APIs and Services

  • Telegram Bot API: For receiving and sending messages
  • HuggingFace Inference API: For language detection, translation, embeddings, and text generation
  • PostgreSQL Database: For storing user profiles and vector embeddings

Required Credentials

  • telegramApi - Telegram bot token (credential: "Ahmer's Bot")
  • postgres - PostgreSQL database connection (credential: "Postgres Waringa")
  • huggingFaceApi - HuggingFace API token (credential: "Waresh")

Models Used

  • facebook/bart-large-mnli - Language detection
  • Helsinki-NLP/opus-mt-mul-en - Luganda to English translation
  • BAAI/bge-small-en-v1.5 - Text embeddings
  • mistralai/Mistral-7B-Instruct-v0.2 - Response generation

Environment Variables

No specific environment variables are configured in this workflow. All external service connections use n8n's credential system.

Data Flow

Input

  • Telegram messages containing user queries in English or Luganda
  • User metadata (user ID, chat ID) from Telegram

Processing

  • User profile data (conversation history, needs, learned behavior)
  • Language detection results
  • Translated text (if applicable)
  • Vector embeddings of user queries
  • Retrieved knowledge base documents
  • AI-generated responses with metadata

Output

  • Contextual responses sent via Telegram
  • Updated user profiles in PostgreSQL
  • Conversation history tracking
  • Inferred user needs and behavioral patterns

Error Handling

The workflow includes basic error handling through: - Conditional checks for user existence before database operations - alwaysOutputData settings on critical nodes to prevent workflow breaks - Merge nodes to handle different execution paths - Try-catch logic in JavaScript code nodes for JSON parsing

Known Limitations

No specific limitations documented in the provided context.

No related workflows mentioned in the provided context.

Setup Instructions

Prerequisites

  1. n8n instance with LangChain nodes installed
  2. PostgreSQL database with vector extension (pgvector)
  3. Telegram bot token
  4. HuggingFace API access

Database Setup

  1. Create the required tables:
     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    -- User profiles table
    CREATE TABLE user_profiles (
      id SERIAL PRIMARY KEY,
      user_id BIGINT UNIQUE NOT NULL,
      customer_id VARCHAR(255),
      preferred_language VARCHAR(50),
      profile_created_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
      last_interaction_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
      conversation_history JSONB DEFAULT '[]',
      needs JSONB DEFAULT '[]',
      learned_behavior JSONB DEFAULT '{}'
    );
    
    -- Knowledge base documents table
    CREATE TABLE knowledge_base_documents (
      id SERIAL PRIMARY KEY,
      content TEXT NOT NULL,
      metadata JSONB
    );
    
    -- Document embeddings table (requires pgvector extension)
    CREATE TABLE document_embbedings (
      id SERIAL PRIMARY KEY,
      embedding VECTOR(384),
      metadata JSONB
    );
    

Workflow Import

  1. Import the workflow JSON into your n8n instance
  2. Configure credentials:
    • Set up Telegram API credentials with your bot token
    • Configure PostgreSQL connection
    • Add HuggingFace API token
  3. Populate the knowledge base by running the manual trigger path
  4. Activate the workflow to start receiving Telegram messages

Initial Knowledge Base Setup

  1. Insert your knowledge base documents into the knowledge_base_documents table
  2. Execute the manual trigger to generate embeddings for all documents
  3. Verify embeddings are stored in the document_embbedings table

The workflow will automatically handle user registration, language detection, and response generation once properly configured.