From 7905f2fcbe1ecb935e9eb1e43208d931de0a56a4 Mon Sep 17 00:00:00 2001 From: HanishKVC Date: Sun, 19 May 2024 03:20:30 +0530 Subject: [PATCH] SimpleChat:JS: Allow for changing system prompt anytime for future --- .../server/public_simplechat/simplechat.js | 33 +++++++++++++++++-- 1 file changed, 30 insertions(+), 3 deletions(-) diff --git a/examples/server/public_simplechat/simplechat.js b/examples/server/public_simplechat/simplechat.js index d476dd69e..d7cf61bc2 100644 --- a/examples/server/public_simplechat/simplechat.js +++ b/examples/server/public_simplechat/simplechat.js @@ -21,6 +21,7 @@ class SimpleChat { * @type {{role: string, content: string}[]} */ this.xchat = []; + this.iLastSys = -1; } /** @@ -33,6 +34,9 @@ class SimpleChat { return false; } this.xchat.push( {role: role, content: content} ); + if (role == Roles.System) { + this.iLastSys = this.xchat.length - 1; + } return true; } @@ -96,10 +100,10 @@ class SimpleChat { /** - * Handle setting of system prompt. + * Handle setting of system prompt, but only at begining. * @param {HTMLInputElement} inputSystem */ -function handle_systemprompt(inputSystem) { +function handle_systemprompt_begin(inputSystem) { let sysPrompt = inputSystem.value; if (gChat.xchat.length == 0) { if (sysPrompt.length > 0) { @@ -118,6 +122,29 @@ function handle_systemprompt(inputSystem) { } } +/** + * Handle setting of system prompt, at any time. + * @param {HTMLInputElement} inputSystem + */ +function handle_systemprompt_anytime(inputSystem) { + let sysPrompt = inputSystem.value; + if (sysPrompt.length <= 0) { + return; + } + + if (gChat.iLastSys < 0) { + gChat.add(Roles.System, sysPrompt); + return; + } + + let lastSys = gChat.xchat[gChat.iLastSys].content; + if (lastSys !== sysPrompt) { + gChat.add(Roles.System, sysPrompt); + return; + } +} + + /** * Handle submit request by user * @param {HTMLInputElement} inputSystem @@ -127,7 +154,7 @@ function handle_systemprompt(inputSystem) { */ async function handle_submit(inputSystem, inputUser, divChat, apiEP) { - handle_systemprompt(inputSystem); + handle_systemprompt_anytime(inputSystem); let content = inputUser?.value; if (!gChat.add(Roles.User, content)) {