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.
- package/LICENSE +21 -0
- package/README.md +210 -0
- package/dist/AbortError.d.ts +3 -0
- package/dist/AbortError.js +6 -0
- package/dist/AbortError.js.map +1 -0
- package/dist/ChatPromptWrapper.d.ts +7 -0
- package/dist/ChatPromptWrapper.js +14 -0
- package/dist/ChatPromptWrapper.js.map +1 -0
- package/dist/LlamaChatSession.d.ts +24 -0
- package/dist/LlamaChatSession.js +86 -0
- package/dist/LlamaChatSession.js.map +1 -0
- package/dist/LlamaModel.d.ts +13 -0
- package/dist/LlamaModel.js +43 -0
- package/dist/LlamaModel.js.map +1 -0
- package/dist/chatWrappers/EmptyChatPromptWrapper.d.ts +3 -0
- package/dist/chatWrappers/EmptyChatPromptWrapper.js +4 -0
- package/dist/chatWrappers/EmptyChatPromptWrapper.js.map +1 -0
- package/dist/chatWrappers/LlamaChatPromptWrapper.d.ts +8 -0
- package/dist/chatWrappers/LlamaChatPromptWrapper.js +16 -0
- package/dist/chatWrappers/LlamaChatPromptWrapper.js.map +1 -0
- package/dist/cli/cli.d.ts +2 -0
- package/dist/cli/cli.js +33 -0
- package/dist/cli/cli.js.map +1 -0
- package/dist/cli/commands/BuildCommand.d.ts +8 -0
- package/dist/cli/commands/BuildCommand.js +41 -0
- package/dist/cli/commands/BuildCommand.js.map +1 -0
- package/dist/cli/commands/ChatCommand.d.ts +8 -0
- package/dist/cli/commands/ChatCommand.js +83 -0
- package/dist/cli/commands/ChatCommand.js.map +1 -0
- package/dist/cli/commands/ClearCommand.d.ts +7 -0
- package/dist/cli/commands/ClearCommand.js +42 -0
- package/dist/cli/commands/ClearCommand.js.map +1 -0
- package/dist/cli/commands/DownloadCommand.d.ts +11 -0
- package/dist/cli/commands/DownloadCommand.js +179 -0
- package/dist/cli/commands/DownloadCommand.js.map +1 -0
- package/dist/cli/commands/OnPostInstallCommand.d.ts +4 -0
- package/dist/cli/commands/OnPostInstallCommand.js +24 -0
- package/dist/cli/commands/OnPostInstallCommand.js.map +1 -0
- package/dist/commands.d.ts +4 -0
- package/dist/commands.js +5 -0
- package/dist/commands.js.map +1 -0
- package/dist/config.d.ts +9 -0
- package/dist/config.js +25 -0
- package/dist/config.js.map +1 -0
- package/dist/index.d.ts +7 -0
- package/dist/index.js +8 -0
- package/dist/index.js.map +1 -0
- package/dist/utils/clearLlamaBuild.d.ts +1 -0
- package/dist/utils/clearLlamaBuild.js +12 -0
- package/dist/utils/clearLlamaBuild.js.map +1 -0
- package/dist/utils/clearTempFolder.d.ts +1 -0
- package/dist/utils/clearTempFolder.js +16 -0
- package/dist/utils/clearTempFolder.js.map +1 -0
- package/dist/utils/compileLLamaCpp.d.ts +6 -0
- package/dist/utils/compileLLamaCpp.js +43 -0
- package/dist/utils/compileLLamaCpp.js.map +1 -0
- package/dist/utils/getBin.d.ts +18 -0
- package/dist/utils/getBin.js +70 -0
- package/dist/utils/getBin.js.map +1 -0
- package/dist/utils/spawnCommand.d.ts +1 -0
- package/dist/utils/spawnCommand.js +39 -0
- package/dist/utils/spawnCommand.js.map +1 -0
- package/dist/utils/usedBinFlag.d.ts +6 -0
- package/dist/utils/usedBinFlag.js +15 -0
- package/dist/utils/usedBinFlag.js.map +1 -0
- package/dist/utils/withLock.d.ts +1 -0
- package/dist/utils/withLock.js +19 -0
- package/dist/utils/withLock.js.map +1 -0
- package/dist/utils/withOra.d.ts +5 -0
- package/dist/utils/withOra.js +21 -0
- package/dist/utils/withOra.js.map +1 -0
- package/llama/.clang-format +46 -0
- package/llama/addon.cpp +183 -0
- package/llama/binding.gyp +24 -0
- package/llama/usedBin.json +3 -0
- package/llamaBins/linux-arm64-16.node +0 -0
- package/llamaBins/linux-arm64-17.node +0 -0
- package/llamaBins/linux-arm64-18.node +0 -0
- package/llamaBins/linux-arm64-19.node +0 -0
- package/llamaBins/linux-arm64-20.node +0 -0
- package/llamaBins/linux-armv7l-16.node +0 -0
- package/llamaBins/linux-armv7l-17.node +0 -0
- package/llamaBins/linux-armv7l-18.node +0 -0
- package/llamaBins/linux-armv7l-19.node +0 -0
- package/llamaBins/linux-armv7l-20.node +0 -0
- package/llamaBins/linux-ppc64le-16.node +0 -0
- package/llamaBins/linux-ppc64le-17.node +0 -0
- package/llamaBins/linux-ppc64le-18.node +0 -0
- package/llamaBins/linux-ppc64le-19.node +0 -0
- package/llamaBins/linux-ppc64le-20.node +0 -0
- package/llamaBins/linux-x64-16.node +0 -0
- package/llamaBins/linux-x64-17.node +0 -0
- package/llamaBins/linux-x64-18.node +0 -0
- package/llamaBins/linux-x64-19.node +0 -0
- package/llamaBins/linux-x64-20.node +0 -0
- package/llamaBins/mac-arm64-16.node +0 -0
- package/llamaBins/mac-arm64-17.node +0 -0
- package/llamaBins/mac-arm64-18.node +0 -0
- package/llamaBins/mac-arm64-19.node +0 -0
- package/llamaBins/mac-arm64-20.node +0 -0
- package/llamaBins/mac-x64-16.node +0 -0
- package/llamaBins/mac-x64-17.node +0 -0
- package/llamaBins/mac-x64-18.node +0 -0
- package/llamaBins/mac-x64-19.node +0 -0
- package/llamaBins/mac-x64-20.node +0 -0
- package/llamaBins/win-x64-16.node +0 -0
- package/llamaBins/win-x64-17.node +0 -0
- package/llamaBins/win-x64-18.node +0 -0
- package/llamaBins/win-x64-19.node +0 -0
- package/llamaBins/win-x64-20.node +0 -0
- package/package.json +71 -10
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2023 Gilad S.
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
CHANGED
|
@@ -0,0 +1,210 @@
|
|
|
1
|
+
# Node Llama.cpp
|
|
2
|
+
Node.js bindings for llama.cpp.
|
|
3
|
+
|
|
4
|
+
Pre-built bindings are provided with a fallback to building from source with `node-gyp`.
|
|
5
|
+
|
|
6
|
+
[](https://github.com/withcatai/node-llama-cpp/actions/workflows/build.yml)
|
|
7
|
+
[](https://www.npmjs.com/package/node-llama-cpp)
|
|
8
|
+
|
|
9
|
+
|
|
10
|
+
## Installation
|
|
11
|
+
```bash
|
|
12
|
+
npm install --save node-llama-cpp
|
|
13
|
+
```
|
|
14
|
+
|
|
15
|
+
This package comes with pre-built binaries for macOS, Linux and Windows.
|
|
16
|
+
|
|
17
|
+
If binaries are not available for your platform, it'll fallback to download the latest version of `llama.cpp` and build it from source with `node-gyp`.
|
|
18
|
+
To disable this behavior set the environment variable `NODE_LLAMA_CPP_SKIP_DOWNLOAD` to `true`.
|
|
19
|
+
|
|
20
|
+
## Documentation
|
|
21
|
+
### Usage
|
|
22
|
+
#### As a chatbot
|
|
23
|
+
```typescript
|
|
24
|
+
import {fileURLToPath} from "url";
|
|
25
|
+
import path from "path";
|
|
26
|
+
import {LlamaModel, LlamaChatSession} from "node-llama-cpp";
|
|
27
|
+
|
|
28
|
+
const __dirname = path.dirname(fileURLToPath(import.meta.url));
|
|
29
|
+
|
|
30
|
+
const model = new LlamaModel({
|
|
31
|
+
modelPath: path.join(__dirname, "models", "vicuna-13b-v1.5-16k.ggmlv3.q5_1.bin")
|
|
32
|
+
})
|
|
33
|
+
const session = new LlamaChatSession({model});
|
|
34
|
+
|
|
35
|
+
|
|
36
|
+
const q1 = "Hi there, how are you?";
|
|
37
|
+
console.log("User: " + q1);
|
|
38
|
+
|
|
39
|
+
const a1 = await session.prompt(q1);
|
|
40
|
+
console.log("AI: " + a1);
|
|
41
|
+
|
|
42
|
+
|
|
43
|
+
const q2 = "Summerize what you said";
|
|
44
|
+
console.log("User: " + q2);
|
|
45
|
+
|
|
46
|
+
const a2 = await session.prompt(q2);
|
|
47
|
+
console.log("AI: " + a2);
|
|
48
|
+
```
|
|
49
|
+
|
|
50
|
+
##### Custom prompt handling against the model
|
|
51
|
+
```typescript
|
|
52
|
+
import {fileURLToPath} from "url";
|
|
53
|
+
import path from "path";
|
|
54
|
+
import {LlamaModel, LlamaChatSession, ChatPromptWrapper} from "node-llama-cpp";
|
|
55
|
+
|
|
56
|
+
const __dirname = path.dirname(fileURLToPath(import.meta.url));
|
|
57
|
+
|
|
58
|
+
export class MyCustomChatPromptWrapper extends ChatPromptWrapper {
|
|
59
|
+
public override wrapPrompt(prompt: string, {systemPrompt, promptIndex}: {systemPrompt: string, promptIndex: number}) {
|
|
60
|
+
if (promptIndex === 0) {
|
|
61
|
+
return "SYSTEM: " + systemPrompt + "\nUSER: " + prompt + "\nASSISTANT:";
|
|
62
|
+
} else {
|
|
63
|
+
return "USER: " + prompt + "\nASSISTANT:";
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
public override getStopStrings(): string[] {
|
|
68
|
+
return ["USER:"];
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
const model = new LlamaModel({
|
|
73
|
+
modelPath: path.join(__dirname, "models", "vicuna-13b-v1.5-16k.ggmlv3.q5_1.bin"),
|
|
74
|
+
promptWrapper: new MyCustomChatPromptWrapper() // by default, LlamaChatPromptWrapper is used
|
|
75
|
+
})
|
|
76
|
+
const session = new LlamaChatSession({model});
|
|
77
|
+
|
|
78
|
+
|
|
79
|
+
const q1 = "Hi there, how are you?";
|
|
80
|
+
console.log("User: " + q1);
|
|
81
|
+
|
|
82
|
+
const a1 = await session.prompt(q1);
|
|
83
|
+
console.log("AI: " + a1);
|
|
84
|
+
|
|
85
|
+
|
|
86
|
+
const q2 = "Summerize what you said";
|
|
87
|
+
console.log("User: " + q2);
|
|
88
|
+
|
|
89
|
+
const a2 = await session.prompt(q2);
|
|
90
|
+
console.log("AI: " + a2);
|
|
91
|
+
```
|
|
92
|
+
|
|
93
|
+
#### Raw
|
|
94
|
+
```typescript
|
|
95
|
+
import {fileURLToPath} from "url";
|
|
96
|
+
import path from "path";
|
|
97
|
+
import {LlamaModel, LlamaChatSession} from "node-llama-cpp";
|
|
98
|
+
|
|
99
|
+
const __dirname = path.dirname(fileURLToPath(import.meta.url));
|
|
100
|
+
|
|
101
|
+
const model = new LlamaChatSession({
|
|
102
|
+
modelPath: path.join(__dirname, "models", "vicuna-13b-v1.5-16k.ggmlv3.q5_1.bin")
|
|
103
|
+
});
|
|
104
|
+
|
|
105
|
+
const q1 = "Hi there, how are you?";
|
|
106
|
+
console.log("AI: " + q1);
|
|
107
|
+
|
|
108
|
+
const tokens = model.encode(q1);
|
|
109
|
+
const res: number[] = [];
|
|
110
|
+
for await (const chunk of model.evaluate(tokens)) {
|
|
111
|
+
res.push(chunk);
|
|
112
|
+
|
|
113
|
+
// it's important to not concatinate the results as strings,
|
|
114
|
+
// as doing so will break some characters (like some emojis) that are made of multiple tokens.
|
|
115
|
+
// by using an array of tokens, we can decode them correctly together.
|
|
116
|
+
const resString: string = model.decode(Uint32Array.from(res));
|
|
117
|
+
|
|
118
|
+
const lastPart = resString.split("ASSISTANT:").reverse()[0];
|
|
119
|
+
if (lastPart.includes("USER:"))
|
|
120
|
+
break;
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
const a1 = model.decode(Uint32Array.from(res)).split("USER:")[0];
|
|
124
|
+
console.log("AI: " + a1);
|
|
125
|
+
```
|
|
126
|
+
|
|
127
|
+
### CLI
|
|
128
|
+
```txt
|
|
129
|
+
Usage: node-llama-cpp <command> [options]
|
|
130
|
+
|
|
131
|
+
Commands:
|
|
132
|
+
node-llama-cpp download Download a release of llama.cpp and compile it
|
|
133
|
+
node-llama-cpp build Compile the currently downloaded llama.cpp
|
|
134
|
+
node-llama-cpp clear [type] Clear files created by llama-cli
|
|
135
|
+
node-llama-cpp chat Chat with a LLama model
|
|
136
|
+
|
|
137
|
+
Options:
|
|
138
|
+
-h, --help Show help [boolean]
|
|
139
|
+
-v, --version Show version number [boolean] [boolean]
|
|
140
|
+
```
|
|
141
|
+
|
|
142
|
+
#### `download` command
|
|
143
|
+
```txt
|
|
144
|
+
node-llama-cpp download
|
|
145
|
+
|
|
146
|
+
Download a release of llama.cpp and compile it
|
|
147
|
+
|
|
148
|
+
Options:
|
|
149
|
+
-h, --help Show help [boolean]
|
|
150
|
+
--repo The GitHub repository to download a release of llama.cpp from. Can also be set v
|
|
151
|
+
ia the NODE_LLAMA_CPP_REPO environment variable
|
|
152
|
+
[string] [default: "ggerganov/llama.cpp"]
|
|
153
|
+
--release The tag of the llama.cpp release to download. Can also be set via the NODE_LLAMA
|
|
154
|
+
_CPP_REPO_RELEASE environment variable [string] [default: "latest"]
|
|
155
|
+
--arch The architecture to compile llama.cpp for [string]
|
|
156
|
+
--nodeTarget The Node.js version to compile llama.cpp for. Example: v18.0.0 [string]
|
|
157
|
+
-v, --version Show version number [boolean]
|
|
158
|
+
```
|
|
159
|
+
|
|
160
|
+
#### `build` command
|
|
161
|
+
```txt
|
|
162
|
+
node-llama-cpp build
|
|
163
|
+
|
|
164
|
+
Compile the currently downloaded llama.cpp
|
|
165
|
+
|
|
166
|
+
Options:
|
|
167
|
+
-h, --help Show help [boolean]
|
|
168
|
+
--arch The architecture to compile llama.cpp for [string]
|
|
169
|
+
--nodeTarget The Node.js version to compile llama.cpp for. Example: v18.0.0 [string]
|
|
170
|
+
-v, --version Show version number [boolean]
|
|
171
|
+
```
|
|
172
|
+
|
|
173
|
+
#### `clear` command
|
|
174
|
+
```txt
|
|
175
|
+
node-llama-cpp clear [type]
|
|
176
|
+
|
|
177
|
+
Clear files created by llama-cli
|
|
178
|
+
|
|
179
|
+
Options:
|
|
180
|
+
-h, --help Show help [boolean]
|
|
181
|
+
--type Files to clear [string] [choices: "source", "build", "all"] [default: "all"]
|
|
182
|
+
-v, --version Show version number [boolean]
|
|
183
|
+
```
|
|
184
|
+
|
|
185
|
+
#### `chat` command
|
|
186
|
+
```txt
|
|
187
|
+
node-llama-cpp chat
|
|
188
|
+
|
|
189
|
+
Chat with a LLama model
|
|
190
|
+
|
|
191
|
+
Required:
|
|
192
|
+
--model LLama model file to use for the chat [string] [required]
|
|
193
|
+
|
|
194
|
+
Optional:
|
|
195
|
+
--systemInfo Print llama.cpp system info [boolean] [default: false]
|
|
196
|
+
--systemPrompt System prompt to use against the model. [default value: You are a helpful, res
|
|
197
|
+
pectful and honest assistant. Always answer as helpfully as possible. If a que
|
|
198
|
+
stion does not make any sense, or is not factually coherent, explain why inste
|
|
199
|
+
ad of answering something not correct. If you don't know the answer to a quest
|
|
200
|
+
ion, please don't share false information.]
|
|
201
|
+
[string] [default: "You are a helpful, respectful and honest assistant. Always answer as helpfully
|
|
202
|
+
as possible.
|
|
203
|
+
If a question does not make any sense, or is not factually coherent, explain why ins
|
|
204
|
+
tead of answering something not correct. If you don't know the answer to a question, please don't
|
|
205
|
+
share false information."]
|
|
206
|
+
|
|
207
|
+
Options:
|
|
208
|
+
-h, --help Show help [boolean]
|
|
209
|
+
-v, --version Show version number [boolean]
|
|
210
|
+
```
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"AbortError.js","sourceRoot":"","sources":["../src/AbortError.ts"],"names":[],"mappings":"AAAA,MAAM,OAAO,UAAW,SAAQ,KAAK;IACjC;QACI,KAAK,CAAC,YAAY,CAAC,CAAC;IACxB,CAAC;CACJ"}
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
export class ChatPromptWrapper {
|
|
2
|
+
wrapPrompt(prompt, { systemPrompt, promptIndex }) {
|
|
3
|
+
if (promptIndex === 0) {
|
|
4
|
+
return systemPrompt + "\n" + prompt;
|
|
5
|
+
}
|
|
6
|
+
else {
|
|
7
|
+
return prompt;
|
|
8
|
+
}
|
|
9
|
+
}
|
|
10
|
+
getStopStrings() {
|
|
11
|
+
return [];
|
|
12
|
+
}
|
|
13
|
+
}
|
|
14
|
+
//# sourceMappingURL=ChatPromptWrapper.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"ChatPromptWrapper.js","sourceRoot":"","sources":["../src/ChatPromptWrapper.ts"],"names":[],"mappings":"AAAA,MAAM,OAAgB,iBAAiB;IAC5B,UAAU,CAAC,MAAc,EAAE,EAAC,YAAY,EAAE,WAAW,EAA8C;QACtG,IAAI,WAAW,KAAK,CAAC,EAAE;YACnB,OAAO,YAAY,GAAG,IAAI,GAAG,MAAM,CAAC;SACvC;aAAM;YACH,OAAO,MAAM,CAAC;SACjB;IACL,CAAC;IAEM,cAAc;QACjB,OAAO,EAAE,CAAC;IACd,CAAC;CACJ"}
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
/// <reference types="node" />
|
|
2
|
+
import { LlamaModel } from "./LlamaModel.js";
|
|
3
|
+
import { ChatPromptWrapper } from "./ChatPromptWrapper.js";
|
|
4
|
+
export declare class LlamaChatSession {
|
|
5
|
+
private readonly _model;
|
|
6
|
+
private readonly _systemPrompt;
|
|
7
|
+
private readonly _printLLamaSystemInfo;
|
|
8
|
+
private readonly _promptWrapper;
|
|
9
|
+
private _promptIndex;
|
|
10
|
+
private _initialized;
|
|
11
|
+
constructor({ model, printLLamaSystemInfo, promptWrapper, systemPrompt }: {
|
|
12
|
+
model: LlamaModel;
|
|
13
|
+
printLLamaSystemInfo?: boolean;
|
|
14
|
+
promptWrapper?: ChatPromptWrapper;
|
|
15
|
+
systemPrompt?: string;
|
|
16
|
+
});
|
|
17
|
+
get initialized(): boolean;
|
|
18
|
+
get model(): LlamaModel;
|
|
19
|
+
init(): Promise<void>;
|
|
20
|
+
prompt(prompt: string, onToken?: (token: number) => void, { signal }?: {
|
|
21
|
+
signal?: AbortSignal;
|
|
22
|
+
}): Promise<string>;
|
|
23
|
+
private _evalTokens;
|
|
24
|
+
}
|
|
@@ -0,0 +1,86 @@
|
|
|
1
|
+
import { defaultChatSystemPrompt } from "./config.js";
|
|
2
|
+
import { withLock } from "./utils/withLock.js";
|
|
3
|
+
import { LlamaChatPromptWrapper } from "./chatWrappers/LlamaChatPromptWrapper.js";
|
|
4
|
+
import { AbortError } from "./AbortError.js";
|
|
5
|
+
export class LlamaChatSession {
|
|
6
|
+
_model;
|
|
7
|
+
_systemPrompt;
|
|
8
|
+
_printLLamaSystemInfo;
|
|
9
|
+
_promptWrapper;
|
|
10
|
+
_promptIndex = 0;
|
|
11
|
+
_initialized = false;
|
|
12
|
+
constructor({ model, printLLamaSystemInfo = false, promptWrapper = new LlamaChatPromptWrapper(), systemPrompt = defaultChatSystemPrompt }) {
|
|
13
|
+
this._model = model;
|
|
14
|
+
this._printLLamaSystemInfo = printLLamaSystemInfo;
|
|
15
|
+
this._promptWrapper = promptWrapper;
|
|
16
|
+
this._systemPrompt = systemPrompt;
|
|
17
|
+
}
|
|
18
|
+
get initialized() {
|
|
19
|
+
return this._initialized;
|
|
20
|
+
}
|
|
21
|
+
get model() {
|
|
22
|
+
return this._model;
|
|
23
|
+
}
|
|
24
|
+
async init() {
|
|
25
|
+
await withLock(this, "init", async () => {
|
|
26
|
+
if (this._initialized)
|
|
27
|
+
return;
|
|
28
|
+
if (this._printLLamaSystemInfo)
|
|
29
|
+
console.log("Llama system info", this._model.systemInfo);
|
|
30
|
+
this._initialized = true;
|
|
31
|
+
});
|
|
32
|
+
}
|
|
33
|
+
async prompt(prompt, onToken, { signal } = {}) {
|
|
34
|
+
if (!this.initialized)
|
|
35
|
+
await this.init();
|
|
36
|
+
return await withLock(this, "prompt", async () => {
|
|
37
|
+
const promptText = this._promptWrapper.wrapPrompt(prompt, { systemPrompt: this._systemPrompt, promptIndex: this._promptIndex });
|
|
38
|
+
this._promptIndex++;
|
|
39
|
+
return await this._evalTokens(this._model.encode(promptText), onToken, { signal });
|
|
40
|
+
});
|
|
41
|
+
}
|
|
42
|
+
async _evalTokens(tokens, onToken, { signal } = {}) {
|
|
43
|
+
const stopStrings = this._promptWrapper.getStopStrings();
|
|
44
|
+
const stopStringIndexes = Array(stopStrings.length).fill(0);
|
|
45
|
+
const skippedChunksQueue = [];
|
|
46
|
+
let res = "";
|
|
47
|
+
for await (const chunk of this._model.evaluate(tokens)) {
|
|
48
|
+
if (signal?.aborted)
|
|
49
|
+
throw new AbortError();
|
|
50
|
+
const tokenStr = this._model.decode(Uint32Array.from([chunk]));
|
|
51
|
+
let skipTokenEvent = false;
|
|
52
|
+
for (let stopStringIndex = 0; stopStringIndex < stopStrings.length; stopStringIndex++) {
|
|
53
|
+
const stopString = stopStrings[stopStringIndex];
|
|
54
|
+
let localShouldSkipTokenEvent = false;
|
|
55
|
+
for (let i = 0; i < tokenStr.length && stopStringIndexes[stopStringIndex] !== stopString.length; i++) {
|
|
56
|
+
if (tokenStr[i] === stopString[stopStringIndexes[stopStringIndex]]) {
|
|
57
|
+
stopStringIndexes[stopStringIndex]++;
|
|
58
|
+
localShouldSkipTokenEvent = true;
|
|
59
|
+
}
|
|
60
|
+
else {
|
|
61
|
+
stopStringIndexes[stopStringIndex] = 0;
|
|
62
|
+
localShouldSkipTokenEvent = false;
|
|
63
|
+
break;
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
if (stopStringIndexes[stopStringIndex] === stopString.length) {
|
|
67
|
+
return res;
|
|
68
|
+
}
|
|
69
|
+
skipTokenEvent ||= localShouldSkipTokenEvent;
|
|
70
|
+
}
|
|
71
|
+
if (skipTokenEvent) {
|
|
72
|
+
skippedChunksQueue.push(chunk);
|
|
73
|
+
continue;
|
|
74
|
+
}
|
|
75
|
+
while (skippedChunksQueue.length > 0) {
|
|
76
|
+
const token = skippedChunksQueue.shift();
|
|
77
|
+
res += this._model.decode(Uint32Array.from([token]));
|
|
78
|
+
onToken?.(token);
|
|
79
|
+
}
|
|
80
|
+
res += tokenStr;
|
|
81
|
+
onToken?.(chunk);
|
|
82
|
+
}
|
|
83
|
+
return res;
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
//# sourceMappingURL=LlamaChatSession.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"LlamaChatSession.js","sourceRoot":"","sources":["../src/LlamaChatSession.ts"],"names":[],"mappings":"AAAA,OAAO,EAAC,uBAAuB,EAAC,MAAM,aAAa,CAAC;AACpD,OAAO,EAAC,QAAQ,EAAC,MAAM,qBAAqB,CAAC;AAG7C,OAAO,EAAC,sBAAsB,EAAC,MAAM,0CAA0C,CAAC;AAChF,OAAO,EAAC,UAAU,EAAC,MAAM,iBAAiB,CAAC;AAG3C,MAAM,OAAO,gBAAgB;IACR,MAAM,CAAa;IACnB,aAAa,CAAS;IACtB,qBAAqB,CAAU;IAC/B,cAAc,CAAoB;IAC3C,YAAY,GAAW,CAAC,CAAC;IACzB,YAAY,GAAY,KAAK,CAAC;IAEtC,YAAmB,EACf,KAAK,EACL,oBAAoB,GAAG,KAAK,EAC5B,aAAa,GAAG,IAAI,sBAAsB,EAAE,EAC5C,YAAY,GAAG,uBAAuB,EAMzC;QACG,IAAI,CAAC,MAAM,GAAG,KAAK,CAAC;QACpB,IAAI,CAAC,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CAAC;wBAClC,MAAM;qBACT;iBACJ;gBAED,IAAI,iBAAiB,CAAC,eAAe,CAAC,KAAK,UAAU,CAAC,MAAM,EAAE;oBAC1D,OAAO,GAAG,CAAC;iBACd;gBAED,cAAc,KAAK,yBAAyB,CAAC;aAChD;YAED,IAAI,cAAc,EAAE;gBAChB,kBAAkB,CAAC,IAAI,CAAC,KAAK,CAAC,CAAC;gBAC/B,SAAS;aACZ;YAED,OAAO,kBAAkB,CAAC,MAAM,GAAG,CAAC,EAAE;gBAClC,MAAM,KAAK,GAAG,kBAAkB,CAAC,KAAK,EAAG,CAAC;gBAC1C,GAAG,IAAI,IAAI,CAAC,MAAM,CAAC,MAAM,CAAC,WAAW,CAAC,IAAI,CAAC,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC;gBACrD,OAAO,EAAE,CAAC,KAAK,CAAC,CAAC;aACpB;YAED,GAAG,IAAI,QAAQ,CAAC;YAChB,OAAO,EAAE,CAAC,KAAK,CAAC,CAAC;SACpB;QAED,OAAO,GAAG,CAAC;IACf,CAAC;CACJ"}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
export declare class LlamaModel {
|
|
2
|
+
private readonly _model;
|
|
3
|
+
private readonly _ctx;
|
|
4
|
+
private _prependBos;
|
|
5
|
+
constructor({ modelPath, prependBos }: {
|
|
6
|
+
modelPath: string;
|
|
7
|
+
prependBos?: boolean;
|
|
8
|
+
});
|
|
9
|
+
get systemInfo(): string;
|
|
10
|
+
encode(text: string): Uint32Array;
|
|
11
|
+
decode(tokens: Uint32Array): string;
|
|
12
|
+
evaluate(tokens: Uint32Array, getRestrictions?: () => Uint32Array): AsyncGenerator<number, void, unknown>;
|
|
13
|
+
}
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
import { loadBin } from "./utils/getBin.js";
|
|
2
|
+
const llamaCppNode = await loadBin();
|
|
3
|
+
const { LLAMAModel, LLAMAContext } = llamaCppNode;
|
|
4
|
+
export class LlamaModel {
|
|
5
|
+
_model;
|
|
6
|
+
_ctx;
|
|
7
|
+
_prependBos;
|
|
8
|
+
constructor({ modelPath, prependBos = true }) {
|
|
9
|
+
this._model = new LLAMAModel(modelPath);
|
|
10
|
+
this._ctx = new LLAMAContext(this._model);
|
|
11
|
+
this._prependBos = prependBos;
|
|
12
|
+
}
|
|
13
|
+
get systemInfo() {
|
|
14
|
+
return llamaCppNode.systemInfo();
|
|
15
|
+
}
|
|
16
|
+
encode(text) {
|
|
17
|
+
return this._ctx.encode(text);
|
|
18
|
+
}
|
|
19
|
+
decode(tokens) {
|
|
20
|
+
return this._ctx.decode(tokens);
|
|
21
|
+
}
|
|
22
|
+
async *evaluate(tokens, getRestrictions) {
|
|
23
|
+
let evalTokens = tokens;
|
|
24
|
+
if (this._prependBos) {
|
|
25
|
+
const tokensArray = Array.from(tokens);
|
|
26
|
+
tokensArray.unshift(llamaCppNode.tokenBos());
|
|
27
|
+
evalTokens = Uint32Array.from(tokensArray);
|
|
28
|
+
this._prependBos = false;
|
|
29
|
+
}
|
|
30
|
+
// eslint-disable-next-line no-constant-condition
|
|
31
|
+
while (true) {
|
|
32
|
+
// Evaluate to get the next token.
|
|
33
|
+
const nextToken = await this._ctx.eval(evalTokens, getRestrictions?.());
|
|
34
|
+
// the assistant finished answering
|
|
35
|
+
if (nextToken === llamaCppNode.tokenEos())
|
|
36
|
+
break;
|
|
37
|
+
yield nextToken;
|
|
38
|
+
// Create tokens for the next eval.
|
|
39
|
+
evalTokens = Uint32Array.from([nextToken]);
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
//# sourceMappingURL=LlamaModel.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"LlamaModel.js","sourceRoot":"","sources":["../src/LlamaModel.ts"],"names":[],"mappings":"AAAA,OAAO,EAAC,OAAO,EAAqC,MAAM,mBAAmB,CAAC;AAE9E,MAAM,YAAY,GAAG,MAAM,OAAO,EAAE,CAAC;AACrC,MAAM,EAAC,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Y,CAAC,QAAQ,EAAE;gBACrC,MAAM;YAEV,MAAM,SAAS,CAAC;YAEhB,mCAAmC;YACnC,UAAU,GAAG,WAAW,CAAC,IAAI,CAAC,CAAC,SAAS,CAAC,CAAC,CAAC;SAC9C;IACL,CAAC;CACJ"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"EmptyChatPromptWrapper.js","sourceRoot":"","sources":["../../src/chatWrappers/EmptyChatPromptWrapper.ts"],"names":[],"mappings":"AAAA,OAAO,EAAC,iBAAiB,EAAC,MAAM,yBAAyB,CAAC;AAE1D,MAAM,OAAO,sBAAuB,SAAQ,iBAAiB;CAAG"}
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import { ChatPromptWrapper } from "../ChatPromptWrapper.js";
|
|
2
|
+
export declare class LlamaChatPromptWrapper extends ChatPromptWrapper {
|
|
3
|
+
wrapPrompt(prompt: string, { systemPrompt, promptIndex }: {
|
|
4
|
+
systemPrompt: string;
|
|
5
|
+
promptIndex: number;
|
|
6
|
+
}): string;
|
|
7
|
+
getStopStrings(): string[];
|
|
8
|
+
}
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import { ChatPromptWrapper } from "../ChatPromptWrapper.js";
|
|
2
|
+
// source: https://huggingface.co/blog/llama2#how-to-prompt-llama-2
|
|
3
|
+
export class LlamaChatPromptWrapper extends ChatPromptWrapper {
|
|
4
|
+
wrapPrompt(prompt, { systemPrompt, promptIndex }) {
|
|
5
|
+
if (promptIndex === 0) {
|
|
6
|
+
return "<s>[INST] <<SYS>>\n" + systemPrompt + "\n<</SYS>>\n\n" + prompt + " [/INST]\n\n";
|
|
7
|
+
}
|
|
8
|
+
else {
|
|
9
|
+
return "<s>[INST] " + prompt + " [/INST]\n\n";
|
|
10
|
+
}
|
|
11
|
+
}
|
|
12
|
+
getStopStrings() {
|
|
13
|
+
return ["</s><s>[INST]"];
|
|
14
|
+
}
|
|
15
|
+
}
|
|
16
|
+
//# sourceMappingURL=LlamaChatPromptWrapper.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"LlamaChatPromptWrapper.js","sourceRoot":"","sources":["../../src/chatWrappers/LlamaChatPromptWrapper.ts"],"names":[],"mappings":"AAAA,OAAO,EAAC,iBAAiB,EAAC,MAAM,yBAAyB,CAAC;AAE1D,mEAAmE;AACnE,MAAM,OAAO,sBAAuB,SAAQ,iBAAiB;IACzC,UAAU,CAAC,MAAc,EAAE,EAAC,YAAY,EAAE,WAAW,EAA8C;QAC/G,IAAI,WAAW,KAAK,CAAC,EAAE;YACnB,OAAO,qBAAqB,GAAG,YAAY,GAAG,gBAAgB,GAAG,MAAM,GAAG,cAAc,CAAC;SAC5F;aAAM;YACH,OAAO,YAAY,GAAG,MAAM,GAAG,cAAc,CAAC;SACjD;IACL,CAAC;IAEe,cAAc;QAC1B,OAAO,CAAC,eAAe,CAAC,CAAC;IAC7B,CAAC;CACJ"}
|
package/dist/cli/cli.js
ADDED
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
import { fileURLToPath } from "url";
|
|
3
|
+
import path from "path";
|
|
4
|
+
import yargs from "yargs";
|
|
5
|
+
// eslint-disable-next-line node/file-extension-in-import
|
|
6
|
+
import { hideBin } from "yargs/helpers";
|
|
7
|
+
import fs from "fs-extra";
|
|
8
|
+
import { DownloadCommand } from "./commands/DownloadCommand.js";
|
|
9
|
+
import { BuildCommand } from "./commands/BuildCommand.js";
|
|
10
|
+
import { OnPostInstallCommand } from "./commands/OnPostInstallCommand.js";
|
|
11
|
+
import { ClearCommand } from "./commands/ClearCommand.js";
|
|
12
|
+
import { ChatCommand } from "./commands/ChatCommand.js";
|
|
13
|
+
const __dirname = path.dirname(fileURLToPath(import.meta.url));
|
|
14
|
+
const packageJson = fs.readJSONSync(path.join(__dirname, "..", "..", "package.json"));
|
|
15
|
+
const yarg = yargs(hideBin(process.argv));
|
|
16
|
+
yarg
|
|
17
|
+
.usage("Usage: $0 <command> [options]")
|
|
18
|
+
.command(DownloadCommand)
|
|
19
|
+
.command(BuildCommand)
|
|
20
|
+
.command(ClearCommand)
|
|
21
|
+
.command(ChatCommand)
|
|
22
|
+
.command(OnPostInstallCommand)
|
|
23
|
+
.recommendCommands()
|
|
24
|
+
.demandCommand(1)
|
|
25
|
+
.strict()
|
|
26
|
+
.strictCommands()
|
|
27
|
+
.alias("v", "version")
|
|
28
|
+
.help("h")
|
|
29
|
+
.alias("h", "help")
|
|
30
|
+
.version(packageJson.version)
|
|
31
|
+
.wrap(Math.min(100, yarg.terminalWidth()))
|
|
32
|
+
.parse();
|
|
33
|
+
//# sourceMappingURL=cli.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"cli.js","sourceRoot":"","sources":["../../src/cli/cli.ts"],"names":[],"mappings":";AAEA,OAAO,EAAC,aAAa,EAAC,MAAM,KAAK,CAAC;AAClC,OAAO,IAAI,MAAM,MAAM,CAAC;AACxB,OAAO,KAAK,MAAM,OAAO,CAAC;AAC1B,yDAAyD;AACzD,OAAO,EAAC,OAAO,EAAC,MAAM,eAAe,CAAC;AACtC,OAAO,EAAE,MAAM,UAAU,CAAC;AAC1B,OAAO,EAAC,eAAe,EAAC,MAAM,+BAA+B,CAAC;AAC9D,OAAO,EAAC,YAAY,EAAC,MAAM,4BAA4B,CAAC;AACxD,OAAO,EAAC,oBAAoB,EAAC,MAAM,oCAAoC,CAAC;AACxE,OAAO,EAAC,YAAY,EAAC,MAAM,4BAA4B,CAAC;AACxD,OAAO,EAAC,WAAW,EAAC,MAAM,2BAA2B,CAAC;AAEtD,MAAM,SAAS,GAAG,IAAI,CAAC,OAAO,CAAC,aAAa,CAAC,MAAM,CAAC,IAAI,CAAC,GAAG,CAAC,CAAC,CAAC;AAE/D,MAAM,WAAW,GAAG,EAAE,CAAC,YAAY,CAAC,IAAI,CAAC,IAAI,CAAC,SAAS,EAAE,IAAI,EAAE,IAAI,EAAE,cAAc,CAAC,CAAC,CAAC;AAEtF,MAAM,IAAI,GAAG,KAAK,CAAC,OAAO,CAAC,OAAO,CAAC,IAAI,CAAC,CAAC,CAAC;AAE1C,IAAI;KACC,KAAK,CAAC,+BAA+B,CAAC;KACtC,OAAO,CAAC,eAAe,CAAC;KACxB,OAAO,CAAC,YAAY,CAAC;KACrB,OAAO,CAAC,YAAY,CAAC;KACrB,OAAO,CAAC,WAAW,CAAC;KACpB,OAAO,CAAC,oBAAoB,CAAC;KAC7B,iBAAiB,EAAE;KACnB,aAAa,CAAC,CAAC,CAAC;KAChB,MAAM,EAAE;KACR,cAAc,EAAE;KAChB,KAAK,CAAC,GAAG,EAAE,SAAS,CAAC;KACrB,IAAI,CAAC,GAAG,CAAC;KACT,KAAK,CAAC,GAAG,EAAE,MAAM,CAAC;KAClB,OAAO,CAAC,WAAW,CAAC,OAAO,CAAC;KAC5B,IAAI,CAAC,IAAI,CAAC,GAAG,CAAC,GAAG,EAAE,IAAI,CAAC,aAAa,EAAE,CAAC,CAAC;KACzC,KAAK,EAAE,CAAC"}
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import { CommandModule } from "yargs";
|
|
2
|
+
type BuildCommand = {
|
|
3
|
+
arch?: string;
|
|
4
|
+
nodeTarget?: string;
|
|
5
|
+
};
|
|
6
|
+
export declare const BuildCommand: CommandModule<object, BuildCommand>;
|
|
7
|
+
export declare function BuildLlamaCppCommand({ arch, nodeTarget }: BuildCommand): Promise<void>;
|
|
8
|
+
export {};
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
import chalk from "chalk";
|
|
2
|
+
import { compileLlamaCpp } from "../../utils/compileLLamaCpp.js";
|
|
3
|
+
import withOra from "../../utils/withOra.js";
|
|
4
|
+
import { clearTempFolder } from "../../utils/clearTempFolder.js";
|
|
5
|
+
export const BuildCommand = {
|
|
6
|
+
command: "build",
|
|
7
|
+
describe: "Compile the currently downloaded llama.cpp",
|
|
8
|
+
builder(yargs) {
|
|
9
|
+
return yargs
|
|
10
|
+
.option("arch", {
|
|
11
|
+
type: "string",
|
|
12
|
+
description: "The architecture to compile llama.cpp for"
|
|
13
|
+
})
|
|
14
|
+
.option("nodeTarget", {
|
|
15
|
+
type: "string",
|
|
16
|
+
description: "The Node.js version to compile llama.cpp for. Example: v18.0.0"
|
|
17
|
+
});
|
|
18
|
+
},
|
|
19
|
+
handler: BuildLlamaCppCommand
|
|
20
|
+
};
|
|
21
|
+
export async function BuildLlamaCppCommand({ arch, nodeTarget }) {
|
|
22
|
+
await withOra({
|
|
23
|
+
loading: chalk.blue("Compiling llama.cpp"),
|
|
24
|
+
success: chalk.blue("Compiled llama.cpp"),
|
|
25
|
+
fail: chalk.blue("Failed to compile llama.cpp")
|
|
26
|
+
}, async () => {
|
|
27
|
+
await compileLlamaCpp({
|
|
28
|
+
arch: arch ? arch : undefined,
|
|
29
|
+
nodeTarget: nodeTarget ? nodeTarget : undefined,
|
|
30
|
+
setUsedBingFlag: true
|
|
31
|
+
});
|
|
32
|
+
});
|
|
33
|
+
await withOra({
|
|
34
|
+
loading: chalk.blue("Removing temporary files"),
|
|
35
|
+
success: chalk.blue("Removed temporary files"),
|
|
36
|
+
fail: chalk.blue("Failed to remove temporary files")
|
|
37
|
+
}, async () => {
|
|
38
|
+
await clearTempFolder();
|
|
39
|
+
});
|
|
40
|
+
}
|
|
41
|
+
//# sourceMappingURL=BuildCommand.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"BuildCommand.js","sourceRoot":"","sources":["../../../src/cli/commands/BuildCommand.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,MAAM,OAAO,CAAC;AAC1B,OAAO,EAAC,eAAe,EAAC,MAAM,gCAAgC,CAAC;AAC/D,OAAO,OAAO,MAAM,wBAAwB,CAAC;AAC7C,OAAO,EAAC,eAAe,EAAC,MAAM,gCAAgC,CAAC;AAO/D,MAAM,CAAC,MAAM,YAAY,GAAwC;IAC7D,OAAO,EAAE,OAAO;IAChB,QAAQ,EAAE,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