diff --git a/CMakeLists.txt b/CMakeLists.txt index 9676027..4aec819 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -77,6 +77,12 @@ file( "src/LlamaCompletionWorker.h" "src/LlamaContext.cpp" "src/LlamaContext.h" + "src/TokenizeWorker.cpp" + "src/TokenizeWorker.h" + "src/DetokenizeWorker.cpp" + "src/DetokenizeWorker.h" + "src/EmbeddingWorker.cpp" + "src/EmbeddingWorker.h" "src/LoadSessionWorker.cpp" "src/LoadSessionWorker.h" "src/SaveSessionWorker.cpp" diff --git a/README.md b/README.md index 9972e2e..8ac931a 100644 --- a/README.md +++ b/README.md @@ -47,7 +47,8 @@ console.log('Result:', text) ## Lib Variants -- [x] `default`: General usage, Supported GPU: Metal (macOS) and Vulkan (Linux / Windows) +- [x] `default`: General usage, not support GPU except macOS (Metal) +- [x] `vulkan`: Support GPU Vulkan (Windows/Linux), but some scenario might unstable ## License diff --git a/lib/binding.ts b/lib/binding.ts index 0d270f5..6ec4089 100644 --- a/lib/binding.ts +++ b/lib/binding.ts @@ -37,11 +37,22 @@ export type LlamaCompletionToken = { token: string } +export type TokenizeResult = { + tokens: Int32Array +} + +export type EmbeddingResult = { + embedding: Float32Array +} + export interface LlamaContext { new (options: LlamaModelOptions): LlamaContext getSystemInfo(): string completion(options: LlamaCompletionOptions, callback?: (token: LlamaCompletionToken) => void): Promise stopCompletion(): void + tokenize(text: string): Promise + detokenize(tokens: number[]): Promise + embedding(text: string): Promise saveSession(path: string): Promise loadSession(path: string): Promise release(): Promise diff --git a/scripts/build-linux.sh b/scripts/build-linux.sh index 9cd2a44..8bbf859 100755 --- a/scripts/build-linux.sh +++ b/scripts/build-linux.sh @@ -5,7 +5,9 @@ set -e # General if [ $(uname -m) == "x86_64" ]; then - yarn clean && yarn build-native --CDLLAMA_VULKAN=1 + yarn clean && yarn build-native + yarn clean && yarn build-native --CDLLAMA_VULKAN=1 --CDVARIANT=vulkan else - yarn clean && yarn build-native --CDLLAMA_VULKAN=1 --CDVULKAN_SDK="$(realpath 'externals/arm64-Vulkan-SDK')" + yarn clean && yarn build-native + yarn clean && yarn build-native --CDLLAMA_VULKAN=1 --CDVULKAN_SDK="$(realpath 'externals/arm64-Vulkan-SDK')" --CDVARIANT=vulkan fi diff --git a/scripts/build-windows.ps1 b/scripts/build-windows.ps1 index ecd13f1..a99036e 100644 --- a/scripts/build-windows.ps1 +++ b/scripts/build-windows.ps1 @@ -2,5 +2,10 @@ $ErrorActionPreference='Stop' # General -yarn clean ; yarn build-native -a x86_64 --CDCMAKE_PREFIX_PATH=(Resolve-Path 'externals/win32-x64/Vulkan-SDK') -yarn clean ; yarn build-native -a arm64 --CDCMAKE_PREFIX_PATH=(Resolve-Path 'externals/win32-arm64/Vulkan-SDK') +yarn clean ; yarn build-native -a x86_64 +yarn clean ; yarn build-native -a arm64 + +# Vulkan, might crash on some senerios + +yarn clean ; yarn build-native -a x86_64 --CDVULKAN_SDK=(Resolve-Path 'externals/win32-x64/Vulkan-SDK') --CDVARIANT=vulkan --CDLLAMA_VULKAN=1 +yarn clean ; yarn build-native -a arm64 --CDVULKAN_SDK=(Resolve-Path 'externals/win32-arm64/Vulkan-SDK') --CDVARIANT=vulkan --CDLLAMA_VULKAN=1 diff --git a/src/DetokenizeWorker.cpp b/src/DetokenizeWorker.cpp new file mode 100644 index 0000000..c04497a --- /dev/null +++ b/src/DetokenizeWorker.cpp @@ -0,0 +1,22 @@ +#include "DetokenizeWorker.h" +#include "LlamaContext.h" + +DetokenizeWorker::DetokenizeWorker(const Napi::CallbackInfo &info, + LlamaSessionPtr &sess, + std::vector &tokens) + : AsyncWorker(info.Env()), Deferred(info.Env()), _sess(sess), + _tokens(std::move(tokens)) {} + +void DetokenizeWorker::Execute() { + const auto text = ::llama_detokenize_bpe(_sess->context(), _tokens); + _text = std::move(text); +} + +void DetokenizeWorker::OnOK() { + Napi::Promise::Deferred::Resolve( + Napi::String::New(Napi::AsyncWorker::Env(), _text)); +} + +void DetokenizeWorker::OnError(const Napi::Error &err) { + Napi::Promise::Deferred::Reject(err.Value()); +} diff --git a/src/DetokenizeWorker.h b/src/DetokenizeWorker.h new file mode 100644 index 0000000..97d8703 --- /dev/null +++ b/src/DetokenizeWorker.h @@ -0,0 +1,19 @@ +#include "common.hpp" +#include + +class DetokenizeWorker : public Napi::AsyncWorker, + public Napi::Promise::Deferred { +public: + DetokenizeWorker(const Napi::CallbackInfo &info, LlamaSessionPtr &sess, + std::vector &tokens); + +protected: + void Execute(); + void OnOK(); + void OnError(const Napi::Error &err); + +private: + LlamaSessionPtr _sess; + std::vector _tokens; + std::string _text; +}; diff --git a/src/EmbeddingWorker.cpp b/src/EmbeddingWorker.cpp new file mode 100644 index 0000000..9e268e9 --- /dev/null +++ b/src/EmbeddingWorker.cpp @@ -0,0 +1,46 @@ +#include "EmbeddingWorker.h" +#include "LlamaContext.h" + +EmbeddingWorker::EmbeddingWorker(const Napi::CallbackInfo &info, + LlamaSessionPtr &sess, std::string text) + : AsyncWorker(info.Env()), Deferred(info.Env()), _sess(sess), _text(text) {} + +void EmbeddingWorker::Execute() { + llama_kv_cache_clear(_sess->context()); + auto tokens = ::llama_tokenize(_sess->context(), _text, true); + // add SEP if not present + if (tokens.empty() || tokens.back() != llama_token_sep(_sess->model())) { + tokens.push_back(llama_token_sep(_sess->model())); + } + const int n_embd = llama_n_embd(_sess->model()); + do { + int ret = + llama_decode(_sess->context(), + llama_batch_get_one(tokens.data(), tokens.size(), 0, 0)); + if (ret < 0) { + SetError("Failed to inference, code: " + std::to_string(ret)); + break; + } + const float *embd = llama_get_embeddings_seq(_sess->context(), 0); + if (embd == nullptr) { + SetError("Failed to get embeddings"); + break; + } + _result.embedding.resize(n_embd); + memcpy(_result.embedding.data(), embd, n_embd * sizeof(float)); + } while (false); +} + +void EmbeddingWorker::OnOK() { + auto result = Napi::Object::New(Napi::AsyncWorker::Env()); + auto embedding = Napi::Float32Array::New(Napi::AsyncWorker::Env(), + _result.embedding.size()); + memcpy(embedding.Data(), _result.embedding.data(), + _result.embedding.size() * sizeof(float)); + result.Set("embedding", embedding); + Napi::Promise::Deferred::Resolve(result); +} + +void EmbeddingWorker::OnError(const Napi::Error &err) { + Napi::Promise::Deferred::Reject(err.Value()); +} diff --git a/src/EmbeddingWorker.h b/src/EmbeddingWorker.h new file mode 100644 index 0000000..a21ffbf --- /dev/null +++ b/src/EmbeddingWorker.h @@ -0,0 +1,23 @@ +#include "common.hpp" +#include + +struct EmbeddingResult { + std::vector embedding; +}; + +class EmbeddingWorker : public Napi::AsyncWorker, + public Napi::Promise::Deferred { +public: + EmbeddingWorker(const Napi::CallbackInfo &info, LlamaSessionPtr &sess, + std::string text); + +protected: + void Execute(); + void OnOK(); + void OnError(const Napi::Error &err); + +private: + LlamaSessionPtr _sess; + std::string _text; + EmbeddingResult _result; +}; diff --git a/src/LlamaContext.cpp b/src/LlamaContext.cpp index 3aeccda..2db1d3a 100644 --- a/src/LlamaContext.cpp +++ b/src/LlamaContext.cpp @@ -1,8 +1,11 @@ #include "LlamaContext.h" +#include "DetokenizeWorker.h" #include "DisposeWorker.h" +#include "EmbeddingWorker.h" #include "LlamaCompletionWorker.h" #include "LoadSessionWorker.h" #include "SaveSessionWorker.h" +#include "TokenizeWorker.h" void LlamaContext::Init(Napi::Env env, Napi::Object &exports) { Napi::Function func = DefineClass( @@ -16,6 +19,13 @@ void LlamaContext::Init(Napi::Env env, Napi::Object &exports) { InstanceMethod<&LlamaContext::StopCompletion>( "stopCompletion", static_cast(napi_enumerable)), + InstanceMethod<&LlamaContext::Tokenize>( + "tokenize", static_cast(napi_enumerable)), + InstanceMethod<&LlamaContext::Detokenize>( + "detokenize", + static_cast(napi_enumerable)), + InstanceMethod<&LlamaContext::Embedding>( + "embedding", static_cast(napi_enumerable)), InstanceMethod<&LlamaContext::SaveSession>( "saveSession", static_cast(napi_enumerable)), @@ -163,6 +173,58 @@ void LlamaContext::StopCompletion(const Napi::CallbackInfo &info) { } } +// tokenize(text: string): Promise +Napi::Value LlamaContext::Tokenize(const Napi::CallbackInfo &info) { + Napi::Env env = info.Env(); + if (info.Length() < 1 || !info[0].IsString()) { + Napi::TypeError::New(env, "String expected").ThrowAsJavaScriptException(); + } + if (_sess == nullptr) { + Napi::TypeError::New(env, "Context is disposed") + .ThrowAsJavaScriptException(); + } + auto text = info[0].ToString().Utf8Value(); + auto *worker = new TokenizeWorker(info, _sess, text); + worker->Queue(); + return worker->Promise(); +} + +// detokenize(tokens: number[]): Promise +Napi::Value LlamaContext::Detokenize(const Napi::CallbackInfo &info) { + Napi::Env env = info.Env(); + if (info.Length() < 1 || !info[0].IsArray()) { + Napi::TypeError::New(env, "Array expected").ThrowAsJavaScriptException(); + } + if (_sess == nullptr) { + Napi::TypeError::New(env, "Context is disposed") + .ThrowAsJavaScriptException(); + } + auto tokens = info[0].As(); + std::vector token_ids; + for (size_t i = 0; i < tokens.Length(); i++) { + token_ids.push_back(tokens.Get(i).ToNumber().Int32Value()); + } + auto *worker = new DetokenizeWorker(info, _sess, token_ids); + worker->Queue(); + return worker->Promise(); +} + +// embedding(text: string): Promise +Napi::Value LlamaContext::Embedding(const Napi::CallbackInfo &info) { + Napi::Env env = info.Env(); + if (info.Length() < 1 || !info[0].IsString()) { + Napi::TypeError::New(env, "String expected").ThrowAsJavaScriptException(); + } + if (_sess == nullptr) { + Napi::TypeError::New(env, "Context is disposed") + .ThrowAsJavaScriptException(); + } + auto text = info[0].ToString().Utf8Value(); + auto *worker = new EmbeddingWorker(info, _sess, text); + worker->Queue(); + return worker->Promise(); +} + // saveSession(path: string): Promise throws error Napi::Value LlamaContext::SaveSession(const Napi::CallbackInfo &info) { Napi::Env env = info.Env(); diff --git a/src/LlamaContext.h b/src/LlamaContext.h index 37323df..b44db08 100644 --- a/src/LlamaContext.h +++ b/src/LlamaContext.h @@ -11,6 +11,9 @@ class LlamaContext : public Napi::ObjectWrap { Napi::Value GetSystemInfo(const Napi::CallbackInfo &info); Napi::Value Completion(const Napi::CallbackInfo &info); void StopCompletion(const Napi::CallbackInfo &info); + Napi::Value Tokenize(const Napi::CallbackInfo &info); + Napi::Value Detokenize(const Napi::CallbackInfo &info); + Napi::Value Embedding(const Napi::CallbackInfo &info); Napi::Value SaveSession(const Napi::CallbackInfo &info); Napi::Value LoadSession(const Napi::CallbackInfo &info); Napi::Value Release(const Napi::CallbackInfo &info); diff --git a/src/TokenizeWorker.cpp b/src/TokenizeWorker.cpp new file mode 100644 index 0000000..c9cf374 --- /dev/null +++ b/src/TokenizeWorker.cpp @@ -0,0 +1,26 @@ +#include "TokenizeWorker.h" +#include "LlamaContext.h" + +TokenizeWorker::TokenizeWorker(const Napi::CallbackInfo &info, + LlamaSessionPtr &sess, std::string text) + : AsyncWorker(info.Env()), Deferred(info.Env()), _sess(sess), _text(text) {} + +void TokenizeWorker::Execute() { + const auto tokens = ::llama_tokenize(_sess->context(), _text, false); + _result = {.tokens = std::move(tokens)}; +} + +void TokenizeWorker::OnOK() { + Napi::HandleScope scope(Napi::AsyncWorker::Env()); + auto result = Napi::Object::New(Napi::AsyncWorker::Env()); + auto tokens = + Napi::Int32Array::New(Napi::AsyncWorker::Env(), _result.tokens.size()); + memcpy(tokens.Data(), _result.tokens.data(), + _result.tokens.size() * sizeof(llama_token)); + result.Set("tokens", tokens); + Napi::Promise::Deferred::Resolve(result); +} + +void TokenizeWorker::OnError(const Napi::Error &err) { + Napi::Promise::Deferred::Reject(err.Value()); +} diff --git a/src/TokenizeWorker.h b/src/TokenizeWorker.h new file mode 100644 index 0000000..9df95d8 --- /dev/null +++ b/src/TokenizeWorker.h @@ -0,0 +1,23 @@ +#include "common.hpp" +#include + +struct TokenizeResult { + std::vector tokens; +}; + +class TokenizeWorker : public Napi::AsyncWorker, + public Napi::Promise::Deferred { +public: + TokenizeWorker(const Napi::CallbackInfo &info, LlamaSessionPtr &sess, + std::string text); + +protected: + void Execute(); + void OnOK(); + void OnError(const Napi::Error &err); + +private: + LlamaSessionPtr _sess; + std::string _text; + TokenizeResult _result; +}; diff --git a/src/common.hpp b/src/common.hpp index 08e4e36..06e2f5b 100644 --- a/src/common.hpp +++ b/src/common.hpp @@ -47,7 +47,8 @@ constexpr T get_option(const Napi::Object &options, const std::string &name, class LlamaSession { public: LlamaSession(llama_model *model, llama_context *ctx, gpt_params params) - : model_(LlamaCppModel(model, llama_free_model)), ctx_(LlamaCppContext(ctx, llama_free)), params_(params) { + : model_(LlamaCppModel(model, llama_free_model)), + ctx_(LlamaCppContext(ctx, llama_free)), params_(params) { tokens_.reserve(params.n_ctx); } @@ -57,7 +58,7 @@ class LlamaSession { inline llama_model *model() { return model_.get(); } - inline std::vector* tokens_ptr() { return &tokens_; } + inline std::vector *tokens_ptr() { return &tokens_; } inline void set_tokens(std::vector tokens) { tokens_ = std::move(tokens); diff --git a/test/__snapshots__/index.test.ts.snap b/test/__snapshots__/index.test.ts.snap index 997c35c..c5e2e12 100644 --- a/test/__snapshots__/index.test.ts.snap +++ b/test/__snapshots__/index.test.ts.snap @@ -1,5 +1,409 @@ // Jest Snapshot v1, https://goo.gl/fbAQLP +exports[`embedding 1`] = ` +{ + "embedding": Float32Array [ + -0.30589157342910767, + -0.4362618029117584, + 0.2843831181526184, + -0.28524255752563477, + 0.23747456073760986, + 0.040684834122657776, + 0.4085618555545807, + 0.09371089190244675, + 0.3960898816585541, + -0.0703088641166687, + -0.05577404797077179, + 0.2844226658344269, + 0.4143484830856323, + 0.10765813291072845, + 0.49067679047584534, + -0.003068389371037483, + -0.2556751072406769, + -0.2243092656135559, + -1.04776930809021, + -0.21991387009620667, + 0.6473131775856018, + -0.7225925922393799, + 0.22158470749855042, + 0.12514051795005798, + -0.29667380452156067, + -0.07211534678936005, + 0.15393966436386108, + -0.24953509867191315, + -0.18747948110103607, + -0.8110515475273132, + -0.09495679289102554, + -0.47837579250335693, + 0.28167256712913513, + -0.04567524045705795, + 0.09557905793190002, + 0.6385042667388916, + -0.1807587444782257, + 0.3338685631752014, + -0.13681086897850037, + -0.07725971937179565, + 0.02739766612648964, + -0.12554121017456055, + -0.4204360246658325, + -0.6282246708869934, + -0.9502851963043213, + -0.47330400347709656, + -0.08839747309684753, + -0.10240276157855988, + 0.2345031499862671, + 0.18280169367790222, + -0.0009333677589893341, + -0.2396443635225296, + 0.550329864025116, + -0.4179900586605072, + -0.10021661221981049, + 0.5332830548286438, + 0.592762291431427, + -0.007329106330871582, + 0.05194619297981262, + -0.014329902827739716, + 0.2755645513534546, + 0.063106469810009, + -1.3568179607391357, + 0.766026496887207, + 0.7298449277877808, + 0.2546301484107971, + 0.260549932718277, + -0.3763808608055115, + 0.49982962012290955, + -0.03974828124046326, + -1.1695173978805542, + 0.07225055992603302, + 0.4311821162700653, + 0.23318898677825928, + 0.09386086463928223, + 0.9605568647384644, + 0.09174823015928268, + -0.2051878124475479, + 0.03775341808795929, + -0.06524430215358734, + 0.012495104223489761, + 0.017125405371189117, + -0.26258236169815063, + 0.2933964133262634, + 0.30597907304763794, + -0.21742507815361023, + 0.15749552845954895, + -0.15050096809864044, + 0.6142578125, + -0.1297122836112976, + -0.49175378680229187, + -0.13542315363883972, + 0.02461285889148712, + 0.33537840843200684, + -0.8381355404853821, + -0.38630789518356323, + 0.7646851539611816, + -0.5067175030708313, + 0.4679988622665405, + 3.0185353755950928, + -0.8399708271026611, + 0.2677401900291443, + 0.4926097095012665, + 0.2716555893421173, + 0.37902921438217163, + -0.4686928987503052, + 0.5060027837753296, + -0.6808865666389465, + 0.7367299199104309, + 0.2570103704929352, + 0.04516962915658951, + -0.7184986472129822, + -0.11782032251358032, + -0.7458704113960266, + 0.10478334128856659, + 0.5075545310974121, + 0.4493100643157959, + -0.2865859568119049, + 0.2982551157474518, + 0.20750033855438232, + 0.2677593231201172, + 0.24854658544063568, + -0.3192046582698822, + -0.5292011499404907, + -0.8594997525215149, + -0.5959538817405701, + 1.0806324481964111, + 0.7279831171035767, + -0.27364927530288696, + -0.24527287483215332, + 0.1758672297000885, + -0.10259231925010681, + 0.21862730383872986, + 0.14386384189128876, + -0.27913928031921387, + -0.12339706718921661, + -0.1409936547279358, + 0.10944246500730515, + 0.027284547686576843, + -0.7473660707473755, + -0.2622608542442322, + -1.9535417556762695, + -0.34194716811180115, + -0.5407118201255798, + 0.017283879220485687, + 0.024391233921051025, + 0.6229960322380066, + 0.8663342595100403, + -0.3071446418762207, + -0.08252221345901489, + -0.7198472023010254, + 0.11145271360874176, + -0.5668234825134277, + -0.008619673550128937, + 0.2784949541091919, + 0.3475468158721924, + 0.1836673468351364, + 0.2114388644695282, + 0.2842211127281189, + -0.11185640096664429, + -0.21098832786083221, + -0.3025798201560974, + 0.3275958299636841, + -0.24895787239074707, + 0.19390754401683807, + -0.9152684211730957, + 0.09419406950473785, + 0.539104700088501, + -0.03158541023731232, + -0.1861647367477417, + 0.378104567527771, + 0.23312538862228394, + -0.40820398926734924, + 0.24449624121189117, + 0.743332028388977, + 0.13647067546844482, + -0.02837749943137169, + 0.09980937093496323, + -0.2923177480697632, + 0.45938464999198914, + 0.21493542194366455, + -0.30366891622543335, + 0.1957925260066986, + 0.26896172761917114, + 0.36437684297561646, + -0.04340284317731857, + 0.5426405668258667, + -0.3077859878540039, + 0.7508651614189148, + 0.4905651807785034, + 0.7319092750549316, + -0.16428036987781525, + 0.230209618806839, + 0.2558862864971161, + -0.434606671333313, + 0.22762459516525269, + 0.3282041847705841, + -0.6517583131790161, + -0.06724375486373901, + -0.9909067153930664, + -0.5841869115829468, + -0.5296915173530579, + 0.20155811309814453, + -0.14103156328201294, + 0.5509096384048462, + 0.31542760133743286, + 0.2955850660800934, + -0.1614847183227539, + -0.1404646337032318, + -0.23052823543548584, + -0.29584670066833496, + 0.05748496949672699, + 0.5023753643035889, + -0.1881052553653717, + -0.3115173876285553, + -0.15852509438991547, + -0.08207494020462036, + 0.22114767134189606, + 0.44965773820877075, + 0.014335982501506805, + 0.30281561613082886, + -0.3791540265083313, + 0.025841832160949707, + -2.6462764739990234, + 0.4000885486602783, + -0.20332515239715576, + -0.22497355937957764, + -0.35778823494911194, + -0.4141441583633423, + -0.04818977415561676, + -0.43822476267814636, + 0.39832189679145813, + 0.025510281324386597, + -0.27054646611213684, + -0.3327883780002594, + 0.18184900283813477, + -0.3234720230102539, + 0.04162818193435669, + 0.15017476677894592, + 0.05754746124148369, + 0.28941819071769714, + 0.18197064101696014, + 0.25220412015914917, + -0.19192975759506226, + 0.2946318984031677, + -0.03631554916501045, + -0.22511330246925354, + 0.1183200478553772, + -0.05337872356176376, + 1.5004233121871948, + 1.097097635269165, + -0.4877122640609741, + -0.3661215305328369, + -0.08679819107055664, + -0.03559386730194092, + -0.28740009665489197, + -0.34556180238723755, + -0.2969852387905121, + 0.3777680993080139, + 0.3704182505607605, + -0.20114508271217346, + -0.13527636229991913, + -0.5893090963363647, + -0.37171560525894165, + 0.12260761857032776, + -0.16997741162776947, + -0.014653682708740234, + -0.7562674880027771, + -0.4182020127773285, + 0.4490223526954651, + 0.13718143105506897, + -0.31484150886535645, + 0.6581723690032959, + 0.2504929304122925, + -0.713886022567749, + -0.172017902135849, + 0.0750531554222107, + -0.18888339400291443, + -0.084627665579319, + -0.10178094357252121, + 0.040924541652202606, + 0.018024511635303497, + 0.04558829963207245, + 0.25789231061935425, + 0.41295164823532104, + -0.2633710205554962, + -0.13638345897197723, + 0.1539003551006317, + 0.1437481790781021, + -0.30370962619781494, + -0.6250884532928467, + -0.09580758213996887, + -0.37087106704711914, + -0.63509202003479, + 0.2148924469947815, + -0.3971850275993347, + -0.24478872120380402, + 0.19401757419109344, + 0.1746038794517517, + 0.47645848989486694, + -0.09134512394666672, + -0.5615236163139343, + 0.1621193140745163, + -0.30952954292297363, + -0.18234726786613464, + -0.03492484986782074, + 0.5544385313987732, + 0.05228151008486748, + 0.1500079184770584, + -0.2142195999622345, + 0.19012056291103363, + -0.01149933785200119, + -0.011156745254993439, + -0.5459588766098022, + 0.35099416971206665, + 0.4465286135673523, + 0.0028657615184783936, + -0.16332046687602997, + -0.2468421459197998, + -2.2933030128479004, + 0.15226686000823975, + -0.38667356967926025, + 0.7138491272926331, + -0.16730822622776031, + 0.595676064491272, + -0.09855373203754425, + 0.6798598170280457, + 0.046835437417030334, + -0.20687727630138397, + -0.05769139528274536, + 0.3734232187271118, + 0.588481605052948, + 0.1398356854915619, + -0.43932798504829407, + 0.29884442687034607, + 0.5846158862113953, + -0.04616348072886467, + 0.0204644575715065, + -0.7233595252037048, + 0.41476011276245117, + 0.18898114562034607, + 1.7121883630752563, + -0.03178849816322327, + 0.006135813891887665, + -0.27237266302108765, + 0.25189393758773804, + 0.0849611684679985, + 0.2183677852153778, + 0.3552272617816925, + 0.09292054176330566, + 0.06251377612352371, + 0.1789976805448532, + -0.2100553810596466, + 0.025026649236679077, + 0.2708532512187958, + -0.15171603858470917, + 0.19773134589195251, + 0.1373325139284134, + 0.14517076313495636, + -0.3431127071380615, + 0.4348764419555664, + -0.14019416272640228, + -0.6299729347229004, + 0.7841408252716064, + 0.0076477304100990295, + -0.15605485439300537, + -0.0022411197423934937, + -0.4123740792274475, + -0.18040762841701508, + -0.4707123339176178, + -0.11488653719425201, + 0.7781919836997986, + 0.3377082645893097, + -0.0562475323677063, + 0.3699168860912323, + -0.04673091322183609, + 0.8123000860214233, + 0.1334376484155655, + 0.20715650916099548, + 0.0655558630824089, + -0.07470184564590454, + 0.3042880892753601, + -0.09481601417064667, + 0.03576074540615082, + ], +} +`; + +exports[`tokeneize 1`] = ` +{ + "tokens": Int32Array [ + 9038, + 2501, + 263, + 931, + ], +} +`; + +exports[`tokeneize 2`] = `"xxx"`; + exports[`work fine 1`] = ` { "text": " swochadoorter scientific WindowsCa occupiedrå alta", diff --git a/test/index.test.ts b/test/index.test.ts index e27ee26..908760b 100644 --- a/test/index.test.ts +++ b/test/index.test.ts @@ -24,3 +24,27 @@ it('work fine', async () => { await model.loadSession(path.resolve(__dirname, './tmp.sess')) await model.release() }) + +it('tokeneize', async () => { + const model = await loadModel({ model: path.resolve(__dirname, './tiny-random-llama.gguf') }) + { + const result = await model.tokenize('Once upon a time') + expect(result).toMatchSnapshot() + } + { + const result = await model.detokenize([123, 123, 123]) + expect(result).toMatchSnapshot() + } + await model.release() +}) + +it('embedding', async () => { + const model = await loadModel({ + model: path.resolve(__dirname, './bge-small-en.gguf'), + embedding: true, + n_gpu_layers: 0, + }) + const result = await model.embedding('Once upon a time') + expect(result).toMatchSnapshot() + await model.release() +})