Skip to content

Commit

Permalink
fix: simplify
Browse files Browse the repository at this point in the history
  • Loading branch information
developer239 committed Jul 27, 2024
1 parent 0999a44 commit fa70521
Show file tree
Hide file tree
Showing 7 changed files with 19 additions and 54 deletions.
22 changes: 0 additions & 22 deletions cpp/src/LlamaCPPBinding.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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<Napi::String>().Utf8Value();
Napi::Function callback = info[1].As<Napi::Function>();
size_t max_tokens = 1000;
if (info.Length() > 2 && info[2].IsNumber()) {
max_tokens = info[2].As<Napi::Number>().Uint32Value();
}

llama_->RunQueryStream(prompt, max_tokens, [&env, &callback](const std::string& token) {
callback.Call(env.Global(), {Napi::String::New(env, token)});
});

return env.Undefined();
}
1 change: 0 additions & 1 deletion cpp/src/LlamaCPPBinding.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ class LlamaCPPBinding : public Napi::ObjectWrap<LlamaCPPBinding> {

Napi::Value Initialize(const Napi::CallbackInfo& info);
Napi::Value RunQuery(const Napi::CallbackInfo& info);
Napi::Value RunQueryStream(const Napi::CallbackInfo& info);

std::unique_ptr<LlamaWrapper> llama_;
};
13 changes: 13 additions & 0 deletions example/index.js
Original file line number Diff line number Diff line change
@@ -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);
24 changes: 0 additions & 24 deletions example/test.js

This file was deleted.

1 change: 0 additions & 1 deletion index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
8 changes: 4 additions & 4 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down

0 comments on commit fa70521

Please sign in to comment.