From c9cfebd7d3fcc0ee59324aa1f9d4a34604cfff2c Mon Sep 17 00:00:00 2001 From: Louis Date: Fri, 17 Nov 2023 16:01:33 +0700 Subject: [PATCH] chore: fix convo summary --- plugins/inference-plugin/src/module.ts | 4 +- web/hooks/useCreateConversation.ts | 3 +- web/hooks/useDeleteModel.ts | 4 +- web/hooks/useSendChatMessage.ts | 96 ++++++++++++-------------- 4 files changed, 48 insertions(+), 59 deletions(-) diff --git a/plugins/inference-plugin/src/module.ts b/plugins/inference-plugin/src/module.ts index 613ad9fca..a9e60e4cd 100644 --- a/plugins/inference-plugin/src/module.ts +++ b/plugins/inference-plugin/src/module.ts @@ -109,10 +109,10 @@ async function validateModelStatus(): Promise { return { error: undefined }; } } - return { error: "Model is not loaded successfully" }; + return { error: "Model loading failed" }; }) .catch((err) => { - return { error: `Model is not loaded successfully. ${err.message}` }; + return { error: `Model loading failed. ${err.message}` }; }); } diff --git a/web/hooks/useCreateConversation.ts b/web/hooks/useCreateConversation.ts index 625ae59cd..5f42ee7ce 100644 --- a/web/hooks/useCreateConversation.ts +++ b/web/hooks/useCreateConversation.ts @@ -20,11 +20,10 @@ export const useCreateConversation = () => { const addNewConvoState = useSetAtom(addNewConversationStateAtom) const requestCreateConvo = async (model: Model) => { - const summary = model.name const mappedConvo: Thread = { id: generateConversationId(), modelId: model.id, - summary, + summary: model.name, createdAt: new Date().toISOString(), updatedAt: new Date().toISOString(), messages: [], diff --git a/web/hooks/useDeleteModel.ts b/web/hooks/useDeleteModel.ts index f59860719..c3b03c61a 100644 --- a/web/hooks/useDeleteModel.ts +++ b/web/hooks/useDeleteModel.ts @@ -20,8 +20,8 @@ export default function useDeleteModel() { // reload models setDownloadedModels(downloadedModels.filter((e) => e.id !== model.id)) toaster({ - title: 'Delete a Model', - description: `Model ${model.id} has been deleted.`, + title: 'Model Deletion Successful', + description: `The model ${model.id} has been successfully deleted.`, }) } diff --git a/web/hooks/useSendChatMessage.ts b/web/hooks/useSendChatMessage.ts index 963944f38..12f54abd0 100644 --- a/web/hooks/useSendChatMessage.ts +++ b/web/hooks/useSendChatMessage.ts @@ -16,6 +16,8 @@ import { ulid } from 'ulid' import { currentPromptAtom } from '@/containers/Providers/Jotai' +import { useActiveModel } from './useActiveModel' + import { addNewMessageAtom, getCurrentChatMessagesAtom, @@ -34,54 +36,51 @@ export default function useSendChatMessage() { const updateConvWaiting = useSetAtom(updateConversationWaitingForResponseAtom) const [currentPrompt, setCurrentPrompt] = useAtom(currentPromptAtom) const currentMessages = useAtomValue(getCurrentChatMessagesAtom) - - let timeout: NodeJS.Timeout | undefined = undefined + const { activeModel } = useActiveModel() function updateConvSummary(newMessage: MessageRequest) { - if (timeout) { - clearTimeout(timeout) - } - timeout = setTimeout(() => { - const conv = currentConvo - if ( - !currentConvo?.summary || + if ( + currentConvo && + newMessage.messages && + newMessage.messages.length > 2 && + (!currentConvo.summary || currentConvo.summary === '' || - currentConvo.summary.startsWith('Prompt:') - ) { - const summaryMsg: ChatCompletionMessage = { - role: ChatCompletionRole.User, - content: - 'summary this conversation in 5 words, the response should just include the summary', - } - // Request convo summary - setTimeout(async () => { - const result = await pluginManager - .get(PluginType.Inference) - ?.inferenceRequest({ - ...newMessage, - messages: newMessage.messages?.concat([summaryMsg]), - }) - - if ( - result?.message && - result.message.split(' ').length <= 10 && - conv?.id - ) { - const updatedConv = { - ...conv, - summary: result.message, - } - updateConversation(updatedConv) - pluginManager - .get(PluginType.Conversational) - ?.saveConversation({ - ...updatedConv, - messages: currentMessages, - }) - } - }, 1000) + currentConvo.summary === activeModel?.name) + ) { + const summaryMsg: ChatCompletionMessage = { + role: ChatCompletionRole.User, + content: + 'summary this conversation in a few words, the response should just include the summary', } - }, 100) + // Request convo summary + setTimeout(async () => { + const result = await pluginManager + .get(PluginType.Inference) + ?.inferenceRequest({ + ...newMessage, + messages: newMessage.messages?.slice(0, -1).concat([summaryMsg]), + }) + if ( + currentConvo && + currentConvo.id === newMessage.threadId && + result?.message && + result?.message?.trim().length > 0 && + result.message.split(' ').length <= 10 + ) { + const updatedConv = { + ...currentConvo, + summary: result.message, + } + updateConversation(updatedConv) + pluginManager + .get(PluginType.Conversational) + ?.saveConversation({ + ...updatedConv, + messages: currentMessages, + }) + } + }, 1000) + } } const sendChatMessage = async () => { @@ -124,15 +123,6 @@ export default function useSendChatMessage() { addNewMessage(threadMessage) events.emit(EventName.OnNewMessageRequest, messageRequest) - if (!currentConvo?.summary && currentConvo) { - const updatedConv: Thread = { - ...currentConvo, - summary: `Prompt: ${prompt}`, - } - - updateConversation(updatedConv) - } - updateConvSummary(messageRequest) }