mirror of
https://github.com/danny-avila/LibreChat.git
synced 2025-12-17 08:50:15 +01:00
fix: debounce title request and handle error with default title
This commit is contained in:
parent
96ca783517
commit
2fd50c99b8
7 changed files with 70 additions and 100 deletions
|
|
@ -1,4 +1,5 @@
|
||||||
const { Configuration, OpenAIApi } = require('openai');
|
const { Configuration, OpenAIApi } = require('openai');
|
||||||
|
const _ = require('lodash');
|
||||||
|
|
||||||
const proxyEnvToAxiosProxy = (proxyString) => {
|
const proxyEnvToAxiosProxy = (proxyString) => {
|
||||||
if (!proxyString) return null;
|
if (!proxyString) return null;
|
||||||
|
|
@ -11,29 +12,37 @@ const proxyEnvToAxiosProxy = (proxyString) => {
|
||||||
port: port ? parseInt(port) : undefined,
|
port: port ? parseInt(port) : undefined,
|
||||||
auth: username && password ? { username, password } : undefined
|
auth: username && password ? { username, password } : undefined
|
||||||
};
|
};
|
||||||
|
|
||||||
return proxyConfig
|
return proxyConfig;
|
||||||
}
|
};
|
||||||
|
|
||||||
const titleConvo = async ({ message, response, model }) => {
|
const titleConvo = async ({ message, response, model }) => {
|
||||||
const configuration = new Configuration({
|
const configuration = new Configuration({
|
||||||
apiKey: process.env.OPENAI_KEY
|
apiKey: process.env.OPENAI_KEY
|
||||||
});
|
});
|
||||||
const openai = new OpenAIApi(configuration);
|
const openai = new OpenAIApi(configuration);
|
||||||
const completion = await openai.createChatCompletion({
|
const completion = await openai.createChatCompletion(
|
||||||
model: 'gpt-3.5-turbo',
|
{
|
||||||
messages: [
|
model: 'gpt-3.5-turbo',
|
||||||
{
|
messages: [
|
||||||
role: 'system',
|
{
|
||||||
content:
|
role: 'system',
|
||||||
'You are a title-generator with one job: giving a conversation, detect the language and titling the conversation provided by a user in title case, using the same language.'
|
content:
|
||||||
},
|
'You are a title-generator with one job: giving a conversation, detect the language and titling the conversation provided by a user in title case, using the same language.'
|
||||||
{ 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: ` },
|
},
|
||||||
]
|
{
|
||||||
}, { proxy: proxyEnvToAxiosProxy(process.env.PROXY || null) });
|
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: `
|
||||||
|
}
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{ proxy: proxyEnvToAxiosProxy(process.env.PROXY || null) }
|
||||||
|
);
|
||||||
|
|
||||||
//eslint-disable-next-line
|
//eslint-disable-next-line
|
||||||
return completion.data.choices[0].message.content.replace(/["\.]/g, '');
|
return completion.data.choices[0].message.content.replace(/["\.]/g, '');
|
||||||
};
|
};
|
||||||
|
|
||||||
module.exports = titleConvo;
|
const debouncedTitleConvo = _.debounce(titleConvo, 500);
|
||||||
|
|
||||||
|
module.exports = debouncedTitleConvo;
|
||||||
|
|
|
||||||
|
|
@ -3,15 +3,15 @@ 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 {
|
const { askClient, browserClient, customClient } = require('../../app/');
|
||||||
titleConvo,
|
|
||||||
askClient,
|
|
||||||
browserClient,
|
|
||||||
customClient
|
|
||||||
// detectCode
|
|
||||||
} = require('../../app/');
|
|
||||||
const { getConvo, saveMessage, getConvoTitle, saveConvo } = require('../../models');
|
const { getConvo, saveMessage, getConvoTitle, saveConvo } = require('../../models');
|
||||||
const { handleError, sendMessage, createOnProgress, handleText } = require('./handlers');
|
const {
|
||||||
|
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);
|
||||||
|
|
@ -42,15 +42,6 @@ router.post('/', async (req, res) => {
|
||||||
...convo
|
...convo
|
||||||
});
|
});
|
||||||
|
|
||||||
// if (model === 'chatgptCustom' && !chatGptLabel && conversationId) {
|
|
||||||
// const convo = await getConvo({ conversationId });
|
|
||||||
// if (convo) {
|
|
||||||
// console.log('found convo for custom gpt', { convo })
|
|
||||||
// chatGptLabel = convo.chatGptLabel;
|
|
||||||
// promptPrefix = convo.promptPrefix;
|
|
||||||
// }
|
|
||||||
// }
|
|
||||||
|
|
||||||
await saveMessage(userMessage);
|
await saveMessage(userMessage);
|
||||||
await saveConvo({ ...userMessage, model, ...convo });
|
await saveConvo({ ...userMessage, model, ...convo });
|
||||||
|
|
||||||
|
|
@ -94,17 +85,6 @@ router.post('/regenerate', async (req, res) => {
|
||||||
res
|
res
|
||||||
});
|
});
|
||||||
} else return handleError(res, { text: 'Parent message not found' });
|
} else return handleError(res, { text: 'Parent message not found' });
|
||||||
|
|
||||||
// if (model === 'chatgptCustom' && !chatGptLabel && conversationId) {
|
|
||||||
// const convo = await getConvo({ conversationId });
|
|
||||||
// if (convo) {
|
|
||||||
// console.log('found convo for custom gpt', { convo })
|
|
||||||
// chatGptLabel = convo.chatGptLabel;
|
|
||||||
// promptPrefix = convo.promptPrefix;
|
|
||||||
// }
|
|
||||||
// }
|
|
||||||
|
|
||||||
// await saveConvo({ ...userMessage, model, chatGptLabel, promptPrefix });
|
|
||||||
});
|
});
|
||||||
|
|
||||||
const ask = async ({
|
const ask = async ({
|
||||||
|
|
@ -212,13 +192,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 titleConvo({
|
const title = await genTitle({ model, text, response: gptResponse });
|
||||||
model,
|
|
||||||
message: text,
|
|
||||||
response: JSON.stringify(gptResponse?.text)
|
|
||||||
});
|
|
||||||
|
|
||||||
console.log('CONVERSATION TITLE', title);
|
|
||||||
|
|
||||||
await saveConvo({
|
await saveConvo({
|
||||||
conversationId,
|
conversationId,
|
||||||
|
|
|
||||||
|
|
@ -1,9 +1,15 @@
|
||||||
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 { getCitations, citeText, 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,
|
||||||
|
genTitle,
|
||||||
|
handleText
|
||||||
|
} = require('./handlers');
|
||||||
const citationRegex = /\[\^\d+?\^]/g;
|
const citationRegex = /\[\^\d+?\^]/g;
|
||||||
|
|
||||||
router.post('/', async (req, res) => {
|
router.post('/', async (req, res) => {
|
||||||
|
|
@ -146,13 +152,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 titleConvo({
|
const title = await genTitle({ model, text, response });
|
||||||
model,
|
|
||||||
message: text,
|
|
||||||
response: JSON.stringify(response?.text)
|
|
||||||
});
|
|
||||||
|
|
||||||
console.log('CONVERSATION TITLE', title);
|
|
||||||
|
|
||||||
await saveConvo({
|
await saveConvo({
|
||||||
conversationId,
|
conversationId,
|
||||||
|
|
|
||||||
|
|
@ -1,9 +1,15 @@
|
||||||
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 { getCitations, citeText, 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,
|
||||||
|
genTitle,
|
||||||
|
handleText
|
||||||
|
} = require('./handlers');
|
||||||
const citationRegex = /\[\^\d+?\^]/g;
|
const citationRegex = /\[\^\d+?\^]/g;
|
||||||
|
|
||||||
router.post('/', async (req, res) => {
|
router.post('/', async (req, res) => {
|
||||||
|
|
@ -158,13 +164,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 titleConvo({
|
const title = await genTitle({ model, text, response });
|
||||||
model,
|
|
||||||
message: text,
|
|
||||||
response: JSON.stringify(response?.text)
|
|
||||||
});
|
|
||||||
|
|
||||||
console.log('CONVERSATION TITLE', title);
|
|
||||||
|
|
||||||
await saveConvo({
|
await saveConvo({
|
||||||
conversationId,
|
conversationId,
|
||||||
|
|
|
||||||
|
|
@ -1,41 +1,12 @@
|
||||||
const express = require('express');
|
const express = require('express');
|
||||||
const router = express.Router();
|
const router = express.Router();
|
||||||
const { titleConvo } = require('../../app/');
|
|
||||||
const { getConvo, saveConvo, getConvoTitle } = require('../../models');
|
|
||||||
const { getConvosByPage, deleteConvos, updateConvo } = require('../../models/Conversation');
|
const { getConvosByPage, deleteConvos, updateConvo } = require('../../models/Conversation');
|
||||||
const { getMessages } = require('../../models/Message');
|
|
||||||
|
|
||||||
router.get('/', async (req, res) => {
|
router.get('/', async (req, res) => {
|
||||||
const pageNumber = req.query.pageNumber || 1;
|
const pageNumber = req.query.pageNumber || 1;
|
||||||
res.status(200).send(await getConvosByPage(pageNumber));
|
res.status(200).send(await getConvosByPage(pageNumber));
|
||||||
});
|
});
|
||||||
|
|
||||||
router.post('/gen_title', async (req, res) => {
|
|
||||||
const { conversationId } = req.body.arg;
|
|
||||||
|
|
||||||
const convo = await getConvo(conversationId)
|
|
||||||
const firstMessage = (await getMessages({ conversationId }))[0]
|
|
||||||
const secondMessage = (await getMessages({ conversationId }))[1]
|
|
||||||
|
|
||||||
// if (convo.title == 'New Chat') {
|
|
||||||
// const title = await titleConvo({
|
|
||||||
// model: convo?.model,
|
|
||||||
// message: firstMessage?.text,
|
|
||||||
// response: JSON.stringify(secondMessage?.text || '')
|
|
||||||
// });
|
|
||||||
|
|
||||||
// console.log('CONVERSATION TITLE', title);
|
|
||||||
|
|
||||||
// await saveConvo({
|
|
||||||
// conversationId,
|
|
||||||
// title
|
|
||||||
// })
|
|
||||||
|
|
||||||
// res.status(200).send(title);
|
|
||||||
// } else
|
|
||||||
return res.status(200).send(convo.title);
|
|
||||||
});
|
|
||||||
|
|
||||||
router.post('/clear', async (req, res) => {
|
router.post('/clear', async (req, res) => {
|
||||||
let filter = {};
|
let filter = {};
|
||||||
const { conversationId } = req.body.arg;
|
const { conversationId } = req.body.arg;
|
||||||
|
|
|
||||||
|
|
@ -1,4 +1,4 @@
|
||||||
const { citeText, detectCode } = require('../../app/');
|
const { titleConvo, citeText, detectCode } = require('../../app/');
|
||||||
const _ = require('lodash');
|
const _ = require('lodash');
|
||||||
const sanitizeHtml = require('sanitize-html');
|
const sanitizeHtml = require('sanitize-html');
|
||||||
|
|
||||||
|
|
@ -14,6 +14,23 @@ 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 = '';
|
||||||
|
|
@ -56,4 +73,4 @@ const handleText = async (input) => {
|
||||||
return text;
|
return text;
|
||||||
};
|
};
|
||||||
|
|
||||||
module.exports = { handleError, sendMessage, createOnProgress, handleText };
|
module.exports = { handleError, sendMessage, createOnProgress, genTitle, handleText };
|
||||||
|
|
|
||||||
|
|
@ -3,7 +3,6 @@ import Spinner from '../svg/Spinner';
|
||||||
import { CSSTransition } from 'react-transition-group';
|
import { CSSTransition } from 'react-transition-group';
|
||||||
import ScrollToBottom from './ScrollToBottom';
|
import ScrollToBottom from './ScrollToBottom';
|
||||||
import MultiMessage from './MultiMessage';
|
import MultiMessage from './MultiMessage';
|
||||||
import buildTree from '~/utils/buildTree';
|
|
||||||
import { useSelector } from 'react-redux';
|
import { useSelector } from 'react-redux';
|
||||||
|
|
||||||
const Messages = ({ messages, messageTree }) => {
|
const Messages = ({ messages, messageTree }) => {
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue