merge commit

This commit is contained in:
Daniel Avila 2023-03-17 19:40:44 -04:00
commit 586c162404
25 changed files with 431 additions and 283 deletions

View file

@ -12,7 +12,7 @@ router.use('/bing', askBing);
router.use('/sydney', askSydney);
router.post('/', async (req, res) => {
let { model, text, parentMessageId, conversationId: oldConversationId, ...convo } = req.body;
let { model, text, overrideParentMessageId=null, parentMessageId, conversationId: oldConversationId, ...convo } = req.body;
if (text.length === 0) {
return handleError(res, { text: 'Prompt empty or too short' });
}
@ -36,51 +36,22 @@ router.post('/', async (req, res) => {
...convo
});
await saveMessage(userMessage);
await saveConvo(req?.session?.user?.username, { ...userMessage, model, ...convo });
if (!overrideParentMessageId) {
await saveMessage(userMessage);
await saveConvo(req?.session?.user?.username, { ...userMessage, model, ...convo });
}
return await ask({
userMessage,
model,
convo,
preSendRequest: true,
overrideParentMessageId,
req,
res
});
});
router.post('/regenerate', async (req, res) => {
const { model } = req.body;
const oldUserMessage = await getMessages({ messageId: req.body });
if (oldUserMessage) {
const convo = await getConvo(userMessage?.conversationId);
const userMessageId = crypto.randomUUID();
let userMessage = {
...userMessage,
messageId: userMessageId
};
console.log('ask log for regeneration', {
model,
...userMessage,
...convo
});
return await ask({
userMessage,
model,
convo,
preSendRequest: false,
req,
res
});
} else return handleError(res, { text: 'Parent message not found' });
});
const ask = async ({
userMessage,
overrideParentMessageId = null,
@ -119,6 +90,14 @@ const ask = async ({
try {
const progressCallback = createOnProgress();
const abortController = new AbortController();
res.on('close', () => {
console.log('The client has disconnected.');
// 执行其他操作
abortController.abort();
})
let gptResponse = await client({
text,
onProgress: progressCallback.call(null, model, { res, text }),
@ -127,7 +106,8 @@ const ask = async ({
conversationId,
...convo
},
...convo
...convo,
abortController
});
console.log('CLIENT RESPONSE', gptResponse);
@ -136,10 +116,10 @@ const ask = async ({
gptResponse.text = gptResponse.response;
// gptResponse.id = gptResponse.messageId;
gptResponse.parentMessageId = overrideParentMessageId || userMessageId;
userMessage.conversationId = conversationId
? conversationId
: gptResponse.conversationId;
await saveMessage(userMessage);
// userMessage.conversationId = conversationId
// ? conversationId
// : gptResponse.conversationId;
// await saveMessage(userMessage);
delete gptResponse.response;
}

View file

@ -9,6 +9,7 @@ router.post('/', async (req, res) => {
const {
model,
text,
overrideParentMessageId=null,
parentMessageId,
conversationId: oldConversationId,
...convo
@ -37,8 +38,10 @@ router.post('/', async (req, res) => {
...convo
});
await saveMessage(userMessage);
await saveConvo(req?.session?.user?.username, { ...userMessage, model, ...convo });
if (!overrideParentMessageId) {
await saveMessage(userMessage);
await saveConvo(req?.session?.user?.username, { ...userMessage, model, ...convo });
}
return await ask({
isNewConversation,
@ -46,6 +49,7 @@ router.post('/', async (req, res) => {
model,
convo,
preSendRequest: true,
overrideParentMessageId,
req,
res
});
@ -80,6 +84,14 @@ const ask = async ({
try {
const progressCallback = createOnProgress();
const abortController = new AbortController();
res.on('close', () => {
console.log('The client has disconnected.');
// 执行其他操作
abortController.abort();
})
let response = await askBing({
text,
onProgress: progressCallback.call(null, model, {
@ -91,7 +103,8 @@ const ask = async ({
...convo,
parentMessageId: userParentMessageId,
conversationId
}
},
abortController
});
console.log('BING RESPONSE', response);
@ -101,7 +114,8 @@ const ask = async ({
convo.conversationSignature || response.conversationSignature;
userMessage.conversationId = response.conversationId || conversationId;
userMessage.invocationId = response.invocationId;
await saveMessage(userMessage);
if (!overrideParentMessageId)
await saveMessage(userMessage);
// Bing API will not use our conversationId at the first time,
// so change the placeholder conversationId to the real one.
@ -117,8 +131,8 @@ const ask = async ({
);
conversationId = userMessage.conversationId;
response.text = response.response;
delete response.response;
response.text = response.response || response.details.spokenText || '**Bing refused to answer.**';
// delete response.response;
// response.id = response.details.messageId;
response.suggestions =
response.details.suggestedResponses &&

View file

@ -9,6 +9,7 @@ router.post('/', async (req, res) => {
const {
model,
text,
overrideParentMessageId=null,
parentMessageId,
conversationId: oldConversationId,
...convo
@ -37,8 +38,10 @@ router.post('/', async (req, res) => {
...convo
});
await saveMessage(userMessage);
await saveConvo(req?.session?.user?.username, { ...userMessage, model, ...convo });
if (!overrideParentMessageId) {
await saveMessage(userMessage);
await saveConvo(req?.session?.user?.username, { ...userMessage, model, ...convo });
}
return await ask({
isNewConversation,
@ -46,6 +49,7 @@ router.post('/', async (req, res) => {
model,
convo,
preSendRequest: true,
overrideParentMessageId,
req,
res
});
@ -80,6 +84,14 @@ const ask = async ({
try {
const progressCallback = createOnProgress();
const abortController = new AbortController();
res.on('close', () => {
console.log('The client has disconnected.');
// 执行其他操作
abortController.abort();
})
let response = await askSydney({
text,
onProgress: progressCallback.call(null, model, {
@ -91,7 +103,8 @@ const ask = async ({
parentMessageId: userParentMessageId,
conversationId,
...convo
}
},
abortController
});
console.log('SYDNEY RESPONSE', response);
@ -102,7 +115,8 @@ const ask = async ({
userMessage.conversationId = response.conversationId || conversationId;
userMessage.invocationId = response.invocationId;
// Unlike gpt and bing, Sydney will never accept our given userMessage.messageId, it will generate its own one.
await saveMessage(userMessage);
if (!overrideParentMessageId)
await saveMessage(userMessage);
// Save sydney response
// response.id = response.messageId;
@ -111,8 +125,8 @@ const ask = async ({
response.conversationSignature = convo.conversationSignature
? convo.conversationSignature
: crypto.randomUUID();
response.text = response.response;
delete response.response;
response.text = response.response || response.details.spokenText || '**Bing refused to answer.**';
// delete response.response;
response.suggestions =
response.details.suggestedResponses &&
response.details.suggestedResponses.map((s) => s.text);
@ -125,7 +139,8 @@ const ask = async ({
// Save user message
userMessage.conversationId = response.conversationId || conversationId;
await saveMessage(userMessage);
if (!overrideParentMessageId)
await saveMessage(userMessage);
// Bing API will not use our conversationId at the first time,
// so change the placeholder conversationId to the real one.

View file

@ -1,8 +1,6 @@
const _ = require('lodash');
const sanitizeHtml = require('sanitize-html');
const citationRegex = /\[\^\d+?\^]/g;
const { getCitations, citeText, detectCode } = require('../../app/');
// const htmlTagRegex = /(<\/?\s*[a-zA-Z]*\s*(?:\s+[a-zA-Z]+\s*=\s*(?:"[^"]*"|'[^']*'))*\s*(?:\/?)>|<\s*[a-zA-Z]+\s*(?:\s+[a-zA-Z]+\s*=\s*(?:"[^"]*"|'[^']*'))*\s*(?:\/?>|<\/?>))/g;
const handleError = (res, message) => {
res.write(`event: error\ndata: ${JSON.stringify(message)}\n\n`);
@ -28,14 +26,6 @@ const createOnProgress = () => {
tokens = tokens.replace(/^\n/, '');
}
// const htmlTags = tokens.match(htmlTagRegex);
// if (tokens.includes('```') && htmlTags && htmlTags.length > 0) {
// htmlTags.forEach((tag) => {
// const sanitizedTag = sanitizeHtml(tag);
// tokens = tokens.replaceAll(tag, sanitizedTag);
// });
// }
if (bing) {
tokens = citeText(tokens, true);
}
@ -54,7 +44,7 @@ const createOnProgress = () => {
const handleText = async (response, bing = false) => {
let { text } = response;
text = await detectCode(text);
// text = await detectCode(text);
response.text = text;
if (bing) {
@ -66,14 +56,6 @@ const handleText = async (response, bing = false) => {
text += links?.length > 0 ? `\n<small>${links}</small>` : '';
}
// const htmlTags = text.match(htmlTagRegex);
// if (text.includes('```') && htmlTags && htmlTags.length > 0) {
// htmlTags.forEach((tag) => {
// const sanitizedTag = sanitizeHtml(tag);
// text = text.replaceAll(tag, sanitizedTag);
// });
// }
return text;
};