Skip to content

Commit 3791ad2

Browse files
authored
SimpleChat v3.1: Boolean chat request options in Settings UI, cache_prompt (#7950)
* SimpleChat: Allow for chat req bool options to be user controlled * SimpleChat: Allow user to control cache_prompt flag in request * SimpleChat: Add sample GUI images to readme file Show the chat screen and the settings screen * SimpleChat:Readme: Add quickstart block, title to image, cleanup * SimpleChat: RePosition contents of the Info and Settings UI Make it more logically structured and flow through. * SimpleChat: Rename to apiRequestOptions from chatRequestOptions So that it is not wrongly assumed that these request options are used only for chat/completions endpoint. Rather these are used for both the end points, so rename to match semantic better. * SimpleChat: Update image included with readme wrt settings ui * SimpleChat:ReadMe: Switch to webp screen image to reduce size
1 parent f702a90 commit 3791ad2

File tree

3 files changed

+68
-48
lines changed

3 files changed

+68
-48
lines changed

examples/server/public_simplechat/readme.md

Lines changed: 26 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,13 @@
33

44
by Humans for All.
55

6+
## quickstart
7+
8+
To run from the build dir
9+
10+
bin/llama-server -m path/model.gguf --path ../examples/server/public_simplechat
11+
12+
Continue reading for the details.
613

714
## overview
815

@@ -14,6 +21,8 @@ own system prompts.
1421
This allows seeing the generated text / ai-model response in oneshot at the end, after it is fully generated,
1522
or potentially as it is being generated, in a streamed manner from the server/ai-model.
1623

24+
![Chat and Settings screens](./simplechat_screens.webp "Chat and Settings screens")
25+
1726
Auto saves the chat session locally as and when the chat is progressing and inturn at a later time when you
1827
open SimpleChat, option is provided to restore the old chat session, if a matching one exists.
1928

@@ -170,17 +179,23 @@ It is attached to the document object. Some of these can also be updated using t
170179
The histogram/freq based trimming logic is currently tuned for english language wrt its
171180
is-it-a-alpabetic|numeral-char regex match logic.
172181

173-
chatRequestOptions - maintains the list of options/fields to send along with chat request,
182+
apiRequestOptions - maintains the list of options/fields to send along with api request,
174183
irrespective of whether /chat/completions or /completions endpoint.
175184

176185
If you want to add additional options/fields to send to the server/ai-model, and or
177186
modify the existing options value or remove them, for now you can update this global var
178187
using browser's development-tools/console.
179188

180-
For string and numeric fields in chatRequestOptions, including even those added by a user
181-
at runtime by directly modifying gMe.chatRequestOptions, setting ui entries will be auto
189+
For string, numeric and boolean fields in apiRequestOptions, including even those added by a
190+
user at runtime by directly modifying gMe.apiRequestOptions, setting ui entries will be auto
182191
created.
183192

193+
cache_prompt option supported by example/server is allowed to be controlled by user, so that
194+
any caching supported wrt system-prompt and chat history, if usable can get used. When chat
195+
history sliding window is enabled, cache_prompt logic may or may not kick in at the backend
196+
wrt same, based on aspects related to model, positional encoding, attention mechanism etal.
197+
However system prompt should ideally get the benefit of caching.
198+
184199
headers - maintains the list of http headers sent when request is made to the server. By default
185200
Content-Type is set to application/json. Additionally Authorization entry is provided, which can
186201
be set if needed using the settings ui.
@@ -197,10 +212,10 @@ It is attached to the document object. Some of these can also be updated using t
197212
>0 : Send the latest chat history from the latest system prompt, limited to specified cnt.
198213

199214

200-
By using gMe's iRecentUserMsgCnt and chatRequestOptions.max_tokens one can try to control the
201-
implications of loading of the ai-model's context window by chat history, wrt chat response to
202-
some extent in a simple crude way. You may also want to control the context size enabled when
203-
the server loads ai-model, on the server end.
215+
By using gMe's iRecentUserMsgCnt and apiRequestOptions.max_tokens/n_predict one can try to control
216+
the implications of loading of the ai-model's context window by chat history, wrt chat response to
217+
some extent in a simple crude way. You may also want to control the context size enabled when the
218+
server loads ai-model, on the server end.
204219

205220

206221
Sometimes the browser may be stuborn with caching of the file, so your updates to html/css/js
@@ -237,12 +252,12 @@ also be started with a model context size of 1k or more, to be on safe side.
237252
internal n_predict, for now add the same here on the client side, maybe later add max_tokens
238253
to /completions endpoint handling code on server side.
239254

240-
NOTE: One may want to experiment with frequency/presence penalty fields in chatRequestOptions
241-
wrt the set of fields sent to server along with the user query. To check how the model behaves
255+
NOTE: One may want to experiment with frequency/presence penalty fields in apiRequestOptions
256+
wrt the set of fields sent to server along with the user query, to check how the model behaves
242257
wrt repeatations in general in the generated text response.
243258

244259
A end-user can change these behaviour by editing gMe from browser's devel-tool/console or by
245-
using the providing settings ui.
260+
using the provided settings ui (for settings exposed through the ui).
246261

247262

248263
### OpenAi / Equivalent API WebService
@@ -253,7 +268,7 @@ for a minimal chatting experimentation by setting the below.
253268
* the baseUrl in settings ui
254269
* https://api.openai.com/v1 or similar
255270

256-
* Wrt request body - gMe.chatRequestOptions
271+
* Wrt request body - gMe.apiRequestOptions
257272
* model (settings ui)
258273
* any additional fields if required in future
259274

examples/server/public_simplechat/simplechat.js

Lines changed: 42 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -222,8 +222,8 @@ class SimpleChat {
222222
* @param {Object} obj
223223
*/
224224
request_jsonstr_extend(obj) {
225-
for(let k in gMe.chatRequestOptions) {
226-
obj[k] = gMe.chatRequestOptions[k];
225+
for(let k in gMe.apiRequestOptions) {
226+
obj[k] = gMe.apiRequestOptions[k];
227227
}
228228
if (gMe.bStream) {
229229
obj["stream"] = true;
@@ -740,11 +740,12 @@ class Me {
740740
"Authorization": "", // Authorization: Bearer OPENAI_API_KEY
741741
}
742742
// Add needed fields wrt json object to be sent wrt LLM web services completions endpoint.
743-
this.chatRequestOptions = {
743+
this.apiRequestOptions = {
744744
"model": "gpt-3.5-turbo",
745745
"temperature": 0.7,
746746
"max_tokens": 1024,
747747
"n_predict": 1024,
748+
"cache_prompt": false,
748749
//"frequency_penalty": 1.2,
749750
//"presence_penalty": 1.2,
750751
};
@@ -800,51 +801,55 @@ class Me {
800801

801802
ui.el_create_append_p(`bStream:${this.bStream}`, elDiv);
802803

803-
ui.el_create_append_p(`bCompletionFreshChatAlways:${this.bCompletionFreshChatAlways}`, elDiv);
804-
805-
ui.el_create_append_p(`bCompletionInsertStandardRolePrefix:${this.bCompletionInsertStandardRolePrefix}`, elDiv);
806-
807804
ui.el_create_append_p(`bTrimGarbage:${this.bTrimGarbage}`, elDiv);
808805

806+
ui.el_create_append_p(`ApiEndPoint:${this.apiEP}`, elDiv);
807+
809808
ui.el_create_append_p(`iRecentUserMsgCnt:${this.iRecentUserMsgCnt}`, elDiv);
810809

811-
ui.el_create_append_p(`ApiEndPoint:${this.apiEP}`, elDiv);
810+
ui.el_create_append_p(`bCompletionFreshChatAlways:${this.bCompletionFreshChatAlways}`, elDiv);
811+
812+
ui.el_create_append_p(`bCompletionInsertStandardRolePrefix:${this.bCompletionInsertStandardRolePrefix}`, elDiv);
812813

813814
}
814815

815-
ui.el_create_append_p(`chatRequestOptions:${JSON.stringify(this.chatRequestOptions, null, " - ")}`, elDiv);
816+
ui.el_create_append_p(`apiRequestOptions:${JSON.stringify(this.apiRequestOptions, null, " - ")}`, elDiv);
816817
ui.el_create_append_p(`headers:${JSON.stringify(this.headers, null, " - ")}`, elDiv);
817818

818819
}
819820

820821
/**
821-
* Auto create ui input elements for fields in ChatRequestOptions
822+
* Auto create ui input elements for fields in apiRequestOptions
822823
* Currently supports text and number field types.
823824
* @param {HTMLDivElement} elDiv
824825
*/
825-
show_settings_chatrequestoptions(elDiv) {
826+
show_settings_apirequestoptions(elDiv) {
826827
let typeDict = {
827828
"string": "text",
828829
"number": "number",
829830
};
830831
let fs = document.createElement("fieldset");
831832
let legend = document.createElement("legend");
832-
legend.innerText = "ChatRequestOptions";
833+
legend.innerText = "ApiRequestOptions";
833834
fs.appendChild(legend);
834835
elDiv.appendChild(fs);
835-
for(const k in this.chatRequestOptions) {
836-
let val = this.chatRequestOptions[k];
836+
for(const k in this.apiRequestOptions) {
837+
let val = this.apiRequestOptions[k];
837838
let type = typeof(val);
838-
if (!((type == "string") || (type == "number"))) {
839-
continue;
839+
if (((type == "string") || (type == "number"))) {
840+
let inp = ui.el_creatediv_input(`Set${k}`, k, typeDict[type], this.apiRequestOptions[k], (val)=>{
841+
if (type == "number") {
842+
val = Number(val);
843+
}
844+
this.apiRequestOptions[k] = val;
845+
});
846+
fs.appendChild(inp.div);
847+
} else if (type == "boolean") {
848+
let bbtn = ui.el_creatediv_boolbutton(`Set{k}`, k, {true: "true", false: "false"}, val, (userVal)=>{
849+
this.apiRequestOptions[k] = userVal;
850+
});
851+
fs.appendChild(bbtn.div);
840852
}
841-
let inp = ui.el_creatediv_input(`Set${k}`, k, typeDict[type], this.chatRequestOptions[k], (val)=>{
842-
if (type == "number") {
843-
val = Number(val);
844-
}
845-
this.chatRequestOptions[k] = val;
846-
});
847-
fs.appendChild(inp.div);
848853
}
849854
}
850855

@@ -870,32 +875,32 @@ class Me {
870875
});
871876
elDiv.appendChild(bb.div);
872877

873-
bb = ui.el_creatediv_boolbutton("SetCompletionFreshChatAlways", "CompletionFreshChatAlways", {true: "[+] yes fresh", false: "[-] no, with history"}, this.bCompletionFreshChatAlways, (val)=>{
874-
this.bCompletionFreshChatAlways = val;
878+
bb = ui.el_creatediv_boolbutton("SetTrimGarbage", "TrimGarbage", {true: "[+] yes trim", false: "[-] dont trim"}, this.bTrimGarbage, (val)=>{
879+
this.bTrimGarbage = val;
875880
});
876881
elDiv.appendChild(bb.div);
877882

878-
bb = ui.el_creatediv_boolbutton("SetCompletionInsertStandardRolePrefix", "CompletionInsertStandardRolePrefix", {true: "[+] yes insert", false: "[-] dont insert"}, this.bCompletionInsertStandardRolePrefix, (val)=>{
879-
this.bCompletionInsertStandardRolePrefix = val;
880-
});
881-
elDiv.appendChild(bb.div);
883+
this.show_settings_apirequestoptions(elDiv);
882884

883-
bb = ui.el_creatediv_boolbutton("SetTrimGarbage", "TrimGarbage", {true: "[+] yes trim", false: "[-] dont trim"}, this.bTrimGarbage, (val)=>{
884-
this.bTrimGarbage = val;
885+
let sel = ui.el_creatediv_select("SetApiEP", "ApiEndPoint", ApiEP.Type, this.apiEP, (val)=>{
886+
this.apiEP = ApiEP.Type[val];
885887
});
886-
elDiv.appendChild(bb.div);
888+
elDiv.appendChild(sel.div);
887889

888-
let sel = ui.el_creatediv_select("SetChatHistoryInCtxt", "ChatHistoryInCtxt", this.sRecentUserMsgCnt, this.iRecentUserMsgCnt, (val)=>{
890+
sel = ui.el_creatediv_select("SetChatHistoryInCtxt", "ChatHistoryInCtxt", this.sRecentUserMsgCnt, this.iRecentUserMsgCnt, (val)=>{
889891
this.iRecentUserMsgCnt = this.sRecentUserMsgCnt[val];
890892
});
891893
elDiv.appendChild(sel.div);
892894

893-
sel = ui.el_creatediv_select("SetApiEP", "ApiEndPoint", ApiEP.Type, this.apiEP, (val)=>{
894-
this.apiEP = ApiEP.Type[val];
895+
bb = ui.el_creatediv_boolbutton("SetCompletionFreshChatAlways", "CompletionFreshChatAlways", {true: "[+] yes fresh", false: "[-] no, with history"}, this.bCompletionFreshChatAlways, (val)=>{
896+
this.bCompletionFreshChatAlways = val;
895897
});
896-
elDiv.appendChild(sel.div);
898+
elDiv.appendChild(bb.div);
897899

898-
this.show_settings_chatrequestoptions(elDiv);
900+
bb = ui.el_creatediv_boolbutton("SetCompletionInsertStandardRolePrefix", "CompletionInsertStandardRolePrefix", {true: "[+] yes insert", false: "[-] dont insert"}, this.bCompletionInsertStandardRolePrefix, (val)=>{
901+
this.bCompletionInsertStandardRolePrefix = val;
902+
});
903+
elDiv.appendChild(bb.div);
899904

900905
}
901906

Binary file not shown.

0 commit comments

Comments
 (0)