SimpleChat:Move extracting assistant response to SimpleChat class

so also the trimming of garbage.
This commit is contained in:
HanishKVC 2024-05-27 20:15:41 +05:30
parent b2c10b960d
commit 269cf3f596

View file

@ -206,6 +206,34 @@ class SimpleChat {
} }
} }
/**
* Extract the ai-model/assistant's response from the http response got.
* Optionally trim the message wrt any garbage at the end.
* @param {any} respBody
* @param {string} apiEP
*/
response_extract(respBody, apiEP) {
let theResp = {
assistant: "",
trimmed: "",
}
if (apiEP == ApiEP.Type.Chat) {
theResp.assistant = respBody["choices"][0]["message"]["content"];
} else {
try {
theResp.assistant = respBody["choices"][0]["text"];
} catch {
theResp.assistant = respBody["content"];
}
}
if (gMe.bTrimGarbage) {
let origMsg = theResp.assistant;
theResp.assistant = du.trim_hist_garbage_at_end_loop(theResp.assistant, 8, 24, 72);
theResp.trimmed = origMsg.substring(theResp.assistant.length);
}
return theResp;
}
/** /**
* Allow setting of system prompt, but only at begining. * Allow setting of system prompt, but only at begining.
* @param {string} sysPrompt * @param {string} sysPrompt
@ -447,27 +475,12 @@ class MultiChatUI {
let respBody = await resp.json(); let respBody = await resp.json();
//let respBody = await this.read_json_early(resp); //let respBody = await this.read_json_early(resp);
console.debug(`DBUG:SimpleChat:MCUI:${chatId}:HandleUserSubmit:RespBody:${JSON.stringify(respBody)}`); console.debug(`DBUG:SimpleChat:MCUI:${chatId}:HandleUserSubmit:RespBody:${JSON.stringify(respBody)}`);
let assistantMsg; let theResp = chat.response_extract(respBody, apiEP);
let trimmedMsg = ""; chat.add(Roles.Assistant, theResp.assistant);
if (apiEP == ApiEP.Type.Chat) {
assistantMsg = respBody["choices"][0]["message"]["content"];
} else {
try {
assistantMsg = respBody["choices"][0]["text"];
} catch {
assistantMsg = respBody["content"];
}
}
if (gMe.bTrimGarbage) {
let origMsg = assistantMsg;
assistantMsg = du.trim_hist_garbage_at_end_loop(assistantMsg, 8, 24, 72);
trimmedMsg = origMsg.substring(assistantMsg.length);
}
chat.add(Roles.Assistant, assistantMsg);
if (chatId == this.curChatId) { if (chatId == this.curChatId) {
chat.show(this.elDivChat); chat.show(this.elDivChat);
if (trimmedMsg.length > 0) { if (theResp.trimmed.length > 0) {
let p = ui.el_create_append_p(`TRIMMED:${trimmedMsg}`, this.elDivChat); let p = ui.el_create_append_p(`TRIMMED:${theResp.trimmed}`, this.elDivChat);
p.className="role-trim"; p.className="role-trim";
} }
} else { } else {