This document describes the internal structure and logic of the Telegram bot.
The project implements the Retrieval-Augmented Generation (RAG) pattern.
- Input Request: A user sends a text or voice message in Telegram.
- Processing:
- Voice-to-Text (if needed): Voice messages are transcribed into text using the
gpt-4o-mini-transcribemodel. - Knowledge Base Search (Retrieval): The query text is converted into a vector (embedding) and used to find the most relevant text fragments (
chunks) in the vector database (ChromaDB). - Prompt Assembly: The relevant chunks, along with the dialogue history and the user's original question, are assembled into a system prompt for the Large Language Model (LLM).
- Response Generation: The assembled prompt is sent to the OpenAI (GPT) model, which generates the final answer.
- Voice-to-Text (if needed): Voice messages are transcribed into text using the
- User Response: The generated answer is sent back to the user in Telegram.
- Logging: The entire conversation (question-answer) is saved to a text log file.
-
main.py: Entry Point. Initializes the bot, theaiogramdispatcher, databases, and starts polling. -
/handlers: Message Handlers.handlers.py: Receives messages fromaiogram(commands, text, voice). It performs initial validation (request limits), manages conversation state (FSM), and triggers the main processing logic.
-
/assistant: Assistant Core (LLM).assistant.py: Contains the core RAG logic. Theget_assistant_responsefunction orchestrates the vector search, chunk filtering, prompt creation, and the call to the OpenAI API.prompt.py: Stores system prompts and templates for the LLM, separating logic from model instructions.
-
/database: Data Management.vector_store.py: An abstraction layer for the ChromaDB vector database. It provides functions for searching (search_vector_store) and loading/creating the database.build_index.py: A script for pre-processing the knowledge base. It reads.txtfiles, splits them into chunks, and vectorizes them.user_database.py: Manages a user database with SQLite (for demonstration purposes) to track request counts and enforce daily limits.database.py: Basic functions for initializing the databases.
-
/transcriber: Speech Recognition.transcriber.py: Responsible for processing voice messages. It downloads the audio file, sends it to the OpenAI API with thegpt-4o-mini-transcribemodel, and returns the text.
-
/utils: Helper Utilities.utils.py: Contains thelog_conversationfunction for saving conversation histories to text files.
-
config.py: Configuration. A central place for all settings: tokens, API keys, file paths (LOGS_DIR,DB_PATH), and RAG/model parameters (CHUNK_SIZE,RELEVANCE_THRESHOLD).
handlers.pycatches the message.- The user's request limit is checked in
user_database. assistant.py:get_assistant_responseis called with the question text and history.assistant.pycallsdatabase/vector_store.pyto find relevant documents.assistant.pybuilds a prompt using a template fromprompt.pyand the retrieved documents.assistant.pymakes a request to the OpenAI API.handlers.pyreceives the response and sends it to the user.utils.py:log_conversationsaves the dialogue.- The conversation history is updated in the FSM.
handlers.pycatches the voice message.transcriber.py:process_voice_messageis called.transcriber.pydownloads the file, sends it to the OpenAI API with thegpt-4o-mini-transcribemodel, and receives the text.- From here, the process is identical to the text message flow, starting from step 2.