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
This commit is contained in:
HanishKVC 2024-05-25 20:28:38 +05:30
parent 9b596417af
commit c83c19ad4c
3 changed files with 52 additions and 1 deletions

View file

@ -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)
}

View file

@ -8,7 +8,14 @@
<meta name="description" content="SimpleChat: trigger LLM web service endpoints /chat/completions and /completions, single/multi chat sessions" />
<meta name="author" content="by Humans for All" />
<meta http-equiv="Cache-Control" content="no-cache, no-store, must-revalidate" />
<script src="simplechat.js" defer></script>
<script type="importmap">
{
"imports": {
"datautils": "./datautils.mjs"
}
}
</script>
<script src="simplechat.js" type="module" defer></script>
<link rel="stylesheet" href="simplechat.css" />
</head>
<body>

View file

@ -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);
}