From 269cf3f59675b7bac53c7714d989506264c71c8e Mon Sep 17 00:00:00 2001 From: HanishKVC Date: Mon, 27 May 2024 20:15:41 +0530 Subject: [PATCH] SimpleChat:Move extracting assistant response to SimpleChat class so also the trimming of garbage. --- .../server/public_simplechat/simplechat.js | 51 ++++++++++++------- 1 file changed, 32 insertions(+), 19 deletions(-) diff --git a/examples/server/public_simplechat/simplechat.js b/examples/server/public_simplechat/simplechat.js index ff5e5bce4..37ae40a3a 100644 --- a/examples/server/public_simplechat/simplechat.js +++ b/examples/server/public_simplechat/simplechat.js @@ -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. * @param {string} sysPrompt @@ -447,27 +475,12 @@ class MultiChatUI { let respBody = await resp.json(); //let respBody = await this.read_json_early(resp); console.debug(`DBUG:SimpleChat:MCUI:${chatId}:HandleUserSubmit:RespBody:${JSON.stringify(respBody)}`); - let assistantMsg; - let trimmedMsg = ""; - 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); + let theResp = chat.response_extract(respBody, apiEP); + chat.add(Roles.Assistant, theResp.assistant); if (chatId == this.curChatId) { chat.show(this.elDivChat); - if (trimmedMsg.length > 0) { - let p = ui.el_create_append_p(`TRIMMED:${trimmedMsg}`, this.elDivChat); + if (theResp.trimmed.length > 0) { + let p = ui.el_create_append_p(`TRIMMED:${theResp.trimmed}`, this.elDivChat); p.className="role-trim"; } } else {