-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.go
1358 lines (1156 loc) · 35.7 KB
/
main.go
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
// Ollamark By Carsen Klock 2024 under the MIT license
// https://github.com/context-labs/ollamark
// https://ollamark.com
// Ollamark Client
package main
import (
"bytes"
"crypto/aes"
"crypto/cipher"
"crypto/hmac"
"crypto/rand"
"crypto/rsa"
"crypto/sha256"
"crypto/x509"
"encoding/base64"
"encoding/hex"
"encoding/json"
"encoding/pem"
"flag"
"fmt"
"image/color"
"io"
"net/http"
"net/url"
"os"
"os/exec"
"runtime"
"strconv"
"strings"
"time"
"fyne.io/fyne/v2"
"fyne.io/fyne/v2/app"
"fyne.io/fyne/v2/canvas"
"fyne.io/fyne/v2/container"
"fyne.io/fyne/v2/storage"
"fyne.io/fyne/v2/theme"
"fyne.io/fyne/v2/widget"
xwidget "fyne.io/x/fyne/widget"
"github.com/dgrijalva/jwt-go"
"github.com/google/uuid"
"github.com/joho/godotenv"
"github.com/shirou/gopsutil/mem"
)
type BenchmarkResult struct {
ModelName string `json:"model_name"`
Timestamp int64 `json:"timestamp"`
Duration float64 `json:"duration"`
TokensPerSecond float64 `json:"tokens_per_second"`
EvalCount int `json:"eval_count"`
EvalDuration int64 `json:"eval_duration"`
Iterations int `json:"iterations"`
SysInfo *SysInfo `json:"sys_info"`
GPUInfo *GPUInfo `json:"gpu_info"`
OllamaVersion string `json:"ollama_version"`
ClientType string `json:"client_type"`
ClientVersion string `json:"client_version"`
IP string `json:"ip"`
ProofOfWork ProofOfWorkSolution `json:"proof_of_work"`
}
type OllamaRequest struct {
ModelName string `json:"model"`
Prompt string `json:"prompt"`
}
type ModelRequest struct {
Name string `json:"name"`
}
type OllamaResponse struct {
Model string `json:"model"`
CreatedAt string `json:"created_at"`
Response string `json:"response"`
Done bool `json:"done"`
EvalCount int `json:"eval_count"`
EvalDuration int64 `json:"eval_duration"`
}
type SysInfo struct {
OS string `json:"os"`
Arch string `json:"arch"`
Version string `json:"version"`
Kernel string `json:"kernel"`
CPU string `json:"cpu"`
CPUName string `json:"cpu_name"`
Memory string `json:"memory"`
}
type GPUInfo struct {
Name string `json:"name"`
Vendor string `json:"vendor"`
Memory string `json:"memory"`
DriverVersion string `json:"driver_version"`
Count int `json:"count"`
}
var (
globalModels []ModelInfo
apiEndpoint string
clientVersion = "0.0.1"
)
// ProofOfWorkChallenge represents a proof-of-work challenge
type ProofOfWorkChallenge struct {
Challenge string `json:"challenge"`
Difficulty int `json:"difficulty"`
Timestamp int64 `json:"timestamp"`
}
// ProofOfWorkSolution represents a solution to a proof-of-work challenge
type ProofOfWorkSolution struct {
Challenge string `json:"challenge"`
Nonce string `json:"nonce"`
Timestamp int64 `json:"timestamp"`
Difficulty int `json:"difficulty"`
}
// requestProofOfWorkChallenge requests a new proof-of-work challenge from the server
func requestProofOfWorkChallenge(apiEndpoint string) (ProofOfWorkChallenge, error) {
resp, err := http.Get(apiEndpoint + "/api/pow-challenge")
if err != nil {
return ProofOfWorkChallenge{}, err
}
defer resp.Body.Close()
body, err := io.ReadAll(resp.Body)
if err != nil {
return ProofOfWorkChallenge{}, err
}
var challenge ProofOfWorkChallenge
if err := json.Unmarshal(body, &challenge); err != nil {
return ProofOfWorkChallenge{}, err
}
return challenge, nil
}
// solveProofOfWork solves the proof-of-work challenge
func solveProofOfWork(challenge ProofOfWorkChallenge) (string, error) {
prefix := strings.Repeat("0", challenge.Difficulty)
for i := 0; ; i++ {
nonce := strconv.Itoa(i)
hash := sha256.Sum256([]byte(challenge.Challenge + nonce))
if strings.HasPrefix(hex.EncodeToString(hash[:]), prefix) {
return nonce, nil
}
}
}
type ModelInfo struct {
Name string `json:"name"`
Parameters string `json:"parameters"`
Quantization string `json:"quantization"`
}
func fetchModels() ([]ModelInfo, error) {
mainURL := os.Getenv("OLLAMARK_API")
resp, err := http.Get(mainURL + "/api/model-list")
if err != nil {
return nil, err
}
defer resp.Body.Close()
// Read the raw JSON response
body, err := io.ReadAll(resp.Body)
if err != nil {
return nil, err
}
// Unmarshal the JSON response
var result struct {
Models []ModelInfo `json:"models"`
}
if err := json.Unmarshal(body, &result); err != nil {
return nil, err
}
return result.Models, nil
}
func initModels() error {
models, err := fetchModels()
if err != nil {
return err
}
globalModels = models
return nil
}
func LoadPublicKey() (*rsa.PublicKey, error) {
publicKeyData := os.Getenv("PUBLIC_KEY")
block, _ := pem.Decode([]byte(publicKeyData))
if block == nil {
return nil, fmt.Errorf("failed to parse PEM block containing the public key")
}
publicKey, err := x509.ParsePKIXPublicKey(block.Bytes)
if err != nil {
return nil, err
}
rsaPublicKey, ok := publicKey.(*rsa.PublicKey)
if !ok {
return nil, fmt.Errorf("failed to cast public key to RSA public key")
}
return rsaPublicKey, nil
}
func EncryptData(publicKey *rsa.PublicKey, data []byte) ([]byte, error) {
return rsa.EncryptOAEP(sha256.New(), rand.Reader, publicKey, data, nil)
}
// Generate a random AES key
func generateAESKey() ([]byte, error) {
key := make([]byte, 32) // AES-256
_, err := rand.Read(key)
if err != nil {
return nil, err
}
return key, nil
}
// Encrypt data using AES-GCM
func encryptAESGCM(key, plaintext []byte) ([]byte, []byte, error) {
block, err := aes.NewCipher(key)
if err != nil {
return nil, nil, err
}
aesGCM, err := cipher.NewGCM(block)
if err != nil {
return nil, nil, err
}
nonce := make([]byte, aesGCM.NonceSize())
if _, err := io.ReadFull(rand.Reader, nonce); err != nil {
return nil, nil, err
}
ciphertext := aesGCM.Seal(nil, nonce, plaintext, nil)
return nonce, ciphertext, nil
}
// Encrypt the AES key using RSA
func encryptRSA(publicKey *rsa.PublicKey, data []byte) ([]byte, error) {
return rsa.EncryptOAEP(sha256.New(), rand.Reader, publicKey, data, nil)
}
// Generate a random UUID
func generateUUID() string {
return uuid.New().String()
}
// Sign the UUID with HMAC-SHA256
func signUUID(uuid string, secretKey string) string {
h := hmac.New(sha256.New, []byte(secretKey))
h.Write([]byte(uuid))
return base64.StdEncoding.EncodeToString(h.Sum(nil))
}
func getCPUName() string {
// get windows cpu name
if runtime.GOOS == "windows" {
cmd := exec.Command("wmic", "cpu", "get", "name")
output, err := cmd.Output()
if err != nil {
return "Unknown"
}
lines := strings.Split(string(output), "\n")
if len(lines) > 1 {
return strings.TrimSpace(lines[1])
}
return "Unknown"
}
// get linux cpu name
if runtime.GOOS == "linux" {
cmd := exec.Command("lshw", "-C", "cpu")
output, err := cmd.Output()
if err != nil {
return "Unknown"
}
lines := strings.Split(string(output), "\n")
for _, line := range lines {
if strings.Contains(line, "product:") {
return strings.TrimSpace(strings.Split(line, ":")[1])
}
}
return "Unknown"
}
return "Unknown"
}
func getKernelVersion() (string, error) {
if runtime.GOOS == "windows" {
cmd := exec.Command("wmic", "os", "get", "Version", "/value")
output, err := cmd.Output()
if err != nil {
return "", err
}
lines := strings.Split(string(output), "\n")
for _, line := range lines {
if strings.HasPrefix(line, "Version=") {
return strings.TrimSpace(strings.Split(line, "=")[1]), nil
}
}
return "", fmt.Errorf("failed to parse Windows version")
}
cmd := exec.Command("uname", "-r")
output, err := cmd.Output()
if err != nil {
return "", err
}
return strings.TrimSpace(string(output)), nil
}
func getSysInfo() (*SysInfo, error) {
v, _ := mem.VirtualMemory()
// s, _ := mem.SwapMemory()
totalMemory := v.Total / 1024 / 1024 / 1024
// usedMemory := v.Used
// availableMemory := v.Available
// swapTotal := s.Total
// swapUsed := s.Used
sysInfo := &SysInfo{}
sysInfo.OS = runtime.GOOS
sysInfo.Arch = runtime.GOARCH
sysInfo.Version = "0.0.1"
kernelVersion, err := getKernelVersion()
if err != nil {
return nil, err
}
sysInfo.Kernel = kernelVersion
sysInfo.CPU = strconv.Itoa(runtime.NumCPU())
// get CPU Name for Windows and Linux
sysInfo.CPUName = getCPUName()
sysInfo.Memory = strconv.Itoa(int(totalMemory)) + " GB"
// Get system information if macOS (darwin) and aarch64 (arm64) then get the info with apple silicon only command: TODO (Test)
if runtime.GOOS == "darwin" && runtime.GOARCH == "arm64" {
cmd := exec.Command("system_profiler", "SPHardwareDataType")
output, err := cmd.Output()
if err != nil {
return nil, err
}
// Extract the CPU information from the output
lines := strings.Split(string(output), "\n")
for _, line := range lines {
if strings.Contains(line, "Chip:") {
sysInfo.CPUName = strings.TrimSpace(strings.Split(line, ":")[1])
break
}
}
}
return sysInfo, nil
}
func getMacGPUInfo() (*GPUInfo, error) {
cmd := exec.Command("system_profiler", "SPDisplaysDataType")
output, err := cmd.Output()
if err != nil {
return nil, err
}
gpuInfo := &GPUInfo{}
lines := strings.Split(string(output), "\n")
for _, line := range lines {
if strings.Contains(line, "Chipset Model:") {
gpuInfo.Name = strings.TrimSpace(strings.Split(line, ":")[1])
gpuInfo.Vendor = "Apple"
break
}
}
// If we couldn't find GPU info, it's likely integrated with the CPU
if gpuInfo.Name == "" {
cpuCmd := exec.Command("system_profiler", "SPHardwareDataType")
cpuOutput, err := cpuCmd.Output()
if err != nil {
return nil, err
}
cpuLines := strings.Split(string(cpuOutput), "\n")
for _, line := range cpuLines {
if strings.Contains(line, "Chip:") {
gpuInfo.Name = strings.TrimSpace(strings.Split(line, ":")[1]) + " GPU"
gpuInfo.Vendor = "Apple"
break
}
}
}
// Memory information isn't easily available for integrated GPUs
gpuInfo.Memory = "Shared"
gpuInfo.DriverVersion = "N/A"
gpuInfo.Count = 1
return gpuInfo, nil
}
func getGPUInfo() (*GPUInfo, error) {
// First, attempt to use nvidia-smi to fetch Nvidia GPU info
nvidiaGPU, err := getNvidiaGPUInfo()
if err == nil {
return nvidiaGPU, nil
}
// If Nvidia GPU info fetching fails, attempt to fetch AMD GPU info
amdGPU, err := getAMDGPUInfo()
if err == nil {
return amdGPU, nil
}
// Check if we're on macOS (darwin) and arm64 architecture
if runtime.GOOS == "darwin" && runtime.GOARCH == "arm64" {
return getMacGPUInfo()
}
// If both methods fail, return the last error
return nil, err
}
func getNvidiaGPUInfo() (*GPUInfo, error) {
cmd := exec.Command("nvidia-smi", "--query-gpu=name,memory.total,driver_version", "--format=csv,noheader")
output, err := cmd.Output()
if err != nil {
return nil, err
}
outputStr := strings.TrimSpace(string(output))
lines := strings.Split(outputStr, "\n")[0] // Assuming single GPU
fields := strings.Split(lines, ",")
if len(fields) < 2 {
return nil, fmt.Errorf("failed to parse Nvidia GPU information")
}
return &GPUInfo{
Name: strings.TrimSpace(fields[0]),
Vendor: "NVIDIA",
Memory: strings.TrimSpace(fields[1]),
DriverVersion: strings.TrimSpace(fields[2]),
Count: len(lines),
}, nil
}
func getAMDGPUInfo() (*GPUInfo, error) {
switch runtime.GOOS {
case "windows":
return getAMDGPUInfoWindows()
case "linux":
return getAMDGPUInfoLinux()
case "darwin":
return nil, fmt.Errorf("macOS, Skipping AMD GPU info")
default:
return nil, fmt.Errorf("AMD GPU unsupported operating system")
}
}
func getAMDGPUInfoWindows() (*GPUInfo, error) {
cmd := exec.Command("wmic", "path", "win32_VideoController", "get", "Name,DriverVersion", "/format:list")
output, err := cmd.CombinedOutput()
if err != nil {
return nil, fmt.Errorf("failed to execute wmic command: %v", err)
}
outputStr := string(output)
return parseWMICOutput(outputStr)
}
func parseWMICOutput(output string) (*GPUInfo, error) {
lines := strings.Split(output, "\n")
info := GPUInfo{}
gpuNames := make(map[string]bool) // To track unique GPU names
for _, line := range lines {
if strings.HasPrefix(line, "Name=") {
name := strings.TrimSpace(strings.Split(line, "=")[1])
// Skip integrated and virtual GPUs
if strings.Contains(name, "Integrated") || strings.Contains(name, "Display Adapter") || strings.Contains(name, "AMD Radeon(TM) Graphics") {
continue
}
if !gpuNames[name] {
gpuNames[name] = true
info.Name = name
info.Vendor = "AMD" // Assuming AMD if we are parsing this on an AMD system check
info.Count++
}
} else if strings.HasPrefix(line, "DriverVersion=") {
info.DriverVersion = strings.TrimSpace(strings.Split(line, "=")[1])
info.Memory = "Unknown" // Placeholder for memory, as WMIC does not provide it directly
}
}
if info.Name == "" {
return nil, fmt.Errorf("no dedicated AMD GPUs found")
}
return &info, nil
}
func getAMDGPUInfoLinux() (*GPUInfo, error) {
cmd := exec.Command("lshw", "-C", "display")
output, err := cmd.Output()
if err != nil {
return nil, err
}
outputStr := string(output)
// Example of parsing, adjust according to actual output
if strings.Contains(outputStr, "Radeon") || strings.Contains(outputStr, "AMD") {
name := extractField(outputStr, "product")
// vendor := "AMD"
memory := extractField(outputStr, "size")
return &GPUInfo{
Name: name,
// Vendor: vendor,
Memory: memory,
}, nil
}
return nil, fmt.Errorf("no AMD GPU detected")
}
func getIPAddress() string {
resp, err := http.Get("https://icanhazip.com")
if err != nil {
return "Unknown"
}
defer resp.Body.Close()
ip, err := io.ReadAll(resp.Body)
if err != nil {
return "Unknown"
}
return strings.TrimSpace(string(ip))
}
func getOllamaVersion() string {
cmd := exec.Command("ollama", "--version")
output, err := cmd.Output()
if err != nil {
return "Unknown"
}
// remove "ollama version is " from the output
return strings.TrimSpace(strings.Split(string(output), "ollama version is ")[1])
}
func extractField(data, fieldName string) string {
// Simple parsing logic, needs to be adjusted based on actual output
start := strings.Index(data, fieldName+":")
if start == -1 {
return ""
}
start += len(fieldName) + 1
end := strings.Index(data[start:], "\n")
if end == -1 {
return data[start:]
}
return strings.TrimSpace(data[start : start+end])
}
func main() {
// Load environment variables from .env file
err := godotenv.Load()
if err != nil {
fmt.Println("Error loading .env file:", err)
}
fmt.Println("Loading Ollamark...")
fmt.Println("Checking Ollama Version...")
ollamaVersion := getOllamaVersion()
if ollamaVersion == "Unknown" {
fmt.Println("Ollama not found, please install Ollama from https://ollama.com/download to Ollamark 😎")
return
}
fmt.Println("Ollama Version:", ollamaVersion)
err = initModels()
if err != nil {
fmt.Println("Failed to initialize models:", err)
return
}
flag.Usage = func() {
fmt.Println("Usage: ollamark [options]")
fmt.Println("Options:")
flag.PrintDefaults()
fmt.Println("Examples:")
fmt.Println(" For Ollamark GUI mode:")
fmt.Println(" ollamark (no flags)")
fmt.Println(" For Ollamark CLI mode:")
fmt.Println(" ollamark -m llama3 -i 10")
fmt.Println(" ollamark -m phi3")
fmt.Println(" ollamark -m phi3 -s")
fmt.Println(" ollamark -m phi3 -s -o http://localhost:11434/api/generate")
}
// Parse command-line arguments (Ollamark CLI)
modelPtr := flag.String("m", "llama3", "Model name to benchmark (default: llama3)")
submitPtr := flag.Bool("s", false, "Submit benchmark results to Ollamark.com (default false)")
ollamaPtr := flag.String("o", "http://localhost:11434", "Ollama API endpoint (default http://localhost:11434)")
iterationsPtr := flag.Int("i", 2, "Number of benchmark iterations (Min 2, Max 20)")
flag.Parse()
// Set the global API endpoint
apiEndpoint = *ollamaPtr
// Check if CLI arguments are provided
if flag.NFlag() > 0 {
if *modelPtr == "" || *ollamaPtr == "" {
flag.Usage()
os.Exit(1)
}
if flag.NArg() > 0 {
flag.Usage()
os.Exit(1)
}
if (*iterationsPtr < 2) || (*iterationsPtr > 20) {
flag.Usage()
os.Exit(1)
}
// Run ollamark in CLI mode
runBenchmarkCLI(*modelPtr, *submitPtr, apiEndpoint, *iterationsPtr)
return
}
// Create a new Fyne app
a := app.NewWithID("Ollamark")
a.Settings().SetTheme(theme.DarkTheme())
fyne.CurrentApp().Settings().SetTheme(fyne.CurrentApp().Settings().Theme())
w := a.NewWindow("Ollamark - Ollama Benchmark")
// set window size
w.Resize(fyne.NewSize(400, 300))
w.CenterOnScreen()
// create a logo
logo := canvas.NewImageFromFile("logo.svg")
logo.FillMode = canvas.ImageFillContain // Use 'Contain' to ensure the image fits well
logo.SetMinSize(fyne.NewSize(100, 100))
// Load the SVG icon
icon, err := fyne.LoadResourceFromPath("logo.svg")
if err != nil {
// Handle the error if the icon file cannot be loaded
fmt.Println("Failed to load icon:", err)
} else {
// Set the application icon
a.SetIcon(icon)
}
sysinfo, _ := getSysInfo()
gpuinfo, _ := getGPUInfo()
ollamaVersion = getOllamaVersion()
// create an api entry field
apiEntry := widget.NewEntry()
apiEntry.SetText(apiEndpoint)
// create a title label
titleLabel := widget.NewLabel("Ollama API Endpoint")
titleLabel.TextStyle = fyne.TextStyle{Bold: true}
title2Label := widget.NewLabel("Select a model to benchmark")
title2Label.TextStyle = fyne.TextStyle{Bold: true}
// Create a slice of model names for the dropdown
modelNames := make([]string, len(globalModels))
for i, model := range globalModels {
modelNames[i] = model.Name
}
// Create the select widget with model names
modelSelect := widget.NewSelect(modelNames, func(value string) {
// You can add logic here if needed when a model is selected
})
// Set the default selected model
// Find the index of "llama3" in the modelNames slice
defaultIndex := 0
for i, name := range modelNames {
if name == "llama3" {
defaultIndex = i
break
}
}
modelSelect.SetSelected(modelNames[defaultIndex])
resultLabel := widget.NewLabel("")
resultLabel.Alignment = fyne.TextAlignCenter
resultLabel.Hide()
// Custom text field for tokens per second
tokensPerSecondText := canvas.NewText("", color.White)
tokensPerSecondText.TextStyle.Bold = true
tokensPerSecondText.TextSize = 38 // Larger text size
tokensPerSecondText.Alignment = fyne.TextAlignCenter
tokensPerSecondText.Hide()
tpsText := canvas.NewText("", color.White)
tpsText.TextStyle.Bold = true
tpsText.TextSize = 16 // Larger text size
tpsText.Alignment = fyne.TextAlignCenter
tpsText.Hide()
sysText := widget.NewLabel("")
sysText.Hide()
gpuText := widget.NewLabel("")
gpuText.Hide()
ollamaVersionText := widget.NewLabel("")
ollamaVersionText.Hide()
iterationsSlider := widget.NewSlider(2, 20)
iterationsSlider.SetValue(2)
iterationsSlider.Step = 1
iterationsLabel := widget.NewLabel("Iterations: 2")
iterationsSlider.OnChanged = func(value float64) {
iterationsLabel.SetText(fmt.Sprintf("Iterations: %d", int(value)))
}
sysText.SetText(fmt.Sprintf("CPU: %s\nMemory: %s\nOS: %s\nKernel: %s", sysinfo.CPUName, sysinfo.Memory, sysinfo.OS, sysinfo.Kernel))
sysText.Show()
sysText.Refresh()
// if gpu Info is available, show it
if gpuinfo != nil {
gpuText.SetText(fmt.Sprintf("GPU Name: %s\nDriver Version: %s", gpuinfo.Name, gpuinfo.DriverVersion))
gpuText.Show()
gpuText.Refresh()
}
// set ollama version text make version bold
ollamaVersionText.SetText(fmt.Sprintf("Ollama Version: %s", ollamaVersion))
ollamaVersionText.Show()
ollamaVersionText.Refresh()
// create a progress bar
progressBar := widget.NewProgressBarInfinite()
progressBar.Hide()
gifURI := storage.NewFileURI("loader.gif")
gif, err := xwidget.NewAnimatedGif(gifURI)
if err != nil {
fmt.Println("Error loading gif:", err)
} else {
gif.Start()
gif.Show()
}
var benchmarkResult *BenchmarkResult
var submitButton *widget.Button
var linkButton *widget.Button
benchmarkButton := widget.NewButton("Benchmark", nil)
benchmarkButton.OnTapped = func() {
linkButton.Hide()
benchmarkButton.SetText("Benchmarking...")
benchmarkButton.Disable()
submitButton.Disable()
resultLabel.Show()
resultLabel.SetText("Benchmarks starting...")
resultLabel.Refresh()
tokensPerSecondText.Hide()
tpsText.Hide()
// sysText.Hide()
// gpuText.Hide()
go func() {
progressBar.Show()
progressBar.Refresh()
// get api url and model name from entry fields
apiURL := apiEntry.Text
modelName := modelSelect.Selected
iterations := int(iterationsSlider.Value)
modelRequest := ModelRequest{
Name: modelName,
}
jsonData, _ := json.Marshal(modelRequest)
fullURL := apiEndpoint + "/api/pull"
resultLabel.SetText("Pulling model " + modelName + ", Please wait...")
resultLabel.Refresh()
resp, err := http.Post(fullURL, "application/json", bytes.NewBuffer(jsonData))
if err != nil {
resultLabel.SetText("Error: " + err.Error())
benchmarkButton.SetText("Benchmark")
benchmarkButton.Enable()
progressBar.Hide()
progressBar.Refresh()
gif.Hide()
return
}
defer resp.Body.Close()
body, _ := io.ReadAll(resp.Body)
if resp.StatusCode != http.StatusOK {
resultLabel.SetText(fmt.Sprintf("Error pulling model: %s", body))
benchmarkButton.SetText("Benchmark")
benchmarkButton.Enable()
progressBar.Hide()
progressBar.Refresh()
gif.Hide()
return
}
// fmt.Println("Model pull response:", string(body)) // Debug print
resultLabel.SetText("Model pulled successfully")
resultLabel.Refresh()
resultLabel.SetText("Benchmarking...")
resultLabel.Refresh()
var totalTokensPerSecond float64
var evalCount int
var evalDuration float64
start := time.Now()
for i := 0; i < iterations; i++ {
requestBody := OllamaRequest{
ModelName: modelName,
Prompt: "Tell me about Llamas in 500 words.",
}
jsonData, _ := json.Marshal(requestBody)
resp, err := http.Post(apiURL+"/api/generate", "application/json", bytes.NewBuffer(jsonData))
if err != nil {
resultLabel.SetText("Error: " + err.Error())
benchmarkButton.SetText("Benchmark")
benchmarkButton.Enable()
progressBar.Hide()
progressBar.Refresh()
gif.Hide()
return
}
defer resp.Body.Close()
// start := time.Now()
var response OllamaResponse
var responseText string
decoder := json.NewDecoder(resp.Body)
resultLabel.SetText(fmt.Sprintf("Benchmark #%d in progress...", i+1))
resultLabel.Refresh()
for {
err := decoder.Decode(&response)
if err == io.EOF {
break
}
if err != nil {
resultLabel.SetText("Error: " + err.Error())
progressBar.Hide()
progressBar.Refresh()
benchmarkButton.SetText("Benchmark")
benchmarkButton.Enable()
return
}
responseText += response.Response
progressBar.Refresh()
}
// duration := time.Since(start).Seconds()
tokensPerSecond := float64(response.EvalCount) / (float64(response.EvalDuration) / 1e9)
totalTokensPerSecond += tokensPerSecond
evalCount = response.EvalCount
evalDuration = float64(response.EvalDuration) / 1e9
}
EvalCount := evalCount
EvalDuration := evalDuration
avgTokensPerSecond := totalTokensPerSecond / float64(iterations)
benchmarkResult = &BenchmarkResult{
ModelName: modelName,
Timestamp: time.Now().Unix(),
Duration: time.Since(start).Seconds(),
EvalCount: EvalCount,
EvalDuration: int64(EvalDuration),
TokensPerSecond: avgTokensPerSecond,
Iterations: iterations,
SysInfo: sysinfo,
GPUInfo: gpuinfo,
OllamaVersion: ollamaVersion,
ClientType: "ollamark-gui",
ClientVersion: clientVersion,
IP: getIPAddress(),
}
resultLabel.SetText(fmt.Sprintf("Benchmark completed for %s\nAverage Tokens per second: %.2f\nBenchmarked with %d iterations", modelName, avgTokensPerSecond, iterations))
resultLabel.Alignment = fyne.TextAlignCenter
resultLabel.Refresh()
// update custom text
tokensPerSecondText.Text = fmt.Sprintf("%.2f", avgTokensPerSecond) // Update the custom text
tokensPerSecondText.Show()
tpsText.Text = "Tokens per second"
tokensPerSecondText.Refresh()
tpsText.Refresh() // Refresh to update the display
tpsText.Show()
progressBar.Hide()
gif.Hide()
progressBar.Refresh() // Refresh after hiding the ProgressBar
benchmarkButton.SetText("Benchmark")
benchmarkButton.Enable()
submitButton.Show()
submitButton.Enable()
}()
}
submitButton = widget.NewButton("Share Benchmark", nil)
linkButton = widget.NewButton("View on Ollamark.com", nil)
linkButton.Hide()
submitButton.OnTapped = func() {
if benchmarkResult != nil {
subEndpoint := os.Getenv("OLLAMARK_API")
secretKey := os.Getenv("KEY")
publicKey, err := LoadPublicKey()
if err != nil {
resultLabel.SetText("Error loading public key: " + err.Error())
return
}
// Generate AES key
aesKey, err := generateAESKey()
if err != nil {
resultLabel.SetText("Error generating AES key: " + err.Error())
return
}
var submissionID = generateUUID()
// Generate JWT token
jwtToken, err := generateJWT(submissionID)
if err != nil {
resultLabel.SetText("Error generating JWT token: " + err.Error())
return
}
// Request proof-of-work challenge
challenge, err := requestProofOfWorkChallenge(subEndpoint)
if err != nil {
resultLabel.SetText("Error requesting proof-of-work challenge: " + err.Error())
return
}
// Solve proof-of-work challenge
powNonce, err := solveProofOfWork(challenge)
if err != nil {
resultLabel.SetText("Error solving proof-of-work challenge: " + err.Error())
return
}
// Include proof-of-work solution in the benchmark result
benchmarkResult.ProofOfWork = ProofOfWorkSolution{
Challenge: challenge.Challenge,
Nonce: powNonce,
Timestamp: challenge.Timestamp,
Difficulty: challenge.Difficulty,
}
// Encrypt benchmark result with AES key
jsonData, _ := json.Marshal(benchmarkResult)
nonce, encryptedData, err := encryptAESGCM(aesKey, jsonData)
if err != nil {
resultLabel.SetText("Error encrypting data with AES: " + err.Error())
return
}
// Encrypt AES key with RSA public key
encryptedAESKey, err := encryptRSA(publicKey, aesKey)