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.
- package/LICENSE +21 -0
- package/LlamaCpp.podspec +17 -0
- package/Package.swift +28 -0
- package/README.md +574 -0
- package/android/build.gradle +58 -0
- package/android/src/main/AndroidManifest.xml +2 -0
- package/android/src/main/CMakeLists.txt +148 -0
- package/android/src/main/java/ai/annadata/plugin/capacitor/LlamaCpp.java +677 -0
- package/android/src/main/java/ai/annadata/plugin/capacitor/LlamaCppPlugin.java +482 -0
- package/android/src/main/jni-utils.h +139 -0
- package/android/src/main/jni.cpp +271 -0
- package/android/src/main/res/.gitkeep +0 -0
- package/dist/docs.json +5513 -0
- package/dist/esm/definitions.d.ts +653 -0
- package/dist/esm/definitions.js +2 -0
- package/dist/esm/definitions.js.map +1 -0
- package/dist/esm/index.d.ts +180 -0
- package/dist/esm/index.js +518 -0
- package/dist/esm/index.js.map +1 -0
- package/dist/plugin.cjs.js +531 -0
- package/dist/plugin.cjs.js.map +1 -0
- package/dist/plugin.js +534 -0
- package/dist/plugin.js.map +1 -0
- package/ios/Sources/LlamaCppPlugin/LlamaCpp.swift +596 -0
- package/ios/Sources/LlamaCppPlugin/LlamaCppPlugin.swift +514 -0
- package/ios/Tests/LlamaCppPluginTests/LlamaCppPluginTests.swift +15 -0
- package/package.json +108 -0
|
@@ -0,0 +1,271 @@
|
|
|
1
|
+
#include "jni-utils.h"
|
|
2
|
+
#include "rn-llama.h"
|
|
3
|
+
#include <android/log.h>
|
|
4
|
+
#include <cstring>
|
|
5
|
+
#include <memory>
|
|
6
|
+
|
|
7
|
+
#define LOG_TAG "LlamaCpp"
|
|
8
|
+
#define LOGI(...) __android_log_print(ANDROID_LOG_INFO, LOG_TAG, __VA_ARGS__)
|
|
9
|
+
#define LOGE(...) __android_log_print(ANDROID_LOG_ERROR, LOG_TAG, __VA_ARGS__)
|
|
10
|
+
|
|
11
|
+
namespace jni_utils {
|
|
12
|
+
|
|
13
|
+
std::string jstring_to_string(JNIEnv* env, jstring jstr) {
|
|
14
|
+
if (jstr == nullptr) return "";
|
|
15
|
+
const char* chars = env->GetStringUTFChars(jstr, nullptr);
|
|
16
|
+
std::string str(chars);
|
|
17
|
+
env->ReleaseStringUTFChars(jstr, chars);
|
|
18
|
+
return str;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
jstring string_to_jstring(JNIEnv* env, const std::string& str) {
|
|
22
|
+
return env->NewStringUTF(str.c_str());
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
std::vector<std::string> jstring_array_to_string_vector(JNIEnv* env, jobjectArray jarray) {
|
|
26
|
+
std::vector<std::string> result;
|
|
27
|
+
if (jarray == nullptr) return result;
|
|
28
|
+
|
|
29
|
+
jsize length = env->GetArrayLength(jarray);
|
|
30
|
+
for (jsize i = 0; i < length; i++) {
|
|
31
|
+
jstring jstr = (jstring)env->GetObjectArrayElement(jarray, i);
|
|
32
|
+
result.push_back(jstring_to_string(env, jstr));
|
|
33
|
+
env->DeleteLocalRef(jstr);
|
|
34
|
+
}
|
|
35
|
+
return result;
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
jobjectArray string_vector_to_jstring_array(JNIEnv* env, const std::vector<std::string>& vec) {
|
|
39
|
+
jclass stringClass = env->FindClass("java/lang/String");
|
|
40
|
+
jobjectArray result = env->NewObjectArray(vec.size(), stringClass, nullptr);
|
|
41
|
+
|
|
42
|
+
for (size_t i = 0; i < vec.size(); i++) {
|
|
43
|
+
jstring jstr = string_to_jstring(env, vec[i]);
|
|
44
|
+
env->SetObjectArrayElement(result, i, jstr);
|
|
45
|
+
env->DeleteLocalRef(jstr);
|
|
46
|
+
}
|
|
47
|
+
return result;
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
bool jboolean_to_bool(jboolean jbool) {
|
|
51
|
+
return jbool == JNI_TRUE;
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
jboolean bool_to_jboolean(bool b) {
|
|
55
|
+
return b ? JNI_TRUE : JNI_FALSE;
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
int jint_to_int(jint jint_val) {
|
|
59
|
+
return static_cast<int>(jint_val);
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
jint int_to_jint(int val) {
|
|
63
|
+
return static_cast<jint>(val);
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
float jfloat_to_float(jfloat jfloat_val) {
|
|
67
|
+
return static_cast<float>(jfloat_val);
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
jfloat float_to_jfloat(float val) {
|
|
71
|
+
return static_cast<jfloat>(val);
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
long jlong_to_long(jlong jlong_val) {
|
|
75
|
+
return static_cast<long>(jlong_val);
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
jlong long_to_jlong(long val) {
|
|
79
|
+
return static_cast<jlong>(val);
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
double jdouble_to_double(jdouble jdouble_val) {
|
|
83
|
+
return static_cast<double>(jdouble_val);
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
jdouble double_to_jdouble(double val) {
|
|
87
|
+
return static_cast<jdouble>(val);
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
void throw_java_exception(JNIEnv* env, const char* class_name, const char* message) {
|
|
91
|
+
jclass exceptionClass = env->FindClass(class_name);
|
|
92
|
+
if (exceptionClass != nullptr) {
|
|
93
|
+
env->ThrowNew(exceptionClass, message);
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
bool check_exception(JNIEnv* env) {
|
|
98
|
+
return env->ExceptionCheck() == JNI_TRUE;
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
jfieldID get_field_id(JNIEnv* env, jclass clazz, const char* name, const char* sig) {
|
|
102
|
+
jfieldID fieldID = env->GetFieldID(clazz, name, sig);
|
|
103
|
+
if (check_exception(env)) {
|
|
104
|
+
return nullptr;
|
|
105
|
+
}
|
|
106
|
+
return fieldID;
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
jmethodID get_method_id(JNIEnv* env, jclass clazz, const char* name, const char* sig) {
|
|
110
|
+
jmethodID methodID = env->GetMethodID(clazz, name, sig);
|
|
111
|
+
if (check_exception(env)) {
|
|
112
|
+
return nullptr;
|
|
113
|
+
}
|
|
114
|
+
return methodID;
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
jclass find_class(JNIEnv* env, const char* name) {
|
|
118
|
+
jclass clazz = env->FindClass(name);
|
|
119
|
+
if (check_exception(env)) {
|
|
120
|
+
return nullptr;
|
|
121
|
+
}
|
|
122
|
+
return clazz;
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
// Global context storage
|
|
126
|
+
static std::map<jlong, std::unique_ptr<llama_rn_context>> contexts;
|
|
127
|
+
static jlong next_context_id = 1;
|
|
128
|
+
|
|
129
|
+
extern "C" {
|
|
130
|
+
|
|
131
|
+
JNIEXPORT jlong JNICALL
|
|
132
|
+
Java_ai_annadata_plugin_capacitor_LlamaCpp_initContext(
|
|
133
|
+
JNIEnv* env, jobject thiz, jstring model_path, jobject params) {
|
|
134
|
+
|
|
135
|
+
try {
|
|
136
|
+
std::string model_path_str = jstring_to_string(env, model_path);
|
|
137
|
+
|
|
138
|
+
// Create new context
|
|
139
|
+
auto context = std::make_unique<llama_rn_context>();
|
|
140
|
+
|
|
141
|
+
// Initialize common parameters (simplified)
|
|
142
|
+
common_params cparams;
|
|
143
|
+
cparams.model = model_path_str;
|
|
144
|
+
cparams.n_ctx = 2048;
|
|
145
|
+
cparams.n_batch = 512;
|
|
146
|
+
cparams.n_threads = 4;
|
|
147
|
+
cparams.n_gpu_layers = 0;
|
|
148
|
+
|
|
149
|
+
// Load model
|
|
150
|
+
if (!context->loadModel(cparams)) {
|
|
151
|
+
throw_java_exception(env, "java/lang/RuntimeException", "Failed to load model");
|
|
152
|
+
return -1;
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
// Store context
|
|
156
|
+
jlong context_id = next_context_id++;
|
|
157
|
+
contexts[context_id] = std::move(context);
|
|
158
|
+
|
|
159
|
+
LOGI("Initialized context %lld with model: %s", context_id, model_path_str.c_str());
|
|
160
|
+
return context_id;
|
|
161
|
+
|
|
162
|
+
} catch (const std::exception& e) {
|
|
163
|
+
LOGE("Exception in initContext: %s", e.what());
|
|
164
|
+
throw_java_exception(env, "java/lang/RuntimeException", e.what());
|
|
165
|
+
return -1;
|
|
166
|
+
}
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
JNIEXPORT void JNICALL
|
|
170
|
+
Java_ai_annadata_plugin_capacitor_LlamaCpp_releaseContext(
|
|
171
|
+
JNIEnv* env, jobject thiz, jlong context_id) {
|
|
172
|
+
|
|
173
|
+
try {
|
|
174
|
+
auto it = contexts.find(context_id);
|
|
175
|
+
if (it != contexts.end()) {
|
|
176
|
+
contexts.erase(it);
|
|
177
|
+
LOGI("Released context %lld", context_id);
|
|
178
|
+
}
|
|
179
|
+
} catch (const std::exception& e) {
|
|
180
|
+
LOGE("Exception in releaseContext: %s", e.what());
|
|
181
|
+
throw_java_exception(env, "java/lang/RuntimeException", e.what());
|
|
182
|
+
}
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
JNIEXPORT jstring JNICALL
|
|
186
|
+
Java_ai_annadata_plugin_capacitor_LlamaCpp_completion(
|
|
187
|
+
JNIEnv* env, jobject thiz, jlong context_id, jstring prompt, jobject params) {
|
|
188
|
+
|
|
189
|
+
try {
|
|
190
|
+
auto it = contexts.find(context_id);
|
|
191
|
+
if (it == contexts.end()) {
|
|
192
|
+
throw_java_exception(env, "java/lang/IllegalArgumentException", "Invalid context ID");
|
|
193
|
+
return nullptr;
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
std::string prompt_str = jstring_to_string(env, prompt);
|
|
197
|
+
|
|
198
|
+
// Simplified completion (placeholder implementation)
|
|
199
|
+
std::string result = "Generated text for: " + prompt_str;
|
|
200
|
+
|
|
201
|
+
LOGI("Completion for context %lld: %s", context_id, prompt_str.c_str());
|
|
202
|
+
return string_to_jstring(env, result);
|
|
203
|
+
|
|
204
|
+
} catch (const std::exception& e) {
|
|
205
|
+
LOGE("Exception in completion: %s", e.what());
|
|
206
|
+
throw_java_exception(env, "java/lang/RuntimeException", e.what());
|
|
207
|
+
return nullptr;
|
|
208
|
+
}
|
|
209
|
+
}
|
|
210
|
+
|
|
211
|
+
JNIEXPORT void JNICALL
|
|
212
|
+
Java_ai_annadata_plugin_capacitor_LlamaCpp_stopCompletion(
|
|
213
|
+
JNIEnv* env, jobject thiz, jlong context_id) {
|
|
214
|
+
|
|
215
|
+
try {
|
|
216
|
+
auto it = contexts.find(context_id);
|
|
217
|
+
if (it != contexts.end()) {
|
|
218
|
+
// Stop completion logic would go here
|
|
219
|
+
LOGI("Stopped completion for context %lld", context_id);
|
|
220
|
+
}
|
|
221
|
+
} catch (const std::exception& e) {
|
|
222
|
+
LOGE("Exception in stopCompletion: %s", e.what());
|
|
223
|
+
throw_java_exception(env, "java/lang/RuntimeException", e.what());
|
|
224
|
+
}
|
|
225
|
+
}
|
|
226
|
+
|
|
227
|
+
JNIEXPORT jstring JNICALL
|
|
228
|
+
Java_ai_annadata_plugin_capacitor_LlamaCpp_getFormattedChat(
|
|
229
|
+
JNIEnv* env, jobject thiz, jlong context_id, jstring messages, jstring chat_template) {
|
|
230
|
+
|
|
231
|
+
try {
|
|
232
|
+
auto it = contexts.find(context_id);
|
|
233
|
+
if (it == contexts.end()) {
|
|
234
|
+
throw_java_exception(env, "java/lang/IllegalArgumentException", "Invalid context ID");
|
|
235
|
+
return nullptr;
|
|
236
|
+
}
|
|
237
|
+
|
|
238
|
+
std::string messages_str = jstring_to_string(env, messages);
|
|
239
|
+
std::string template_str = jstring_to_string(env, chat_template);
|
|
240
|
+
|
|
241
|
+
// Simplified chat formatting (placeholder implementation)
|
|
242
|
+
std::string result = "Formatted chat: " + messages_str;
|
|
243
|
+
|
|
244
|
+
LOGI("Formatted chat for context %lld", context_id);
|
|
245
|
+
return string_to_jstring(env, result);
|
|
246
|
+
|
|
247
|
+
} catch (const std::exception& e) {
|
|
248
|
+
LOGE("Exception in getFormattedChat: %s", e.what());
|
|
249
|
+
throw_java_exception(env, "java/lang/RuntimeException", e.what());
|
|
250
|
+
return nullptr;
|
|
251
|
+
}
|
|
252
|
+
}
|
|
253
|
+
|
|
254
|
+
JNIEXPORT jboolean JNICALL
|
|
255
|
+
Java_ai_annadata_plugin_capacitor_LlamaCpp_toggleNativeLog(
|
|
256
|
+
JNIEnv* env, jobject thiz, jboolean enabled) {
|
|
257
|
+
|
|
258
|
+
try {
|
|
259
|
+
rnllama_verbose = jboolean_to_bool(enabled);
|
|
260
|
+
LOGI("Native logging %s", enabled ? "enabled" : "disabled");
|
|
261
|
+
return bool_to_jboolean(true);
|
|
262
|
+
} catch (const std::exception& e) {
|
|
263
|
+
LOGE("Exception in toggleNativeLog: %s", e.what());
|
|
264
|
+
throw_java_exception(env, "java/lang/RuntimeException", e.what());
|
|
265
|
+
return bool_to_jboolean(false);
|
|
266
|
+
}
|
|
267
|
+
}
|
|
268
|
+
|
|
269
|
+
} // extern "C"
|
|
270
|
+
|
|
271
|
+
} // namespace jni_utils
|
|
File without changes
|