-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathUI.h
2391 lines (1967 loc) · 107 KB
/
UI.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#pragma once
#include "imgui.h"
#include "misc/cpp/imgui_stdlib.h"
#include <stdio.h> // printf, fprintf
#include <stdlib.h> // abort
#include <SDL.h>
#include "tinyfiledialogs.h"
#include <thread_chat.h>
#if defined(GGML_USE_CLBLAST)
# define clblast 1
#else
# define clblast 0
#endif
///////////////////////////////////////
// Helper from the main demo to display a little (?) mark which shows a tooltip when hovered.
// In your own code you may want to display an actual icon if you are using a merged icon fonts (see docs/FONTS.md)
static void HelpMarker(const char* desc)
{
ImGui::TextDisabled("(?)");
if (ImGui::BeginItemTooltip())
{
ImGui::PushTextWrapPos(ImGui::GetFontSize() * 35.0f);
ImGui::TextUnformatted(desc);
ImGui::PopTextWrapPos();
ImGui::EndTooltip();
}
}
static void HelpTooltip(const char* desc)
{
if (ImGui::BeginItemTooltip())
{
ImGui::PushTextWrapPos(ImGui::GetFontSize() * 35.0f);
ImGui::TextUnformatted(desc);
ImGui::PopTextWrapPos();
ImGui::EndTooltip();
}
}
static void TextWTooltip(const char* name, const char* desc)
{
ImGui::Text(name);
if (ImGui::BeginItemTooltip())
{
ImGui::PushTextWrapPos(ImGui::GetFontSize() * 35.0f);
ImGui::TextUnformatted(desc);
ImGui::PopTextWrapPos();
ImGui::EndTooltip();
}
}
// theme!
void retroTheme(){
// Setup style
ImGui::StyleColorsClassic();
ImGuiStyle& style = ImGui::GetStyle();
style.Colors[ImGuiCol_Text] = ImVec4(0.31f, 0.25f, 0.24f, 1.00f);
style.Colors[ImGuiCol_WindowBg] = ImVec4(0.94f, 0.94f, 0.94f, 1.00f);
style.Colors[ImGuiCol_MenuBarBg] = ImVec4(0.74f, 0.74f, 0.94f, 1.00f);
style.Colors[ImGuiCol_ChildBg] = ImVec4(0.68f, 0.68f, 0.68f, 0.00f);
style.Colors[ImGuiCol_Border] = ImVec4(0.50f, 0.50f, 0.50f, 0.60f);
style.Colors[ImGuiCol_BorderShadow] = ImVec4(0.00f, 0.00f, 0.00f, 0.00f);
style.Colors[ImGuiCol_FrameBg] = ImVec4(0.62f, 0.70f, 0.72f, 0.56f);
style.Colors[ImGuiCol_FrameBgHovered] = ImVec4(0.95f, 0.33f, 0.14f, 0.47f);
style.Colors[ImGuiCol_FrameBgActive] = ImVec4(0.97f, 0.31f, 0.13f, 0.81f);
style.Colors[ImGuiCol_TitleBg] = ImVec4(0.42f, 0.75f, 1.00f, 0.53f);
style.Colors[ImGuiCol_TitleBgCollapsed] = ImVec4(0.40f, 0.65f, 0.80f, 0.20f);
style.Colors[ImGuiCol_ScrollbarBg] = ImVec4(0.40f, 0.62f, 0.80f, 0.15f);
style.Colors[ImGuiCol_ScrollbarGrab] = ImVec4(0.39f, 0.64f, 0.80f, 0.30f);
style.Colors[ImGuiCol_ScrollbarGrabHovered] = ImVec4(0.28f, 0.67f, 0.80f, 0.59f);
style.Colors[ImGuiCol_ScrollbarGrabActive] = ImVec4(0.25f, 0.48f, 0.53f, 0.67f);
//style.Colors[ImGuiCol_ComboBg] = ImVec4(0.89f, 0.98f, 1.00f, 0.99f);
style.Colors[ImGuiCol_Tab] = ImVec4(0.89f, 0.98f, 1.00f, 0.99f);
style.Colors[ImGuiCol_CheckMark] = ImVec4(0.48f, 0.47f, 0.47f, 0.71f);
style.Colors[ImGuiCol_SliderGrabActive] = ImVec4(0.31f, 0.47f, 0.99f, 1.00f);
style.Colors[ImGuiCol_Button] = ImVec4(1.00f, 0.79f, 0.18f, 0.78f);
style.Colors[ImGuiCol_ButtonHovered] = ImVec4(0.42f, 0.82f, 1.00f, 0.81f);
style.Colors[ImGuiCol_ButtonActive] = ImVec4(0.72f, 1.00f, 1.00f, 0.86f);
style.Colors[ImGuiCol_Header] = ImVec4(0.65f, 0.78f, 0.84f, 0.80f);
style.Colors[ImGuiCol_HeaderHovered] = ImVec4(0.75f, 0.88f, 0.94f, 0.80f);
style.Colors[ImGuiCol_HeaderActive] = ImVec4(0.55f, 0.68f, 0.74f, 0.80f);//ImVec4(0.46f, 0.84f, 0.90f, 1.00f);
style.Colors[ImGuiCol_ResizeGrip] = ImVec4(0.60f, 0.60f, 0.80f, 0.30f);
style.Colors[ImGuiCol_ResizeGripHovered] = ImVec4(1.00f, 1.00f, 1.00f, 0.60f);
style.Colors[ImGuiCol_ResizeGripActive] = ImVec4(1.00f, 1.00f, 1.00f, 0.90f);
//style.Colors[ImGuiCol_CloseButton] = ImVec4(0.41f, 0.75f, 0.98f, 0.50f);
//style.Colors[ImGuiCol_CloseButtonHovered] = ImVec4(1.00f, 0.47f, 0.41f, 0.60f);
//style.Colors[ImGuiCol_CloseButtonActive] = ImVec4(1.00f, 0.16f, 0.00f, 1.00f);
style.Colors[ImGuiCol_TextSelectedBg] = ImVec4(1.00f, 0.99f, 0.54f, 0.43f);
style.Colors[ImGuiCol_PopupBg] = ImVec4(0.82f, 0.92f, 1.00f, 0.90f);
style.Alpha = 1.0f;
//style.WindowFillAlphaDefault = 1.0f;
style.FrameRounding = 4;
style.IndentSpacing = 12.0f;
}
// utils for configs
static void sanitizePath(std::string& path){
int slashes = path.rfind("\\");
while (slashes != path.npos){
path.replace(slashes,1,"/");
slashes = path.rfind("\\");
}
}
std::string getStringFromJson(std::string fimeName, std::string stringName){
nlohmann::json config;
std::fstream o1(fimeName);
if (o1.is_open()) {
o1 >> std::setw(4) >> config;
o1.close();
} else {
return "NULL";
}
if (config.contains(stringName)) {
if(config[stringName].is_string()) return config[stringName].get<std::string>();
}
return "NULL";
}
static void sliderTemp(float& temp, float& default_temp)
{
{
if (ImGui::Button("-##temp")) {
temp -= 0.001f;
}
ImGui::SameLine();
if (ImGui::Button("+##temp")) {
temp += 0.001f;
}
ImGui::SameLine();
}
ImGui::SliderFloat("temp", &temp, 0.0f, 2.0f);
if (ImGui::BeginPopupContextItem("temp"))
{
if (ImGui::Selectable("Reset to default")){
temp = default_temp;
}
ImGui::EndPopup();
} ImGui::SameLine(); HelpMarker( ("Adjust the randomness of the generated text. Lower means more robotic. Default: " + std::to_string(default_temp)).c_str());
}
static void sliderTopK(int& top_k, int& default_top_k)
{
{
if (ImGui::Button("-##top_k")) {
--top_k;
}
ImGui::SameLine();
if (ImGui::Button("+##top_k")) {
top_k++;
}
ImGui::SameLine();
}
ImGui::SliderInt("top_k", &top_k, 0, 100);
if (ImGui::BeginPopupContextItem("top_k"))
{
if (ImGui::Selectable("Reset to default")){
top_k = default_top_k;
}
ImGui::EndPopup();
} ImGui::SameLine(); HelpMarker(("Top-k sampling. Selects the next token only from the top k most likely tokens predicted by the model. It helps reduce the risk of generating low-probability or nonsensical tokens, but it may also limit the diversity of the output. Default: " + std::to_string(default_top_k)).c_str());
}
static void sliderTopP(float& top_p, float& default_top_p){
{
if (ImGui::Button("-##top_p")) {
top_p -= 0.001f;
}
ImGui::SameLine();
if (ImGui::Button("+##top_p")) {
top_p += 0.001f;
}
ImGui::SameLine();
}
ImGui::SliderFloat("top_p", &top_p, 0.5f, 1.0f);
if (ImGui::BeginPopupContextItem("top_p"))
{
if (ImGui::Selectable("Reset to default")){
top_p = default_top_p;
}
ImGui::EndPopup();
} ImGui::SameLine(); HelpMarker(("Top-p (Nucleus) sampling. Selects the next token from a subset of tokens that together have a cumulative probability of at least p. Higher values will lead to more diverse text, lower values will generate more focused and conservative text. Default: " + std::to_string(default_top_p)).c_str());
}
static void sliderRepeatPen(float& repeat_penalty, float& default_repeat_penalty){
{
if (ImGui::Button("-##repeat_penalty")) {
repeat_penalty -= 0.001f;
}
ImGui::SameLine();
if (ImGui::Button("+##repeat_penalty")) {
repeat_penalty += 0.001f;
}
ImGui::SameLine();
}
ImGui::SliderFloat("repeat_penalty", &repeat_penalty, 0.0f, 2.0f);
if (ImGui::BeginPopupContextItem("repeat_penalty"))
{
if (ImGui::Selectable("Reset to default")){
repeat_penalty = default_repeat_penalty;
}
ImGui::EndPopup();
} ImGui::SameLine(); HelpMarker(("Controls the repetition of token sequences in the generated text. Default: " + std::to_string(default_repeat_penalty)).c_str());
}
static void sliderFrequencyPen(float& frequency_penalty, float& default_frequency_penalty){
{
if (ImGui::Button("-##frequency_penalty")) {
frequency_penalty -= 0.001f;
}
ImGui::SameLine();
if (ImGui::Button("+##frequency_penalty")) {
frequency_penalty += 0.001f;
}
ImGui::SameLine();
}
ImGui::SliderFloat("frequency_penalty", &frequency_penalty, 0.0f, 2.0f);
if (ImGui::BeginPopupContextItem("frequency_penalty"))
{
if (ImGui::Selectable("Reset to default")){
frequency_penalty = default_frequency_penalty;
}
ImGui::EndPopup();
} ImGui::SameLine(); HelpMarker(("Descreases the chance of repeating the same lines in the output. Affects the immediate result. Default: " + std::to_string(default_frequency_penalty)).c_str());
}
static void sliderPresencePen(float& presence_penalty, float& default_presence_penalty){
{
if (ImGui::Button("-##presence_penalty")) {
presence_penalty -= 0.001f;
}
ImGui::SameLine();
if (ImGui::Button("+##presence_penalty")) {
presence_penalty += 0.001f;
}
ImGui::SameLine();
}
ImGui::SliderFloat("presence_penalty", &presence_penalty, 0.0f, 2.0f);
if (ImGui::BeginPopupContextItem("presence_penalty"))
{
if (ImGui::Selectable("Reset to default")){
presence_penalty = default_presence_penalty;
}
ImGui::EndPopup();
} ImGui::SameLine(); HelpMarker(("Descreases the chance of repeating the same lines in the output. Affects the entrire dialog. Default: " + std::to_string(default_presence_penalty)).c_str());
}
static void sliderMinP(float& min_p, float& default_min_p){
{
if (ImGui::Button("-##min_p")) {
min_p -= 0.001f;
}
ImGui::SameLine();
if (ImGui::Button("+##min_p")) {
min_p += 0.001f;
}
ImGui::SameLine();
}
ImGui::SliderFloat("min_p", &min_p, 0.00f, 0.99f);
if (ImGui::BeginPopupContextItem("min_p"))
{
if (ImGui::Selectable("Reset to default")){
min_p = default_min_p;
}
ImGui::EndPopup();
} ImGui::SameLine(); HelpMarker(("New proposed sampling method. Every possible token has a probability percentage attached to it that we will be measuring for consideration. The base min p value represents the starting required percentage. This gets scaled by the top token in the entire list's probability. For example, 0.05 = only include tokens that are at least 5% probable. Default: " + std::to_string(default_min_p)).c_str());
}
static void sliderTfsZ(float& tfs_z, float& default_tfs_z){
{
if (ImGui::Button("-##tfs_z")) {
tfs_z -= 0.001f;
}
ImGui::SameLine();
if (ImGui::Button("+##tfs_z")) {
tfs_z += 0.001f;
}
ImGui::SameLine();
}
ImGui::SliderFloat("tfs_z", &tfs_z, 0.5f, 1.0f); ImGui::SameLine();
if (ImGui::BeginPopupContextItem("tfs_z"))
{
if (ImGui::Selectable("Reset to default")){
tfs_z = default_tfs_z;
}
ImGui::EndPopup();
} HelpMarker(("Tail free sampling, parameter z. TFS filters out logits based on the second derivative of their probabilities. Adding tokens is stopped after the sum of the second derivatives reaches the parameter z. In short: TFS looks how quickly the probabilities of the tokens decrease and cuts off the tail of unlikely tokens using the parameter z. Default: " + std::to_string(default_tfs_z)).c_str());
}
static void sliderTypicalP(float& typical_p, float& default_typical_p){
{
if (ImGui::Button("-##typical_p")) {
typical_p -= 0.001f;
}
ImGui::SameLine();
if (ImGui::Button("+##typical_p")) {
typical_p += 0.001f;
}
ImGui::SameLine();
}
ImGui::SliderFloat("typical_p", &typical_p, 0.5f, 1.0f);
if (ImGui::BeginPopupContextItem("typical_p"))
{
if (ImGui::Selectable("Reset to default")){
typical_p = default_typical_p;
}
ImGui::EndPopup();
} ImGui::SameLine(); HelpMarker(("Locally typical sampling, parameter p. Promotes the generation of contextually coherent and diverse text by sampling tokens that are typical or expected based on the surrounding context. Default: " + std::to_string(default_typical_p)).c_str());
}
static void sliderMirostat(int& mirostat, int& default_mirostat){
{
if (ImGui::Button("-##mirostat")) {
if (mirostat > 0) --mirostat;
}
ImGui::SameLine();
if (ImGui::Button("+##mirostat")) {
if (mirostat < 2) mirostat++;
}
ImGui::SameLine();
}
ImGui::SliderInt("mirostat", &mirostat, 0, 2); ImGui::SameLine();
if (ImGui::BeginPopupContextItem("mirostat"))
{
if (ImGui::Selectable("Reset to default")){
mirostat = default_mirostat;
}
ImGui::EndPopup();
} HelpMarker(("Uses Mirostat sampling. Top K, Nucleus, Tail Free and Locally Typical samplers are ignored with this. Default: " + std::to_string(default_mirostat)).c_str());
}
static void sliderMirostatTau(float& mirostat_tau, float& default_mirostat_tau){
{
if (ImGui::Button("-##mirostat_tau")) {
mirostat_tau -= 0.001f;
}
ImGui::SameLine();
if (ImGui::Button("+##mirostat_tau")) {
mirostat_tau += 0.001f;
}
ImGui::SameLine();
}
ImGui::SliderFloat("mirostat_tau", &mirostat_tau, 0.0f, 10.0f);
if (ImGui::BeginPopupContextItem("mirostat_tau"))
{
if (ImGui::Selectable("Reset to default")){
mirostat_tau = default_mirostat_tau;
}
ImGui::EndPopup();
} ImGui::SameLine(); HelpMarker(("Mirostat learning rate. Default: " + std::to_string(default_mirostat_tau)).c_str());
}
static void sliderMirostatEta(float& mirostat_eta, float& default_mirostat_eta){
{
if (ImGui::Button("-##mirostat_eta")) {
mirostat_eta -= 0.001f;
}
ImGui::SameLine();
if (ImGui::Button("+##mirostat_eta")) {
mirostat_eta += 0.001f;
}
ImGui::SameLine();
}
ImGui::SliderFloat("mirostat_eta", &mirostat_eta, 0.00f, 0.50f);
if (ImGui::BeginPopupContextItem("mirostat_eta"))
{
if (ImGui::Selectable("Reset to default")){
mirostat_eta = default_mirostat_eta;
}
ImGui::EndPopup();
} ImGui::SameLine(); HelpMarker(("Mirostat target entropy. Default: " + std::to_string(default_mirostat_eta)).c_str());
}
static void sliderCfgScale(float& cfg_scale, float& default_cfg_scale){
{
if (ImGui::Button("-##cfg_scale")) {
cfg_scale -= 0.001f;
}
ImGui::SameLine();
if (ImGui::Button("+##cfg_scale")) {
cfg_scale += 0.001f;
}
ImGui::SameLine();
}
ImGui::SliderFloat("cfg_scale", &cfg_scale, 1.0f, 4.0f);
if (ImGui::BeginPopupContextItem("cfg_scale"))
{
if (ImGui::Selectable("Reset to default")){
cfg_scale = default_cfg_scale;
}
ImGui::EndPopup();
} ImGui::SameLine(); HelpMarker(("How strong the cfg is. High values might result in no answer generated. Default: " + std::to_string(default_cfg_scale)).c_str());
}
static void paramsPanelNew(gpt_params& params, int& totalThreads, ImVec2 size){
ImGui::BeginChild("Params", size, false);
#if GGML_OLD_FORMAT
sliderTemp(params.temp, paramsDefault.temp);
sliderTopK(params.top_k, paramsDefault.top_k);
sliderTopP(params.top_p, paramsDefault.top_p);
sliderMinP(params.min_p, paramsDefault.min_p);
sliderRepeatPen(params.repeat_penalty, paramsDefault.repeat_penalty);
sliderFrequencyPen(params.frequency_penalty, paramsDefault.frequency_penalty);
sliderPresencePen(params.presence_penalty, paramsDefault.presence_penalty);
sliderTfsZ(params.tfs_z, paramsDefault.tfs_z);
sliderTypicalP(params.typical_p, paramsDefault.typical_p);
sliderMirostat(params.mirostat, paramsDefault.mirostat);
sliderMirostatTau(params.mirostat_tau, paramsDefault.mirostat_tau);
sliderMirostatEta(params.mirostat_eta, paramsDefault.mirostat_eta);
sliderCfgScale(params.cfg_scale, paramsDefault.cfg_scale);
#elif GGML_USE_VULKAN2
//ImGui::SliderInt("n_threads", &localSettings.n_threads, 1, 4);
sliderTemp(params.sampling_params.temp, paramsDefault.sampling_params.temp);
sliderTopK(params.sampling_params.top_k, paramsDefault.sampling_params.top_k);
sliderTopP(params.sampling_params.top_p, paramsDefault.sampling_params.top_p);
sliderRepeatPen(params.sampling_params.repeat_penalty, paramsDefault.sampling_params.repeat_penalty);
sliderFrequencyPen(params.sampling_params.frequency_penalty, paramsDefault.sampling_params.frequency_penalty);
sliderPresencePen(params.sampling_params.presence_penalty, paramsDefault.sampling_params.presence_penalty);
sliderTfsZ(params.sampling_params.tfs_z, paramsDefault.sampling_params.tfs_z);
sliderTypicalP(params.sampling_params.typical_p, paramsDefault.sampling_params.typical_p);
sliderMirostat(params.sampling_params.mirostat, paramsDefault.sampling_params.mirostat);
sliderMirostatTau(params.sampling_params.mirostat_tau, paramsDefault.sampling_params.mirostat_tau);
sliderMirostatEta(params.sampling_params.mirostat_eta, paramsDefault.sampling_params.mirostat_eta);
sliderCfgScale(params.sampling_params.cfg_scale, paramsDefault.sampling_params.cfg_scale);
#else
//ImGui::SliderInt("n_threads", &localSettings.n_threads, 1, 4);
sliderTemp(params.sparams.temp, paramsDefault.sparams.temp);
sliderTopK(params.sparams.top_k, paramsDefault.sparams.top_k);
sliderTopP(params.sparams.top_p, paramsDefault.sparams.top_p);
sliderMinP(params.sparams.min_p, paramsDefault.sparams.min_p);
sliderRepeatPen(params.sparams.penalty_repeat, paramsDefault.sparams.penalty_repeat);
sliderFrequencyPen(params.sparams.penalty_freq, paramsDefault.sparams.penalty_freq);
sliderPresencePen(params.sparams.penalty_present, paramsDefault.sparams.penalty_present);
sliderTfsZ(params.sparams.tfs_z, paramsDefault.sparams.tfs_z);
sliderTypicalP(params.sparams.typical_p, paramsDefault.sparams.typical_p);
sliderMirostat(params.sparams.mirostat, paramsDefault.sparams.mirostat);
sliderMirostatTau(params.sparams.mirostat_tau, paramsDefault.sparams.mirostat_tau);
sliderMirostatEta(params.sparams.mirostat_eta, paramsDefault.sparams.mirostat_eta);
sliderCfgScale(params.sparams.cfg_scale, paramsDefault.sparams.cfg_scale);
#endif
ImGui::SliderInt("n_threads", ¶ms.n_threads, 1, totalThreads); ImGui::SameLine(); HelpMarker("Number of threads to use for generation, doesn't have to be maximum at the moment - try to find a sweetspot.");
//#ifdef GGML_EXPERIMENTAL1
ImGui::SliderInt("n_threads_batch", ¶ms.n_threads_batch, -1, totalThreads); ImGui::SameLine(); HelpMarker("Number of threads for prompt evaluation, recommended to set to maximum.");
//#endif
// ImGui::SliderFloat("cfg_smooth_factor", &localSettings.cfg_smooth_factor, 0.0f, 2.0f); ImGui::SameLine(); HelpMarker("Desfines the mix between outpus with and without cfg.");
ImGui::EndChild();
}
static void templatesList(nlohmann::json& templatesJson, std::string& inputStr){
ImGui::BeginChild("Templates from json file");
int numTemplates = 0;
for (auto& [key, value] : templatesJson.items() ){
if (value.is_array()) {
ImGui::SeparatorText(key.c_str());
for (auto& element : value) {
ImGui::TextWrapped(element.get<std::string>().c_str());
if (ImGui::BeginPopupContextItem(std::to_string(numTemplates).c_str()))
{
if (ImGui::Selectable("Use template")){
inputStr = element.get<std::string>();
}
if (ImGui::Selectable("Copy")){
ImGui::LogToClipboard();
ImGui::LogText(element.get<std::string>().c_str());
ImGui::LogFinish();
}
// if (ImGui::Selectable("Delete")){
// value.erase(numTemplates);
// }
ImGui::EndPopup();
}
++numTemplates;
ImGui::Separator();
}
}
}
ImGui::EndChild();
}
static void templatesListSelect(nlohmann::json& templatesJson, std::string& inputStr){
ImGui::BeginChild("Templates from json file");
for (auto& [key, value] : templatesJson.items() ){
if (value.is_array()) {
ImGui::SeparatorText(key.c_str());
for (auto& element : value) {
//HelpMarker(element.get<std::string>().c_str());
//ImGui::SameLine();
if (ImGui::Selectable(element.get<std::string>().c_str()))
inputStr = element.get<std::string>();
HelpTooltip(element.get<std::string>().c_str());
}
}
}
ImGui::EndChild();
}
static std::string openFile(const char* const* filterPatterns){
std::string currPath = filesystem::current_path().string() + '/';
auto getFileName = tinyfd_openFileDialog("Select a file...", currPath.c_str(),1, filterPatterns, NULL,0);
if (getFileName) {
std::string result = getFileName;
return result;
}
return "NULL";
}
static std::string openGrammar(){
char const *FilterPatterns[1]={"*.gbnf"};
std::string currPath = filesystem::current_path().string() + '\\';
auto getFileName = tinyfd_openFileDialog("Select a grammar file...", currPath.c_str(),1, FilterPatterns, NULL,0);
if (getFileName) {
std::string result = getFileName;
sanitizePath(result);
return result;
}
return "";
}
static void addStyling(){
ImGuiStyle& style = ImGui::GetStyle();
style.AntiAliasedLines = false;
style.AntiAliasedLinesUseTex = false;
style.AntiAliasedFill = false;
style.FrameBorderSize = 0.0f;
style.ChildBorderSize = 1.0f;
style.WindowPadding.x = 2.0f;
style.CellPadding.x = 3.0f;
style.ItemSpacing.x = 4.0f;
style.ChildRounding = 6.0f;
style.FrameRounding = 7.0f;
style.PopupRounding = 8.0f;
style.TabRounding = 9.0f;
style.GrabRounding = 10.0f;
style.FramePadding.x = 10.0f;
//for the "full-frame" main window
style.WindowRounding = 0.0f;
}
struct chatUI{
int width = 600;
int height = 800;
float fontSize = 21.0f;
int latency = 30;
std::string fontFile = "DroidSans.ttf";
std::string fontEmojisFile = "GikodotemojiRegular-EaBr4.ttf";
std::string modelsFolderName = "NULL";
std::string promptsFolderName = "NULL";
nlohmann::json configJson = getJson("chatConfig.json");
std::string windowLable = "Llama.cpp chat (gguf format)";
bool show_demo_window = false;
bool show_another_window = true;
bool my_tool_active = true;
bool chatMode = true;
bool scrolled = true;
bool cancelled = false;
bool copy = false;
bool autoscroll = true;
bool use_work_area = true;
configurableChat localSettings;
#if GGML_OLD_FORMAT
char const *modelFilterPatterns[1]={"*.bin"};
#else
char const *modelFilterPatterns[2]={"*.gguf","*.bin"};
#endif
char const *instructFilterPatterns[1]={"*.txt"};
char const *fontFilterPatterns[1]={"*.ttf"};
char const *jsonFilterPatterns[1]={"*.json"};
std::string currPath = filesystem::current_path().string() + '/';
modelThread newChat;
std::vector<std::pair<std::string, std::string>> localResultPairs;
nlohmann::json templatesJson = getJson("templates.json");
presetTest Test;
nlohmann::json testJson;
std::string testPrompt;
std::string testFolder;
std::vector<std::string> testPresets;
std::string testPresetsSummary;
bool isGeneratingTest = false;
std::map<std::string, std::vector<std::string>> sessionHistory;
bool copiedDialog = false;
bool copiedSettings = false;
bool copiedTimings = false;
bool initTokens = false;
bool hasModel = false;
int totalThreads = get_num_physical_cores();
int n_ctx_idx = 0;
int tokens_this_session = 0;
int consumed_this_session = 0;
int past_this_session = 0;
std::string inputStr = "";
std::string inputPrompt = "";
std::string inputAntiprompt = "";
std::string shortModelName = "Model isn't loaded yet.";
std::string inputAntiCFG = "";
std::string helpLabel = "Your question";
std::string aTiming = "Not calculated yet...";
std::string output = "...";
std::string inputGrammar = "";
std::string textFileContents = "";
int maxString = 3000;
int messageNum = 0;
int messageWidth = width;
int themeIdx = 2;
ImGuiWindowFlags chat_window_flags = ImGuiWindowFlags_MenuBar|ImGuiWindowFlags_NoMove|ImGuiWindowFlags_NoCollapse|ImGuiWindowFlags_NoTitleBar|ImGuiWindowFlags_NoResize;
void setLable(){
#if GGML_OLD_FORMAT
windowLable = "Llama.cpp chat (ggmlv3 format)";
#endif
#if GGML_USE_CLBLAST
windowLable += ": CLBLAST";
#elif GGML_USE_VULKAN
windowLable += ": VULKAN";
#endif
}
void readFromConfigJson(){
if (!configJson["error"].is_string()){
if (configJson["latency"].is_number()) latency = configJson["latency"].get<int>();
if (configJson["width"].is_number()) width = configJson["width"].get<int>();
if (configJson["height"].is_number()) height = configJson["height"].get<int>();
if (configJson["font"].is_string()) fontFile = configJson["font"].get<std::string>();
if (configJson["fontEmojis"].is_string()) fontEmojisFile = configJson["fontEmojis"].get<std::string>();
if (configJson["fontSize"].is_number()) fontSize = configJson["fontSize"].get<float>();
if (configJson.contains("modelsFolder") && configJson["modelsFolder"].is_string()) modelsFolderName = configJson["modelsFolder"].get<std::string>();
if (configJson.contains("promptsFolder") && configJson["promptsFolder"].is_string()) promptsFolderName = configJson["promptsFolder"].get<std::string>();
if (configJson.contains("theme") && configJson["theme"].is_number()) {
themeIdx = configJson["theme"].get<int>();
}
//configJson.erase("error");
}
}
void openCard(){
if (ImGui::Button("Open a preset card")) {
auto presetCardFilePath = tinyfd_openFileDialog("Select a preset card file...", currPath.c_str(),1, jsonFilterPatterns, NULL,0);
if (presetCardFilePath) {
nlohmann::json presetCardFile = getJson(presetCardFilePath);
localSettings.getSettingsFromJson(presetCardFile);
// we probably don't want presets to be instantly applied
//localSettings.fillLocalJson();
}
}
}
void saveCard(){
if (ImGui::Button("Save a preset card")) {
auto presetCardFilePath = tinyfd_saveFileDialog( "preset" , currPath.c_str() , 1 , jsonFilterPatterns, NULL);
if (presetCardFilePath) {
nlohmann::json presetCardFile = localSettings.createNewCard();
writeJson(presetCardFile, presetCardFilePath);
}
}
}
void settingsTab(){
if (newChat.loaded == 9) {
if (copiedSettings){
ImGui::TextWrapped( ("SEED: " + std::to_string(localSettings.params.seed)).c_str() );
//ImGui::BeginChild("PreData");
ImGui::TextWrapped( ("Base (initial) prompt: " + localSettings.params.prompt).c_str() ); ImGui::SameLine(); HelpMarker("This serves as the basis for the dialog, providing context. Make sure it ends with the antiptrompt if you don't want the NN to converse endlessly.");
ImGui::Separator();
//ImGui::EndChild();
ImGui::TextWrapped( ("Antiprompt: " + localSettings.params.antiprompt[0]).c_str() ); ImGui::SameLine(); HelpMarker("This serves as a stop-word for the NN to be able to end generation and wait for your input.");
ImGui::Separator();
#if GGML_OLD_FORMAT
ImGui::TextWrapped( ("CFG (negative) prompt: " + localSettings.params.cfg_negative_prompt).c_str() );
#elif GGML_USE_VULKAN2
ImGui::TextWrapped( ("CFG (negative) prompt: " + localSettings.params.sampling_params.cfg_negative_prompt).c_str() );
#else
ImGui::TextWrapped( ("CFG (negative) prompt: " + localSettings.params.sparams.cfg_negative_prompt).c_str() );
#endif
ImGui::SameLine(); HelpMarker("If cfg_scale > 1.0, CFG negative prompt is used for additional guiding.");
ImGui::Separator();
if (localSettings.params.rope_freq_scale != paramsDefault.rope_freq_scale){
ImGui::TextWrapped( ("rope_freq_scale: " + std::to_string(localSettings.params.rope_freq_scale)).c_str() ); ImGui::SameLine(); HelpMarker("Specific paramenter to adjust for extra large context models - needs to be (base model n_ctx)/(this model n_ctx), for example, 0.25 for llama2 (4096) -based everythinglm-13b-16k (16384)");
ImGui::Separator();
}
if (localSettings.params.rope_freq_base != paramsDefault.rope_freq_base){
ImGui::TextWrapped( ("rope_freq_base: " + std::to_string(localSettings.params.rope_freq_base)).c_str() ); ImGui::SameLine(); HelpMarker("Specific paramenter to adjust for extra large context models.");
ImGui::Separator();
}
ImGui::TextWrapped( ("Context size: " + std::to_string(localSettings.params.n_ctx)).c_str() ); ImGui::SameLine(); HelpMarker("The amount of data that can be stored and processed by the model within a session - depends on the model and should be used carefully. Models with context higher than 2048 usually have it in their name or description.");
ImGui::Separator();
ImGui::Spacing();
if (newChat.isContinue != 'w') {
if (ImGui::Button("Apply settings")) {
localSettings.fillLocalJson();
localSettings.pushSettings(newChat.newChat);
//localJsonDump = localSettings.modelConfig.dump(3);
localSettings.updateDump();
}
ImGui::SameLine();
if (ImGui::Button("Restore settings from model")) {
localSettings.getSettings(newChat.newChat);
localSettings.fillLocalJson();
}
ImGui::SameLine();
openCard();
ImGui::SameLine();
saveCard();
// ImGui::Checkbox("Temperature first", &newChat.newChat.tempFirst);
// ImGui::SameLine();
// HelpMarker("Temperature sampling is originally last in llama.cpp, which affect how it's randomeness affects the resulting array of tokens. You can change it for experiment.");
//} else ImGui::TextWrapped( (newChat.newChat.tempFirst ? "Temperature is first in sampling." : "Temperature is last in sampling.") );
}
ImGui::TextWrapped( ("Order of samplers: " + newChat.sparamsList ).c_str() );
}
} else {
ImGui::TextWrapped("Model isn't loaded yet...");
if (ImGui::Button("Apply settings")) {
localSettings.fillLocalJson();
//localJsonDump = localSettings.modelConfig.dump(3);
localSettings.updateDump();
}
ImGui::SameLine();
if (ImGui::Button("Restore settings from config")) {
localSettings.getSettingsFromModelConfig();
localSettings.fillLocalJson();
}
ImGui::SameLine();
openCard();
ImGui::SameLine();
saveCard();
}
//ImGui::BeginChild("SettingsTabsHalf", ImVec2( ImGui::GetContentRegionAvail().x * 0.5, ImGui::GetContentRegionAvail().y), false);
paramsPanelNew(localSettings.params, totalThreads, ImVec2( ImGui::GetContentRegionAvail().x * 0.80f, ImGui::GetContentRegionAvail().y));
//ImGui::EndChild();
ImGui::SameLine();
//ImGui::BeginChild("Timings", ImVec2( ImGui::GetContentRegionAvail().x, ImGui::GetContentRegionAvail().y), false);
if (copiedTimings == false) {
aTiming = newChat.lastTimings;
copiedTimings = true;
}
ImGui::TextWrapped(aTiming.c_str());
//ImGui::TextWrapped((newChat.lastTimings).c_str());
//ImGui::EndChild();
}
void jsonTab(){
if (ImGui::Button("Reset config")) {
localSettings.resetConfig("config.json");
//localSettings.getFromJson("config.json");
//localSettings.fillLocalJson();
//localSettings.updateDump();
}
ImGui::SameLine();
if (ImGui::Button("Update config")) {
localSettings.updateDump();
}
ImGui::SameLine();
if (ImGui::Button("Save config")) {
localSettings.pushToMainConfig();
writeJson(localSettings.localConfig, "config.json");
}
ImGui::BeginChild("Parameters from json file");
ImGui::TextWrapped(localSettings.localJsonDump.c_str());
ImGui::EndChild();
}
void templatesTab(){
if (!templatesJson.contains("error")){
if (ImGui::Button("Reset templates config")) {
templatesJson = getJson("templates.json");
//localSettings.getFromJson("config.json");
//localSettings.fillLocalJson();
//localJsonDump = localSettings.modelConfig.dump(3);
}
ImGui::SameLine();
if (ImGui::Button("Save templates")) {
writeJson(templatesJson, "templates.json");
}
templatesList(templatesJson, inputStr);
} else {
ImGui::TextWrapped("This is where your saved templates (instructs or dialog lines) will be stored for this session - or, if you save them, loaded from templates.json file.");
}
}
void promptFilesTab(const ImGuiViewport* viewport){
ImGui::TextWrapped(localSettings.promptFilesFolder.c_str());
ImGui::Separator();
ImGui::Spacing();
if (localSettings.promptFiles.size()){
if (ImGui::Button("Refresh config")) {
localSettings.getFilesList();
}
// ImGui::SameLine();
// if (ImGui::Button("Save config")) {
// writeJson(templatesJson, "templates.json");
// }
//templatesList(templatesJson, inputStr);
ImGui::BeginChild("Prompt files from folder");
ImVec2 center = viewport->GetCenter();
ImVec2 work_size = viewport->WorkSize;
for ( auto promptFile : localSettings.promptFiles){
//ImGui::TextWrapped(promptFile.string().c_str());
if (ImGui::Selectable( promptFile.filename().string().c_str() )){
ImGui::OpenPopup(promptFile.filename().string().c_str());
}
ImGui::SetNextWindowPos(center, ImGuiCond_Appearing, ImVec2(0.5f, 0.5f));
if (ImGui::BeginPopupModal(promptFile.filename().string().c_str()))
{
if (ImGui::Button("Close", ImVec2(work_size.x * 0.4f, 0))) { ImGui::CloseCurrentPopup(); }
if (ImGui::BeginChild("content", ImVec2( work_size.x * 0.5f, work_size.y * 0.5f)))
{
std::ifstream file(promptFile);
if (!file) {
ImGui::TextWrapped(("Cannot open the file " + promptFile.filename().string()).c_str());
} else {
std::string fileContent;
std::copy(std::istreambuf_iterator<char>(file), std::istreambuf_iterator<char>(), back_inserter(fileContent));
// std::string line;
// while ( getline( file, line ) ) {
// fileContent += line + "\n";
// }
ImGui::TextWrapped(fileContent.c_str());
}
}
ImGui::EndChild();
ImGui::EndPopup();
}
}
ImGui::EndChild();
} else {
ImGui::TextWrapped("This is where a list of prompt files (in .txt) is shown. Open a folder with them to see the list.");
}
}
void historyTab(){
if (sessionHistory.size()){
ImGui::BeginChild("History of your prompts");
int totalNum = 0;
for (auto sV : sessionHistory){
if (sV.second.size()){
ImGui::SeparatorText(sV.first.c_str());
for (auto msg : sV.second){
ImGui::TextWrapped(msg.c_str());
if (ImGui::BeginPopupContextItem(std::to_string(totalNum).c_str()))
{
if (ImGui::Selectable("Use")){
inputStr = msg;
}
if (ImGui::Selectable("Copy")){
ImGui::LogToClipboard();
ImGui::LogText(msg.c_str());
ImGui::LogFinish();
}
if(templatesJson.contains("error")){
if (ImGui::Selectable("Store as saved")){
templatesJson["saved"].push_back(msg);
templatesJson.erase("error");
}
} else {
for (auto& [key, value] : templatesJson.items() ){
if (value.is_array()) {
if (ImGui::Selectable(("Store as " + key).c_str())){
templatesJson[key].push_back(msg);