JNI: apply the model's chat template so instruct models answer properly
Browse files
app/src/main/cpp/llama_jni.cpp
CHANGED
|
@@ -8,6 +8,19 @@ struct State {
|
|
| 8 |
llama_context *ctx;
|
| 9 |
};
|
| 10 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 11 |
extern "C" JNIEXPORT jlong JNICALL
|
| 12 |
Java_io_github_abrarul_modularai_engine_LlamaEngine_nativeLoad(JNIEnv *env, jobject, jstring path,
|
| 13 |
jint nCtx, jint threads) {
|
|
@@ -41,8 +54,9 @@ Java_io_github_abrarul_modularai_engine_LlamaEngine_nativeGenerate(JNIEnv *env,
|
|
| 41 |
const llama_vocab *vocab = llama_model_get_vocab(s->model);
|
| 42 |
|
| 43 |
const char *pr = env->GetStringUTFChars(prompt, nullptr);
|
| 44 |
-
std::string
|
| 45 |
env->ReleaseStringUTFChars(prompt, pr);
|
|
|
|
| 46 |
|
| 47 |
llama_memory_clear(llama_get_memory(s->ctx), true);
|
| 48 |
|
|
@@ -62,6 +76,13 @@ Java_io_github_abrarul_modularai_engine_LlamaEngine_nativeGenerate(JNIEnv *env,
|
|
| 62 |
|
| 63 |
jclass cls = env->GetObjectClass(sink);
|
| 64 |
jmethodID mid = env->GetMethodID(cls, "onToken", "([B)Z");
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 65 |
|
| 66 |
llama_token tok; // must outlive the loop body: the batch points at it
|
| 67 |
llama_batch batch = llama_batch_get_one(toks.data(), (int) toks.size());
|
|
|
|
| 8 |
llama_context *ctx;
|
| 9 |
};
|
| 10 |
|
| 11 |
+
static std::string apply_chat(const llama_model *model, const std::string &user) {
|
| 12 |
+
const char *tmpl = llama_model_chat_template(model, nullptr);
|
| 13 |
+
if (!tmpl) return user; // base model: use the raw prompt
|
| 14 |
+
llama_chat_message msg{"user", user.c_str()};
|
| 15 |
+
std::vector<char> buf(user.size() * 2 + 1024);
|
| 16 |
+
int n = llama_chat_apply_template(tmpl, &msg, 1, true, buf.data(), (int) buf.size());
|
| 17 |
+
if (n > (int) buf.size()) {
|
| 18 |
+
buf.resize(n);
|
| 19 |
+
n = llama_chat_apply_template(tmpl, &msg, 1, true, buf.data(), (int) buf.size());
|
| 20 |
+
}
|
| 21 |
+
return n > 0 ? std::string(buf.data(), n) : user;
|
| 22 |
+
}
|
| 23 |
+
|
| 24 |
extern "C" JNIEXPORT jlong JNICALL
|
| 25 |
Java_io_github_abrarul_modularai_engine_LlamaEngine_nativeLoad(JNIEnv *env, jobject, jstring path,
|
| 26 |
jint nCtx, jint threads) {
|
|
|
|
| 54 |
const llama_vocab *vocab = llama_model_get_vocab(s->model);
|
| 55 |
|
| 56 |
const char *pr = env->GetStringUTFChars(prompt, nullptr);
|
| 57 |
+
std::string raw(pr);
|
| 58 |
env->ReleaseStringUTFChars(prompt, pr);
|
| 59 |
+
std::string text = apply_chat(s->model, raw);
|
| 60 |
|
| 61 |
llama_memory_clear(llama_get_memory(s->ctx), true);
|
| 62 |
|
|
|
|
| 76 |
|
| 77 |
jclass cls = env->GetObjectClass(sink);
|
| 78 |
jmethodID mid = env->GetMethodID(cls, "onToken", "([B)Z");
|
| 79 |
+
if (!mid) {
|
| 80 |
+
llama_sampler_free(smpl);
|
| 81 |
+
return;
|
| 82 |
+
}
|
| 83 |
+
|
| 84 |
+
int budget = (int) llama_n_ctx(s->ctx) - n;
|
| 85 |
+
if (maxTokens > budget) maxTokens = budget;
|
| 86 |
|
| 87 |
llama_token tok; // must outlive the loop body: the batch points at it
|
| 88 |
llama_batch batch = llama_batch_get_one(toks.data(), (int) toks.size());
|