"magic_pdf/pre_proc/ocr_detect_layout.py.bak" did not exist on "61405b8af816748513dabf77d92e36bb63825b4a"
routes_test.go 26 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"
18
	"reflect"
19
	"slices"
Patrick Devine's avatar
Patrick Devine committed
20
	"sort"
21
	"strings"
22
	"testing"
23
	"unicode"
24

25
26
	"github.com/gin-gonic/gin"
	"github.com/google/go-cmp/cmp"
27
	"github.com/ollama/ollama/api"
Michael Yang's avatar
Michael Yang committed
28
	"github.com/ollama/ollama/fs/ggml"
29
	"github.com/ollama/ollama/openai"
30
	"github.com/ollama/ollama/server/internal/client/ollama"
31
	"github.com/ollama/ollama/types/model"
32
	"github.com/ollama/ollama/version"
33
34
)

35
func createTestFile(t *testing.T, name string) (string, string) {
36
	t.Helper()
37

38
39
40
41
42
	modelDir := os.Getenv("OLLAMA_MODELS")
	if modelDir == "" {
		t.Fatalf("OLLAMA_MODELS not specified")
	}

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

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

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

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

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

69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
	// 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
84
}
85

86
87
88
89
90
91
92
93
94
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) {
95
96
97
98
99
100
	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)
101
102
103
	}

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

106
		_, digest := createTestFile(t, "ollama-model")
107
108
109
110

		fn := func(resp api.ProgressResponse) {
			t.Logf("Status: %s", resp.Status)
		}
111
112
113
114
115
116
117
118
119
120
121
122
123
124

		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)
125
126
127
		if err != nil {
			t.Fatalf("failed to create model: %v", err)
		}
128

129
		config := &model.ConfigV2{
130
131
			OS:           "linux",
			Architecture: "amd64",
132
			RootFS: model.RootFS{
133
134
135
136
137
				Type: "layers",
			},
		}

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

	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")
151
152
153
				if contentType != "application/json; charset=utf-8" {
					t.Errorf("expected content type application/json; charset=utf-8, got %s", contentType)
				}
154
				body, err := io.ReadAll(resp.Body)
155
156
157
158
159
160
161
				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))
				}
162
163
			},
		},
164
165
166
167
168
169
		{
			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")
170
171
172
				if contentType != "application/json; charset=utf-8" {
					t.Errorf("expected content type application/json; charset=utf-8, got %s", contentType)
				}
173
				body, err := io.ReadAll(resp.Body)
174
175
176
				if err != nil {
					t.Fatalf("failed to read response body: %v", err)
				}
177
178
179
180

				var modelList api.ListResponse

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

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

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

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

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

236
237
				var modelList api.ListResponse
				err = json.Unmarshal(body, &modelList)
238
239
240
241
242
243
244
245
246
247
248
249
250
251
				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) {
252
				createTestModel(t, "model_to_delete")
253
254

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

262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
				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{
282
					Name: "non_existent_model",
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
				}
				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"])
				}
310
311
			},
		},
312
313
314
315
316
317
		{
			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")
318
319
320
				if contentType != "application/json" {
					t.Errorf("expected content type application/json, got %s", contentType)
				}
321
				body, err := io.ReadAll(resp.Body)
322
323
324
				if err != nil {
					t.Fatalf("failed to read response body: %v", err)
				}
325
326
327

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

332
333
334
				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)
				}
335
336
			},
		},
337
338
339
340
341
		{
			Name:   "Create Model Handler",
			Method: http.MethodPost,
			Path:   "/api/create",
			Setup: func(t *testing.T, req *http.Request) {
342
				_, digest := createTestFile(t, "ollama-model")
343
344
				stream := false
				createReq := api.CreateRequest{
345
346
347
					Name:   "t-bone",
					Files:  map[string]string{"test.gguf": digest},
					Stream: &stream,
348
349
				}
				jsonData, err := json.Marshal(createReq)
350
351
352
				if err != nil {
					t.Fatalf("failed to marshal create request: %v", err)
				}
353
354
355
356
357

				req.Body = io.NopCloser(bytes.NewReader(jsonData))
			},
			Expected: func(t *testing.T, resp *http.Response) {
				contentType := resp.Header.Get("Content-Type")
358
359
360
				if contentType != "application/json" {
					t.Errorf("expected content type application/json, got %s", contentType)
				}
361
				_, err := io.ReadAll(resp.Body)
362
363
364
365
366
367
				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)
				}
368
369

				model, err := GetModel("t-bone")
370
371
372
373
374
375
				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)
				}
376
377
378
379
380
381
382
383
384
385
386
387
388
			},
		},
		{
			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)
389
390
391
				if err != nil {
					t.Fatalf("failed to marshal copy request: %v", err)
				}
392
393
394
395
396

				req.Body = io.NopCloser(bytes.NewReader(jsonData))
			},
			Expected: func(t *testing.T, resp *http.Response) {
				model, err := GetModel("beefsteak")
397
398
399
400
401
402
				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)
				}
403
404
			},
		},
Patrick Devine's avatar
Patrick Devine committed
405
406
407
408
409
410
411
412
		{
			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)
413
414
415
				if err != nil {
					t.Fatalf("failed to marshal show request: %v", err)
				}
Patrick Devine's avatar
Patrick Devine committed
416
417
418
419
				req.Body = io.NopCloser(bytes.NewReader(jsonData))
			},
			Expected: func(t *testing.T, resp *http.Response) {
				contentType := resp.Header.Get("Content-Type")
420
421
422
				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
423
				body, err := io.ReadAll(resp.Body)
424
425
426
				if err != nil {
					t.Fatalf("failed to read response body: %v", err)
				}
Patrick Devine's avatar
Patrick Devine committed
427
428
429

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

				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",
				}
446
				if !slices.Equal(params, expectedParams) {
447
448
449
450
451
452
453
454
455
					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
456
457
			},
		},
458
		{
459
460
461
462
			Name: "openai retrieve model handler",
			Setup: func(t *testing.T, req *http.Request) {
				createTestModel(t, "show-model")
			},
463
464
465
466
			Method: http.MethodGet,
			Path:   "/v1/models/show-model",
			Expected: func(t *testing.T, resp *http.Response) {
				contentType := resp.Header.Get("Content-Type")
467
468
469
				if contentType != "application/json" {
					t.Errorf("expected content type application/json, got %s", contentType)
				}
470
				body, err := io.ReadAll(resp.Body)
471
472
473
				if err != nil {
					t.Fatalf("failed to read response body: %v", err)
				}
474

475
476
				var m openai.Model
				err = json.Unmarshal(body, &m)
477
478
479
				if err != nil {
					t.Fatalf("failed to unmarshal response body: %v", err)
				}
480

481
482
				if m.Id != "show-model" || m.OwnedBy != "library" {
					t.Errorf("expected model 'show-model' owned by 'library', got %v", m)
483
				}
484
485
			},
		},
486
487
488
489
490
491
492
493
494
495
		{
			Name:   "Method Not Allowed",
			Method: http.MethodGet,
			Path:   "/api/show",
			Expected: func(t *testing.T, resp *http.Response) {
				if resp.StatusCode != 405 {
					t.Errorf("expected status code 405, got %d", resp.StatusCode)
				}
			},
		},
496
497
	}

498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
	modelsDir := t.TempDir()
	t.Setenv("OLLAMA_MODELS", modelsDir)

	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,
	}
515

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

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

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

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

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

			if tc.Expected != nil {
				tc.Expected(t, resp)
			}
		})
547
548
	}
}
549

550
551
552
553
554
555
556
557
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])
		}
558
	}
559
560
	return string(rr)
}
561

562
563
func TestManifestCaseSensitivity(t *testing.T) {
	t.Setenv("OLLAMA_MODELS", t.TempDir())
564

565
566
567
568
569
570
571
572
573
574
575
576
577
	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
578
			}
579
580
581
582
			nameUsed[v] = true
			return v
		}
	}
583

584
	wantStableName := name()
585

586
587
	t.Logf("stable name: %s", wantStableName)

588
589
590
591
	// 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()
592

593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
		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)
		}
612

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

618
619
620
621
622
623
624
625
		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)
		}
	}
626

627
628
629
630
631
632
633
	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())
		}
	}
634

635
636
637
638
	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())
639
	}
640
641
642
	t.Cleanup(func() { testMakeRequestDialContext = nil })

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

	t.Logf("creating (again)")
	checkOK(createRequest(t, s.CreateHandler, api.CreateRequest{
655
656
657
		Name:   name(),
		Files:  map[string]string{"test.gguf": digest},
		Stream: &stream,
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
	}))
	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()
675
676
677
678
679
680
681
682
683
684
685
686

	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())
	}
687
}
688
689
690
691
692
693

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

	var s Server

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

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

702
	w := createRequest(t, s.ShowHandler, api.ShowRequest{
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
		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"])
	}
}
723
724
725

func TestNormalize(t *testing.T) {
	type testCase struct {
726
727
		input       []float32
		expectError bool
728
729
730
	}

	testCases := []testCase{
731
732
733
734
735
736
737
738
739
		{input: []float32{1}, expectError: false},
		{input: []float32{0, 1, 2, 3}, expectError: false},
		{input: []float32{0.1, 0.2, 0.3}, expectError: false},
		{input: []float32{-0.1, 0.2, 0.3, -0.4}, expectError: false},
		{input: []float32{0, 0, 0}, expectError: false},
		{input: []float32{float32(math.NaN()), 0.2, 0.3}, expectError: true},
		{input: []float32{0.1, float32(math.NaN()), 0.3}, expectError: true},
		{input: []float32{float32(math.Inf(1)), 0.2, 0.3}, expectError: true},
		{input: []float32{float32(math.Inf(-1)), 0.2, 0.3}, expectError: true},
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
	}

	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) {
756
757
758
759
760
761
762
763
764
765
766
767
			normalized, err := normalize(tc.input)
			if tc.expectError {
				if err == nil {
					t.Errorf("Expected error for input %v, but got none", tc.input)
				}
			} else {
				if err != nil {
					t.Errorf("Unexpected error for input %v: %v", tc.input, err)
				}
				if !isNormalized(normalized) {
					t.Errorf("Vector %v is not normalized", tc.input)
				}
768
769
770
771
			}
		})
	}
}
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791

func TestFilterThinkTags(t *testing.T) {
	type testCase struct {
		msgs  []api.Message
		want  []api.Message
		model *Model
	}
	testCases := []testCase{
		{
			msgs: []api.Message{
				{Role: "user", Content: "Hello, world!"},
				{Role: "assistant", Content: "<think>Thinking... about the answer</think>abc"},
				{Role: "user", Content: "What is the answer?"},
			},
			want: []api.Message{
				{Role: "user", Content: "Hello, world!"},
				{Role: "assistant", Content: "abc"},
				{Role: "user", Content: "What is the answer?"},
			},
			model: &Model{
792
				Config: model.ConfigV2{
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
					ModelFamily: "qwen3",
				},
			},
		},
		// with newlines inside the think tag aned newlines after
		{
			msgs: []api.Message{
				{Role: "user", Content: "Hello, world!"},
				{Role: "assistant", Content: "<think>Thinking... \n\nabout \nthe answer</think>\n\nabc\ndef"},
				{Role: "user", Content: "What is the answer?"},
			},
			want: []api.Message{
				{Role: "user", Content: "Hello, world!"},
				{Role: "assistant", Content: "abc\ndef"},
				{Role: "user", Content: "What is the answer?"},
			},
			model: &Model{
810
				Config: model.ConfigV2{
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
					ModelFamily: "qwen3",
				},
			},
		},
		// should leave thinking tags if it's after the last user message
		{
			msgs: []api.Message{
				{Role: "user", Content: "Hello, world!"},
				{Role: "assistant", Content: "<think>Thinking...</think>after"},
				{Role: "user", Content: "What is the answer?"},
				{Role: "assistant", Content: "<think>thinking again</think>hjk"},
				{Role: "assistant", Content: "<think>thinking yet again</think>hjk"},
			},
			want: []api.Message{
				{Role: "user", Content: "Hello, world!"},
				{Role: "assistant", Content: "after"},
				{Role: "user", Content: "What is the answer?"},
				{Role: "assistant", Content: "<think>thinking again</think>hjk"},
				{Role: "assistant", Content: "<think>thinking yet again</think>hjk"},
			},
			model: &Model{
832
				Config: model.ConfigV2{
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
					ModelFamily: "qwen3",
				},
			},
		},
		{
			// shouldn't strip anything because the model family isn't one of the hardcoded ones
			msgs: []api.Message{
				{Role: "user", Content: "Hello, world!"},
				{Role: "assistant", Content: "<think>Thinking... about the answer</think>abc"},
				{Role: "user", Content: "What is the answer?"},
			},
			want: []api.Message{
				{Role: "user", Content: "Hello, world!"},
				{Role: "assistant", Content: "<think>Thinking... about the answer</think>abc"},
				{Role: "user", Content: "What is the answer?"},
			},
			model: &Model{
850
				Config: model.ConfigV2{
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
					ModelFamily: "llama3",
				},
			},
		},
		{
			// deepseek-r1:-prefixed model
			msgs: []api.Message{
				{Role: "user", Content: "Hello, world!"},
				{Role: "assistant", Content: "<think>Thinking... about the answer</think>abc"},
				{Role: "user", Content: "What is the answer?"},
			},
			want: []api.Message{
				{Role: "user", Content: "Hello, world!"},
				{Role: "assistant", Content: "abc"},
				{Role: "user", Content: "What is the answer?"},
			},
			model: &Model{
				Name:      "registry.ollama.ai/library/deepseek-r1:latest",
				ShortName: "deepseek-r1:7b",
870
				Config:    model.ConfigV2{},
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
			},
		},
	}

	for i, tc := range testCases {
		filtered := filterThinkTags(tc.msgs, tc.model)

		if !reflect.DeepEqual(filtered, tc.want) {
			t.Errorf("messages differ for case %d:", i)
			for i := range tc.want {
				if i >= len(filtered) {
					t.Errorf("  missing message %d: %+v", i, tc.want[i])
					continue
				}
				if !reflect.DeepEqual(filtered[i], tc.want[i]) {
					t.Errorf("  message %d:\n    want: %+v\n    got:  %+v", i, tc.want[i], filtered[i])
				}
			}
			if len(filtered) > len(tc.want) {
				for i := len(tc.want); i < len(filtered); i++ {
					t.Errorf("  extra message %d: %+v", i, filtered[i])
				}
			}
		}
	}
}
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

func TestWaitForStream(t *testing.T) {
	gin.SetMode(gin.TestMode)

	cases := []struct {
		name       string
		messages   []any
		expectCode int
		expectBody string
	}{
		{
			name: "error",
			messages: []any{
				gin.H{"error": "internal server error"},
			},
			expectCode: http.StatusInternalServerError,
			expectBody: `{"error":"internal server error"}`,
		},
		{
			name: "error status",
			messages: []any{
				gin.H{"status": http.StatusNotFound, "error": "not found"},
			},
			expectCode: http.StatusNotFound,
			expectBody: `{"error":"not found"}`,
		},
		{
			name: "unknown error",
			messages: []any{
				gin.H{"msg": "something else"},
			},
			expectCode: http.StatusInternalServerError,
			expectBody: `{"error":"unknown error"}`,
		},
		{
			name: "unknown type",
			messages: []any{
				struct{}{},
			},
			expectCode: http.StatusInternalServerError,
			expectBody: `{"error":"unknown message type"}`,
		},
		{
			name: "progress success",
			messages: []any{
				api.ProgressResponse{Status: "success"},
			},
			expectCode: http.StatusOK,
			expectBody: `{"status":"success"}`,
		},
		{
			name: "progress more than success",
			messages: []any{
				api.ProgressResponse{Status: "success"},
				api.ProgressResponse{Status: "one more thing"},
			},
			expectCode: http.StatusOK,
			expectBody: `{"status":"one more thing"}`,
		},
	}

	for _, tt := range cases {
		t.Run(tt.name, func(t *testing.T) {
			w := httptest.NewRecorder()
			c, _ := gin.CreateTestContext(w)

			ch := make(chan any, len(tt.messages))
			for _, msg := range tt.messages {
				ch <- msg
			}
			close(ch)

			waitForStream(c, ch)

			if w.Code != tt.expectCode {
				t.Errorf("expected status %d, got %d", tt.expectCode, w.Code)
			}

			if diff := cmp.Diff(w.Body.String(), tt.expectBody); diff != "" {
				t.Errorf("body mismatch (-want +got):\n%s", diff)
			}
		})
	}
}