@capgo/capacitor-llm 8.0.11 → 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/README.md +86 -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 +401 -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,13 +31,13 @@ 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 through Swift Package Manager.
|
|
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.
|
|
@@ -55,13 +55,13 @@ use_frameworks! :linkage => :static
|
|
|
55
55
|
# And add this to your post_install hook:
|
|
56
56
|
post_install do |installer|
|
|
57
57
|
assertDeploymentTarget(installer)
|
|
58
|
-
|
|
58
|
+
|
|
59
59
|
# Fix for static framework dependencies
|
|
60
60
|
installer.pods_project.targets.each do |target|
|
|
61
61
|
target.build_configurations.each do |config|
|
|
62
62
|
config.build_settings['BUILD_LIBRARY_FOR_DISTRIBUTION'] = 'YES'
|
|
63
63
|
end
|
|
64
|
-
|
|
64
|
+
|
|
65
65
|
# Specifically for MediaPipe pods
|
|
66
66
|
if target.name.include?('MediaPipeTasksGenAI')
|
|
67
67
|
target.build_configurations.each do |config|
|
|
@@ -73,181 +73,83 @@ end
|
|
|
73
73
|
```
|
|
74
74
|
|
|
75
75
|
**Using Swift Package Manager:**
|
|
76
|
-
|
|
77
|
-
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:
|
|
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
|
|
85
|
-
|
|
86
|
-
**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.
|
|
87
|
-
|
|
88
|
-
**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.
|
|
89
77
|
|
|
90
78
|
## Adding a Model to Your App
|
|
91
79
|
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
On iOS, you have two options:
|
|
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.
|
|
95
81
|
|
|
96
|
-
|
|
97
|
-
2. **Custom Models**: Use your own models via MediaPipe (requires CocoaPods) - Note: Some model formats may have compatibility issues
|
|
82
|
+
### ExecuTorch Models (iOS and Android)
|
|
98
83
|
|
|
99
|
-
|
|
84
|
+
iOS uses ExecuTorch when the app target links the ExecuTorch SwiftPM products. Android uses the ExecuTorch Maven package.
|
|
100
85
|
|
|
101
|
-
|
|
86
|
+
Bundle the model files:
|
|
102
87
|
|
|
103
|
-
|
|
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/...`.
|
|
104
90
|
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
### Android (Working ✅)
|
|
110
|
-
- **Model**: Gemma-3 models (270M, 1B, etc.)
|
|
111
|
-
- **Format**: `.task` + `.litertlm` files
|
|
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`
|
|
91
|
+
```typescript
|
|
92
|
+
import { Capacitor } from '@capacitor/core';
|
|
93
|
+
import { CapgoLLM } from '@capgo/capacitor-llm';
|
|
115
94
|
|
|
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.
|
|
95
|
+
const isAndroid = Capacitor.getPlatform() === 'android';
|
|
126
96
|
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
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
|
+
});
|
|
131
105
|
|
|
132
|
-
|
|
106
|
+
const { id: chatId } = await CapgoLLM.createChat();
|
|
107
|
+
```
|
|
133
108
|
|
|
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/`
|
|
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.
|
|
139
110
|
|
|
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
|
|
111
|
+
### MediaPipe Models
|
|
146
112
|
|
|
147
|
-
|
|
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.
|
|
148
114
|
|
|
149
115
|
```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',
|
|
116
|
+
await CapgoLLM.setModel({
|
|
117
|
+
path: '/android_asset/gemma-3-270m-it-int8.task',
|
|
158
118
|
modelType: 'task',
|
|
159
|
-
maxTokens:
|
|
119
|
+
maxTokens: 2048,
|
|
120
|
+
topk: 40,
|
|
121
|
+
temperature: 0.8,
|
|
160
122
|
});
|
|
161
|
-
|
|
162
|
-
// Now you can create chats and send messages
|
|
163
|
-
const chat = await CapgoLLM.createChat();
|
|
164
123
|
```
|
|
165
124
|
|
|
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.
|
|
125
|
+
### Apple Intelligence
|
|
169
126
|
|
|
170
|
-
|
|
171
|
-
https://developer.android.com/ai/gemini-nano/experimental
|
|
127
|
+
On supported iOS devices, Apple Intelligence can be used without bundling a model.
|
|
172
128
|
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
}
|
|
129
|
+
```typescript
|
|
130
|
+
await CapgoLLM.setModel({
|
|
131
|
+
path: 'Apple Intelligence',
|
|
132
|
+
engine: 'apple',
|
|
133
|
+
});
|
|
179
134
|
```
|
|
180
135
|
|
|
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**:
|
|
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.
|
|
216
139
|
|
|
217
140
|
```typescript
|
|
218
|
-
|
|
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
|
+
});
|
|
219
146
|
|
|
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();
|
|
147
|
+
await CapgoLLM.setModel({
|
|
148
|
+
engine: 'executorch',
|
|
149
|
+
path: result.path,
|
|
150
|
+
tokenizerPath: result.companionPath,
|
|
151
|
+
sequenceLength: 2048,
|
|
152
|
+
});
|
|
251
153
|
```
|
|
252
154
|
|
|
253
155
|
## Usage Example
|
|
@@ -255,34 +157,22 @@ const chat = await CapgoLLM.createChat();
|
|
|
255
157
|
```typescript
|
|
256
158
|
import { CapgoLLM } from '@capgo/capacitor-llm';
|
|
257
159
|
|
|
258
|
-
// Check if LLM is ready
|
|
259
160
|
const { readiness } = await CapgoLLM.getReadiness();
|
|
260
161
|
console.log('LLM readiness:', readiness);
|
|
261
162
|
|
|
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
163
|
const { id: chatId } = await CapgoLLM.createChat();
|
|
271
164
|
|
|
272
|
-
// Listen for AI responses
|
|
273
165
|
CapgoLLM.addListener('textFromAi', (event) => {
|
|
274
166
|
console.log('AI:', event.text);
|
|
275
167
|
});
|
|
276
168
|
|
|
277
|
-
// Listen for completion
|
|
278
169
|
CapgoLLM.addListener('aiFinished', (event) => {
|
|
279
170
|
console.log('AI finished responding to chat:', event.chatId);
|
|
280
171
|
});
|
|
281
172
|
|
|
282
|
-
// Send a message
|
|
283
173
|
await CapgoLLM.sendMessage({
|
|
284
174
|
chatId,
|
|
285
|
-
message: 'Hello! How are you today?'
|
|
175
|
+
message: 'Hello! How are you today?',
|
|
286
176
|
});
|
|
287
177
|
```
|
|
288
178
|
|
|
@@ -357,8 +247,8 @@ setModel(options: ModelOptions) => Promise<void>
|
|
|
357
247
|
```
|
|
358
248
|
|
|
359
249
|
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)
|
|
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)
|
|
362
252
|
|
|
363
253
|
| Param | Type | Description |
|
|
364
254
|
| ------------- | ----------------------------------------------------- | ------------------------- |
|
|
@@ -480,14 +370,18 @@ Get the native Capacitor plugin version.
|
|
|
480
370
|
|
|
481
371
|
Model configuration options
|
|
482
372
|
|
|
483
|
-
| Prop
|
|
484
|
-
|
|
|
485
|
-
| **`path`**
|
|
486
|
-
| **`
|
|
487
|
-
| **`
|
|
488
|
-
| **`
|
|
489
|
-
| **`
|
|
490
|
-
| **`
|
|
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 |
|
|
491
385
|
|
|
492
386
|
|
|
493
387
|
#### DownloadModelResult
|
|
@@ -554,92 +448,27 @@ Event data for readiness status changes
|
|
|
554
448
|
|
|
555
449
|
## Example App Model Setup
|
|
556
450
|
|
|
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
|
|
564
|
-
|
|
565
|
-
#### Android Setup (Gemma 3 270M) ✅
|
|
566
|
-
|
|
567
|
-
1. **Create Kaggle account and accept license:**
|
|
568
|
-
- Create a [Kaggle account](https://www.kaggle.com)
|
|
569
|
-
- Visit [Gemma 3 models](https://www.kaggle.com/models/google/gemma-3) and accept terms
|
|
570
|
-
|
|
571
|
-
2. **Download the model:**
|
|
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`
|
|
576
|
-
|
|
577
|
-
3. **Place in Android app:**
|
|
578
|
-
- Copy BOTH files to: `example-app/android/app/src/main/assets/`
|
|
579
|
-
- In code, reference with `/android_asset/` prefix
|
|
580
|
-
|
|
581
|
-
#### iOS Setup ⚠️
|
|
451
|
+
The example app can use either the older MediaPipe assets or the new ExecuTorch assets.
|
|
582
452
|
|
|
583
|
-
|
|
584
|
-
- No model download needed
|
|
585
|
-
- Works out of the box on iOS 26.0+
|
|
453
|
+
### Recommended Custom Path: ExecuTorch
|
|
586
454
|
|
|
587
|
-
|
|
588
|
-
|
|
589
|
-
|
|
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
|
-
});
|
|
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`.
|
|
601
458
|
|
|
602
|
-
|
|
603
|
-
await CapgoLLM.setModel({
|
|
604
|
-
path: 'Apple Intelligence'
|
|
605
|
-
});
|
|
459
|
+
### Legacy MediaPipe Path
|
|
606
460
|
|
|
607
|
-
|
|
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
|
-
```
|
|
461
|
+
Android still supports Gemma 3 LiteRT assets from Kaggle. Download both files and place them in `example-app/android/app/src/main/assets/`:
|
|
616
462
|
|
|
617
|
-
|
|
463
|
+
- `gemma-3-270m-it-int8.task`
|
|
464
|
+
- `gemma-3-270m-it-int8.litertlm`
|
|
618
465
|
|
|
619
|
-
|
|
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
|
|
466
|
+
iOS MediaPipe remains experimental because some `.task` models can fail during prefill. Apple Intelligence or ExecuTorch is preferred on iOS.
|
|
630
467
|
|
|
631
468
|
## Known Issues
|
|
632
469
|
|
|
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
|
|
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"
|