-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathhack_gemini.js
72 lines (56 loc) · 2.12 KB
/
hack_gemini.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
import { config } from 'dotenv';
//https://www.npmjs.com/package/dotenv/v/8.2.0
//Multiple .env files: https://stackoverflow.com/questions/55406055/toggle-between-multiple-env-files-like-env-development-with-node-js
import { GoogleGenerativeAI } from "@google/generative-ai"
import readline from 'readline'
config();
// ACTIVITY 1
// Use readline to read data from a Readable stream
// Create a userinterface and specify the inputs and outputs
const userInterface = readline.createInterface({
input: process.stdin,
output: process.stdout});
userInterface.setPrompt('OHAI> ');
console.log(process.env.GEMINI_API_KEY);
const genAI = new GoogleGenerativeAI(process.env.GEMINI_API_KEY);
// rl.prompt();
// rl.on('line', function(line) {
// switch(line.trim()){
// case 'hello':
// console.log('world');
// break;
// default:
// console.log('Say what? I might have heard `' + line.trim() + '` ');
// break;
// }
// rl.prompt();
// }).on('close', function() {
// console.log('Have a great day!');
// process.exit(0);
// });
async function handleInput(input) {
try {
// ACTIVITY 2
// initialize the gemini model as "model" and use the model to generate Content
const model = genAI.getGenerativeModel({ model: "gemini-pro" });
// Generate content based on the user's input
// save the model's response in the variable "response"
// print the model's reponse
const result = await model.generateContent(input);
const res = await result.response;
// Activity 1
// re prompt the interface to get the next input from the user
// hint: you might want to use .prompt()
// hint 2: you will also have to prompt outside of this function to initialize chat
console.log(res.text());
userInterface.prompt();
} catch (error) {
console.error('Error processing your input:', error);
}
}
userInterface.prompt();
// ACTIVITY 1
userInterface.on("line", (input) => {
// use the handle input function you wrote here
handleInput(input);
});