From ee2ccc3e9b037e65fe14d1d6d156785e72c76bf9 Mon Sep 17 00:00:00 2001 From: linonetwo Date: Wed, 10 Apr 2024 14:14:23 +0800 Subject: [PATCH] feat: decode token --- localization/locales/zh_CN/translation.json | 3 ++- src/services/languageModel/index.ts | 2 +- src/services/languageModel/llmWorker/llamaCpp.ts | 11 +++++++++-- 3 files changed, 12 insertions(+), 4 deletions(-) diff --git a/localization/locales/zh_CN/translation.json b/localization/locales/zh_CN/translation.json index 6000d059..e1a6d2ec 100644 --- a/localization/locales/zh_CN/translation.json +++ b/localization/locales/zh_CN/translation.json @@ -479,7 +479,8 @@ "ModelNotExist": "找不到模型", "ModelNotExistDescription": "尝试使用你给的这个路径加载模型,但在这个位置其实没有所需要的模型", "OpenThisPath": "打开该位置", - "GenerationTimeout": "模型生成超时,已中止。" + "GenerationTimeout": "模型生成超时,已中止。", + "ModalDisposed": "模型已卸载,生成中止,需要重新加载模型。" }, "Help": { "Alternatives": "其它源", diff --git a/src/services/languageModel/index.ts b/src/services/languageModel/index.ts index 9e88f5c9..274b94b7 100644 --- a/src/services/languageModel/index.ts +++ b/src/services/languageModel/index.ts @@ -120,7 +120,7 @@ export class LanguageModel implements ILanguageModelService { return; } let observable; - const texts = { timeout: i18n.t('LanguageModel.GenerationTimeout') }; + const texts = { timeout: i18n.t('LanguageModel.GenerationTimeout'), disposed: i18n.t('LanguageModel.ModalDisposed') }; switch (runner) { case LanguageModelRunner.llamaCpp: { observable = worker.runLLama({ completionOptions, loadConfig: { ...config, modelPath }, conversationID }, texts); diff --git a/src/services/languageModel/llmWorker/llamaCpp.ts b/src/services/languageModel/llmWorker/llamaCpp.ts index b4fd2568..776de685 100644 --- a/src/services/languageModel/llmWorker/llamaCpp.ts +++ b/src/services/languageModel/llmWorker/llamaCpp.ts @@ -59,7 +59,7 @@ export function runLLama( conversationID: IRunLLAmaOptions['id']; loadConfig: IRunLLAmaOptions['loadConfig']; }, - texts: { timeout: string }, + texts: { disposed: string; timeout: string }, ): Observable { const { conversationID, completionOptions, loadConfig } = options; @@ -105,8 +105,15 @@ export function runLLama( ...completionOptions, signal: abortController.signal, onToken: (tokens) => { + if (modalInstance === undefined) { + abortController.abort(); + runnerAbortControllers.delete(conversationID); + subscriber.next({ type: 'result', token: texts.timeout, id: conversationID }); + subscriber.complete(); + return; + } updateTimeout(); - subscriber.next({ type: 'result', token, id: conversationID }); + subscriber.next({ type: 'result', token: modalInstance.detokenize(tokens), id: conversationID }); }, }); // completed