A Flutter chatbot application with dual AI provider support: Google Gemini API and Google Vertex AI.
- π€ Dual AI Provider Support - Switch between Gemini API and Vertex AI
- π Multiple Authentication Methods - API keys and Service Account authentication
- π¬ Modern Material Design UI - Clean, responsive interface
- π± Mobile & Web Ready - iOS, Android, and Web support
- π‘οΈ Enterprise-Ready - Production logging, error handling, and validation
- β‘ Fast and Responsive - Optimized for performance
- π Auto-Recovery - Smart retry mechanisms and connection management
lib/
βββ main.dart # App entry point with logging setup
βββ models/
β βββ api_config.dart # Configuration model for both providers
β βββ message.dart # Message data model
βββ screens/
β βββ chat_screen.dart # Enhanced chat interface
βββ services/
β βββ ai_service.dart # Abstract service interface
β βββ gemini_api_service.dart # Gemini API implementation
β βββ vertex_ai_service.dart # Vertex AI implementation
β βββ service_factory.dart # Dynamic service creation
β βββ gemini_service.dart # Legacy compatibility layer
βββ widgets/
βββ message_bubble.dart # Chat bubble UI component
- Flutter SDK (3.10.0+)
- Dart SDK (3.0.0+)
- Google Cloud Project (for Vertex AI) or Gemini API key
-
Clone and setup:
git clone https://github.com/acesley180604/gemini-chatbot-flutter.git cd gemini-chatbot-flutter flutter pub get
-
Choose your configuration:
Option A: Gemini API (Simplest)
cp .env.gemini.example .env # Edit .env and add your Gemini API key
Option B: Vertex AI (Enterprise)
cp .env.vertex.example .env # Edit .env and configure Vertex AI settings
-
Run the app:
flutter run
- Get your API key from Google AI Studio
- Create
.env
file:AI_PROVIDER=gemini_api GEMINI_API_KEY=your_api_key_here GEMINI_MODEL=gemini-1.5-flash-latest TEMPERATURE=0.9 MAX_OUTPUT_TOKENS=2048
- Create a Google Cloud Project
- Enable the Vertex AI API
- Create a service account with Vertex AI permissions
- Download the service account JSON file
- Configure
.env
:AI_PROVIDER=vertex_ai VERTEX_AUTH_TYPE=service_account VERTEX_SERVICE_ACCOUNT_PATH=/path/to/service-account.json VERTEX_PROJECT_ID=your-project-id VERTEX_LOCATION=us-central1 VERTEX_MODEL=gemini-1.5-flash TEMPERATURE=0.9 MAX_OUTPUT_TOKENS=2048
AI_PROVIDER=vertex_ai
VERTEX_AUTH_TYPE=api_key
VERTEX_API_KEY=your_vertex_api_key
VERTEX_PROJECT_ID=your-project-id
VERTEX_LOCATION=us-central1
VERTEX_MODEL=gemini-1.5-flash
Variable | Description | Required | Default |
---|---|---|---|
AI_PROVIDER |
Service provider (gemini_api or vertex_ai ) |
No | gemini_api |
GEMINI_API_KEY |
Gemini API key | Yes (Gemini) | - |
GEMINI_MODEL |
Gemini model name | No | gemini-1.5-flash-latest |
VERTEX_AUTH_TYPE |
Vertex auth type (api_key or service_account ) |
No | service_account |
VERTEX_API_KEY |
Vertex AI API key | Yes (Vertex + API key) | - |
VERTEX_SERVICE_ACCOUNT_PATH |
Path to service account JSON | Yes (Vertex + SA) | - |
VERTEX_PROJECT_ID |
Google Cloud Project ID | Yes (Vertex) | - |
VERTEX_LOCATION |
Vertex AI region | No | us-central1 |
VERTEX_MODEL |
Vertex AI model name | No | gemini-1.5-flash |
TEMPERATURE |
Response creativity (0.0-1.0) | No | 0.9 |
MAX_OUTPUT_TOKENS |
Maximum response length | No | 2048 |
import 'services/service_factory.dart';
import 'services/ai_service.dart';
// Create service with auto-detection
final service = AIServiceFactory.createService();
// Create and validate service
final validatedService = await AIServiceFactory.createAndValidateService();
// Custom configuration
final customConfig = APIConfig(
provider: AIProvider.vertexAI,
authType: AuthType.serviceAccount,
serviceAccountPath: '/path/to/key.json',
projectId: 'my-project',
);
final customService = AIServiceFactory.createService(config: customConfig);
The app includes comprehensive error handling for:
- Authentication errors - Invalid API keys or credentials
- Rate limiting - Automatic retry suggestions
- Network issues - Connection problem detection
- Invalid requests - Request format validation
- Service errors - Provider-specific error handling
# Web
flutter run -d chrome
# iOS (macOS only)
flutter run -d ios
# Android
flutter run -d android
# Web
flutter build web
# iOS
flutter build ios --release
# Android
flutter build apk --release
Authentication Errors:
- β Verify API keys are correct and active
- β Check service account has proper permissions
- β Ensure service account JSON file path is correct
Network/Connection Issues:
- β Check internet connectivity
- β Verify firewall settings
- β
For web: use
--web-browser-flag "--disable-web-security"
Configuration Issues:
- β Verify all required environment variables are set
- β
Check
.env
file format and syntax - β Restart app after configuration changes
Vertex AI Specific:
- β Enable Vertex AI API in Google Cloud Console
- β Check project ID and location settings
- β Verify service account permissions
The app includes comprehensive logging. Check the console for detailed error messages with π΄ (errors), π‘ (warnings), and π΅ (info) indicators.
The app maintains backward compatibility. Existing .env
files with only GEMINI_API_KEY
will continue to work without changes.
To upgrade to the new architecture:
- Add
AI_PROVIDER=gemini_api
to your.env
file - Optionally migrate to the new service factory pattern
- Fork the repository
- Create your feature branch (
git checkout -b feature/amazing-feature
) - Commit your changes (
git commit -m 'Add some amazing feature'
) - Push to the branch (
git push origin feature/amazing-feature
) - Open a Pull Request
This project is licensed under the MIT License - see the LICENSE file for details.
- Google Gemini AI for AI capabilities
- Google Vertex AI for enterprise AI platform
- Flutter for the cross-platform framework
- Material Design for UI components