Skip to content

Latest commit

ย 

History

31 Commits

Folders and files

NameName
Last commit message
Last commit date
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 

Repository files navigation

TeleAIAgent - Intelligent Telegram Chat Bot (AsyncIO + Qdrant RAG + AI Image Analysis)

TeleAIAgent is an extensible, AI-powered Telegram bot with concurrent processing capabilities for text, file, and multimedia processing. Built with AsyncIO for high-performance concurrent request handling, the bot leverages multiple AI backends (Perplexity AI, Ollama) and provides advanced semantic search through Qdrant vector database with SentenceTransformers embeddings for enhanced context-aware conversations. NEW: Includes dedicated Image Analysis Microservice with AI-powered visual analysis and automated semantic storage.

๐Ÿš€ AsyncIO Implementation - Concurrent Processing

โšก NEW: The bot now supports multiple simultaneous users without blocking!

  • Concurrent AI requests - multiple users can ask questions at the same time
  • Non-blocking file processing - upload files while AI processes other requests
  • Modern aiogram 3.x architecture - event-driven message handling
  • Production-ready scalability - handles group chats with multiple active users

๐Ÿ“‹ AsyncIO Documentation - Complete technical details and migration guide

๐ŸŽฏ Latest Update: Qdrant Vector Database Migration

๐Ÿ”ฅ MAJOR UPGRADE: Successfully migrated from ChromaDB to Qdrant for enhanced semantic search!

โœจ New Features (September 2025)

  • ๐Ÿš€ Qdrant v1.11.0: High-performance vector database with 384D semantic embeddings
  • ๐Ÿง  SentenceTransformers: Advanced semantic understanding with all-MiniLM-L6-v2 model
  • ๐Ÿ’ป CPU-Optimized: PyTorch CPU-only installation (no GPU required)
  • โšก Semantic Search: Find relevant context by meaning, not just keywords
  • ๐ŸŽฏ Similarity Scoring: Configurable relevance threshold (0.3 default)
  • ๐Ÿ”„ Backward Compatible: All existing functionality preserved
  • ๐Ÿ“Š Better Performance: Improved vector similarity search with optimized embeddings

๐Ÿ–ผ๏ธ NEW: AI Image Analysis Microservice

  • ๐Ÿ—๏ธ Microservice Architecture: Dedicated vision service on port 7777
  • ๐Ÿค– Vision AI Integration: Ollama-powered image analysis with specialized prompts
  • ๐Ÿ“ Description Generation: Comprehensive 3-sentence German descriptions of images
  • ๐Ÿ“ Smart Organization: Year/month/day directory structure for images
  • ๐Ÿ” Semantic Image Search: Find similar images using vector similarity
  • ๐Ÿ’พ Metadata Preservation: Full Telegram context stored with embeddings
  • ๐Ÿณ Containerized: FastAPI service with health monitoring and error recovery

๐Ÿ”ง Technical Improvements

  • Vector Dimensions: 384D embeddings for efficient CPU processing
  • Memory Efficiency: Resource-optimized for containers without GPU
  • Service Architecture: Docker containerized Qdrant service (ports 6333/6334)
  • Async Integration: Non-blocking semantic search with AsyncIO patterns
  • Data Persistence: Vector collections survive container restarts

๐Ÿ“ˆ Migration Benefits

  • Better Context Retrieval: Semantic similarity instead of keyword matching
  • Improved AI Responses: More relevant context leads to better answers
  • Resource Efficient: CPU-only setup reduces hardware requirements
  • Production Ready: Battle-tested Qdrant database with proven scalability
  • Future-Proof: Modern vector database architecture for AI applications

๐Ÿ—๏ธ Project Architecture

๐Ÿ“ฆ TeleAIAgent Project Structure
โ”œโ”€ ๐Ÿณ Docker Infrastructure
โ”‚  โ”œโ”€ docker-compose.yml          # Multi-container orchestration (4 services)
โ”‚  โ”œโ”€ Dockerfile-teleaiagent      # Bot container image
โ”‚  โ”œโ”€ Dockerfile-vision           # Image analysis microservice container
โ”‚  โ””โ”€ .env                        # Environment variables
โ”‚
โ”œโ”€ ๐Ÿš€ Core Application (teleaiagent/) - **AsyncIO Architecture**
โ”‚  โ”œโ”€ main.py                     # AsyncIO bot with aiogram 3.x & concurrent handlers
โ”‚  โ”œโ”€ config.py                   # Central configuration
โ”‚  โ”œโ”€ requirements.txt            # AsyncIO dependencies (aiogram, aiohttp, aiofiles)
โ”‚  โ”œโ”€ test_qdrant.py              # Qdrant semantic search test
โ”‚  โ”œโ”€ test_async.py               # AsyncIO functionality & concurrent testing
โ”‚  โ”‚
โ”‚  โ”œโ”€ ๐Ÿ”ง handlers/ (AsyncIO)      # Concurrent message processing
โ”‚  โ”‚  โ”œโ”€ text_handler.py          # Async text & AI interactions
โ”‚  โ”‚  โ””โ”€ file_handler.py          # Async file downloads + tagger integration
โ”‚  โ”‚
โ”‚  โ””โ”€ ๐Ÿ› ๏ธ utils/ (AsyncIO)         # Async core services
โ”‚     โ”œโ”€ ai_client.py             # Async AI backend manager (aiohttp)
โ”‚     โ”œโ”€ context_manager.py       # Chat context & Qdrant integration
โ”‚     โ”œโ”€ text_processor.py        # Markdown/HTML conversion
โ”‚     โ”œโ”€ vision_client.py         # HTTP client for vision microservice
โ”‚     โ””โ”€ monitoring.py            # Async system monitoring
โ”‚
โ”œโ”€ ๐Ÿ–ผ๏ธ **NEW: Vision Microservice (vision/)** - **AI Image Analysis**
โ”‚  โ”œโ”€ main.py                     # FastAPI service with lifespan management
โ”‚  โ”œโ”€ config.py                   # Vision configuration (port 7777)
โ”‚  โ”œโ”€ requirements.txt            # FastAPI, vision dependencies
โ”‚  โ”œโ”€ README.md                   # Vision documentation
โ”‚  โ”‚
โ”‚  โ”œโ”€ ๐Ÿ–ผ๏ธ handlers/               # Image processing pipeline
โ”‚  โ”‚  โ””โ”€ image_handler.py         # AI-powered image analysis & description generation
โ”‚  โ”‚
โ”‚  โ””โ”€ ๐Ÿ› ๏ธ utils/                  # Vision core services
โ”‚     โ”œโ”€ ollama_client.py         # Vision AI client for image analysis
โ”‚     โ”œโ”€ qdrant_client.py         # Vector storage for image embeddings
โ”‚     โ””โ”€ file_manager.py          # Organized file storage (year/month/day)
โ”‚
โ”œโ”€ ๐Ÿ’พ Persistent Data (volumes/)
โ”‚  โ”œโ”€ qdrant/                     # Vector database for RAG + image embeddings
โ”‚  โ”œโ”€ teleaiagent/
โ”‚  โ”‚  โ”œโ”€ context/                 # Chat history files
โ”‚  โ”‚  โ”œโ”€ images/                  # **Shared**: Downloaded + organized images
โ”‚  โ”‚  โ”œโ”€ documents/               # Documents & PDFs
โ”‚  โ”‚  โ”œโ”€ voice/                   # Voice messages
โ”‚  โ”‚  โ”œโ”€ videos/                  # Video files
โ”‚  โ”‚  โ”œโ”€ audio/                   # Audio files
โ”‚  โ”‚  โ”œโ”€ logs/                    # TeleAIAgent logs
โ”‚  โ”‚  โ””โ”€ cache/                   # Model cache
โ”‚  โ”œโ”€ vision/
โ”‚  โ”‚  โ”œโ”€ logs/                    # Vision service logs
โ”‚  โ”‚  โ””โ”€ cache/                   # Vision model cache
โ”‚  โ””โ”€ ollama/                     # Local LLM + vision models
โ”‚
โ”œโ”€ ๐Ÿงช Testing & Integration
โ”‚  โ””โ”€ test_vision_integration.py  # End-to-end vision integration tests
โ”‚
โ””โ”€ ๐Ÿ“‹ Documentation
   โ”œโ”€ README.md                   # This documentation (AsyncIO + Qdrant + Vision)
   โ””โ”€ doc/
      โ”œโ”€ ASYNCIO_README.md        # AsyncIO implementation & concurrent processing
      โ”œโ”€ CHROMADB_INTEGRATION.md  # Migration guide (ChromaDB โ†’ Qdrant)
      โ”œโ”€ QDRANT_INTEGRATION.md    # Qdrant configuration and usage
      โ””โ”€ OLLAMA_BACKEND_SETUP.md  # Local AI backend configuration

๐Ÿ”„ AsyncIO Data Flow - Concurrent Processing + AI Image Analysis

โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”    โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”    โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
โ”‚   Multiple      โ”‚    โ”‚   TeleAIAgent   โ”‚    โ”‚   Async AI      โ”‚
โ”‚   Telegram      โ”‚โ”€โ”€โ”€โ”€โ”‚   (AsyncIO)     โ”‚โ”€โ”€โ”€โ”€โ”‚   Client        โ”‚
โ”‚   Users/Groups  โ”‚    โ”‚   aiogram 3.x   โ”‚    โ”‚   aiohttp       โ”‚
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜    โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜    โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
         โ”‚                       โ”‚                       โ”‚
   ๐Ÿ”€ Multiple              ๐Ÿ”€ Concurrent         ๐Ÿ”€ Parallel
      Messages                  Handlers               API Calls
         โ”‚                       โ”‚                       โ”‚
         โ–ผ                       โ–ผ                       โ–ผ
โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”    โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”    โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
โ”‚  Async Text/    โ”‚    โ”‚  Async Context  โ”‚    โ”‚  AI Backends    โ”‚
โ”‚  File Handlers  โ”‚โ”€โ”€โ”€โ”€โ”‚  Manager        โ”‚    โ”‚ โ€ข Perplexity    โ”‚
โ”‚  (aiofiles)     โ”‚    โ”‚  + Qdrant RAG   โ”‚    โ”‚ โ€ข Ollama        โ”‚
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜    โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜    โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
         โ”‚                       โ”‚                       โ”‚
    ๐Ÿš€ Non-blocking      ๐Ÿง  Semantic Search     โšก Concurrent
      File Ops           (384D Embeddings)        Processing
         โ”‚                       โ”‚                       โ”‚
         โ–ผ                       โ–ผ                       โ–ผ
โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”    โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”    โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
โ”‚  **NEW**: Image โ”‚    โ”‚  Vision         โ”‚    โ”‚  Vision AI      โ”‚
โ”‚  โ†’ Vision       โ”‚โ”€โ”€โ”€โ”€โ”‚  Microservice   โ”‚โ”€โ”€โ”€โ”€โ”‚  Analysis       โ”‚
โ”‚  Microservice   โ”‚    โ”‚  Port 7777      โ”‚    โ”‚  (Ollama)       โ”‚
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜    โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜    โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
         โ”‚                       โ”‚                       โ”‚
    ๐Ÿ–ผ๏ธ AI Image          ๐Ÿ“ Description Gen    ๐Ÿค– Vision Models
      Analysis           (3-sentence German)    (llama3.2-vision)
         โ”‚                       โ”‚                       โ”‚
         โ–ผ                       โ–ผ                       โ–ผ
โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”    โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”    โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
โ”‚  Organized      โ”‚    โ”‚  Vector Storage โ”‚    โ”‚   Async System  โ”‚
โ”‚  File Storage   โ”‚โ”€โ”€โ”€โ”€โ”‚  (Qdrant DB)    โ”‚โ”€โ”€โ”€โ”€โ”‚   Monitoring    โ”‚
โ”‚  (year/m/day)   โ”‚    โ”‚  + Metadata     โ”‚    โ”‚   (asyncio)     โ”‚
โ”‚ โ€ข Smart Dirs    โ”‚    โ”‚ โ€ข Image Embedds โ”‚    โ”‚ โ€ข CPU/RAM       โ”‚
โ”‚ โ€ข Unique Names  โ”‚    โ”‚ โ€ข Description   โ”‚    โ”‚ โ€ข 4 Services    โ”‚
โ”‚ โ€ข Chat Context  โ”‚    โ”‚ โ€ข Similarity    โ”‚    โ”‚ โ€ข Health Checks โ”‚
โ”‚ โ€ข Telegram Meta โ”‚    โ”‚ โ€ข Search Ready  โ”‚    โ”‚ โ€ข Performance   โ”‚
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜    โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜    โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜

๐ŸŽฏ Enhanced Benefits: 
   ๐Ÿ“ท Images โ†’ AI Analysis โ†’ Description Generation โ†’ Semantic Search โ†’ Organized Storage!
   ๐Ÿš€ Multiple users โ†’ Concurrent processing โ†’ No waiting queues โ†’ AI-powered insights!

โš™๏ธ Core Features

โšก AsyncIO Concurrent Processing

  • Multiple Simultaneous Users: No more waiting in queue!
  • Concurrent AI Requests: Multiple questions processed simultaneously
  • Non-blocking File Operations: Upload files while AI processes other requests
  • aiogram 3.x Architecture: Modern, event-driven message handling
  • Production Scalability: Handles high-traffic group chats efficiently

๐Ÿค– AI Integration

  • Multi-Engine Support: Perplexity AI & Ollama local models
  • Context-Aware Responses: Chat history is considered for better answers
  • Semantic Search: Qdrant-powered vector similarity retrieval
  • Automatic Model Management: Ollama models are downloaded on-demand
  • Async API Calls: Non-blocking AI requests with aiohttp

๐Ÿ’ฌ Enhanced Chat Features

  • Concurrent Group Support: Multiple users in groups processed simultaneously
  • Fast Private Chats: Direct 1:1 communication without blocking
  • Multi-language: German preferred, multi-language support
  • Markdown Support: Rich-text formatting in responses
  • Intelligent Message Chunking: Automatic splitting of long messages
  • Command System: /start, /help, /status commands

๐Ÿ“ Async File Processing + AI Image Analysis

  • Non-blocking Image Processing: Automatic download and storage (aiofiles)
  • ๐Ÿ†• AI-Powered Image Analysis: Automatic description generation via dedicated microservice
  • ๐Ÿ†• Smart Image Organization: Year/month/day directory structure
  • ๐Ÿ†• Vision AI Integration: Ollama-based image understanding and description generation
  • ๐Ÿ†• Metadata Preservation: Full Telegram context stored with images
  • Concurrent Document Handling: PDF, Word, Excel, etc. (multiple uploads simultaneously)
  • Parallel Audio/Video Processing: Multimedia files processed without blocking
  • Async Voice Messages: OGG format support with stream processing
  • Upload While Processing: Users can send multiple files while others process

๐Ÿ” Advanced Semantic Search & RAG + Image Search

  • Qdrant Vector Database: High-performance vector similarity search
  • SentenceTransformers: 384-dimensional semantic embeddings (all-MiniLM-L6-v2)
  • CPU-Optimized: PyTorch CPU-only for efficient resource usage
  • RAG (Retrieval-Augmented Generation): Context-aware AI responses
  • Semantic Context Retrieval: Find relevant chat history by meaning, not keywords
  • ๐Ÿ†• Image Semantic Search: Find similar images using AI-generated description embeddings
  • ๐Ÿ†• Visual Content Discovery: Search images by description, objects, mood, setting
  • ๐Ÿ†• Cross-Modal Search: Text queries return relevant images and conversations
  • Persistent Storage: All data survives container restarts
  • Similarity Threshold Filtering: Configurable relevance scoring (default: 0.3)

๐Ÿ“Š AsyncIO Monitoring & Administration

  • Real-time System Statistics: CPU, RAM, concurrent task monitoring
  • Concurrent Chat Analytics: Message counts, parallel processing stats
  • Async Qdrant Health: Non-blocking connection status and vector collection stats
  • Semantic Search Metrics: Embedding performance and similarity scores
  • Performance Monitoring: AsyncIO task tracking, concurrent request metrics
  • Comprehensive Logging: Debug and error logs with async rotation
  • Live Statistics: /status command shows concurrent processing and vector DB information

๐Ÿš€ AsyncIO Installation & Setup

Prerequisites (AsyncIO Optimized)

  • Docker Engine 20.10+
  • Modern Docker Compose Plugin (uses docker compose not docker-compose)
  • Git
  • 4GB+ RAM recommended (handles concurrent processing efficiently)
  • CPU with multiple cores (better for async operations)
  • Python 3.11+ with venv (for local development and testing)

1. Clone Repository

git clone https://github.com/spommerening/TeleAIAgent.git
cd TeleAIAgent

2. Setup Development Environment

# Create Python virtual environment for local development
python3 -m venv ./venv
source ./venv/bin/activate

# Install development dependencies (for running tests)
pip install -r teleaiagent/requirements.txt
pip install -r vision/requirements.txt

# Create/edit .env file
cp .env.example .env
nano .env

# Required API Keys:
TG_BOT_TOKEN="your_telegram_bot_token"
PERPLEXITY_API_KEY="your_perplexity_api_key"
AI_BACKEND="perplexity"  # or "ollama"

๐Ÿ“‹ Important: Always activate the virtual environment with source ./venv/bin/activate before running Python scripts from the host system. The ./venv directory is not committed to the repository.

3. Start AsyncIO Services + Vision Microservice

# Start all services (AsyncIO Bot + Vision + Qdrant + Ollama)
docker compose up -d

# Start with live logs (watch concurrent processing + AI image analysis!)
docker compose up

# Quick test concurrent processing:
# Send multiple messages to your bot simultaneously - they'll all process at once!
# Send images to see AI description generation in action!

# Check vision service health:
curl http://localhost:7777/health

# โš ๏ธ IMPORTANT: CPU Performance Notes
# First AI operations (Ollama model downloads, image analysis) can take 5-15+ minutes
# Be patient during initial startup - subsequent operations will be faster
# Use extended timeouts (600s+) for production deployments

๐Ÿณ AsyncIO Docker Management

Container Operations (AsyncIO + Vision Optimized)

# Check status (AsyncIO bot + vision microservice performance)
docker compose ps

# View concurrent processing + AI image analysis logs
docker compose logs -f teleaiagent  # Watch AsyncIO in action!
docker compose logs -f vision       # AI image analysis and description generation
docker compose logs -f qdrant       # Vector database operations (text + images)
docker compose logs -f ollama       # Parallel AI model requests (text + vision)

# Restart services
docker compose restart teleaiagent
docker compose restart vision
docker compose restart ollama

# Stop specific service
docker compose stop teleaiagent
docker compose stop vision

# Stop all services
docker compose down

# Stop with volume removal (WARNING: Data loss!)
docker compose down -v

Image Management

# Build new version
docker compose build --no-cache teleaiagent

# Update images
docker compose pull

# Clean unused images
docker image prune

# Rebuild & restart
docker compose down && docker compose build && docker compose up -d

AsyncIO + Vision Debugging & Maintenance

# Enter containers (check AsyncIO processes + vision service)
docker compose exec teleaiagent bash
docker compose exec vision bash
docker compose exec qdrant bash
docker compose exec ollama bash

# Check container resources (AsyncIO + AI image analysis efficiency)
docker stats  # Watch CPU usage during concurrent processing + image analysis

# Check volume contents
docker compose exec teleaiagent ls -la /app/context/
docker compose exec teleaiagent ls -la /app/images/  # Organized by year/month/day
docker compose exec vision ls -la /app/volume_images/  # Shared image storage
docker compose exec qdrant ls -la /qdrant/storage/
docker compose exec ollama ls -la /root/.ollama/

# Manage Ollama models (text + vision)
docker compose exec ollama ollama list
docker compose exec ollama ollama pull llama3.2
docker compose exec ollama ollama pull llama3.2-vision:11b  # Vision model for image analysis

# Test vision service
curl -X GET http://localhost:7777/health
curl -X GET http://localhost:7777/stats

๐Ÿ“ก AsyncIO AI Backend Configuration

Supported AI Providers (Concurrent Processing)

  • Perplexity AI: Main engine with integrated web search (async aiohttp requests)
  • Ollama: Local LLM models (llama3.2, gemma, phi3, etc.) with concurrent model access

Backend Selection

Set in .env file:

# Use Perplexity AI (cloud-based)
AI_BACKEND=perplexity
PERPLEXITY_API_KEY=your_api_key

# Or use Ollama (local models)
AI_BACKEND=ollama
# No API key required

Model Configuration

In teleaiagent/config.py:

# Perplexity settings
PERPLEXITY_MODEL = "sonar"
PERPLEXITY_TEMPERATURE = 0.7

# Ollama settings  
OLLAMA_MODEL = "gemma3n:e2b"  # or llama3.2, phi3, etc.
OLLAMA_TEMPERATURE = 0.7

In vision/config.py:

# Vision service settings
VISION_PORT = 7777
IMAGES_VOLUME_DIR = "/app/volume_images"

# Vision AI settings
OLLAMA_MODEL = "llama3.2-vision:11b"  # Vision-capable model
OLLAMA_BASE_URL = "http://ollama:11434"
DESCRIPTION_PROMPT = "Describe this image in exactly 3 short sentences..."

# Qdrant settings for image storage
QDRANT_HOST = "qdrant"
QDRANT_PORT = 6333
IMAGE_COLLECTION = "image_descriptions"

๐Ÿ“‹ Bot Commands

/start, /help     - Bot information and help
/stats           - System and chat statistics (includes vision service status)
/reconnect       - Rebuild Qdrant connection
@botname <query> - Mention bot in groups

๐Ÿ–ผ๏ธ Vision Microservice API

The Vision microservice provides a REST API for AI-powered image analysis:

API Endpoints

GET /health

Check service health status

curl http://localhost:7777/health

GET /stats

Get processing statistics and service information

curl http://localhost:7777/stats

POST /analyze-image

Process an image with AI analysis and description generation

curl -X POST \
  -F "image=@/path/to/image.jpg" \
  -F "chat_id=-1001234567890" \
  -F "message_id=123" \
  -F "file_id=ABC123xyz" \
  http://localhost:7777/analyze-image

Image Processing Workflow

๐Ÿ“ท Image Upload โ†’ ๐Ÿค– Vision AI Analysis โ†’ ๐Ÿ“ Description Generation โ†’ 
๐Ÿ“ File Organization โ†’ ๐Ÿ’พ Vector Storage โ†’ ๐Ÿ” Searchable Content
  1. Image Reception: FastAPI receives image with Telegram metadata
  2. AI Analysis: Ollama vision model analyzes visual content
  3. Description Generation: Comprehensive 3-sentence German description generated
  4. File Storage: Image saved in organized year/month/day directory structure
  5. Vector Storage: Description and metadata stored in Qdrant for semantic search
  6. Completion: Returns success status with generated description and storage path

Supported Image Formats

  • JPEG/JPG
  • PNG
  • WebP
  • GIF

Example AI-Generated Descriptions

  • Nature Scene: 1. Mountain landscape with golden sunset over peaceful valley. 2. Serene natural beauty with warm lighting and scenic vista. 3. Outdoor wilderness setting with dramatic golden hour atmosphere.
  • Urban Photo: 1. Busy city street with modern architecture and heavy traffic. 2. Metropolitan environment with tall buildings and urban activity. 3. Contemporary cityscape showing bustling street life and commerce.
  • Portrait: 1. Friendly person with warm smile in casual indoor setting. 2. Close-up portrait showing natural facial expression and relaxed demeanor. 3. Human subject captured in comfortable, informal environment.

๐Ÿ”ง AsyncIO Development & Extension

Local Development Environment

# โš ๏ธ ALWAYS activate the shared virtual environment first
source ./venv/bin/activate  # Required for all Python operations from host

# Run AsyncIO bot locally (from host, requires Docker services running)
cd teleaiagent/
python main.py  # Starts AsyncIO event loop with concurrent processing

# Run vision service locally (alternative to Docker)
cd ../vision/
python main.py  # Starts FastAPI service on port 7777

# Note: Local development still requires Qdrant and Ollama containers
docker compose up qdrant ollama -d

Testing AsyncIO + Vision Integration

# โš ๏ธ CRITICAL: Always activate venv before running Python tests
source ./venv/bin/activate

# Test Qdrant connectivity and semantic search (async)
cd teleaiagent/
python test_qdrant.py

# Test concurrent processing
python test_async.py  # Validates AsyncIO performance and concurrent operations

# Test vision microservice integration (from root directory)
cd ../
python test_vision_integration.py  # End-to-end image processing test

# Test individual vision components
cd vision/
python -m pytest  # Run vision unit tests (if available)

๐Ÿ“Š Current Project Status (September 2025)

โœ… Completed Features

  • โœ… Full Qdrant Migration: ChromaDB โ†’ Qdrant v1.11.0 complete
  • โœ… Semantic Embeddings: SentenceTransformers integration working
  • โœ… CPU Optimization: PyTorch 2.4.0+cpu installed and tested
  • โœ… Docker Integration: Multi-container setup (teleaiagent, vision, qdrant, ollama)
  • โœ… AsyncIO Architecture: Concurrent processing with aiogram 3.x
  • โœ… Service Networking: Container communication configured
  • โœ… Vector Collections: 384D embedding storage operational
  • โœ… Similarity Search: Semantic context retrieval functional
  • โœ… ๐Ÿ†• Vision Microservice: AI-powered image analysis and description generation
  • โœ… ๐Ÿ†• Vision AI Integration: Ollama vision models (llama3.2-vision:11b)
  • โœ… ๐Ÿ†• Smart File Organization: Year/month/day directory structure
  • โœ… ๐Ÿ†• Image Semantic Search: Vector storage for analyzed images
  • โœ… ๐Ÿ†• Metadata Preservation: Full Telegram context with images
  • โœ… ๐Ÿ†• FastAPI Service: RESTful API on port 7777 with health monitoring
  • โœ… Backward Compatibility: All existing features preserved

๐Ÿ”ง System Health

# Current status (all services operational):
โœ… TeleAI Bot: Running with AsyncIO concurrent processing + vision integration
โœ… Vision Service: AI image analysis microservice (port 7777) - HEALTHY
โœ… Qdrant DB: Vector collections active (6333/6334 ports) - text + image embeddings 
โœ… Ollama: Local AI models ready (11434 port) - text + vision models
โœ… Semantic Search: 384D embeddings with 0.63+ similarity scores
โœ… Image Processing: Automated description generation with year/month/day organization
โœ… CPU Performance: PyTorch optimized for non-GPU environments
โœ… Data Persistence: All volumes mounted and persistent
โœ… Service Communication: Internal Docker networking functional

๐ŸŽฏ Performance Metrics

  • Embedding Model: all-MiniLM-L6-v2 (384 dimensions)
  • Similarity Threshold: 0.3 (configurable)
  • Vector Database: Qdrant collections with async operations (text + images)
  • CPU Utilization: Optimized PyTorch without CUDA dependencies
  • Memory Usage: ~2GB RAM for Qdrant, ~3GB for Vision service
  • Response Time: <500ms for semantic search, ~2-5s for image AI analysis
  • Concurrent Users: Multiple simultaneous requests supported
  • ๐Ÿ†• Image Processing: Comprehensive 3-sentence descriptions per image with metadata storage
  • ๐Ÿ†• File Organization: Automatic year/month/day directory structure
  • ๐Ÿ†• Vision Health: FastAPI service with comprehensive health monitoring

๐Ÿงช Validation Tests

# All tests passing:
โœ… docker compose up -d          # 4 services start successfully (teleaiagent, vision, qdrant, ollama)
โœ… python test_qdrant.py         # Semantic search working (0.63 similarity)
โœ… python test_async.py          # Concurrent processing validated
โœ… python test_vision_integration.py  # End-to-end image processing validated
โœ… curl http://localhost:7777/health  # Vision service healthy
โœ… Bot polling active            # Telegram integration operational
โœ… Vector collections created    # Qdrant database functional (text + images)
โœ… SentenceTransformer loaded    # CPU-optimized embeddings ready
โœ… Vision AI models loaded       # Ollama llama3.2-vision:11b for image analysis
โœ… Service networking functional # All container communication working

๐Ÿ”ฎ Ready for Production

The TeleAI system is now production-ready with:

  • Modern vector database architecture (Qdrant)
  • Semantic search capabilities with transformer embeddings
  • ๐Ÿ†• AI-powered image analysis and description generation microservice
  • ๐Ÿ†• Automated visual content organization and search
  • ๐Ÿ†• Vision AI integration with semantic storage
  • CPU-optimized performance (no GPU requirements)
  • Full AsyncIO concurrent processing
  • Comprehensive Docker containerization (4-service architecture)
  • Robust error handling and monitoring
  • Scalable microservice architecture

Adding New AsyncIO Features

  1. Extend Async Handlers: handlers/ for new message types (use async/await patterns)
  2. Add Async Utilities: utils/ for helper functions (aiohttp, aiofiles)
  3. Modify Configuration: config.py for settings
  4. Follow AsyncIO Patterns: Always use async def and await for I/O operations
  5. Concurrent Design: Design features to handle multiple simultaneous users

Key Configuration Options

All settings in src/config.py:

  • AI backend selection and API endpoints
  • Qdrant connection settings and similarity thresholds
  • SentenceTransformers model configuration
  • File storage directories
  • Bot personality and behavior
  • Semantic search parameters

๐Ÿ”’ Security & Privacy

  • API Keys: Secure management via environment variables
  • Container Isolation: Services run in isolated containers
  • Volume Protection: Persistent data outside containers
  • Log Rotation: Automatic log cleanup (10MB max, 3 files)
  • Non-root Execution: Bot runs as non-privileged user

๐Ÿ› AsyncIO Troubleshooting

Common AsyncIO Issues

Bot not responding or AsyncIO errors:

docker compose logs teleaiagent | grep ERROR
docker compose logs teleaiagent | grep "asyncio"  # Check AsyncIO specific errors
docker compose restart teleaiagent

Qdrant connection issues:

docker compose logs qdrant
# Check if port 6333 is accessible
curl http://localhost:6333/collections

Ollama model problems:

docker compose logs ollama
docker compose exec ollama ollama list
docker compose exec ollama ollama pull your-model
docker compose exec ollama ollama pull llama3.2-vision:11b  # Vision model for vision service

Vision service issues:

docker compose logs vision
curl http://localhost:7777/health  # Check service health
curl http://localhost:7777/stats   # Check processing statistics
docker compose restart vision

Storage space full:

# Clean logs
docker compose exec teleaiagent find /app/logs -name "*.log" -delete

# Clean Docker system
docker system prune -a

# Check volume usage
docker system df

AsyncIO Performance issues:

# Monitor concurrent processing resources
docker stats --no-stream

# Check AsyncIO performance with concurrent test
docker compose exec teleaiagent python test_async.py

# Check Qdrant semantic search performance
docker compose exec teleaiagent python test_qdrant.py

๐Ÿ“Š Configuration Reference

Environment Variables (.env)

# Required
TG_BOT_TOKEN=your_telegram_bot_token

# AI Backend (choose one)
AI_BACKEND=perplexity
PERPLEXITY_API_KEY=your_perplexity_key

# Optional
ANONYMIZED_TELEMETRY=TRUE
DEBUG=false

Volume Mappings

  • ./volumes/teleaiagent/context โ†’ /app/context (Chat histories)
  • ./volumes/teleaiagent/images โ†’ /app/images (Downloaded images)
  • ./volumes/teleaiagent/documents โ†’ /app/documents (Documents)
  • ./volumes/teleaiagent/voice โ†’ /app/voice (Voice messages)
  • ./volumes/teleaiagent/videos โ†’ /app/videos (Videos)
  • ./volumes/teleaiagent/audio โ†’ /app/audio (Audio files)
  • ./volumes/teleaiagent/logs โ†’ /app/logs (Application logs)
  • ./volumes/teleaiagent/cache โ†’ /root/.cache (Model cache)
  • ./volumes/qdrant โ†’ /qdrant/storage (Vector database)
  • ./volumes/ollama โ†’ /root/.ollama (Ollama models)

Network Configuration

  • Internal Network: mynetwork (bridge)
  • Qdrant Ports: 6333 (HTTP API), 6334 (gRPC) - exposed to host
  • Vision Port: 7777 (HTTP API) - exposed to host for image processing
  • Ollama Port: 11434 (internal only)

๐Ÿ“ž Support & Dependencies

Key AsyncIO + Vision Dependencies

  • aiogram 3.13.0: Modern async Telegram Bot API wrapper (replaces pyTelegramBotAPI)
  • aiohttp 3.10.10: Async HTTP client for API requests + vision communication
  • aiofiles ~23.2.1: Non-blocking file operations
  • FastAPI 0.104.1: Modern async web framework for vision microservice
  • Qdrant 1.11.0: High-performance vector database for semantic search (text + images)
  • SentenceTransformers 3.0.1: Semantic embeddings (all-MiniLM-L6-v2 model)
  • PyTorch 2.4.0+cpu: CPU-optimized machine learning framework
  • Ollama: Local LLM inference with vision models (llama3.2-vision:11b for image analysis)
  • Perplexity AI: Cloud AI service with async requests (optional)

AsyncIO + Vision System Requirements

  • CPU: 2+ cores recommended (AsyncIO + AI image processing utilizes multiple cores efficiently)
  • RAM: 6GB+ for optimal concurrent performance (8GB+ recommended with Ollama + Vision)
  • Storage: 15GB+ for data, logs, models, and organized image storage
  • Network: Stable internet connection for concurrent API requests

AsyncIO Architecture

  • Container Runtime: Docker with compose orchestration + AsyncIO event loop
  • Concurrent Processing: aiogram 3.x with async/await patterns throughout
  • Data Persistence: Named volumes for data safety with async file operations
  • Service Discovery: Internal DNS via Docker networks
  • Health Monitoring: AsyncIO-aware health checks and concurrent logging
  • Event-Driven Design: Non-blocking message handling with concurrent AI requests

๐ŸŽ‰ Major Updates Summary (September 2025)

This project has been successfully upgraded with major architectural improvements:

โœ… Completed Migration & New Features

  • ๐Ÿš€ Vector Database: ChromaDB โ†’ Qdrant v1.11.0
  • ๐Ÿง  Semantic Embeddings: Integrated SentenceTransformers with 384D vectors
  • ๐Ÿ’ป CPU Optimization: PyTorch 2.4.0+cpu (no GPU required)
  • ๐Ÿณ Docker Integration: Full containerized setup with service networking
  • โšก Performance: Improved semantic search with similarity scoring
  • ๐Ÿ”„ Compatibility: All existing features preserved and enhanced
  • ๐Ÿ†• Vision Microservice: AI-powered image analysis and description generation system
  • ๐Ÿ†• Vision AI: Ollama integration with vision-capable models (llama3.2-vision:11b)
  • ๐Ÿ†• Smart Organization: Year/month/day directory structure for images
  • ๐Ÿ†• Image Search: Semantic search for visual content using AI-generated descriptions

๐Ÿ“ˆ Key Benefits

  • Better Context Understanding: Semantic similarity vs keyword matching
  • AI-Powered Visual Analysis: Automated image description generation and organization
  • Cross-Modal Search: Find images using text descriptions and vice versa
  • Resource Efficient: CPU-only setup reduces hardware requirements
  • Production Ready: Scalable microservice architecture
  • Modern Stack: Latest AsyncIO patterns with concurrent processing

โšก Performance Considerations & CPU-Only Operation

๐Ÿ–ฅ๏ธ CPU-Only Architecture Benefits

  • No GPU Required: Runs on standard CPU-only hardware
  • Cost Effective: Reduced infrastructure requirements
  • Wide Compatibility: Works on any modern multi-core CPU system

โฑ๏ธ Performance Expectations (CPU-Only)

  • First Startup: 5-15+ minutes for initial model downloads and setup
  • Image Description Generation: 2-5 minutes per image (Ollama vision models on CPU)
  • Text Processing: Near real-time with SentenceTransformers
  • Vector Search: Sub-second response times after embedding generation
  • Subsequent Operations: Faster due to model caching

๐Ÿ› ๏ธ Optimization Strategies

  • Extended Timeouts: Configure 600+ second timeouts for AI operations
  • Model Caching: First model downloads are cached for future use
  • Concurrent Processing: AsyncIO handles multiple requests efficiently
  • Resource Limits: 2GB RAM for bot, 3GB for vision service
  • Background Processing: Long operations don't block user interactions

๐Ÿ“‹ Development Tips:

  • Use source ./venv/bin/activate before running Python scripts
  • Test with docker compose (modern syntax) not docker-compose
  • Be patient during first AI operations - they get faster!
  • Monitor logs with docker compose logs -f [service]

๐Ÿ“– Detailed Guides:

๐Ÿš€ Current Status: Production Ready with AI Image Processing

All services operational (4-container architecture), tests passing, and ready for deployment with comprehensive AI-powered image management!


Developed with โค๏ธ for intelligent Telegram automation and AI-powered content management

Latest Update: AI Image Analysis Microservice + Qdrant Vector Database Migration (September 2025)

License

MIT License - see LICENSE file for details.

About

๐Ÿค– TeleAIAgent - AsyncIO Telegram Bot with AI Image Tagging & Semantic Search - Concurrent processing, Qdrant vector DB, AI-powered image analysis, multi-backend support (Perplexity, Ollama), SentenceTransformers embeddings, Docker deployment. Scalable AI chat with visual content management. ๐Ÿš€โœจ

Resources

Stars

0 stars

Watchers

0 watching

Forks

Contributors

Languages