-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
89 lines (69 loc) · 2.2 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
78
79
80
81
82
83
84
85
86
87
88
89
const { GoogleGenerativeAI } = require("@google/generative-ai");
const fs = require("fs");
const dotenv = require("dotenv");
dotenv.config();
// Access your API key as an environment variable (see "Set up your API key" above)
const genAI = new GoogleGenerativeAI(process.env.API_KEY);
const readline = require('readline');
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout,
});
function fileToGenerativePart(path, mimeType) {
return {
inlineData: {
data: Buffer.from(fs.readFileSync(path)).toString("base64"),
mimeType
},
};
}
async function problemSolving() {
// For text-and-image input (multimodal), use the gemini-pro-vision model
const model = genAI.getGenerativeModel({ model: "gemini-pro-vision" });
const prompt = "";
const imageParts = [
fileToGenerativePart("prob.jpg", "image/jpeg"),
// fileToGenerativePart("image2.jpeg", "image/jpeg"),
];
const result = await model.generateContent([prompt, ...imageParts]);
const response = await result.response;
const text = response.text();
console.log(text);
}
// problemSolving();
async function textQuery() {
// For text-only input, use the gemini-pro model
const model = genAI.getGenerativeModel({ model: "gemini-pro"});
const prompt = "What is Newton's First Law ?"
const result = await model.generateContent(prompt);
const response = await result.response;
const text = response.text();
console.log(text);
}
// textQuery();
async function chatBot() {
// For text-only input, use the gemini-pro model
const model = genAI.getGenerativeModel({ model: "gemini-pro"});
const chat = model.startChat({
history: [],
generationConfig: {
maxOutputTokens: 100,
},
});
async function askAndRespond(){
rl.question("You: ", async(msg)=>{
if(msg.toLowerCase() === "exit"){
rl.close();
}
else{
const result = await model.generateContent(msg);
const response = await result.response;
const text = await response.text();
console.log("AI: ", text);
askAndRespond();
}
});
}
askAndRespond();
}
chatBot();