From c83c19ad4c2a065a689a475ea4fdc76359b3f9f6 Mon Sep 17 00:00:00 2001 From: HanishKVC Date: Sat, 25 May 2024 20:28:38 +0530 Subject: [PATCH] SimpleChat:DU:BringIn local helper js modules using importmap Use it to bring in a simple trim garbage at end logic, which is used to trim received response. Also given that importmap assumes esm / standard js modules, so also global variables arent implicitly available outside the modules. So add it has a member of document for now --- .../server/public_simplechat/datautils.mjs | 40 +++++++++++++++++++ examples/server/public_simplechat/index.html | 9 ++++- .../server/public_simplechat/simplechat.js | 4 ++ 3 files changed, 52 insertions(+), 1 deletion(-) create mode 100644 examples/server/public_simplechat/datautils.mjs diff --git a/examples/server/public_simplechat/datautils.mjs b/examples/server/public_simplechat/datautils.mjs new file mode 100644 index 000000000..9a06743cd --- /dev/null +++ b/examples/server/public_simplechat/datautils.mjs @@ -0,0 +1,40 @@ +//@ts-check +// Helpers to work with different data types +// by Humans for All +// + + +/** + * Simple minded logic to help remove repeating garbage at end of the string. + * TODO: Initial skeleton + * @param {string} sIn + */ +export function trim_repeat_garbage_at_end(sIn, maxSubL=10) { + let rCnt = [0]; + const MaxMatchLenThreshold = maxSubL*4; + let maxMatchLen = maxSubL; + let iMML = -1; + for(let subL=1; subL < maxSubL; subL++) { + rCnt.push(0); + let i; + let refS = sIn.substring(sIn.length-subL, sIn.length); + for(i=sIn.length; i > 0; i -= subL) { + let curS = sIn.substring(i-subL, i); + if (refS != curS) { + let curMatchLen = rCnt[subL]*subL; + if (maxMatchLen < curMatchLen) { + maxMatchLen = curMatchLen; + iMML = subL; + } + break; + } + rCnt[subL] += 1; + } + } + console.log("DBUG:DU:TrimRepeatGarbage:", rCnt); + if ((iMML == -1) || (maxMatchLen < MaxMatchLenThreshold)) { + return sIn; + } + let iEnd = sIn.length - maxMatchLen + iMML; + return sIn.substring(0,iEnd) +} diff --git a/examples/server/public_simplechat/index.html b/examples/server/public_simplechat/index.html index 1a1a34208..212f507b9 100644 --- a/examples/server/public_simplechat/index.html +++ b/examples/server/public_simplechat/index.html @@ -8,7 +8,14 @@ - + + diff --git a/examples/server/public_simplechat/simplechat.js b/examples/server/public_simplechat/simplechat.js index 0c48da879..77ae5b96f 100644 --- a/examples/server/public_simplechat/simplechat.js +++ b/examples/server/public_simplechat/simplechat.js @@ -2,6 +2,8 @@ // A simple completions and chat/completions test related web front end logic // by Humans for All +import * as du from "./datautils.mjs"; + class Roles { static System = "system"; static User = "user"; @@ -479,6 +481,7 @@ class MultiChatUI { assistantMsg = respBody["content"]; } } + assistantMsg = du.trim_repeat_garbage_at_end(assistantMsg, 32); chat.add(Roles.Assistant, assistantMsg); if (chatId == this.curChatId) { chat.show(this.elDivChat); @@ -619,6 +622,7 @@ let gMe; function startme() { console.log("INFO:SimpleChat:StartMe:Starting..."); gMe = new Me(); + document["gMe"] = gMe; for (let cid of gMe.defaultChatIds) { gMe.multiChat.new_chat_session(cid); }