-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathcompletion.js
executable file
·191 lines (173 loc) · 6.31 KB
/
completion.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
#! /usr/bin/env node
//
// Copyright 2024 Picovoice Inc.
//
// You may not use this file except in compliance with the license. A copy of the license is located in the "LICENSE"
// file accompanying this source.
//
// Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
// an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
// specific language governing permissions and limitations under the License.
//
"use strict";
const { program } = require("commander");
const readline = require("readline");
const { PicoLLM } = require("@picovoice/picollm-node");
program
.option(
"--access_key <string>",
"AccessKey obtain from the Picovoice Console (https://console.picovoice.ai/)."
)
.option(
"--library_path <string>",
"Absolute path to picollm dynamic library."
)
.option("--model_path <string>", "Absolute path to picollm model")
.option("--prompt <string>", "Prompt string.")
.option(
"--device <string>",
"String representation of the device (e.g., CPU or GPU) to use for inference. If set to `best`, picoLLM " +
"picks the most suitable device. If set to `gpu`, the engine uses the first available GPU device. To " +
"select a specific GPU device, set this argument to `gpu:${GPU_INDEX}`, where `${GPU_INDEX}` is the index " +
"of the target GPU. If set to `cpu`, the engine will run on the CPU with the default number of threads. " +
"To specify the number of threads, set this argument to `cpu:${NUM_THREADS}`, where `${NUM_THREADS}` is " +
"the desired number of threads.")
.option(
"--completion_token_limit <number>",
"Maximum number of tokens in the completion. Set to `undefined` to impose no limit.",
Number,
128)
.option(
"--stop_phrases <string>",
"The generation process stops when it encounters any of these phrases in the completion. The already " +
"generated completion, including the encountered stop phrase, will be returned.")
.option(
"--seed <number>",
"The internal random number generator uses it as its seed if set to a positive integer value. Seeding " +
"enforces deterministic outputs. Set to `None` for randomized responses.",
Number)
.option(
"--presence_penalty <number>",
"It penalizes logits already appearing in the partial completion if set to a positive value. If set to " +
"`0.0`, it has no effect.",
Number,
0)
.option(
"--frequency_penalty <number>",
"If set to a positive floating-point value, it penalizes logits proportional to the frequency of their " +
"appearance in the partial completion. If set to `0.0`, it has no effect.",
Number,
0)
.option(
"--temperature <number>",
"Sampling temperature. Temperature is a non-negative floating-point value that controls the randomness of " +
"the sampler. A higher temperature smoothens the samplers' output, increasing the randomness. In " +
"contrast, a lower temperature creates a narrower distribution and reduces variability. Setting it to " +
"`0` selects the maximum logit during sampling.",
Number,
0)
.option(
"--top_p <number>",
"A positive floating-point number within (0, 1]. It restricts the sampler's choices to high-probability " +
"logits that form the `top_p` portion of the probability mass. Hence, it avoids randomly selecting " +
"unlikely logits. A value of `1.` enables the sampler to pick any token with non-zero probability, " +
"turning off the feature.",
Number,
1)
.option(
"--num_top_choices <number>",
"If set to a positive value, picoLLM returns the list of the highest probability tokens for any generated " +
"token. Set to `0` to turn off the feature.",
Number,
0)
.option(
"--show_available_devices",
"Show the list of available devices for LLM inference.",
false)
.option("--verbose", "Enable verbose logging.");
if (process.argv.length < 1) {
program.help();
}
program.parse(process.argv);
async function completionDemo() {
const accessKey = program["access_key"];
const libraryPath = program["library_path"];
const modelPath = program["model_path"];
const prompt = program["prompt"];
const device = program["device"];
const completionTokenLimit = program["completion_token_limit"];
const stopPhrases = program["stop_phrases"];
const seed = program["seed"];
const presencePenalty = program["presence_penalty"];
const frequencyPenalty = program["frequency_penalty"];
const temperature = program["temperature"];
const topP = program["top_p"];
const numTopChoices = program["num_top_choices"];
const showAvailableDevices = program["show_available_devices"];
const verbose = program["verbose"];
if (showAvailableDevices) {
console.log(PicoLLM.listAvailableDevices().join('\n'));
process.exit();
}
if (accessKey === undefined) {
console.error("No AccessKey provided");
process.exit();
}
const picoLLM = new PicoLLM(
accessKey,
modelPath,
{
libraryPath: libraryPath,
device: device
}
);
console.log(`picoLLM '${picoLLM.version}'`);
console.log(`Loaded '${picoLLM.model}'`);
console.log(">>> Press `Enter` to exit: ");
readline.emitKeypressEvents(process.stdin);
if (process.stdin.isTTY) {
process.stdin.setRawMode(true);
}
process.stdin.on("keypress", (key, str) => {
if (str.sequence === '\r') {
picoLLM.interrupt();
} else if (str.sequence === '\x03') {
picoLLM.interrupt();
}
});
const startSec = [0];
const streamCallback = (token) => {
if (startSec[0] === 0) {
startSec[0] = performance.now();
}
process.stdout.write(token);
};
try {
const res = await picoLLM.generate(
prompt,
{
completionTokenLimit,
stopPhrases,
seed,
presencePenalty,
frequencyPenalty,
temperature,
topP,
numTopChoices,
streamCallback,
}
);
console.log();
if (verbose) {
console.log(res.completionTokens);
}
const tos = (res.usage.completionTokens - 1) / ((performance.now() - startSec[0]) / 1000);
console.log(`Generated ${Math.round(tos * 100) / 100} tokens per second`)
} catch (e) {
console.error(e);
} finally {
picoLLM.release();
process.exit();
}
}
completionDemo();