node-llama-cpp 1.3.1 → 2.1.0

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 (121) hide show
  1. package/README.md +100 -28
  2. package/dist/ChatPromptWrapper.d.ts +3 -0
  3. package/dist/ChatPromptWrapper.js.map +1 -1
  4. package/dist/chatWrappers/ChatMLPromptWrapper.d.ts +11 -0
  5. package/dist/chatWrappers/ChatMLPromptWrapper.js +19 -0
  6. package/dist/chatWrappers/ChatMLPromptWrapper.js.map +1 -0
  7. package/dist/chatWrappers/EmptyChatPromptWrapper.d.ts +1 -0
  8. package/dist/chatWrappers/EmptyChatPromptWrapper.js +1 -0
  9. package/dist/chatWrappers/EmptyChatPromptWrapper.js.map +1 -1
  10. package/dist/chatWrappers/GeneralChatPromptWrapper.d.ts +11 -1
  11. package/dist/chatWrappers/GeneralChatPromptWrapper.js +28 -4
  12. package/dist/chatWrappers/GeneralChatPromptWrapper.js.map +1 -1
  13. package/dist/chatWrappers/LlamaChatPromptWrapper.d.ts +4 -1
  14. package/dist/chatWrappers/LlamaChatPromptWrapper.js +9 -5
  15. package/dist/chatWrappers/LlamaChatPromptWrapper.js.map +1 -1
  16. package/dist/chatWrappers/createChatWrapperByBos.d.ts +2 -0
  17. package/dist/chatWrappers/createChatWrapperByBos.js +14 -0
  18. package/dist/chatWrappers/createChatWrapperByBos.js.map +1 -0
  19. package/dist/cli/commands/BuildCommand.d.ts +3 -1
  20. package/dist/cli/commands/BuildCommand.js +24 -2
  21. package/dist/cli/commands/BuildCommand.js.map +1 -1
  22. package/dist/cli/commands/ChatCommand.d.ts +7 -1
  23. package/dist/cli/commands/ChatCommand.js +87 -12
  24. package/dist/cli/commands/ChatCommand.js.map +1 -1
  25. package/dist/cli/commands/ClearCommand.js +1 -1
  26. package/dist/cli/commands/ClearCommand.js.map +1 -1
  27. package/dist/cli/commands/DownloadCommand.d.ts +4 -1
  28. package/dist/cli/commands/DownloadCommand.js +71 -70
  29. package/dist/cli/commands/DownloadCommand.js.map +1 -1
  30. package/dist/cli/commands/OnPostInstallCommand.js +4 -2
  31. package/dist/cli/commands/OnPostInstallCommand.js.map +1 -1
  32. package/dist/config.d.ts +5 -0
  33. package/dist/config.js +11 -1
  34. package/dist/config.js.map +1 -1
  35. package/dist/index.d.ts +5 -1
  36. package/dist/index.js +4 -1
  37. package/dist/index.js.map +1 -1
  38. package/dist/llamaEvaluator/LlamaBins.d.ts +3 -3
  39. package/dist/llamaEvaluator/LlamaBins.js +2 -2
  40. package/dist/llamaEvaluator/LlamaBins.js.map +1 -1
  41. package/dist/llamaEvaluator/LlamaChatSession.d.ts +7 -2
  42. package/dist/llamaEvaluator/LlamaChatSession.js +51 -11
  43. package/dist/llamaEvaluator/LlamaChatSession.js.map +1 -1
  44. package/dist/llamaEvaluator/LlamaContext.d.ts +31 -2
  45. package/dist/llamaEvaluator/LlamaContext.js +74 -7
  46. package/dist/llamaEvaluator/LlamaContext.js.map +1 -1
  47. package/dist/llamaEvaluator/LlamaGrammar.d.ts +14 -0
  48. package/dist/llamaEvaluator/LlamaGrammar.js +30 -0
  49. package/dist/llamaEvaluator/LlamaGrammar.js.map +1 -0
  50. package/dist/llamaEvaluator/LlamaModel.d.ts +49 -1
  51. package/dist/llamaEvaluator/LlamaModel.js +25 -9
  52. package/dist/llamaEvaluator/LlamaModel.js.map +1 -1
  53. package/dist/types.d.ts +1 -0
  54. package/dist/types.js +2 -0
  55. package/dist/types.js.map +1 -0
  56. package/dist/utils/binariesGithubRelease.d.ts +6 -0
  57. package/dist/utils/binariesGithubRelease.js +15 -0
  58. package/dist/utils/binariesGithubRelease.js.map +1 -0
  59. package/dist/utils/compileLLamaCpp.d.ts +3 -1
  60. package/dist/utils/compileLLamaCpp.js +34 -4
  61. package/dist/utils/compileLLamaCpp.js.map +1 -1
  62. package/dist/utils/getBin.d.ts +18 -4
  63. package/dist/utils/getBin.js +4 -2
  64. package/dist/utils/getBin.js.map +1 -1
  65. package/dist/utils/getGrammarsFolder.d.ts +1 -0
  66. package/dist/utils/getGrammarsFolder.js +18 -0
  67. package/dist/utils/getGrammarsFolder.js.map +1 -0
  68. package/dist/utils/getTextCompletion.d.ts +3 -0
  69. package/dist/utils/getTextCompletion.js +12 -0
  70. package/dist/utils/getTextCompletion.js.map +1 -0
  71. package/dist/utils/removeNullFields.d.ts +1 -0
  72. package/dist/utils/removeNullFields.js +9 -0
  73. package/dist/utils/removeNullFields.js.map +1 -0
  74. package/dist/utils/spawnCommand.d.ts +2 -1
  75. package/dist/utils/spawnCommand.js +2 -2
  76. package/dist/utils/spawnCommand.js.map +1 -1
  77. package/llama/addon.cpp +180 -42
  78. package/llama/binariesGithubRelease.json +3 -0
  79. package/llama/binding.gyp +6 -3
  80. package/llama/grammars/README.md +91 -0
  81. package/llama/grammars/arithmetic.gbnf +6 -0
  82. package/llama/grammars/chess.gbnf +13 -0
  83. package/llama/grammars/japanese.gbnf +7 -0
  84. package/llama/grammars/json.gbnf +25 -0
  85. package/llama/grammars/list.gbnf +4 -0
  86. package/llamaBins/linux-arm64-16.node +0 -0
  87. package/llamaBins/linux-arm64-17.node +0 -0
  88. package/llamaBins/linux-arm64-18.node +0 -0
  89. package/llamaBins/linux-arm64-19.node +0 -0
  90. package/llamaBins/linux-arm64-20.node +0 -0
  91. package/llamaBins/linux-armv7l-16.node +0 -0
  92. package/llamaBins/linux-armv7l-17.node +0 -0
  93. package/llamaBins/linux-armv7l-18.node +0 -0
  94. package/llamaBins/linux-armv7l-19.node +0 -0
  95. package/llamaBins/linux-armv7l-20.node +0 -0
  96. package/llamaBins/linux-ppc64le-16.node +0 -0
  97. package/llamaBins/linux-ppc64le-17.node +0 -0
  98. package/llamaBins/linux-ppc64le-18.node +0 -0
  99. package/llamaBins/linux-ppc64le-19.node +0 -0
  100. package/llamaBins/linux-ppc64le-20.node +0 -0
  101. package/llamaBins/linux-x64-16.node +0 -0
  102. package/llamaBins/linux-x64-17.node +0 -0
  103. package/llamaBins/linux-x64-18.node +0 -0
  104. package/llamaBins/linux-x64-19.node +0 -0
  105. package/llamaBins/linux-x64-20.node +0 -0
  106. package/llamaBins/mac-arm64-16.node +0 -0
  107. package/llamaBins/mac-arm64-17.node +0 -0
  108. package/llamaBins/mac-arm64-18.node +0 -0
  109. package/llamaBins/mac-arm64-19.node +0 -0
  110. package/llamaBins/mac-arm64-20.node +0 -0
  111. package/llamaBins/mac-x64-16.node +0 -0
  112. package/llamaBins/mac-x64-17.node +0 -0
  113. package/llamaBins/mac-x64-18.node +0 -0
  114. package/llamaBins/mac-x64-19.node +0 -0
  115. package/llamaBins/mac-x64-20.node +0 -0
  116. package/llamaBins/win-x64-16.node +0 -0
  117. package/llamaBins/win-x64-17.node +0 -0
  118. package/llamaBins/win-x64-18.node +0 -0
  119. package/llamaBins/win-x64-19.node +0 -0
  120. package/llamaBins/win-x64-20.node +0 -0
  121. package/package.json +12 -6
@@ -0,0 +1,91 @@
1
+ # GBNF Guide
2
+
3
+ GBNF (GGML BNF) is a format for defining [formal grammars](https://en.wikipedia.org/wiki/Formal_grammar) to constrain model outputs in `llama.cpp`. For example, you can use it to force the model to generate valid JSON, or speak only in emojis. GBNF grammars are supported in various ways in `examples/main` and `examples/server`.
4
+
5
+ ## Background
6
+
7
+ [Bakus-Naur Form (BNF)](https://en.wikipedia.org/wiki/Backus%E2%80%93Naur_form) is a notation for describing the syntax of formal languages like programming languages, file formats, and protocols. GBNF is an extension of BNF that primarily adds a few modern regex-like features.
8
+
9
+ ## Basics
10
+
11
+ In GBNF, we define *production rules* that specify how a *non-terminal* (rule name) can be replaced with sequences of *terminals* (characters, specifically Unicode [code points](https://en.wikipedia.org/wiki/Code_point)) and other non-terminals. The basic format of a production rule is `nonterminal ::= sequence...`.
12
+
13
+ ## Example
14
+
15
+ Before going deeper, let's look at some of the features demonstrated in `grammars/chess.gbnf`, a small chess notation grammar:
16
+ ```
17
+ # `root` specifies the pattern for the overall output
18
+ root ::= (
19
+ # it must start with the characters "1. " followed by a sequence
20
+ # of characters that match the `move` rule, followed by a space, followed
21
+ # by another move, and then a newline
22
+ "1. " move " " move "\n"
23
+
24
+ # it's followed by one or more subsequent moves, numbered with one or two digits
25
+ ([1-9] [0-9]? ". " move " " move "\n")+
26
+ )
27
+
28
+ # `move` is an abstract representation, which can be a pawn, nonpawn, or castle.
29
+ # The `[+#]?` denotes the possibility of checking or mate signs after moves
30
+ move ::= (pawn | nonpawn | castle) [+#]?
31
+
32
+ pawn ::= ...
33
+ nonpawn ::= ...
34
+ castle ::= ...
35
+ ```
36
+
37
+ ## Non-Terminals and Terminals
38
+
39
+ Non-terminal symbols (rule names) stand for a pattern of terminals and other non-terminals. They are required to be a dashed lowercase word, like `move`, `castle`, or `check-mate`.
40
+
41
+ Terminals are actual characters ([code points](https://en.wikipedia.org/wiki/Code_point)). They can be specified as a sequence like `"1"` or `"O-O"` or as ranges like `[1-9]` or `[NBKQR]`.
42
+
43
+ ## Characters and character ranges
44
+
45
+ Terminals support the full range of Unicode. Unicode characters can be specified directly in the grammar, for example `hiragana ::= [ぁ-ゟ]`, or with escapes: 8-bit (`\xXX`), 16-bit (`\uXXXX`) or 32-bit (`\UXXXXXXXX`).
46
+
47
+ Character ranges can be negated with `^`:
48
+ ```
49
+ single-line ::= [^\n]+ "\n"`
50
+ ```
51
+
52
+ ## Sequences and Alternatives
53
+
54
+ The order of symbols in a sequence matter. For example, in `"1. " move " " move "\n"`, the `"1. "` must come before the first `move`, etc.
55
+
56
+ Alternatives, denoted by `|`, give different sequences that are acceptable. For example, in `move ::= pawn | nonpawn | castle`, `move` can be a `pawn` move, a `nonpawn` move, or a `castle`.
57
+
58
+ Parentheses `()` can be used to group sequences, which allows for embedding alternatives in a larger rule or applying repetition and optptional symbols (below) to a sequence.
59
+
60
+ ## Repetition and Optional Symbols
61
+
62
+ - `*` after a symbol or sequence means that it can be repeated zero or more times.
63
+ - `+` denotes that the symbol or sequence should appear one or more times.
64
+ - `?` makes the preceding symbol or sequence optional.
65
+
66
+ ## Comments and newlines
67
+
68
+ Comments can be specified with `#`:
69
+ ```
70
+ # defines optional whitspace
71
+ ws ::= [ \t\n]+
72
+ ```
73
+
74
+ Newlines are allowed between rules and between symbols or sequences nested inside parentheses. Additionally, a newline after an alternate marker `|` will continue the current rule, even outside of parentheses.
75
+
76
+ ## The root rule
77
+
78
+ In a full grammar, the `root` rule always defines the starting point of the grammar. In other words, it specifies what the entire output must match.
79
+
80
+ ```
81
+ # a grammar for lists
82
+ root ::= ("- " item)+
83
+ item ::= [^\n]+ "\n"
84
+ ```
85
+
86
+ ## Next steps
87
+
88
+ This guide provides a brief overview. Check out the GBNF files in this directory (`grammars/`) for examples of full grammars. You can try them out with:
89
+ ```
90
+ ./main -m <model> --grammar-file grammars/some-grammar.gbnf -p 'Some prompt'
91
+ ```
@@ -0,0 +1,6 @@
1
+ root ::= (expr "=" ws term "\n")+
2
+ expr ::= term ([-+*/] term)*
3
+ term ::= ident | num | "(" ws expr ")" ws
4
+ ident ::= [a-z] [a-z0-9_]* ws
5
+ num ::= [0-9]+ ws
6
+ ws ::= [ \t\n]*
@@ -0,0 +1,13 @@
1
+ # Specifies chess moves as a list in algebraic notation, using PGN conventions
2
+
3
+ # Force first move to "1. ", then any 1-2 digit number after, relying on model to follow the pattern
4
+ root ::= "1. " move " " move "\n" ([1-9] [0-9]? ". " move " " move "\n")+
5
+ move ::= (pawn | nonpawn | castle) [+#]?
6
+
7
+ # piece type, optional file/rank, optional capture, dest file & rank
8
+ nonpawn ::= [NBKQR] [a-h]? [1-8]? "x"? [a-h] [1-8]
9
+
10
+ # optional file & capture, dest file & rank, optional promotion
11
+ pawn ::= ([a-h] "x")? [a-h] [1-8] ("=" [NBKQR])?
12
+
13
+ castle ::= "O-O" "-O"?
@@ -0,0 +1,7 @@
1
+ # A probably incorrect grammar for Japanese
2
+ root ::= jp-char+ ([ \t\n] jp-char+)*
3
+ jp-char ::= hiragana | katakana | punctuation | cjk
4
+ hiragana ::= [ぁ-ゟ]
5
+ katakana ::= [ァ-ヿ]
6
+ punctuation ::= [、-〾]
7
+ cjk ::= [一-鿿]
@@ -0,0 +1,25 @@
1
+ root ::= object
2
+ value ::= object | array | string | number | ("true" | "false" | "null") ws
3
+
4
+ object ::=
5
+ "{" ws (
6
+ string ":" ws value
7
+ ("," ws string ":" ws value)*
8
+ )? "}" ws
9
+
10
+ array ::=
11
+ "[" ws (
12
+ value
13
+ ("," ws value)*
14
+ )? "]" ws
15
+
16
+ string ::=
17
+ "\"" (
18
+ [^"\\] |
19
+ "\\" (["\\/bfnrt] | "u" [0-9a-fA-F] [0-9a-fA-F] [0-9a-fA-F] [0-9a-fA-F]) # escapes
20
+ )* "\"" ws
21
+
22
+ number ::= ("-"? ([0-9] | [1-9] [0-9]*)) ("." [0-9]+)? ([eE] [-+]? [0-9]+)? ws
23
+
24
+ # Optional space: by convention, applied in this grammar after literal chars when allowed
25
+ ws ::= ([ \t\n] ws)?
@@ -0,0 +1,4 @@
1
+ root ::= item+
2
+
3
+ # Excludes various line break characters
4
+ item ::= "- " [^\r\n\x0b\x0c\x85\u2028\u2029]+ "\n"
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
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "node-llama-cpp",
3
- "version": "1.3.1",
3
+ "version": "2.1.0",
4
4
  "description": "node.js bindings for llama.cpp",
5
5
  "main": "dist/index.js",
6
6
  "type": "module",
@@ -40,10 +40,10 @@
40
40
  "node": ">=18.0.0"
41
41
  },
42
42
  "scripts": {
43
- "postinstall": "node ./dist/cli/cli.js postinstall",
44
43
  "prepare": "[ $CI = true ] || [ -d '.husky/_' ] || husky install",
45
44
  "prebuild": "rm -rf ./dist ./tsconfig.tsbuildinfo",
46
45
  "build": "tsc --build tsconfig.json --force",
46
+ "addPostinstallScript": "npm pkg set scripts.postinstall=\"node ./dist/cli/cli.js postinstall\"",
47
47
  "generate-docs": "typedoc",
48
48
  "prewatch": "rm -rf ./dist ./tsconfig.tsbuildinfo",
49
49
  "watch": "tsc --build tsconfig.json --watch --force",
@@ -54,7 +54,8 @@
54
54
  "lint": "npm run lint:eslint",
55
55
  "lint:eslint": "eslint --ext .js --ext .ts .",
56
56
  "format": "npm run lint:eslint -- --fix",
57
- "clean": "rm -rf ./node_modules ./dist ./tsconfig.tsbuildinfo"
57
+ "clean": "rm -rf ./node_modules ./dist ./tsconfig.tsbuildinfo",
58
+ "postinstall": "node ./dist/cli/cli.js postinstall"
58
59
  },
59
60
  "repository": {
60
61
  "type": "git",
@@ -68,15 +69,19 @@
68
69
  "node-gyp",
69
70
  "prebuilt-binaries",
70
71
  "llm",
71
- "ggml",
72
- "ggmlv3",
72
+ "gguf",
73
+ "grammar",
74
+ "json-grammar",
75
+ "temperature",
76
+ "topK",
77
+ "topP",
73
78
  "raspberry-pi",
74
79
  "self-hosted",
75
80
  "local",
76
81
  "catai"
77
82
  ],
78
83
  "author": "Gilad S.",
79
- "license": "ISC",
84
+ "license": "MIT",
80
85
  "bugs": {
81
86
  "url": "https://github.com/withcatai/node-llama-cpp/issues"
82
87
  },
@@ -119,6 +124,7 @@
119
124
  "node-stream-zip": "^1.15.0",
120
125
  "octokit": "^3.1.0",
121
126
  "ora": "^7.0.1",
127
+ "simple-git": "^3.19.1",
122
128
  "uuid": "^9.0.0",
123
129
  "yargs": "^17.7.2"
124
130
  }