chore: delegate response text parsing to one location

This commit is contained in:
Danny Avila 2023-03-15 15:44:48 -04:00
parent a0c94715ce
commit 84b104e65f
5 changed files with 22 additions and 23 deletions

View file

@ -162,7 +162,7 @@ const ask = async ({
gptResponse.sender = model === 'chatgptCustom' ? convo.chatGptLabel : model; gptResponse.sender = model === 'chatgptCustom' ? convo.chatGptLabel : model;
gptResponse.model = model; gptResponse.model = model;
// gptResponse.final = true; // gptResponse.final = true;
gptResponse.text = await handleText(gptResponse.text); gptResponse.text = await handleText(gptResponse);
if (convo.chatGptLabel?.length > 0 && model === 'chatgptCustom') { if (convo.chatGptLabel?.length > 0 && model === 'chatgptCustom') {
gptResponse.chatGptLabel = convo.chatGptLabel; gptResponse.chatGptLabel = convo.chatGptLabel;

View file

@ -1,10 +1,9 @@
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, citeText, askBing } = require('../../app/'); const { titleConvo, askBing } = require('../../app/');
const { saveMessage, getConvoTitle, saveConvo } = require('../../models'); const { saveMessage, getConvoTitle, saveConvo } = require('../../models');
const { handleError, sendMessage, createOnProgress, handleText } = require('./handlers'); const { handleError, sendMessage, createOnProgress, handleText } = require('./handlers');
const citationRegex = /\[\^\d+?\^]/g;
router.post('/', async (req, res) => { router.post('/', async (req, res) => {
const { const {
@ -129,12 +128,7 @@ const ask = async ({
response.parentMessageId = response.parentMessageId =
overrideParentMessageId || response.parentMessageId || userMessageId; overrideParentMessageId || response.parentMessageId || userMessageId;
const links = getCitations(response); response.text = await handleText(response, true);
response.text =
citeText(response) +
(links?.length > 0 && hasCitations ? `\n<small>${links}</small>` : '');
response.text = await handleText(response.text);
await saveMessage(response); await saveMessage(response);
await saveConvo({ ...response, model, chatGptLabel: null, promptPrefix: null, ...convo }); await saveConvo({ ...response, model, chatGptLabel: null, promptPrefix: null, ...convo });
sendMessage(res, { sendMessage(res, {

View file

@ -1,10 +1,9 @@
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, citeText, askSydney } = require('../../app/'); const { titleConvo, askSydney } = require('../../app/');
const { saveMessage, saveConvo, getConvoTitle } = require('../../models'); const { saveMessage, saveConvo, getConvoTitle } = require('../../models');
const { handleError, sendMessage, createOnProgress, handleText } = require('./handlers'); const { handleError, sendMessage, createOnProgress, handleText } = require('./handlers');
const citationRegex = /\[\^\d+?\^]/g;
router.post('/', async (req, res) => { router.post('/', async (req, res) => {
const { const {
@ -97,7 +96,6 @@ const ask = async ({
console.log('SYDNEY RESPONSE', response); console.log('SYDNEY RESPONSE', response);
// console.dir(response, { depth: null }); // console.dir(response, { depth: null });
const hasCitations = response.response.match(citationRegex)?.length > 0;
userMessage.conversationSignature = userMessage.conversationSignature =
convo.conversationSignature || response.conversationSignature; convo.conversationSignature || response.conversationSignature;
@ -125,12 +123,6 @@ const ask = async ({
response.parentMessageId = response.parentMessageId =
overrideParentMessageId || response.parentMessageId || userMessageId; overrideParentMessageId || response.parentMessageId || userMessageId;
const links = getCitations(response);
response.text =
citeText(response) +
(links?.length > 0 && hasCitations ? `\n<small>${links}</small>` : '');
response.text = await handleText(response.text);
// Save user message // Save user message
userMessage.conversationId = response.conversationId || conversationId; userMessage.conversationId = response.conversationId || conversationId;
await saveMessage(userMessage); await saveMessage(userMessage);
@ -146,6 +138,7 @@ const ask = async ({
}); });
conversationId = userMessage.conversationId; conversationId = userMessage.conversationId;
response.text = await handleText(response, true);
// Save sydney response & convo, then send // Save sydney response & convo, then send
await saveMessage(response); await saveMessage(response);
await saveConvo({ ...response, model, chatGptLabel: null, promptPrefix: null, ...convo }); await saveConvo({ ...response, model, chatGptLabel: null, promptPrefix: null, ...convo });

View file

@ -1,7 +1,8 @@
const _ = require('lodash'); const _ = require('lodash');
const sanitizeHtml = require('sanitize-html'); const sanitizeHtml = require('sanitize-html');
const { titleConvo, citeText, detectCode } = require('../../app/'); const citationRegex = /\[\^\d+?\^]/g;
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 { 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) => { const handleError = (res, message) => {
res.write(`event: error\ndata: ${JSON.stringify(message)}\n\n`); res.write(`event: error\ndata: ${JSON.stringify(message)}\n\n`);
@ -51,9 +52,20 @@ const createOnProgress = () => {
return onProgress; return onProgress;
}; };
const handleText = async (input) => { const handleText = async (response, bing = false) => {
let text = input; let { text } = response;
text = await detectCode(text); text = await detectCode(text);
response.text = text;
if (bing) {
// const hasCitations = response.response.match(citationRegex)?.length > 0;
const links = getCitations(response);
if (response.text.match(citationRegex)?.length > 0) {
text = citeText(response);
}
text += links?.length > 0 ? `\n<small>${links}</small>` : '';
}
// const htmlTags = text.match(htmlTagRegex); // const htmlTags = text.match(htmlTagRegex);
// if (text.includes('```') && htmlTags && htmlTags.length > 0) { // if (text.includes('```') && htmlTags && htmlTags.length > 0) {
// htmlTags.forEach((tag) => { // htmlTags.forEach((tag) => {

View file

@ -142,7 +142,7 @@ export default function TextWrapper({ text }) {
// map over the parts and wrap any text between tildes with <code> tags // map over the parts and wrap any text between tildes with <code> tags
const parts = text.split(markupRegex); const parts = text.split(markupRegex);
const codeParts = inLineWrap(parts); const codeParts = inLineWrap(parts);
return <>{codeParts}</>; // return the wrapped text return <Markdown options={mdOptions}>{codeParts}</Markdown>; // return the wrapped text
} else { } else {
return <Markdown options={mdOptions}>{text}</Markdown>; return <Markdown options={mdOptions}>{text}</Markdown>;
} }