feat: includes sources

This commit is contained in:
Daniel Avila 2023-03-09 18:42:36 -05:00
parent a451574760
commit 30936573ac
6 changed files with 49 additions and 9 deletions

View file

@ -8,7 +8,7 @@ const citeText = (res, noLinks = false) => {
if (noLinks) { if (noLinks) {
citations.forEach((citation) => { citations.forEach((citation) => {
const digit = citation.match(/\d+?/g)[0]; const digit = citation.match(/\d+?/g)[0];
result = result.replaceAll(citation, `<sup>[${digit}](#)</sup>`); result = result.replaceAll(citation, `<sup>[${digit}](#) </sup>`);
}); });
return result; return result;
@ -20,7 +20,7 @@ const citeText = (res, noLinks = false) => {
citations.forEach((citation) => { citations.forEach((citation) => {
const digit = citation.match(/\d+?/g)[0]; const digit = citation.match(/\d+?/g)[0];
result = result.replaceAll(citation, `<sup>[${digit}](${sources[digit - 1]})</sup>`); result = result.replaceAll(citation, `<sup>[${digit}](${sources[digit - 1]}) </sup>`);
}); });
return result; return result;

View file

@ -6,8 +6,8 @@ const getCitations = (res) => {
if (!textBlocks) return ''; if (!textBlocks) return '';
let links = textBlocks[textBlocks.length - 1]?.text.match(regex); let links = textBlocks[textBlocks.length - 1]?.text.match(regex);
if (links?.length === 0 || !links) return ''; if (links?.length === 0 || !links) return '';
links = links.map((link) => '- ' + link.trim()); links = links.map((link) => link.trim());
return 'Learn more:\n' + links.join('\n'); return links.join('\n');
}; };
module.exports = getCitations; module.exports = getCitations;

View file

@ -1,9 +1,10 @@
const express = require('express'); const express = require('express');
const crypto = require('crypto'); const crypto = require('crypto');
const router = express.Router(); const router = express.Router();
const { titleConvo, askBing } = require('../../app/'); const { titleConvo, getCitations, citeText, askBing } = require('../../app/');
const { saveMessage, deleteMessages, saveConvo } = require('../../models'); const { saveMessage, deleteMessages, saveConvo } = require('../../models');
const { handleError, sendMessage } = require('./handlers'); const { handleError, sendMessage } = require('./handlers');
const citationRegex = /\[\^\d+?\^]/g;
router.post('/', async (req, res) => { router.post('/', async (req, res) => {
const { model, text, ...convo } = req.body; const { model, text, ...convo } = req.body;
@ -29,6 +30,7 @@ router.post('/', async (req, res) => {
const progressCallback = async (partial) => { const progressCallback = async (partial) => {
tokens += partial === text ? '' : partial; tokens += partial === text ? '' : partial;
// tokens = appendCode(tokens); // tokens = appendCode(tokens);
tokens = citeText(tokens, true);
sendMessage(res, { text: tokens, message: true }); sendMessage(res, { text: tokens, message: true });
}; };
@ -39,7 +41,8 @@ router.post('/', async (req, res) => {
}); });
console.log('BING RESPONSE'); console.log('BING RESPONSE');
console.dir(response, { depth: null }); // console.dir(response, { depth: null });
const hasCitations = citationRegex.test(response.response);
userMessage.conversationSignature = userMessage.conversationSignature =
convo.conversationSignature || response.conversationSignature; convo.conversationSignature || response.conversationSignature;
@ -56,12 +59,19 @@ router.post('/', async (req, res) => {
} }
response.text = response.response; response.text = response.response;
delete response.response;
response.id = response.details.messageId; response.id = response.details.messageId;
response.suggestions = response.suggestions =
response.details.suggestedResponses && response.details.suggestedResponses &&
response.details.suggestedResponses.map((s) => s.text); response.details.suggestedResponses.map((s) => s.text);
response.sender = model; response.sender = model;
response.final = true; response.final = true;
const links = getCitations(response);
response.text =
citeText(response) +
(links?.length > 0 && hasCitations ? `\n<small>${links}</small>` : '');
await saveMessage(response); await saveMessage(response);
await saveConvo(response); await saveConvo(response);
sendMessage(res, response); sendMessage(res, response);

View file

@ -4,6 +4,7 @@ const router = express.Router();
const { titleConvo, getCitations, citeText, askSydney } = require('../../app/'); const { titleConvo, getCitations, citeText, askSydney } = require('../../app/');
const { saveMessage, deleteMessages, saveConvo, getConvoTitle } = require('../../models'); const { saveMessage, deleteMessages, saveConvo, getConvoTitle } = require('../../models');
const { handleError, sendMessage } = require('./handlers'); const { handleError, sendMessage } = require('./handlers');
const citationRegex = /\[\^\d+?\^]/g;
router.post('/', async (req, res) => { router.post('/', async (req, res) => {
const { model, text, ...convo } = req.body; const { model, text, ...convo } = req.body;
@ -40,7 +41,9 @@ router.post('/', async (req, res) => {
}); });
console.log('SYDNEY RESPONSE'); console.log('SYDNEY RESPONSE');
console.log(response.response);
// console.dir(response, { depth: null }); // console.dir(response, { depth: null });
const hasCitations = citationRegex.test(response.response);
// Save sydney response // Save sydney response
response.id = response.messageId; response.id = response.messageId;
@ -69,7 +72,9 @@ router.post('/', async (req, res) => {
response.final = true; response.final = true;
const links = getCitations(response); const links = getCitations(response);
response.text = citeText(response); response.text =
citeText(response) +
(links?.length > 0 && hasCitations ? `\n<small>${links}</small>` : '');
// Save user message // Save user message
userMessage.conversationId = response.conversationId; userMessage.conversationId = response.conversationId;

View file

@ -91,7 +91,6 @@ export default function TextChat({ messages }) {
}) })
); );
} else if (model === 'sydney') { } else if (model === 'sydney') {
console.log('Sydney data:', data)
const { const {
title, title,
jailbreakConversationId, jailbreakConversationId,

View file

@ -7,6 +7,32 @@
outline: 1px solid limegreen !important; outline: 1px solid limegreen !important;
} */ } */
/* p small {
opacity: 0;
animation: fadeIn 3s ease forwards;
}
@keyframes fadeIn {
from {
opacity: 0;
transform: translateY(-10px);
}
to {
opacity: 1;
transform: translateY(0);
}
} */
p > small {
opacity: 0;
animation: fadein 2s forwards;
}
@keyframes fadein {
from { opacity: 0; transform: translateY(-20px); }
to { opacity: 1; transform: translateY(0); }
}
blockquote, dd, dl, fieldset, figure, h1, h2, h3, h4, h5, h6, hr, p, pre { blockquote, dd, dl, fieldset, figure, h1, h2, h3, h4, h5, h6, hr, p, pre {
margin: 0; margin: 0;
} }