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¶
- Message Reception: A user sends a message via Telegram
- User Profile Check: The system checks if the user exists in the database, creating a new profile if needed
- Language Detection: The message is analyzed to determine if it's in English or Luganda
- Translation (if needed): Luganda messages are translated to English for processing
- Embedding Generation: The user's query is converted to a vector embedding
- Knowledge Base Search: The system finds the top 3 most relevant documents from the knowledge base
- User Context Retrieval: Current user profile data (needs, behavior, conversation history) is fetched
- Response Generation: An AI model generates a response using the query, knowledge base results, and user context
- Profile Update: User needs, learned behavior, and conversation history are updated
- 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 embeddingsHUGGINGFACE_API_TOKEN_2: For translation and LLM inferenceTELEGRAM_BOT_TOKEN: For Telegram bot communicationPOSTGRES_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
Related Workflows¶
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 | |
2. Telegram Bot Setup¶
- Create a new bot via @BotFather on Telegram
- Get the bot token
- Configure the webhook URL in n8n
3. HuggingFace Setup¶
- Create HuggingFace account
- Generate API tokens
- Ensure access to required models
4. n8n Configuration¶
- Import the workflow JSON
- Configure all credential connections:
- PostgreSQL database
- Telegram bot token
- HuggingFace API tokens
- Populate knowledge base documents
- Run the document embedding workflow (manual trigger)
- 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.