Skip to main content

Quick Start Guide

This guide will help you set up and run your MilesONerd AI Telegram bot in just a few minutes. By the end, you’ll have a fully functional AI-powered bot responding to messages on Telegram.
1
Prerequisites
2
Before you begin, ensure you have the following:
3
  • Python 3.8 or higher installed on your system
  • pip (Python package manager)
  • A Telegram account
  • Git (for cloning the repository)
  • 4
    GPU Recommended: While the bot can run on CPU, using a GPU with at least 40GB VRAM is highly recommended for optimal performance with the Llama 3.1-Nemotron 70B model.
    5
    Get Your Telegram Bot Token
    6
    You need a bot token from Telegram to connect your bot:
    7
  • Open Telegram and search for @BotFather
  • Start a chat and send the command /newbot
  • Follow the prompts to name your bot:
    • Name: MilesONerd AI (or your preferred name)
    • Username: Must end in “bot” (e.g., milesonerd_ai_bot)
  • Copy the bot token provided by BotFather - you’ll need this in the next steps
  • 8
    Save your bot token securely. You’ll use it in the configuration step. The token looks like: 1234567890:ABCdefGHIjklMNOpqrsTUVwxyz
    9
    Clone the Repository
    10
    Clone the MilesONerd AI bot repository to your local machine:
    11
    git clone https://github.com/MilesONerd/telegram-bot.git
    cd telegram-bot
    
    12
    Install Dependencies
    13
    Install all required Python packages using pip:
    14
    pip install -r requirements.txt
    
    15
    The installation includes:
    • python-telegram-bot (21.10) - Telegram Bot API wrapper
    • transformers (4.48.0) - Hugging Face model library
    • torch (2.5.1) - PyTorch for model inference
    • python-dotenv (1.0.1) - Environment variable management
    • Additional dependencies for model optimization
    16
    This may take several minutes as it installs PyTorch, Transformers, and other dependencies.
    17
    Configure Environment Variables
    18
    Set up your bot configuration:
    19
  • Create a .env file in the project root:
  • 20
    cp .env.example .env
    
    21
    If .env.example doesn’t exist, create .env manually:
    22
    touch .env
    
    23
  • Edit the .env file and add your Telegram bot token:
  • 24
    # Required: Your Telegram Bot Token from @BotFather
    TELEGRAM_BOT_TOKEN=your_bot_token_here
    
    # Optional: Default AI model to use
    DEFAULT_MODEL=llama
    
    # Optional: Enable continuous learning (future feature)
    ENABLE_CONTINUOUS_LEARNING=true
    
    # Optional: Google Search API key (future feature)
    # SERPAPI_API_KEY=your_serpapi_key_here
    
    25
    Security: Never commit your .env file to version control. The token grants full access to your bot.
    26
    Start the Bot
    27
    Launch your bot with a single command:
    28
    python bot.py
    
    29
    You should see output indicating the models are being initialized:
    30
    2026-03-09 10:30:45 - __main__ - INFO - Initializing AI models...
    2026-03-09 10:30:45 - ai_handler - INFO - Starting model initialization...
    2026-03-09 10:30:45 - ai_handler - INFO - CUDA available: True
    2026-03-09 10:30:45 - ai_handler - INFO - GPU Device: NVIDIA A100-SXM4-80GB
    2026-03-09 10:30:46 - ai_handler - INFO - Loading BART model: facebook/bart-large
    2026-03-09 10:30:52 - ai_handler - INFO - BART tokenizer loaded successfully
    2026-03-09 10:30:58 - ai_handler - INFO - BART model loaded successfully
    2026-03-09 10:30:58 - ai_handler - INFO - Loading Llama model: nvidia/Llama-3.1-Nemotron-70B-Instruct-HF
    2026-03-09 10:31:15 - ai_handler - INFO - Llama tokenizer loaded successfully
    2026-03-09 10:32:45 - ai_handler - INFO - Llama model loaded successfully
    2026-03-09 10:32:45 - ai_handler - INFO - All models initialized successfully
    2026-03-09 10:32:45 - __main__ - INFO - MilesONerd AI Bot is starting...
    
    31
    First Run: The first time you run the bot, it will download the AI models from Hugging Face (~150GB total). This can take 10-30 minutes depending on your internet connection. Subsequent runs will use cached models.
    32
    Test Your Bot
    33
    Now that your bot is running, let’s test it on Telegram:
    34
  • Open Telegram on your phone or desktop
  • Search for your bot using the username you created (e.g., @milesonerd_ai_bot)
  • Start a conversation by clicking “Start” or sending /start
  • 35
    You should receive a welcome message:
    36
    Hi @yourusername! I'm MilesONerd AI, your intelligent assistant.
    I can help you with various tasks using advanced AI models and internet search.
    Use /help to see available commands.
    
    37
    Try Basic Commands
    38
    Test the bot’s functionality with these commands:
    39
    /start
    /start
    
    /help
    /help
    
    /about
    /about
    
    40
    Expected responses:
    41
    /start
    Hi @yourusername! I'm MilesONerd AI, your intelligent assistant.
    I can help you with various tasks using advanced AI models and internet search.
    Use /help to see available commands.
    
    /help
    Available commands:
    /start - Start the bot
    /help - Show this help message
    /about - Learn more about MilesONerd AI
    
    Message handling:
    - Short questions: Quick responses using lightweight model
    - Long messages: Summarization and detailed response
    - Include 'summarize' or 'tldr' for text summarization
    - Chat-related queries: Optimized conversation handling
    - Regular messages: Comprehensive AI-powered responses
    
    You can send me any message, and I'll process it using the most appropriate AI model!
    
    /about
    MilesONerd AI is an intelligent assistant powered by advanced AI models and internet search capabilities.
    
    Features:
    - Advanced language understanding
    - Internet search integration
    - Continuous learning from interactions
    - Multiple AI models for different tasks
    
    Created with ❤️ using python-telegram-bot and Hugging Face models.
    
    42
    Test Message Processing
    43
    Try different types of messages to see how the bot responds:
    44
    1. Short Question (uses Llama 3.1-Nemotron)
    45
    What is Python?
    
    46
    2. Conversation Request (uses Llama 3.1-Nemotron)
    47
    Let's chat about artificial intelligence
    
    48
    3. Summarization Request (uses BART)
    49
    Please summarize this: [paste a long article or text]
    
    50
    or
    51
    tldr: [paste a long article or text]
    
    52
    4. Long Message (uses BART + Llama 3.1-Nemotron)
    53
    Send a message with more than 100 words. The bot will:
    54
  • Summarize it using BART
  • Generate a response using Llama based on the summary
  • Understanding the Bot’s Behavior

    The bot intelligently routes your messages to different AI models based on content analysis:

    Message Routing Logic

    # From bot.py - How the bot decides which model to use
    
    if len(user_message.split()) > 100:
        # Long messages: BART summarization + Llama response
        summary = await ai_handler.summarize_text(user_message)
        response = await ai_handler.generate_response(
            f"Based on this summary: {summary}\nGenerate a helpful response:",
            model_key='llama',
            max_length=200
        )
    
    elif 'summarize' in user_message.lower() or 'tldr' in user_message.lower():
        # Explicit summarization: BART only
        response = await ai_handler.summarize_text(user_message)
    
    elif 'chat' in user_message.lower() or 'conversation' in user_message.lower():
        # Conversation: Llama with extended length
        response = await ai_handler.generate_response(
            user_message,
            model_key='llama',
            max_length=200
        )
    
    elif len(user_message.split()) < 10:
        # Short queries: Llama with limited length
        response = await ai_handler.generate_response(
            user_message,
            model_key='llama',
            max_length=100
        )
    
    else:
        # Default: Llama with standard length
        response = await ai_handler.generate_response(
            user_message,
            model_key='llama',
            max_length=150
        )
    

    Model Selection Table

    Message TypeWord CountKeywordsAI Model UsedMax Length
    Long message> 100 words-BART → Llama200 tokens
    SummarizationAny”summarize”, “tldr”, “summary”BART130 tokens
    ConversationAny”chat”, “conversation”, “talk”Llama200 tokens
    Short query< 10 words-Llama100 tokens
    Default10-100 words-Llama150 tokens

    Troubleshooting

    Bot Token Error

    ERROR - No token found! Make sure to set TELEGRAM_BOT_TOKEN in .env file
    
    Solution: Verify your .env file exists and contains the correct token:
    cat .env
    
    Ensure the line reads:
    TELEGRAM_BOT_TOKEN=your_actual_token_here
    

    Model Initialization Failed

    ERROR - Failed to initialize AI models. Exiting...
    
    Possible causes:
    • Insufficient GPU/CPU memory
    • Network issues downloading models
    • Missing dependencies
    Solutions:
    1. Check GPU memory:
    nvidia-smi
    
    1. Verify PyTorch installation:
    python -c "import torch; print(torch.cuda.is_available())"
    
    1. Reinstall dependencies:
    pip install -r requirements.txt --upgrade
    

    Out of Memory Error

    RuntimeError: CUDA out of memory
    
    Solution: The Llama 3.1-Nemotron 70B model requires significant memory. Options:
    1. Use a smaller model (modify ai_handler.py:35)
    2. Enable CPU offloading (already configured with device_map='auto')
    3. Use quantization (future enhancement)

    Bot Not Responding

    Checklist:
    • Is bot.py running without errors?
    • Did you use the correct bot username in Telegram?
    • Is your internet connection stable?
    • Check logs for error messages

    Model Download Takes Too Long

    The models are large (~150GB combined). To monitor progress:
    watch -n 1 du -sh ~/.cache/huggingface/
    
    You can manually pre-download models using the huggingface-cli:
    huggingface-cli download nvidia/Llama-3.1-Nemotron-70B-Instruct-HF
    huggingface-cli download facebook/bart-large
    

    Configuration Options

    Environment Variables

    Customize your bot’s behavior by editing .env:
    .env
    # Required
    TELEGRAM_BOT_TOKEN=your_bot_token_here
    
    # Optional: Choose default model (llama or bart)
    DEFAULT_MODEL=llama
    
    # Optional: Enable/disable continuous learning
    ENABLE_CONTINUOUS_LEARNING=true
    
    # Optional: Future feature - internet search
    SERPAPI_API_KEY=your_api_key_here
    

    Model Parameters

    You can adjust generation parameters in ai_handler.py:
    # Default parameters in generate_response() at line 109
    temperature: float = 0.2  # Lower = more focused, Higher = more creative
    top_p: float = 0.4        # Nucleus sampling threshold
    max_length: int = 300     # Maximum response length in tokens
    max_attempts: int = 5     # Retry attempts for valid responses
    

    Next Steps

    Now that your bot is running, explore more:

    API Reference

    Learn about all available commands and functions

    Advanced Configuration

    Customize model parameters and behavior

    Deployment Guide

    Deploy your bot to production servers

    Model Details

    Contribute to the MilesONerd AI project

    Getting Help

    If you encounter issues:
    Logging: The bot outputs detailed logs to the console. Use these logs to diagnose issues:
    python bot.py 2>&1 | tee bot.log
    

    Congratulations! You now have a fully functional AI-powered Telegram bot. Start chatting and explore its capabilities!

    Build docs developers (and LLMs) love