mirror of
https://github.com/danny-avila/LibreChat.git
synced 2025-12-18 01:10:14 +01:00
chore: refactor titleConvo
This commit is contained in:
parent
a8aad30fc8
commit
a0c94715ce
5 changed files with 47 additions and 71 deletions
|
|
@ -16,33 +16,44 @@ const proxyEnvToAxiosProxy = (proxyString) => {
|
|||
return proxyConfig;
|
||||
};
|
||||
|
||||
const titleConvo = async ({ message, response, model }) => {
|
||||
const configuration = new Configuration({
|
||||
apiKey: process.env.OPENAI_KEY
|
||||
});
|
||||
const openai = new OpenAIApi(configuration);
|
||||
const completion = await openai.createChatCompletion(
|
||||
{
|
||||
model: 'gpt-3.5-turbo',
|
||||
messages: [
|
||||
{
|
||||
role: 'system',
|
||||
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) }
|
||||
);
|
||||
const titleConvo = async ({ model, text, response }) => {
|
||||
let title = 'New Chat';
|
||||
try {
|
||||
const configuration = new Configuration({
|
||||
apiKey: process.env.OPENAI_KEY
|
||||
});
|
||||
const openai = new OpenAIApi(configuration);
|
||||
const completion = await openai.createChatCompletion(
|
||||
{
|
||||
model: 'gpt-3.5-turbo',
|
||||
messages: [
|
||||
{
|
||||
role: 'system',
|
||||
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: "${text}"\n\n${model}: "${JSON.stringify(
|
||||
response?.text
|
||||
)}"\n\nTitle: `
|
||||
}
|
||||
]
|
||||
},
|
||||
{ proxy: proxyEnvToAxiosProxy(process.env.PROXY || null) }
|
||||
);
|
||||
|
||||
//eslint-disable-next-line
|
||||
return completion.data.choices[0].message.content.replace(/["\.]/g, '');
|
||||
//eslint-disable-next-line
|
||||
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;
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue