LibreChat/api/server/routes/ask/askBingAI.js

226 lines
7.2 KiB
JavaScript
Raw Normal View History

2023-02-25 09:04:32 -05:00
const express = require('express');
const crypto = require('crypto');
const router = express.Router();
2023-04-05 16:15:46 +08:00
const { titleConvo, askBing } = require('../../../app');
const { saveMessage, getConvoTitle, saveConvo, getConvo } = require('../../../models');
2023-03-15 15:21:04 -04:00
const { handleError, sendMessage, createOnProgress, handleText } = require('./handlers');
2023-02-25 09:04:32 -05:00
router.post('/', async (req, res) => {
const {
2023-03-31 03:22:57 +08:00
endpoint,
text,
2023-03-31 03:22:57 +08:00
messageId,
overrideParentMessageId = null,
parentMessageId,
2023-03-31 03:22:57 +08:00
conversationId: oldConversationId
} = req.body;
2023-03-31 03:22:57 +08:00
if (text.length === 0) return handleError(res, { text: 'Prompt empty or too short' });
if (endpoint !== 'bingAI') return handleError(res, { text: 'Illegal request' });
2023-03-31 03:22:57 +08:00
// build user message
const conversationId = oldConversationId || crypto.randomUUID();
const isNewConversation = !oldConversationId;
2023-03-31 03:22:57 +08:00
const userMessageId = messageId;
const userParentMessageId = parentMessageId || '00000000-0000-0000-0000-000000000000';
let userMessage = {
messageId: userMessageId,
sender: 'User',
text,
parentMessageId: userParentMessageId,
conversationId,
isCreatedByUser: true
};
2023-02-25 09:04:32 -05:00
2023-03-31 03:22:57 +08:00
// build endpoint option
let endpointOption = {};
if (req.body?.jailbreak)
endpointOption = {
2023-04-06 23:14:42 +08:00
jailbreak: req.body?.jailbreak ?? false,
jailbreakConversationId: req.body?.jailbreakConversationId ?? null,
systemMessage: req.body?.systemMessage ?? null,
context: req.body?.context ?? null,
toneStyle: req.body?.toneStyle ?? 'fast'
};
else
endpointOption = {
2023-04-06 23:14:42 +08:00
jailbreak: req.body?.jailbreak ?? false,
systemMessage: req.body?.systemMessage ?? null,
context: req.body?.context ?? null,
conversationSignature: req.body?.conversationSignature ?? null,
clientId: req.body?.clientId ?? null,
invocationId: req.body?.invocationId ?? null,
toneStyle: req.body?.toneStyle ?? 'fast'
};
2023-03-31 03:22:57 +08:00
console.log('ask log', {
2023-03-31 03:22:57 +08:00
userMessage,
endpointOption,
conversationId
});
2023-03-17 02:08:03 +08:00
if (!overrideParentMessageId) {
await saveMessage(userMessage);
await saveConvo(req?.session?.user?.username, {
...userMessage,
...endpointOption,
conversationId,
endpoint
});
2023-03-17 02:08:03 +08:00
}
// eslint-disable-next-line no-use-before-define
return await ask({
isNewConversation,
userMessage,
2023-03-31 03:22:57 +08:00
endpointOption,
conversationId,
preSendRequest: true,
2023-03-17 02:08:03 +08:00
overrideParentMessageId,
req,
res
});
});
const ask = async ({
isNewConversation,
userMessage,
2023-03-31 03:22:57 +08:00
endpointOption,
conversationId,
preSendRequest = true,
2023-03-31 03:22:57 +08:00
overrideParentMessageId = null,
req,
res
}) => {
2023-03-31 03:22:57 +08:00
let { text, parentMessageId: userParentMessageId, messageId: userMessageId } = userMessage;
2023-02-25 09:04:32 -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'
});
if (preSendRequest) sendMessage(res, { message: userMessage, created: true });
2023-02-25 09:04:32 -05:00
try {
const progressCallback = createOnProgress();
2023-03-17 03:13:42 +08:00
const abortController = new AbortController();
2023-03-31 03:22:57 +08:00
res.on('close', () => abortController.abort());
2023-02-25 09:04:32 -05:00
let response = await askBing({
text,
2023-03-31 03:22:57 +08:00
parentMessageId: userParentMessageId,
conversationId,
...endpointOption,
onProgress: progressCallback.call(null, {
res,
text,
parentMessageId: overrideParentMessageId || userMessageId
}),
2023-03-17 03:13:42 +08:00
abortController
2023-02-25 09:04:32 -05:00
});
console.log('BING RESPONSE', response);
2023-02-25 09:04:32 -05:00
2023-04-06 23:14:42 +08:00
const newConversationId = endpointOption?.jailbreak
? response.jailbreakConversationId
: response.conversationId || conversationId;
const newUserMassageId = response.parentMessageId || userMessageId;
const newResponseMessageId = response.parentMessageId || response.details.requestId || userMessageId;
// STEP1 generate response message
response.text = response.response || response.details.spokenText || '**Bing refused to answer.**';
let responseMessage = {
2023-04-06 23:14:42 +08:00
conversationId: newConversationId,
messageId: newResponseMessageId,
parentMessageId: overrideParentMessageId || newUserMassageId,
sender: endpointOption?.jailbreak ? 'Sydney' : 'BingAI',
text: await handleText(response, true),
2023-04-06 23:14:42 +08:00
suggestions: response.details.suggestedResponses && response.details.suggestedResponses.map(s => s.text)
};
await saveMessage(responseMessage);
// STEP2 update the convosation.
// First update conversationId if needed
// Note!
// Bing API will not use our conversationId at the first time,
// so change the placeholder conversationId to the real one.
// Attition: the api will also create new conversationId while using invalid userMessage.parentMessageId,
// but in this situation, don't change the conversationId, but create new convo.
2023-04-06 23:14:42 +08:00
let conversationUpdate = { conversationId: newConversationId, endpoint: 'bingAI' };
if (conversationId != newConversationId)
if (isNewConversation) {
// change the conversationId to new one
conversationUpdate = {
...conversationUpdate,
conversationId: conversationId,
newConversationId: newConversationId
};
} else {
// create new conversation
conversationUpdate = {
...conversationUpdate,
...endpointOption
};
}
if (endpointOption?.jailbreak) {
conversationUpdate.jailbreak = true;
conversationUpdate.jailbreakConversationId = response.jailbreakConversationId;
} else {
conversationUpdate.jailbreak = false;
conversationUpdate.conversationSignature = response.conversationSignature;
conversationUpdate.clientId = response.clientId;
conversationUpdate.invocationId = response.invocationId;
}
await saveConvo(req?.session?.user?.username, conversationUpdate);
2023-04-06 23:14:42 +08:00
conversationId = newConversationId;
2023-04-04 01:10:50 +08:00
// STEP3 update the user message
2023-04-06 23:14:42 +08:00
userMessage.conversationId = newConversationId;
userMessage.messageId = newUserMassageId;
2023-04-04 01:10:50 +08:00
// If response has parentMessageId, the fake userMessage.messageId should be updated to the real one.
2023-04-06 23:14:42 +08:00
if (!overrideParentMessageId)
await saveMessage({ ...userMessage, messageId: userMessageId, newMessageId: newUserMassageId });
userMessageId = newUserMassageId;
sendMessage(res, {
title: await getConvoTitle(req?.session?.user?.username, conversationId),
final: true,
2023-03-31 03:22:57 +08:00
conversation: await getConvo(req?.session?.user?.username, conversationId),
requestMessage: userMessage,
responseMessage: responseMessage
});
2023-02-25 09:04:32 -05:00
res.end();
2023-03-14 11:42:35 +08:00
if (userParentMessageId == '00000000-0000-0000-0000-000000000000') {
2023-04-05 22:12:59 +08:00
const title = await titleConvo({ endpoint: endpointOption?.endpoint, text, response: responseMessage });
2023-03-31 03:22:57 +08:00
await saveConvo(req?.session?.user?.username, {
conversationId: conversationId,
title
});
2023-03-14 11:42:35 +08:00
}
2023-02-25 09:04:32 -05:00
} catch (error) {
console.log(error);
const errorMessage = {
messageId: crypto.randomUUID(),
2023-03-31 03:22:57 +08:00
sender: endpointOption?.jailbreak ? 'Sydney' : 'BingAI',
conversationId,
parentMessageId: overrideParentMessageId || userMessageId,
error: true,
text: error.message
};
2023-04-05 22:12:59 +08:00
await saveMessage(errorMessage);
handleError(res, errorMessage);
2023-02-25 09:04:32 -05:00
}
};
2023-02-25 09:04:32 -05:00
2023-03-31 03:22:57 +08:00
module.exports = router;