2023-02-12 16:38:33 -05:00
|
|
|
const express = require('express');
|
|
|
|
const crypto = require('crypto');
|
|
|
|
const router = express.Router();
|
2023-02-25 09:04:32 -05:00
|
|
|
const askBing = require('./askBing');
|
2023-03-08 19:47:23 -05:00
|
|
|
const askSydney = require('./askSydney');
|
2023-03-03 15:52:06 -05:00
|
|
|
const {
|
|
|
|
titleConvo,
|
|
|
|
askClient,
|
|
|
|
browserClient,
|
|
|
|
customClient,
|
|
|
|
detectCode
|
|
|
|
} = require('../../app/');
|
2023-03-13 05:26:17 +08:00
|
|
|
const { getConvo, saveMessage, deleteMessagesSince, deleteMessages, saveConvo } = require('../../models');
|
2023-02-25 09:04:32 -05:00
|
|
|
const { handleError, sendMessage } = require('./handlers');
|
2023-02-12 16:38:33 -05:00
|
|
|
|
2023-03-08 21:06:58 -05:00
|
|
|
router.use('/bing', askBing);
|
|
|
|
router.use('/sydney', askSydney);
|
2023-02-15 12:29:56 -05:00
|
|
|
|
2023-02-12 16:38:33 -05:00
|
|
|
router.post('/', async (req, res) => {
|
2023-03-13 12:35:55 +08:00
|
|
|
let { model, text, parentMessageId, conversationId, chatGptLabel, promptPrefix } = req.body;
|
2023-03-11 12:10:00 -05:00
|
|
|
if (text.length === 0) {
|
2023-02-13 13:32:54 -05:00
|
|
|
return handleError(res, 'Prompt empty or too short');
|
2023-02-12 16:38:33 -05:00
|
|
|
}
|
|
|
|
|
2023-03-13 12:35:55 +08:00
|
|
|
const userMessageId = crypto.randomUUID();
|
|
|
|
const userParentMessageId = parentMessageId || '00000000-0000-0000-0000-000000000000'
|
|
|
|
let userMessage = {
|
|
|
|
messageId: userMessageId,
|
|
|
|
sender: 'User',
|
|
|
|
text,
|
|
|
|
parentMessageId: userParentMessageId,
|
|
|
|
conversationId,
|
|
|
|
isCreatedByUser: true
|
|
|
|
};
|
2023-02-12 16:38:33 -05:00
|
|
|
|
2023-03-03 15:52:06 -05:00
|
|
|
console.log('ask log', {
|
|
|
|
model,
|
|
|
|
...userMessage,
|
2023-03-13 12:35:55 +08:00
|
|
|
parentMessageId: userParentMessageId,
|
2023-03-03 15:52:06 -05:00
|
|
|
conversationId,
|
|
|
|
chatGptLabel,
|
|
|
|
promptPrefix
|
|
|
|
});
|
|
|
|
|
|
|
|
let client;
|
2023-02-12 16:38:33 -05:00
|
|
|
|
2023-03-03 15:52:06 -05:00
|
|
|
if (model === 'chatgpt') {
|
|
|
|
client = askClient;
|
|
|
|
} else if (model === 'chatgptCustom') {
|
|
|
|
client = customClient;
|
|
|
|
} else {
|
|
|
|
client = browserClient;
|
|
|
|
}
|
2023-03-01 21:39:57 -05:00
|
|
|
|
2023-03-07 14:22:33 -05:00
|
|
|
if (model === 'chatgptCustom' && !chatGptLabel && conversationId) {
|
|
|
|
const convo = await getConvo({ conversationId });
|
|
|
|
if (convo) {
|
|
|
|
console.log('found convo for custom gpt', { convo })
|
|
|
|
chatGptLabel = convo.chatGptLabel;
|
|
|
|
promptPrefix = convo.promptPrefix;
|
|
|
|
}
|
|
|
|
}
|
2023-03-13 05:26:17 +08:00
|
|
|
|
2023-03-13 12:35:55 +08:00
|
|
|
// if (messageId) {
|
|
|
|
// // existing conversation
|
|
|
|
// await saveMessage(userMessage);
|
|
|
|
// await deleteMessagesSince(userMessage);
|
|
|
|
// } else {}
|
2023-03-07 14:22:33 -05:00
|
|
|
|
2023-02-12 16:38:33 -05:00
|
|
|
res.writeHead(200, {
|
|
|
|
Connection: 'keep-alive',
|
|
|
|
'Content-Type': 'text/event-stream',
|
|
|
|
'Cache-Control': 'no-cache, no-transform',
|
|
|
|
'Access-Control-Allow-Origin': '*',
|
|
|
|
'X-Accel-Buffering': 'no'
|
|
|
|
});
|
|
|
|
|
|
|
|
try {
|
|
|
|
let i = 0;
|
2023-02-12 22:24:36 -05:00
|
|
|
let tokens = '';
|
2023-02-12 16:38:33 -05:00
|
|
|
const progressCallback = async (partial) => {
|
2023-02-12 22:24:36 -05:00
|
|
|
if (i === 0 && typeof partial === 'object') {
|
2023-02-12 16:38:33 -05:00
|
|
|
userMessage.conversationId = conversationId ? conversationId : partial.conversationId;
|
|
|
|
await saveMessage(userMessage);
|
2023-02-13 13:32:54 -05:00
|
|
|
sendMessage(res, { ...partial, initial: true });
|
2023-02-12 16:38:33 -05:00
|
|
|
i++;
|
|
|
|
}
|
2023-02-12 22:24:36 -05:00
|
|
|
|
|
|
|
if (typeof partial === 'object') {
|
2023-02-13 13:32:54 -05:00
|
|
|
sendMessage(res, { ...partial, message: true });
|
2023-02-12 22:24:36 -05:00
|
|
|
} else {
|
2023-02-25 09:04:32 -05:00
|
|
|
tokens += partial === text ? '' : partial;
|
2023-03-11 23:11:55 -05:00
|
|
|
if (tokens.match(/^\n/)) {
|
|
|
|
tokens = tokens.replace(/^\n/, '');
|
|
|
|
}
|
|
|
|
|
2023-02-21 21:30:56 -05:00
|
|
|
if (tokens.includes('[DONE]')) {
|
|
|
|
tokens = tokens.replace('[DONE]', '');
|
|
|
|
}
|
2023-02-25 09:04:32 -05:00
|
|
|
|
2023-03-02 16:07:36 -05:00
|
|
|
// tokens = await detectCode(tokens);
|
2023-02-24 16:31:59 -05:00
|
|
|
sendMessage(res, { text: tokens, message: true, initial: i === 0 ? true : false });
|
|
|
|
i++;
|
2023-02-12 22:24:36 -05:00
|
|
|
}
|
2023-02-12 16:38:33 -05:00
|
|
|
};
|
|
|
|
|
2023-03-01 21:39:57 -05:00
|
|
|
let gptResponse = await client({
|
2023-02-13 21:15:28 -05:00
|
|
|
text,
|
|
|
|
progressCallback,
|
|
|
|
convo: {
|
2023-03-13 12:35:55 +08:00
|
|
|
parentMessageId: userParentMessageId,
|
2023-02-13 21:15:28 -05:00
|
|
|
conversationId
|
2023-03-03 15:52:06 -05:00
|
|
|
},
|
|
|
|
chatGptLabel,
|
|
|
|
promptPrefix
|
2023-02-13 13:32:54 -05:00
|
|
|
});
|
2023-02-12 22:24:36 -05:00
|
|
|
|
2023-03-01 21:39:57 -05:00
|
|
|
console.log('CLIENT RESPONSE', gptResponse);
|
2023-02-12 22:24:36 -05:00
|
|
|
|
2023-02-13 13:32:54 -05:00
|
|
|
if (!gptResponse.parentMessageId) {
|
2023-02-12 22:24:36 -05:00
|
|
|
gptResponse.text = gptResponse.response;
|
2023-03-13 12:35:55 +08:00
|
|
|
// gptResponse.id = gptResponse.messageId;
|
|
|
|
gptResponse.parentMessageId = userMessage.messageId;
|
2023-02-13 21:15:28 -05:00
|
|
|
userMessage.conversationId = conversationId
|
|
|
|
? conversationId
|
|
|
|
: gptResponse.conversationId;
|
2023-02-13 13:32:54 -05:00
|
|
|
await saveMessage(userMessage);
|
2023-02-12 22:24:36 -05:00
|
|
|
delete gptResponse.response;
|
|
|
|
}
|
|
|
|
|
2023-02-12 16:38:33 -05:00
|
|
|
if (
|
2023-02-13 13:32:54 -05:00
|
|
|
(gptResponse.text.includes('2023') && !gptResponse.text.trim().includes(' ')) ||
|
|
|
|
gptResponse.text.toLowerCase().includes('no response') ||
|
|
|
|
gptResponse.text.toLowerCase().includes('no answer')
|
2023-02-12 16:38:33 -05:00
|
|
|
) {
|
2023-02-13 13:32:54 -05:00
|
|
|
return handleError(res, 'Prompt empty or too short');
|
2023-02-12 16:38:33 -05:00
|
|
|
}
|
|
|
|
|
2023-03-13 12:35:55 +08:00
|
|
|
// if (!parentMessageId) {
|
|
|
|
// gptResponse.title = await titleConvo({
|
|
|
|
// model,
|
|
|
|
// message: text,
|
|
|
|
// response: JSON.stringify(gptResponse.text)
|
|
|
|
// });
|
|
|
|
// }
|
2023-03-03 15:52:06 -05:00
|
|
|
gptResponse.sender = model === 'chatgptCustom' ? chatGptLabel : model;
|
2023-03-13 12:35:55 +08:00
|
|
|
// gptResponse.final = true;
|
2023-03-02 16:07:36 -05:00
|
|
|
gptResponse.text = await detectCode(gptResponse.text);
|
2023-03-04 17:39:06 -05:00
|
|
|
|
|
|
|
if (chatGptLabel?.length > 0 && model === 'chatgptCustom') {
|
|
|
|
gptResponse.chatGptLabel = chatGptLabel;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (promptPrefix?.length > 0 && model === 'chatgptCustom') {
|
|
|
|
gptResponse.promptPrefix = promptPrefix;
|
|
|
|
}
|
|
|
|
|
2023-02-12 16:38:33 -05:00
|
|
|
await saveMessage(gptResponse);
|
|
|
|
await saveConvo(gptResponse);
|
2023-03-13 12:35:55 +08:00
|
|
|
sendMessage(res, {
|
|
|
|
final: true,
|
|
|
|
requestMessage: userMessage,
|
|
|
|
responseMessage: gptResponse
|
|
|
|
});
|
2023-02-12 16:38:33 -05:00
|
|
|
res.end();
|
|
|
|
} catch (error) {
|
|
|
|
console.log(error);
|
2023-03-13 12:35:55 +08:00
|
|
|
await deleteMessages({ messageId: userMessageId });
|
2023-02-13 13:32:54 -05:00
|
|
|
handleError(res, error.message);
|
2023-02-12 16:38:33 -05:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2023-02-21 21:30:56 -05:00
|
|
|
module.exports = router;
|