From fa7052130fde975ffef3a84ab5210b3f2b91d7d0 Mon Sep 17 00:00:00 2001 From: michaljarnot Date: Sat, 27 Jul 2024 19:14:34 +0200 Subject: [PATCH] fix: simplify --- cpp/src/LlamaCPPBinding.cpp | 22 ---------------------- cpp/src/LlamaCPPBinding.h | 1 - example/index.js | 13 +++++++++++++ example/test.js | 24 ------------------------ index.d.ts | 1 - package.json | 4 ++-- yarn.lock | 8 ++++---- 7 files changed, 19 insertions(+), 54 deletions(-) create mode 100644 example/index.js delete mode 100644 example/test.js diff --git a/cpp/src/LlamaCPPBinding.cpp b/cpp/src/LlamaCPPBinding.cpp index 9368191..603d2a9 100644 --- a/cpp/src/LlamaCPPBinding.cpp +++ b/cpp/src/LlamaCPPBinding.cpp @@ -9,7 +9,6 @@ Napi::Object LlamaCPPBinding::Init(Napi::Env env, Napi::Object exports) { Napi::Function func = DefineClass(env, "LlamaCPP", { InstanceMethod("initialize", &LlamaCPPBinding::Initialize), InstanceMethod("runQuery", &LlamaCPPBinding::RunQuery), - InstanceMethod("runQueryStream", &LlamaCPPBinding::RunQueryStream), }); constructor = Napi::Persistent(func); @@ -59,24 +58,3 @@ Napi::Value LlamaCPPBinding::RunQuery(const Napi::CallbackInfo& info) { std::string response = llama_->RunQuery(prompt, max_tokens); return Napi::String::New(env, response); } - -Napi::Value LlamaCPPBinding::RunQueryStream(const Napi::CallbackInfo& info) { - Napi::Env env = info.Env(); - if (info.Length() < 2 || !info[0].IsString() || !info[1].IsFunction()) { - Napi::TypeError::New(env, "String and function expected").ThrowAsJavaScriptException(); - return env.Null(); - } - - std::string prompt = info[0].As().Utf8Value(); - Napi::Function callback = info[1].As(); - size_t max_tokens = 1000; - if (info.Length() > 2 && info[2].IsNumber()) { - max_tokens = info[2].As().Uint32Value(); - } - - llama_->RunQueryStream(prompt, max_tokens, [&env, &callback](const std::string& token) { - callback.Call(env.Global(), {Napi::String::New(env, token)}); - }); - - return env.Undefined(); -} diff --git a/cpp/src/LlamaCPPBinding.h b/cpp/src/LlamaCPPBinding.h index 6991990..1790edb 100644 --- a/cpp/src/LlamaCPPBinding.h +++ b/cpp/src/LlamaCPPBinding.h @@ -13,7 +13,6 @@ class LlamaCPPBinding : public Napi::ObjectWrap { Napi::Value Initialize(const Napi::CallbackInfo& info); Napi::Value RunQuery(const Napi::CallbackInfo& info); - Napi::Value RunQueryStream(const Napi::CallbackInfo& info); std::unique_ptr llama_; }; diff --git a/example/index.js b/example/index.js new file mode 100644 index 0000000..f63ef70 --- /dev/null +++ b/example/index.js @@ -0,0 +1,13 @@ +const Llama = require('../index.js'); + +async function main() { + const llama = new Llama(); + + llama.initialize(__dirname + "/models/Meta-Llama-3.1-8B-Instruct-Q3_K_S.gguf"); + + console.log("\nRunning a simple query:"); + const response = llama.runQuery("Tell me a short story.", 100); + console.log("Response:", response); +} + +main().catch(console.error); diff --git a/example/test.js b/example/test.js deleted file mode 100644 index 90a85f9..0000000 --- a/example/test.js +++ /dev/null @@ -1,24 +0,0 @@ -const Llama = require('../index.js'); - -async function main() { - const llama = new Llama(); - - console.log("Initializing model..."); - const initialized = llama.initialize(__dirname + "/models/Meta-Llama-3.1-8B-Instruct-Q3_K_S.gguf"); - if (!initialized) { - console.error("Failed to initialize the model."); - return; - } - console.log("Model initialized successfully."); - - console.log("\nRunning a simple query:"); - const response = llama.runQuery("What is the capital of France?", 100); - console.log("Response:", response); - - console.log("\nRunning a streaming query:"); - llama.runQueryStream("Tell me a short story about a robot.", (token) => { - console.log(token); - }, 1000); -} - -main().catch(console.error); diff --git a/index.d.ts b/index.d.ts index d89f532..00d9006 100644 --- a/index.d.ts +++ b/index.d.ts @@ -2,5 +2,4 @@ export class Llama { constructor(); initialize(modelPath: string, contextSize?: number): boolean; runQuery(prompt: string, maxTokens?: number): string; - runQueryStream(prompt: string, callback: (token: string) => void, maxTokens: number): void; } diff --git a/package.json b/package.json index 4fdb54c..859afd8 100644 --- a/package.json +++ b/package.json @@ -13,13 +13,13 @@ "node": ">=18.0.0" }, "dependencies": { - "node-addon-api": "6.1.0", + "node-addon-api": "8.1.0", "cmake-js": "7.2.1" }, "scripts": { "install": "cmake-js compile -d cpp print-configure", "build": "cmake-js compile -d cpp print-configure", - "example": "node example/test.js" + "example": "node example/index.js" }, "keywords": [ "node", diff --git a/yarn.lock b/yarn.lock index 4b0ebe5..2608fb3 100644 --- a/yarn.lock +++ b/yarn.lock @@ -2498,10 +2498,10 @@ nerf-dart@^1.0.0: resolved "https://registry.yarnpkg.com/nerf-dart/-/nerf-dart-1.0.0.tgz#e6dab7febf5ad816ea81cf5c629c5a0ebde72c1a" integrity sha512-EZSPZB70jiVsivaBLYDCyntd5eH8NTSMOn3rB+HxwdmKThGELLdYv8qVIMWvZEFy9w8ZZpW9h9OB32l1rGtj7g== -node-addon-api@6.1.0: - version "6.1.0" - resolved "https://registry.yarnpkg.com/node-addon-api/-/node-addon-api-6.1.0.tgz#ac8470034e58e67d0c6f1204a18ae6995d9c0d76" - integrity sha512-+eawOlIgy680F0kBzPUNFhMZGtJ1YmqM6l4+Crf4IkImjYrO/mqPwRMh352g23uIaQKFItcQ64I7KMaJxHgAVA== +node-addon-api@8.1.0: + version "8.1.0" + resolved "https://registry.yarnpkg.com/node-addon-api/-/node-addon-api-8.1.0.tgz#55a573685dd4bd053f189cffa4e6332d2b1f1645" + integrity sha512-yBY+qqWSv3dWKGODD6OGE6GnTX7Q2r+4+DfpqxHSHh8x0B4EKP9+wVGLS6U/AM1vxSNNmUEuIV5EGhYwPpfOwQ== node-api-headers@^0.0.2: version "0.0.2"