Skip to content

iu2frl/Telegram_GeminiAI_RAG_Bot

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

35 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

AI Assistant Telegram Bot

This bot is an AI-powered assistant that uses Google's Gemini AI to process user queries and provide responses based on uploaded documents. It integrates with Telegram for user interaction.

Features

  • Responds to user queries about specific documents or topics.
  • Utilizes Google Gemini AI for natural language understanding and document-based querying.
  • Handles Telegram messages and commands.
  • Supports MarkdownV2 formatting for responses.
  • Supports dynamic loading of documents from a specified GitHub repository.
  • Supports rate limit handling for Gemini API models when using free-tier accounts (only if GOOGLE_API_MODEL is set to auto).

Warning

If the GOOGLE_API_MODEL environment variable is set to a specific model name (e.g., gemini-2.0-flash), the bot will not monitor rate limits automatically. This may lead to unexpected costs or rate limiting issues.

Warning

The list of supported free-tier Gemini models may change over time. Please refer to the official documentation for the most up-to-date information. If a model expires, it can no longer be used by the bot until the code in modules/state.py is updated.

Prerequisites

Python environment

  1. Python 3.8 or higher.
  2. Required Python packages (install using pip install -r requirements.txt)

Gemini API key

  1. Generate a Gemini API key from Google AI Studio

Telegram API key

  1. Use @BotFather to generate a bot
  2. Get the API key from the generate bot

Setup

  1. Clone the Repository
git clone <repository-url>
cd <repository-folder>
  1. Create a .env File

Create a .env file in the project root directory with the following variables:

TELEGRAM_API_KEY=your-telegram-bot-token
TELEGRAM_BOT_NAME=@your-bot-username
GOOGLE_API_KEY=your-google-api-key
GOOGLE_API_MODEL=gemini-2.0-flash
GOOGLE_API_MAX_ATTEMPTS=2
REPO_URL=https://github.com/octocat/hello-world
TELEGRAM_RESTART_DELAY_SECONDS=15

Please note: creating the .env file is optional, if the variables are set in the current environment, the bot will retrieve them from there (or from the Docker environment variables)

  1. Prepare the sources

The bot pulls documents from the repository defined by REPO_URL and stores them in ./sources. You can also place your documents directly inside the sources folder if you want to manage them locally.

Running the Bot

  1. Install Dependencies
pip install -r requirements.txt
  1. Start the Bot using:
python main.py

Usage

Commands

  • /start: Initiates interaction with the bot and provides an introductory message.

Messaging

  • Send a text message with your query to the bot, and it will respond based on the information in the uploaded documents.

Logs

The bot logs all activity to the console. Logs are categorized into:

  • INFO: General information about the bot's operation.
  • DEBUG: Detailed logs for troubleshooting.
  • WARNING: Potential issues that do not stop the bot.
  • ERROR: Issues that prevent successful operation.

You can adjust the log level in the configure_logging() function.

Troubleshooting

  • Missing API keys: Ensure the .env file is correctly configured.
  • Google Gemini AI errors: Check your API key and ensure the uploaded files meet the requirements.
  • Telegram bot issues: Verify the bot token and bot username.

For detailed error messages, check the logs.

Usage with Docker

  1. Clone the repo
  2. Build the container with docker build -t notebook-lm-bot:latest .
  3. Create the docker-compose.yml file containing the text below
  4. Start the container with docker compose up
services:
    bot:
        container_name: notebook-lm-bot
        environment:
          - TELEGRAM_API_KEY=your-telegram-bot-token
          - TELEGRAM_BOT_NAME=@your-bot-username
          - GOOGLE_API_KEY=your-google-api-key
          - GOOGLE_API_MODEL=gemini-2.0-flash
          - GOOGLE_API_MAX_ATTEMPTS=2
          - REPO_URL=https://github.com/octocat/hello-world
          - TELEGRAM_RESTART_DELAY_SECONDS=15
        restart: unless-stopped
        image: notebook-lm-bot:latest
        deploy:
          resources:
            limits:
              cpus: '1'
              memory: 256M

About

A small Telegram bot powered by Gemini AI to retrieve informations from a custom set of documents

Topics

Resources

License

Stars

4 stars

Watchers

1 watching

Forks

Packages

 
 
 

Contributors