adds db connectivity and saving, need to process user messages still

This commit is contained in:
Daniel Avila 2023-02-05 23:05:07 -05:00
parent f889f23792
commit 254f9d7e26
3 changed files with 100 additions and 12 deletions

View file

@ -1,6 +1,9 @@
const path = require('path');
const express = require('express');
const { ask } = require('../app/chatgpt');
const dbConnect = require('../models/dbConnect');
const { saveMessage } = require('../models/Message');
const crypto = require('crypto');
const path = require('path');
const app = express();
const port = 3050;
const cors = require('cors');
@ -10,6 +13,8 @@ app.use(express.json());
const projectPath = path.join(__dirname, '..');
app.use(express.static(path.join(projectPath, 'public')));
dbConnect().then((connection) => console.log('Connected to MongoDB'));
app.get('/', function (req, res) {
console.log(path.join(projectPath, 'public', 'index.html'));
res.sendFile(path.join(projectPath, 'public', 'index.html'));
@ -18,6 +23,7 @@ app.get('/', function (req, res) {
app.post('/ask', async (req, res) => {
console.log(req.body);
const { text, parentMessageId, conversationId } = req.body;
const userMessageId = crypto.randomUUID();
res.writeHead(200, {
Connection: 'keep-alive',
@ -34,11 +40,18 @@ app.post('/ask', async (req, res) => {
res.write(`event: message\ndata: ${JSON.stringify({ ...partial, initial: true })}\n\n`);
i++;
}
const data = JSON.stringify({...partial, message: true });
const data = JSON.stringify({ ...partial, message: true });
res.write(`event: message\ndata: ${data}\n\n`);
};
const gptResponse = await ask(text, progressCallback, { parentMessageId, conversationId });
let gptResponse = await ask(text, progressCallback, { parentMessageId, conversationId });
if (!!parentMessageId) {
console.log('req parent vs res parent', parentMessageId, gptResponse.parentMessageId);
gptResponse = { ...gptResponse, parentMessageId, sender: 'GPT' };
}
await saveMessage(gptResponse);
res.write(`event: message\ndata: ${JSON.stringify(gptResponse)}\n\n`);
res.end();
});