SimpleChat:DU: Add trim garbage at end in loop helper

This commit is contained in:
HanishKVC 2024-05-25 21:45:54 +05:30
parent c83c19ad4c
commit 54802dc184
2 changed files with 26 additions and 8 deletions

View file

@ -6,12 +6,12 @@
/** /**
* Simple minded logic to help remove repeating garbage at end of the string. * Simple minded logic to help remove repeating garbage at end of the string.
* TODO: Initial skeleton
* @param {string} sIn * @param {string} sIn
* @param {number} maxSubL
* @param {number} maxMatchLenThreshold
*/ */
export function trim_repeat_garbage_at_end(sIn, maxSubL=10) { export function trim_repeat_garbage_at_end(sIn, maxSubL=10, maxMatchLenThreshold=40) {
let rCnt = [0]; let rCnt = [0];
const MaxMatchLenThreshold = maxSubL*4;
let maxMatchLen = maxSubL; let maxMatchLen = maxSubL;
let iMML = -1; let iMML = -1;
for(let subL=1; subL < maxSubL; subL++) { for(let subL=1; subL < maxSubL; subL++) {
@ -32,9 +32,27 @@ export function trim_repeat_garbage_at_end(sIn, maxSubL=10) {
} }
} }
console.log("DBUG:DU:TrimRepeatGarbage:", rCnt); console.log("DBUG:DU:TrimRepeatGarbage:", rCnt);
if ((iMML == -1) || (maxMatchLen < MaxMatchLenThreshold)) { if ((iMML == -1) || (maxMatchLen < maxMatchLenThreshold)) {
return sIn; return {trimmed: false, data: sIn};
}
let iEnd = sIn.length - maxMatchLen;
return { trimmed: true, data: sIn.substring(0, iEnd) };
}
/**
* Simple minded logic to help remove repeating garbage at end of the string, till it cant.
* @param {string} sIn
* @param {number} maxSubL
* @param {number | undefined} [maxMatchLenThreshold]
*/
export function trim_repeat_garbage_at_end_loop(sIn, maxSubL, maxMatchLenThreshold) {
let sCur = sIn;
while(true) {
let got = trim_repeat_garbage_at_end(sCur, maxSubL, maxMatchLenThreshold);
if (got.trimmed != true) {
return got.data;
}
sCur = got.data;
} }
let iEnd = sIn.length - maxMatchLen + iMML;
return sIn.substring(0,iEnd)
} }

View file

@ -481,7 +481,7 @@ class MultiChatUI {
assistantMsg = respBody["content"]; assistantMsg = respBody["content"];
} }
} }
assistantMsg = du.trim_repeat_garbage_at_end(assistantMsg, 32); assistantMsg = du.trim_repeat_garbage_at_end_loop(assistantMsg, 32, 72);
chat.add(Roles.Assistant, assistantMsg); chat.add(Roles.Assistant, assistantMsg);
if (chatId == this.curChatId) { if (chatId == this.curChatId) {
chat.show(this.elDivChat); chat.show(this.elDivChat);