-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
executable file
·85 lines (72 loc) · 2.2 KB
/
app.js
File metadata and controls
executable file
·85 lines (72 loc) · 2.2 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
/*
* Install the Generative AI SDK
*
* $ npm install @google/generative-ai
*
* See the getting started guide for more information
* https://ai.google.dev/gemini-api/docs/get-started/node
*/
import {
GoogleGenerativeAI,
HarmCategory,
HarmBlockThreshold,
} from "@google/generative-ai";
import dotenv from "dotenv";
import readLineSync from "readline-sync";
import colors from "colors";
dotenv.config();
const apiKey = process.env.GEMINI_API_KEY;
const genAI = new GoogleGenerativeAI(apiKey);
const model = genAI.getGenerativeModel({
model: "gemini-1.5-flash",
});
const generationConfig = {
temperature: 1,
topP: 0.95,
topK: 64,
maxOutputTokens: 8192,
responseMimeType: "text/plain",
};
// async function run() {
// const chatSession = model.startChat({
// generationConfig,
// // safetySettings: Adjust safety settings
// // See https://ai.google.dev/gemini-api/docs/safety-settings
// history: [],
// });
// const result = await chatSession.sendMessage("Who won copa america 2020?");
// console.log(result.response.text());
// }
async function main() {
console.log(colors.bold.cyan("Welcome to the Chatbot Program!"));
console.log(colors.bold.cyan("You can start chatting with the bot."));
const chatHistory = [];
while (true) {
const userInput = readLineSync.question(colors.magenta("You: "));
try {
// Constructing text by iterating history
const texts = chatHistory.map(([role, parts]) => ({
role,
parts: parts.map(part => ({text: part}))
}))
texts.push({role:'user', parts:[{text:userInput}]})
const chatSession = model.startChat({
generationConfig,
history: texts,
});
const result = await chatSession.sendMessage(userInput);
const completionText = result.response.text();
if (userInput.toLowerCase() === "exit") {
console.log(colors.yellow("Bot: ") + result.response.text());
return;
}
console.log(colors.yellow("Bot: ") + result.response.text());
chatHistory.push(["user", [userInput]]);
chatHistory.push(["model", [completionText]]);
} catch (error) {
console.error(colors.red(error));
}
}
}
// run();
main();