SimpleChat:DU: Try trim using histogram based info

TODO: May have to add max number of uniq chars in histogram at
end of learning phase.
This commit is contained in:
HanishKVC 2024-05-26 01:07:07 +05:30
parent 6390f3489a
commit f33aa28149
2 changed files with 54 additions and 1 deletions

View file

@ -68,3 +68,55 @@ export function trim_repeat_garbage_at_end_loop(sIn, maxSubL, maxMatchLenThresho
sCur = got.data; sCur = got.data;
} }
} }
/**
* A simple minded try trim garbage at end using histogram characteristics
* @param {string} sIn
* @param {number} maxSubL
* @param {number} maxMatchLenThreshold
*/
export function trim_hist_garbage_at_end(sIn, maxSubL, maxMatchLenThreshold) {
if (sIn.length < maxMatchLenThreshold) {
return { trimmed: false, data: sIn };
}
// Learn
let hist = {};
for(let i=0; i<maxSubL; i++) {
let c = sIn[sIn.length-1-i];
if (c in hist) {
hist[c] += 1;
} else {
hist[c] = 1;
}
}
console.log("DBUG:TrimHistGarbage:", hist);
// Catch and Trim
for(let i=0; i < sIn.length; i++) {
let c = sIn[sIn.length-1-i];
if (!(c in hist)) {
if (i < maxMatchLenThreshold) {
return { trimmed: false, data: sIn };
}
return { trimmed: true, data: sIn.substring(0, sIn.length-i+1) };
}
}
return { trimmed: true, data: "" };
}
/**
* Keep trimming repeatedly using hist_garbage logic, till you no longer can
* @param {any} sIn
* @param {number} maxSubL
* @param {number} maxMatchLenThreshold
*/
export function trim_hist_garbage_at_end_loop(sIn, maxSubL, maxMatchLenThreshold) {
let sCur = sIn;
while (true) {
let got = trim_hist_garbage_at_end(sCur, maxSubL, maxMatchLenThreshold);
if (!got.trimmed) {
return got.data;
}
sCur = got.data;
}
}

View file

@ -481,7 +481,7 @@ class MultiChatUI {
assistantMsg = respBody["content"]; assistantMsg = respBody["content"];
} }
} }
assistantMsg = du.trim_repeat_garbage_at_end_loop(assistantMsg, 32, 72); assistantMsg = du.trim_hist_garbage_at_end_loop(assistantMsg, 12, 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);
@ -623,6 +623,7 @@ function startme() {
console.log("INFO:SimpleChat:StartMe:Starting..."); console.log("INFO:SimpleChat:StartMe:Starting...");
gMe = new Me(); gMe = new Me();
document["gMe"] = gMe; document["gMe"] = gMe;
document["du"] = du;
for (let cid of gMe.defaultChatIds) { for (let cid of gMe.defaultChatIds) {
gMe.multiChat.new_chat_session(cid); gMe.multiChat.new_chat_session(cid);
} }