const readline = require("readline");
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout,
});
console.log("ChatGPT 터미널 챗 앱\n");
rl.prompt();
rl.on("line", (question) => {
fetch("https://api.openai.com/v1/chat/completions", {
method: "POST",
headers: {
Authorization: `Bearer ${process.env.OPENAI_API_KEY}`,
"Content-Type": "application/json",
},
body: JSON.stringify({
model: "gpt-3.5-turbo",
messages: [{role: "user", content: question}],
}),
})
.then((res) => res.json())
.then((data) => data.choices[0].message.content)
.then((answer) => {
console.log(`${answer}\n`);
rl.prompt();
});
});