mirror of
https://github.com/danny-avila/LibreChat.git
synced 2025-12-17 08:50:15 +01:00
chore: refactor titleConvo
This commit is contained in:
parent
a8aad30fc8
commit
a0c94715ce
5 changed files with 47 additions and 71 deletions
|
|
@ -16,7 +16,9 @@ const proxyEnvToAxiosProxy = (proxyString) => {
|
||||||
return proxyConfig;
|
return proxyConfig;
|
||||||
};
|
};
|
||||||
|
|
||||||
const titleConvo = async ({ message, response, model }) => {
|
const titleConvo = async ({ model, text, response }) => {
|
||||||
|
let title = 'New Chat';
|
||||||
|
try {
|
||||||
const configuration = new Configuration({
|
const configuration = new Configuration({
|
||||||
apiKey: process.env.OPENAI_KEY
|
apiKey: process.env.OPENAI_KEY
|
||||||
});
|
});
|
||||||
|
|
@ -32,7 +34,9 @@ const titleConvo = async ({ message, response, model }) => {
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
role: 'user',
|
role: 'user',
|
||||||
content: `In 5 words or less, summarize the conversation below with a title in title case using the language the user writes in. Don't refer to the participants of the conversation by name. Do not include punctuation or quotation marks. Your response should be in title case, exclusively containing the title. Conversation:\n\nUser: "${message}"\n\n${model}: "${response}"\n\nTitle: `
|
content: `In 5 words or less, summarize the conversation below with a title in title case using the language the user writes in. Don't refer to the participants of the conversation by name. Do not include punctuation or quotation marks. Your response should be in title case, exclusively containing the title. Conversation:\n\nUser: "${text}"\n\n${model}: "${JSON.stringify(
|
||||||
|
response?.text
|
||||||
|
)}"\n\nTitle: `
|
||||||
}
|
}
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
|
|
@ -40,9 +44,16 @@ const titleConvo = async ({ message, response, model }) => {
|
||||||
);
|
);
|
||||||
|
|
||||||
//eslint-disable-next-line
|
//eslint-disable-next-line
|
||||||
return completion.data.choices[0].message.content.replace(/["\.]/g, '');
|
title = completion.data.choices[0].message.content.replace(/["\.]/g, '');
|
||||||
|
} catch (e) {
|
||||||
|
console.error(e);
|
||||||
|
console.log('There was an issue generating title, see error above');
|
||||||
|
}
|
||||||
|
|
||||||
|
console.log('CONVERSATION TITLE', title);
|
||||||
|
return title;
|
||||||
};
|
};
|
||||||
|
|
||||||
const debouncedTitleConvo = _.debounce(titleConvo, 500);
|
const throttledTitleConvo = _.throttle(titleConvo, 1000);
|
||||||
|
|
||||||
module.exports = debouncedTitleConvo;
|
module.exports = throttledTitleConvo;
|
||||||
|
|
|
||||||
|
|
@ -3,15 +3,9 @@ const crypto = require('crypto');
|
||||||
const router = express.Router();
|
const router = express.Router();
|
||||||
const askBing = require('./askBing');
|
const askBing = require('./askBing');
|
||||||
const askSydney = require('./askSydney');
|
const askSydney = require('./askSydney');
|
||||||
const { askClient, browserClient, customClient } = require('../../app/');
|
const { titleConvo, askClient, browserClient, customClient } = require('../../app/');
|
||||||
const { getConvo, saveMessage, getConvoTitle, saveConvo } = require('../../models');
|
const { getConvo, saveMessage, getConvoTitle, saveConvo } = require('../../models');
|
||||||
const {
|
const { handleError, sendMessage, createOnProgress, handleText } = require('./handlers');
|
||||||
handleError,
|
|
||||||
sendMessage,
|
|
||||||
createOnProgress,
|
|
||||||
genTitle,
|
|
||||||
handleText
|
|
||||||
} = require('./handlers');
|
|
||||||
const { getMessages } = require('../../models/Message');
|
const { getMessages } = require('../../models/Message');
|
||||||
|
|
||||||
router.use('/bing', askBing);
|
router.use('/bing', askBing);
|
||||||
|
|
@ -192,7 +186,7 @@ const ask = async ({
|
||||||
res.end();
|
res.end();
|
||||||
|
|
||||||
if (userParentMessageId == '00000000-0000-0000-0000-000000000000') {
|
if (userParentMessageId == '00000000-0000-0000-0000-000000000000') {
|
||||||
const title = await genTitle({ model, text, response: gptResponse });
|
const title = await titleConvo({ model, text, response: gptResponse });
|
||||||
|
|
||||||
await saveConvo({
|
await saveConvo({
|
||||||
conversationId,
|
conversationId,
|
||||||
|
|
|
||||||
|
|
@ -1,15 +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 { getCitations, citeText, askBing } = require('../../app/');
|
const { titleConvo, getCitations, citeText, askBing } = require('../../app/');
|
||||||
const { saveMessage, getConvoTitle, saveConvo } = require('../../models');
|
const { saveMessage, getConvoTitle, saveConvo } = require('../../models');
|
||||||
const {
|
const { handleError, sendMessage, createOnProgress, handleText } = require('./handlers');
|
||||||
handleError,
|
|
||||||
sendMessage,
|
|
||||||
createOnProgress,
|
|
||||||
genTitle,
|
|
||||||
handleText
|
|
||||||
} = require('./handlers');
|
|
||||||
const citationRegex = /\[\^\d+?\^]/g;
|
const citationRegex = /\[\^\d+?\^]/g;
|
||||||
|
|
||||||
router.post('/', async (req, res) => {
|
router.post('/', async (req, res) => {
|
||||||
|
|
@ -152,7 +146,7 @@ const ask = async ({
|
||||||
res.end();
|
res.end();
|
||||||
|
|
||||||
if (userParentMessageId == '00000000-0000-0000-0000-000000000000') {
|
if (userParentMessageId == '00000000-0000-0000-0000-000000000000') {
|
||||||
const title = await genTitle({ model, text, response });
|
const title = await titleConvo({ model, text, response });
|
||||||
|
|
||||||
await saveConvo({
|
await saveConvo({
|
||||||
conversationId,
|
conversationId,
|
||||||
|
|
|
||||||
|
|
@ -1,15 +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 { getCitations, citeText, askSydney } = require('../../app/');
|
const { titleConvo, getCitations, citeText, askSydney } = require('../../app/');
|
||||||
const { saveMessage, saveConvo, getConvoTitle } = require('../../models');
|
const { saveMessage, saveConvo, getConvoTitle } = require('../../models');
|
||||||
const {
|
const { handleError, sendMessage, createOnProgress, handleText } = require('./handlers');
|
||||||
handleError,
|
|
||||||
sendMessage,
|
|
||||||
createOnProgress,
|
|
||||||
genTitle,
|
|
||||||
handleText
|
|
||||||
} = require('./handlers');
|
|
||||||
const citationRegex = /\[\^\d+?\^]/g;
|
const citationRegex = /\[\^\d+?\^]/g;
|
||||||
|
|
||||||
router.post('/', async (req, res) => {
|
router.post('/', async (req, res) => {
|
||||||
|
|
@ -164,7 +158,7 @@ const ask = async ({
|
||||||
res.end();
|
res.end();
|
||||||
|
|
||||||
if (userParentMessageId == '00000000-0000-0000-0000-000000000000') {
|
if (userParentMessageId == '00000000-0000-0000-0000-000000000000') {
|
||||||
const title = await genTitle({ model, text, response });
|
const title = await titleConvo({ model, text, response });
|
||||||
|
|
||||||
await saveConvo({
|
await saveConvo({
|
||||||
conversationId,
|
conversationId,
|
||||||
|
|
|
||||||
|
|
@ -15,23 +15,6 @@ const sendMessage = (res, message) => {
|
||||||
res.write(`event: message\ndata: ${JSON.stringify(message)}\n\n`);
|
res.write(`event: message\ndata: ${JSON.stringify(message)}\n\n`);
|
||||||
};
|
};
|
||||||
|
|
||||||
const genTitle = async ({ model, text, response }) => {
|
|
||||||
let title = 'New Chat';
|
|
||||||
try {
|
|
||||||
title = await titleConvo({
|
|
||||||
model,
|
|
||||||
message: text,
|
|
||||||
response: JSON.stringify(response?.text)
|
|
||||||
});
|
|
||||||
} catch (e) {
|
|
||||||
console.error(e);
|
|
||||||
console.log('There was an issue generating title, see error above');
|
|
||||||
}
|
|
||||||
|
|
||||||
console.log('CONVERSATION TITLE', title);
|
|
||||||
return title;
|
|
||||||
};
|
|
||||||
|
|
||||||
const createOnProgress = () => {
|
const createOnProgress = () => {
|
||||||
let i = 0;
|
let i = 0;
|
||||||
let tokens = '';
|
let tokens = '';
|
||||||
|
|
@ -82,4 +65,4 @@ const handleText = async (input) => {
|
||||||
return text;
|
return text;
|
||||||
};
|
};
|
||||||
|
|
||||||
module.exports = { handleError, sendMessage, createOnProgress, genTitle, handleText };
|
module.exports = { handleError, sendMessage, createOnProgress, handleText };
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue