llama-cpp-capacitor 0.0.1

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.
@@ -0,0 +1,482 @@
1
+ package ai.annadata.plugin.capacitor;
2
+
3
+ import com.getcapacitor.JSObject;
4
+ import com.getcapacitor.Plugin;
5
+ import com.getcapacitor.PluginCall;
6
+ import com.getcapacitor.PluginMethod;
7
+ import com.getcapacitor.annotation.CapacitorPlugin;
8
+
9
+ @CapacitorPlugin(name = "LlamaCpp")
10
+ public class LlamaCppPlugin extends Plugin {
11
+
12
+ private LlamaCpp implementation = new LlamaCpp();
13
+
14
+ // MARK: - Core initialization and management
15
+
16
+ @PluginMethod
17
+ public void toggleNativeLog(PluginCall call) {
18
+ boolean enabled = call.getBoolean("enabled", false);
19
+ implementation.toggleNativeLog(enabled, result -> {
20
+ if (result.isSuccess()) {
21
+ call.resolve();
22
+ } else {
23
+ call.reject(result.getError().getMessage());
24
+ }
25
+ });
26
+ }
27
+
28
+ @PluginMethod
29
+ public void setContextLimit(PluginCall call) {
30
+ int limit = call.getInt("limit", 10);
31
+ implementation.setContextLimit(limit, result -> {
32
+ if (result.isSuccess()) {
33
+ call.resolve();
34
+ } else {
35
+ call.reject(result.getError().getMessage());
36
+ }
37
+ });
38
+ }
39
+
40
+ @PluginMethod
41
+ public void modelInfo(PluginCall call) {
42
+ String path = call.getString("path", "");
43
+ String[] skip = call.getArray("skip", String.class);
44
+ if (skip == null) skip = new String[0];
45
+
46
+ implementation.modelInfo(path, skip, result -> {
47
+ if (result.isSuccess()) {
48
+ call.resolve(result.getData());
49
+ } else {
50
+ call.reject(result.getError().getMessage());
51
+ }
52
+ });
53
+ }
54
+
55
+ @PluginMethod
56
+ public void initContext(PluginCall call) {
57
+ int contextId = call.getInt("contextId", 0);
58
+ JSObject params = call.getObject("params", new JSObject());
59
+
60
+ implementation.initContext(contextId, params, result -> {
61
+ if (result.isSuccess()) {
62
+ call.resolve(result.getData());
63
+ } else {
64
+ call.reject(result.getError().getMessage());
65
+ }
66
+ });
67
+ }
68
+
69
+ @PluginMethod
70
+ public void releaseContext(PluginCall call) {
71
+ int contextId = call.getInt("contextId", 0);
72
+
73
+ implementation.releaseContext(contextId, result -> {
74
+ if (result.isSuccess()) {
75
+ call.resolve();
76
+ } else {
77
+ call.reject(result.getError().getMessage());
78
+ }
79
+ });
80
+ }
81
+
82
+ @PluginMethod
83
+ public void releaseAllContexts(PluginCall call) {
84
+ implementation.releaseAllContexts(result -> {
85
+ if (result.isSuccess()) {
86
+ call.resolve();
87
+ } else {
88
+ call.reject(result.getError().getMessage());
89
+ }
90
+ });
91
+ }
92
+
93
+ // MARK: - Chat and completion
94
+
95
+ @PluginMethod
96
+ public void getFormattedChat(PluginCall call) {
97
+ int contextId = call.getInt("contextId", 0);
98
+ String messages = call.getString("messages", "");
99
+ String chatTemplate = call.getString("chatTemplate");
100
+ JSObject params = call.getObject("params");
101
+
102
+ implementation.getFormattedChat(contextId, messages, chatTemplate, params, result -> {
103
+ if (result.isSuccess()) {
104
+ call.resolve(result.getData());
105
+ } else {
106
+ call.reject(result.getError().getMessage());
107
+ }
108
+ });
109
+ }
110
+
111
+ @PluginMethod
112
+ public void completion(PluginCall call) {
113
+ int contextId = call.getInt("contextId", 0);
114
+ JSObject params = call.getObject("params", new JSObject());
115
+
116
+ implementation.completion(contextId, params, result -> {
117
+ if (result.isSuccess()) {
118
+ call.resolve(result.getData());
119
+ } else {
120
+ call.reject(result.getError().getMessage());
121
+ }
122
+ });
123
+ }
124
+
125
+ @PluginMethod
126
+ public void stopCompletion(PluginCall call) {
127
+ int contextId = call.getInt("contextId", 0);
128
+
129
+ implementation.stopCompletion(contextId, result -> {
130
+ if (result.isSuccess()) {
131
+ call.resolve();
132
+ } else {
133
+ call.reject(result.getError().getMessage());
134
+ }
135
+ });
136
+ }
137
+
138
+ // MARK: - Session management
139
+
140
+ @PluginMethod
141
+ public void loadSession(PluginCall call) {
142
+ int contextId = call.getInt("contextId", 0);
143
+ String filepath = call.getString("filepath", "");
144
+
145
+ implementation.loadSession(contextId, filepath, result -> {
146
+ if (result.isSuccess()) {
147
+ call.resolve(result.getData());
148
+ } else {
149
+ call.reject(result.getError().getMessage());
150
+ }
151
+ });
152
+ }
153
+
154
+ @PluginMethod
155
+ public void saveSession(PluginCall call) {
156
+ int contextId = call.getInt("contextId", 0);
157
+ String filepath = call.getString("filepath", "");
158
+ int size = call.getInt("size", -1);
159
+
160
+ implementation.saveSession(contextId, filepath, size, result -> {
161
+ if (result.isSuccess()) {
162
+ JSObject ret = new JSObject();
163
+ ret.put("tokensSaved", result.getData());
164
+ call.resolve(ret);
165
+ } else {
166
+ call.reject(result.getError().getMessage());
167
+ }
168
+ });
169
+ }
170
+
171
+ // MARK: - Tokenization
172
+
173
+ @PluginMethod
174
+ public void tokenize(PluginCall call) {
175
+ int contextId = call.getInt("contextId", 0);
176
+ String text = call.getString("text", "");
177
+ String[] imagePaths = call.getArray("imagePaths", String.class);
178
+ if (imagePaths == null) imagePaths = new String[0];
179
+
180
+ implementation.tokenize(contextId, text, imagePaths, result -> {
181
+ if (result.isSuccess()) {
182
+ call.resolve(result.getData());
183
+ } else {
184
+ call.reject(result.getError().getMessage());
185
+ }
186
+ });
187
+ }
188
+
189
+ @PluginMethod
190
+ public void detokenize(PluginCall call) {
191
+ int contextId = call.getInt("contextId", 0);
192
+ Integer[] tokens = call.getArray("tokens", Integer.class);
193
+ if (tokens == null) tokens = new Integer[0];
194
+
195
+ implementation.detokenize(contextId, tokens, result -> {
196
+ if (result.isSuccess()) {
197
+ JSObject ret = new JSObject();
198
+ ret.put("text", result.getData());
199
+ call.resolve(ret);
200
+ } else {
201
+ call.reject(result.getError().getMessage());
202
+ }
203
+ });
204
+ }
205
+
206
+ // MARK: - Embeddings and reranking
207
+
208
+ @PluginMethod
209
+ public void embedding(PluginCall call) {
210
+ int contextId = call.getInt("contextId", 0);
211
+ String text = call.getString("text", "");
212
+ JSObject params = call.getObject("params", new JSObject());
213
+
214
+ implementation.embedding(contextId, text, params, result -> {
215
+ if (result.isSuccess()) {
216
+ call.resolve(result.getData());
217
+ } else {
218
+ call.reject(result.getError().getMessage());
219
+ }
220
+ });
221
+ }
222
+
223
+ @PluginMethod
224
+ public void rerank(PluginCall call) {
225
+ int contextId = call.getInt("contextId", 0);
226
+ String query = call.getString("query", "");
227
+ String[] documents = call.getArray("documents", String.class);
228
+ if (documents == null) documents = new String[0];
229
+ JSObject params = call.getObject("params");
230
+
231
+ implementation.rerank(contextId, query, documents, params, result -> {
232
+ if (result.isSuccess()) {
233
+ JSObject ret = new JSObject();
234
+ ret.put("results", result.getData());
235
+ call.resolve(ret);
236
+ } else {
237
+ call.reject(result.getError().getMessage());
238
+ }
239
+ });
240
+ }
241
+
242
+ // MARK: - Benchmarking
243
+
244
+ @PluginMethod
245
+ public void bench(PluginCall call) {
246
+ int contextId = call.getInt("contextId", 0);
247
+ int pp = call.getInt("pp", 0);
248
+ int tg = call.getInt("tg", 0);
249
+ int pl = call.getInt("pl", 0);
250
+ int nr = call.getInt("nr", 0);
251
+
252
+ implementation.bench(contextId, pp, tg, pl, nr, result -> {
253
+ if (result.isSuccess()) {
254
+ JSObject ret = new JSObject();
255
+ ret.put("result", result.getData());
256
+ call.resolve(ret);
257
+ } else {
258
+ call.reject(result.getError().getMessage());
259
+ }
260
+ });
261
+ }
262
+
263
+ // MARK: - LoRA adapters
264
+
265
+ @PluginMethod
266
+ public void applyLoraAdapters(PluginCall call) {
267
+ int contextId = call.getInt("contextId", 0);
268
+ JSObject[] loraAdapters = call.getArray("loraAdapters", JSObject.class);
269
+ if (loraAdapters == null) loraAdapters = new JSObject[0];
270
+
271
+ implementation.applyLoraAdapters(contextId, loraAdapters, result -> {
272
+ if (result.isSuccess()) {
273
+ call.resolve();
274
+ } else {
275
+ call.reject(result.getError().getMessage());
276
+ }
277
+ });
278
+ }
279
+
280
+ @PluginMethod
281
+ public void removeLoraAdapters(PluginCall call) {
282
+ int contextId = call.getInt("contextId", 0);
283
+
284
+ implementation.removeLoraAdapters(contextId, result -> {
285
+ if (result.isSuccess()) {
286
+ call.resolve();
287
+ } else {
288
+ call.reject(result.getError().getMessage());
289
+ }
290
+ });
291
+ }
292
+
293
+ @PluginMethod
294
+ public void getLoadedLoraAdapters(PluginCall call) {
295
+ int contextId = call.getInt("contextId", 0);
296
+
297
+ implementation.getLoadedLoraAdapters(contextId, result -> {
298
+ if (result.isSuccess()) {
299
+ JSObject ret = new JSObject();
300
+ ret.put("adapters", result.getData());
301
+ call.resolve(ret);
302
+ } else {
303
+ call.reject(result.getError().getMessage());
304
+ }
305
+ });
306
+ }
307
+
308
+ // MARK: - Multimodal methods
309
+
310
+ @PluginMethod
311
+ public void initMultimodal(PluginCall call) {
312
+ int contextId = call.getInt("contextId", 0);
313
+ JSObject params = call.getObject("params", new JSObject());
314
+ String path = params.getString("path", "");
315
+ boolean useGpu = params.getBoolean("use_gpu", true);
316
+
317
+ implementation.initMultimodal(contextId, path, useGpu, result -> {
318
+ if (result.isSuccess()) {
319
+ JSObject ret = new JSObject();
320
+ ret.put("success", result.getData());
321
+ call.resolve(ret);
322
+ } else {
323
+ call.reject(result.getError().getMessage());
324
+ }
325
+ });
326
+ }
327
+
328
+ @PluginMethod
329
+ public void isMultimodalEnabled(PluginCall call) {
330
+ int contextId = call.getInt("contextId", 0);
331
+
332
+ implementation.isMultimodalEnabled(contextId, result -> {
333
+ if (result.isSuccess()) {
334
+ JSObject ret = new JSObject();
335
+ ret.put("enabled", result.getData());
336
+ call.resolve(ret);
337
+ } else {
338
+ call.reject(result.getError().getMessage());
339
+ }
340
+ });
341
+ }
342
+
343
+ @PluginMethod
344
+ public void getMultimodalSupport(PluginCall call) {
345
+ int contextId = call.getInt("contextId", 0);
346
+
347
+ implementation.getMultimodalSupport(contextId, result -> {
348
+ if (result.isSuccess()) {
349
+ call.resolve(result.getData());
350
+ } else {
351
+ call.reject(result.getError().getMessage());
352
+ }
353
+ });
354
+ }
355
+
356
+ @PluginMethod
357
+ public void releaseMultimodal(PluginCall call) {
358
+ int contextId = call.getInt("contextId", 0);
359
+
360
+ implementation.releaseMultimodal(contextId, result -> {
361
+ if (result.isSuccess()) {
362
+ call.resolve();
363
+ } else {
364
+ call.reject(result.getError().getMessage());
365
+ }
366
+ });
367
+ }
368
+
369
+ // MARK: - TTS methods
370
+
371
+ @PluginMethod
372
+ public void initVocoder(PluginCall call) {
373
+ int contextId = call.getInt("contextId", 0);
374
+ JSObject params = call.getObject("params", new JSObject());
375
+ String path = params.getString("path", "");
376
+ Integer nBatch = params.getInteger("n_batch");
377
+
378
+ implementation.initVocoder(contextId, path, nBatch, result -> {
379
+ if (result.isSuccess()) {
380
+ JSObject ret = new JSObject();
381
+ ret.put("success", result.getData());
382
+ call.resolve(ret);
383
+ } else {
384
+ call.reject(result.getError().getMessage());
385
+ }
386
+ });
387
+ }
388
+
389
+ @PluginMethod
390
+ public void isVocoderEnabled(PluginCall call) {
391
+ int contextId = call.getInt("contextId", 0);
392
+
393
+ implementation.isVocoderEnabled(contextId, result -> {
394
+ if (result.isSuccess()) {
395
+ JSObject ret = new JSObject();
396
+ ret.put("enabled", result.getData());
397
+ call.resolve(ret);
398
+ } else {
399
+ call.reject(result.getError().getMessage());
400
+ }
401
+ });
402
+ }
403
+
404
+ @PluginMethod
405
+ public void getFormattedAudioCompletion(PluginCall call) {
406
+ int contextId = call.getInt("contextId", 0);
407
+ String speakerJsonStr = call.getString("speakerJsonStr", "");
408
+ String textToSpeak = call.getString("textToSpeak", "");
409
+
410
+ implementation.getFormattedAudioCompletion(contextId, speakerJsonStr, textToSpeak, result -> {
411
+ if (result.isSuccess()) {
412
+ call.resolve(result.getData());
413
+ } else {
414
+ call.reject(result.getError().getMessage());
415
+ }
416
+ });
417
+ }
418
+
419
+ @PluginMethod
420
+ public void getAudioCompletionGuideTokens(PluginCall call) {
421
+ int contextId = call.getInt("contextId", 0);
422
+ String textToSpeak = call.getString("textToSpeak", "");
423
+
424
+ implementation.getAudioCompletionGuideTokens(contextId, textToSpeak, result -> {
425
+ if (result.isSuccess()) {
426
+ JSObject ret = new JSObject();
427
+ ret.put("tokens", result.getData());
428
+ call.resolve(ret);
429
+ } else {
430
+ call.reject(result.getError().getMessage());
431
+ }
432
+ });
433
+ }
434
+
435
+ @PluginMethod
436
+ public void decodeAudioTokens(PluginCall call) {
437
+ int contextId = call.getInt("contextId", 0);
438
+ Integer[] tokens = call.getArray("tokens", Integer.class);
439
+ if (tokens == null) tokens = new Integer[0];
440
+
441
+ implementation.decodeAudioTokens(contextId, tokens, result -> {
442
+ if (result.isSuccess()) {
443
+ JSObject ret = new JSObject();
444
+ ret.put("decodedTokens", result.getData());
445
+ call.resolve(ret);
446
+ } else {
447
+ call.reject(result.getError().getMessage());
448
+ }
449
+ });
450
+ }
451
+
452
+ @PluginMethod
453
+ public void releaseVocoder(PluginCall call) {
454
+ int contextId = call.getInt("contextId", 0);
455
+
456
+ implementation.releaseVocoder(contextId, result -> {
457
+ if (result.isSuccess()) {
458
+ call.resolve();
459
+ } else {
460
+ call.reject(result.getError().getMessage());
461
+ }
462
+ });
463
+ }
464
+
465
+ // MARK: - Events
466
+
467
+ @PluginMethod
468
+ public void addListener(PluginCall call) {
469
+ String eventName = call.getString("eventName", "");
470
+ // Note: In Capacitor, event listeners are typically handled differently
471
+ // This is a placeholder for the event system
472
+ call.resolve();
473
+ }
474
+
475
+ @PluginMethod
476
+ public void removeAllListeners(PluginCall call) {
477
+ String eventName = call.getString("eventName", "");
478
+ // Note: In Capacitor, event listeners are typically handled differently
479
+ // This is a placeholder for the event system
480
+ call.resolve();
481
+ }
482
+ }
@@ -0,0 +1,139 @@
1
+ #ifndef JNI_UTILS_H
2
+ #define JNI_UTILS_H
3
+
4
+ #include <jni.h>
5
+ #include <string>
6
+ #include <vector>
7
+ #include "rn-llama.h"
8
+
9
+ namespace jni_utils {
10
+
11
+ // Convert Java string to C++ string
12
+ std::string jstring_to_string(JNIEnv* env, jstring jstr);
13
+
14
+ // Convert C++ string to Java string
15
+ jstring string_to_jstring(JNIEnv* env, const std::string& str);
16
+
17
+ // Convert Java string array to C++ string vector
18
+ std::vector<std::string> jstring_array_to_string_vector(JNIEnv* env, jobjectArray jarray);
19
+
20
+ // Convert C++ string vector to Java string array
21
+ jobjectArray string_vector_to_jstring_array(JNIEnv* env, const std::vector<std::string>& vec);
22
+
23
+ // Convert Java boolean to C++ bool
24
+ bool jboolean_to_bool(jboolean jbool);
25
+
26
+ // Convert C++ bool to Java boolean
27
+ jboolean bool_to_jboolean(bool b);
28
+
29
+ // Convert Java int to C++ int
30
+ int jint_to_int(jint jint_val);
31
+
32
+ // Convert C++ int to Java int
33
+ jint int_to_jint(int val);
34
+
35
+ // Convert Java float to C++ float
36
+ float jfloat_to_float(jfloat jfloat_val);
37
+
38
+ // Convert C++ float to Java float
39
+ jfloat float_to_jfloat(float val);
40
+
41
+ // Convert Java long to C++ long
42
+ long jlong_to_long(jlong jlong_val);
43
+
44
+ // Convert C++ long to Java long
45
+ jlong long_to_jlong(long val);
46
+
47
+ // Convert Java double to C++ double
48
+ double jdouble_to_double(jdouble jdouble_val);
49
+
50
+ // Convert C++ double to Java double
51
+ jdouble double_to_jdouble(double val);
52
+
53
+ // Throw Java exception
54
+ void throw_java_exception(JNIEnv* env, const char* class_name, const char* message);
55
+
56
+ // Check if exception occurred
57
+ bool check_exception(JNIEnv* env);
58
+
59
+ // Get field ID safely
60
+ jfieldID get_field_id(JNIEnv* env, jclass clazz, const char* name, const char* sig);
61
+
62
+ // Get method ID safely
63
+ jmethodID get_method_id(JNIEnv* env, jclass clazz, const char* name, const char* sig);
64
+
65
+ // Find class safely
66
+ jclass find_class(JNIEnv* env, const char* name);
67
+
68
+ // Create object safely
69
+ jobject create_object(JNIEnv* env, jclass clazz, jmethodID constructor, ...);
70
+
71
+ // Call method safely
72
+ jobject call_method(JNIEnv* env, jobject obj, jmethodID method, ...);
73
+
74
+ // Call static method safely
75
+ jobject call_static_method(JNIEnv* env, jclass clazz, jmethodID method, ...);
76
+
77
+ // Set field safely
78
+ void set_field(JNIEnv* env, jobject obj, jfieldID field, ...);
79
+
80
+ // Get field safely
81
+ jobject get_field(JNIEnv* env, jobject obj, jfieldID field);
82
+
83
+ // Set static field safely
84
+ void set_static_field(JNIEnv* env, jclass clazz, jfieldID field, ...);
85
+
86
+ // Get static field safely
87
+ jobject get_static_field(JNIEnv* env, jclass clazz, jfieldID field);
88
+
89
+ // Convert llama_rn_context to jobject
90
+ jobject llama_context_to_jobject(JNIEnv* env, const llama_rn_context* context);
91
+
92
+ // Convert jobject to llama_rn_context
93
+ llama_rn_context* jobject_to_llama_context(JNIEnv* env, jobject obj);
94
+
95
+ // Convert completion result to jobject
96
+ jobject completion_result_to_jobject(JNIEnv* env, const completion_token_output& result);
97
+
98
+ // Convert jobject to completion parameters
99
+ completion_params jobject_to_completion_params(JNIEnv* env, jobject obj);
100
+
101
+ // Convert chat parameters to jobject
102
+ jobject chat_params_to_jobject(JNIEnv* env, const common_chat_params& params);
103
+
104
+ // Convert jobject to chat parameters
105
+ common_chat_params jobject_to_chat_params(JNIEnv* env, jobject obj);
106
+
107
+ // Convert tokenize result to jobject
108
+ jobject tokenize_result_to_jobject(JNIEnv* env, const llama_rn_tokenize_result& result);
109
+
110
+ // Convert embedding result to jobject
111
+ jobject embedding_result_to_jobject(JNIEnv* env, const std::vector<float>& embedding);
112
+
113
+ // Convert rerank result to jobject
114
+ jobject rerank_result_to_jobject(JNIEnv* env, const std::vector<std::pair<size_t, float>>& results);
115
+
116
+ // Convert benchmark result to jobject
117
+ jobject benchmark_result_to_jobject(JNIEnv* env, const std::vector<float>& timings);
118
+
119
+ // Convert LoRA adapter info to jobject
120
+ jobject lora_adapter_info_to_jobject(JNIEnv* env, const common_adapter_lora_info& info);
121
+
122
+ // Convert jobject to LoRA adapter info
123
+ common_adapter_lora_info jobject_to_lora_adapter_info(JNIEnv* env, jobject obj);
124
+
125
+ // Convert multimodal support info to jobject
126
+ jobject multimodal_support_to_jobject(JNIEnv* env, bool vision, bool audio);
127
+
128
+ // Convert TTS result to jobject
129
+ jobject tts_result_to_jobject(JNIEnv* env, const std::vector<float>& audio_data, int sample_rate);
130
+
131
+ // Convert session data to jobject
132
+ jobject session_data_to_jobject(JNIEnv* env, const std::string& data);
133
+
134
+ // Convert jobject to session data
135
+ std::string jobject_to_session_data(JNIEnv* env, jobject obj);
136
+
137
+ } // namespace jni_utils
138
+
139
+ #endif // JNI_UTILS_H