-
Notifications
You must be signed in to change notification settings - Fork 0
/
gpt3.js
49 lines (41 loc) · 1.38 KB
/
gpt3.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
const { Configuration, OpenAIApi } = require('openai');
const dotenv = require('dotenv');
dotenv.config();
const configuration = new Configuration({
apiKey: process.env.OPENAI_API_KEY,
});
const openai = new OpenAIApi(configuration);
async function generateQuiz() {
const prompt = `Generate a 10-question multiple-choice geography quiz with 4 answer choices per question. Each question should have only one correct answer. Return in JSON format of questions, choices, and correct answers.`;
const response = await openai.createChatCompletion({
model: "gpt-3.5-turbo",
messages: [{role: "user", content: prompt}]
});
const contentStr = response.data.choices[0].message.content;
const regex = /"question": "([^"]+)",\n\s+"choices": \[\n\s+"([^"]+)",\n\s+"([^"]+)",\n\s+"([^"]+)",\n\s+"([^"]+)"\n\s+\],\n\s+"answer": "([^"]+)"/g;
const quiz = { questions: [] };
let match;
while ((match = regex.exec(contentStr)) !== null) {
const question = match[1];
const choiceA = match[2];
const choiceB = match[3];
const choiceC = match[4];
const choiceD = match[5];
const correctAnswer = match[6];
quiz.questions.push({
question: question,
choices: {
A: choiceA,
B: choiceB,
C: choiceC,
D: choiceD,
},
answer: correctAnswer,
});
}
console.log(quiz)
return quiz;
}
module.exports = {
generateQuiz,
};