From 9eefa3e24c327bd5e9a47d8a05ebcc2c3f922bb1 Mon Sep 17 00:00:00 2001 From: Danny Avila <110412045+danny-avila@users.noreply.github.com> Date: Fri, 7 Jul 2023 02:03:23 -0400 Subject: [PATCH] fix: add PaLM icon as SVG and improve meilisearch syncing to prevent large indicing jobs (#600) * feat(getIcon.jsx): replace palm.png with google-palm.svg as the icon for the 'google' endpoint * fix(mongoMeili): improve syncing, prevent large indicing jobs from being queued fix(gptPlugins.js, openAI.js): use unfinished and cancelled values when saving messages to help optimize syncing --- api/app/clients/BaseClient.js | 8 ++-- api/models/plugins/mongoMeili.js | 56 ++++++++++++++++++---- api/server/routes/ask/gptPlugins.js | 4 +- api/server/routes/ask/openAI.js | 4 +- client/public/assets/google-palm.svg | 67 +++++++++++++++++++++++++++ client/public/assets/palm.png | Bin 1957 -> 0 bytes client/src/utils/getIcon.jsx | 12 ++--- 7 files changed, 127 insertions(+), 24 deletions(-) create mode 100644 client/public/assets/google-palm.svg delete mode 100644 client/public/assets/palm.png diff --git a/api/app/clients/BaseClient.js b/api/app/clients/BaseClient.js index f5d83c514e..3ecd7f45a1 100644 --- a/api/app/clients/BaseClient.js +++ b/api/app/clients/BaseClient.js @@ -71,7 +71,7 @@ class BaseClient { }; } - createUserMessage({ messageId, parentMessageId, conversationId, text}) { + createUserMessage({ messageId, parentMessageId, conversationId, text }) { const userMessage = { messageId, parentMessageId, @@ -293,7 +293,7 @@ class BaseClient { }; } - async handleContextStrategy({instructions, orderedMessages, formattedMessages}) { + async handleContextStrategy({ instructions, orderedMessages, formattedMessages }) { let payload = this.addInstructions(formattedMessages, instructions); let orderedWithInstructions = this.addInstructions(orderedMessages, instructions); let { @@ -350,7 +350,7 @@ class BaseClient { } if (index === refineIndex) { - map.refined = { ...refinedMessage, messageId: message.messageId}; + map.refined = { ...refinedMessage, messageId: message.messageId }; } map[message.messageId] = payload[index].tokenCount; @@ -457,7 +457,7 @@ class BaseClient { } async saveMessageToDatabase(message, endpointOptions, user = null) { - await saveMessage({ ...message, unfinished: false }); + await saveMessage({ ...message, unfinished: false, cancelled: false }); await saveConvo(user, { conversationId: message.conversationId, endpoint: this.options.endpoint, diff --git a/api/models/plugins/mongoMeili.js b/api/models/plugins/mongoMeili.js index d157eccacc..b123b3153b 100644 --- a/api/models/plugins/mongoMeili.js +++ b/api/models/plugins/mongoMeili.js @@ -190,19 +190,55 @@ module.exports = function mongoMeili(schema, options) { schema.post('remove', function (doc) { doc.postRemoveHook(); }); - schema.post('deleteMany', function () { - // console.log('deleteMany hook', doc); - if (Object.prototype.hasOwnProperty.call(schema.obj, 'messages')) { - console.log('Syncing convos...'); - mongoose.model('Conversation').syncWithMeili(); - } - if (Object.prototype.hasOwnProperty.call(schema.obj, 'messageId')) { - console.log('Syncing messages...'); - mongoose.model('Message').syncWithMeili(); + schema.pre('deleteMany', async function (next) { + try { + if (Object.prototype.hasOwnProperty.call(schema.obj, 'messages')) { + const convoIndex = client.index('convos'); + const deletedConvos = await mongoose.model('Conversation').find(this._conditions).lean(); + let promises = []; + for (const convo of deletedConvos) { + promises.push(convoIndex.deleteDocument(convo.conversationId)); + } + await Promise.all(promises); + } + + if (Object.prototype.hasOwnProperty.call(schema.obj, 'messageId')) { + const messageIndex = client.index('messages'); + const deletedMessages = await mongoose.model('Message').find(this._conditions).lean(); + let promises = []; + for (const message of deletedMessages) { + promises.push(messageIndex.deleteDocument(message.messageId)); + } + await Promise.all(promises); + } + return next(); + } catch (error) { + console.log('[Meilisearch] There was an issue deleting conversation indexes upon deletion, next startup may be slow due to syncing'); + console.error(error); + return next(error); } }); - schema.post('findOneAndUpdate', function (doc) { + + schema.post('findOneAndUpdate', async function (doc) { + if (doc.unfinished) { + return; + } + + let meiliDoc; + // Doc is a Conversation + if (doc.messages) { + try { + meiliDoc = await client.index('convos').getDocument(doc.conversationId); + } catch (error) { + console.log('[Meilisearch] Convo not found and will index', doc.conversationId); + } + } + + if (meiliDoc && meiliDoc.title === doc.title) { + return; + } + doc.postSaveHook(); }); }; diff --git a/api/server/routes/ask/gptPlugins.js b/api/server/routes/ask/gptPlugins.js index 90792caea2..c672aacc86 100644 --- a/api/server/routes/ask/gptPlugins.js +++ b/api/server/routes/ask/gptPlugins.js @@ -117,8 +117,8 @@ const ask = async ({ text, endpoint, endpointOption, parentMessageId = null, con parentMessageId: overrideParentMessageId || userMessageId, text: partialText, model: endpointOption.modelOptions.model, - unfinished: false, - cancelled: true, + unfinished: true, + cancelled: false, error: false }); } diff --git a/api/server/routes/ask/openAI.js b/api/server/routes/ask/openAI.js index e123f462fa..d5baf81663 100644 --- a/api/server/routes/ask/openAI.js +++ b/api/server/routes/ask/openAI.js @@ -88,8 +88,8 @@ const ask = async ({ text, endpointOption, parentMessageId = null, endpoint, con parentMessageId: overrideParentMessageId || userMessageId, text: partialText, model: endpointOption.modelOptions.model, - unfinished: false, - cancelled: true, + unfinished: true, + cancelled: false, error: false }); } diff --git a/client/public/assets/google-palm.svg b/client/public/assets/google-palm.svg new file mode 100644 index 0000000000..5c345fe1c1 --- /dev/null +++ b/client/public/assets/google-palm.svg @@ -0,0 +1,67 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/client/public/assets/palm.png b/client/public/assets/palm.png deleted file mode 100644 index 3488a714574ee46cd3d0569d0f52724b2b603f73..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 1957 zcmbVMc~Dc=9zNM*hYX_zmzq>midGZWqM%7we1HgHl@L}+k%V9_=x8VL!v7c$Rr+Ij!HJKz1z_xpa|@7z0c zW=;t&{BL&FtE~Y5b|JxmQTU23zLq4M1;i>Z00;_RSWFOxVTK%<8Ccn%8E>4wiZ_`* ziNEfE2XJ$Vov>1+QJQYZU2iHZniu&j6xSOig896yrh?pgae#l}?AvHg)W40lTfBlt zzvTMN72o_*AW3?wUYz+b*W+>SJ-mgFkWV@Z&WU+dAK(_)Y7g89G)QE$ZhzQ=Jn~p))&&Ly`~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-k(JPpe4*^?Gg{my0+nWHwXy|4{Y-1Oa1^rci-y( diff --git a/client/src/utils/getIcon.jsx b/client/src/utils/getIcon.jsx index 35e4f4a577..d0a435f44f 100644 --- a/client/src/utils/getIcon.jsx +++ b/client/src/utils/getIcon.jsx @@ -14,7 +14,7 @@ const getIcon = (props) => { width: size, height: size }} - className={`relative flex items-center justify-center` + props?.className} + className={'relative flex items-center justify-center' + props?.className} > { name = 'Plugins'; } else if (endpoint === 'google') { const { modelLabel } = props; - icon = ; + icon = Palm Icon; name = modelLabel || 'PaLM2'; } else if (endpoint === 'bingAI') { const { jailbreak } = props; icon = ; - bg = jailbreak ? `radial-gradient(circle at 90% 110%, #F0F0FA, #D0E0F9)` : `transparent`; + bg = jailbreak ? 'radial-gradient(circle at 90% 110%, #F0F0FA, #D0E0F9)' : 'transparent'; name = jailbreak ? 'Sydney' : 'BingAI'; } else if (endpoint === 'chatGPTBrowser') { icon = ; @@ -67,11 +67,11 @@ const getIcon = (props) => { name = 'ChatGPT'; } else if (endpoint === null) { icon = ; - bg = `grey`; + bg = 'grey'; name = 'N/A'; } else { icon = ; - bg = `grey`; + bg = 'grey'; name = 'UNKNOWN'; } @@ -84,7 +84,7 @@ const getIcon = (props) => { height: size }} className={ - `relative flex items-center justify-center rounded-sm text-white ` + props?.className + 'relative flex items-center justify-center rounded-sm text-white ' + props?.className } > {icon}