A Telegram bot that uses vector embeddings and RAG to provide context-aware responses.
- Listens to and stores chat messages in a vector database
- Uses embeddings to find similar messages when questions are asked
- Generates responses using OpenAI models with chat history context
- Supports uploading Telegram chat backup files
This project implements a Telegram bot that:
- Stores all messages it receives in a vector database (Qdrant) with their embeddings
- Retrieves semantically similar messages when queried
- Generates intelligent responses using OpenAI's GPT models based on the retrieved context
- Stays up-to-date with the latest conversations in real-time
The bot uses a Retrieval-Augmented Generation (RAG) approach, which enhances large language model responses with relevant information from a knowledge base - in this case, the chat history.
The project consists of three main components:
The core bot that:
- Interacts with users through Telegram
- Processes incoming messages and queries
- Stores messages in the vector database
- Retrieves relevant messages when queried
- Calls OpenAI API to generate responses
- Presents both AI-generated answers and relevant messages to users
A FastAPI service that:
- Generates text embeddings using the
https://huggingface.co/sentence-transformers/distiluse-base-multilingual-cased-v1
model - Provides a REST API for embedding generation
- Supports batch processing of texts
- Runs as a separate microservice
A utility tool that:
- Parses Telegram group backups in JSON format
- Extracts text from each message
- Calculates embeddings using the embedding service
- Saves message data to the Qdrant vector database
-
Message Storage:
- When the bot receives a regular message, it generates embeddings for the text
- The message text, username, and embeddings are stored in Qdrant
- The bot intelligently ignores its own messages to prevent redundant storage
-
Query Processing:
- When the bot is mentioned with a query, it generates embeddings for the query
- It searches the vector database for the top 10 semantically similar messages
- It constructs a prompt for OpenAI using these messages
- It calls the OpenAI API to generate a response
- It returns both the AI-generated answer and the top 5 most relevant messages
-
Historical Data Import:
- The backup uploader tool can be used to import historical chat data
- This allows the bot to have context from conversations that happened before it was added to a group
- Go 1.21 or higher
- Docker and Docker Compose
- Telegram Bot Token (from BotFather)
- OpenAI API Key
Required:
TELEGRAM_BOT_TOKEN
: Your Telegram bot token from BotFatherOPENAI_API_KEY
: Your OpenAI API key
Optional:
TG_GROUP_LIST
: Comma-separated list of allowed group/chat IDsEMBEDDING_SERVICE_ADDRESS
: Custom address for embedding serviceQDRANT_SERVICE_ADDRESS
: Custom address for Qdrant service
-
Clone the repository:
git clone https://github.com/korjavin/ragtgbot.git cd ragtgbot
-
Set your environment variables:
export TELEGRAM_BOT_TOKEN=your_telegram_token export OPENAI_API_KEY=your_openai_api_key
-
Start all services using Docker Compose:
docker-compose up -d
Alternatively, use pre-built images from GitHub Container Registry:
docker-compose -f docker-compose.ghcr.yml up -d
cd cmd/tgbot
go run main.go
cd embedding_service
pip install -r requirements.txt
python main.py
cd cmd/uploadbackup
go run main.go
The bot uses the following configuration options:
- Embedding Service: Address configurable via
EMBEDDING_SERVICE_ADDRESS
environment variable (default: "http://localhost:8000/embeddings") - Qdrant Service: Address configurable via
QDRANT_SERVICE_ADDRESS
environment variable (default: "http://localhost:6333") - Collection Name: Name of the collection in Qdrant (default: "chat_history")
- OpenAI Model: Model to use for generating responses (default: "gpt-4o-mini")
- Vector Search Limit: Number of similar messages to retrieve (default: 10)
The bot can be restricted to specific Telegram groups/chats:
- Set
TG_GROUP_LIST
environment variable with a comma-separated list of allowed group/chat IDs - Example:
export TG_GROUP_LIST="-1001234567890,-1009876543210"
- When restricted, the bot will:
- Only store and process messages from allowed groups
- Respond with an explanation message in other chats
- Continue to ignore messages that don't mention it
- If
TG_GROUP_LIST
is not set, the bot will work in all chats
To find your group ID:
- Add the bot to your group
- Send a message in the group
- Check the bot's logs for the chat ID number
- Use this ID in the TG_GROUP_LIST
Example docker-compose configuration:
services:
tgbot:
environment:
- TG_GROUP_LIST=-1001234567890,-1009876543210
-
Add the bot to a Telegram group or start a direct conversation with it.
-
Send regular messages to be stored in the vector database.
-
Mention the bot with a query to get an AI-generated response based on chat history:
@your_bot_name what did we discuss yesterday about the project?
-
The bot will respond with:
- An AI-generated answer based on the context
- A list of the most relevant messages from the chat history
The project provides Docker images for all components:
- Telegram Bot:
ghcr.io/korjavin/ragtgbot-tgbot:latest
- Embedding Service:
ghcr.io/korjavin/ragtgbot-embedding-service:latest
go test ./...
golangci-lint run
Contributions are welcome! Please feel free to submit a Pull Request.
This project is licensed under the MIT License