@capgo/capacitor-llm 8.0.10 → 8.0.12
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CapgoCapacitorLlm.podspec +8 -0
- package/README.md +88 -257
- package/android/build.gradle +1 -0
- package/android/src/main/java/ee/forgr/capgo_llm/LLM.java +290 -86
- package/android/src/main/java/ee/forgr/capgo_llm/LLMPlugin.java +39 -2
- package/dist/docs.json +29 -1
- package/dist/esm/definitions.d.ts +16 -2
- package/dist/esm/definitions.js.map +1 -1
- package/dist/esm/web.js +11 -0
- package/dist/esm/web.js.map +1 -1
- package/dist/plugin.cjs.js +11 -0
- package/dist/plugin.cjs.js.map +1 -1
- package/dist/plugin.js +11 -0
- package/dist/plugin.js.map +1 -1
- package/ios/Sources/LLMPlugin/LLMPlugin.swift +433 -10
- package/package.json +1 -1
|
@@ -11,7 +11,15 @@ Pod::Spec.new do |s|
|
|
|
11
11
|
s.author = package['author']
|
|
12
12
|
s.source = { :git => package['repository']['url'], :tag => s.version.to_s }
|
|
13
13
|
s.source_files = 'ios/Sources/**/*.{swift,h,m,c,cc,mm,cpp}'
|
|
14
|
+
s.platform = :ios, '15.0'
|
|
14
15
|
s.ios.deployment_target = '15.0'
|
|
16
|
+
s.pod_target_xcconfig = {
|
|
17
|
+
'SUPPORTS_MACCATALYST' => 'NO',
|
|
18
|
+
'EXCLUDED_ARCHS[sdk=macosx*]' => 'arm64 x86_64'
|
|
19
|
+
}
|
|
20
|
+
s.user_target_xcconfig = {
|
|
21
|
+
'EXCLUDED_ARCHS[sdk=macosx*]' => 'arm64 x86_64'
|
|
22
|
+
}
|
|
15
23
|
s.dependency 'Capacitor'
|
|
16
24
|
s.dependency 'MediaPipeTasksGenAI'
|
|
17
25
|
s.dependency 'MediaPipeTasksGenAIC'
|
package/README.md
CHANGED
|
@@ -9,7 +9,9 @@
|
|
|
9
9
|
|
|
10
10
|
Adds support for LLM locally run for Capacitor
|
|
11
11
|
|
|
12
|
-
It uses Apple Intelligence
|
|
12
|
+
It uses Apple Intelligence for the iOS system model, MediaPipe for existing `.task` models, and ExecuTorch `.pte` models on both iOS and Android.
|
|
13
|
+
|
|
14
|
+
**Mac Catalyst:** Native iOS functionality is disabled for Mac Catalyst builds. MediaPipe pods are skipped and native calls will return an unsupported response; use an iOS/iPadOS target for native features.
|
|
13
15
|
|
|
14
16
|
## Documentation
|
|
15
17
|
|
|
@@ -29,13 +31,13 @@ The most complete doc is available here: https://capgo.app/docs/plugins/llm/
|
|
|
29
31
|
## Installation
|
|
30
32
|
|
|
31
33
|
```bash
|
|
32
|
-
|
|
33
|
-
|
|
34
|
+
bun add @capgo/capacitor-llm
|
|
35
|
+
bunx cap sync
|
|
34
36
|
```
|
|
35
37
|
|
|
36
38
|
### iOS Additional Setup for Custom Models
|
|
37
39
|
|
|
38
|
-
|
|
40
|
+
Apple Intelligence works without bundled model files on supported iOS versions. For custom models, the plugin supports MediaPipe through CocoaPods and ExecuTorch through Swift Package Manager.
|
|
39
41
|
|
|
40
42
|
**Using CocoaPods:**
|
|
41
43
|
The MediaPipe dependencies are already configured in the podspec. Make sure to run `pod install` after adding the plugin.
|
|
@@ -53,13 +55,13 @@ use_frameworks! :linkage => :static
|
|
|
53
55
|
# And add this to your post_install hook:
|
|
54
56
|
post_install do |installer|
|
|
55
57
|
assertDeploymentTarget(installer)
|
|
56
|
-
|
|
58
|
+
|
|
57
59
|
# Fix for static framework dependencies
|
|
58
60
|
installer.pods_project.targets.each do |target|
|
|
59
61
|
target.build_configurations.each do |config|
|
|
60
62
|
config.build_settings['BUILD_LIBRARY_FOR_DISTRIBUTION'] = 'YES'
|
|
61
63
|
end
|
|
62
|
-
|
|
64
|
+
|
|
63
65
|
# Specifically for MediaPipe pods
|
|
64
66
|
if target.name.include?('MediaPipeTasksGenAI')
|
|
65
67
|
target.build_configurations.each do |config|
|
|
@@ -71,181 +73,83 @@ end
|
|
|
71
73
|
```
|
|
72
74
|
|
|
73
75
|
**Using Swift Package Manager:**
|
|
74
|
-
|
|
75
|
-
MediaPipe GenAI does not officially support SPM yet (see [MediaPipe issue #5464](https://github.com/google-ai-edge/mediapipe/issues/5464)). However, you can use the community package [SwiftTasksGenAI](https://github.com/paescebu/SwiftTasksGenAI) by adding it manually in Xcode:
|
|
76
|
-
|
|
77
|
-
**To add MediaPipe GenAI via SPM:**
|
|
78
|
-
1. Open your app project in Xcode
|
|
79
|
-
2. Go to **File > Add Package Dependencies...**
|
|
80
|
-
3. Enter the URL: `https://github.com/paescebu/SwiftTasksGenAI.git`
|
|
81
|
-
4. Select your desired version (e.g., 0.10.27)
|
|
82
|
-
5. Add the `SwiftTasksGenAI` product to your app target
|
|
83
|
-
|
|
84
|
-
**Note:** SwiftTasksGenAI uses unsafe build flags, which means it cannot be added directly in Package.swift files, but works fine when added through Xcode's UI. This is the same approach used by SwiftTasksVision.
|
|
85
|
-
|
|
86
|
-
**Alternatively:** Use CocoaPods for a more traditional setup, or use Apple Intelligence (iOS 18.2+) which works with both SPM and CocoaPods.
|
|
76
|
+
The main Swift package stays compatible with iOS 15. To use ExecuTorch `.pte` models on iOS, add the ExecuTorch Swift package to your app target and link `executorch_llm`, `backend_xnnpack`, `kernels_llm`, `kernels_optimized`, `kernels_quantized`, and `kernels_torchao`. ExecuTorch currently requires iOS 17 or newer in the app target that links it. MediaPipe GenAI still does not officially support SPM, so use CocoaPods for MediaPipe `.task` models.
|
|
87
77
|
|
|
88
78
|
## Adding a Model to Your App
|
|
89
79
|
|
|
90
|
-
|
|
80
|
+
The simplest cross-platform custom model path is ExecuTorch. It uses the same kind of `.pte` model file on iOS and Android, plus a tokenizer file.
|
|
91
81
|
|
|
92
|
-
|
|
82
|
+
### ExecuTorch Models (iOS and Android)
|
|
93
83
|
|
|
94
|
-
|
|
95
|
-
2. **Custom Models**: Use your own models via MediaPipe (requires CocoaPods) - Note: Some model formats may have compatibility issues
|
|
84
|
+
iOS uses ExecuTorch when the app target links the ExecuTorch SwiftPM products. Android uses the ExecuTorch Maven package.
|
|
96
85
|
|
|
97
|
-
|
|
86
|
+
Bundle the model files:
|
|
98
87
|
|
|
99
|
-
|
|
88
|
+
- iOS: add the `.pte` model and tokenizer file to your app target's Copy Bundle Resources.
|
|
89
|
+
- Android: place the `.pte` model and tokenizer file under `android/app/src/main/assets/`, then reference them with `/android_asset/...`.
|
|
100
90
|
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
## Model Compatibility Guide
|
|
106
|
-
|
|
107
|
-
### Android (Working ✅)
|
|
108
|
-
- **Model**: Gemma-3 models (270M, 1B, etc.)
|
|
109
|
-
- **Format**: `.task` + `.litertlm` files
|
|
110
|
-
- **Where to download**:
|
|
111
|
-
- [Kaggle Gemma models](https://www.kaggle.com/models/google/gemma) - "LiteRT (formerly TFLite)" tab
|
|
112
|
-
- Example: `gemma-3-270m-it-int8.task` + `gemma-3-270m-it-int8.litertlm`
|
|
91
|
+
```typescript
|
|
92
|
+
import { Capacitor } from '@capacitor/core';
|
|
93
|
+
import { CapgoLLM } from '@capgo/capacitor-llm';
|
|
113
94
|
|
|
114
|
-
|
|
115
|
-
- **Recommended**: Use Apple Intelligence (built-in, no download needed)
|
|
116
|
-
- **Alternative (Experimental)**:
|
|
117
|
-
- **Model**: Gemma-2 2B (documented as compatible but may still fail)
|
|
118
|
-
- **Format**: `.task` files (`.bin` format preferred but not available)
|
|
119
|
-
- **Where to download**:
|
|
120
|
-
- [Hugging Face MediaPipe models](https://huggingface.co/collections/google/mediapipe-668392ead2d6768e82fb3b87)
|
|
121
|
-
- Look for `Gemma2-2B-IT_*_ekv*.task` files
|
|
122
|
-
- Example: `Gemma2-2B-IT_multi-prefill-seq_q8_ekv1280.task`
|
|
123
|
-
- **Note**: Even with Gemma-2 2B, you may still encounter errors. Apple Intelligence is more reliable.
|
|
95
|
+
const isAndroid = Capacitor.getPlatform() === 'android';
|
|
124
96
|
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
97
|
+
await CapgoLLM.setModel({
|
|
98
|
+
engine: 'executorch',
|
|
99
|
+
path: isAndroid ? '/android_asset/model.pte' : 'model.pte',
|
|
100
|
+
tokenizerPath: isAndroid ? '/android_asset/tokenizer.model' : 'tokenizer.model',
|
|
101
|
+
maxTokens: 2048,
|
|
102
|
+
sequenceLength: 2048,
|
|
103
|
+
temperature: 0.8,
|
|
104
|
+
});
|
|
129
105
|
|
|
130
|
-
|
|
106
|
+
const { id: chatId } = await CapgoLLM.createChat();
|
|
107
|
+
```
|
|
131
108
|
|
|
132
|
-
|
|
133
|
-
- Go to [Kaggle Gemma models](https://www.kaggle.com/models/google/gemma)
|
|
134
|
-
- Click "LiteRT (formerly TFLite)" tab
|
|
135
|
-
- Download both `.task` and `.litertlm` files
|
|
136
|
-
- Place in `android/app/src/main/assets/`
|
|
109
|
+
`engine: 'auto'` also selects ExecuTorch when the model path ends in `.pte` or when `tokenizerPath` is provided. Passing `engine: 'executorch'` is recommended when loading ExecuTorch models so failures are explicit.
|
|
137
110
|
|
|
138
|
-
|
|
139
|
-
- Visit [Hugging Face MediaPipe collection](https://huggingface.co/collections/google/mediapipe-668392ead2d6768e82fb3b87)
|
|
140
|
-
- Find Gemma-2 2B models
|
|
141
|
-
- Download `.task` files (and `.litertlm` if available)
|
|
142
|
-
- Add to Xcode project in "Copy Bundle Resources"
|
|
143
|
-
- Note: Success is not guaranteed due to format compatibility issues
|
|
111
|
+
### MediaPipe Models
|
|
144
112
|
|
|
145
|
-
|
|
113
|
+
MediaPipe remains available for existing `.task` models. Android models usually need both `.task` and `.litertlm` files. iOS MediaPipe support is available through CocoaPods and remains experimental for some `.task` files.
|
|
146
114
|
|
|
147
115
|
```typescript
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
// iOS - Use Apple Intelligence (default)
|
|
151
|
-
await CapgoLLM.setModel({ path: 'Apple Intelligence' });
|
|
152
|
-
|
|
153
|
-
// iOS - Use MediaPipe model (experimental)
|
|
154
|
-
await CapgoLLM.setModel({
|
|
155
|
-
path: 'Gemma2-2B-IT_multi-prefill-seq_q8_ekv1280',
|
|
116
|
+
await CapgoLLM.setModel({
|
|
117
|
+
path: '/android_asset/gemma-3-270m-it-int8.task',
|
|
156
118
|
modelType: 'task',
|
|
157
|
-
maxTokens:
|
|
119
|
+
maxTokens: 2048,
|
|
120
|
+
topk: 40,
|
|
121
|
+
temperature: 0.8,
|
|
158
122
|
});
|
|
159
|
-
|
|
160
|
-
// Now you can create chats and send messages
|
|
161
|
-
const chat = await CapgoLLM.createChat();
|
|
162
123
|
```
|
|
163
124
|
|
|
164
|
-
###
|
|
165
|
-
|
|
166
|
-
For Android, you need to include a compatible LLM model in your app. The plugin uses MediaPipe's tasks-genai, which supports various model formats.
|
|
125
|
+
### Apple Intelligence
|
|
167
126
|
|
|
168
|
-
|
|
169
|
-
https://developer.android.com/ai/gemini-nano/experimental
|
|
127
|
+
On supported iOS devices, Apple Intelligence can be used without bundling a model.
|
|
170
128
|
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
}
|
|
129
|
+
```typescript
|
|
130
|
+
await CapgoLLM.setModel({
|
|
131
|
+
path: 'Apple Intelligence',
|
|
132
|
+
engine: 'apple',
|
|
133
|
+
});
|
|
177
134
|
```
|
|
178
135
|
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
- **Recommended**: Gemma 3 270M - Smallest, most efficient (~240-400MB)
|
|
183
|
-
- Text-only model optimized for on-device inference
|
|
184
|
-
- Download from [Kaggle](https://www.kaggle.com/models/google/gemma) → "LiteRT (formerly TFLite)" tab
|
|
185
|
-
- Get both `.task` and `.litertlm` files
|
|
186
|
-
|
|
187
|
-
**For iOS: Limited Options** (⚠️)
|
|
188
|
-
- **Option 1** (Recommended): Use Apple Intelligence - no download needed
|
|
189
|
-
- **Option 2** (Experimental): Try Gemma-2 2B from [Hugging Face](https://huggingface.co/collections/google/mediapipe-668392ead2d6768e82fb3b87)
|
|
190
|
-
- Download `Gemma2-2B-IT_*.task` files
|
|
191
|
-
- May still encounter compatibility errors
|
|
192
|
-
|
|
193
|
-
**Available Model Sizes** (from [Google AI](https://ai.google.dev/gemma/docs/core#sizes)):
|
|
194
|
-
- `gemma-3-270m` - Most portable, text-only (~240-400MB) - Android only
|
|
195
|
-
- `gemma-3-1b` - Larger text-only (~892MB-1.5GB) - Android only
|
|
196
|
-
- `gemma-2-2b` - Cross-platform compatible (~1-2GB) - iOS experimental
|
|
197
|
-
- Larger models available but not recommended for mobile
|
|
198
|
-
|
|
199
|
-
For automated download, see the script in the example app section below.
|
|
200
|
-
|
|
201
|
-
2. **Add the model to your app**:
|
|
202
|
-
|
|
203
|
-
**Option A - Bundle in APK (for smaller models)**:
|
|
204
|
-
- Place the model files in your app's `android/app/src/main/assets/` directory
|
|
205
|
-
- The `/android_asset/` prefix is used to reference files from the assets folder
|
|
206
|
-
- Note: This approach may increase APK size significantly
|
|
207
|
-
|
|
208
|
-
**Option B - Download at runtime (recommended for production)**:
|
|
209
|
-
- Host the model files on a server
|
|
210
|
-
- Download them to your app's files directory at runtime
|
|
211
|
-
- This keeps your APK size small
|
|
212
|
-
|
|
213
|
-
3. **Set the model path**:
|
|
136
|
+
### Downloading Models at Runtime
|
|
137
|
+
|
|
138
|
+
Use `downloadModel` to keep large model files out of the app bundle. For ExecuTorch, `companionUrl` can point at the tokenizer file.
|
|
214
139
|
|
|
215
140
|
```typescript
|
|
216
|
-
|
|
141
|
+
const result = await CapgoLLM.downloadModel({
|
|
142
|
+
url: 'https://your-server.com/models/model.pte',
|
|
143
|
+
companionUrl: 'https://your-server.com/models/tokenizer.model',
|
|
144
|
+
filename: 'model.pte',
|
|
145
|
+
});
|
|
217
146
|
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
// Example: Download model at runtime (recommended for production)
|
|
225
|
-
async function downloadModel() {
|
|
226
|
-
// Add progress listener
|
|
227
|
-
await CapgoLLM.addListener('downloadProgress', (event) => {
|
|
228
|
-
console.log(`Download progress: ${event.progress}%`);
|
|
229
|
-
});
|
|
230
|
-
|
|
231
|
-
// Download the model
|
|
232
|
-
const result = await CapgoLLM.downloadModel({
|
|
233
|
-
url: 'https://your-server.com/models/gemma-3-270m-it-int8.task',
|
|
234
|
-
companionUrl: 'https://your-server.com/models/gemma-3-270m-it-int8.litertlm', // Android only
|
|
235
|
-
filename: 'gemma-3-270m-it-int8.task' // Optional, defaults to filename from URL
|
|
236
|
-
});
|
|
237
|
-
|
|
238
|
-
console.log('Model downloaded to:', result.path);
|
|
239
|
-
if (result.companionPath) {
|
|
240
|
-
console.log('Companion file downloaded to:', result.companionPath);
|
|
241
|
-
}
|
|
242
|
-
|
|
243
|
-
// Now set the model path
|
|
244
|
-
await CapgoLLM.setModelPath({ path: result.path });
|
|
245
|
-
}
|
|
246
|
-
|
|
247
|
-
// Now you can create chats and send messages
|
|
248
|
-
const chat = await CapgoLLM.createChat();
|
|
147
|
+
await CapgoLLM.setModel({
|
|
148
|
+
engine: 'executorch',
|
|
149
|
+
path: result.path,
|
|
150
|
+
tokenizerPath: result.companionPath,
|
|
151
|
+
sequenceLength: 2048,
|
|
152
|
+
});
|
|
249
153
|
```
|
|
250
154
|
|
|
251
155
|
## Usage Example
|
|
@@ -253,34 +157,22 @@ const chat = await CapgoLLM.createChat();
|
|
|
253
157
|
```typescript
|
|
254
158
|
import { CapgoLLM } from '@capgo/capacitor-llm';
|
|
255
159
|
|
|
256
|
-
// Check if LLM is ready
|
|
257
160
|
const { readiness } = await CapgoLLM.getReadiness();
|
|
258
161
|
console.log('LLM readiness:', readiness);
|
|
259
162
|
|
|
260
|
-
// Set a custom model (optional - uses system default if not called)
|
|
261
|
-
// iOS: Use a GGUF model from bundle or path
|
|
262
|
-
// Android: Use a MediaPipe model from assets or files
|
|
263
|
-
await CapgoLLM.setModelPath({
|
|
264
|
-
path: Platform.isIOS ? 'gemma-3-270m.gguf' : '/android_asset/gemma-3-270m-it-int8.task'
|
|
265
|
-
});
|
|
266
|
-
|
|
267
|
-
// Create a chat session
|
|
268
163
|
const { id: chatId } = await CapgoLLM.createChat();
|
|
269
164
|
|
|
270
|
-
// Listen for AI responses
|
|
271
165
|
CapgoLLM.addListener('textFromAi', (event) => {
|
|
272
166
|
console.log('AI:', event.text);
|
|
273
167
|
});
|
|
274
168
|
|
|
275
|
-
// Listen for completion
|
|
276
169
|
CapgoLLM.addListener('aiFinished', (event) => {
|
|
277
170
|
console.log('AI finished responding to chat:', event.chatId);
|
|
278
171
|
});
|
|
279
172
|
|
|
280
|
-
// Send a message
|
|
281
173
|
await CapgoLLM.sendMessage({
|
|
282
174
|
chatId,
|
|
283
|
-
message: 'Hello! How are you today?'
|
|
175
|
+
message: 'Hello! How are you today?',
|
|
284
176
|
});
|
|
285
177
|
```
|
|
286
178
|
|
|
@@ -355,8 +247,8 @@ setModel(options: ModelOptions) => Promise<void>
|
|
|
355
247
|
```
|
|
356
248
|
|
|
357
249
|
Sets the model configuration
|
|
358
|
-
- iOS: Use "Apple Intelligence" as path for system model, or
|
|
359
|
-
- Android: Path to a MediaPipe model file (in assets or files directory)
|
|
250
|
+
- iOS: Use "Apple Intelligence" as path for system model, provide a MediaPipe model, or set engine to "executorch"
|
|
251
|
+
- Android: Path to a MediaPipe or ExecuTorch model file (in assets or files directory)
|
|
360
252
|
|
|
361
253
|
| Param | Type | Description |
|
|
362
254
|
| ------------- | ----------------------------------------------------- | ------------------------- |
|
|
@@ -478,14 +370,18 @@ Get the native Capacitor plugin version.
|
|
|
478
370
|
|
|
479
371
|
Model configuration options
|
|
480
372
|
|
|
481
|
-
| Prop
|
|
482
|
-
|
|
|
483
|
-
| **`path`**
|
|
484
|
-
| **`
|
|
485
|
-
| **`
|
|
486
|
-
| **`
|
|
487
|
-
| **`
|
|
488
|
-
| **`
|
|
373
|
+
| Prop | Type | Description |
|
|
374
|
+
| -------------------- | ------------------------------------------------------------- | --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
|
|
375
|
+
| **`path`** | <code>string</code> | Model path or "Apple Intelligence" for iOS system model |
|
|
376
|
+
| **`engine`** | <code>'auto' \| 'apple' \| 'mediapipe' \| 'executorch'</code> | Runtime engine to use. - "auto": uses Apple Intelligence on iOS when path is "Apple Intelligence", ExecuTorch for `.pte` or tokenizerPath, otherwise MediaPipe - "apple": iOS Foundation Models / Apple Intelligence - "mediapipe": MediaPipe GenAI `.task` models - "executorch": ExecuTorch `.pte` models with a tokenizer file |
|
|
377
|
+
| **`modelType`** | <code>string</code> | Model file type/extension (e.g., "task", "bin", "litertlm"). If not provided, will be extracted from path. |
|
|
378
|
+
| **`tokenizerPath`** | <code>string</code> | Tokenizer path for ExecuTorch models. Required when engine is "executorch". |
|
|
379
|
+
| **`specialTokens`** | <code>string[]</code> | Optional special tokens passed to iOS ExecuTorch tokenizers. |
|
|
380
|
+
| **`maxTokens`** | <code>number</code> | Maximum number of tokens the model handles |
|
|
381
|
+
| **`sequenceLength`** | <code>number</code> | Sequence length for ExecuTorch generation. Defaults to maxTokens when omitted. |
|
|
382
|
+
| **`topk`** | <code>number</code> | Number of tokens the model considers at each step |
|
|
383
|
+
| **`temperature`** | <code>number</code> | Amount of randomness in generation (0.0-1.0) |
|
|
384
|
+
| **`randomSeed`** | <code>number</code> | Random seed for generation |
|
|
489
385
|
|
|
490
386
|
|
|
491
387
|
#### DownloadModelResult
|
|
@@ -552,92 +448,27 @@ Event data for readiness status changes
|
|
|
552
448
|
|
|
553
449
|
## Example App Model Setup
|
|
554
450
|
|
|
555
|
-
The example app
|
|
556
|
-
|
|
557
|
-
### Downloading Models
|
|
558
|
-
|
|
559
|
-
Since AI models require license acceptance, you need to download them manually:
|
|
560
|
-
|
|
561
|
-
### Model Setup by Platform
|
|
562
|
-
|
|
563
|
-
#### Android Setup (Gemma 3 270M) ✅
|
|
564
|
-
|
|
565
|
-
1. **Create Kaggle account and accept license:**
|
|
566
|
-
- Create a [Kaggle account](https://www.kaggle.com)
|
|
567
|
-
- Visit [Gemma 3 models](https://www.kaggle.com/models/google/gemma-3) and accept terms
|
|
568
|
-
|
|
569
|
-
2. **Download the model:**
|
|
570
|
-
- Click "LiteRT (formerly TFLite)" tab
|
|
571
|
-
- Download `gemma-3-270m-it-int8` (get BOTH files):
|
|
572
|
-
- `gemma-3-270m-it-int8.task`
|
|
573
|
-
- `gemma-3-270m-it-int8.litertlm`
|
|
574
|
-
|
|
575
|
-
3. **Place in Android app:**
|
|
576
|
-
- Copy BOTH files to: `example-app/android/app/src/main/assets/`
|
|
577
|
-
- In code, reference with `/android_asset/` prefix
|
|
578
|
-
|
|
579
|
-
#### iOS Setup ⚠️
|
|
451
|
+
The example app can use either the older MediaPipe assets or the new ExecuTorch assets.
|
|
580
452
|
|
|
581
|
-
|
|
582
|
-
- No model download needed
|
|
583
|
-
- Works out of the box on iOS 26.0+
|
|
453
|
+
### Recommended Custom Path: ExecuTorch
|
|
584
454
|
|
|
585
|
-
|
|
586
|
-
|
|
587
|
-
|
|
588
|
-
3. Add to Xcode project in "Copy Bundle Resources"
|
|
589
|
-
4. Note: May still encounter errors - Apple Intelligence is more reliable
|
|
590
|
-
### Update your code to use the model:
|
|
591
|
-
```typescript
|
|
592
|
-
// Android - Gemma 3 270M
|
|
593
|
-
await CapgoLLM.setModel({
|
|
594
|
-
path: '/android_asset/gemma-3-270m-it-int8.task',
|
|
595
|
-
maxTokens: 2048,
|
|
596
|
-
topk: 40,
|
|
597
|
-
temperature: 0.8
|
|
598
|
-
});
|
|
455
|
+
1. Export or download an ExecuTorch `.pte` model and matching tokenizer file.
|
|
456
|
+
2. Add both files to the iOS app bundle, or place both files in `example-app/android/app/src/main/assets/` for Android.
|
|
457
|
+
3. Call `setModel` with `engine: 'executorch'`, `path`, and `tokenizerPath`.
|
|
599
458
|
|
|
600
|
-
|
|
601
|
-
await CapgoLLM.setModel({
|
|
602
|
-
path: 'Apple Intelligence'
|
|
603
|
-
});
|
|
459
|
+
### Legacy MediaPipe Path
|
|
604
460
|
|
|
605
|
-
|
|
606
|
-
await CapgoLLM.setModel({
|
|
607
|
-
path: 'Gemma2-2B-IT_multi-prefill-seq_q8_ekv1280',
|
|
608
|
-
modelType: 'task',
|
|
609
|
-
maxTokens: 1280,
|
|
610
|
-
topk: 40,
|
|
611
|
-
temperature: 0.8
|
|
612
|
-
});
|
|
613
|
-
```
|
|
461
|
+
Android still supports Gemma 3 LiteRT assets from Kaggle. Download both files and place them in `example-app/android/app/src/main/assets/`:
|
|
614
462
|
|
|
615
|
-
|
|
463
|
+
- `gemma-3-270m-it-int8.task`
|
|
464
|
+
- `gemma-3-270m-it-int8.litertlm`
|
|
616
465
|
|
|
617
|
-
|
|
618
|
-
- Smallest size (~240-400MB)
|
|
619
|
-
- Text-only (perfect for chat)
|
|
620
|
-
- Optimized for mobile devices
|
|
621
|
-
- Works reliably with MediaPipe
|
|
622
|
-
|
|
623
|
-
**For iOS**: Apple Intelligence is recommended because:
|
|
624
|
-
- No download required
|
|
625
|
-
- Native iOS integration
|
|
626
|
-
- Better performance
|
|
627
|
-
- No compatibility issues
|
|
466
|
+
iOS MediaPipe remains experimental because some `.task` models can fail during prefill. Apple Intelligence or ExecuTorch is preferred on iOS.
|
|
628
467
|
|
|
629
468
|
## Known Issues
|
|
630
469
|
|
|
631
|
-
-
|
|
632
|
-
|
|
633
|
-
|
|
634
|
-
|
|
635
|
-
|
|
636
|
-
- **Platform Requirements**:
|
|
637
|
-
- iOS: Apple Intelligence requires iOS 26.0+
|
|
638
|
-
- Android: Minimum SDK 24
|
|
639
|
-
|
|
640
|
-
- **Performance Considerations**:
|
|
641
|
-
- Model files are large (300MB-2GB)
|
|
642
|
-
- Initial download takes time
|
|
643
|
-
- Some devices may have memory limitations
|
|
470
|
+
- ExecuTorch is native-only and is not available on web.
|
|
471
|
+
- iOS ExecuTorch requires linking the ExecuTorch SwiftPM products in the app target, and that app target must support iOS 17 or newer.
|
|
472
|
+
- Apple Intelligence requires iOS 26.0 or later and a supported device.
|
|
473
|
+
- Android requires minSdkVersion 24 or higher.
|
|
474
|
+
- Model files are large, so production apps should usually download them after install.
|
package/android/build.gradle
CHANGED
|
@@ -53,6 +53,7 @@ dependencies {
|
|
|
53
53
|
implementation project(':capacitor-android')
|
|
54
54
|
implementation "androidx.appcompat:appcompat:$androidxAppCompatVersion"
|
|
55
55
|
implementation 'com.google.mediapipe:tasks-genai:0.10.27'
|
|
56
|
+
implementation 'org.pytorch:executorch-android:1.2.0'
|
|
56
57
|
testImplementation "junit:junit:$junitVersion"
|
|
57
58
|
androidTestImplementation "androidx.test.ext:junit:$androidxJunitVersion"
|
|
58
59
|
androidTestImplementation "androidx.test.espresso:espresso-core:$androidxEspressoCoreVersion"
|