From 7297dda3765082d9f19f6a34906a5bc8ae776ee7 Mon Sep 17 00:00:00 2001 From: HanishKVC Date: Tue, 21 May 2024 17:24:06 +0530 Subject: [PATCH] SimpleChat: Take care of system prompt Helper to get the latest system prompt and inturn use same to set the system prompt ui, when switching. Ensure that system prompt is set if and when enter key is pressed. --- .../server/public_simplechat/simplechat.js | 24 +++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/examples/server/public_simplechat/simplechat.js b/examples/server/public_simplechat/simplechat.js index 57876cf8b..ce7d77275 100644 --- a/examples/server/public_simplechat/simplechat.js +++ b/examples/server/public_simplechat/simplechat.js @@ -141,6 +141,17 @@ class SimpleChat { return false; } + /** + * Retrieve the latest system prompt. + */ + get_system() { + let sysPrompt = this.xchat[this.iLastSys].content; + if (!sysPrompt) { + sysPrompt = ""; + } + return sysPrompt; + } + } @@ -194,6 +205,7 @@ class MultiChatUI { * @param {boolean} [bSwitchSession=false] */ setup_ui(defaultChatId, bSwitchSession=false) { + this.curChatId = defaultChatId; if (bSwitchSession) { this.handle_session_switch(this.curChatId); @@ -214,6 +226,17 @@ class MultiChatUI { ev.preventDefault(); } }); + + this.elInSystem.addEventListener("keyup", (ev)=> { + // allow user to insert enter into the system prompt using shift+enter. + // while just pressing enter key will lead to setting the system prompt. + if ((ev.key === "Enter") && (!ev.shiftKey)) { + let chat = this.simpleChats[this.curChatId]; + chat.add_system_anytime(this.elInSystem.value, this.curChatId); + ev.preventDefault(); + } + }); + } /** @@ -330,6 +353,7 @@ class MultiChatUI { console.error(`ERRR:MCUI:Session:${chatId} missing...`); return; } + this.elInSystem.value = chat.get_system(); this.elInUser.value = ""; chat.show(this.elDivChat); this.elInUser.focus();