@fugood/llama.node 0.3.10 → 0.3.11

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.
Binary file
Binary file
Binary file
Binary file
Binary file
Binary file
Binary file
Binary file
Binary file
Binary file
Binary file
package/lib/binding.ts CHANGED
@@ -150,6 +150,7 @@ export interface LlamaContext {
150
150
  getLoadedLoraAdapters(): { path: string; scaled: number }[]
151
151
  // static
152
152
  loadModelInfo(path: string, skip: string[]): Promise<Object>
153
+ toggleNativeLog(enable: boolean, callback: (level: string, text: string) => void): void
153
154
  }
154
155
 
155
156
  export interface Module {
package/lib/index.js CHANGED
@@ -23,7 +23,8 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
23
23
  });
24
24
  };
25
25
  Object.defineProperty(exports, "__esModule", { value: true });
26
- exports.loadLlamaModelInfo = exports.initLlama = exports.loadModel = void 0;
26
+ exports.toggleNativeLog = exports.loadLlamaModelInfo = exports.initLlama = exports.loadModel = void 0;
27
+ exports.addNativeLogListener = addNativeLogListener;
27
28
  const binding_1 = require("./binding");
28
29
  __exportStar(require("./binding"), exports);
29
30
  const mods = {};
@@ -49,3 +50,22 @@ const loadLlamaModelInfo = (path) => __awaiter(void 0, void 0, void 0, function*
49
50
  return mods[variant].LlamaContext.loadModelInfo(path, modelInfoSkip);
50
51
  });
51
52
  exports.loadLlamaModelInfo = loadLlamaModelInfo;
53
+ const logListeners = [];
54
+ const logCallback = (level, text) => {
55
+ logListeners.forEach((listener) => listener(level, text));
56
+ };
57
+ const toggleNativeLog = (enable, options) => __awaiter(void 0, void 0, void 0, function* () {
58
+ var _a, _b;
59
+ const v = (_a = options === null || options === void 0 ? void 0 : options.variant) !== null && _a !== void 0 ? _a : 'default';
60
+ (_b = mods[v]) !== null && _b !== void 0 ? _b : (mods[v] = yield (0, binding_1.loadModule)(v));
61
+ return mods[v].LlamaContext.toggleNativeLog(enable, logCallback);
62
+ });
63
+ exports.toggleNativeLog = toggleNativeLog;
64
+ function addNativeLogListener(listener) {
65
+ logListeners.push(listener);
66
+ return {
67
+ remove: () => {
68
+ logListeners.splice(logListeners.indexOf(listener), 1);
69
+ },
70
+ };
71
+ }
package/lib/index.ts CHANGED
@@ -32,3 +32,31 @@ export const loadLlamaModelInfo = async (path: string): Promise<Object> => {
32
32
  mods[variant] ??= await loadModule(variant)
33
33
  return mods[variant].LlamaContext.loadModelInfo(path, modelInfoSkip)
34
34
  }
35
+
36
+ const logListeners: Array<(level: string, text: string) => void> = []
37
+
38
+ const logCallback = (level: string, text: string) => {
39
+ logListeners.forEach((listener) => listener(level, text))
40
+ }
41
+
42
+ export const toggleNativeLog = async (
43
+ enable: boolean,
44
+ options?: {
45
+ variant?: LibVariant
46
+ },
47
+ ) => {
48
+ const v = options?.variant ?? 'default'
49
+ mods[v] ??= await loadModule(v)
50
+ return mods[v].LlamaContext.toggleNativeLog(enable, logCallback)
51
+ }
52
+
53
+ export function addNativeLogListener(
54
+ listener: (level: string, text: string) => void,
55
+ ): { remove: () => void } {
56
+ logListeners.push(listener)
57
+ return {
58
+ remove: () => {
59
+ logListeners.splice(logListeners.indexOf(listener), 1)
60
+ },
61
+ }
62
+ }
package/package.json CHANGED
@@ -1,8 +1,8 @@
1
1
  {
2
2
  "name": "@fugood/llama.node",
3
3
  "access": "public",
4
- "version": "0.3.10",
5
- "description": "Llama.cpp for Node.js",
4
+ "version": "0.3.11",
5
+ "description": "An another Node binding of llama.cpp",
6
6
  "main": "lib/index.js",
7
7
  "scripts": {
8
8
  "test": "jest",
@@ -22,7 +22,9 @@
22
22
  "llama",
23
23
  "llm",
24
24
  "ai",
25
- "genai"
25
+ "genai",
26
+ "Local LLM",
27
+ "llama.cpp"
26
28
  ],
27
29
  "author": "Hans <hans.chen@bricks.tools>",
28
30
  "license": "MIT",
@@ -120,6 +120,9 @@ void LlamaContext::Init(Napi::Env env, Napi::Object &exports) {
120
120
  "release", static_cast<napi_property_attributes>(napi_enumerable)),
121
121
  StaticMethod<&LlamaContext::ModelInfo>(
122
122
  "loadModelInfo",
123
+ static_cast<napi_property_attributes>(napi_enumerable)),
124
+ StaticMethod<&LlamaContext::ToggleNativeLog>(
125
+ "toggleNativeLog",
123
126
  static_cast<napi_property_attributes>(napi_enumerable))});
124
127
  Napi::FunctionReference *constructor = new Napi::FunctionReference();
125
128
  *constructor = Napi::Persistent(func);
@@ -278,6 +281,46 @@ bool validateModelChatTemplate(const struct llama_model * model, const bool use_
278
281
  return common_chat_verify_template(tmpl, use_jinja);
279
282
  }
280
283
 
284
+ static Napi::FunctionReference _log_callback;
285
+
286
+ // toggleNativeLog(enable: boolean, callback: (log: string) => void): void
287
+ void LlamaContext::ToggleNativeLog(const Napi::CallbackInfo &info) {
288
+ bool enable = info[0].ToBoolean().Value();
289
+ if (enable) {
290
+ _log_callback.Reset(info[1].As<Napi::Function>());
291
+
292
+ llama_log_set([](ggml_log_level level, const char * text, void * user_data) {
293
+ llama_log_callback_default(level, text, user_data);
294
+
295
+ std::string level_str = "";
296
+ if (level == GGML_LOG_LEVEL_ERROR) {
297
+ level_str = "error";
298
+ } else if (level == GGML_LOG_LEVEL_INFO) {
299
+ level_str = "info";
300
+ } else if (level == GGML_LOG_LEVEL_WARN) {
301
+ level_str = "warn";
302
+ }
303
+
304
+ if (_log_callback.IsEmpty()) {
305
+ return;
306
+ }
307
+ try {
308
+ Napi::Env env = _log_callback.Env();
309
+ Napi::HandleScope scope(env);
310
+ _log_callback.Call({
311
+ Napi::String::New(env, level_str),
312
+ Napi::String::New(env, text)
313
+ });
314
+ } catch (const std::exception &e) {
315
+ // printf("Error calling log callback: %s\n", e.what());
316
+ }
317
+ }, nullptr);
318
+ } else {
319
+ _log_callback.Reset();
320
+ llama_log_set(llama_log_callback_default, nullptr);
321
+ }
322
+ }
323
+
281
324
  // getModelInfo(): object
282
325
  Napi::Value LlamaContext::GetModelInfo(const Napi::CallbackInfo &info) {
283
326
  char desc[1024];
@@ -312,8 +355,7 @@ Napi::Value LlamaContext::GetModelInfo(const Napi::CallbackInfo &info) {
312
355
  defaultCaps.Set("parallelToolCalls", _templates.template_default->original_caps().supports_parallel_tool_calls);
313
356
  defaultCaps.Set("toolCallId", _templates.template_default->original_caps().supports_tool_call_id);
314
357
  minja.Set("defaultCaps", defaultCaps);
315
- Napi::Object toolUse = Napi::Object::New(info.Env());
316
- toolUse.Set("toolUse", validateModelChatTemplate(model, true, "tool_use"));
358
+ minja.Set("toolUse", validateModelChatTemplate(model, true, "tool_use"));
317
359
  if (_templates.template_tool_use) {
318
360
  Napi::Object toolUseCaps = Napi::Object::New(info.Env());
319
361
  toolUseCaps.Set("tools", _templates.template_tool_use->original_caps().supports_tools);
@@ -322,13 +364,15 @@ Napi::Value LlamaContext::GetModelInfo(const Napi::CallbackInfo &info) {
322
364
  toolUseCaps.Set("systemRole", _templates.template_tool_use->original_caps().supports_system_role);
323
365
  toolUseCaps.Set("parallelToolCalls", _templates.template_tool_use->original_caps().supports_parallel_tool_calls);
324
366
  toolUseCaps.Set("toolCallId", _templates.template_tool_use->original_caps().supports_tool_call_id);
325
- toolUse.Set("toolUseCaps", toolUseCaps);
367
+ minja.Set("toolUseCaps", toolUseCaps);
326
368
  }
327
- minja.Set("toolUse", toolUse);
328
369
  chatTemplates.Set("minja", minja);
329
370
  details.Set("chatTemplates", chatTemplates);
330
371
 
331
372
  details.Set("metadata", metadata);
373
+
374
+ // Deprecated: use chatTemplates.llamaChat instead
375
+ details.Set("isChatTemplateSupported", validateModelChatTemplate(_sess->model(), false, ""));
332
376
  return details;
333
377
  }
334
378
 
@@ -5,6 +5,7 @@ class LlamaCompletionWorker;
5
5
  class LlamaContext : public Napi::ObjectWrap<LlamaContext> {
6
6
  public:
7
7
  LlamaContext(const Napi::CallbackInfo &info);
8
+ static void ToggleNativeLog(const Napi::CallbackInfo &info);
8
9
  static Napi::Value ModelInfo(const Napi::CallbackInfo& info);
9
10
  static void Init(Napi::Env env, Napi::Object &exports);
10
11