A chatbot based on Slack API and OpenAI API with streaming responses, conversation memory, and image capabilities
- π§΅ Thread-based Chat Sessions: Each Slack thread maintains its own conversation context
- β‘ Real-time Streaming: Live message updates as the AI generates responses
- π§ Conversation Memory: Bot remembers previous messages within each thread
- π Multi-user Support: Multiple users can participate in the same thread conversation
- π οΈ Session Management: Automatic session creation, tracking, and cleanup
- π Admin Commands: Session info, clearing, and statistics
- π¨ Error Handling: Robust error handling and fallback mechanisms
- π¬ Deep Research Mode: Advanced AI analysis using OpenAI's deep research model
- πΌοΈ Image Analysis: Upload and analyze images using GPT-4 Vision
- π¨ Image Generation: Generate images using DALL-E 3
- New Conversation: Mention the bot or send a DM to start a new chat session
- Continue Conversation: Reply in the same thread to maintain context
- Multiple Threads: Each thread is a separate conversation with its own memory
- Automatic Cleanup: Sessions expire after 1 hour of inactivity
- Install dependencies:
pip install -r requirements.txt - Configure environment variables in
.env(see.env.example) - Run the app:
python app.py
# Slack Bot Configuration
SLACK_BOT_TOKEN=xoxb-your-bot-token-here
SLACK_APP_TOKEN=xapp-your-app-token-here
# OpenAI Configuration
OPENAI_API_KEY=sk-your-openai-api-key-here@ChatHakase Hello, can you help me with Python?
> Of course! I'd be happy to help you with Python. What would you like to learn?
What are decorators?
> [Remembers you're asking about Python] Decorators in Python are...
@ChatHakase /deep What are the latest developments in quantum computing and their potential impact on cryptography?
> π¬ Deep Research Results
>
> [Comprehensive analysis with detailed research findings...]
[Upload an image with text]
@ChatHakase What do you see in this image?
> πΌοΈ [Analyzes the uploaded image and provides detailed description]
@ChatHakase /generate a sunset over mountains with a lake in the foreground
> π¨ Generating image...
> [Posts generated image]
@ChatHakase /image a cute robot assistant
> π¨ [Generates and posts the requested image]
session info- Show current session detailsclear session- Reset conversation history for this threadall sessions- Show all active sessions (admin)help- Show available commands
- Session Management: Run
python test_sessions.pyto test session functionality - OpenAI Streaming: Run
python test_streaming.pyto test OpenAI streaming without Slack - Deep Research: Run
python test_deep_research.pyto test deep research functionality - Image Features: Run
python test_image_features.pyto test image processing and generation - Improved Image Command: Run
python test_improved_image_command.pyto test the enhanced /image command
app.py: Main Slack application with event handlers and image supportassistant.py: OpenAI integration with streaming responses, vision, and image generationsession_manager.py: Chat session management and conversation memorytest_sessions.py: Test suite for session functionalitytest_image_features.py: Test suite for image functionality
- The bot posts an initial "thinking" message
- Updates the message every 500ms with partial responses
- Shows a typing indicator (β³) during generation
- Finalizes with the complete response
- Stores the conversation in session memory
- Vision Analysis: Automatically detects uploaded images and analyzes them using GPT-4 Vision
- Image Resizing: Automatically resizes large images to meet OpenAI API requirements
- Base64 Encoding: Converts images to base64 format for API transmission
- Error Handling: Robust error handling for image processing failures
- DALL-E 3: Uses OpenAI's latest image generation model
- Quality Options: Supports both standard and HD quality generation
- Size Options: Supports multiple image sizes (1024x1024, 1792x1024, 1024x1792)
- Prompt Enhancement: DALL-E 3 automatically enhances prompts for better results
- Sessions are identified by
channel_id + thread_timestamp - Each session maintains conversation history including image interactions
- OpenAI receives full context for better responses
- Automatic cleanup prevents memory leaks
/image <description>- Smart image command (generates or analyzes based on context)/image generate <description>- Explicitly generate an image using DALL-E 3/image create <description>- Alternative explicit generation command/generate <description>- Legacy generation command- Examples:
/image a futuristic cityscape at night/image generate a cute cat wearing a space helmet
- Upload any image file to Slack and mention the bot
/image <question>- Analyze recently uploaded images (auto-detected)/image analyze <question>- Explicitly analyze uploaded images/image describe <question>- Alternative explicit analysis command- Examples:
- Upload image β
/image what do you see in this screenshot? - Upload diagram β
/image analyze explain this flowchart
- Upload image β
- Chat Sessions Guide: Detailed documentation about session functionality
- Streaming Improvements: Technical details about streaming implementation
ChatHakase/
βββ app.py # Main Slack application with image support
βββ assistant.py # OpenAI integration with vision and generation
βββ session_manager.py # Session management
βββ test_sessions.py # Session tests
βββ test_streaming.py # Streaming tests
βββ test_image_features.py # Image functionality tests
βββ requirements.txt # Dependencies (updated with image libraries)
βββ .env.example # Environment template
βββ README.md # This file
βββ CHAT_SESSIONS.md # Session documentation
βββ STREAMING_IMPROVEMENTS.md
- Thread Detection: Automatically detects if message is in existing thread
- Context Management: Maintains up to 20 recent messages per session
- Token Management: Limits message history to stay within OpenAI token limits
- Error Recovery: Graceful handling of API failures and rate limits
- Memory Efficiency: Automatic cleanup of expired sessions
- Image Processing: Automatic image detection, resizing, and encoding
- Vision Integration: Seamless integration with GPT-4 Vision for image analysis
- Image Generation: DALL-E 3 integration with quality and size options
- Persistent storage for conversation history
- Conversation summarization for long threads
- User-specific session limits
- Analytics and usage tracking
- Integration with other AI models
- Image editing and manipulation features
- Batch image processing
- Custom image generation styles