routes_test.go 19.8 KB
Newer Older
1
2
3
package server

import (
4
	"bytes"
5
	"context"
6
	"encoding/binary"
7
8
	"encoding/json"
	"fmt"
9
	"io"
10
	"io/fs"
11
	"math"
12
13
	"math/rand/v2"
	"net"
14
15
	"net/http"
	"net/http/httptest"
16
	"os"
17
	"path/filepath"
Patrick Devine's avatar
Patrick Devine committed
18
	"sort"
19
	"strings"
20
	"testing"
21
	"unicode"
22

23
	"github.com/ollama/ollama/api"
Michael Yang's avatar
Michael Yang committed
24
	"github.com/ollama/ollama/fs/ggml"
25
	"github.com/ollama/ollama/openai"
26
27
	"github.com/ollama/ollama/server/internal/cache/blob"
	"github.com/ollama/ollama/server/internal/client/ollama"
28
	"github.com/ollama/ollama/types/model"
29
	"github.com/ollama/ollama/version"
30
31
)

32
func createTestFile(t *testing.T, name string) (string, string) {
33
	t.Helper()
34

35
36
37
38
39
	modelDir := os.Getenv("OLLAMA_MODELS")
	if modelDir == "" {
		t.Fatalf("OLLAMA_MODELS not specified")
	}

40
	f, err := os.CreateTemp(t.TempDir(), name)
41
42
43
	if err != nil {
		t.Fatalf("failed to create temp file: %v", err)
	}
44
	defer f.Close()
45

46
	err = binary.Write(f, binary.LittleEndian, []byte("GGUF"))
47
48
49
	if err != nil {
		t.Fatalf("failed to write to file: %v", err)
	}
50

51
	err = binary.Write(f, binary.LittleEndian, uint32(3))
52
53
54
	if err != nil {
		t.Fatalf("failed to write to file: %v", err)
	}
55

56
	err = binary.Write(f, binary.LittleEndian, uint64(0))
57
58
59
	if err != nil {
		t.Fatalf("failed to write to file: %v", err)
	}
60

61
	err = binary.Write(f, binary.LittleEndian, uint64(0))
62
63
64
	if err != nil {
		t.Fatalf("failed to write to file: %v", err)
	}
65

66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
	// Calculate sha256 sum of file
	if _, err := f.Seek(0, 0); err != nil {
		t.Fatal(err)
	}

	digest, _ := GetSHA256Digest(f)
	if err := f.Close(); err != nil {
		t.Fatal(err)
	}

	if err := createLink(f.Name(), filepath.Join(modelDir, "blobs", fmt.Sprintf("sha256-%s", strings.TrimPrefix(digest, "sha256:")))); err != nil {
		t.Fatal(err)
	}

	return f.Name(), digest
81
}
82

83
84
85
86
87
88
89
90
91
92
93
94
95
// equalStringSlices checks if two slices of strings are equal.
func equalStringSlices(a, b []string) bool {
	if len(a) != len(b) {
		return false
	}
	for i := range a {
		if a[i] != b[i] {
			return false
		}
	}
	return true
}

96
97
98
99
100
101
102
103
104
type panicTransport struct{}

func (t *panicTransport) RoundTrip(r *http.Request) (*http.Response, error) {
	panic("unexpected RoundTrip call")
}

var panicOnRoundTrip = &http.Client{Transport: &panicTransport{}}

func TestRoutes(t *testing.T) {
105
106
107
108
109
110
	type testCase struct {
		Name     string
		Method   string
		Path     string
		Setup    func(t *testing.T, req *http.Request)
		Expected func(t *testing.T, resp *http.Response)
111
112
113
	}

	createTestModel := func(t *testing.T, name string) {
114
115
		t.Helper()

116
		_, digest := createTestFile(t, "ollama-model")
117
118
119
120

		fn := func(resp api.ProgressResponse) {
			t.Logf("Status: %s", resp.Status)
		}
121
122
123
124
125
126
127
128
129
130
131
132
133
134

		r := api.CreateRequest{
			Name:  name,
			Files: map[string]string{"test.gguf": digest},
			Parameters: map[string]any{
				"seed":  42,
				"top_p": 0.9,
				"stop":  []string{"foo", "bar"},
			},
		}

		modelName := model.ParseName(name)

		baseLayers, err := ggufLayers(digest, fn)
135
136
137
		if err != nil {
			t.Fatalf("failed to create model: %v", err)
		}
138
139
140
141

		if err := createModel(r, modelName, baseLayers, fn); err != nil {
			t.Fatal(err)
		}
142
	}
143
144
145
146
147
148
149
150
151
152

	testCases := []testCase{
		{
			Name:   "Version Handler",
			Method: http.MethodGet,
			Path:   "/api/version",
			Setup: func(t *testing.T, req *http.Request) {
			},
			Expected: func(t *testing.T, resp *http.Response) {
				contentType := resp.Header.Get("Content-Type")
153
154
155
				if contentType != "application/json; charset=utf-8" {
					t.Errorf("expected content type application/json; charset=utf-8, got %s", contentType)
				}
156
				body, err := io.ReadAll(resp.Body)
157
158
159
160
161
162
163
				if err != nil {
					t.Fatalf("failed to read response body: %v", err)
				}
				expectedBody := fmt.Sprintf(`{"version":"%s"}`, version.Version)
				if string(body) != expectedBody {
					t.Errorf("expected body %s, got %s", expectedBody, string(body))
				}
164
165
			},
		},
166
167
168
169
170
171
		{
			Name:   "Tags Handler (no tags)",
			Method: http.MethodGet,
			Path:   "/api/tags",
			Expected: func(t *testing.T, resp *http.Response) {
				contentType := resp.Header.Get("Content-Type")
172
173
174
				if contentType != "application/json; charset=utf-8" {
					t.Errorf("expected content type application/json; charset=utf-8, got %s", contentType)
				}
175
				body, err := io.ReadAll(resp.Body)
176
177
178
				if err != nil {
					t.Fatalf("failed to read response body: %v", err)
				}
179
180
181
182

				var modelList api.ListResponse

				err = json.Unmarshal(body, &modelList)
183
184
185
				if err != nil {
					t.Fatalf("failed to unmarshal response body: %v", err)
				}
186

187
188
189
				if modelList.Models == nil || len(modelList.Models) != 0 {
					t.Errorf("expected empty model list, got %v", modelList.Models)
				}
190
191
			},
		},
192
193
194
195
196
197
		{
			Name:   "openai empty list",
			Method: http.MethodGet,
			Path:   "/v1/models",
			Expected: func(t *testing.T, resp *http.Response) {
				contentType := resp.Header.Get("Content-Type")
198
199
200
				if contentType != "application/json" {
					t.Errorf("expected content type application/json, got %s", contentType)
				}
201
				body, err := io.ReadAll(resp.Body)
202
203
204
				if err != nil {
					t.Fatalf("failed to read response body: %v", err)
				}
205
206
207

				var modelList openai.ListCompletion
				err = json.Unmarshal(body, &modelList)
208
209
210
				if err != nil {
					t.Fatalf("failed to unmarshal response body: %v", err)
				}
211

212
213
214
				if modelList.Object != "list" || len(modelList.Data) != 0 {
					t.Errorf("expected empty model list, got %v", modelList.Data)
				}
215
216
			},
		},
217
218
219
220
221
		{
			Name:   "Tags Handler (yes tags)",
			Method: http.MethodGet,
			Path:   "/api/tags",
			Setup: func(t *testing.T, req *http.Request) {
222
				createTestModel(t, "test-model")
223
224
225
			},
			Expected: func(t *testing.T, resp *http.Response) {
				contentType := resp.Header.Get("Content-Type")
226
227
228
				if contentType != "application/json; charset=utf-8" {
					t.Errorf("expected content type application/json; charset=utf-8, got %s", contentType)
				}
229
				body, err := io.ReadAll(resp.Body)
230
231
232
				if err != nil {
					t.Fatalf("failed to read response body: %v", err)
				}
233

234
235
236
				if strings.Contains(string(body), "expires_at") {
					t.Errorf("response body should not contain 'expires_at'")
				}
237

238
239
				var modelList api.ListResponse
				err = json.Unmarshal(body, &modelList)
240
241
242
243
244
245
246
247
248
249
250
251
252
253
				if err != nil {
					t.Fatalf("failed to unmarshal response body: %v", err)
				}

				if len(modelList.Models) != 1 || modelList.Models[0].Name != "test-model:latest" {
					t.Errorf("expected model 'test-model:latest', got %v", modelList.Models)
				}
			},
		},
		{
			Name:   "Delete Model Handler",
			Method: http.MethodDelete,
			Path:   "/api/delete",
			Setup: func(t *testing.T, req *http.Request) {
254
				createTestModel(t, "model_to_delete")
255
256

				deleteReq := api.DeleteRequest{
257
					Name: "model_to_delete",
258
259
260
261
262
				}
				jsonData, err := json.Marshal(deleteReq)
				if err != nil {
					t.Fatalf("failed to marshal delete request: %v", err)
				}
263

264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
				req.Body = io.NopCloser(bytes.NewReader(jsonData))
			},
			Expected: func(t *testing.T, resp *http.Response) {
				if resp.StatusCode != http.StatusOK {
					t.Errorf("expected status code 200, got %d", resp.StatusCode)
				}

				// Verify the model was deleted
				_, err := GetModel("model-to-delete")
				if err == nil || !os.IsNotExist(err) {
					t.Errorf("expected model to be deleted, got error %v", err)
				}
			},
		},
		{
			Name:   "Delete Non-existent Model",
			Method: http.MethodDelete,
			Path:   "/api/delete",
			Setup: func(t *testing.T, req *http.Request) {
				deleteReq := api.DeleteRequest{
284
					Name: "non_existent_model",
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
				}
				jsonData, err := json.Marshal(deleteReq)
				if err != nil {
					t.Fatalf("failed to marshal delete request: %v", err)
				}

				req.Body = io.NopCloser(bytes.NewReader(jsonData))
			},
			Expected: func(t *testing.T, resp *http.Response) {
				if resp.StatusCode != http.StatusNotFound {
					t.Errorf("expected status code 404, got %d", resp.StatusCode)
				}

				body, err := io.ReadAll(resp.Body)
				if err != nil {
					t.Fatalf("failed to read response body: %v", err)
				}

				var errorResp map[string]string
				err = json.Unmarshal(body, &errorResp)
				if err != nil {
					t.Fatalf("failed to unmarshal response body: %v", err)
				}

				if !strings.Contains(errorResp["error"], "not found") {
					t.Errorf("expected error message to contain 'not found', got %s", errorResp["error"])
				}
312
313
			},
		},
314
315
316
317
318
319
		{
			Name:   "openai list models with tags",
			Method: http.MethodGet,
			Path:   "/v1/models",
			Expected: func(t *testing.T, resp *http.Response) {
				contentType := resp.Header.Get("Content-Type")
320
321
322
				if contentType != "application/json" {
					t.Errorf("expected content type application/json, got %s", contentType)
				}
323
				body, err := io.ReadAll(resp.Body)
324
325
326
				if err != nil {
					t.Fatalf("failed to read response body: %v", err)
				}
327
328
329

				var modelList openai.ListCompletion
				err = json.Unmarshal(body, &modelList)
330
331
332
				if err != nil {
					t.Fatalf("failed to unmarshal response body: %v", err)
				}
333

334
335
336
				if len(modelList.Data) != 1 || modelList.Data[0].Id != "test-model:latest" || modelList.Data[0].OwnedBy != "library" {
					t.Errorf("expected model 'test-model:latest' owned by 'library', got %v", modelList.Data)
				}
337
338
			},
		},
339
340
341
342
343
		{
			Name:   "Create Model Handler",
			Method: http.MethodPost,
			Path:   "/api/create",
			Setup: func(t *testing.T, req *http.Request) {
344
				_, digest := createTestFile(t, "ollama-model")
345
346
				stream := false
				createReq := api.CreateRequest{
347
348
349
					Name:   "t-bone",
					Files:  map[string]string{"test.gguf": digest},
					Stream: &stream,
350
351
				}
				jsonData, err := json.Marshal(createReq)
352
353
354
				if err != nil {
					t.Fatalf("failed to marshal create request: %v", err)
				}
355
356
357
358
359

				req.Body = io.NopCloser(bytes.NewReader(jsonData))
			},
			Expected: func(t *testing.T, resp *http.Response) {
				contentType := resp.Header.Get("Content-Type")
360
361
362
				if contentType != "application/json" {
					t.Errorf("expected content type application/json, got %s", contentType)
				}
363
				_, err := io.ReadAll(resp.Body)
364
365
366
367
368
369
				if err != nil {
					t.Fatalf("failed to read response body: %v", err)
				}
				if resp.StatusCode != http.StatusOK { // Updated line
					t.Errorf("expected status code 200, got %d", resp.StatusCode)
				}
370
371

				model, err := GetModel("t-bone")
372
373
374
375
376
377
				if err != nil {
					t.Fatalf("failed to get model: %v", err)
				}
				if model.ShortName != "t-bone:latest" {
					t.Errorf("expected model name 't-bone:latest', got %s", model.ShortName)
				}
378
379
380
381
382
383
384
385
386
387
388
389
390
			},
		},
		{
			Name:   "Copy Model Handler",
			Method: http.MethodPost,
			Path:   "/api/copy",
			Setup: func(t *testing.T, req *http.Request) {
				createTestModel(t, "hamshank")
				copyReq := api.CopyRequest{
					Source:      "hamshank",
					Destination: "beefsteak",
				}
				jsonData, err := json.Marshal(copyReq)
391
392
393
				if err != nil {
					t.Fatalf("failed to marshal copy request: %v", err)
				}
394
395
396
397
398

				req.Body = io.NopCloser(bytes.NewReader(jsonData))
			},
			Expected: func(t *testing.T, resp *http.Response) {
				model, err := GetModel("beefsteak")
399
400
401
402
403
404
				if err != nil {
					t.Fatalf("failed to get model: %v", err)
				}
				if model.ShortName != "beefsteak:latest" {
					t.Errorf("expected model name 'beefsteak:latest', got %s", model.ShortName)
				}
405
406
			},
		},
Patrick Devine's avatar
Patrick Devine committed
407
408
409
410
411
412
413
414
		{
			Name:   "Show Model Handler",
			Method: http.MethodPost,
			Path:   "/api/show",
			Setup: func(t *testing.T, req *http.Request) {
				createTestModel(t, "show-model")
				showReq := api.ShowRequest{Model: "show-model"}
				jsonData, err := json.Marshal(showReq)
415
416
417
				if err != nil {
					t.Fatalf("failed to marshal show request: %v", err)
				}
Patrick Devine's avatar
Patrick Devine committed
418
419
420
421
				req.Body = io.NopCloser(bytes.NewReader(jsonData))
			},
			Expected: func(t *testing.T, resp *http.Response) {
				contentType := resp.Header.Get("Content-Type")
422
423
424
				if contentType != "application/json; charset=utf-8" {
					t.Errorf("expected content type application/json; charset=utf-8, got %s", contentType)
				}
Patrick Devine's avatar
Patrick Devine committed
425
				body, err := io.ReadAll(resp.Body)
426
427
428
				if err != nil {
					t.Fatalf("failed to read response body: %v", err)
				}
Patrick Devine's avatar
Patrick Devine committed
429
430
431

				var showResp api.ShowResponse
				err = json.Unmarshal(body, &showResp)
432
433
434
				if err != nil {
					t.Fatalf("failed to unmarshal response body: %v", err)
				}
Patrick Devine's avatar
Patrick Devine committed
435
436
437
438
439
440
441
442
443
444
445
446
447

				var params []string
				paramsSplit := strings.Split(showResp.Parameters, "\n")
				for _, p := range paramsSplit {
					params = append(params, strings.Join(strings.Fields(p), " "))
				}
				sort.Strings(params)
				expectedParams := []string{
					"seed 42",
					"stop \"bar\"",
					"stop \"foo\"",
					"top_p 0.9",
				}
448
449
450
451
452
453
454
455
456
457
				if !equalStringSlices(params, expectedParams) {
					t.Errorf("expected parameters %v, got %v", expectedParams, params)
				}
				paramCount, ok := showResp.ModelInfo["general.parameter_count"].(float64)
				if !ok {
					t.Fatalf("expected parameter count to be a float64, got %T", showResp.ModelInfo["general.parameter_count"])
				}
				if math.Abs(paramCount) > 1e-9 {
					t.Errorf("expected parameter count to be 0, got %f", paramCount)
				}
Patrick Devine's avatar
Patrick Devine committed
458
459
			},
		},
460
		{
461
462
463
464
			Name: "openai retrieve model handler",
			Setup: func(t *testing.T, req *http.Request) {
				createTestModel(t, "show-model")
			},
465
466
467
468
			Method: http.MethodGet,
			Path:   "/v1/models/show-model",
			Expected: func(t *testing.T, resp *http.Response) {
				contentType := resp.Header.Get("Content-Type")
469
470
471
				if contentType != "application/json" {
					t.Errorf("expected content type application/json, got %s", contentType)
				}
472
				body, err := io.ReadAll(resp.Body)
473
474
475
				if err != nil {
					t.Fatalf("failed to read response body: %v", err)
				}
476
477
478

				var retrieveResp api.RetrieveModelResponse
				err = json.Unmarshal(body, &retrieveResp)
479
480
481
				if err != nil {
					t.Fatalf("failed to unmarshal response body: %v", err)
				}
482

483
484
485
				if retrieveResp.Id != "show-model" || retrieveResp.OwnedBy != "library" {
					t.Errorf("expected model 'show-model' owned by 'library', got %v", retrieveResp)
				}
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
	modelsDir := t.TempDir()
	t.Setenv("OLLAMA_MODELS", modelsDir)

	c, err := blob.Open(modelsDir)
	if err != nil {
		t.Fatalf("failed to open models dir: %v", err)
	}

	rc := &ollama.Registry{
		// This is a temporary measure to allow us to move forward,
		// surfacing any code contacting ollama.com we do not intended
		// to.
		//
		// Currently, this only handles DELETE /api/delete, which
		// should not make any contact with the ollama.com registry, so
		// be clear about that.
		//
		// Tests that do need to contact the registry here, will be
		// consumed into our new server/api code packages and removed
		// from here.
		HTTPClient: panicOnRoundTrip,
	}
512

513
	s := &Server{}
514
515
516
517
	router, err := s.GenerateRoutes(c, rc)
	if err != nil {
		t.Fatalf("failed to generate routes: %v", err)
	}
518
519
520
521
522

	httpSrv := httptest.NewServer(router)
	t.Cleanup(httpSrv.Close)

	for _, tc := range testCases {
Michael Yang's avatar
Michael Yang committed
523
524
525
		t.Run(tc.Name, func(t *testing.T) {
			u := httpSrv.URL + tc.Path
			req, err := http.NewRequestWithContext(context.TODO(), tc.Method, u, nil)
526
527
528
			if err != nil {
				t.Fatalf("failed to create request: %v", err)
			}
Michael Yang's avatar
Michael Yang committed
529
530
531
532
533
534

			if tc.Setup != nil {
				tc.Setup(t, req)
			}

			resp, err := httpSrv.Client().Do(req)
535
536
537
			if err != nil {
				t.Fatalf("failed to do request: %v", err)
			}
Michael Yang's avatar
Michael Yang committed
538
539
540
541
542
543
			defer resp.Body.Close()

			if tc.Expected != nil {
				tc.Expected(t, resp)
			}
		})
544
545
	}
}
546

547
548
549
550
551
552
553
554
func casingShuffle(s string) string {
	rr := []rune(s)
	for i := range rr {
		if rand.N(2) == 0 {
			rr[i] = unicode.ToUpper(rr[i])
		} else {
			rr[i] = unicode.ToLower(rr[i])
		}
555
	}
556
557
	return string(rr)
}
558

559
560
func TestManifestCaseSensitivity(t *testing.T) {
	t.Setenv("OLLAMA_MODELS", t.TempDir())
561

562
563
564
565
566
567
568
569
570
571
572
573
574
	r := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		w.WriteHeader(http.StatusOK)
		io.WriteString(w, `{}`) //nolint:errcheck
	}))
	defer r.Close()

	nameUsed := make(map[string]bool)
	name := func() string {
		const fqmn = "example/namespace/model:tag"
		for {
			v := casingShuffle(fqmn)
			if nameUsed[v] {
				continue
575
			}
576
577
578
579
			nameUsed[v] = true
			return v
		}
	}
580

581
	wantStableName := name()
582

583
584
	t.Logf("stable name: %s", wantStableName)

585
586
587
588
	// checkManifestList tests that there is strictly one manifest in the
	// models directory, and that the manifest is for the model under test.
	checkManifestList := func() {
		t.Helper()
589

590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
		mandir := filepath.Join(os.Getenv("OLLAMA_MODELS"), "manifests/")
		var entries []string
		t.Logf("dir entries:")
		fsys := os.DirFS(mandir)
		err := fs.WalkDir(fsys, ".", func(path string, info fs.DirEntry, err error) error {
			if err != nil {
				return err
			}
			t.Logf("    %s", fs.FormatDirEntry(info))
			if info.IsDir() {
				return nil
			}
			path = strings.TrimPrefix(path, mandir)
			entries = append(entries, path)
			return nil
		})
		if err != nil {
			t.Fatalf("failed to walk directory: %v", err)
		}
609

610
611
612
613
		if len(entries) != 1 {
			t.Errorf("len(got) = %d, want 1", len(entries))
			return // do not use Fatal so following steps run
		}
614

615
616
617
618
619
620
621
622
		g := entries[0] // raw path
		g = filepath.ToSlash(g)
		w := model.ParseName(wantStableName).Filepath()
		w = filepath.ToSlash(w)
		if g != w {
			t.Errorf("\ngot:  %s\nwant: %s", g, w)
		}
	}
623

624
625
626
627
628
629
630
	checkOK := func(w *httptest.ResponseRecorder) {
		t.Helper()
		if w.Code != http.StatusOK {
			t.Errorf("code = %d, want 200", w.Code)
			t.Logf("body: %s", w.Body.String())
		}
	}
631

632
633
634
635
	var s Server
	testMakeRequestDialContext = func(ctx context.Context, _, _ string) (net.Conn, error) {
		var d net.Dialer
		return d.DialContext(ctx, "tcp", r.Listener.Addr().String())
636
	}
637
638
639
	t.Cleanup(func() { testMakeRequestDialContext = nil })

	t.Logf("creating")
640
	_, digest := createBinFile(t, nil, nil)
641
642
643
	checkOK(createRequest(t, s.CreateHandler, api.CreateRequest{
		// Start with the stable name, and later use a case-shuffled
		// version.
644
645
646
		Name:   wantStableName,
		Files:  map[string]string{"test.gguf": digest},
		Stream: &stream,
647
648
649
650
651
	}))
	checkManifestList()

	t.Logf("creating (again)")
	checkOK(createRequest(t, s.CreateHandler, api.CreateRequest{
652
653
654
		Name:   name(),
		Files:  map[string]string{"test.gguf": digest},
		Stream: &stream,
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
	}))
	checkManifestList()

	t.Logf("pulling")
	checkOK(createRequest(t, s.PullHandler, api.PullRequest{
		Name:     name(),
		Stream:   &stream,
		Insecure: true,
	}))
	checkManifestList()

	t.Logf("copying")
	checkOK(createRequest(t, s.CopyHandler, api.CopyRequest{
		Source:      name(),
		Destination: name(),
	}))
	checkManifestList()
672
673
674
675
676
677
678
679
680
681
682
683

	t.Logf("pushing")
	rr := createRequest(t, s.PushHandler, api.PushRequest{
		Model:    name(),
		Insecure: true,
		Username: "alice",
		Password: "x",
	})
	checkOK(rr)
	if !strings.Contains(rr.Body.String(), `"status":"success"`) {
		t.Errorf("got = %q, want success", rr.Body.String())
	}
684
}
685
686
687
688
689
690

func TestShow(t *testing.T) {
	t.Setenv("OLLAMA_MODELS", t.TempDir())

	var s Server

Michael Yang's avatar
Michael Yang committed
691
692
	_, digest1 := createBinFile(t, ggml.KV{"general.architecture": "test"}, nil)
	_, digest2 := createBinFile(t, ggml.KV{"general.type": "projector", "general.architecture": "clip"}, nil)
693

694
	createRequest(t, s.CreateHandler, api.CreateRequest{
695
696
		Name:  "show-model",
		Files: map[string]string{"model.gguf": digest1, "projector.gguf": digest2},
697
698
	})

699
	w := createRequest(t, s.ShowHandler, api.ShowRequest{
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
		Name: "show-model",
	})

	if w.Code != http.StatusOK {
		t.Fatalf("expected status code 200, actual %d", w.Code)
	}

	var resp api.ShowResponse
	if err := json.NewDecoder(w.Body).Decode(&resp); err != nil {
		t.Fatal(err)
	}

	if resp.ModelInfo["general.architecture"] != "test" {
		t.Fatal("Expected model architecture to be 'test', but got", resp.ModelInfo["general.architecture"])
	}

	if resp.ProjectorInfo["general.architecture"] != "clip" {
		t.Fatal("Expected projector architecture to be 'clip', but got", resp.ProjectorInfo["general.architecture"])
	}
}
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

func TestNormalize(t *testing.T) {
	type testCase struct {
		input []float32
	}

	testCases := []testCase{
		{input: []float32{1}},
		{input: []float32{0, 1, 2, 3}},
		{input: []float32{0.1, 0.2, 0.3}},
		{input: []float32{-0.1, 0.2, 0.3, -0.4}},
		{input: []float32{0, 0, 0}},
	}

	isNormalized := func(vec []float32) (res bool) {
		sum := 0.0
		for _, v := range vec {
			sum += float64(v * v)
		}
		if math.Abs(sum-1) > 1e-6 {
			return sum == 0
		} else {
			return true
		}
	}

	for _, tc := range testCases {
		t.Run("", func(t *testing.T) {
			normalized := normalize(tc.input)
			if !isNormalized(normalized) {
				t.Errorf("Vector %v is not normalized", tc.input)
			}
		})
	}
}