-
Notifications
You must be signed in to change notification settings - Fork 1
/
final.js
118 lines (92 loc) · 3.97 KB
/
final.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
const express = require("express");
const app = express();
const bodyParser = require("body-parser");
const path = require("path");
const { Configuration, OpenAIApi } = require("openai");
require("dotenv").config()
const configuration = new Configuration({
apiKey: process.env.OPENAI_API_KEY,
});
const openai = new OpenAIApi(configuration);
// Middleware to parse JSON body
app.use(bodyParser.json());
// Serve the index.html file
app.get("/", (req, res) => {
res.sendFile(path.join(__dirname, "index.html"));
});
app.get("/quiz", (req, res) => {
res.sendFile(path.join(__dirname, "quiz.html"));
});
app.get("/explain", (req, res) => {
res.sendFile(path.join(__dirname, "Explainer.html"));
});
// POST endpoint handler
app.post("/process-text", async(req, res) => {
const text = req.body.text;
// // Perform your desired operation on the received text
const completion = await openai.createChatCompletion({
model: "gpt-3.5-turbo-16k",
messages: [{"role": "system", "content": "You are a helpful assistant."}, {role: "user",
content: ` for the given content do the following : generate 2 summaries simple summary and expert summary based on the instructions for both below:
Act as a teacher who will be generating a simple 8 point summary based on the content provided, Make the summary such that a novice learner who is learning the concept for the first time understand.
Add bullet points for each point. when the simple summary ends add "%$" to denote it's end.
For the expert summary act as an industry leader generating a 8 point summary the for an experienced professional in the same domain of the content.
Make sure to include relevant industry usecases of the concept.Add bullet points for each point.
In both summary make sure that the whole context of the content is intact.
${text}`
}],
});
const summary = completion.data.choices[0].message?.content;
let expertsummary = '';
if (summary.includes('%$') ){
const splitIndex = summary.indexOf('%$');
const simpsummary = summary.substring(0, splitIndex);
const expertsummary = summary.substring(splitIndex + '%$'.length);
// Send the processed text back to the client
res.json({
summary: simpsummary,
expertsummary: expertsummary
});
}
});
app.post("/quiz-text", async(req, res) => {
const text = req.body.text;
// // Perform your desired operation on the received text
const completion = await openai.createChatCompletion({
model: "gpt-3.5-turbo-16k",
messages: [{role: "user",
content: ` for the given content do the following :
Act as a quiz master who will be generating a 5 question mcq quiz based on the content provided,
Make the quiz such that each qustion is more difficult than the last.
Add radio for each question an insure to add a "?" after each question.
Start the output directly with the question nothing before that.
${text}`
}],
});
const quiz = completion.data.choices[0].message?.content;
// Send the processed text back to the client
res.json({
quiz: quiz,
});
});
app.post("/explain-text", async(req, res) => {
const text = req.body.text;
// // Perform your desired operation on the received text
const completion = await openai.createChatCompletion({
model: "gpt-3.5-turbo-16k",
messages: [{"role": "system", "content": "You are a helpful assistant."}, {role: "user",
content: `for the given content give an explaination of what it is and real life examples
to solidify the understanding. Explain in simple words in max 100 words.
${text}`
}],
});
const explaination = completion.data.choices[0].message?.content;
// Send the processed text back to the client
res.json({
explaination: explaination
});
});
// Start the server
app.listen(4000, () => {
console.log("Server started on port 4000");
});