-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
130 lines (107 loc) · 3.71 KB
/
server.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
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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
const express = require("express");
const bodyParser = require("body-parser");
const OpenAI = require("openai");
const CORS = require("cors");
require("dotenv").config();
const openai = new OpenAI({
apiKey: process.env.OPENAI_API_KEY,
});
const app = express();
app.use(bodyParser.json());
app.use(CORS());
let conversationLog = []
const quizPrompt = (info) => {
return `File content: ${info.fileContent}.
Based on the file content provided, generate ${info.questionNumber} quiz questions to test my knowledge.
Level of difficulty: ${info.difficulty}.
Return the questions in this JSON format:
{
"questions": [
{
"question": "Question 1 Text",
"options": [
{"id": 1, "text": "Option 1 Text"},
{"id": 2, "text": "Option 2 Text"},
{"id": 3, "text": "Option 3 Text"},
{"id": 4, "text": "Option 4 Text"}
],
"correctOptionId": 3
},
{
"question": "Question 2 Text",
"options": [
{"id": 5, "text": "Option 1 Text"},
{"id": 6, "text": "Option 2 Text"},
{"id": 7, "text": "Option 3 Text"},
{"id": 8, "text": "Option 4 Text"}
],
"correctOptionId": 5
},
{
"question": "Question 3 Text",
"options": [
{"id": 9, "text": "Option 1 Text"},
{"id": 10, "text": "Option 2 Text"},
{"id": 11, "text": "Option 3 Text"},
{"id": 12, "text": "Option 4 Text"}
],
"correctOptionId": 9
}
]
}
Return the JSON Object only. Do Not add other things.`;
};
const reQuizPrompt = (info) => {
return `Please generate another ${info.questionNumber} questions for me.`;
}
app.get("/", (req, res) => {
res.send("This is a GET!");
});
app.post("/quiz", async (req, res) => {
const conversation = quizPrompt(req.body);
conversationLog = [];
conversationLog.push({
role: "user",
content: conversation,
});
try {
const completion = await openai.chat.completions.create({
messages: conversationLog,
model: "gpt-3.5-turbo-1106",
});
conversationLog.push({
role: "assistant",
content: completion.choices[0].message.content,
});
res.send(completion.choices[0].message.content);
} catch (error) {
console.log(error);
}
console.log(conversationLog);
});
app.post("/new-questions-quiz", async (req, res) => {
const conversation = reQuizPrompt(req.body);
conversationLog.push({
role: "user",
content: conversation,
});
console.log(conversationLog);
try {
const completion = await openai.chat.completions.create({
messages: conversationLog,
model: "gpt-3.5-turbo-1106",
});
conversationLog.push({
role: "assistant",
content: completion.choices[0].message.content,
});
res.send(completion.choices[0].message.content);
} catch (error) {
console.log(error);
}
console.log(conversationLog);
});
const PORT = 8000;
app.listen(PORT, () => {
console.log("Listening on 8000...");
});