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:
parent
9b596417af
commit
c83c19ad4c
3 changed files with 52 additions and 1 deletions
40
examples/server/public_simplechat/datautils.mjs
Normal file
40
examples/server/public_simplechat/datautils.mjs
Normal 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)
|
||||||
|
}
|
|
@ -8,7 +8,14 @@
|
||||||
<meta name="description" content="SimpleChat: trigger LLM web service endpoints /chat/completions and /completions, single/multi chat sessions" />
|
<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 name="author" content="by Humans for All" />
|
||||||
<meta http-equiv="Cache-Control" content="no-cache, no-store, must-revalidate" />
|
<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" />
|
<link rel="stylesheet" href="simplechat.css" />
|
||||||
</head>
|
</head>
|
||||||
<body>
|
<body>
|
||||||
|
|
|
@ -2,6 +2,8 @@
|
||||||
// A simple completions and chat/completions test related web front end logic
|
// A simple completions and chat/completions test related web front end logic
|
||||||
// by Humans for All
|
// by Humans for All
|
||||||
|
|
||||||
|
import * as du from "./datautils.mjs";
|
||||||
|
|
||||||
class Roles {
|
class Roles {
|
||||||
static System = "system";
|
static System = "system";
|
||||||
static User = "user";
|
static User = "user";
|
||||||
|
@ -479,6 +481,7 @@ class MultiChatUI {
|
||||||
assistantMsg = respBody["content"];
|
assistantMsg = respBody["content"];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
assistantMsg = du.trim_repeat_garbage_at_end(assistantMsg, 32);
|
||||||
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);
|
||||||
|
@ -619,6 +622,7 @@ let gMe;
|
||||||
function startme() {
|
function startme() {
|
||||||
console.log("INFO:SimpleChat:StartMe:Starting...");
|
console.log("INFO:SimpleChat:StartMe:Starting...");
|
||||||
gMe = new Me();
|
gMe = new Me();
|
||||||
|
document["gMe"] = gMe;
|
||||||
for (let cid of gMe.defaultChatIds) {
|
for (let cid of gMe.defaultChatIds) {
|
||||||
gMe.multiChat.new_chat_session(cid);
|
gMe.multiChat.new_chat_session(cid);
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue