capacitor-gemini-nano
v1.0.0
Published
Capacitor plugin for on-device AI with Gemini Nano via ML Kit GenAI Prompt API
Maintainers
Readme
capacitor-gemini-nano
A Capacitor plugin for on-device AI using Gemini Nano via Google's ML Kit GenAI Prompt API.
Features
- On-device AI: Run Gemini Nano locally on supported Android devices
- No API keys required: Completely free and unlimited
- Offline capable: Works without internet connection
- Privacy-first: All processing stays on device
Supported Devices
Gemini Nano is available on:
- Google Pixel 9, Pixel 9 Pro, Pixel 9 Pro XL, Pixel 9 Pro Fold
- Samsung Galaxy S24 series (with One UI 6.1+)
- Samsung Galaxy S25 series
- Other devices with Android AICore support
Installation
npm install capacitor-gemini-nano
npx cap syncRequirements
- Capacitor 8.0+
- Android minSdkVersion 26+
- Device with Gemini Nano support
- AICore app installed and updated
Usage
Check Status
import { GeminiNano } from 'capacitor-gemini-nano';
const status = await GeminiNano.checkStatus();
console.log(status);
// { status: 'available' | 'downloadable' | 'downloading' | 'unavailable', message: string }Download Model (if needed)
// Listen for download progress
GeminiNano.addListener('onDownloadProgress', (data) => {
console.log(`Download progress: ${data.progress}%`);
});
const result = await GeminiNano.downloadModel();
if (result.success) {
console.log('Model downloaded!');
}Generate Text
// Warm up the model (optional but recommended)
await GeminiNano.warmup();
// Generate response
const result = await GeminiNano.generate({
prompt: 'What is the capital of France?',
temperature: 0.7,
maxOutputTokens: 256
});
if (result.success) {
console.log(result.text);
}Streaming Generation
GeminiNano.addListener('onToken', (data) => {
process.stdout.write(data.token);
});
GeminiNano.addListener('onComplete', (data) => {
console.log('\nComplete:', data.fullText);
});
GeminiNano.addListener('onError', (data) => {
console.error('Error:', data.error);
});
await GeminiNano.generateStream({
prompt: 'Write a short poem about coding',
temperature: 0.8
});API
Methods
| Method | Description |
|--------|-------------|
| checkStatus() | Check if Gemini Nano is available |
| downloadModel() | Download the model if status is 'downloadable' |
| warmup() | Pre-warm the model for faster first generation |
| generate(options) | Generate text (non-streaming) |
| generateStream(options) | Generate text with streaming |
GenerateOptions
| Property | Type | Default | Description |
|----------|------|---------|-------------|
| prompt | string | required | The input prompt |
| temperature | number | 0.7 | Creativity (0.0-1.0) |
| topK | number | 40 | Top-K sampling |
| maxOutputTokens | number | 256 | Maximum output length |
Events
| Event | Data | Description |
|-------|------|-------------|
| onDownloadProgress | { progress: number } | Download progress (0-100) |
| onToken | { token: string } | Streaming token |
| onComplete | { fullText: string } | Streaming complete |
| onError | { error: string } | Error occurred |
Limitations
- Output tokens: ~256 tokens max per generation
- Context window: ~3000 words input
- Android only: iOS not supported (no Gemini Nano on iOS)
- Device dependent: Only works on supported hardware
Troubleshooting
"Gemini Nano unavailable"
- Check if your device is supported
- Update Google Play Services
- Update AICore app (Settings > Apps > AICore)
- Change device region to US (some regions restricted)
- Join the Android AICore beta program
"Model needs download"
Call downloadModel() and wait for completion. This requires WiFi and can take several minutes.
License
MIT
Credits
Built with ML Kit GenAI Prompt API.
