2024-06-22 08:42:51 -07:00
const { CacheKeys , Constants } = require ( 'librechat-data-provider' ) ;
const getLogStores = require ( '~/cache/getLogStores' ) ;
const { isEnabled } = require ( '~/server/utils' ) ;
const { saveConvo } = require ( '~/models' ) ;
const { logger } = require ( '~/config' ) ;
const initializeClient = require ( './initializeClient' ) ;
const addTitle = async ( req , { text , response , client } ) => {
const { TITLE _CONVO = 'true' } = process . env ? ? { } ;
if ( ! isEnabled ( TITLE _CONVO ) ) {
return ;
}
if ( client . options . titleConvo === false ) {
return ;
}
const DEFAULT _TITLE _MODEL = 'gemini-pro' ;
const { GOOGLE _TITLE _MODEL } = process . env ? ? { } ;
let model = GOOGLE _TITLE _MODEL ? ? DEFAULT _TITLE _MODEL ;
if ( GOOGLE _TITLE _MODEL === Constants . CURRENT _MODEL ) {
model = client . options ? . modelOptions . model ;
if ( client . isVisionModel ) {
logger . warn (
` current_model was specified for Google title request, but the model ${ model } cannot process a text-only conversation. Falling back to ${ DEFAULT _TITLE _MODEL } ` ,
) ;
model = DEFAULT _TITLE _MODEL ;
}
}
const titleEndpointOptions = {
... client . options ,
modelOptions : { ... client . options ? . modelOptions , model : model } ,
attachments : undefined , // After a response, this is set to an empty array which results in an error during setOptions
} ;
const { client : titleClient } = await initializeClient ( {
req ,
res : response ,
endpointOption : titleEndpointOptions ,
} ) ;
const titleCache = getLogStores ( CacheKeys . GEN _TITLE ) ;
const key = ` ${ req . user . id } - ${ response . conversationId } ` ;
const title = await titleClient . titleConvo ( { text , responseText : response ? . text } ) ;
await titleCache . set ( key , title , 120000 ) ;
2024-07-20 01:51:59 -04:00
await saveConvo (
req ,
{
conversationId : response . conversationId ,
title ,
} ,
{ context : 'api/server/services/Endpoints/google/addTitle.js' } ,
) ;
2024-06-22 08:42:51 -07:00
} ;
module . exports = addTitle ;