-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
243 lines (187 loc) · 8.03 KB
/
index.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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
const express = require("express");
const app = express();
const server = require("http").Server(app);
const fs = require('fs');
const { v4: uuidv4 } = require('uuid');
const OpenAI = require("openai");
const session = require('express-session');
const mongoose = require('mongoose');
const MongoStore = require('connect-mongo');
// const port = 3000;
const port = process.env.PORT || 3000;
//let userInput={};
//let messages = [];
app.set('view engine', 'ejs');
app.use(express.static('public'));
app.use(express.urlencoded({ extended: true }));
// Replace 'your_mongodb_uri' with your actual MongoDB URI
const mongoDbUri = 'Mongo uri goes here';
mongoose.connect(mongoDbUri, { useNewUrlParser: true, useUnifiedTopology: true });
app.use(session({
secret: 'Session key goes here',
resave: false,
saveUninitialized: true,
store: MongoStore.create({ mongoUrl: mongoDbUri })
}));
app.get("/", (req, res) => {
req.session.userInput = {}; // Initialize userInput in the session
req.session.messages = []; // Initialize messages in the session
res.render('index', {});
});
app.get('/new-row', (req, res) => {
res.send(`
<div class="row mb-3">
<div class="col">
<input class="form-control" type="text" name="topic[]" placeholder="Topic">
</div>
<div class="col">
<input class="form-control" type="text" name="description[]" placeholder="Description">
</div>
</div>
`);
});
app.post("/submit-form", (req, res) => {
console.log(req.body);
if (!req.session.userInput) {
req.session.userInput = {};
}
for (let i = 0; i < (req.body.topic).length; i++) {
req.session.userInput[req.body.topic[i]] = req.body.description[i];
}
//adds to user input
console.log(req.session.userInput);
let isEmptyInput = true;
for (const key in req.session.userInput) {
if (req.session.userInput[key].trim() !== "") {
isEmptyInput = false;
break;
}
}
if (isEmptyInput) {
res.send('<p id="save-first">You must input content first</p>');
} else {
const submissionID = uuidv4();
const formatDataWithID = { id: submissionID, data: req.body };
// append data to a file
fs.appendFile('submissions.log', JSON.stringify(formatDataWithID) + '\n', err => {
if (err) throw err;
console.log('data saved with id:', submissionID);
});
res.send(`<p>Your UUID is <span style="font-weight:700">${submissionID}</span>. Use this to save your flashcards.</p>`)
}
});
app.post("/retrieve-data", (req, res) => {
console.log(req.body);
const requestedUuid = req.body.uuid;
// Read the file containing the submissions
fs.readFile('submissions.log', 'utf8', (err, data) => {
if (err) {
console.error(err);
res.send(`Could not find UUID`)
return res.status(500).send('Error reading file');
}
// Parse each line as JSON and find the matching UUID
let submissions = {};
try {
submissions = data.trim().split('\n').map(line => JSON.parse(line));
} catch (error) {
console.error(err);
res.send(`Could not find UUID`)
return res.status(500).send('Error reading file');
}
const matchingSubmission = submissions.find(sub => sub.id === requestedUuid);
console.log(matchingSubmission);
if (matchingSubmission) {
// Send back the matching data
let htmlContent = "";
for (let i = 0; i < (matchingSubmission.data.topic).length; i++) {
let topic = matchingSubmission.data.topic[i];
let description = matchingSubmission.data.description[i];
if (topic && description) {
htmlContent += `
<div class="row mb-3">
<div class="col">
<input class="form-control" type="text" name="topic[]" placeholder="Topic" value="${topic}">
</div>
<div class="col">
<input class="form-control" type="text" name="description[]" placeholder="Description" value ="${description}">
</div>
</div>`
}
}
res.send(htmlContent);
} else {
res.send(`<p class="UUID-error">No data found for UUID: ${requestedUuid} </p>`);
}
});
});
app.post('/chat-message', (req, res) => {
if (req.body.message != "") {
async function main() {
const openai = new OpenAI({
apiKey: "API key goes here",
});
const message = req.body.message; // Get the message from the request
// Add message to the messages array
req.session.messages.push({ role: "user", content: message });
const chatCompletion = await openai.chat.completions.create({
messages: req.session.messages,
model: "gpt-3.5-turbo-1106",
});
const generatedText = chatCompletion.choices[0].message.content;
// Add the assistant's response to the messages array
req.session.messages.push({ role: "assistant", content: generatedText });
console.log(generatedText);
let htmlOutput = `
<div class="chat-mess">
<p class="role">User<p>
<p class="message">${req.body.message}</p>
</div>
<div class="chat-mess">
<p class="role">GPT<p>
<p class="message">${generatedText}</p>
</div>`
res.send(htmlOutput);
}
main().catch((error) => {
console.error(error);
});
}
});
app.post('/gpt', (req, res) => {
console.log(req.session.userInput);
if (req.session.userInput == null || (Object.keys(req.session.userInput).length === 0 && req.session.userInput.constructor === Object)) {
res.send('<p id="save-first">Save your work before chatting with GPT.</p>')
}
else {
async function main() {
const openai = new OpenAI({
apiKey: "API key goes here",
});
// Add the assistant's instructions to the messages array
const assistantInstructions = `Your name is Recall GPT, your goal is to assist the user in studying via active recall. Your job is to prompt the user to explain the topic and check to see if their input has the same meaning as the provided description. If the meaning is not the same, then guide the user in the right path and give them another chance to explain the topic. You will be given the input data in JSON format in the form "topic": "description". You should start with the first topic and work your way through the list, each time, prompting the user. When you have completed all tasks, thank the user.`;
req.session.messages.push({ role: "assistant", content: assistantInstructions });
// Add the user's input to the messages array
req.session.messages.push({ role: "user", content: JSON.stringify(req.session.userInput) });
const chatCompletion = await openai.chat.completions.create({
messages: req.session.messages,
model: "gpt-3.5-turbo-1106",
});
const generatedText = chatCompletion.choices[0].message.content;
// Add the assistant's response to the messages array
req.session.messages.push({ role: "assistant", content: generatedText });
console.log(generatedText);
res.send(`
<div class="chat-mess">
<p class="role">GPT<p>
<p class="message">${generatedText}</p>
</div>`);
}
main().catch((error) => {
console.error(error);
});
}
});
server.listen(port, () => {
console.log(`Listening on port ${port}`)
})