feat: cites text with links

This commit is contained in:
Daniel Avila 2023-03-09 18:07:36 -05:00
parent 5e57deab5f
commit a451574760
6 changed files with 31 additions and 25 deletions

View file

@ -1,27 +1,29 @@
/*
// example
const ex = "Fetch API[^1^], Axios[^3^], or XMLHttpRequest[^2^]. Each of these...";
const links = [
'https://www.freecodecamp.org/news/here-is-the-most-popular-ways-to-make-an-http-request-in-javascript-954ce8c95aaa/',
'https://stackoverflow.com/questions/247483/http-get-request-in-javascript',
'https://livecodestream.dev/post/5-ways-to-make-http-requests-in-javascript/'
];
const regex = /\[\^\d+?\^]/g;
const citations = Array.from(new Set(ex.match(regex)));
const linkMap = {};
citations.forEach(citation => {
const digit = citation.match(/\d+?/g)[0];
linkMap[citation] = links[digit - 1];
});
*/
const citationRegex = /\[\^\d+?\^]/g; const citationRegex = /\[\^\d+?\^]/g;
const citeText = (res) => { const citeText = (res, noLinks = false) => {
let result = res.text || res;
const citations = Array.from(new Set(result.match(citationRegex)));
if (citations?.length === 0) return result;
if (noLinks) {
citations.forEach((citation) => {
const digit = citation.match(/\d+?/g)[0];
result = result.replaceAll(citation, `<sup>[${digit}](#)</sup>`);
});
return result;
}
let sources = res.details.sourceAttributions; let sources = res.details.sourceAttributions;
if (!sources) return res.response; if (sources?.length === 0) return result;
sources = sources.map((source) => source.seeMoreUrl); sources = sources.map((source) => source.seeMoreUrl);
citations.forEach((citation) => {
const digit = citation.match(/\d+?/g)[0];
result = result.replaceAll(citation, `<sup>[${digit}](${sources[digit - 1]})</sup>`);
});
return result;
}; };
module.exports = citeText; module.exports = citeText;

View file

@ -5,7 +5,7 @@ const getCitations = (res) => {
const textBlocks = res.details.adaptiveCards[0].body; const textBlocks = res.details.adaptiveCards[0].body;
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) 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 'Learn more:\n' + links.join('\n');
}; };

View file

@ -5,6 +5,7 @@ const { askBing } = require('./bingai');
const { askSydney } = require('./sydney'); const { askSydney } = require('./sydney');
const titleConvo = require('./titleConvo'); const titleConvo = require('./titleConvo');
const getCitations = require('./getCitations'); const getCitations = require('./getCitations');
const citeText = require('./citeText');
const detectCode = require('./detectCode'); const detectCode = require('./detectCode');
module.exports = { module.exports = {
@ -15,5 +16,6 @@ module.exports = {
askSydney, askSydney,
titleConvo, titleConvo,
getCitations, getCitations,
citeText,
detectCode detectCode
}; };

View file

@ -1,7 +1,7 @@
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, getCitations, 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');
@ -29,6 +29,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 });
}; };
@ -60,6 +61,7 @@ router.post('/', async (req, res) => {
? convo.conversationSignature ? convo.conversationSignature
: crypto.randomUUID(); : crypto.randomUUID();
response.text = response.response; response.text = response.response;
delete response.response;
response.suggestions = response.suggestions =
response.details.suggestedResponses && response.details.suggestedResponses &&
response.details.suggestedResponses.map((s) => s.text); response.details.suggestedResponses.map((s) => s.text);
@ -67,7 +69,7 @@ router.post('/', async (req, res) => {
response.final = true; response.final = true;
const links = getCitations(response); const links = getCitations(response);
console.log('sydney links', links); response.text = citeText(response);
// Save user message // Save user message
userMessage.conversationId = response.conversationId; userMessage.conversationId = response.conversationId;

View file

@ -24,7 +24,6 @@ export default function handleSubmit({
const isBing = model === 'bingai' || model === 'sydney'; const isBing = model === 'bingai' || model === 'sydney';
if (isBing && convo.conversationId) { if (isBing && convo.conversationId) {
console.log('bing convo', convo);
payload = { payload = {
...payload, ...payload,
jailbreakConversationId: convo.jailbreakConversationId, jailbreakConversationId: convo.jailbreakConversationId,

View file

@ -10,6 +10,7 @@ module.exports = {
* to use its built-in optimizations accordingly. default is production * to use its built-in optimizations accordingly. default is production
*/ */
mode: 'development', mode: 'development',
cache: false,
/** "entry" /** "entry"
* the entry point * the entry point
*/ */