@fugood/llama.node 0.3.8 → 0.3.9

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
@@ -37,6 +37,9 @@ export type LlamaModelOptions = {
37
37
  use_mlock?: boolean
38
38
  use_mmap?: boolean
39
39
  vocab_only?: boolean
40
+ lora?: string
41
+ lora_scaled?: number
42
+ lora_list?: { path: string; scaled: number }[]
40
43
  }
41
44
 
42
45
  export type LlamaCompletionOptions = {
@@ -111,6 +114,9 @@ export interface LlamaContext {
111
114
  saveSession(path: string): Promise<void>
112
115
  loadSession(path: string): Promise<void>
113
116
  release(): Promise<void>
117
+ applyLoraAdapters(adapters: { path: string; scaled: number }[]): void
118
+ removeLoraAdapters(adapters: { path: string }[]): void
119
+ getLoadedLoraAdapters(): { path: string; scaled: number }[]
114
120
  // static
115
121
  loadModelInfo(path: string, skip: string[]): Promise<Object>
116
122
  }
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@fugood/llama.node",
3
3
  "access": "public",
4
- "version": "0.3.8",
4
+ "version": "0.3.9",
5
5
  "description": "Llama.cpp for Node.js",
6
6
  "main": "lib/index.js",
7
7
  "scripts": {
@@ -103,6 +103,15 @@ void LlamaContext::Init(Napi::Env env, Napi::Object &exports) {
103
103
  InstanceMethod<&LlamaContext::LoadSession>(
104
104
  "loadSession",
105
105
  static_cast<napi_property_attributes>(napi_enumerable)),
106
+ InstanceMethod<&LlamaContext::ApplyLoraAdapters>(
107
+ "applyLoraAdapters",
108
+ static_cast<napi_property_attributes>(napi_enumerable)),
109
+ InstanceMethod<&LlamaContext::RemoveLoraAdapters>(
110
+ "removeLoraAdapters",
111
+ static_cast<napi_property_attributes>(napi_enumerable)),
112
+ InstanceMethod<&LlamaContext::GetLoadedLoraAdapters>(
113
+ "getLoadedLoraAdapters",
114
+ static_cast<napi_property_attributes>(napi_enumerable)),
106
115
  InstanceMethod<&LlamaContext::Release>(
107
116
  "release", static_cast<napi_property_attributes>(napi_enumerable)),
108
117
  StaticMethod<&LlamaContext::ModelInfo>(
@@ -202,6 +211,48 @@ LlamaContext::LlamaContext(const Napi::CallbackInfo &info)
202
211
  .ThrowAsJavaScriptException();
203
212
  }
204
213
 
214
+ auto ctx = sess->context();
215
+ auto model = sess->model();
216
+
217
+ std::vector<common_adapter_lora_info> lora;
218
+ auto lora_path = get_option<std::string>(options, "lora", "");
219
+ auto lora_scaled = get_option<float>(options, "lora_scaled", 1.0f);
220
+ if (lora_path != "") {
221
+ common_adapter_lora_info la;
222
+ la.path = lora_path;
223
+ la.scale = lora_scaled;
224
+ la.ptr = llama_adapter_lora_init(model, lora_path.c_str());
225
+ if (la.ptr == nullptr) {
226
+ Napi::TypeError::New(env, "Failed to load lora adapter")
227
+ .ThrowAsJavaScriptException();
228
+ }
229
+ lora.push_back(la);
230
+ }
231
+
232
+ if (options.Has("lora_list") && options.Get("lora_list").IsArray()) {
233
+ auto lora_list = options.Get("lora_list").As<Napi::Array>();
234
+ if (lora_list != nullptr) {
235
+ int lora_list_size = lora_list.Length();
236
+ for (int i = 0; i < lora_list_size; i++) {
237
+ auto lora_adapter = lora_list.Get(i).As<Napi::Object>();
238
+ auto path = lora_adapter.Get("path").ToString();
239
+ if (path != nullptr) {
240
+ common_adapter_lora_info la;
241
+ la.path = path;
242
+ la.scale = lora_adapter.Get("scaled").ToNumber().FloatValue();
243
+ la.ptr = llama_adapter_lora_init(model, path.Utf8Value().c_str());
244
+ if (la.ptr == nullptr) {
245
+ Napi::TypeError::New(env, "Failed to load lora adapter")
246
+ .ThrowAsJavaScriptException();
247
+ }
248
+ lora.push_back(la);
249
+ }
250
+ }
251
+ }
252
+ }
253
+ common_set_adapter_lora(ctx, lora);
254
+ _lora = lora;
255
+
205
256
  _sess = sess;
206
257
  _info = common_params_get_system_info(params);
207
258
  }
@@ -242,6 +293,7 @@ Napi::Value LlamaContext::GetModelInfo(const Napi::CallbackInfo &info) {
242
293
  }
243
294
  Napi::Object details = Napi::Object::New(info.Env());
244
295
  details.Set("desc", desc);
296
+ details.Set("nEmbd", llama_model_n_embd(model));
245
297
  details.Set("nParams", llama_model_n_params(model));
246
298
  details.Set("size", llama_model_size(model));
247
299
  details.Set("isChatTemplateSupported", validateModelChatTemplate(model));
@@ -451,6 +503,49 @@ Napi::Value LlamaContext::LoadSession(const Napi::CallbackInfo &info) {
451
503
  return worker->Promise();
452
504
  }
453
505
 
506
+ // applyLoraAdapters(lora_adapters: [{ path: string, scaled: number }]): void
507
+ void LlamaContext::ApplyLoraAdapters(const Napi::CallbackInfo &info) {
508
+ Napi::Env env = info.Env();
509
+ std::vector<common_adapter_lora_info> lora;
510
+ auto lora_adapters = info[0].As<Napi::Array>();
511
+ for (size_t i = 0; i < lora_adapters.Length(); i++) {
512
+ auto lora_adapter = lora_adapters.Get(i).As<Napi::Object>();
513
+ auto path = lora_adapter.Get("path").ToString().Utf8Value();
514
+ auto scaled = lora_adapter.Get("scaled").ToNumber().FloatValue();
515
+ common_adapter_lora_info la;
516
+ la.path = path;
517
+ la.scale = scaled;
518
+ la.ptr = llama_adapter_lora_init(_sess->model(), path.c_str());
519
+ if (la.ptr == nullptr) {
520
+ Napi::TypeError::New(env, "Failed to load lora adapter")
521
+ .ThrowAsJavaScriptException();
522
+ }
523
+ lora.push_back(la);
524
+ }
525
+ common_set_adapter_lora(_sess->context(), lora);
526
+ _lora = lora;
527
+ }
528
+
529
+ // removeLoraAdapters(): void
530
+ void LlamaContext::RemoveLoraAdapters(const Napi::CallbackInfo &info) {
531
+ _lora.clear();
532
+ common_set_adapter_lora(_sess->context(), _lora);
533
+ }
534
+
535
+ // getLoadedLoraAdapters(): Promise<{ count, lora_adapters: [{ path: string,
536
+ // scaled: number }] }>
537
+ Napi::Value LlamaContext::GetLoadedLoraAdapters(const Napi::CallbackInfo &info) {
538
+ Napi::Env env = info.Env();
539
+ Napi::Array lora_adapters = Napi::Array::New(env, _lora.size());
540
+ for (size_t i = 0; i < _lora.size(); i++) {
541
+ Napi::Object lora_adapter = Napi::Object::New(env);
542
+ lora_adapter.Set("path", _lora[i].path);
543
+ lora_adapter.Set("scaled", _lora[i].scale);
544
+ lora_adapters.Set(i, lora_adapter);
545
+ }
546
+ return lora_adapters;
547
+ }
548
+
454
549
  // release(): Promise<void>
455
550
  Napi::Value LlamaContext::Release(const Napi::CallbackInfo &info) {
456
551
  auto env = info.Env();
@@ -19,10 +19,14 @@ private:
19
19
  Napi::Value Embedding(const Napi::CallbackInfo &info);
20
20
  Napi::Value SaveSession(const Napi::CallbackInfo &info);
21
21
  Napi::Value LoadSession(const Napi::CallbackInfo &info);
22
+ void ApplyLoraAdapters(const Napi::CallbackInfo &info);
23
+ void RemoveLoraAdapters(const Napi::CallbackInfo &info);
24
+ Napi::Value GetLoadedLoraAdapters(const Napi::CallbackInfo &info);
22
25
  Napi::Value Release(const Napi::CallbackInfo &info);
23
26
 
24
27
  std::string _info;
25
28
  Napi::Object _meta;
26
29
  LlamaSessionPtr _sess = nullptr;
30
+ std::vector<common_adapter_lora_info> _lora;
27
31
  LlamaCompletionWorker *_wip = nullptr;
28
32
  };