From 354bcbd88b1b9c114c8fc0eb8d7e45650412c01c Mon Sep 17 00:00:00 2001 From: kwaroran Date: Thu, 18 Apr 2024 22:47:38 +0900 Subject: [PATCH] Add removeIncompleteResponse --- src/lang/en.ts | 1 + src/lib/Setting/Pages/AdvancedSettings.svelte | 3 ++ src/ts/process/index.ts | 30 +++++++------------ src/ts/storage/database.ts | 1 + src/ts/util.ts | 29 ++++++++++++++++++ 5 files changed, 44 insertions(+), 20 deletions(-) diff --git a/src/lang/en.ts b/src/lang/en.ts index ea8b4df4..d3ff2005 100644 --- a/src/lang/en.ts +++ b/src/lang/en.ts @@ -575,4 +575,5 @@ export const languageEnglish = { metaData: "Meta Data", autoContinueMinTokens: "Target Tokens (Auto Continue)", autoContinueChat: "Anti-Incomplete Response (Auto Continue)", + removeIncompleteResponse: "Remove Incomplete Sentences", } \ No newline at end of file diff --git a/src/lib/Setting/Pages/AdvancedSettings.svelte b/src/lib/Setting/Pages/AdvancedSettings.svelte index 541cd2a1..2891d6f7 100644 --- a/src/lib/Setting/Pages/AdvancedSettings.svelte +++ b/src/lib/Setting/Pages/AdvancedSettings.svelte @@ -76,6 +76,9 @@
+
+ +
diff --git a/src/ts/process/index.ts b/src/ts/process/index.ts index 37bc03c8..914c09c1 100644 --- a/src/ts/process/index.ts +++ b/src/ts/process/index.ts @@ -5,7 +5,7 @@ import { ChatTokenizer, tokenize, tokenizeNum } from "../tokenizer"; import { language } from "../../lang"; import { alertError } from "../alert"; import { loadLoreBookPrompt } from "./lorebook"; -import { findCharacterbyId, getAuthorNoteDefaultText } from "../util"; +import { findCharacterbyId, getAuthorNoteDefaultText, isLastCharPunctuation, trimUntilPunctuation } from "../util"; import { requestChatData } from "./request"; import { stableDiff } from "./stableDiff"; import { processScript, processScriptFull, risuChatParser } from "./scripts"; @@ -1026,7 +1026,10 @@ export async function sendChat(chatProcessIndex = -1,arg:{chatAdditonalTokens?:n if(db.cipherChat){ result = decipherChat(result) } - const result2 = await processScriptFull(nowChatroom, reformatContent(prefix + result), 'editoutput', msgIndex) + if(db.removeIncompleteResponse){ + result = trimUntilPunctuation(result) + } + let result2 = await processScriptFull(nowChatroom, reformatContent(prefix + result), 'editoutput', msgIndex) db.characters[selectedChar].chats[selectedChat].message[msgIndex].data = result2.data emoChanged = result2.emoChanged db.characters[selectedChar].reloadKeys += 1 @@ -1078,6 +1081,9 @@ export async function sendChat(chatProcessIndex = -1,arg:{chatAdditonalTokens?:n let beforeChat = db.characters[selectedChar].chats[selectedChat].message[msgIndex] result2 = await processScriptFull(nowChatroom, reformatContent(beforeChat.data + mess), 'editoutput', msgIndex) } + if(db.removeIncompleteResponse){ + result2.data = trimUntilPunctuation(result2.data) + } result = result2.data const inlayResult = runInlayScreen(currentChar, result) result = inlayResult.text @@ -1138,25 +1144,9 @@ export async function sendChat(chatProcessIndex = -1,arg:{chatAdditonalTokens?:n needsAutoContinue = true } - if(db.autoContinueChat){ + if(db.autoContinueChat && (!isLastCharPunctuation(result))){ //if result doesn't end with punctuation or special characters, auto continue - const lastChar = result.trim().at(-1) - const punctuation = [ - '.', '!', '?', '。', '!', '?', '…', '@', '#', '$', '%', '^', '&', '*', '(', ')', '-', '_', '+', '=', '{', '}', '[', ']', '|', '\\', ':', ';', '"', "'", '<', '>', ',', '.', '/', '~', '`', ' ', - '¡', '¿', '‽', '⁉' - ] - if(lastChar && !(punctuation.indexOf(lastChar) !== -1 - //spacing modifier letters - || (lastChar.charCodeAt(0) >= 0x02B0 && lastChar.charCodeAt(0) <= 0x02FF) - //combining diacritical marks - || (lastChar.charCodeAt(0) >= 0x0300 && lastChar.charCodeAt(0) <= 0x036F) - //hebrew punctuation - || (lastChar.charCodeAt(0) >= 0x0590 && lastChar.charCodeAt(0) <= 0x05CF) - //CJK symbols and punctuation - || (lastChar.charCodeAt(0) >= 0x3000 && lastChar.charCodeAt(0) <= 0x303F) - )){ - needsAutoContinue = true - } + needsAutoContinue = true } if(needsAutoContinue){ diff --git a/src/ts/storage/database.ts b/src/ts/storage/database.ts index e00ecc47..ec3f681f 100644 --- a/src/ts/storage/database.ts +++ b/src/ts/storage/database.ts @@ -626,6 +626,7 @@ export interface Database{ ollamaModel:string autoContinueChat:boolean autoContinueMinTokens:number + removeIncompleteResponse:boolean } export interface customscript{ diff --git a/src/ts/util.ts b/src/ts/util.ts index cfede7b7..453ef253 100644 --- a/src/ts/util.ts +++ b/src/ts/util.ts @@ -463,4 +463,33 @@ export function uuidtoNumber(uuid:string){ result += uuid.charCodeAt(i) } return result +} + +export function isLastCharPunctuation(s:string){ + const lastChar = s.trim().at(-1) + const punctuation = [ + '.', '!', '?', '。', '!', '?', '…', '@', '#', '$', '%', '^', '&', '*', '(', ')', '-', '_', '+', '=', '{', '}', '[', ']', '|', '\\', ':', ';', '<', '>', ',', '.', '/', '~', '`', ' ', + '¡', '¿', '‽', '⁉' + ] + if(lastChar && !(punctuation.indexOf(lastChar) !== -1 + //spacing modifier letters + || (lastChar.charCodeAt(0) >= 0x02B0 && lastChar.charCodeAt(0) <= 0x02FF) + //combining diacritical marks + || (lastChar.charCodeAt(0) >= 0x0300 && lastChar.charCodeAt(0) <= 0x036F) + //hebrew punctuation + || (lastChar.charCodeAt(0) >= 0x0590 && lastChar.charCodeAt(0) <= 0x05CF) + //CJK symbols and punctuation + || (lastChar.charCodeAt(0) >= 0x3000 && lastChar.charCodeAt(0) <= 0x303F) + )){ + return false + } + return true +} + +export function trimUntilPunctuation(s:string){ + let result = s + while(result.length > 0 && !isLastCharPunctuation(result)){ + result = result.slice(0, -1) + } + return result } \ No newline at end of file