node-llama-cpp 0.0.1 → 1.1.1

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.
Files changed (111) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +210 -0
  3. package/dist/AbortError.d.ts +3 -0
  4. package/dist/AbortError.js +6 -0
  5. package/dist/AbortError.js.map +1 -0
  6. package/dist/ChatPromptWrapper.d.ts +7 -0
  7. package/dist/ChatPromptWrapper.js +14 -0
  8. package/dist/ChatPromptWrapper.js.map +1 -0
  9. package/dist/LlamaChatSession.d.ts +24 -0
  10. package/dist/LlamaChatSession.js +86 -0
  11. package/dist/LlamaChatSession.js.map +1 -0
  12. package/dist/LlamaModel.d.ts +13 -0
  13. package/dist/LlamaModel.js +43 -0
  14. package/dist/LlamaModel.js.map +1 -0
  15. package/dist/chatWrappers/EmptyChatPromptWrapper.d.ts +3 -0
  16. package/dist/chatWrappers/EmptyChatPromptWrapper.js +4 -0
  17. package/dist/chatWrappers/EmptyChatPromptWrapper.js.map +1 -0
  18. package/dist/chatWrappers/LlamaChatPromptWrapper.d.ts +8 -0
  19. package/dist/chatWrappers/LlamaChatPromptWrapper.js +16 -0
  20. package/dist/chatWrappers/LlamaChatPromptWrapper.js.map +1 -0
  21. package/dist/cli/cli.d.ts +2 -0
  22. package/dist/cli/cli.js +33 -0
  23. package/dist/cli/cli.js.map +1 -0
  24. package/dist/cli/commands/BuildCommand.d.ts +8 -0
  25. package/dist/cli/commands/BuildCommand.js +41 -0
  26. package/dist/cli/commands/BuildCommand.js.map +1 -0
  27. package/dist/cli/commands/ChatCommand.d.ts +8 -0
  28. package/dist/cli/commands/ChatCommand.js +83 -0
  29. package/dist/cli/commands/ChatCommand.js.map +1 -0
  30. package/dist/cli/commands/ClearCommand.d.ts +7 -0
  31. package/dist/cli/commands/ClearCommand.js +42 -0
  32. package/dist/cli/commands/ClearCommand.js.map +1 -0
  33. package/dist/cli/commands/DownloadCommand.d.ts +11 -0
  34. package/dist/cli/commands/DownloadCommand.js +179 -0
  35. package/dist/cli/commands/DownloadCommand.js.map +1 -0
  36. package/dist/cli/commands/OnPostInstallCommand.d.ts +4 -0
  37. package/dist/cli/commands/OnPostInstallCommand.js +24 -0
  38. package/dist/cli/commands/OnPostInstallCommand.js.map +1 -0
  39. package/dist/commands.d.ts +4 -0
  40. package/dist/commands.js +5 -0
  41. package/dist/commands.js.map +1 -0
  42. package/dist/config.d.ts +9 -0
  43. package/dist/config.js +25 -0
  44. package/dist/config.js.map +1 -0
  45. package/dist/index.d.ts +7 -0
  46. package/dist/index.js +8 -0
  47. package/dist/index.js.map +1 -0
  48. package/dist/utils/clearLlamaBuild.d.ts +1 -0
  49. package/dist/utils/clearLlamaBuild.js +12 -0
  50. package/dist/utils/clearLlamaBuild.js.map +1 -0
  51. package/dist/utils/clearTempFolder.d.ts +1 -0
  52. package/dist/utils/clearTempFolder.js +16 -0
  53. package/dist/utils/clearTempFolder.js.map +1 -0
  54. package/dist/utils/compileLLamaCpp.d.ts +6 -0
  55. package/dist/utils/compileLLamaCpp.js +43 -0
  56. package/dist/utils/compileLLamaCpp.js.map +1 -0
  57. package/dist/utils/getBin.d.ts +18 -0
  58. package/dist/utils/getBin.js +70 -0
  59. package/dist/utils/getBin.js.map +1 -0
  60. package/dist/utils/spawnCommand.d.ts +1 -0
  61. package/dist/utils/spawnCommand.js +39 -0
  62. package/dist/utils/spawnCommand.js.map +1 -0
  63. package/dist/utils/usedBinFlag.d.ts +6 -0
  64. package/dist/utils/usedBinFlag.js +15 -0
  65. package/dist/utils/usedBinFlag.js.map +1 -0
  66. package/dist/utils/withLock.d.ts +1 -0
  67. package/dist/utils/withLock.js +19 -0
  68. package/dist/utils/withLock.js.map +1 -0
  69. package/dist/utils/withOra.d.ts +5 -0
  70. package/dist/utils/withOra.js +21 -0
  71. package/dist/utils/withOra.js.map +1 -0
  72. package/llama/.clang-format +46 -0
  73. package/llama/addon.cpp +183 -0
  74. package/llama/binding.gyp +24 -0
  75. package/llama/usedBin.json +3 -0
  76. package/llamaBins/linux-arm64-16.node +0 -0
  77. package/llamaBins/linux-arm64-17.node +0 -0
  78. package/llamaBins/linux-arm64-18.node +0 -0
  79. package/llamaBins/linux-arm64-19.node +0 -0
  80. package/llamaBins/linux-arm64-20.node +0 -0
  81. package/llamaBins/linux-armv7l-16.node +0 -0
  82. package/llamaBins/linux-armv7l-17.node +0 -0
  83. package/llamaBins/linux-armv7l-18.node +0 -0
  84. package/llamaBins/linux-armv7l-19.node +0 -0
  85. package/llamaBins/linux-armv7l-20.node +0 -0
  86. package/llamaBins/linux-ppc64le-16.node +0 -0
  87. package/llamaBins/linux-ppc64le-17.node +0 -0
  88. package/llamaBins/linux-ppc64le-18.node +0 -0
  89. package/llamaBins/linux-ppc64le-19.node +0 -0
  90. package/llamaBins/linux-ppc64le-20.node +0 -0
  91. package/llamaBins/linux-x64-16.node +0 -0
  92. package/llamaBins/linux-x64-17.node +0 -0
  93. package/llamaBins/linux-x64-18.node +0 -0
  94. package/llamaBins/linux-x64-19.node +0 -0
  95. package/llamaBins/linux-x64-20.node +0 -0
  96. package/llamaBins/mac-arm64-16.node +0 -0
  97. package/llamaBins/mac-arm64-17.node +0 -0
  98. package/llamaBins/mac-arm64-18.node +0 -0
  99. package/llamaBins/mac-arm64-19.node +0 -0
  100. package/llamaBins/mac-arm64-20.node +0 -0
  101. package/llamaBins/mac-x64-16.node +0 -0
  102. package/llamaBins/mac-x64-17.node +0 -0
  103. package/llamaBins/mac-x64-18.node +0 -0
  104. package/llamaBins/mac-x64-19.node +0 -0
  105. package/llamaBins/mac-x64-20.node +0 -0
  106. package/llamaBins/win-x64-16.node +0 -0
  107. package/llamaBins/win-x64-17.node +0 -0
  108. package/llamaBins/win-x64-18.node +0 -0
  109. package/llamaBins/win-x64-19.node +0 -0
  110. package/llamaBins/win-x64-20.node +0 -0
  111. package/package.json +71 -10
@@ -0,0 +1,70 @@
1
+ import { createRequire } from "module";
2
+ import * as console from "console";
3
+ import path from "path";
4
+ import process from "process";
5
+ import fs from "fs-extra";
6
+ import { defaultLlamaCppGitHubRepo, defaultLlamaCppRelease, defaultSkipDownload, llamaBinsDirectory } from "../config.js";
7
+ import { DownloadLlamaCppCommand } from "../cli/commands/DownloadCommand.js";
8
+ import { getUsedBinFlag } from "./usedBinFlag.js";
9
+ import { getCompiledLlamaCppBinaryPath } from "./compileLLamaCpp.js";
10
+ const require = createRequire(import.meta.url);
11
+ export async function getPrebuildBinPath() {
12
+ const majorNodeVersion = parseInt(process.version.slice("v".length));
13
+ const supportedVersions = [majorNodeVersion, majorNodeVersion - 1];
14
+ function createPath(platform, arch, nodeVersion) {
15
+ return path.join(llamaBinsDirectory, `${platform}-${arch}-${nodeVersion}.node`);
16
+ }
17
+ async function resolvePath(platform, arch, nodeVersions) {
18
+ for (const nodeVersion of nodeVersions) {
19
+ const binPath = createPath(platform, arch, nodeVersion);
20
+ if (await fs.exists(binPath))
21
+ return binPath;
22
+ }
23
+ return null;
24
+ }
25
+ async function getPath() {
26
+ switch (process.platform) {
27
+ case "win32":
28
+ case "cygwin":
29
+ return resolvePath("win", process.arch, supportedVersions);
30
+ case "linux":
31
+ case "android":
32
+ return resolvePath("linux", process.arch, supportedVersions);
33
+ case "darwin":
34
+ return resolvePath("mac", process.arch, supportedVersions);
35
+ }
36
+ return null;
37
+ }
38
+ return await getPath();
39
+ }
40
+ export async function loadBin() {
41
+ const usedBingFlag = await getUsedBinFlag();
42
+ if (usedBingFlag === "prebuiltBinaries") {
43
+ const prebuildBinPath = await getPrebuildBinPath();
44
+ if (prebuildBinPath == null) {
45
+ console.warn("Prebuild binaries not found, falling back to to locally built binaries");
46
+ }
47
+ else {
48
+ return require(prebuildBinPath);
49
+ }
50
+ }
51
+ const modulePath = await getCompiledLlamaCppBinaryPath();
52
+ if (modulePath == null) {
53
+ if (defaultSkipDownload) {
54
+ throw new Error("No prebuild binaries found and NODE_LLAMA_CPP_SKIP_DOWNLOAD env var is set to true");
55
+ }
56
+ else {
57
+ await DownloadLlamaCppCommand({
58
+ repo: defaultLlamaCppGitHubRepo,
59
+ release: defaultLlamaCppRelease
60
+ });
61
+ const modulePath = await getCompiledLlamaCppBinaryPath();
62
+ if (modulePath == null) {
63
+ throw new Error("Failed to download and compile llama.cpp");
64
+ }
65
+ return require(modulePath);
66
+ }
67
+ }
68
+ return require(modulePath);
69
+ }
70
+ //# sourceMappingURL=getBin.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"getBin.js","sourceRoot":"","sources":["../../src/utils/getBin.ts"],"names":[],"mappings":"AAAA,OAAO,EAAC,aAAa,EAAC,MAAM,QAAQ,CAAC;AACrC,OAAO,KAAK,OAAO,MAAM,SAAS,CAAC;AACnC,OAAO,IAAI,MAAM,MAAM,CAAC;AACxB,OAAO,OAAO,MAAM,SAAS,CAAC;AAC9B,OAAO,EAAE,MAAM,UAAU,CAAC;AAC1B,OAAO,EAAC,yBAAyB,EAAE,sBAAsB,EAAE,mBAAmB,EAAE,kBAAkB,EAAC,MAAM,cAAc,CAAC;AACxH,OAAO,EAAC,uBAAuB,EAAC,MAAM,oCAAoC,CAAC;AAC3E,OAAO,EAAC,cAAc,EAAC,MAAM,kBAAkB,CAAC;AAChD,OAAO,EAAC,6BAA6B,EAAC,MAAM,sBAAsB,CAAC;AAEnE,MAAM,OAAO,GAAG,aAAa,CAAC,MAAM,CAAC,IAAI,CAAC,GAAG,CAAC,CAAC;AAE/C,MAAM,CAAC,KAAK,UAAU,kBAAkB;IACpC,MAAM,gBAAgB,GAAG,QAAQ,CAAC,OAAO,CAAC,OAAO,CAAC,KAAK,CAAC,GAAG,CAAC,MAAM,CAAC,CAAC,CAAC;IACrE,MAAM,iBAAiB,GAAG,CAAC,gBAAgB,EAAE,gBAAgB,GAAG,CAAC,CAAC,CAAC;IAEnE,SAAS,UAAU,CAAC,QAAgB,EAAE,IAAY,EAAE,WAAmB;QACnE,OAAO,IAAI,CAAC,IAAI,CAAC,kBAAkB,EAAE,GAAG,QAA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AC;IAEzD,IAAI,UAAU,IAAI,IAAI,EAAE;QACpB,IAAI,mBAAmB,EAAE;YACrB,MAAM,IAAI,KAAK,CAAC,oFAAoF,CAAC,CAAC;SACzG;aAAM;YACH,MAAM,uBAAuB,CAAC;gBAC1B,IAAI,EAAE,yBAAyB;gBAC/B,OAAO,EAAE,sBAAsB;aAClC,CAAC,CAAC;YAEH,MAAM,UAAU,GAAG,MAAM,6BAA6B,EAAE,CAAC;YAEzD,IAAI,UAAU,IAAI,IAAI,EAAE;gBACpB,MAAM,IAAI,KAAK,CAAC,0CAA0C,CAAC,CAAC;aAC/D;YAED,OAAO,OAAO,CAAC,UAAU,CAAC,CAAC;SAC9B;KACJ;IAED,OAAO,OAAO,CAAC,UAAU,CAAC,CAAC;AAC/B,CAAC"}
@@ -0,0 +1 @@
1
+ export declare function spawnCommand(command: string, args: string[], cwd: string): Promise<void>;
@@ -0,0 +1,39 @@
1
+ import spawn from "cross-spawn";
2
+ export function spawnCommand(command, args, cwd) {
3
+ function getCommandString() {
4
+ let res = command;
5
+ for (const arg of args) {
6
+ if (arg.includes(" ")) {
7
+ res += ` "${arg.split('"').join('\\"')}"`;
8
+ }
9
+ else {
10
+ res += ` ${arg}`;
11
+ }
12
+ }
13
+ return res;
14
+ }
15
+ return new Promise((resolve, reject) => {
16
+ const child = spawn(command, args, {
17
+ stdio: "inherit",
18
+ cwd,
19
+ env: process.env,
20
+ detached: false,
21
+ windowsHide: true
22
+ });
23
+ child.on("exit", (code) => {
24
+ if (code == 0)
25
+ resolve();
26
+ else
27
+ reject(new Error(`Command ${getCommandString()} exited with code ${code}`));
28
+ });
29
+ child.on("error", reject);
30
+ child.on("disconnect", () => reject(new Error(`Command ${getCommandString()} disconnected`)));
31
+ child.on("close", code => {
32
+ if (code == 0)
33
+ resolve();
34
+ else
35
+ reject(new Error(`Command ${getCommandString()} closed with code ${code}`));
36
+ });
37
+ });
38
+ }
39
+ //# sourceMappingURL=spawnCommand.js.map
@@ -0,0 +1 @@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AAC,CAAC;QAC9F,KAAK,CAAC,EAAE,CAAC,OAAO,EAAE,IAAI,CAAC,EAAE;YACrB,IAAI,IAAI,IAAI,CAAC;gBACT,OAAO,EAAE,CAAC;;gBAEV,MAAM,CAAC,IAAI,KAAK,CAAC,WAAW,gBAAgB,EAAE,qBAAqB,IAAI,EAAE,CAAC,CAAC,CAAC;QACpF,CAAC,CAAC,CAAC;IACP,CAAC,CAAC,CAAC;AACP,CAAC"}
@@ -0,0 +1,6 @@
1
+ type UsedBinFlagFile = {
2
+ use: "prebuiltBinaries" | "localBuildFromSource";
3
+ };
4
+ export declare function getUsedBinFlag(): Promise<"prebuiltBinaries" | "localBuildFromSource">;
5
+ export declare function setUsedBinFlag(useFlag: UsedBinFlagFile["use"]): Promise<void>;
6
+ export {};
@@ -0,0 +1,15 @@
1
+ import fs from "fs-extra";
2
+ import { usedBinFlagJsonPath } from "../config.js";
3
+ export async function getUsedBinFlag() {
4
+ const usedBinFlagJson = await fs.readJson(usedBinFlagJsonPath);
5
+ return usedBinFlagJson.use;
6
+ }
7
+ export async function setUsedBinFlag(useFlag) {
8
+ const usedBinFlagJson = {
9
+ use: useFlag
10
+ };
11
+ await fs.writeJson(usedBinFlagJsonPath, usedBinFlagJson, {
12
+ spaces: 4
13
+ });
14
+ }
15
+ //# sourceMappingURL=usedBinFlag.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"usedBinFlag.js","sourceRoot":"","sources":["../../src/utils/usedBinFlag.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,UAAU,CAAC;AAC1B,OAAO,EAAC,mBAAmB,EAAC,MAAM,cAAc,CAAC;AAMjD,MAAM,CAAC,KAAK,UAAU,cAAc;IAChC,MAAM,eAAe,GAAoB,MAAM,EAAE,CAAC,QAAQ,CAAC,mBAAmB,CAAC,CAAC;IAEhF,OAAO,eAAe,CAAC,GAAG,CAAC;AAC/B,CAAC;AAED,MAAM,CAAC,KAAK,UAAU,cAAc,CAAC,OAA+B;IAChE,MAAM,eAAe,GAAoB;QACrC,GAAG,EAAE,OAAO;KACf,CAAC;IAEF,MAAM,EAAE,CAAC,SAAS,CAAC,mBAAmB,EAAE,eAAe,EAAE;QACrD,MAAM,EAAE,CAAC;KACZ,CAAC,CAAC;AACP,CAAC"}
@@ -0,0 +1 @@
1
+ export declare function withLock<ReturnType>(scope: any, key: string, callback: () => Promise<ReturnType>): Promise<ReturnType>;
@@ -0,0 +1,19 @@
1
+ const locks = new Map();
2
+ export async function withLock(scope, key, callback) {
3
+ while (locks.get(scope)?.has(key)) {
4
+ await locks.get(scope)?.get(key);
5
+ }
6
+ const promise = callback();
7
+ if (!locks.has(scope))
8
+ locks.set(scope, new Map());
9
+ locks.get(scope).set(key, promise);
10
+ try {
11
+ return await promise;
12
+ }
13
+ finally {
14
+ locks.get(scope)?.delete(key);
15
+ if (locks.get(scope)?.size === 0)
16
+ locks.delete(scope);
17
+ }
18
+ }
19
+ //# sourceMappingURL=withLock.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"withLock.js","sourceRoot":"","sources":["../../src/utils/withLock.ts"],"names":[],"mappings":"AAAA,MAAM,KAAK,GAAG,IAAI,GAAG,EAAkC,CAAC;AAExD,MAAM,CAAC,KAAK,UAAU,QAAQ,CAAa,KAAU,EAAE,GAAW,EAAE,QAAmC;IACnG,OAAO,KAAK,CAAC,GAAG,CAAC,KAAK,CAAC,EAAE,GAAG,CAAC,GAAG,CAAC,EAAE;QAC/B,MAAM,KAAK,CAAC,GAAG,CAAC,KAAK,CAAC,EAAE,GAAG,CAAC,GAAG,CAAC,CAAC;KACpC;IAED,MAAM,OAAO,GAAG,QAAQ,EAAE,CAAC;IAE3B,IAAI,CAAC,KAAK,CAAC,GAAG,CAAC,KAAK,CAAC;QACjB,KAAK,CAAC,GAAG,CAAC,KAAK,EAAE,IAAI,GAAG,EAAE,CAAC,CAAC;IAEhC,KAAK,CAAC,GAAG,CAAC,KAAK,CAAE,CAAC,GAAG,CAAC,GAAG,EAAE,OAAO,CAAC,CAAC;IAEpC,IAAI;QACA,OAAO,MAAM,OAAO,CAAC;KACxB;YAAS;QACN,KAAK,CAAC,GAAG,CAAC,KAAK,CAAC,EAAE,MAAM,CAAC,GAAG,CAAC,CAAC;QAE9B,IAAI,KAAK,CAAC,GAAG,CAAC,KAAK,CAAC,EAAE,IAAI,KAAK,CAAC;YAC5B,KAAK,CAAC,MAAM,CAAC,KAAK,CAAC,CAAC;KAC3B;AACL,CAAC"}
@@ -0,0 +1,5 @@
1
+ export default function withOra<T>(message: string | {
2
+ loading: string;
3
+ success?: string;
4
+ fail?: string;
5
+ }, callback: () => Promise<T>): Promise<T>;
@@ -0,0 +1,21 @@
1
+ import ora from "ora";
2
+ export default async function withOra(message, callback) {
3
+ const spinner = ora(typeof message === "string" ? message : message.loading);
4
+ spinner.start();
5
+ try {
6
+ const res = await callback();
7
+ if (typeof message !== "string")
8
+ spinner.succeed(message.success);
9
+ else
10
+ spinner.succeed();
11
+ return res;
12
+ }
13
+ catch (er) {
14
+ if (typeof message !== "string")
15
+ spinner.fail(message.fail);
16
+ else
17
+ spinner.fail();
18
+ throw er;
19
+ }
20
+ }
21
+ //# sourceMappingURL=withOra.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"withOra.js","sourceRoot":"","sources":["../../src/utils/withOra.ts"],"names":[],"mappings":"AAAA,OAAO,GAAG,MAAM,KAAK,CAAC;AAEtB,MAAM,CAAC,OAAO,CAAC,KAAK,UAAU,OAAO,CACjC,OAIC,EACD,QAA0B;IAE1B,MAAM,OAAO,GAAG,GAAG,CAAC,OAAO,OAAO,KAAK,QAAQ,CAAC,CAAC,CAAC,OAAO,CAAC,CAAC,CAAC,OAAO,CAAC,OAAO,CAAC,CAAC;IAE7E,OAAO,CAAC,KAAK,EAAE,CAAC;IAEhB,IAAI;QACA,MAAM,GAAG,GAAG,MAAM,QAAQ,EAAE,CAAC;QAE7B,IAAI,OAAO,OAAO,KAAK,QAAQ;YAC3B,OAAO,CAAC,OAAO,CAAC,OAAO,CAAC,OAAO,CAAC,CAAC;;YAEjC,OAAO,CAAC,OAAO,EAAE,CAAC;QAEtB,OAAO,GAAG,CAAC;KACd;IAAC,OAAO,EAAE,EAAE;QACT,IAAI,OAAO,OAAO,KAAK,QAAQ;YAC3B,OAAO,CAAC,IAAI,CAAC,OAAO,CAAC,IAAI,CAAC,CAAC;;YAE3B,OAAO,CAAC,IAAI,EAAE,CAAC;QAEnB,MAAM,EAAE,CAAC;KACZ;AACL,CAAC"}
@@ -0,0 +1,46 @@
1
+ BasedOnStyle: Google
2
+ IndentWidth: 2
3
+ UseTab: Never
4
+ TabWidth: 2
5
+ ColumnLimit: 500
6
+ AllowShortCaseLabelsOnASingleLine: true
7
+ AllowShortFunctionsOnASingleLine: true
8
+ AllowShortIfStatementsOnASingleLine: true
9
+ AllowShortLoopsOnASingleLine: true
10
+ AlignTrailingComments: false
11
+ SpaceAfterTemplateKeyword: false
12
+ AllowShortBlocksOnASingleLine: true
13
+ MaxEmptyLinesToKeep: 3
14
+ NamespaceIndentation: None
15
+ CommentPragmas: '^[^ ]'
16
+ FixNamespaceComments: false
17
+ AccessModifierOffset: -4
18
+ SpaceAfterCStyleCast: false
19
+ PointerAlignment: Left
20
+ IndentCaseLabels: false
21
+ BinPackArguments: false
22
+ BinPackParameters: false
23
+ Cpp11BracedListStyle: false
24
+ SpaceBeforeCpp11BracedList: true
25
+ SpaceInEmptyBlock: true
26
+ KeepEmptyLinesAtTheStartOfBlocks: false
27
+ DerivePointerAlignment: false
28
+ AlwaysBreakTemplateDeclarations: No
29
+ DeriveLineEnding: false
30
+ UseCRLF: false
31
+ AllowAllArgumentsOnNextLine: true
32
+ AlignAfterOpenBracket: DontAlign
33
+ PackConstructorInitializers: CurrentLine
34
+ BraceWrapping:
35
+ AfterStruct: false
36
+ AfterClass: false
37
+ AfterUnion: false
38
+ AfterEnum: false
39
+ AfterControlStatement: false
40
+ AfterFunction: false
41
+ AfterNamespace: false
42
+ AfterExternBlock: false
43
+ BeforeElse: false
44
+ SplitEmptyFunction: false
45
+ SplitEmptyRecord: false
46
+ SplitEmptyNamespace: false
@@ -0,0 +1,183 @@
1
+ #include <stddef.h>
2
+ #include <algorithm>
3
+ #include <sstream>
4
+ #include <vector>
5
+
6
+ #include "llama.h"
7
+ #include "napi.h"
8
+
9
+ class LLAMAModel : public Napi::ObjectWrap<LLAMAModel> {
10
+ public:
11
+ llama_context_params params;
12
+ llama_model* model;
13
+ LLAMAModel(const Napi::CallbackInfo& info) : Napi::ObjectWrap<LLAMAModel>(info) {
14
+ params = llama_context_default_params();
15
+ params.seed = -1;
16
+ params.n_ctx = 4096;
17
+ model = llama_load_model_from_file(info[0].As<Napi::String>().Utf8Value().c_str(), params);
18
+
19
+ if (model == NULL) {
20
+ Napi::Error::New(info.Env(), "Failed to load model").ThrowAsJavaScriptException();
21
+ return;
22
+ }
23
+ }
24
+ ~LLAMAModel() { llama_free_model(model); }
25
+ static void init(Napi::Object exports) { exports.Set("LLAMAModel", DefineClass(exports.Env(), "LLAMAModel", {})); }
26
+ };
27
+
28
+ class LLAMAContext : public Napi::ObjectWrap<LLAMAContext> {
29
+ public:
30
+ LLAMAModel* model;
31
+ llama_context* ctx;
32
+ LLAMAContext(const Napi::CallbackInfo& info) : Napi::ObjectWrap<LLAMAContext>(info) {
33
+ model = Napi::ObjectWrap<LLAMAModel>::Unwrap(info[0].As<Napi::Object>());
34
+ model->Ref();
35
+ ctx = llama_new_context_with_model(model->model, model->params);
36
+ Napi::MemoryManagement::AdjustExternalMemory(Env(), llama_get_state_size(ctx));
37
+ }
38
+ ~LLAMAContext() {
39
+ Napi::MemoryManagement::AdjustExternalMemory(Env(), -(int64_t)llama_get_state_size(ctx));
40
+ llama_free(ctx);
41
+ model->Unref();
42
+ }
43
+ Napi::Value Encode(const Napi::CallbackInfo& info) {
44
+ std::string text = info[0].As<Napi::String>().Utf8Value();
45
+
46
+ std::vector<llama_token> tokens(text.size());
47
+ int n = llama_tokenize(ctx, text.data(), tokens.data(), text.size(), false);
48
+
49
+ if (n < 0) {
50
+ Napi::Error::New(info.Env(), "String expected").ThrowAsJavaScriptException();
51
+ return info.Env().Undefined();
52
+ }
53
+ tokens.resize(n);
54
+
55
+ Napi::Uint32Array result = Napi::Uint32Array::New(info.Env(), n);
56
+ for (size_t i = 0; i < tokens.size(); ++i) { result[i] = static_cast<uint32_t>(tokens[i]); }
57
+
58
+ return result;
59
+ }
60
+ Napi::Value Decode(const Napi::CallbackInfo& info) {
61
+ Napi::Uint32Array tokens = info[0].As<Napi::Uint32Array>();
62
+
63
+ // Create a stringstream for accumulating the decoded string.
64
+ std::stringstream ss;
65
+
66
+ // Decode each token and accumulate the result.
67
+ for (size_t i = 0; i < tokens.ElementLength(); i++) {
68
+ const char* str = llama_token_to_str(ctx, (llama_token)tokens[i]);
69
+ if (str == nullptr) {
70
+ Napi::Error::New(info.Env(), "Invalid token").ThrowAsJavaScriptException();
71
+ return info.Env().Undefined();
72
+ }
73
+ ss << str;
74
+ }
75
+
76
+ return Napi::String::New(info.Env(), ss.str());
77
+ }
78
+ Napi::Value Eval(const Napi::CallbackInfo& info);
79
+ static void init(Napi::Object exports) {
80
+ exports.Set("LLAMAContext",
81
+ DefineClass(exports.Env(),
82
+ "LLAMAContext",
83
+ {
84
+ InstanceMethod("encode", &LLAMAContext::Encode),
85
+ InstanceMethod("decode", &LLAMAContext::Decode),
86
+ InstanceMethod("eval", &LLAMAContext::Eval),
87
+ }));
88
+ }
89
+ };
90
+
91
+
92
+ class LLAMAContextEvalWorker : Napi::AsyncWorker, Napi::Promise::Deferred {
93
+ LLAMAContext* ctx;
94
+ std::vector<llama_token> tokens;
95
+ std::vector<llama_token> restriction;
96
+ llama_token result;
97
+
98
+ public:
99
+ LLAMAContextEvalWorker(const Napi::CallbackInfo& info, LLAMAContext* ctx) : Napi::AsyncWorker(info.Env(), "LLAMAContextEvalWorker"), ctx(ctx), Napi::Promise::Deferred(info.Env()) {
100
+ ctx->Ref();
101
+ Napi::Uint32Array tokens = info[0].As<Napi::Uint32Array>();
102
+ this->tokens.reserve(tokens.ElementLength());
103
+ for (size_t i = 0; i < tokens.ElementLength(); i++) { this->tokens.push_back(static_cast<llama_token>(tokens[i])); }
104
+
105
+ if (info.Length() > 1 && info[1].IsTypedArray()) {
106
+ Napi::Uint32Array restriction = info[1].As<Napi::Uint32Array>();
107
+ this->restriction.reserve(restriction.ElementLength());
108
+ for (size_t i = 0; i < restriction.ElementLength(); i++) { this->restriction.push_back(static_cast<llama_token>(restriction[i])); }
109
+ std::sort(this->restriction.begin(), this->restriction.end());
110
+ }
111
+ }
112
+ ~LLAMAContextEvalWorker() { ctx->Unref(); }
113
+ using Napi::AsyncWorker::Queue;
114
+ using Napi::Promise::Deferred::Promise;
115
+
116
+ protected:
117
+ void Execute() {
118
+ // Perform the evaluation using llama_eval.
119
+ int r = llama_eval(ctx->ctx, tokens.data(), tokens.size(), llama_get_kv_cache_token_count(ctx->ctx), 6);
120
+ if (r != 0) {
121
+ SetError("Eval has failed");
122
+ return;
123
+ }
124
+
125
+ // Select the best prediction.
126
+ float* logits = llama_get_logits(ctx->ctx);
127
+ int n_vocab = llama_n_vocab(ctx->ctx);
128
+ llama_token re;
129
+ if (restriction.empty()) {
130
+ float max = logits[0];
131
+ re = 0;
132
+ for (llama_token id = 1; id < n_vocab; id++) {
133
+ float logit = logits[id];
134
+ if (logit > max) {
135
+ max = logit;
136
+ re = id;
137
+ }
138
+ }
139
+ } else {
140
+ float max = logits[restriction[0]];
141
+ re = 0;
142
+ for (size_t i = 1; i < restriction.size(); i++) {
143
+ llama_token id = restriction[i];
144
+ float logit = logits[id];
145
+ if (logit > max) {
146
+ max = logit;
147
+ re = id;
148
+ }
149
+ }
150
+ }
151
+ result = re;
152
+ }
153
+ void OnOK() {
154
+ Napi::Env env = Napi::AsyncWorker::Env();
155
+ Napi::Number resultValue = Napi::Number::New(env, static_cast<uint32_t>(result));
156
+ Napi::Promise::Deferred::Resolve(resultValue);
157
+ }
158
+ void OnError(const Napi::Error& err) { Napi::Promise::Deferred::Reject(err.Value()); }
159
+ };
160
+
161
+ Napi::Value LLAMAContext::Eval(const Napi::CallbackInfo& info) {
162
+ LLAMAContextEvalWorker* worker = new LLAMAContextEvalWorker(info, this);
163
+ worker->Queue();
164
+ return worker->Promise();
165
+ }
166
+
167
+ Napi::Value tokenBos(const Napi::CallbackInfo& info) { return Napi::Number::From(info.Env(), llama_token_bos()); }
168
+ Napi::Value tokenEos(const Napi::CallbackInfo& info) { return Napi::Number::From(info.Env(), llama_token_eos()); }
169
+ Napi::Value systemInfo(const Napi::CallbackInfo& info) { return Napi::String::From(info.Env(), llama_print_system_info()); }
170
+
171
+ Napi::Object registerCallback(Napi::Env env, Napi::Object exports) {
172
+ llama_backend_init(false);
173
+ exports.DefineProperties({
174
+ Napi::PropertyDescriptor::Function("tokenBos", tokenBos),
175
+ Napi::PropertyDescriptor::Function("tokenEos", tokenEos),
176
+ Napi::PropertyDescriptor::Function("systemInfo", systemInfo),
177
+ });
178
+ LLAMAModel::init(exports);
179
+ LLAMAContext::init(exports);
180
+ return exports;
181
+ }
182
+
183
+ NODE_API_MODULE(NODE_GYP_MODULE_NAME, registerCallback)
@@ -0,0 +1,24 @@
1
+ {
2
+ "targets": [
3
+ {
4
+ "target_name": "llama",
5
+ "sources": [
6
+ "addon.cpp",
7
+ "llama.cpp/ggml.c",
8
+ "llama.cpp/ggml-alloc.c",
9
+ "llama.cpp/k_quants.c",
10
+ "llama.cpp/llama.cpp"
11
+ ],
12
+ "include_dirs": [
13
+ "<!@(node -p \"require('node-addon-api').include\")",
14
+ "llama.cpp"
15
+ ],
16
+ "cflags": ["-fexceptions"],
17
+ "cflags_cc": ["-fexceptions"],
18
+ "defines": [ "GGML_USE_K_QUANTS", "NAPI_CPP_EXCEPTIONS" ],
19
+ "msvs_settings": {
20
+ "VCCLCompilerTool": { "AdditionalOptions": [ '/arch:AVX2', '/EHsc' ] }
21
+ }
22
+ }
23
+ ]
24
+ }
@@ -0,0 +1,3 @@
1
+ {
2
+ "use": "prebuiltBinaries"
3
+ }
Binary file
Binary file
Binary file
Binary file
Binary file
Binary file
Binary file
Binary file
Binary file
Binary file
Binary file
Binary file
Binary file
Binary file
Binary file
Binary file
Binary file
Binary file
Binary file
Binary file
Binary file
Binary file
Binary file
Binary file
Binary file
Binary file
Binary file
Binary file
Binary file
Binary file
Binary file
Binary file
Binary file
Binary file
Binary file