mirror of
https://github.com/danny-avila/LibreChat.git
synced 2025-12-18 01:10:14 +01:00
feat: includes sources
This commit is contained in:
parent
a451574760
commit
30936573ac
6 changed files with 49 additions and 9 deletions
|
|
@ -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;
|
||||||
|
|
|
||||||
|
|
@ -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;
|
||||||
|
|
@ -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);
|
||||||
|
|
|
||||||
|
|
@ -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,8 +41,10 @@ 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;
|
||||||
// response.parentMessageId = convo.parentMessageId ? convo.parentMessageId : response.messageId;
|
// response.parentMessageId = convo.parentMessageId ? convo.parentMessageId : 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;
|
||||||
|
|
|
||||||
|
|
@ -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,
|
||||||
|
|
|
||||||
|
|
@ -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;
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue