-
Notifications
You must be signed in to change notification settings - Fork 1
/
chat-cli.js
56 lines (49 loc) · 1.29 KB
/
chat-cli.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
import readline from 'node:readline'
import chalk from 'chalk'
import { ModelServer } from '#package/index.js'
// A command-line chat example using the ModelServer.
const modelServer = new ModelServer({
// log: 'info',
models: {
'my-model': {
task: 'text-completion',
minInstances: 1,
url: 'https://huggingface.co/HuggingFaceTB/smollm-135M-instruct-v0.2-Q8_0-GGUF/blob/main/smollm-135m-instruct-add-basics-q8_0.gguf',
sha256: 'a98d3857b95b96c156d954780d28f39dcb35b642e72892ee08ddff70719e6220',
engine: 'node-llama-cpp',
// device: { gpu: false },
},
},
})
console.log('Initializing models...')
await modelServer.start()
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout,
})
const messages = []
while (true) {
const input = await new Promise((resolve) => {
rl.question(chalk.bold(chalk.dim('user > ')), (input) => {
resolve(input)
})
})
messages.push({
role: 'user',
content: input,
})
process.stdout.write(chalk.bold(chalk.dim('model > ')))
const result = await modelServer.processChatCompletionTask(
{
model: 'my-model',
messages,
},
{
onChunk: (chunk) => {
process.stdout.write(chunk.text)
},
},
)
messages.push(result.message)
process.stdout.write(' ' + chalk.dim(`[${result.finishReason}]`) + '\n')
}