A modern AI chat application built with Flask, Langchain, and Google's Gemini AI, featuring intelligent RAG (Retrieval-Augmented Generation) capabilities with multiple search provider options.
- Google Gemini AI Integration: Powered by Google's latest AI model
- Intelligent RAG System: Automatic decision-making for web search vs. direct answers
- Multiple Search Providers: DuckDuckGo, Google Custom Search, Web Scraping
- Manual Search Toggle: Force web search for every query
- Real-time Streaming: Live response generation with "Searching..." notifications
- File Analysis: Upload and analyze images, code files, and documents
- Message Quoting: Quote AI responses in new messages
- File Uploads: Support for images, code files, and documents
- Chat History: Persistent conversation storage
- Streaming Responses: Real-time message generation
- Search Modes: Auto and manual web search options
- Modern Design: Clean, responsive interface
- Dark/Light Themes: User preference with localStorage
- Real-time Features: Character count, auto-resize, streaming
- Mobile Responsive: Works on all devices
- Message Actions: Quote, copy, and file upload capabilities
- User Registration/Login: Secure authentication system
- Profile Management: Update username and email
- Session Management: Persistent login sessions
- Password Security: Hashed password storage
/calliope-ai
βββ app/
β βββ api/ # API endpoints (chat, auth)
β βββ core/ # Configuration & extensions
β βββ models/ # Database models (User, Chat, Message)
β βββ services/ # Business logic (ChatService, RAGService)
β βββ static/ # CSS, JS files
β βββ templates/ # HTML templates
βββ uploads/ # File upload directory
βββ .env # Environment configuration
βββ requirements.txt # Dependencies
βββ run.py # Application entry point
βββ setup.py # Automated setup script
βββ test_setup.py # Setup verification
βββ README.md # This file
- Python 3.8+
- PostgreSQL 12+
- Google API Key (for Gemini AI)
- Google Custom Search ID (optional, for enhanced search)
-
Clone the repository:
git clone https://github.com/mahanzavari/Calliope cd Calliope -
Run automated setup:
python setup.py
-
Configure PostgreSQL:
# Create database createdb calliope_ai # Update .env file with your database credentials
-
Update API keys in
.env:GOOGLE_API_KEY=your-google-api-key-here GOOGLE_CUSTOM_SEARCH_ID=your-google-custom-search-id-here DATABASE_URL=postgresql://username:password@localhost:5432/calliope_ai SEARCH_PROVIDER=duckduckgo
-
Initialize database:
python init_db.py
-
Run the application:
python run.py
-
Access the application: Open http://localhost:5000
-
Create virtual environment:
python -m venv venv source venv/bin/activate # Windows: venv\Scripts\activate
-
Install dependencies:
pip install --upgrade -r requirements.txt pip install --upgrade langchain langchain-core langchain-google-genai langchain-community sentence-transformers torch transformers
-
Install PostgreSQL:
- Windows: Download from PostgreSQL Downloads
- macOS:
brew install postgresql - Ubuntu/Debian:
sudo apt-get install postgresql postgresql-contrib
-
Create database:
createdb calliope_ai
-
Configure environment:
cp .env.example .env # Edit .env with your credentials -
Initialize database:
python init_db.py
-
Run the application:
python run.py
The application supports multiple search providers for web search functionality:
- Free: No API key required
- Privacy-focused: No tracking
- Rate limits: Moderate usage limits
- Configuration:
SEARCH_PROVIDER=duckduckgo
- Enhanced results: Better search quality
- API key required: Google API key + Custom Search ID
- Quota limits: 100 free queries/day
- Configuration:
SEARCH_PROVIDER=google_custom GOOGLE_API_KEY=your-key GOOGLE_CUSTOM_SEARCH_ID=your-search-id
- Fallback option: Simple HTML scraping
- No API key: Works without external services
- Limited results: Basic search functionality
- Configuration:
SEARCH_PROVIDER=web_scraping
- Automatic: Tries multiple providers
- Resilient: Falls back if one fails
- Best option: For production use
- Configuration:
SEARCH_PROVIDER=fallback
- Register/Login: Create an account or sign in
- Start Chatting: Begin conversations with the AI
- Toggle Search: Use the search button to enable/disable web search
- Upload Files: Drag & drop or click to upload files
- Quote Messages: Click "Quote" on any AI response
- Auto-Search Mode: AI decides when to search based on confidence
- Manual Search Mode: Force web search for every query
- File Analysis: Upload images, code, and documents for analysis
- Message Quoting: Reference previous responses in new messages
- Theme Toggle: Switch between dark and light themes
- Dashboard: View chat history and manage profile
The application uses an intelligent RAG (Retrieval-Augmented Generation) system:
- Query Analysis: AI analyzes if the query requires external information
- Confidence Check: Determines if direct answer or web search is needed
- Search & Retrieval: Performs web search using configured provider
- Document Reranking: Uses semantic similarity for better results
- Response Generation: Combines AI knowledge with search results
- Auto Mode: AI automatically decides when to search
- Manual Mode: Force search for every query
- Searching Indicator: Real-time notification during RAG operations
POST /auth/register- User registrationPOST /auth/login- User loginPOST /auth/logout- User logoutGET /auth/profile- Get user profilePUT /auth/profile- Update user profile
POST /api/chat- Send message (streaming response)POST /api/upload- Upload filesGET /api/chats- Get chat historyGET /api/chats/<id>/messages- Get chat messages
# Required
SECRET_KEY=your-secret-key-here
DATABASE_URL=postgresql://username:password@localhost:5432/calliope_ai
GOOGLE_API_KEY=your-google-api-key-here
# Optional
GOOGLE_CUSTOM_SEARCH_ID=your-google-custom-search-id-here
SEARCH_PROVIDER=duckduckgo
FLASK_ENV=development
MAIL_USERNAME=[email protected]
MAIL_PASSWORD=your-app-passwordThe application uses PostgreSQL for data persistence:
- Database:
calliope_ai - Tables:
user,chat,message - Migrations: Flask-Migrate for schema management
- DuckDuckGo: No additional configuration needed
- Google Custom Search: Requires Google API key and Custom Search ID
- Web Scraping: No additional configuration needed
- Fallback: Automatically tries all available providers
- Theme Colors: Modify CSS variables in
app/static/css/style.css - File Upload: Adjust allowed extensions in
app/api/chat.py - Search Settings: Configure RAG parameters in
app/services/chat_service.py
- Low Coupling: Services are independent and easily testable
- High Cohesion: Related functionality is grouped together
- Scalable: Easy to add new features and services
- Maintainable: Clear separation of concerns
- Models: Add database models in
app/models/ - Services: Create business logic in
app/services/ - API: Add endpoints in
app/api/ - Templates: Create views in
app/templates/ - Static: Add CSS/JS in
app/static/
# Create migration
flask db migrate -m "Description of changes"
# Apply migration
flask db upgrade
# Rollback migration
flask db downgrade# Run setup tests
python test_setup.py
# Run application tests (if available)
python -m pytestYou can control which search providers are used by setting the SEARCH_PROVIDERS environment variable in your .env file:
SEARCH_PROVIDERS=tavily,bing,duckduckgo
Providers will be used in the order listed. Make sure you have the necessary API keys for each provider in your .env file if required.
If your Python files are not being staged by git, check your .gitignore for a *.py entry. Remove or comment it out to allow tracking of .py files.
If you encounter errors such as ModuleNotFoundError: No module named 'langchain_core.tracers.context', upgrade your langchain packages:
pip install --upgrade langchain langchain-core langchain-google-genai langchain-community
-
PostgreSQL Connection Error:
- Ensure PostgreSQL is running
- Check database credentials in
.env - Verify database exists:
createdb calliope_ai
-
API Key Errors:
- Verify Google API key is valid
- Check Google Custom Search configuration
- Ensure keys are properly set in
.env
-
Search Provider Issues:
- DuckDuckGo: May be rate limited, try fallback mode
- Google Custom Search: Check API key and search ID
- Web Scraping: May be blocked by some websites
-
Import Errors:
- Activate virtual environment
- Install dependencies:
pip install -r requirements.txt - Check Python version (3.8+ required)
-
Database Errors:
- Run migrations:
flask db upgrade - Check database connection
- Verify PostgreSQL installation
- Run migrations:
Check application logs for detailed error information:
- Flask debug mode shows detailed errors
- Database connection issues appear in console
- API errors are returned in JSON format
- Fork the repository
- Create a feature branch
- Make your changes
- Add tests if applicable
- Submit a pull request
This project is licensed under the MIT License - see the LICENSE file for details.
- Documentation: Check this README and inline code comments
- Issues: Report bugs and feature requests via GitHub Issues
- Discussions: Use GitHub Discussions for questions and ideas
- Google Gemini AI for powerful language model capabilities
- Langchain for AI framework and tools
- Flask for the web framework
- PostgreSQL for reliable database storage
- DuckDuckGo for privacy-focused search
- Font Awesome for beautiful icons
- Marked.js for markdown rendering
Calliope AI - Intelligent conversations powered by modern AI technology.