@fugood/llama.node 0.0.1-alpha.2 → 0.0.1-alpha.3

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/CMakeLists.txt CHANGED
@@ -61,7 +61,21 @@ set(CMAKE_POSITION_INDEPENDENT_CODE ON)
61
61
  set(LLAMA_STATIC ON CACHE BOOL "Build llama as static library")
62
62
  add_subdirectory("src/llama.cpp")
63
63
 
64
- file(GLOB SOURCE_FILES "src/addons.cpp")
64
+ file(
65
+ GLOB SOURCE_FILES
66
+ "src/addons.cc"
67
+ "src/common.hpp"
68
+ "src/DisposeWorker.cpp"
69
+ "src/DisposeWorker.h"
70
+ "src/LlamaCompletionWorker.cpp"
71
+ "src/LlamaCompletionWorker.h"
72
+ "src/LlamaContext.cpp"
73
+ "src/LlamaContext.h"
74
+ "src/LoadSessionWorker.cpp"
75
+ "src/LoadSessionWorker.h"
76
+ "src/SaveSessionWorker.cpp"
77
+ "src/SaveSessionWorker.h"
78
+ )
65
79
 
66
80
  add_library(${PROJECT_NAME} SHARED ${SOURCE_FILES} ${CMAKE_JS_SRC})
67
81
  set_target_properties(${PROJECT_NAME} PROPERTIES PREFIX "" SUFFIX ".node")
@@ -83,3 +97,15 @@ add_custom_command(TARGET ${PROJECT_NAME} POST_BUILD
83
97
  COMMAND ${CMAKE_COMMAND} -E copy $<TARGET_FILE:${PROJECT_NAME}> ${PLATFORM_BINARY_DIR}/$<TARGET_FILE_NAME:${PROJECT_NAME}>
84
98
  COMMENT "Copying to bin folder"
85
99
  )
100
+
101
+ if (LLAMA_METAL)
102
+ # copy ${CMAKE_BINARY_DIR}/bin/default.metallib
103
+ add_custom_target(copy_metallib)
104
+ add_custom_command(
105
+ TARGET copy_metallib
106
+ COMMAND ${CMAKE_COMMAND} -E copy ${CMAKE_BINARY_DIR}/bin/default.metallib ${PLATFORM_BINARY_DIR}/default.metallib
107
+ COMMENT "Copying default.metallib to bin folder"
108
+ )
109
+ add_dependencies(copy_metallib ggml-metal)
110
+ add_dependencies(${PROJECT_NAME} copy_metallib)
111
+ endif()
package/README.md CHANGED
@@ -20,7 +20,7 @@ const context = await loadModel({
20
20
  model: 'path/to/gguf/model',
21
21
  use_mlock: true,
22
22
  n_ctx: 2048,
23
- n_gpu_layers: 1, // > 0: enable Metal on iOS
23
+ n_gpu_layers: 1, // > 0: enable GPU
24
24
  // embedding: true, // use embedding
25
25
  })
26
26
 
Binary file
Binary file
Binary file
Binary file
Binary file
Binary file
Binary file
Binary file
Binary file
package/lib/index.js CHANGED
@@ -26,9 +26,11 @@ Object.defineProperty(exports, "__esModule", { value: true });
26
26
  exports.loadModel = void 0;
27
27
  const binding_1 = require("./binding");
28
28
  __exportStar(require("./binding"), exports);
29
- let module = null;
29
+ const mods = {};
30
30
  const loadModel = (options) => __awaiter(void 0, void 0, void 0, function* () {
31
- module !== null && module !== void 0 ? module : (module = yield (0, binding_1.loadModule)(options.lib_variant));
32
- return new module.LlamaContext(options);
31
+ var _a, _b;
32
+ const variant = (_a = options.lib_variant) !== null && _a !== void 0 ? _a : 'default';
33
+ (_b = mods[variant]) !== null && _b !== void 0 ? _b : (mods[variant] = yield (0, binding_1.loadModule)(options.lib_variant));
34
+ return new mods[variant].LlamaContext(options);
33
35
  });
34
36
  exports.loadModel = loadModel;
package/lib/index.ts CHANGED
@@ -7,9 +7,10 @@ export interface LlamaModelOptionsExtended extends LlamaModelOptions {
7
7
  lib_variant?: string
8
8
  }
9
9
 
10
- let module: Module | null = null
10
+ const mods: { [key: string]: Module } = {}
11
11
 
12
12
  export const loadModel = async (options: LlamaModelOptionsExtended): Promise<LlamaContext> => {
13
- module ??= await loadModule(options.lib_variant)
14
- return new module.LlamaContext(options)
13
+ const variant = options.lib_variant ?? 'default'
14
+ mods[variant] ??= await loadModule(options.lib_variant)
15
+ return new mods[variant].LlamaContext(options)
15
16
  }
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@fugood/llama.node",
3
3
  "access": "public",
4
- "version": "0.0.1-alpha.2",
4
+ "version": "0.0.1-alpha.3",
5
5
  "description": "Llama.cpp for Node.js",
6
6
  "main": "lib/index.js",
7
7
  "scripts": {
@@ -57,7 +57,8 @@
57
57
  "husky": "^9.0.11",
58
58
  "jest": "^29.7.0",
59
59
  "rimraf": "^5.0.5",
60
- "typescript": "^5.4.5"
60
+ "typescript": "^5.4.5",
61
+ "wait-for-expect": "^3.0.2"
61
62
  },
62
63
  "dependencies": {
63
64
  "node-addon-api": "^8.0.0"
@@ -0,0 +1,11 @@
1
+ #include "DisposeWorker.h"
2
+
3
+ DisposeWorker::DisposeWorker(const Napi::CallbackInfo &info,
4
+ LlamaSessionPtr sess)
5
+ : AsyncWorker(info.Env()), Deferred(info.Env()), sess_(std::move(sess)) {}
6
+
7
+ void DisposeWorker::Execute() { sess_->dispose(); }
8
+
9
+ void DisposeWorker::OnOK() { Resolve(AsyncWorker::Env().Undefined()); }
10
+
11
+ void DisposeWorker::OnError(const Napi::Error &err) { Reject(err.Value()); }
@@ -0,0 +1,14 @@
1
+ #include "common.hpp"
2
+
3
+ class DisposeWorker : public Napi::AsyncWorker, public Napi::Promise::Deferred {
4
+ public:
5
+ DisposeWorker(const Napi::CallbackInfo &info, LlamaSessionPtr sess);
6
+
7
+ protected:
8
+ void Execute();
9
+ void OnOK();
10
+ void OnError(const Napi::Error &err);
11
+
12
+ private:
13
+ LlamaSessionPtr sess_;
14
+ };
@@ -0,0 +1,163 @@
1
+ #include "LlamaCompletionWorker.h"
2
+ #include "LlamaContext.h"
3
+
4
+ size_t common_part(const std::vector<llama_token> &a,
5
+ const std::vector<llama_token> &b) {
6
+ size_t i = 0;
7
+ while (i < a.size() && i < b.size() && a[i] == b[i]) {
8
+ i++;
9
+ }
10
+ return i;
11
+ }
12
+
13
+ size_t findStoppingStrings(const std::string &text,
14
+ const size_t last_token_size,
15
+ const std::vector<std::string> &stop_words) {
16
+ size_t stop_pos = std::string::npos;
17
+
18
+ for (const std::string &word : stop_words) {
19
+ size_t pos;
20
+
21
+ const size_t tmp = word.size() + last_token_size;
22
+ const size_t from_pos = text.size() > tmp ? text.size() - tmp : 0;
23
+
24
+ pos = text.find(word, from_pos);
25
+
26
+ if (pos != std::string::npos &&
27
+ (stop_pos == std::string::npos || pos < stop_pos)) {
28
+ stop_pos = pos;
29
+ }
30
+ }
31
+
32
+ return stop_pos;
33
+ }
34
+
35
+ LlamaCompletionWorker::LlamaCompletionWorker(
36
+ const Napi::CallbackInfo &info, LlamaSessionPtr &sess,
37
+ Napi::Function callback, gpt_params params,
38
+ std::vector<std::string> stop_words)
39
+ : AsyncWorker(info.Env()), Deferred(info.Env()), _sess(sess),
40
+ _params(params), _stop_words(stop_words) {
41
+ if (!callback.IsEmpty()) {
42
+ _tsfn = Napi::ThreadSafeFunction::New(info.Env(), callback,
43
+ "LlamaCompletionCallback", 0, 1);
44
+ _has_callback = true;
45
+ }
46
+ }
47
+
48
+ LlamaCompletionWorker::~LlamaCompletionWorker() {
49
+ if (_has_callback) {
50
+ _tsfn.Release();
51
+ }
52
+ }
53
+
54
+ void LlamaCompletionWorker::Execute() {
55
+ _sess->get_mutex().lock();
56
+ const auto t_main_start = ggml_time_us();
57
+ const size_t n_ctx = _params.n_ctx;
58
+ const auto n_keep = _params.n_keep;
59
+ size_t n_cur = 0;
60
+ size_t n_input = 0;
61
+ const auto model = llama_get_model(_sess->context());
62
+ const bool add_bos = llama_should_add_bos_token(model);
63
+ auto ctx = _sess->context();
64
+
65
+ llama_set_rng_seed(ctx, _params.seed);
66
+
67
+ LlamaCppSampling sampling{llama_sampling_init(_params.sparams),
68
+ llama_sampling_free};
69
+
70
+ std::vector<llama_token> prompt_tokens =
71
+ ::llama_tokenize(ctx, _params.prompt, add_bos);
72
+ n_input = prompt_tokens.size();
73
+ if (_sess->tokens_ptr()->size() > 0) {
74
+ n_cur = common_part(*(_sess->tokens_ptr()), prompt_tokens);
75
+ if (n_cur == n_input) {
76
+ --n_cur;
77
+ }
78
+ n_input -= n_cur;
79
+ llama_kv_cache_seq_rm(ctx, 0, n_cur, -1);
80
+ }
81
+ _sess->set_tokens(std::move(prompt_tokens));
82
+
83
+ const int max_len = _params.n_predict < 0 ? 0 : _params.n_predict;
84
+ _sess->tokens_ptr()->reserve(_sess->tokens_ptr()->size() + max_len);
85
+
86
+ auto embd = _sess->tokens_ptr();
87
+ for (int i = 0; i < max_len || _stop; i++) {
88
+ // check if we need to remove some tokens
89
+ if (embd->size() >= _params.n_ctx) {
90
+ const int n_left = n_cur - n_keep - 1;
91
+ const int n_discard = n_left / 2;
92
+
93
+ llama_kv_cache_seq_rm(ctx, 0, n_keep + 1, n_keep + n_discard + 1);
94
+ llama_kv_cache_seq_add(ctx, 0, n_keep + 1 + n_discard, n_cur, -n_discard);
95
+
96
+ // shift the tokens
97
+ embd->insert(embd->begin() + n_keep + 1,
98
+ embd->begin() + n_keep + 1 + n_discard, embd->end());
99
+ embd->resize(embd->size() - n_discard);
100
+
101
+ n_cur -= n_discard;
102
+ _result.truncated = true;
103
+ }
104
+ int ret = llama_decode(
105
+ ctx, llama_batch_get_one(embd->data() + n_cur, n_input, n_cur, 0));
106
+ if (ret < 0) {
107
+ SetError("Failed to decode token, code: " + std::to_string(ret));
108
+ break;
109
+ }
110
+ // sample the next token
111
+ const llama_token new_token_id =
112
+ llama_sampling_sample(sampling.get(), ctx, nullptr);
113
+ // prepare the next batch
114
+ embd->emplace_back(new_token_id);
115
+ auto token = llama_token_to_piece(ctx, new_token_id);
116
+ _result.text += token;
117
+ n_cur += n_input;
118
+ _result.tokens_evaluated += n_input;
119
+ _result.tokens_predicted += 1;
120
+ n_input = 1;
121
+ if (_has_callback) {
122
+ const char *c_token = strdup(token.c_str());
123
+ _tsfn.BlockingCall(c_token, [](Napi::Env env, Napi::Function jsCallback,
124
+ const char *value) {
125
+ auto obj = Napi::Object::New(env);
126
+ obj.Set("token", Napi::String::New(env, value));
127
+ delete value;
128
+ jsCallback.Call({obj});
129
+ });
130
+ }
131
+ // is it an end of generation?
132
+ if (llama_token_is_eog(model, new_token_id)) {
133
+ break;
134
+ }
135
+ // check for stop words
136
+ if (!_stop_words.empty()) {
137
+ const size_t stop_pos =
138
+ findStoppingStrings(_result.text, token.size(), _stop_words);
139
+ if (stop_pos != std::string::npos) {
140
+ break;
141
+ }
142
+ }
143
+ }
144
+ const auto t_main_end = ggml_time_us();
145
+ _sess->get_mutex().unlock();
146
+ }
147
+
148
+ void LlamaCompletionWorker::OnOK() {
149
+ auto result = Napi::Object::New(Napi::AsyncWorker::Env());
150
+ result.Set("tokens_evaluated", Napi::Number::New(Napi::AsyncWorker::Env(),
151
+ _result.tokens_evaluated));
152
+ result.Set("tokens_predicted", Napi::Number::New(Napi::AsyncWorker::Env(),
153
+ _result.tokens_predicted));
154
+ result.Set("truncated",
155
+ Napi::Boolean::New(Napi::AsyncWorker::Env(), _result.truncated));
156
+ result.Set("text",
157
+ Napi::String::New(Napi::AsyncWorker::Env(), _result.text.c_str()));
158
+ Napi::Promise::Deferred::Resolve(result);
159
+ }
160
+
161
+ void LlamaCompletionWorker::OnError(const Napi::Error &err) {
162
+ Napi::Promise::Deferred::Reject(err.Value());
163
+ }
@@ -0,0 +1,34 @@
1
+ #include "common.hpp"
2
+
3
+ struct CompletionResult {
4
+ std::string text = "";
5
+ bool truncated = false;
6
+ size_t tokens_predicted = 0;
7
+ size_t tokens_evaluated = 0;
8
+ };
9
+
10
+ class LlamaCompletionWorker : public Napi::AsyncWorker,
11
+ public Napi::Promise::Deferred {
12
+ public:
13
+ LlamaCompletionWorker(const Napi::CallbackInfo &info, LlamaSessionPtr &sess,
14
+ Napi::Function callback, gpt_params params,
15
+ std::vector<std::string> stop_words = {});
16
+
17
+ ~LlamaCompletionWorker();
18
+
19
+ inline void Stop() { _stop = true; }
20
+
21
+ protected:
22
+ void Execute();
23
+ void OnOK();
24
+ void OnError(const Napi::Error &err);
25
+
26
+ private:
27
+ LlamaSessionPtr _sess;
28
+ gpt_params _params;
29
+ std::vector<std::string> _stop_words;
30
+ Napi::ThreadSafeFunction _tsfn;
31
+ bool _has_callback = false;
32
+ bool _stop = false;
33
+ CompletionResult _result;
34
+ };
@@ -0,0 +1,200 @@
1
+ #include "LlamaContext.h"
2
+ #include "DisposeWorker.h"
3
+ #include "LlamaCompletionWorker.h"
4
+ #include "LoadSessionWorker.h"
5
+ #include "SaveSessionWorker.h"
6
+
7
+ void LlamaContext::Init(Napi::Env env, Napi::Object &exports) {
8
+ Napi::Function func = DefineClass(
9
+ env, "LlamaContext",
10
+ {InstanceMethod<&LlamaContext::GetSystemInfo>(
11
+ "getSystemInfo",
12
+ static_cast<napi_property_attributes>(napi_enumerable)),
13
+ InstanceMethod<&LlamaContext::Completion>(
14
+ "completion",
15
+ static_cast<napi_property_attributes>(napi_enumerable)),
16
+ InstanceMethod<&LlamaContext::StopCompletion>(
17
+ "stopCompletion",
18
+ static_cast<napi_property_attributes>(napi_enumerable)),
19
+ InstanceMethod<&LlamaContext::SaveSession>(
20
+ "saveSession",
21
+ static_cast<napi_property_attributes>(napi_enumerable)),
22
+ InstanceMethod<&LlamaContext::LoadSession>(
23
+ "loadSession",
24
+ static_cast<napi_property_attributes>(napi_enumerable)),
25
+ InstanceMethod<&LlamaContext::Release>(
26
+ "release", static_cast<napi_property_attributes>(napi_enumerable))});
27
+ Napi::FunctionReference *constructor = new Napi::FunctionReference();
28
+ *constructor = Napi::Persistent(func);
29
+ #if NAPI_VERSION > 5
30
+ env.SetInstanceData(constructor);
31
+ #endif
32
+ exports.Set("LlamaContext", func);
33
+ }
34
+
35
+ // construct({ model, embedding, n_ctx, n_batch, n_threads, n_gpu_layers,
36
+ // use_mlock, use_mmap }): LlamaContext throws error
37
+ LlamaContext::LlamaContext(const Napi::CallbackInfo &info)
38
+ : Napi::ObjectWrap<LlamaContext>(info) {
39
+ Napi::Env env = info.Env();
40
+ if (info.Length() < 1 || !info[0].IsObject()) {
41
+ Napi::TypeError::New(env, "Object expected").ThrowAsJavaScriptException();
42
+ }
43
+ auto options = info[0].As<Napi::Object>();
44
+
45
+ gpt_params params;
46
+ params.model = get_option<std::string>(options, "model", "");
47
+ if (params.model.empty()) {
48
+ Napi::TypeError::New(env, "Model is required").ThrowAsJavaScriptException();
49
+ }
50
+ params.embedding = get_option<bool>(options, "embedding", false);
51
+ params.n_ctx = get_option<int32_t>(options, "n_ctx", 512);
52
+ params.n_batch = get_option<int32_t>(options, "n_batch", 2048);
53
+ params.n_threads =
54
+ get_option<int32_t>(options, "n_threads", get_math_cpu_count() / 2);
55
+ params.n_gpu_layers = get_option<int32_t>(options, "n_gpu_layers", -1);
56
+ params.use_mlock = get_option<bool>(options, "use_mlock", false);
57
+ params.use_mmap = get_option<bool>(options, "use_mmap", true);
58
+ params.numa =
59
+ static_cast<ggml_numa_strategy>(get_option<uint32_t>(options, "numa", 0));
60
+
61
+ llama_backend_init();
62
+ llama_numa_init(params.numa);
63
+
64
+ llama_model *model;
65
+ llama_context *ctx;
66
+ std::tie(model, ctx) = llama_init_from_gpt_params(params);
67
+
68
+ if (model == nullptr || ctx == nullptr) {
69
+ Napi::TypeError::New(env, "Failed to load model")
70
+ .ThrowAsJavaScriptException();
71
+ }
72
+
73
+ _sess = std::make_shared<LlamaSession>(ctx, params);
74
+ _info = get_system_info(params);
75
+ }
76
+
77
+ // getSystemInfo(): string
78
+ Napi::Value LlamaContext::GetSystemInfo(const Napi::CallbackInfo &info) {
79
+ return Napi::String::New(info.Env(), _info);
80
+ }
81
+
82
+ // completion(options: LlamaCompletionOptions, onToken?: (token: string) =>
83
+ // void): Promise<LlamaCompletionResult>
84
+ Napi::Value LlamaContext::Completion(const Napi::CallbackInfo &info) {
85
+ Napi::Env env = info.Env();
86
+ if (info.Length() < 1 || !info[0].IsObject()) {
87
+ Napi::TypeError::New(env, "Object expected").ThrowAsJavaScriptException();
88
+ }
89
+ if (info.Length() >= 2 && !info[1].IsFunction()) {
90
+ Napi::TypeError::New(env, "Function expected").ThrowAsJavaScriptException();
91
+ }
92
+ if (_sess == nullptr) {
93
+ Napi::TypeError::New(env, "Context is disposed")
94
+ .ThrowAsJavaScriptException();
95
+ }
96
+ auto options = info[0].As<Napi::Object>();
97
+
98
+ gpt_params params = _sess->params();
99
+ params.prompt = get_option<std::string>(options, "prompt", "");
100
+ if (params.prompt.empty()) {
101
+ Napi::TypeError::New(env, "Prompt is required")
102
+ .ThrowAsJavaScriptException();
103
+ }
104
+ params.n_predict = get_option<int32_t>(options, "n_predict", -1);
105
+ params.sparams.temp = get_option<float>(options, "temperature", 0.80f);
106
+ params.sparams.top_k = get_option<int32_t>(options, "top_k", 40);
107
+ params.sparams.top_p = get_option<float>(options, "top_p", 0.95f);
108
+ params.sparams.min_p = get_option<float>(options, "min_p", 0.05f);
109
+ params.sparams.tfs_z = get_option<float>(options, "tfs_z", 1.00f);
110
+ params.sparams.mirostat = get_option<int32_t>(options, "mirostat", 0.00f);
111
+ params.sparams.mirostat_tau =
112
+ get_option<float>(options, "mirostat_tau", 5.00f);
113
+ params.sparams.mirostat_eta =
114
+ get_option<float>(options, "mirostat_eta", 0.10f);
115
+ params.sparams.penalty_last_n =
116
+ get_option<int32_t>(options, "penalty_last_n", 64);
117
+ params.sparams.penalty_repeat =
118
+ get_option<float>(options, "penalty_repeat", 1.00f);
119
+ params.sparams.penalty_freq =
120
+ get_option<float>(options, "penalty_freq", 0.00f);
121
+ params.sparams.penalty_present =
122
+ get_option<float>(options, "penalty_present", 0.00f);
123
+ params.sparams.penalize_nl = get_option<bool>(options, "penalize_nl", false);
124
+ params.sparams.typical_p = get_option<float>(options, "typical_p", 1.00f);
125
+ params.ignore_eos = get_option<float>(options, "ignore_eos", false);
126
+ params.sparams.grammar = get_option<std::string>(options, "grammar", "");
127
+ params.n_keep = get_option<int32_t>(options, "n_keep", 0);
128
+ params.seed = get_option<int32_t>(options, "seed", LLAMA_DEFAULT_SEED);
129
+ std::vector<std::string> stop_words;
130
+ if (options.Has("stop") && options.Get("stop").IsArray()) {
131
+ auto stop_words_array = options.Get("stop").As<Napi::Array>();
132
+ for (size_t i = 0; i < stop_words_array.Length(); i++) {
133
+ stop_words.push_back(stop_words_array.Get(i).ToString().Utf8Value());
134
+ }
135
+ }
136
+
137
+ Napi::Function callback;
138
+ if (info.Length() >= 2) {
139
+ callback = info[1].As<Napi::Function>();
140
+ }
141
+
142
+ auto *worker =
143
+ new LlamaCompletionWorker(info, _sess, callback, params, stop_words);
144
+ worker->Queue();
145
+ _wip = worker;
146
+ return worker->Promise();
147
+ }
148
+
149
+ // stopCompletion(): void
150
+ void LlamaContext::StopCompletion(const Napi::CallbackInfo &info) {
151
+ if (_wip != nullptr) {
152
+ _wip->Stop();
153
+ }
154
+ }
155
+
156
+ // saveSession(path: string): Promise<void> throws error
157
+ Napi::Value LlamaContext::SaveSession(const Napi::CallbackInfo &info) {
158
+ Napi::Env env = info.Env();
159
+ if (info.Length() < 1 || !info[0].IsString()) {
160
+ Napi::TypeError::New(env, "String expected").ThrowAsJavaScriptException();
161
+ }
162
+ if (_sess == nullptr) {
163
+ Napi::TypeError::New(env, "Context is disposed")
164
+ .ThrowAsJavaScriptException();
165
+ }
166
+ auto *worker = new SaveSessionWorker(info, _sess);
167
+ worker->Queue();
168
+ return worker->Promise();
169
+ }
170
+
171
+ // loadSession(path: string): Promise<{ count }> throws error
172
+ Napi::Value LlamaContext::LoadSession(const Napi::CallbackInfo &info) {
173
+ Napi::Env env = info.Env();
174
+ if (info.Length() < 1 || !info[0].IsString()) {
175
+ Napi::TypeError::New(env, "String expected").ThrowAsJavaScriptException();
176
+ }
177
+ if (_sess == nullptr) {
178
+ Napi::TypeError::New(env, "Context is disposed")
179
+ .ThrowAsJavaScriptException();
180
+ }
181
+ auto *worker = new LoadSessionWorker(info, _sess);
182
+ worker->Queue();
183
+ return worker->Promise();
184
+ }
185
+
186
+ // release(): Promise<void>
187
+ Napi::Value LlamaContext::Release(const Napi::CallbackInfo &info) {
188
+ auto env = info.Env();
189
+ if (_wip != nullptr) {
190
+ _wip->Stop();
191
+ }
192
+ if (_sess == nullptr) {
193
+ auto promise = Napi::Promise::Deferred(env);
194
+ promise.Resolve(env.Undefined());
195
+ return promise.Promise();
196
+ }
197
+ auto *worker = new DisposeWorker(info, std::move(_sess));
198
+ worker->Queue();
199
+ return worker->Promise();
200
+ }
@@ -0,0 +1,21 @@
1
+ #include "common.hpp"
2
+
3
+ class LlamaCompletionWorker;
4
+
5
+ class LlamaContext : public Napi::ObjectWrap<LlamaContext> {
6
+ public:
7
+ LlamaContext(const Napi::CallbackInfo &info);
8
+ static void Init(Napi::Env env, Napi::Object &exports);
9
+
10
+ private:
11
+ Napi::Value GetSystemInfo(const Napi::CallbackInfo &info);
12
+ Napi::Value Completion(const Napi::CallbackInfo &info);
13
+ void StopCompletion(const Napi::CallbackInfo &info);
14
+ Napi::Value SaveSession(const Napi::CallbackInfo &info);
15
+ Napi::Value LoadSession(const Napi::CallbackInfo &info);
16
+ Napi::Value Release(const Napi::CallbackInfo &info);
17
+
18
+ std::string _info;
19
+ LlamaSessionPtr _sess = nullptr;
20
+ LlamaCompletionWorker *_wip = nullptr;
21
+ };
@@ -0,0 +1,24 @@
1
+ #include "LoadSessionWorker.h"
2
+ #include "LlamaContext.h"
3
+
4
+ LoadSessionWorker::LoadSessionWorker(const Napi::CallbackInfo &info,
5
+ LlamaSessionPtr &sess)
6
+ : AsyncWorker(info.Env()), Deferred(info.Env()), _path(info[0].ToString()),
7
+ _sess(sess) {}
8
+
9
+ void LoadSessionWorker::Execute() {
10
+ _sess->get_mutex().lock();
11
+ // reserve the maximum number of tokens for capacity
12
+ std::vector<llama_token> tokens;
13
+ tokens.reserve(_sess->params().n_ctx);
14
+ if (!llama_state_load_file(_sess->context(), _path.c_str(), tokens.data(),
15
+ tokens.capacity(), &count)) {
16
+ SetError("Failed to load session");
17
+ }
18
+ _sess->set_tokens(std::move(tokens));
19
+ _sess->get_mutex().unlock();
20
+ }
21
+
22
+ void LoadSessionWorker::OnOK() { Resolve(AsyncWorker::Env().Undefined()); }
23
+
24
+ void LoadSessionWorker::OnError(const Napi::Error &err) { Reject(err.Value()); }
@@ -0,0 +1,17 @@
1
+ #include "common.hpp"
2
+
3
+ class LoadSessionWorker : public Napi::AsyncWorker,
4
+ public Napi::Promise::Deferred {
5
+ public:
6
+ LoadSessionWorker(const Napi::CallbackInfo &info, LlamaSessionPtr &sess);
7
+
8
+ protected:
9
+ void Execute();
10
+ void OnOK();
11
+ void OnError(const Napi::Error &err);
12
+
13
+ private:
14
+ std::string _path;
15
+ LlamaSessionPtr _sess;
16
+ size_t count = 0;
17
+ };
@@ -0,0 +1,21 @@
1
+ #include "SaveSessionWorker.h"
2
+ #include "LlamaContext.h"
3
+
4
+ SaveSessionWorker::SaveSessionWorker(const Napi::CallbackInfo &info,
5
+ LlamaSessionPtr &sess)
6
+ : AsyncWorker(info.Env()), Deferred(info.Env()), _path(info[0].ToString()),
7
+ _sess(sess) {}
8
+
9
+ void SaveSessionWorker::Execute() {
10
+ _sess->get_mutex().lock();
11
+ auto tokens = _sess->tokens_ptr();
12
+ if (!llama_state_save_file(_sess->context(), _path.c_str(), tokens->data(),
13
+ tokens->size())) {
14
+ SetError("Failed to save session");
15
+ }
16
+ _sess->get_mutex().unlock();
17
+ }
18
+
19
+ void SaveSessionWorker::OnOK() { Resolve(AsyncWorker::Env().Undefined()); }
20
+
21
+ void SaveSessionWorker::OnError(const Napi::Error &err) { Reject(err.Value()); }
@@ -0,0 +1,16 @@
1
+ #include "common.hpp"
2
+
3
+ class SaveSessionWorker : public Napi::AsyncWorker,
4
+ public Napi::Promise::Deferred {
5
+ public:
6
+ SaveSessionWorker(const Napi::CallbackInfo &info, LlamaSessionPtr &sess);
7
+
8
+ protected:
9
+ void Execute();
10
+ void OnOK();
11
+ void OnError(const Napi::Error &err);
12
+
13
+ private:
14
+ std::string _path;
15
+ LlamaSessionPtr _sess;
16
+ };
package/src/addons.cc ADDED
@@ -0,0 +1,9 @@
1
+ #include "LlamaContext.h"
2
+ #include <napi.h>
3
+
4
+ Napi::Object Init(Napi::Env env, Napi::Object exports) {
5
+ LlamaContext::Init(env, exports);
6
+ return exports;
7
+ }
8
+
9
+ NODE_API_MODULE(addons, Init)
package/src/common.hpp ADDED
@@ -0,0 +1,81 @@
1
+ #pragma once
2
+
3
+ #include "common/common.h"
4
+ #include "llama.h"
5
+ #include <memory>
6
+ #include <mutex>
7
+ #include <napi.h>
8
+ #include <string>
9
+ #include <thread>
10
+ #include <tuple>
11
+ #include <vector>
12
+
13
+ typedef std::unique_ptr<llama_model, decltype(&llama_free_model)> LlamaCppModel;
14
+ typedef std::unique_ptr<llama_context, decltype(&llama_free)> LlamaCppContext;
15
+ typedef std::unique_ptr<llama_sampling_context, decltype(&llama_sampling_free)>
16
+ LlamaCppSampling;
17
+ typedef std::unique_ptr<llama_batch, decltype(&llama_batch_free)> LlamaCppBatch;
18
+
19
+ template <typename T>
20
+ constexpr T get_option(const Napi::Object &options, const std::string &name,
21
+ const T default_value) {
22
+ if (options.Has(name) && !options.Get(name).IsUndefined() &&
23
+ !options.Get(name).IsNull()) {
24
+ if constexpr (std::is_same<T, std::string>::value) {
25
+ return options.Get(name).ToString().operator T();
26
+ } else if constexpr (std::is_same<T, int32_t>::value ||
27
+ std::is_same<T, uint32_t>::value ||
28
+ std::is_same<T, float>::value ||
29
+ std::is_same<T, double>::value) {
30
+ return options.Get(name).ToNumber().operator T();
31
+ } else if constexpr (std::is_same<T, bool>::value) {
32
+ return options.Get(name).ToBoolean().operator T();
33
+ } else {
34
+ static_assert(std::is_same<T, std::string>::value ||
35
+ std::is_same<T, int32_t>::value ||
36
+ std::is_same<T, uint32_t>::value ||
37
+ std::is_same<T, float>::value ||
38
+ std::is_same<T, double>::value ||
39
+ std::is_same<T, bool>::value,
40
+ "Unsupported type");
41
+ }
42
+ } else {
43
+ return default_value;
44
+ }
45
+ }
46
+
47
+ class LlamaSession {
48
+ public:
49
+ LlamaSession(llama_context *ctx, gpt_params params)
50
+ : ctx_(LlamaCppContext(ctx, llama_free)), params_(params) {
51
+ tokens_.reserve(params.n_ctx);
52
+ }
53
+
54
+ ~LlamaSession() { dispose(); }
55
+
56
+ llama_context *context() { return ctx_.get(); }
57
+
58
+ std::vector<llama_token>* tokens_ptr() { return &tokens_; }
59
+
60
+ void set_tokens(std::vector<llama_token> tokens) {
61
+ tokens_ = std::move(tokens);
62
+ }
63
+
64
+ const gpt_params &params() const { return params_; }
65
+
66
+ std::mutex &get_mutex() { return mutex; }
67
+
68
+ void dispose() {
69
+ std::lock_guard<std::mutex> lock(mutex);
70
+ tokens_.clear();
71
+ ctx_.reset();
72
+ }
73
+
74
+ private:
75
+ LlamaCppContext ctx_;
76
+ const gpt_params params_;
77
+ std::vector<llama_token> tokens_{};
78
+ std::mutex mutex;
79
+ };
80
+
81
+ typedef std::shared_ptr<LlamaSession> LlamaSessionPtr;
package/src/addons.cpp DELETED
@@ -1,537 +0,0 @@
1
- #include "common/common.h"
2
- #include "llama.h"
3
- #include <memory>
4
- #include <mutex>
5
- #include <napi.h>
6
- #include <string>
7
- #include <thread>
8
- #include <tuple>
9
- #include <vector>
10
-
11
- typedef std::unique_ptr<llama_model, decltype(&llama_free_model)> LlamaCppModel;
12
- typedef std::unique_ptr<llama_context, decltype(&llama_free)> LlamaCppContext;
13
- typedef std::unique_ptr<llama_sampling_context, decltype(&llama_sampling_free)>
14
- LlamaCppSampling;
15
- typedef std::unique_ptr<llama_batch, decltype(&llama_batch_free)> LlamaCppBatch;
16
-
17
- size_t common_part(const std::vector<llama_token> &a,
18
- const std::vector<llama_token> &b) {
19
- size_t i = 0;
20
- while (i < a.size() && i < b.size() && a[i] == b[i]) {
21
- i++;
22
- }
23
- return i;
24
- }
25
-
26
- template <typename T>
27
- constexpr T get_option(const Napi::Object &options, const std::string &name,
28
- const T default_value) {
29
- if (options.Has(name) && !options.Get(name).IsUndefined() &&
30
- !options.Get(name).IsNull()) {
31
- if constexpr (std::is_same<T, std::string>::value) {
32
- return options.Get(name).ToString().operator T();
33
- } else if constexpr (std::is_same<T, int32_t>::value ||
34
- std::is_same<T, uint32_t>::value ||
35
- std::is_same<T, float>::value ||
36
- std::is_same<T, double>::value) {
37
- return options.Get(name).ToNumber().operator T();
38
- } else if constexpr (std::is_same<T, bool>::value) {
39
- return options.Get(name).ToBoolean().operator T();
40
- } else {
41
- static_assert(std::is_same<T, std::string>::value ||
42
- std::is_same<T, int32_t>::value ||
43
- std::is_same<T, uint32_t>::value ||
44
- std::is_same<T, float>::value ||
45
- std::is_same<T, double>::value ||
46
- std::is_same<T, bool>::value,
47
- "Unsupported type");
48
- }
49
- } else {
50
- return default_value;
51
- }
52
- }
53
-
54
- class LlamaCompletionWorker;
55
-
56
- class LlamaContext : public Napi::ObjectWrap<LlamaContext> {
57
- public:
58
- // construct({ model, embedding, n_ctx, n_batch, n_threads, n_gpu_layers,
59
- // use_mlock, use_mmap }): LlamaContext throws error
60
- LlamaContext(const Napi::CallbackInfo &info)
61
- : Napi::ObjectWrap<LlamaContext>(info) {
62
- Napi::Env env = info.Env();
63
- if (info.Length() < 1 || !info[0].IsObject()) {
64
- Napi::TypeError::New(env, "Object expected").ThrowAsJavaScriptException();
65
- }
66
- auto options = info[0].As<Napi::Object>();
67
-
68
- params.model = get_option<std::string>(options, "model", "");
69
- if (params.model.empty()) {
70
- Napi::TypeError::New(env, "Model is required")
71
- .ThrowAsJavaScriptException();
72
- }
73
- params.embedding = get_option<bool>(options, "embedding", false);
74
- params.n_ctx = get_option<int32_t>(options, "n_ctx", 512);
75
- params.n_batch = get_option<int32_t>(options, "n_batch", 2048);
76
- params.n_threads =
77
- get_option<int32_t>(options, "n_threads", get_math_cpu_count() / 2);
78
- params.n_gpu_layers = get_option<int32_t>(options, "n_gpu_layers", -1);
79
- params.use_mlock = get_option<bool>(options, "use_mlock", false);
80
- params.use_mmap = get_option<bool>(options, "use_mmap", true);
81
- params.numa = static_cast<ggml_numa_strategy>(
82
- get_option<uint32_t>(options, "numa", 0));
83
-
84
- llama_backend_init();
85
- llama_numa_init(params.numa);
86
-
87
- auto tuple = llama_init_from_gpt_params(params);
88
- model.reset(std::get<0>(tuple));
89
- ctx.reset(std::get<1>(tuple));
90
-
91
- if (model == nullptr || ctx == nullptr) {
92
- Napi::TypeError::New(env, "Failed to load model")
93
- .ThrowAsJavaScriptException();
94
- }
95
- }
96
-
97
- static void Export(Napi::Env env, Napi::Object &exports) {
98
- Napi::Function func = DefineClass(
99
- env, "LlamaContext",
100
- {InstanceMethod<&LlamaContext::GetSystemInfo>(
101
- "getSystemInfo",
102
- static_cast<napi_property_attributes>(napi_enumerable)),
103
- InstanceMethod<&LlamaContext::Completion>(
104
- "completion",
105
- static_cast<napi_property_attributes>(napi_enumerable)),
106
- InstanceMethod<&LlamaContext::StopCompletion>(
107
- "stopCompletion",
108
- static_cast<napi_property_attributes>(napi_enumerable)),
109
- InstanceMethod<&LlamaContext::SaveSession>(
110
- "saveSession",
111
- static_cast<napi_property_attributes>(napi_enumerable)),
112
- InstanceMethod<&LlamaContext::LoadSession>(
113
- "loadSession",
114
- static_cast<napi_property_attributes>(napi_enumerable)),
115
- InstanceMethod<&LlamaContext::Release>(
116
- "release",
117
- static_cast<napi_property_attributes>(napi_enumerable))});
118
- Napi::FunctionReference *constructor = new Napi::FunctionReference();
119
- *constructor = Napi::Persistent(func);
120
- #if NAPI_VERSION > 5
121
- env.SetInstanceData(constructor);
122
- #endif
123
- exports.Set("LlamaContext", func);
124
- }
125
-
126
- llama_context *getContext() { return ctx.get(); }
127
- llama_model *getModel() { return model.get(); }
128
-
129
- std::vector<llama_token> *getTokens() { return tokens.get(); }
130
-
131
- const gpt_params &getParams() const { return params; }
132
-
133
- void ensureTokens() {
134
- if (tokens == nullptr) {
135
- tokens = std::make_unique<std::vector<llama_token>>();
136
- }
137
- }
138
-
139
- void setTokens(std::vector<llama_token> tokens) {
140
- this->tokens.reset(new std::vector<llama_token>(std::move(tokens)));
141
- }
142
-
143
- std::mutex &getMutex() { return mutex; }
144
-
145
- void Dispose() {
146
- std::lock_guard<std::mutex> lock(mutex);
147
- compl_worker = nullptr;
148
- ctx.reset();
149
- tokens.reset();
150
- model.reset();
151
- }
152
-
153
- private:
154
- Napi::Value GetSystemInfo(const Napi::CallbackInfo &info);
155
- Napi::Value Completion(const Napi::CallbackInfo &info);
156
- void StopCompletion(const Napi::CallbackInfo &info);
157
- Napi::Value SaveSession(const Napi::CallbackInfo &info);
158
- Napi::Value LoadSession(const Napi::CallbackInfo &info);
159
- Napi::Value Release(const Napi::CallbackInfo &info);
160
-
161
- gpt_params params;
162
- LlamaCppModel model{nullptr, llama_free_model};
163
- LlamaCppContext ctx{nullptr, llama_free};
164
- std::unique_ptr<std::vector<llama_token>> tokens;
165
- std::mutex mutex;
166
- LlamaCompletionWorker *compl_worker = nullptr;
167
- };
168
-
169
- class LlamaCompletionWorker : public Napi::AsyncWorker,
170
- public Napi::Promise::Deferred {
171
- LlamaContext *_ctx;
172
- gpt_params _params;
173
- std::vector<std::string> _stop_words;
174
- std::string generated_text = "";
175
- Napi::ThreadSafeFunction _tsfn;
176
- bool _has_callback = false;
177
- bool _stop = false;
178
- size_t tokens_predicted = 0;
179
- size_t tokens_evaluated = 0;
180
- bool truncated = false;
181
-
182
- public:
183
- LlamaCompletionWorker(const Napi::CallbackInfo &info, LlamaContext *ctx,
184
- Napi::Function callback, gpt_params params,
185
- std::vector<std::string> stop_words = {})
186
- : AsyncWorker(info.Env()), Deferred(info.Env()), _ctx(ctx),
187
- _params(params), _stop_words(stop_words) {
188
- _ctx->Ref();
189
- if (!callback.IsEmpty()) {
190
- _tsfn = Napi::ThreadSafeFunction::New(info.Env(), callback,
191
- "LlamaCompletionCallback", 0, 1);
192
- _has_callback = true;
193
- }
194
- }
195
-
196
- ~LlamaCompletionWorker() {
197
- _ctx->Unref();
198
- if (_has_callback) {
199
- _tsfn.Abort();
200
- _tsfn.Release();
201
- }
202
- }
203
-
204
- void Stop() { _stop = true; }
205
-
206
- protected:
207
- size_t findStoppingStrings(const std::string &text,
208
- const size_t last_token_size) {
209
- size_t stop_pos = std::string::npos;
210
-
211
- for (const std::string &word : _stop_words) {
212
- size_t pos;
213
-
214
- const size_t tmp = word.size() + last_token_size;
215
- const size_t from_pos = text.size() > tmp ? text.size() - tmp : 0;
216
-
217
- pos = text.find(word, from_pos);
218
-
219
- if (pos != std::string::npos &&
220
- (stop_pos == std::string::npos || pos < stop_pos)) {
221
- stop_pos = pos;
222
- }
223
- }
224
-
225
- return stop_pos;
226
- }
227
-
228
- void Execute() {
229
- _ctx->getMutex().lock();
230
- _ctx->ensureTokens();
231
- const auto t_main_start = ggml_time_us();
232
- const size_t n_ctx = _params.n_ctx;
233
- auto n_keep = _params.n_keep;
234
- auto n_predict = _params.n_predict;
235
- size_t n_cur = 0;
236
- size_t n_input = 0;
237
- const bool add_bos = llama_should_add_bos_token(_ctx->getModel());
238
- auto *ctx = _ctx->getContext();
239
-
240
- llama_set_rng_seed(ctx, _params.seed);
241
-
242
- LlamaCppSampling sampling{llama_sampling_init(_params.sparams),
243
- llama_sampling_free};
244
-
245
- std::vector<llama_token> prompt_tokens =
246
- ::llama_tokenize(ctx, _params.prompt, add_bos);
247
- n_input = prompt_tokens.size();
248
- if (_ctx->getTokens() != nullptr) {
249
- n_cur = common_part(*_ctx->getTokens(), prompt_tokens);
250
- if (n_cur == n_input) {
251
- --n_cur;
252
- }
253
- n_input -= n_cur;
254
- llama_kv_cache_seq_rm(ctx, 0, n_cur, -1);
255
- }
256
- _ctx->setTokens(std::move(prompt_tokens));
257
-
258
- const int max_len = _params.n_predict < 0 ? 0 : _params.n_predict;
259
-
260
- for (int i = 0; i < max_len || _stop; i++) {
261
- auto *embd = _ctx->getTokens();
262
- // check if we need to remove some tokens
263
- if (embd->size() >= n_ctx) {
264
- const int n_left = n_cur - n_keep - 1;
265
- const int n_discard = n_left / 2;
266
-
267
- llama_kv_cache_seq_rm(ctx, 0, n_keep + 1, n_keep + n_discard + 1);
268
- llama_kv_cache_seq_add(ctx, 0, n_keep + 1 + n_discard, n_cur,
269
- -n_discard);
270
-
271
- for (size_t i = n_keep + 1 + n_discard; i < embd->size(); i++) {
272
- (*embd)[i - n_discard] = (*embd)[i];
273
- }
274
- embd->resize(embd->size() - n_discard);
275
-
276
- n_cur -= n_discard;
277
- truncated = true;
278
- }
279
- int ret = llama_decode(
280
- ctx, llama_batch_get_one(embd->data() + n_cur, n_input, n_cur, 0));
281
- if (ret < 0) {
282
- SetError("Failed to decode token, code: " + std::to_string(ret));
283
- break;
284
- }
285
- // sample the next token
286
- const llama_token new_token_id =
287
- llama_sampling_sample(sampling.get(), ctx, nullptr);
288
- // prepare the next batch
289
- embd->push_back(new_token_id);
290
- auto token = llama_token_to_piece(ctx, new_token_id);
291
- generated_text += token;
292
- n_cur += n_input;
293
- tokens_evaluated += n_input;
294
- tokens_predicted += 1;
295
- n_input = 1;
296
- if (_has_callback) {
297
- const char *c_token = strdup(token.c_str());
298
- _tsfn.BlockingCall(c_token, [](Napi::Env env, Napi::Function jsCallback,
299
- const char *value) {
300
- auto obj = Napi::Object::New(env);
301
- obj.Set("token", Napi::String::New(env, value));
302
- jsCallback.Call({obj});
303
- });
304
- }
305
- // is it an end of generation?
306
- if (llama_token_is_eog(_ctx->getModel(), new_token_id)) {
307
- break;
308
- }
309
- // check for stop words
310
- if (!_stop_words.empty()) {
311
- const size_t stop_pos =
312
- findStoppingStrings(generated_text, token.size());
313
- if (stop_pos != std::string::npos) {
314
- break;
315
- }
316
- }
317
- }
318
- const auto t_main_end = ggml_time_us();
319
- _ctx->getMutex().unlock();
320
- }
321
-
322
- void OnOK() {
323
- auto result = Napi::Object::New(Napi::AsyncWorker::Env());
324
- result.Set("tokens_evaluated",
325
- Napi::Number::New(Napi::AsyncWorker::Env(), tokens_evaluated));
326
- result.Set("tokens_predicted",
327
- Napi::Number::New(Napi::AsyncWorker::Env(), tokens_predicted));
328
- result.Set("truncated",
329
- Napi::Boolean::New(Napi::AsyncWorker::Env(), truncated));
330
- result.Set("text",
331
- Napi::String::New(Napi::AsyncWorker::Env(), generated_text));
332
- Napi::Promise::Deferred::Resolve(result);
333
- }
334
-
335
- void OnError(const Napi::Error &err) {
336
- Napi::Promise::Deferred::Reject(err.Value());
337
- }
338
- };
339
-
340
- class SaveSessionWorker : public Napi::AsyncWorker,
341
- public Napi::Promise::Deferred {
342
- std::string _path;
343
- LlamaContext *_ctx;
344
-
345
- public:
346
- SaveSessionWorker(const Napi::CallbackInfo &info, LlamaContext *ctx)
347
- : AsyncWorker(info.Env()), Deferred(info.Env()),
348
- _path(info[0].ToString()), _ctx(ctx) {
349
- _ctx->Ref();
350
- }
351
-
352
- protected:
353
- void Execute() {
354
- _ctx->getMutex().lock();
355
- if (_ctx->getTokens() == nullptr) {
356
- SetError("Failed to save session");
357
- return;
358
- }
359
- if (!llama_state_save_file(_ctx->getContext(), _path.c_str(),
360
- _ctx->getTokens()->data(),
361
- _ctx->getTokens()->size())) {
362
- SetError("Failed to save session");
363
- }
364
- _ctx->getMutex().unlock();
365
- }
366
-
367
- void OnOK() { Resolve(AsyncWorker::Env().Undefined()); }
368
-
369
- void OnError(const Napi::Error &err) { Reject(err.Value()); }
370
- };
371
-
372
- class LoadSessionWorker : public Napi::AsyncWorker,
373
- public Napi::Promise::Deferred {
374
- std::string _path;
375
- LlamaContext *_ctx;
376
- size_t count = 0;
377
-
378
- public:
379
- LoadSessionWorker(const Napi::CallbackInfo &info, LlamaContext *ctx)
380
- : AsyncWorker(info.Env()), Deferred(info.Env()),
381
- _path(info[0].ToString()), _ctx(ctx) {
382
- _ctx->Ref();
383
- }
384
-
385
- protected:
386
- void Execute() {
387
- _ctx->getMutex().lock();
388
- _ctx->ensureTokens();
389
- // reserve the maximum number of tokens for capacity
390
- _ctx->getTokens()->reserve(_ctx->getParams().n_ctx);
391
- if (!llama_state_load_file(_ctx->getContext(), _path.c_str(),
392
- _ctx->getTokens()->data(),
393
- _ctx->getTokens()->capacity(), &count)) {
394
- SetError("Failed to load session");
395
- }
396
- _ctx->getMutex().unlock();
397
- }
398
-
399
- void OnOK() { Resolve(AsyncWorker::Env().Undefined()); }
400
-
401
- void OnError(const Napi::Error &err) { Reject(err.Value()); }
402
- };
403
-
404
- class DisposeWorker : public Napi::AsyncWorker, public Napi::Promise::Deferred {
405
- public:
406
- DisposeWorker(Napi::Env env, LlamaContext *ctx)
407
- : AsyncWorker(env), Deferred(env), ctx_(ctx) {
408
- ctx_->Ref();
409
- }
410
-
411
- ~DisposeWorker() { ctx_->Unref(); }
412
-
413
- protected:
414
- void Execute() override { ctx_->Dispose(); }
415
-
416
- void OnOK() override { Resolve(AsyncWorker::Env().Undefined()); }
417
-
418
- void OnError(const Napi::Error &err) override { Reject(err.Value()); }
419
-
420
- private:
421
- LlamaContext *ctx_;
422
- };
423
-
424
- // getSystemInfo(): string
425
- Napi::Value LlamaContext::GetSystemInfo(const Napi::CallbackInfo &info) {
426
- return Napi::String::New(info.Env(), get_system_info(params).c_str());
427
- }
428
-
429
- // completion(options: LlamaCompletionOptions, onToken?: (token: string) =>
430
- // void): Promise<LlamaCompletionResult>
431
- Napi::Value LlamaContext::Completion(const Napi::CallbackInfo &info) {
432
- Napi::Env env = info.Env();
433
- if (info.Length() < 1 || !info[0].IsObject()) {
434
- Napi::TypeError::New(env, "Object expected").ThrowAsJavaScriptException();
435
- }
436
- if (info.Length() >= 2 && !info[1].IsFunction()) {
437
- Napi::TypeError::New(env, "Function expected").ThrowAsJavaScriptException();
438
- }
439
- auto options = info[0].As<Napi::Object>();
440
-
441
- gpt_params params;
442
- params.prompt = get_option<std::string>(options, "prompt", "");
443
- if (params.prompt.empty()) {
444
- Napi::TypeError::New(env, "Prompt is required")
445
- .ThrowAsJavaScriptException();
446
- }
447
- params.n_predict = get_option<int32_t>(options, "n_predict", -1);
448
- params.sparams.temp = get_option<float>(options, "temperature", 0.80f);
449
- params.sparams.top_k = get_option<int32_t>(options, "top_k", 40);
450
- params.sparams.top_p = get_option<float>(options, "top_p", 0.95f);
451
- params.sparams.min_p = get_option<float>(options, "min_p", 0.05f);
452
- params.sparams.tfs_z = get_option<float>(options, "tfs_z", 1.00f);
453
- params.sparams.mirostat = get_option<int32_t>(options, "mirostat", 0.00f);
454
- params.sparams.mirostat_tau =
455
- get_option<float>(options, "mirostat_tau", 5.00f);
456
- params.sparams.mirostat_eta =
457
- get_option<float>(options, "mirostat_eta", 0.10f);
458
- params.sparams.penalty_last_n =
459
- get_option<int32_t>(options, "penalty_last_n", 64);
460
- params.sparams.penalty_repeat =
461
- get_option<float>(options, "penalty_repeat", 1.00f);
462
- params.sparams.penalty_freq =
463
- get_option<float>(options, "penalty_freq", 0.00f);
464
- params.sparams.penalty_present =
465
- get_option<float>(options, "penalty_present", 0.00f);
466
- params.sparams.penalize_nl = get_option<bool>(options, "penalize_nl", false);
467
- params.sparams.typical_p = get_option<float>(options, "typical_p", 1.00f);
468
- params.ignore_eos = get_option<float>(options, "ignore_eos", false);
469
- params.sparams.grammar = get_option<std::string>(options, "grammar", "");
470
- params.n_keep = get_option<int32_t>(options, "n_keep", 0);
471
- params.seed = get_option<int32_t>(options, "seed", LLAMA_DEFAULT_SEED);
472
- std::vector<std::string> stop_words;
473
- if (options.Has("stop") && options.Get("stop").IsArray()) {
474
- auto stop_words_array = options.Get("stop").As<Napi::Array>();
475
- for (size_t i = 0; i < stop_words_array.Length(); i++) {
476
- stop_words.push_back(stop_words_array.Get(i).ToString().Utf8Value());
477
- }
478
- }
479
-
480
- // options.on_sample
481
- Napi::Function callback;
482
- if (info.Length() >= 2) {
483
- callback = info[1].As<Napi::Function>();
484
- }
485
-
486
- auto worker =
487
- new LlamaCompletionWorker(info, this, callback, params, stop_words);
488
- worker->Queue();
489
- compl_worker = worker;
490
- return worker->Promise();
491
- }
492
-
493
- // stopCompletion(): void
494
- void LlamaContext::StopCompletion(const Napi::CallbackInfo &info) {
495
- if (compl_worker != nullptr) {
496
- compl_worker->Stop();
497
- }
498
- }
499
-
500
- // saveSession(path: string): Promise<void> throws error
501
- Napi::Value LlamaContext::SaveSession(const Napi::CallbackInfo &info) {
502
- Napi::Env env = info.Env();
503
- if (info.Length() < 1 || !info[0].IsString()) {
504
- Napi::TypeError::New(env, "String expected").ThrowAsJavaScriptException();
505
- }
506
- auto *worker = new SaveSessionWorker(info, this);
507
- worker->Queue();
508
- return worker->Promise();
509
- }
510
-
511
- // loadSession(path: string): Promise<{ count }> throws error
512
- Napi::Value LlamaContext::LoadSession(const Napi::CallbackInfo &info) {
513
- Napi::Env env = info.Env();
514
- if (info.Length() < 1 || !info[0].IsString()) {
515
- Napi::TypeError::New(env, "String expected").ThrowAsJavaScriptException();
516
- }
517
- auto *worker = new LoadSessionWorker(info, this);
518
- worker->Queue();
519
- return worker->Promise();
520
- }
521
-
522
- // release(): Promise<void>
523
- Napi::Value LlamaContext::Release(const Napi::CallbackInfo &info) {
524
- if (compl_worker != nullptr) {
525
- compl_worker->Stop();
526
- }
527
- auto *worker = new DisposeWorker(info.Env(), this);
528
- worker->Queue();
529
- return worker->Promise();
530
- }
531
-
532
- Napi::Object Init(Napi::Env env, Napi::Object exports) {
533
- LlamaContext::Export(env, exports);
534
- return exports;
535
- }
536
-
537
- NODE_API_MODULE(addons, Init)