-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathConsoleGPT.mjs
84 lines (73 loc) · 2.63 KB
/
ConsoleGPT.mjs
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
import { OpenAI } from 'openai';
import readline from 'readline';
import dotenv from 'dotenv';
dotenv.config();
const openai = new OpenAI({
apiKey: process.env.GPT_API_KEY,
dangerouslyAllowBrowser: true
});
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout
});
const askQuestion = async () => {
console.log("Choose an option:");
console.log("1. Answer your query");
console.log("2. Draw an image based on a prompt");
console.log("3. Exit");
const userChoice = await new Promise(resolve => rl.question("Your choice: ", resolve));
if (userChoice === '1') {
await chatbot();
} else if (userChoice === '2') {
await imageGenerator();
} else if (userChoice === '3') {
console.log("Goodbye!");
rl.close();
} else {
console.log("Invalid choice. Please choose again.");
askQuestion();
}
};
const chatbot = async () => {
console.log("Welcome! How can I assist you today?");
const userResponse = await new Promise(resolve => rl.question("You: ", resolve));
try {
const response = await openai.completions.create({
model: 'gpt-3.5-turbo-instruct',
prompt: `User: ${userResponse}\nAI:`,
max_tokens: 150
});
console.log(`Bot: ${response.choices[0].text}`);
const continueResponse = await new Promise(resolve => rl.question("Do you want to explore the bot further? (Y/N): ", resolve));
if (continueResponse.toLowerCase() === 'n') {
console.log("Thank you for using our service. Goodbye!");
rl.close();
return;
}
} catch (error) {
console.error('An error occurred:', error);
}
askQuestion();
};
const imageGenerator = async () => {
console.log("Describe the image you want to be generated:");
const userPrompt = await new Promise(resolve => rl.question("You: ", resolve));
try {
const response = await openai.files.create({
file: userPrompt,
purpose: "generate an image based on the description provided by the user"
});
console.log(`Here's an image link to "${userPrompt}"\n`);
console.log(response.data.url);
const continueResponse = await new Promise(resolve => rl.question("Do you want to explore the bot further? (Y/N): ", resolve));
if (continueResponse.toLowerCase() === 'n') {
console.log("Thank you for using our service. Goodbye!");
rl.close();
return;
}
} catch (error) {
console.error('An error occurred:', error);
}
askQuestion();
};
askQuestion();