Skip to content

RAG BOT Ahmer

A multilingual Telegram chatbot that provides intelligent customer service responses using Retrieval-Augmented Generation (RAG). The bot detects whether users are communicating in English or Luganda, translates when necessary, searches a knowledge base for relevant information, and generates contextually appropriate responses while learning user preferences over time.

Purpose

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

This workflow creates an AI-powered customer service bot that can handle inquiries in both English and Luganda languages. The system maintains user profiles to track conversation history, learned behaviors, and user needs, enabling personalized responses that improve over time.

How It Works

  1. Message Reception: A user sends a message via Telegram
  2. User Profile Check: The system checks if the user exists in the database, creating a new profile if needed
  3. Language Detection: The message is analyzed to determine if it's in English or Luganda
  4. Translation (if needed): Luganda messages are translated to English for processing
  5. Embedding Generation: The user's query is converted to a vector embedding
  6. Knowledge Base Search: The system finds the top 3 most relevant documents from the knowledge base
  7. User Context Retrieval: Current user profile data (needs, behavior, conversation history) is fetched
  8. Response Generation: An AI model generates a response using the query, knowledge base results, and user context
  9. Profile Update: User needs, learned behavior, and conversation history are updated
  10. Response Delivery: The final response is sent back to the user via Telegram

Workflow Diagram

graph TD
    A[Telegram Trigger] --> B[Check User Profile]
    B --> C{User Exists?}
    C -->|No| D[Execute a 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 into English]
    H -->|No| J[Sent Input for Embedding In English]
    I --> K[Merge1]
    J --> L[Sent Input Embedding for Luganda]
    K --> M{Final Language Check}
    M -->|English| N[Sent Input for Embedding In English]
    M -->|Luganda| L
    N --> O[Merge2]
    L --> O
    O --> P[Generate Embedding via API]
    P --> Q[Format Embedding Array]
    Q --> R[Vector Search Top 3]
    R --> S[Execute a SQL query2 - Get User Profile]
    S --> T[Inject prompt to model]
    T --> U[Response Agent]
    U --> V[Pass user history]
    V --> W[Update Database]
    W --> X[Send a text message]

    Y[When clicking Execute workflow] --> Z[Execute a SQL query1 - Get Documents]
    Z --> AA[Document Loader]
    AA --> BB[Postgres Vector Store]
    CC[HuggingFace Embeddings] --> BB
    DD[Recursive Text Splitter] --> AA

Trigger

Telegram Trigger: Activates when a user sends a message to the configured Telegram bot. The trigger captures message content, user ID, and chat information.

Nodes Used

Node Type Purpose
Telegram Trigger Receives incoming messages from Telegram users
Postgres Manages user profiles, knowledge base documents, and vector embeddings
HTTP Request Calls HuggingFace APIs for language detection, translation, and embeddings
Code (JavaScript) Processes language detection results and formats data
If Controls workflow branching based on user existence and language
Merge Combines data streams from different workflow paths
Document Loader Loads documents for vector store creation
Text Splitter Splits documents into chunks for embedding
Vector Store Stores and searches document embeddings
Telegram Sends responses back to users

External Services & Credentials Required

HuggingFace API

  • 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)
  • Credentials: HuggingFace API tokens (multiple tokens used)

Telegram Bot API

  • Credentials: Telegram Bot Token
  • Webhook Configuration: Required for receiving messages

PostgreSQL Database

  • Credentials: Database connection details
  • Extensions: pgvector extension for vector similarity search

Environment Variables

The workflow requires the following credentials to be configured in n8n:

  • HUGGINGFACE_API_TOKEN_1: For language detection and embeddings
  • HUGGINGFACE_API_TOKEN_2: For translation and LLM inference
  • TELEGRAM_BOT_TOKEN: For Telegram bot communication
  • POSTGRES_CONNECTION: Database connection string

Data Flow

Input

  • Telegram message text
  • User ID and chat information
  • Message metadata (timestamp, etc.)

Processing

  • Language detection scores
  • Translated text (if applicable)
  • Vector embeddings (768-dimensional)
  • Knowledge base search results
  • User profile data (needs, behavior, history)

Output

  • Contextual response text in user's preferred language
  • Updated user profile with learned behavior and needs
  • Conversation history maintenance

Error Handling

The workflow includes basic error handling through: - Always Output Data settings on critical nodes to prevent workflow stops - Try-catch logic in JavaScript code nodes for JSON parsing - Conditional branching to handle missing user profiles - Fallback responses when AI model outputs cannot be parsed

Known Limitations

  • Language detection may not be 100% accurate for short messages
  • Translation quality depends on HuggingFace model performance
  • Vector search limited to top 3 results
  • Conversation history limited to last 5 messages
  • No explicit error messaging to users when processing fails

No related workflows specified in the current context.

Setup Instructions

1. Database Setup

 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
27
-- Create 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 '{"messages": []}'::jsonb,
    needs JSONB DEFAULT '{}'::jsonb,
    learned_behavior JSONB DEFAULT '{}'::jsonb
);

-- Create knowledge base documents table
CREATE TABLE knowledge_base_documents (
    id SERIAL PRIMARY KEY,
    content TEXT NOT NULL,
    metadata JSONB DEFAULT '{}'::jsonb
);

-- Create vector embeddings table (requires pgvector extension)
CREATE EXTENSION IF NOT EXISTS vector;
CREATE TABLE document_embbedings (
    id SERIAL PRIMARY KEY,
    embedding vector(768),
    metadata JSONB
);

2. Telegram Bot Setup

  1. Create a new bot via @BotFather on Telegram
  2. Get the bot token
  3. Configure the webhook URL in n8n

3. HuggingFace Setup

  1. Create HuggingFace account
  2. Generate API tokens
  3. Ensure access to required models

4. n8n Configuration

  1. Import the workflow JSON
  2. Configure all credential connections:
    • PostgreSQL database
    • Telegram bot token
    • HuggingFace API tokens
  3. Populate knowledge base documents
  4. Run the document embedding workflow (manual trigger)
  5. Activate the main Telegram trigger

5. Initial Data Population

Run the manual trigger workflow to embed your knowledge base documents into the vector store before activating the bot.