mirror of
https://github.com/danny-avila/LibreChat.git
synced 2026-01-28 13:16:13 +01:00
store all messages to mongoDB
This commit is contained in:
parent
f2003da779
commit
232a823b9b
6 changed files with 102 additions and 15 deletions
|
|
@ -4,9 +4,9 @@ const dbConnect = require('../models/dbConnect');
|
|||
const { saveMessage } = require('../models/Message');
|
||||
const crypto = require('crypto');
|
||||
const path = require('path');
|
||||
const cors = require('cors');
|
||||
const app = express();
|
||||
const port = 3050;
|
||||
const cors = require('cors');
|
||||
app.use(cors());
|
||||
app.use(express.json());
|
||||
|
||||
|
|
@ -24,6 +24,7 @@ app.post('/ask', async (req, res) => {
|
|||
console.log(req.body);
|
||||
const { text, parentMessageId, conversationId } = req.body;
|
||||
const userMessageId = crypto.randomUUID();
|
||||
let userMessage = { id: userMessageId, sender: 'User', text };
|
||||
|
||||
res.writeHead(200, {
|
||||
Connection: 'keep-alive',
|
||||
|
|
@ -34,9 +35,11 @@ app.post('/ask', async (req, res) => {
|
|||
});
|
||||
|
||||
let i = 0;
|
||||
const progressCallback = (partial) => {
|
||||
// console.log('partial', partial);
|
||||
const progressCallback = async (partial) => { // console.log('partial', partial);
|
||||
if (i === 0) {
|
||||
userMessage.parentMessageId = parentMessageId ? parentMessageId : partial.id;
|
||||
userMessage.conversationId = conversationId ? conversationId : partial.conversationId;
|
||||
await saveMessage(userMessage);
|
||||
res.write(`event: message\ndata: ${JSON.stringify({ ...partial, initial: true })}\n\n`);
|
||||
i++;
|
||||
}
|
||||
|
|
@ -46,10 +49,11 @@ app.post('/ask', async (req, res) => {
|
|||
|
||||
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' };
|
||||
// console.log('req parent vs res parent', parentMessageId, gptResponse.parentMessageId);
|
||||
gptResponse = { ...gptResponse, parentMessageId };
|
||||
}
|
||||
|
||||
gptResponse.sender = 'GPT';
|
||||
await saveMessage(gptResponse);
|
||||
|
||||
res.write(`event: message\ndata: ${JSON.stringify(gptResponse)}\n\n`);
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue