Gemini Dart
A comprehensive Dart package for integrating Google's Gemini AI models with support for text, image, and video processing.
Features
- 🤖 Multi-modal AI Integration: Support for text, image, and video inputs
- 🔧 Easy Integration: Simple API for Dart applications
- 🔄 Streaming Responses: Real-time text generation with streaming support
- 🛡️ Type Safety: Strongly typed models and responses
- 🔧 Configurable: Extensive configuration options for different use cases
- 📦 Cross-platform: Works on iOS, Android, Web, and Desktop
- 🚀 Performance Optimized: Efficient file handling and caching mechanisms
Installation
Add this package to your pubspec.yaml
:
dependencies:
gemini_dart: ^0.1.0
Then run:
dart pub get
Quick Start
Basic Text Generation
import 'package:gemini_dart/gemini_dart.dart';
void main() async {
// Initialize the client
final client = GeminiClient();
await client.initialize('YOUR_API_KEY');
// Generate text
final response = await client.generateText(prompt: 'Hello, Gemini!');
print(response.text);
// Clean up
client.dispose();
}
Multi-modal Content
import 'package:gemini_dart/gemini_dart.dart';
void main() async {
final client = GeminiClient();
await client.initialize('YOUR_API_KEY');
// Combine text and image
// Analyze an image with optional prompt
final response = await client.imageHandler.analyzeImage(
imageData: imageBytes,
mimeType: 'image/jpeg',
);
print(response.text);
}
Advanced Usage
import 'package:gemini_dart/gemini_dart.dart';
void main() async {
final client = GeminiClient();
await client.initialize('YOUR_API_KEY');
// Advanced configuration
final config = GenerationConfig(
temperature: 0.7,
maxOutputTokens: 1000,
topP: 0.9,
);
final response = await client.generateText(
prompt: 'Write a creative story',
config: config,
);
print(response.text);
client.dispose();
}
Configuration
Basic Configuration
final config = GeminiConfig(
timeout: Duration(seconds: 30),
maxRetries: 3,
enableLogging: true,
);
final client = GeminiClient();
await client.initialize('YOUR_API_KEY', config: config);
Generation Parameters
final generationConfig = GenerationConfig(
temperature: 0.7,
maxOutputTokens: 1000,
topP: 0.9,
topK: 40,
);
final response = await client.generateText(
prompt: 'Write a story about AI',
config: generationConfig,
);
Supported Content Types
Text
- Simple text prompts
- Multi-turn conversations
- Streaming responses
Images
- JPEG, PNG, WebP formats
- Automatic resizing for size limits
- Multi-image analysis
Video
- MP4, MOV formats
- Large file upload with progress tracking
- Frame-by-frame analysis
Error Handling
The package provides comprehensive error handling:
try {
final response = await client.generateText(prompt: 'Hello');
} on GeminiAuthException catch (e) {
print('Authentication error: ${e.message}');
} on GeminiRateLimitException catch (e) {
print('Rate limit exceeded. Retry after: ${e.retryAfter}');
} on GeminiException catch (e) {
print('Gemini error: ${e.message}');
}
Advanced Features
Streaming Responses
Get real-time text generation with streaming support.
File Processing
Handle images and videos with automatic format detection and optimization.
Conversation Context
Maintain conversation history for multi-turn interactions.
Examples
Check out the /example
directory for complete examples:
- Basic text generation
- Multi-modal content processing
- Image generation and analysis
- Video analysis workflow
API Reference
For detailed API documentation, visit pub.dev documentation.
Requirements
- Dart SDK: >=3.0.0 <4.0.0
- Google AI API key
Getting an API Key
- Visit Google AI Studio
- Create a new API key
- Add the key to your environment or configuration
Contributing
We welcome contributions! Please see our Contributing Guide for details.
License
This project is licensed under the MIT License - see the LICENSE file for details.
Support
Changelog
See CHANGELOG.md for a list of changes in each version.
Libraries
- gemini_dart
- Gemini Dart Package - Main export file