-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgenerator.js
More file actions
74 lines (62 loc) · 1.99 KB
/
generator.js
File metadata and controls
74 lines (62 loc) · 1.99 KB
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
import OpenAI from 'openai';
const openai = new OpenAI({
apiKey: import.meta.env.VITE_OPENAI_API_KEY,
dangerouslyAllowBrowser: true,
});
function generateQuestionsJSON(questionsCount) {
const questions = [];
for (let i = 1; i <= questionsCount; i++) {
const question = {
number: i,
question: '[Enter the question related to the topic]',
options: [
'1: [Input and expected output]',
'2: [Input and expected output]',
'3: [Input and expected output]',
'4: [Input and expected output]',
],
answer: '[Provide the correct answer to the question]',
};
questions.push(question);
}
return questions;
}
function generateFAQsJSON(faqsCount) {
const faqs = {};
for (let i = 1; i <= faqsCount; i++) {
faqs[i.toString()] = {
question: '[Enter a frequently asked question related to the topic]',
answer: '[Provide the answer to the FAQ]',
};
}
return faqs;
}
export async function generateQuestions(
userInput = 'frontend',
questionsCount = 1,
faqsCount = 1
) {
try {
const dynamicQuestions = generateQuestionsJSON(questionsCount);
const dynamicFAQs = generateFAQsJSON(faqsCount);
const basePrompt = `
{
"topics": "[List of topics]",
"difficulty": "[Specify the difficulty level]",
"comment": "For each topic, provide the following:",
"questions": ${JSON.stringify(dynamicQuestions, null, 2)},
"faqs": ${JSON.stringify(dynamicFAQs, null, 2)},
"comment": "[Repeat the above sections for each topic, give me ${questionsCount} questions and ${faqsCount} FAQ's, questions can have code and in the same JSON format, DO NOT UPDATE THE JSON KEYS, ONLY UPDATE THE VALUES]"
}`;
const chatCompletion = await openai.chat.completions.create({
messages: [
{ role: 'system', content: basePrompt },
{ role: 'user', content: `User Input: ${userInput}` },
],
model: 'gpt-3.5-turbo',
});
return chatCompletion.choices[0].message.content;
} catch (error) {
console.error('Error:', error);
}
}