2023-02-04 20:08:28 -05:00
|
|
|
const express = require('express');
|
2023-02-05 23:05:07 -05:00
|
|
|
const dbConnect = require('../models/dbConnect');
|
2023-02-06 18:25:11 -05:00
|
|
|
const { ask, titleConversation } = require('../app/chatgpt');
|
|
|
|
|
const { saveMessage, getMessages } = require('../models/Message');
|
2023-02-06 15:17:54 -05:00
|
|
|
const { saveConversation, getConversations } = require('../models/Conversation');
|
2023-02-05 23:05:07 -05:00
|
|
|
const crypto = require('crypto');
|
|
|
|
|
const path = require('path');
|
2023-02-06 13:28:21 -05:00
|
|
|
const cors = require('cors');
|
2023-02-04 20:08:28 -05:00
|
|
|
const app = express();
|
|
|
|
|
const port = 3050;
|
2023-02-05 15:26:59 -05:00
|
|
|
app.use(cors());
|
2023-02-04 20:08:28 -05:00
|
|
|
app.use(express.json());
|
|
|
|
|
|
|
|
|
|
const projectPath = path.join(__dirname, '..');
|
|
|
|
|
app.use(express.static(path.join(projectPath, 'public')));
|
|
|
|
|
|
2023-02-05 23:05:07 -05:00
|
|
|
dbConnect().then((connection) => console.log('Connected to MongoDB'));
|
|
|
|
|
|
2023-02-04 20:08:28 -05:00
|
|
|
app.get('/', function (req, res) {
|
|
|
|
|
console.log(path.join(projectPath, 'public', 'index.html'));
|
|
|
|
|
res.sendFile(path.join(projectPath, 'public', 'index.html'));
|
|
|
|
|
});
|
|
|
|
|
|
2023-02-06 15:17:54 -05:00
|
|
|
app.get('/convos', async (req, res) => {
|
|
|
|
|
res.status(200).send(await getConversations());
|
|
|
|
|
});
|
|
|
|
|
|
2023-02-06 18:25:11 -05:00
|
|
|
app.get('/messages/:conversationId', async (req, res) => {
|
|
|
|
|
const { conversationId } = req.params;
|
|
|
|
|
res.status(200).send(await getMessages({ conversationId }));
|
2023-02-06 16:28:50 -05:00
|
|
|
});
|
|
|
|
|
|
2023-02-05 19:41:24 -05:00
|
|
|
app.post('/ask', async (req, res) => {
|
|
|
|
|
console.log(req.body);
|
|
|
|
|
const { text, parentMessageId, conversationId } = req.body;
|
2023-02-05 23:05:07 -05:00
|
|
|
const userMessageId = crypto.randomUUID();
|
2023-02-06 13:28:21 -05:00
|
|
|
let userMessage = { id: userMessageId, sender: 'User', text };
|
2023-02-04 20:49:42 -05:00
|
|
|
|
2023-02-05 15:26:59 -05:00
|
|
|
res.writeHead(200, {
|
|
|
|
|
Connection: 'keep-alive',
|
|
|
|
|
'Content-Type': 'text/event-stream',
|
|
|
|
|
'Cache-Control': 'no-cache, no-transform',
|
2023-02-05 19:41:24 -05:00
|
|
|
'Access-Control-Allow-Origin': '*',
|
|
|
|
|
'X-Accel-Buffering': 'no'
|
2023-02-05 15:26:59 -05:00
|
|
|
});
|
2023-02-05 19:41:24 -05:00
|
|
|
|
|
|
|
|
let i = 0;
|
2023-02-06 18:25:11 -05:00
|
|
|
const progressCallback = async (partial) => {
|
|
|
|
|
// console.log('partial', partial);
|
2023-02-05 19:41:24 -05:00
|
|
|
if (i === 0) {
|
2023-02-06 13:28:21 -05:00
|
|
|
userMessage.parentMessageId = parentMessageId ? parentMessageId : partial.id;
|
|
|
|
|
userMessage.conversationId = conversationId ? conversationId : partial.conversationId;
|
|
|
|
|
await saveMessage(userMessage);
|
2023-02-05 19:41:24 -05:00
|
|
|
res.write(`event: message\ndata: ${JSON.stringify({ ...partial, initial: true })}\n\n`);
|
|
|
|
|
i++;
|
|
|
|
|
}
|
2023-02-05 23:05:07 -05:00
|
|
|
const data = JSON.stringify({ ...partial, message: true });
|
2023-02-05 19:41:24 -05:00
|
|
|
res.write(`event: message\ndata: ${data}\n\n`);
|
|
|
|
|
};
|
|
|
|
|
|
2023-02-05 23:05:07 -05:00
|
|
|
let gptResponse = await ask(text, progressCallback, { parentMessageId, conversationId });
|
|
|
|
|
if (!!parentMessageId) {
|
2023-02-06 13:28:21 -05:00
|
|
|
gptResponse = { ...gptResponse, parentMessageId };
|
2023-02-06 14:57:47 -05:00
|
|
|
} else {
|
|
|
|
|
gptResponse.title = await titleConversation(text, gptResponse.text);
|
2023-02-05 23:05:07 -05:00
|
|
|
}
|
|
|
|
|
|
2023-02-06 13:28:21 -05:00
|
|
|
gptResponse.sender = 'GPT';
|
2023-02-05 23:05:07 -05:00
|
|
|
await saveMessage(gptResponse);
|
2023-02-06 14:05:02 -05:00
|
|
|
await saveConversation(gptResponse);
|
2023-02-05 23:05:07 -05:00
|
|
|
|
2023-02-05 19:41:24 -05:00
|
|
|
res.write(`event: message\ndata: ${JSON.stringify(gptResponse)}\n\n`);
|
|
|
|
|
res.end();
|
2023-02-04 20:08:28 -05:00
|
|
|
});
|
|
|
|
|
|
|
|
|
|
app.listen(port, () => {
|
|
|
|
|
console.log(`Server listening at http://localhost:${port}`);
|
|
|
|
|
});
|