@capgo/capacitor-llm 8.0.11 → 8.0.13
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/README.md +95 -254
- 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 +406 -2
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -9,7 +9,7 @@
|
|
|
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
13
|
|
|
14
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.
|
|
15
15
|
|
|
@@ -31,17 +31,19 @@ The most complete doc is available here: https://capgo.app/docs/plugins/llm/
|
|
|
31
31
|
## Installation
|
|
32
32
|
|
|
33
33
|
```bash
|
|
34
|
-
|
|
35
|
-
|
|
34
|
+
bun add @capgo/capacitor-llm
|
|
35
|
+
bunx cap sync
|
|
36
36
|
```
|
|
37
37
|
|
|
38
38
|
### iOS Additional Setup for Custom Models
|
|
39
39
|
|
|
40
|
-
|
|
40
|
+
Apple Intelligence works without bundled model files on supported iOS versions. For custom models, the plugin supports MediaPipe through CocoaPods and ExecuTorch when the host app links ExecuTorch with Swift Package Manager. The plugin package remains iOS 15 compatible; iOS ExecuTorch requires an iOS 17 or newer app target because ExecuTorch requires iOS 17.
|
|
41
41
|
|
|
42
42
|
**Using CocoaPods:**
|
|
43
43
|
The MediaPipe dependencies are already configured in the podspec. Make sure to run `pod install` after adding the plugin.
|
|
44
44
|
|
|
45
|
+
ExecuTorch is not provided by the CocoaPods integration. CocoaPods installs MediaPipe only. CocoaPods builds reject `engine: 'executorch'` with a clear runtime error unless the app also links ExecuTorch separately.
|
|
46
|
+
|
|
45
47
|
**Note about Static Framework Warning:**
|
|
46
48
|
When running `pod install`, you may see a warning about transitive dependencies with statically linked binaries. To fix this, update your Podfile:
|
|
47
49
|
|
|
@@ -55,13 +57,13 @@ use_frameworks! :linkage => :static
|
|
|
55
57
|
# And add this to your post_install hook:
|
|
56
58
|
post_install do |installer|
|
|
57
59
|
assertDeploymentTarget(installer)
|
|
58
|
-
|
|
60
|
+
|
|
59
61
|
# Fix for static framework dependencies
|
|
60
62
|
installer.pods_project.targets.each do |target|
|
|
61
63
|
target.build_configurations.each do |config|
|
|
62
64
|
config.build_settings['BUILD_LIBRARY_FOR_DISTRIBUTION'] = 'YES'
|
|
63
65
|
end
|
|
64
|
-
|
|
66
|
+
|
|
65
67
|
# Specifically for MediaPipe pods
|
|
66
68
|
if target.name.include?('MediaPipeTasksGenAI')
|
|
67
69
|
target.build_configurations.each do |config|
|
|
@@ -73,181 +75,93 @@ end
|
|
|
73
75
|
```
|
|
74
76
|
|
|
75
77
|
**Using Swift Package Manager:**
|
|
78
|
+
The plugin's `Package.swift` does not link ExecuTorch directly because that would force every Swift Package Manager user onto iOS 17 or newer, even when they only use Apple Intelligence or MediaPipe.
|
|
76
79
|
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
**To add MediaPipe GenAI via SPM:**
|
|
80
|
-
1. Open your app project in Xcode
|
|
81
|
-
2. Go to **File > Add Package Dependencies...**
|
|
82
|
-
3. Enter the URL: `https://github.com/paescebu/SwiftTasksGenAI.git`
|
|
83
|
-
4. Select your desired version (e.g., 0.10.27)
|
|
84
|
-
5. Add the `SwiftTasksGenAI` product to your app target
|
|
80
|
+
To use ExecuTorch on iOS, add it to your app target:
|
|
85
81
|
|
|
86
|
-
|
|
82
|
+
1. Set the app target deployment version to iOS 17 or newer.
|
|
83
|
+
2. In Xcode, open the app project, select **Package Dependencies**, and add `https://github.com/pytorch/executorch.git`.
|
|
84
|
+
3. Use the `swiftpm-1.2.0` branch, or the ExecuTorch branch that matches the runtime version you want.
|
|
85
|
+
4. Add these products to the app target: `executorch_llm`, `backend_xnnpack`, `kernels_llm`, `kernels_optimized`, `kernels_quantized`, and `kernels_torchao`.
|
|
86
|
+
5. Add the `.pte` model and tokenizer file to the app target's Copy Bundle Resources, or download them at runtime.
|
|
87
87
|
|
|
88
|
-
|
|
88
|
+
If those ExecuTorch products are not linked into the app target, `setModel({ engine: 'executorch' })` rejects with a clear runtime error. MediaPipe GenAI still does not officially support SPM, so use CocoaPods for MediaPipe `.task` models.
|
|
89
89
|
|
|
90
90
|
## Adding a Model to Your App
|
|
91
91
|
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
On iOS, you have two options:
|
|
95
|
-
|
|
96
|
-
1. **Apple Intelligence (Default)**: Uses the built-in system model (requires iOS 26.0+) - Recommended
|
|
97
|
-
2. **Custom Models**: Use your own models via MediaPipe (requires CocoaPods) - Note: Some model formats may have compatibility issues
|
|
98
|
-
|
|
99
|
-
#### Using Apple Intelligence (Default)
|
|
92
|
+
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.
|
|
100
93
|
|
|
101
|
-
|
|
94
|
+
### ExecuTorch Models (iOS and Android)
|
|
102
95
|
|
|
103
|
-
|
|
96
|
+
iOS uses ExecuTorch only when the host app links the ExecuTorch Swift Package products. It is not included by CocoaPods or by the plugin's `Package.swift`, so an app without those products can use Apple Intelligence or MediaPipe but will refuse `engine: 'executorch'`. Android uses the ExecuTorch Maven package.
|
|
104
97
|
|
|
105
|
-
|
|
98
|
+
Bundle the model files:
|
|
106
99
|
|
|
107
|
-
|
|
100
|
+
- iOS: add the `.pte` model and tokenizer file to your app target's Copy Bundle Resources.
|
|
101
|
+
- Android: place the `.pte` model and tokenizer file under `android/app/src/main/assets/`, then reference them with `/android_asset/...`.
|
|
108
102
|
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
- **Where to download**:
|
|
113
|
-
- [Kaggle Gemma models](https://www.kaggle.com/models/google/gemma) - "LiteRT (formerly TFLite)" tab
|
|
114
|
-
- Example: `gemma-3-270m-it-int8.task` + `gemma-3-270m-it-int8.litertlm`
|
|
103
|
+
```typescript
|
|
104
|
+
import { Capacitor } from '@capacitor/core';
|
|
105
|
+
import { CapgoLLM } from '@capgo/capacitor-llm';
|
|
115
106
|
|
|
116
|
-
|
|
117
|
-
- **Recommended**: Use Apple Intelligence (built-in, no download needed)
|
|
118
|
-
- **Alternative (Experimental)**:
|
|
119
|
-
- **Model**: Gemma-2 2B (documented as compatible but may still fail)
|
|
120
|
-
- **Format**: `.task` files (`.bin` format preferred but not available)
|
|
121
|
-
- **Where to download**:
|
|
122
|
-
- [Hugging Face MediaPipe models](https://huggingface.co/collections/google/mediapipe-668392ead2d6768e82fb3b87)
|
|
123
|
-
- Look for `Gemma2-2B-IT_*_ekv*.task` files
|
|
124
|
-
- Example: `Gemma2-2B-IT_multi-prefill-seq_q8_ekv1280.task`
|
|
125
|
-
- **Note**: Even with Gemma-2 2B, you may still encounter errors. Apple Intelligence is more reliable.
|
|
107
|
+
const isAndroid = Capacitor.getPlatform() === 'android';
|
|
126
108
|
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
109
|
+
await CapgoLLM.setModel({
|
|
110
|
+
engine: 'executorch',
|
|
111
|
+
path: isAndroid ? '/android_asset/model.pte' : 'model.pte',
|
|
112
|
+
tokenizerPath: isAndroid ? '/android_asset/tokenizer.model' : 'tokenizer.model',
|
|
113
|
+
maxTokens: 2048,
|
|
114
|
+
sequenceLength: 2048,
|
|
115
|
+
temperature: 0.8,
|
|
116
|
+
});
|
|
131
117
|
|
|
132
|
-
|
|
118
|
+
const { id: chatId } = await CapgoLLM.createChat();
|
|
119
|
+
```
|
|
133
120
|
|
|
134
|
-
|
|
135
|
-
- Go to [Kaggle Gemma models](https://www.kaggle.com/models/google/gemma)
|
|
136
|
-
- Click "LiteRT (formerly TFLite)" tab
|
|
137
|
-
- Download both `.task` and `.litertlm` files
|
|
138
|
-
- Place in `android/app/src/main/assets/`
|
|
121
|
+
`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.
|
|
139
122
|
|
|
140
|
-
|
|
141
|
-
- Visit [Hugging Face MediaPipe collection](https://huggingface.co/collections/google/mediapipe-668392ead2d6768e82fb3b87)
|
|
142
|
-
- Find Gemma-2 2B models
|
|
143
|
-
- Download `.task` files (and `.litertlm` if available)
|
|
144
|
-
- Add to Xcode project in "Copy Bundle Resources"
|
|
145
|
-
- Note: Success is not guaranteed due to format compatibility issues
|
|
123
|
+
### MediaPipe Models
|
|
146
124
|
|
|
147
|
-
|
|
125
|
+
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.
|
|
148
126
|
|
|
149
127
|
```typescript
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
// iOS - Use Apple Intelligence (default)
|
|
153
|
-
await CapgoLLM.setModel({ path: 'Apple Intelligence' });
|
|
154
|
-
|
|
155
|
-
// iOS - Use MediaPipe model (experimental)
|
|
156
|
-
await CapgoLLM.setModel({
|
|
157
|
-
path: 'Gemma2-2B-IT_multi-prefill-seq_q8_ekv1280',
|
|
128
|
+
await CapgoLLM.setModel({
|
|
129
|
+
path: '/android_asset/gemma-3-270m-it-int8.task',
|
|
158
130
|
modelType: 'task',
|
|
159
|
-
maxTokens:
|
|
131
|
+
maxTokens: 2048,
|
|
132
|
+
topk: 40,
|
|
133
|
+
temperature: 0.8,
|
|
160
134
|
});
|
|
161
|
-
|
|
162
|
-
// Now you can create chats and send messages
|
|
163
|
-
const chat = await CapgoLLM.createChat();
|
|
164
135
|
```
|
|
165
136
|
|
|
166
|
-
###
|
|
167
|
-
|
|
168
|
-
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.
|
|
137
|
+
### Apple Intelligence
|
|
169
138
|
|
|
170
|
-
|
|
171
|
-
https://developer.android.com/ai/gemini-nano/experimental
|
|
139
|
+
On supported iOS devices, Apple Intelligence can be used without bundling a model.
|
|
172
140
|
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
}
|
|
141
|
+
```typescript
|
|
142
|
+
await CapgoLLM.setModel({
|
|
143
|
+
path: 'Apple Intelligence',
|
|
144
|
+
engine: 'apple',
|
|
145
|
+
});
|
|
179
146
|
```
|
|
180
147
|
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
- **Recommended**: Gemma 3 270M - Smallest, most efficient (~240-400MB)
|
|
185
|
-
- Text-only model optimized for on-device inference
|
|
186
|
-
- Download from [Kaggle](https://www.kaggle.com/models/google/gemma) → "LiteRT (formerly TFLite)" tab
|
|
187
|
-
- Get both `.task` and `.litertlm` files
|
|
188
|
-
|
|
189
|
-
**For iOS: Limited Options** (⚠️)
|
|
190
|
-
- **Option 1** (Recommended): Use Apple Intelligence - no download needed
|
|
191
|
-
- **Option 2** (Experimental): Try Gemma-2 2B from [Hugging Face](https://huggingface.co/collections/google/mediapipe-668392ead2d6768e82fb3b87)
|
|
192
|
-
- Download `Gemma2-2B-IT_*.task` files
|
|
193
|
-
- May still encounter compatibility errors
|
|
194
|
-
|
|
195
|
-
**Available Model Sizes** (from [Google AI](https://ai.google.dev/gemma/docs/core#sizes)):
|
|
196
|
-
- `gemma-3-270m` - Most portable, text-only (~240-400MB) - Android only
|
|
197
|
-
- `gemma-3-1b` - Larger text-only (~892MB-1.5GB) - Android only
|
|
198
|
-
- `gemma-2-2b` - Cross-platform compatible (~1-2GB) - iOS experimental
|
|
199
|
-
- Larger models available but not recommended for mobile
|
|
200
|
-
|
|
201
|
-
For automated download, see the script in the example app section below.
|
|
202
|
-
|
|
203
|
-
2. **Add the model to your app**:
|
|
204
|
-
|
|
205
|
-
**Option A - Bundle in APK (for smaller models)**:
|
|
206
|
-
- Place the model files in your app's `android/app/src/main/assets/` directory
|
|
207
|
-
- The `/android_asset/` prefix is used to reference files from the assets folder
|
|
208
|
-
- Note: This approach may increase APK size significantly
|
|
209
|
-
|
|
210
|
-
**Option B - Download at runtime (recommended for production)**:
|
|
211
|
-
- Host the model files on a server
|
|
212
|
-
- Download them to your app's files directory at runtime
|
|
213
|
-
- This keeps your APK size small
|
|
214
|
-
|
|
215
|
-
3. **Set the model path**:
|
|
148
|
+
### Downloading Models at Runtime
|
|
149
|
+
|
|
150
|
+
Use `downloadModel` to keep large model files out of the app bundle. For ExecuTorch, `companionUrl` can point at the tokenizer file.
|
|
216
151
|
|
|
217
152
|
```typescript
|
|
218
|
-
|
|
153
|
+
const result = await CapgoLLM.downloadModel({
|
|
154
|
+
url: 'https://your-server.com/models/model.pte',
|
|
155
|
+
companionUrl: 'https://your-server.com/models/tokenizer.model',
|
|
156
|
+
filename: 'model.pte',
|
|
157
|
+
});
|
|
219
158
|
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
// Example: Download model at runtime (recommended for production)
|
|
227
|
-
async function downloadModel() {
|
|
228
|
-
// Add progress listener
|
|
229
|
-
await CapgoLLM.addListener('downloadProgress', (event) => {
|
|
230
|
-
console.log(`Download progress: ${event.progress}%`);
|
|
231
|
-
});
|
|
232
|
-
|
|
233
|
-
// Download the model
|
|
234
|
-
const result = await CapgoLLM.downloadModel({
|
|
235
|
-
url: 'https://your-server.com/models/gemma-3-270m-it-int8.task',
|
|
236
|
-
companionUrl: 'https://your-server.com/models/gemma-3-270m-it-int8.litertlm', // Android only
|
|
237
|
-
filename: 'gemma-3-270m-it-int8.task' // Optional, defaults to filename from URL
|
|
238
|
-
});
|
|
239
|
-
|
|
240
|
-
console.log('Model downloaded to:', result.path);
|
|
241
|
-
if (result.companionPath) {
|
|
242
|
-
console.log('Companion file downloaded to:', result.companionPath);
|
|
243
|
-
}
|
|
244
|
-
|
|
245
|
-
// Now set the model path
|
|
246
|
-
await CapgoLLM.setModelPath({ path: result.path });
|
|
247
|
-
}
|
|
248
|
-
|
|
249
|
-
// Now you can create chats and send messages
|
|
250
|
-
const chat = await CapgoLLM.createChat();
|
|
159
|
+
await CapgoLLM.setModel({
|
|
160
|
+
engine: 'executorch',
|
|
161
|
+
path: result.path,
|
|
162
|
+
tokenizerPath: result.companionPath,
|
|
163
|
+
sequenceLength: 2048,
|
|
164
|
+
});
|
|
251
165
|
```
|
|
252
166
|
|
|
253
167
|
## Usage Example
|
|
@@ -255,34 +169,22 @@ const chat = await CapgoLLM.createChat();
|
|
|
255
169
|
```typescript
|
|
256
170
|
import { CapgoLLM } from '@capgo/capacitor-llm';
|
|
257
171
|
|
|
258
|
-
// Check if LLM is ready
|
|
259
172
|
const { readiness } = await CapgoLLM.getReadiness();
|
|
260
173
|
console.log('LLM readiness:', readiness);
|
|
261
174
|
|
|
262
|
-
// Set a custom model (optional - uses system default if not called)
|
|
263
|
-
// iOS: Use a GGUF model from bundle or path
|
|
264
|
-
// Android: Use a MediaPipe model from assets or files
|
|
265
|
-
await CapgoLLM.setModelPath({
|
|
266
|
-
path: Platform.isIOS ? 'gemma-3-270m.gguf' : '/android_asset/gemma-3-270m-it-int8.task'
|
|
267
|
-
});
|
|
268
|
-
|
|
269
|
-
// Create a chat session
|
|
270
175
|
const { id: chatId } = await CapgoLLM.createChat();
|
|
271
176
|
|
|
272
|
-
// Listen for AI responses
|
|
273
177
|
CapgoLLM.addListener('textFromAi', (event) => {
|
|
274
178
|
console.log('AI:', event.text);
|
|
275
179
|
});
|
|
276
180
|
|
|
277
|
-
// Listen for completion
|
|
278
181
|
CapgoLLM.addListener('aiFinished', (event) => {
|
|
279
182
|
console.log('AI finished responding to chat:', event.chatId);
|
|
280
183
|
});
|
|
281
184
|
|
|
282
|
-
// Send a message
|
|
283
185
|
await CapgoLLM.sendMessage({
|
|
284
186
|
chatId,
|
|
285
|
-
message: 'Hello! How are you today?'
|
|
187
|
+
message: 'Hello! How are you today?',
|
|
286
188
|
});
|
|
287
189
|
```
|
|
288
190
|
|
|
@@ -357,8 +259,8 @@ setModel(options: ModelOptions) => Promise<void>
|
|
|
357
259
|
```
|
|
358
260
|
|
|
359
261
|
Sets the model configuration
|
|
360
|
-
- iOS: Use "Apple Intelligence" as path for system model, or
|
|
361
|
-
- Android: Path to a MediaPipe model file (in assets or files directory)
|
|
262
|
+
- iOS: Use "Apple Intelligence" as path for system model, provide a MediaPipe model, or set engine to "executorch"
|
|
263
|
+
- Android: Path to a MediaPipe or ExecuTorch model file (in assets or files directory)
|
|
362
264
|
|
|
363
265
|
| Param | Type | Description |
|
|
364
266
|
| ------------- | ----------------------------------------------------- | ------------------------- |
|
|
@@ -480,14 +382,18 @@ Get the native Capacitor plugin version.
|
|
|
480
382
|
|
|
481
383
|
Model configuration options
|
|
482
384
|
|
|
483
|
-
| Prop
|
|
484
|
-
|
|
|
485
|
-
| **`path`**
|
|
486
|
-
| **`
|
|
487
|
-
| **`
|
|
488
|
-
| **`
|
|
489
|
-
| **`
|
|
490
|
-
| **`
|
|
385
|
+
| Prop | Type | Description |
|
|
386
|
+
| -------------------- | ------------------------------------------------------------- | --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
|
|
387
|
+
| **`path`** | <code>string</code> | Model path or "Apple Intelligence" for iOS system model |
|
|
388
|
+
| **`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 |
|
|
389
|
+
| **`modelType`** | <code>string</code> | Model file type/extension (e.g., "task", "bin", "litertlm"). If not provided, will be extracted from path. |
|
|
390
|
+
| **`tokenizerPath`** | <code>string</code> | Tokenizer path for ExecuTorch models. Required when engine is "executorch". |
|
|
391
|
+
| **`specialTokens`** | <code>string[]</code> | Optional special tokens passed to iOS ExecuTorch tokenizers. |
|
|
392
|
+
| **`maxTokens`** | <code>number</code> | Maximum number of tokens the model handles |
|
|
393
|
+
| **`sequenceLength`** | <code>number</code> | Sequence length for ExecuTorch generation. Defaults to maxTokens when omitted. |
|
|
394
|
+
| **`topk`** | <code>number</code> | Number of tokens the model considers at each step |
|
|
395
|
+
| **`temperature`** | <code>number</code> | Amount of randomness in generation (0.0-1.0) |
|
|
396
|
+
| **`randomSeed`** | <code>number</code> | Random seed for generation |
|
|
491
397
|
|
|
492
398
|
|
|
493
399
|
#### DownloadModelResult
|
|
@@ -554,92 +460,27 @@ Event data for readiness status changes
|
|
|
554
460
|
|
|
555
461
|
## Example App Model Setup
|
|
556
462
|
|
|
557
|
-
The example app
|
|
558
|
-
|
|
559
|
-
### Downloading Models
|
|
560
|
-
|
|
561
|
-
Since AI models require license acceptance, you need to download them manually:
|
|
562
|
-
|
|
563
|
-
### Model Setup by Platform
|
|
463
|
+
The example app can use either the older MediaPipe assets or the new ExecuTorch assets.
|
|
564
464
|
|
|
565
|
-
|
|
465
|
+
### Recommended Custom Path: ExecuTorch
|
|
566
466
|
|
|
567
|
-
1.
|
|
568
|
-
|
|
569
|
-
|
|
467
|
+
1. Export or download an ExecuTorch `.pte` model and matching tokenizer file.
|
|
468
|
+
2. Add both files to the iOS app bundle, or place both files in `example-app/android/app/src/main/assets/` for Android.
|
|
469
|
+
3. Call `setModel` with `engine: 'executorch'`, `path`, and `tokenizerPath`.
|
|
570
470
|
|
|
571
|
-
|
|
572
|
-
- Click "LiteRT (formerly TFLite)" tab
|
|
573
|
-
- Download `gemma-3-270m-it-int8` (get BOTH files):
|
|
574
|
-
- `gemma-3-270m-it-int8.task`
|
|
575
|
-
- `gemma-3-270m-it-int8.litertlm`
|
|
471
|
+
### Legacy MediaPipe Path
|
|
576
472
|
|
|
577
|
-
3.
|
|
578
|
-
- Copy BOTH files to: `example-app/android/app/src/main/assets/`
|
|
579
|
-
- In code, reference with `/android_asset/` prefix
|
|
473
|
+
Android still supports Gemma 3 LiteRT assets from Kaggle. Download both files and place them in `example-app/android/app/src/main/assets/`:
|
|
580
474
|
|
|
581
|
-
|
|
475
|
+
- `gemma-3-270m-it-int8.task`
|
|
476
|
+
- `gemma-3-270m-it-int8.litertlm`
|
|
582
477
|
|
|
583
|
-
|
|
584
|
-
- No model download needed
|
|
585
|
-
- Works out of the box on iOS 26.0+
|
|
586
|
-
|
|
587
|
-
**Option 2 (Experimental)**: Try Gemma-2 2B
|
|
588
|
-
1. Visit [Hugging Face MediaPipe models](https://huggingface.co/collections/google/mediapipe-668392ead2d6768e82fb3b87)
|
|
589
|
-
2. Download Gemma-2 2B files (e.g., `Gemma2-2B-IT_multi-prefill-seq_q8_ekv1280.task`)
|
|
590
|
-
3. Add to Xcode project in "Copy Bundle Resources"
|
|
591
|
-
4. Note: May still encounter errors - Apple Intelligence is more reliable
|
|
592
|
-
### Update your code to use the model:
|
|
593
|
-
```typescript
|
|
594
|
-
// Android - Gemma 3 270M
|
|
595
|
-
await CapgoLLM.setModel({
|
|
596
|
-
path: '/android_asset/gemma-3-270m-it-int8.task',
|
|
597
|
-
maxTokens: 2048,
|
|
598
|
-
topk: 40,
|
|
599
|
-
temperature: 0.8
|
|
600
|
-
});
|
|
601
|
-
|
|
602
|
-
// iOS Option 1 - Apple Intelligence (Recommended)
|
|
603
|
-
await CapgoLLM.setModel({
|
|
604
|
-
path: 'Apple Intelligence'
|
|
605
|
-
});
|
|
606
|
-
|
|
607
|
-
// iOS Option 2 - Gemma-2 2B (Experimental)
|
|
608
|
-
await CapgoLLM.setModel({
|
|
609
|
-
path: 'Gemma2-2B-IT_multi-prefill-seq_q8_ekv1280',
|
|
610
|
-
modelType: 'task',
|
|
611
|
-
maxTokens: 1280,
|
|
612
|
-
topk: 40,
|
|
613
|
-
temperature: 0.8
|
|
614
|
-
});
|
|
615
|
-
```
|
|
616
|
-
|
|
617
|
-
### Model Selection Guide
|
|
618
|
-
|
|
619
|
-
**For Android**: Gemma 3 270M is recommended because:
|
|
620
|
-
- Smallest size (~240-400MB)
|
|
621
|
-
- Text-only (perfect for chat)
|
|
622
|
-
- Optimized for mobile devices
|
|
623
|
-
- Works reliably with MediaPipe
|
|
624
|
-
|
|
625
|
-
**For iOS**: Apple Intelligence is recommended because:
|
|
626
|
-
- No download required
|
|
627
|
-
- Native iOS integration
|
|
628
|
-
- Better performance
|
|
629
|
-
- No compatibility issues
|
|
478
|
+
iOS MediaPipe remains experimental because some `.task` models can fail during prefill. Apple Intelligence or ExecuTorch is preferred on iOS.
|
|
630
479
|
|
|
631
480
|
## Known Issues
|
|
632
481
|
|
|
633
|
-
-
|
|
634
|
-
|
|
635
|
-
|
|
636
|
-
|
|
637
|
-
|
|
638
|
-
- **Platform Requirements**:
|
|
639
|
-
- iOS: Apple Intelligence requires iOS 26.0+
|
|
640
|
-
- Android: Minimum SDK 24
|
|
641
|
-
|
|
642
|
-
- **Performance Considerations**:
|
|
643
|
-
- Model files are large (300MB-2GB)
|
|
644
|
-
- Initial download takes time
|
|
645
|
-
- Some devices may have memory limitations
|
|
482
|
+
- ExecuTorch is native-only and is not available on web.
|
|
483
|
+
- iOS ExecuTorch requires linking the ExecuTorch SwiftPM products in the app target, and that app target must support iOS 17 or newer.
|
|
484
|
+
- Apple Intelligence requires iOS 26.0 or later and a supported device.
|
|
485
|
+
- Android requires minSdkVersion 24 or higher.
|
|
486
|
+
- 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"
|