-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
86 lines (65 loc) · 3.09 KB
/
app.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
const nodeModulesPath = require('./modules/getCorrectFilePath').nodeModulesPath;
const express = require(`${nodeModulesPath}/express`);
const bodyParser = require(`${nodeModulesPath}/body-parser`)
const session = require(`${nodeModulesPath}/express-session`);
const NedbStore = require(`${nodeModulesPath}/nedb-session-store`)(session);
const path = require('path');
const getOpenAiResponse = require('./modules/getOpenAiResponse')
const queryBuilder = require('./modules/queryBuilder')
const csvReader = require('./modules/csvReader')
const getPreface = require('./modules/getConstants').getPreface;
const passDiagnostics = require('./modules/getConstants').getFrontEndDiagnostics
const app = express();
const store = new NedbStore({
filename: path.join(__dirname,'db/sessions.db')
});
app.use(session({
secret: 'a bird is my friend',
resave: false,
saveUninitialized: true,
cookie: { secure: false, maxAge: 30 * 60 * 1000 },//IF SET TO TRUE SESSIONS ARENT PRESERVED?
store: store
}));
app.use(bodyParser.urlencoded({ extended: false }));
app.use(bodyParser.json());
app.use(express.static(path.join(__dirname,'public')));
app.use((req, res, next) => {
if (!req.session.userData) {
console.log('new session...')
req.session.userData = { messages: [{'role': 'user', 'content': getPreface()}] };
}
next();
});
app.get('/', (req, res) => {
res.sendFile(__dirname + '/html/index.html')
});
app.get('/faq', (req, res) => {
res.sendFile(__dirname + '/html/faq.html')
});
app.post('/btnSubmit', async (req, res) => {
const query = req.body.query;
const data = await queryBuilder.getQuery(query);
console.log(`1. the session contains ${req.session.userData.messages.length} messages`)
req.session.userData.messages = req.session.userData.messages.slice(0,1)
console.log(`2. the session contains ${req.session.userData.messages.length} messages`)
req.session.userData.messages.push({'role': 'user', 'content': data.text});
console.log(`3. the session contains ${req.session.userData.messages.length} messages`)
//const messages = await queryBuilder.parseMessages(req.session.userData.messages);
//req.session.userData.messages = messages
//console.log(`4. the session contains ${req.session.userData.messages.length} messages`)
const response = await getOpenAiResponse(req.session.userData.messages);
//req.session.userData.messages.push({'role': 'assistant', 'content': response});
var frontendDiagnostics = {}
if (passDiagnostics()) {
frontendDiagnostics = {tokens: response.tokens, finishReason: response.finishReason};
}
await csvReader.logQueryResponse(query, response, data.sources, data.tokens);
//console.log(`5. the session contains ${req.session.userData.messages.length} messages`)
const jsonResponse = JSON.stringify({response: response.content, query: query, frontendDiagnostics: frontendDiagnostics});
res.setHeader('Content-Type', 'application/json');
res.send(jsonResponse);
});
const server = app.listen(process.env.PORT || 3000, () => {
console.log(`Server is now listening on port ${process.env.PORT || 3000}`);
});
server.timeout = 300000;