-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathindex.js
77 lines (61 loc) · 2.16 KB
/
index.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
const run = async () => {
const { GoogleGenerativeAI } = require("@google/generative-ai");
const fs = require("fs");
const { default: chalk } = await import("chalk");
const MODEL_NAME = "gemini-1.0-pro";
const API_KEY = "YOUR_API_KEY"; // https://aistudio.google.com/app/
const genAI = new GoogleGenerativeAI(API_KEY);
const model = genAI.getGenerativeModel({ model: MODEL_NAME });
let history = [];
if (fs.existsSync("history.json")) {
const historyData = fs.readFileSync("history.json");
history = JSON.parse(historyData);
}
console.log("Chat started. You can start typing.");
const chat = model.startChat({
history: history
});
process.stdin.setEncoding("utf-8");
const loadingBar = () => {
const frames = ['|', '/', '-', '\\'];
let i = 0;
return setInterval(() => {
process.stdout.write("\r" + chalk.hex("#FF6161")("Bot: " + frames[i++ % frames.length]) + " Generating...");
}, 250);
};
const promptUser = () => {
process.stdout.write(chalk.hex("#3776FF")("You: "));
};
promptUser();
process.stdin.on("data", async (input) => {
input = input.trim();
if (input.toLowerCase() === "exit") {
console.log("Exiting chat.");
if (fs.existsSync("history.json")) {
fs.unlinkSync("history.json");
console.log("History deleted.");
}
process.exit();
}
const loader = loadingBar();
process.stdin.pause();
await chat.sendMessage(input).then(async (x) => {
clearInterval(loader);
console.log("\r" + chalk.hex("#FF6161")("Bot:"), x.response.text());
history.push(
{
role: "user",
parts: [{ text: input }]
},
{
role: "model",
parts: [{ text: x.response.text() }]
}
);
fs.writeFileSync("history.json", JSON.stringify(history));
process.stdin.resume();
promptUser();
});
});
}
run();