SimpleChat:JS: Move system prompt begin/anytime into SimpleChat

This commit is contained in:
HanishKVC 2024-05-20 22:59:32 +05:30
parent 9b97feab45
commit 5c1a9f4d8b

View file

@ -96,55 +96,55 @@ class SimpleChat {
return this.request_jsonstr(req); return this.request_jsonstr(req);
} }
} /**
* Allow setting of system prompt, but only at begining.
* @param {string} sysPrompt
/** * @param {string} msgTag
* Handle setting of system prompt, but only at begining.
* @param {HTMLInputElement} inputSystem
*/ */
function handle_systemprompt_begin(inputSystem) { add_system_begin(sysPrompt, msgTag) {
let sysPrompt = inputSystem.value; if (this.xchat.length == 0) {
if (gChat.xchat.length == 0) {
if (sysPrompt.length > 0) { if (sysPrompt.length > 0) {
gChat.add(Roles.System, sysPrompt); return this.add(Roles.System, sysPrompt);
} }
} else { } else {
if (sysPrompt.length > 0) { if (sysPrompt.length > 0) {
if (gChat.xchat[0].role !== Roles.System) { if (this.xchat[0].role !== Roles.System) {
console.error("ERRR:HandleSubmit:You need to specify system prompt before any user query, ignoring..."); console.error(`ERRR:SimpleChat:${msgTag}:You need to specify system prompt before any user query, ignoring...`);
} else { } else {
if (gChat.xchat[0].content !== sysPrompt) { if (this.xchat[0].content !== sysPrompt) {
console.error("ERRR:HandleSubmit:You cant change system prompt, mid way through, ignoring..."); console.error(`ERRR:SimpleChat:${msgTag}:You cant change system prompt, mid way through, ignoring...`);
} }
} }
} }
} }
} return false;
}
/** /**
* Handle setting of system prompt, at any time. * Allow setting of system prompt, at any time.
* @param {HTMLInputElement} inputSystem * @param {string} sysPrompt
* @param {string} msgTag
*/ */
function handle_systemprompt_anytime(inputSystem) { add_system_anytime(sysPrompt, msgTag) {
let sysPrompt = inputSystem.value;
if (sysPrompt.length <= 0) { if (sysPrompt.length <= 0) {
return; return false;
} }
if (gChat.iLastSys < 0) { if (this.iLastSys < 0) {
gChat.add(Roles.System, sysPrompt); return this.add(Roles.System, sysPrompt);
return;
} }
let lastSys = gChat.xchat[gChat.iLastSys].content; let lastSys = this.xchat[this.iLastSys].content;
if (lastSys !== sysPrompt) { if (lastSys !== sysPrompt) {
gChat.add(Roles.System, sysPrompt); return this.add(Roles.System, sysPrompt);
return;
} }
return false;
}
} }
/** /**
* Handle submit request by user * Handle submit request by user
* @param {HTMLInputElement} inputSystem * @param {HTMLInputElement} inputSystem
@ -154,7 +154,7 @@ function handle_systemprompt_anytime(inputSystem) {
*/ */
async function handle_submit(inputSystem, inputUser, divChat, apiEP) { async function handle_submit(inputSystem, inputUser, divChat, apiEP) {
handle_systemprompt_anytime(inputSystem); gChat.add_system_anytime(inputSystem.value, "0");
let content = inputUser?.value; let content = inputUser?.value;
if (!gChat.add(Roles.User, content)) { if (!gChat.add(Roles.User, content)) {