-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
134 lines (114 loc) Β· 3.56 KB
/
index.js
File metadata and controls
134 lines (114 loc) Β· 3.56 KB
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
#!/usr/bin/env node
import ora from "ora";
import chalk from "chalk";
import figlet from "figlet";
import inquirer from "inquirer";
import { StopWatch } from "stopwatch-node";
import { pipeline } from "@huggingface/transformers";
console.clear();
console.log(
chalk.blue(figlet.textSync("AI CLI", { horizontalLayout: "default" }))
);
const spinner = ora("π Loading model (downloads on first run)...").start();
let generator;
try {
generator = await pipeline(
"text-generation",
"HuggingFaceTB/SmolLM2-135M-Instruct",
{
dtype: "fp32",
}
);
spinner.succeed("β
Model loaded and ready!");
} catch (err) {
spinner.fail("β Failed to load model");
console.error(err);
process.exit(1);
}
const TOKEN_LIMIT = 1024;
console.log(
"\n" +
chalk.bgBlue.white.bold(" π Welcome to CLI AI! ") +
"\n" +
chalk.gray("ββββββββββββββββββββββββββββββββββββββββββββ") +
"\n" +
chalk.white(" A lightweight, blazing-fast, private AI chatbot") +
"\n" +
chalk.white(" powered by ") +
chalk.bgMagenta.white.bold(" SmolLM2-135M-Instruct ") +
"\n\n" +
chalk.yellow(" Please use correct grammar and avoid typos.") +
"\n\n" +
chalk.white(" Model downloads only once, usually within") +
"\n" +
chalk.green(" a few minutes") +
chalk.white(" based on your internet speed.") +
"\n\n" +
chalk.white(" Runs on low-end devices. No GPU needed.") +
"\n" +
chalk.cyan(" Fully private. No API or internet needed after setup.") +
"\n\n" +
chalk.white(" Unlimited requests") +
chalk.white(" | ") +
chalk.white("Token Limit: ") +
chalk.bgBlue.white.bold(` ${TOKEN_LIMIT} `) +
"\n" +
chalk.white(" Temp: ") +
chalk.bold("0.1") +
chalk.white(" | Top_p: ") +
chalk.bold("0.95") +
"\n" +
chalk.white(" Type ") +
chalk.bgCyan.black.bold(" exit ") +
chalk.white(" to quit.") +
"\n" +
chalk.gray("ββββββββββββββββββββββββββββββββββββββββββββ\n") +
"\n"
);
console.log(
chalk.bgYellow.black(" β οΈ DISCLAIMER ") +
chalk.yellow(
" This is a lightweight local AI model (135M). It may give incorrect answers for complex or vague questions. Please ask clear, specific prompts for best results."
)
);
async function chat() {
while (true) {
const { prompt } = await inquirer.prompt([
{
type: "input",
name: "prompt",
message: chalk.green("You:"),
},
]);
if (prompt.trim().toLowerCase() === "exit") {
console.log(chalk.cyan("π Exiting..."));
break;
}
const formattedPrompt = `### Instruction:\n${prompt}\n\n### Response:\n`;
const sw = new StopWatch("sw");
sw.start("generating response");
const spinnerRes = ora("Generating response....").start();
const response = await generator(formattedPrompt, {
max_new_tokens: TOKEN_LIMIT,
temperature: 0.1,
top_p: 0.95,
repetition_penalty: 1.2,
top_k: 50,
});
spinnerRes.stop();
sw.stop();
const answer = response[0].generated_text
.replace(formattedPrompt, "")
.trim();
console.log(chalk.yellowBright("AI:"), answer);
console.log("\n");
const responseTime =
Math.round(sw.getTask("generating response").timeMills * 0.1) / 100;
console.log(
chalk.gray("Response generated in ") +
chalk.bold.green(`${responseTime} seconds`)
);
console.log("\n");
}
}
chat();