From bb0f0c8a9ab481353b1dc24b03eb4843d79c4e94 Mon Sep 17 00:00:00 2001 From: HanishKVC Date: Fri, 31 May 2024 20:04:50 +0530 Subject: [PATCH] SimpleChat: AutoCreate ChatRequestOptions settings to an extent --- examples/server/public_simplechat/readme.md | 4 ++ .../server/public_simplechat/simplechat.js | 38 ++++++++++++++++--- 2 files changed, 37 insertions(+), 5 deletions(-) diff --git a/examples/server/public_simplechat/readme.md b/examples/server/public_simplechat/readme.md index afde9d7eb..36a46885d 100644 --- a/examples/server/public_simplechat/readme.md +++ b/examples/server/public_simplechat/readme.md @@ -177,6 +177,10 @@ It is attached to the document object. Some of these can also be updated using t modify the existing options value or remove them, for now you can update this global var using browser's development-tools/console. + For string and numeric fields in chatRequestOptions, including even those added by a user + at runtime by directly modifying gMe.chatRequestOptions, setting ui entries will be auto + created. + headers - maintains the list of http headers sent when request is made to the server. By default Content-Type is set to application/json. Additionally Authorization entry is provided, which can be set if needed using the settings ui. diff --git a/examples/server/public_simplechat/simplechat.js b/examples/server/public_simplechat/simplechat.js index d727e2d6b..25afb2564 100644 --- a/examples/server/public_simplechat/simplechat.js +++ b/examples/server/public_simplechat/simplechat.js @@ -817,6 +817,37 @@ class Me { } + /** + * Auto create ui input elements for fields in ChatRequestOptions + * Currently supports text and number field types. + * @param {HTMLDivElement} elDiv + */ + show_settings_chatrequestoptions(elDiv) { + let typeDict = { + "string": "text", + "number": "number", + }; + let fs = document.createElement("fieldset"); + let legend = document.createElement("legend"); + legend.innerText = "ChatRequestOptions"; + fs.appendChild(legend); + elDiv.appendChild(fs); + for(const k in this.chatRequestOptions) { + let val = this.chatRequestOptions[k]; + let type = typeof(val); + if (!((type == "string") || (type == "number"))) { + continue; + } + let inp = ui.el_creatediv_input(`Set${k}`, k, typeDict[type], this.chatRequestOptions[k], (val)=>{ + if (type == "number") { + val = Number(val); + } + this.chatRequestOptions[k] = val; + }); + fs.appendChild(inp.div); + } + } + /** * Show settings ui for configurable parameters, in the passed Div element. * @param {HTMLDivElement} elDiv @@ -834,11 +865,6 @@ class Me { inp.el.placeholder = "Bearer OPENAI_API_KEY"; elDiv.appendChild(inp.div); - inp = ui.el_creatediv_input("SetModel", "Model", "text", this.chatRequestOptions["model"], (val)=>{ - this.chatRequestOptions["model"] = val; - }); - elDiv.appendChild(inp.div); - let bb = ui.el_creatediv_boolbutton("SetStream", "Stream", {true: "[+] yes stream", false: "[-] do oneshot"}, this.bStream, (val)=>{ this.bStream = val; }); @@ -869,6 +895,8 @@ class Me { }); elDiv.appendChild(sel.div); + this.show_settings_chatrequestoptions(elDiv); + } }