A multi-platform AI chatbot with advanced image generation, powered by OpenAI, Gemini, and Stability AI.
-
Multi-Platform Support
- LINE Messaging API integration
- Facebook Messenger integration
- Webhook handling for both platforms
-
AI Capabilities
- Text Chat (OpenAI GPT, Google Gemini)
- Image Generation (DALL-E, Stability AI)
- Voice Processing (OpenAI Whisper)
- Automatic provider selection
-
Enhanced Image Generation
- Smart prompt engineering
- Multiple art styles support
- Quality-focused generation
- Automatic style enhancement
-
Data Management
- BigQuery chat history
- Google Cloud Storage for images
- Persistent conversation tracking
- Automatic data cleanup
ai_bot_app/
βββ README.md # Main project documentation
βββ main.py # Flask application entry point
βββ handle_line.py # LINE messaging API handler
βββ handle_facebook.py # Facebook Messenger API handler
βββ module_enhanced_image.py # Enhanced image generation system
βββ module_prompt_builder.py # Prompt engineering framework
βββ module_openai.py # OpenAI API integration
βββ module_stability.py # Stability AI integration
βββ module_gemini.py # Google Gemini integration
βββ module_gcp_storage.py # Google Cloud Storage
βββ model_chat_log.py # Chat log management
βββ module_common.py # Common data and categories
βββ prompt_config.py # Prompt configuration
βββ prompt_templates.py # Prompt templates and examples
βββ requirements.txt # Python dependencies
βββ Dockerfile # Docker configuration
βββ docker-compose.yml # Docker Compose setup
βββ LICENSE # Project license
βββ docs/ # Additional documentation
βββ INDEX.md # Documentation navigation
βββ IMAGE_GENERATION_ENHANCEMENT.md # Image generation guide
βββ OPTIMIZATION_SUMMARY.md # Code optimization details
- Python 3.10 or higher
- Docker (optional)
- API Keys:
- OpenAI API key
- Stability AI API key
- LINE Messaging API token
- Facebook Messenger token
- Google Cloud (optional):
- Project set up
- BigQuery enabled
- Cloud Storage enabled
- Service account with proper permissions
# Clone repository
git clone <repository-url>
cd ai_bot_app
# Create virtual environment
python -m venv venv
# Activate virtual environment
# On Windows:
venv\Scripts\activate
# On Unix/MacOS:
source venv/bin/activate
# Install dependencies
pip install -r requirements.txt# On Windows PowerShell:
$env:OPENAI_TOKEN = "your_openai_key"
$env:STABILITY_KEY = "your_stability_key"
$env:LINE_API_TOKEN = "your_line_token"
$env:FACEBOOK_ACCESS_TOKEN = "your_facebook_token"
# On Unix/MacOS:
export OPENAI_TOKEN=your_openai_key
export STABILITY_KEY=your_stability_key
export LINE_API_TOKEN=your_line_token
export FACEBOOK_ACCESS_TOKEN=your_facebook_tokenpython main.pySend these commands to your LINE or Facebook bot:
genimg cat in garden
genanime cute girl with blue hair
helpCreate a .env file in the project root directory:
# Required API Keys
OPENAI_TOKEN=your_openai_key
STABILITY_KEY=your_stability_key
LINE_API_TOKEN=your_line_token
FACEBOOK_ACCESS_TOKEN=your_facebook_token
# Optional Configuration
PORT=8080For Google Cloud integration (optional):
# Google Cloud Configuration
GOOGLE_CLOUD_PROJECT=your-project-id
GOOGLE_APPLICATION_CREDENTIALS=path/to/service-account-key.jsonCreate and activate Python virtual environment:
# Create environment
python -m venv venv
# Activate on Windows
venv\Scripts\activate
# Activate on Unix/MacOS
source venv/bin/activate
# Verify Python version
python --version # Should be 3.10 or higherInstall project dependencies:
# Install required packages
pip install -r requirements.txt
# Optional: Install development packages
pip install -r requirements-dev.txtUsing Docker Compose (recommended):
# Start all services
docker-compose up -d
# Check service status
docker-compose psManual Docker deployment:
# Build image
docker build -t ai-bot-app .
# Run container
docker run -p 8080:8080 --env-file .env ai-bot-appThe application will be available at http://localhost:8080
The application features a sophisticated prompt engineering system that automatically enhances user inputs for better image generation results.
genimg <prompt>- OpenAI DALL-E with enhanced promptsgenimgsd <prompt>- Stability AI with enhanced promptsgenauto <prompt>- Auto-select best provider
genanime <description>- Anime-style character generationgenphoto <subject>- Photorealistic image generationgenrandom [subject]- Random artwork with random style
helporimage help- Show detailed usage instructionsreset- Clear chat history
Use the | separator to specify components:
subject | style:anime | location:Tokyo | art:Studio Ghibli style
dragon | style:realistic | location:medieval castle | art:oil painting
robot | style:cartoon | location:space station | art:pixel art
genimg cat in garden
genanime cute girl with blue hair
genphoto beautiful sunset over mountains
genauto dragon | style:anime | art:watercolor
genrandom cat
cat | style:anime | location:Tokyo | art:Studio Ghibli style
Before Enhancement:
Input: "cat"
Output: "cat" (basic prompt)
After Enhancement:
Input: "cat"
Output: "cat, high quality, detailed, professional, award winning"
| Variable | Description | Required | Default |
|---|---|---|---|
OPENAI_TOKEN |
OpenAI API key | Yes | - |
STABILITY_KEY |
Stability AI API key | Yes | - |
LINE_API_TOKEN |
LINE Bot API token | Yes | - |
FACEBOOK_ACCESS_TOKEN |
Facebook Page access token | Yes | - |
PORT |
Application port | No | 8080 |
GOOGLE_CLOUD_PROJECT |
Google Cloud Project ID | For GCP | - |
GOOGLE_APPLICATION_CREDENTIALS |
GCP Service Account Key | For GCP | - |
- Visit OpenAI Platform
- Create an account and get your API key
- Set
OPENAI_TOKENenvironment variable
- Visit Stability AI
- Create an account and get your API key
- Set
STABILITY_KEYenvironment variable
- Visit LINE Developers
- Create a new provider and channel
- Get your Channel Access Token
- Set
LINE_API_TOKENenvironment variable - Configure webhook URL:
https://your-domain.com/openai_gpt_line
- Visit Facebook Developers
- Create a new app and page
- Get your Page Access Token
- Set
FACEBOOK_ACCESS_TOKENenvironment variable - Configure webhook URL:
https://your-domain.com/facebook_webhook
-
Visit Google Cloud Console
-
Create a new project or select existing one
-
Enable required APIs:
- Cloud Run API
- Cloud Storage API
- BigQuery API
-
Set up service account with appropriate permissions
-
Set
GOOGLE_CLOUD_PROJECTenvironment variable -
Configure
GOOGLE_APPLICATION_CREDENTIALSif using service account key
- Image Generation Enhancement - Advanced image generation features
- Code Optimization Summary - Performance improvements and optimizations
- Documentation Index - Complete documentation navigation
- New Users: Start here, then check Image Generation Guide
- Developers: Review Optimization Summary for code improvements
- Contributors: Check Documentation Index for all resources
- Enhanced Prompt Builder: Automatically enhances user prompts
- Multi-Provider Support: OpenAI DALL-E and Stability AI
- Intelligent Provider Selection: Chooses best provider based on prompt
- Quality Boosters: Automatic quality enhancement
- LINE Integration: Full LINE Bot API support
- Facebook Integration: Facebook Messenger API support
- Voice Processing: Audio transcription and response
- Chat History: Persistent conversation management
- Google Cloud Storage: Automatic image upload
- BigQuery: Chat log storage and analytics
- Google Gemini: Additional AI capabilities
module_enhanced_image.py # Main image generation orchestrator
module_prompt_builder.py # Prompt engineering framework
module_openai.py # OpenAI API integration
module_stability.py # Stability AI integration
module_gemini.py # Google Gemini integration
module_gcp_storage.py # Google Cloud Storage
model_chat_log.py # Chat log management
module_common.py # Shared data and categories
prompt_config.py # Configuration and validation
prompt_templates.py # Templates and examples
python main.py# Build and run with Docker Compose
docker-compose up -d
# Or build manually
docker build -t ai-bot-app .
docker run -p 8080:8080 ai-bot-appUpdate your environment variables for API keys and GCP project info, then deploy:
# Deploy to Google Cloud Run
gcloud run deploy ai-bot-app \
--allow-unauthenticated \
--region=asia-northeast1 \
--project=yahayuta \
--source .Note: After deployment, update your LINE and Facebook webhook URLs to point to your Cloud Run service URL:
- LINE Webhook:
https://ai-bot-app-xxxxx-xx.a.run.app/openai_gpt_line - Facebook Webhook:
https://ai-bot-app-xxxxx-xx.a.run.app/facebook_webhook
-
Google Cloud Run (Recommended)
- Serverless deployment
- Automatic scaling
- Built-in SSL certificates
- Easy environment variable management
-
Traditional Server
- Set up a reverse proxy (nginx/Apache)
- Configure SSL certificates
- Set up environment variables
- Deploy with Docker or directly with Python
-
Other Cloud Platforms
- AWS Elastic Beanstalk
- Azure App Service
- Heroku
- DigitalOcean App Platform
# Test prompt validation
python -c "import prompt_config; print(prompt_config.validate_prompt('test'))"
# Test image generation
python -c "import module_enhanced_image; print('Module loaded successfully')"- Test LINE webhook endpoint
- Test Facebook webhook endpoint
- Test image generation commands
- Test voice processing
- Check API keys are set correctly
- Verify internet connectivity
- Check provider-specific error messages
- Verify webhook URL is correct
- Check server is accessible from internet
- Verify SSL certificate (required for production)
- Ensure audio file format is supported
- Check OpenAI API quota
- Verify file permissions
# Enable debug logging
export FLASK_DEBUG=1
python main.py- Fork the repository
- Create a feature branch
- Install development dependencies
- Make your changes
- Add tests if applicable
- Submit a pull request
- Follow PEP 8 style guidelines
- Add type hints where appropriate
- Include docstrings for functions
- Write tests for new features
- Unit tests for all new functions
- Integration tests for API endpoints
- Test image generation with various prompts
- Verify error handling
This project is licensed under the MIT License - see the LICENSE file for details.
- Check the documentation
- Review troubleshooting section
- Open an issue on GitHub
When reporting issues, please include:
- Operating system and Python version
- Error messages and stack traces
- Steps to reproduce the issue
- Environment configuration (without sensitive data)
- Check existing issues first
- Provide detailed use case description
- Include mockups or examples if applicable
Version: 1.0.0
Last Updated: December 2024
Maintainer: AI Bot App Team