Skip to content

Commit 9438d1b

Browse files
add code execution section
1 parent 5efee4e commit 9438d1b

File tree

1 file changed

+66
-0
lines changed

1 file changed

+66
-0
lines changed

code_execution.js

+66
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
import { GoogleGenerativeAI } from '@google/generative-ai';
2+
import readline from 'readline';
3+
4+
import dotenv from 'dotenv';
5+
dotenv.config();
6+
7+
const genAI = new GoogleGenerativeAI(process.env.API_KEY);
8+
const model = genAI.getGenerativeModel({
9+
model: 'gemini-1.5-pro',
10+
tools: [
11+
{
12+
codeExecution: {},
13+
},
14+
],
15+
});
16+
17+
const rl = readline.createInterface({
18+
input: process.stdin,
19+
output: process.stdout,
20+
});
21+
22+
async function askQuestion(query) {
23+
return new Promise((resolve) => {
24+
rl.question(query, (answer) => {
25+
resolve(answer);
26+
});
27+
});
28+
}
29+
30+
async function main() {
31+
while (true) {
32+
try {
33+
const userQuestion1 = await askQuestion(
34+
"Enter your condition (or type 'exit' to quit): "
35+
);
36+
37+
if (userQuestion1.toLowerCase() === 'exit') {
38+
console.log('Exiting the program. Goodbye!');
39+
rl.close();
40+
break;
41+
}
42+
let userQuestion2;
43+
if (userQuestion1 !== '') {
44+
userQuestion2 = await askQuestion(
45+
"Enter your question (or type 'exit' to quit): "
46+
);
47+
if (userQuestion2.toLowerCase() === 'exit') {
48+
console.log('Exiting the program. Goodbye!');
49+
rl.close();
50+
break;
51+
}
52+
}
53+
54+
if (userQuestion1 !== '' && userQuestion2 !== '') {
55+
const result = await model.generateContent(
56+
userQuestion1 + userQuestion2
57+
);
58+
const response = result.response;
59+
}
60+
} catch (error) {
61+
console.error('An error occurred:', error);
62+
}
63+
}
64+
}
65+
66+
main();

0 commit comments

Comments
 (0)