cmd.go 28.5 KB
Newer Older
Jeffrey Morgan's avatar
Jeffrey Morgan committed
1
2
3
package cmd

import (
4
	"archive/zip"
Michael Yang's avatar
Michael Yang committed
5
	"bytes"
Jeffrey Morgan's avatar
Jeffrey Morgan committed
6
	"context"
7
8
	"crypto/ed25519"
	"crypto/rand"
Michael Yang's avatar
Michael Yang committed
9
	"crypto/sha256"
10
	"encoding/pem"
Michael Yang's avatar
Michael Yang committed
11
	"errors"
Bruce MacDonald's avatar
Bruce MacDonald committed
12
	"fmt"
Michael Yang's avatar
Michael Yang committed
13
	"io"
Jeffrey Morgan's avatar
Jeffrey Morgan committed
14
	"log"
15
	"math"
Jeffrey Morgan's avatar
Jeffrey Morgan committed
16
	"net"
17
	"net/http"
Jeffrey Morgan's avatar
Jeffrey Morgan committed
18
	"os"
19
	"os/signal"
20
	"path/filepath"
21
	"regexp"
22
	"runtime"
23
	"slices"
Michael Yang's avatar
Michael Yang committed
24
	"strings"
25
	"syscall"
Michael Yang's avatar
Michael Yang committed
26
	"time"
Jeffrey Morgan's avatar
Jeffrey Morgan committed
27

28
	"github.com/containerd/console"
29
	"github.com/mattn/go-runewidth"
Patrick Devine's avatar
Patrick Devine committed
30
	"github.com/olekukonko/tablewriter"
Michael Yang's avatar
Michael Yang committed
31
	"github.com/spf13/cobra"
32
	"golang.org/x/crypto/ssh"
33
	"golang.org/x/term"
Michael Yang's avatar
Michael Yang committed
34

35
	"github.com/ollama/ollama/api"
36
	"github.com/ollama/ollama/auth"
37
	"github.com/ollama/ollama/envconfig"
38
	"github.com/ollama/ollama/format"
39
	"github.com/ollama/ollama/parser"
40
41
	"github.com/ollama/ollama/progress"
	"github.com/ollama/ollama/server"
42
43
	"github.com/ollama/ollama/types/errtypes"
	"github.com/ollama/ollama/types/model"
44
	"github.com/ollama/ollama/version"
Jeffrey Morgan's avatar
Jeffrey Morgan committed
45
46
)

47
func CreateHandler(cmd *cobra.Command, args []string) error {
48
	filename, _ := cmd.Flags().GetString("file")
49
50
51
52
53
	filename, err := filepath.Abs(filename)
	if err != nil {
		return err
	}

Michael Yang's avatar
Michael Yang committed
54
	client, err := api.ClientFromEnvironment()
55
56
57
	if err != nil {
		return err
	}
58

Michael Yang's avatar
Michael Yang committed
59
60
61
	p := progress.NewProgress(os.Stderr)
	defer p.Stop()

Michael Yang's avatar
Michael Yang committed
62
	f, err := os.Open(filename)
Michael Yang's avatar
Michael Yang committed
63
64
65
	if err != nil {
		return err
	}
Michael Yang's avatar
Michael Yang committed
66
	defer f.Close()
Michael Yang's avatar
Michael Yang committed
67

68
	modelfile, err := parser.ParseFile(f)
Michael Yang's avatar
Michael Yang committed
69
70
71
72
73
74
75
76
77
	if err != nil {
		return err
	}

	home, err := os.UserHomeDir()
	if err != nil {
		return err
	}

78
79
	status := "transferring model data"
	spinner := progress.NewSpinner(status)
80
81
	p.Add(status, spinner)

Michael Yang's avatar
Michael Yang committed
82
83
	for i := range modelfile.Commands {
		switch modelfile.Commands[i].Name {
Michael Yang's avatar
Michael Yang committed
84
		case "model", "adapter":
Michael Yang's avatar
Michael Yang committed
85
			path := modelfile.Commands[i].Args
Michael Yang's avatar
Michael Yang committed
86
87
88
89
90
91
			if path == "~" {
				path = home
			} else if strings.HasPrefix(path, "~/") {
				path = filepath.Join(home, path[2:])
			}

92
93
94
95
			if !filepath.IsAbs(path) {
				path = filepath.Join(filepath.Dir(filename), path)
			}

96
			fi, err := os.Stat(path)
Michael Yang's avatar
Michael Yang committed
97
			if errors.Is(err, os.ErrNotExist) && modelfile.Commands[i].Name == "model" {
Michael Yang's avatar
Michael Yang committed
98
				continue
Michael Yang's avatar
Michael Yang committed
99
100
101
102
			} else if err != nil {
				return err
			}

103
			if fi.IsDir() {
Michael Yang's avatar
Michael Yang committed
104
105
106
				// this is likely a safetensors or pytorch directory
				// TODO make this work w/ adapters
				tempfile, err := tempZipFiles(path)
107
108
109
				if err != nil {
					return err
				}
Michael Yang's avatar
Michael Yang committed
110
				defer os.RemoveAll(tempfile)
111

Michael Yang's avatar
Michael Yang committed
112
				path = tempfile
Michael Yang's avatar
Michael Yang committed
113
114
			}

115
116
			digest, err := createBlob(cmd, client, path)
			if err != nil {
Michael Yang's avatar
Michael Yang committed
117
118
119
				return err
			}

Michael Yang's avatar
Michael Yang committed
120
			modelfile.Commands[i].Args = "@" + digest
Michael Yang's avatar
Michael Yang committed
121
122
		}
	}
Michael Yang's avatar
Michael Yang committed
123

Michael Yang's avatar
Michael Yang committed
124
	bars := make(map[string]*progress.Bar)
125
	fn := func(resp api.ProgressResponse) error {
Michael Yang's avatar
Michael Yang committed
126
127
128
129
130
		if resp.Digest != "" {
			spinner.Stop()

			bar, ok := bars[resp.Digest]
			if !ok {
131
				bar = progress.NewBar(fmt.Sprintf("pulling %s...", resp.Digest[7:19]), resp.Total, resp.Completed)
Michael Yang's avatar
Michael Yang committed
132
133
134
135
136
137
138
139
140
141
142
143
144
				bars[resp.Digest] = bar
				p.Add(resp.Digest, bar)
			}

			bar.Set(resp.Completed)
		} else if status != resp.Status {
			spinner.Stop()

			status = resp.Status
			spinner = progress.NewSpinner(status)
			p.Add(status, spinner)
		}

145
146
147
		return nil
	}

148
	quantize, _ := cmd.Flags().GetString("quantize")
Michael Yang's avatar
Michael Yang committed
149

150
	request := api.CreateRequest{Name: args[0], Modelfile: modelfile.String(), Quantize: quantize}
Michael Yang's avatar
Michael Yang committed
151
	if err := client.Create(cmd.Context(), &request, fn); err != nil {
152
153
154
155
156
157
		return err
	}

	return nil
}

Michael Yang's avatar
Michael Yang committed
158
159
160
161
162
163
164
165
166
167
func tempZipFiles(path string) (string, error) {
	tempfile, err := os.CreateTemp("", "ollama-tf")
	if err != nil {
		return "", err
	}
	defer tempfile.Close()

	zipfile := zip.NewWriter(tempfile)
	defer zipfile.Close()

Michael Yang's avatar
Michael Yang committed
168
169
	detectContentType := func(path string) (string, error) {
		f, err := os.Open(path)
Michael Yang's avatar
Michael Yang committed
170
171
172
		if err != nil {
			return "", err
		}
Michael Yang's avatar
Michael Yang committed
173
		defer f.Close()
Michael Yang's avatar
Michael Yang committed
174

Michael Yang's avatar
Michael Yang committed
175
176
		var b bytes.Buffer
		b.Grow(512)
Michael Yang's avatar
Michael Yang committed
177

Michael Yang's avatar
Michael Yang committed
178
179
180
181
182
183
		if _, err := io.CopyN(&b, f, 512); err != nil && !errors.Is(err, io.EOF) {
			return "", err
		}

		contentType, _, _ := strings.Cut(http.DetectContentType(b.Bytes()), ";")
		return contentType, nil
Michael Yang's avatar
Michael Yang committed
184
185
	}

Michael Yang's avatar
Michael Yang committed
186
187
188
189
190
191
192
193
194
195
196
	glob := func(pattern, contentType string) ([]string, error) {
		matches, err := filepath.Glob(pattern)
		if err != nil {
			return nil, err
		}

		for _, safetensor := range matches {
			if ct, err := detectContentType(safetensor); err != nil {
				return nil, err
			} else if ct != contentType {
				return nil, fmt.Errorf("invalid content type: expected %s for %s", ct, safetensor)
Michael Yang's avatar
Michael Yang committed
197
			}
Michael Yang's avatar
Michael Yang committed
198
199
200
201
202
203
204
205
206
207
208
209
210
211
		}

		return matches, nil
	}

	var files []string
	if st, _ := glob(filepath.Join(path, "model*.safetensors"), "application/octet-stream"); len(st) > 0 {
		// safetensors files might be unresolved git lfs references; skip if they are
		// covers model-x-of-y.safetensors, model.fp32-x-of-y.safetensors, model.safetensors
		files = append(files, st...)
	} else if pt, _ := glob(filepath.Join(path, "pytorch_model*.bin"), "application/zip"); len(pt) > 0 {
		// pytorch files might also be unresolved git lfs references; skip if they are
		// covers pytorch_model-x-of-y.bin, pytorch_model.fp32-x-of-y.bin, pytorch_model.bin
		files = append(files, pt...)
Patrick Devine's avatar
Patrick Devine committed
212
	} else if pt, _ := glob(filepath.Join(path, "consolidated*.pth"), "application/zip"); len(pt) > 0 {
Michael Yang's avatar
Michael Yang committed
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
		// pytorch files might also be unresolved git lfs references; skip if they are
		// covers consolidated.x.pth, consolidated.pth
		files = append(files, pt...)
	} else {
		return "", errors.New("no safetensors or torch files found")
	}

	// add configuration files, json files are detected as text/plain
	js, err := glob(filepath.Join(path, "*.json"), "text/plain")
	if err != nil {
		return "", err
	}
	files = append(files, js...)

	if tks, _ := glob(filepath.Join(path, "tokenizer.model"), "application/octet-stream"); len(tks) > 0 {
		// add tokenizer.model if it exists, tokenizer.json is automatically picked up by the previous glob
		// tokenizer.model might be a unresolved git lfs reference; error if it is
		files = append(files, tks...)
	} else if tks, _ := glob(filepath.Join(path, "**/tokenizer.model"), "text/plain"); len(tks) > 0 {
		// some times tokenizer.model is in a subdirectory (e.g. meta-llama/Meta-Llama-3-8B)
		files = append(files, tks...)
	}

	for _, file := range files {
		f, err := os.Open(file)
		if err != nil {
Michael Yang's avatar
Michael Yang committed
239
240
			return "", err
		}
Michael Yang's avatar
Michael Yang committed
241
		defer f.Close()
Michael Yang's avatar
Michael Yang committed
242
243
244
245
246
247

		fi, err := f.Stat()
		if err != nil {
			return "", err
		}

Michael Yang's avatar
Michael Yang committed
248
		zfi, err := zip.FileInfoHeader(fi)
Michael Yang's avatar
Michael Yang committed
249
250
251
252
		if err != nil {
			return "", err
		}

Michael Yang's avatar
Michael Yang committed
253
		zf, err := zipfile.CreateHeader(zfi)
Michael Yang's avatar
Michael Yang committed
254
255
256
257
		if err != nil {
			return "", err
		}

Michael Yang's avatar
Michael Yang committed
258
		if _, err := io.Copy(zf, f); err != nil {
Michael Yang's avatar
Michael Yang committed
259
260
261
262
263
264
265
			return "", err
		}
	}

	return tempfile.Name(), nil
}

266
267
268
269
270
271
272
273
274
275
276
func createBlob(cmd *cobra.Command, client *api.Client, path string) (string, error) {
	bin, err := os.Open(path)
	if err != nil {
		return "", err
	}
	defer bin.Close()

	hash := sha256.New()
	if _, err := io.Copy(hash, bin); err != nil {
		return "", err
	}
277
278
279
280

	if _, err := bin.Seek(0, io.SeekStart); err != nil {
		return "", err
	}
281
282
283
284
285
286
287
288

	digest := fmt.Sprintf("sha256:%x", hash.Sum(nil))
	if err = client.CreateBlob(cmd.Context(), digest, bin); err != nil {
		return "", err
	}
	return digest, nil
}

289
func RunHandler(cmd *cobra.Command, args []string) error {
Michael Yang's avatar
Michael Yang committed
290
	client, err := api.ClientFromEnvironment()
291
292
293
294
	if err != nil {
		return err
	}

295
	name := args[0]
296

297
	// check if the model exists on the server
298
	show, err := client.Show(cmd.Context(), &api.ShowRequest{Name: name})
Michael Yang's avatar
Michael Yang committed
299
300
301
	var statusError api.StatusError
	switch {
	case errors.As(err, &statusError) && statusError.StatusCode == http.StatusNotFound:
302
		if err := PullHandler(cmd, []string{name}); err != nil {
303
			return err
Michael Yang's avatar
Michael Yang committed
304
		}
305
306
307
308
309

		show, err = client.Show(cmd.Context(), &api.ShowRequest{Name: name})
		if err != nil {
			return err
		}
Michael Yang's avatar
Michael Yang committed
310
311
	case err != nil:
		return err
312
313
	}

314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
	interactive := true

	opts := runOptions{
		Model:       args[0],
		WordWrap:    os.Getenv("TERM") == "xterm-256color",
		Options:     map[string]interface{}{},
		MultiModal:  slices.Contains(show.Details.Families, "clip"),
		ParentModel: show.Details.ParentModel,
	}

	format, err := cmd.Flags().GetString("format")
	if err != nil {
		return err
	}
	opts.Format = format

330
331
332
333
334
335
336
337
338
339
340
341
	keepAlive, err := cmd.Flags().GetString("keepalive")
	if err != nil {
		return err
	}
	if keepAlive != "" {
		d, err := time.ParseDuration(keepAlive)
		if err != nil {
			return err
		}
		opts.KeepAlive = &api.Duration{Duration: d}
	}

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
	prompts := args[1:]
	// prepend stdin to the prompt if provided
	if !term.IsTerminal(int(os.Stdin.Fd())) {
		in, err := io.ReadAll(os.Stdin)
		if err != nil {
			return err
		}

		prompts = append([]string{string(in)}, prompts...)
		opts.WordWrap = false
		interactive = false
	}
	opts.Prompt = strings.Join(prompts, " ")
	if len(prompts) > 0 {
		interactive = false
	}

	nowrap, err := cmd.Flags().GetBool("nowordwrap")
	if err != nil {
		return err
	}
	opts.WordWrap = !nowrap

	if !interactive {
		return generate(cmd, opts)
	}

	return generateInteractive(cmd, opts)
Bruce MacDonald's avatar
Bruce MacDonald committed
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
func errFromUnknownKey(unknownKeyErr error) error {
	// find SSH public key in the error message
	sshKeyPattern := `ssh-\w+ [^\s"]+`
	re := regexp.MustCompile(sshKeyPattern)
	matches := re.FindStringSubmatch(unknownKeyErr.Error())

	if len(matches) > 0 {
		serverPubKey := matches[0]

		localPubKey, err := auth.GetPublicKey()
		if err != nil {
			return unknownKeyErr
		}

		if runtime.GOOS == "linux" && serverPubKey != localPubKey {
			// try the ollama service public key
			svcPubKey, err := os.ReadFile("/usr/share/ollama/.ollama/id_ed25519.pub")
			if err != nil {
				return unknownKeyErr
			}
			localPubKey = strings.TrimSpace(string(svcPubKey))
		}

		// check if the returned public key matches the local public key, this prevents adding a remote key to the user's account
		if serverPubKey != localPubKey {
			return unknownKeyErr
		}

		var msg strings.Builder
		msg.WriteString(unknownKeyErr.Error())
		msg.WriteString("\n\nYour ollama key is:\n")
		msg.WriteString(localPubKey)
		msg.WriteString("\nAdd your key at:\n")
		msg.WriteString("https://ollama.com/settings/keys")

		return errors.New(msg.String())
	}

	return unknownKeyErr
}

413
func PushHandler(cmd *cobra.Command, args []string) error {
Michael Yang's avatar
Michael Yang committed
414
	client, err := api.ClientFromEnvironment()
415
416
417
	if err != nil {
		return err
	}
418

419
420
421
422
423
	insecure, err := cmd.Flags().GetBool("insecure")
	if err != nil {
		return err
	}

Michael Yang's avatar
Michael Yang committed
424
425
426
427
	p := progress.NewProgress(os.Stderr)
	defer p.Stop()

	bars := make(map[string]*progress.Bar)
428
429
	var status string
	var spinner *progress.Spinner
Michael Yang's avatar
Michael Yang committed
430

431
	fn := func(resp api.ProgressResponse) error {
Michael Yang's avatar
Michael Yang committed
432
		if resp.Digest != "" {
433
434
435
			if spinner != nil {
				spinner.Stop()
			}
Michael Yang's avatar
Michael Yang committed
436
437
438

			bar, ok := bars[resp.Digest]
			if !ok {
439
				bar = progress.NewBar(fmt.Sprintf("pushing %s...", resp.Digest[7:19]), resp.Total, resp.Completed)
Michael Yang's avatar
Michael Yang committed
440
441
442
443
444
445
				bars[resp.Digest] = bar
				p.Add(resp.Digest, bar)
			}

			bar.Set(resp.Completed)
		} else if status != resp.Status {
446
447
448
			if spinner != nil {
				spinner.Stop()
			}
Michael Yang's avatar
Michael Yang committed
449
450
451
452
453
454

			status = resp.Status
			spinner = progress.NewSpinner(status)
			p.Add(status, spinner)
		}

455
456
457
		return nil
	}

Michael Yang's avatar
Michael Yang committed
458
	request := api.PushRequest{Name: args[0], Insecure: insecure}
Michael Yang's avatar
Michael Yang committed
459
	if err := client.Push(cmd.Context(), &request, fn); err != nil {
460
461
462
463
464
465
466
467
468
469
470
471
472
473
		if spinner != nil {
			spinner.Stop()
		}
		if strings.Contains(err.Error(), "access denied") {
			return errors.New("you are not authorized to push to this namespace, create the model under a namespace you own")
		}
		host := model.ParseName(args[0]).Host
		isOllamaHost := strings.HasSuffix(host, ".ollama.ai") || strings.HasSuffix(host, ".ollama.com")
		if strings.Contains(err.Error(), errtypes.UnknownOllamaKeyErrMsg) && isOllamaHost {
			// the user has not added their ollama key to ollama.com
			// re-throw an error with a more user-friendly message
			return errFromUnknownKey(err)
		}

Michael Yang's avatar
Michael Yang committed
474
475
476
		return err
	}

477
	spinner.Stop()
Michael Yang's avatar
Michael Yang committed
478
	return nil
479
480
}

481
func ListHandler(cmd *cobra.Command, args []string) error {
Michael Yang's avatar
Michael Yang committed
482
	client, err := api.ClientFromEnvironment()
483
484
485
	if err != nil {
		return err
	}
Patrick Devine's avatar
Patrick Devine committed
486

Michael Yang's avatar
Michael Yang committed
487
	models, err := client.List(cmd.Context())
Patrick Devine's avatar
Patrick Devine committed
488
489
490
491
492
493
494
	if err != nil {
		return err
	}

	var data [][]string

	for _, m := range models.Models {
Michael Yang's avatar
Michael Yang committed
495
		if len(args) == 0 || strings.HasPrefix(m.Name, args[0]) {
496
			data = append(data, []string{m.Name, m.Digest[:12], format.HumanBytes(m.Size), format.HumanTime(m.ModifiedAt, "Never")})
Michael Yang's avatar
Michael Yang committed
497
		}
Patrick Devine's avatar
Patrick Devine committed
498
499
500
	}

	table := tablewriter.NewWriter(os.Stdout)
Patrick Devine's avatar
Patrick Devine committed
501
	table.SetHeader([]string{"NAME", "ID", "SIZE", "MODIFIED"})
Patrick Devine's avatar
Patrick Devine committed
502
503
504
505
506
507
508
509
510
511
512
513
	table.SetHeaderAlignment(tablewriter.ALIGN_LEFT)
	table.SetAlignment(tablewriter.ALIGN_LEFT)
	table.SetHeaderLine(false)
	table.SetBorder(false)
	table.SetNoWhiteSpace(true)
	table.SetTablePadding("\t")
	table.AppendBulk(data)
	table.Render()

	return nil
}

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
func ListRunningHandler(cmd *cobra.Command, args []string) error {
	client, err := api.ClientFromEnvironment()
	if err != nil {
		return err
	}

	models, err := client.ListRunning(cmd.Context())
	if err != nil {
		return err
	}

	var data [][]string

	for _, m := range models.Models {
		if len(args) == 0 || strings.HasPrefix(m.Name, args[0]) {
			var procStr string
			switch {
			case m.SizeVRAM == 0:
				procStr = "100% CPU"
			case m.SizeVRAM == m.Size:
				procStr = "100% GPU"
			case m.SizeVRAM > m.Size || m.Size == 0:
				procStr = "Unknown"
			default:
				sizeCPU := m.Size - m.SizeVRAM
				cpuPercent := math.Round(float64(sizeCPU) / float64(m.Size) * 100)
				procStr = fmt.Sprintf("%d%%/%d%% CPU/GPU", int(cpuPercent), int(100-cpuPercent))
			}
			data = append(data, []string{m.Name, m.Digest[:12], format.HumanBytes(m.Size), procStr, format.HumanTime(m.ExpiresAt, "Never")})
		}
	}

	table := tablewriter.NewWriter(os.Stdout)
	table.SetHeader([]string{"NAME", "ID", "SIZE", "PROCESSOR", "UNTIL"})
	table.SetHeaderAlignment(tablewriter.ALIGN_LEFT)
	table.SetAlignment(tablewriter.ALIGN_LEFT)
	table.SetHeaderLine(false)
	table.SetBorder(false)
	table.SetNoWhiteSpace(true)
	table.SetTablePadding("\t")
	table.AppendBulk(data)
	table.Render()

	return nil
}

560
func DeleteHandler(cmd *cobra.Command, args []string) error {
Michael Yang's avatar
Michael Yang committed
561
	client, err := api.ClientFromEnvironment()
562
563
564
	if err != nil {
		return err
	}
565

566
567
	for _, name := range args {
		req := api.DeleteRequest{Name: name}
Michael Yang's avatar
Michael Yang committed
568
		if err := client.Delete(cmd.Context(), &req); err != nil {
569
570
571
			return err
		}
		fmt.Printf("deleted '%s'\n", name)
572
573
574
575
	}
	return nil
}

Patrick Devine's avatar
Patrick Devine committed
576
func ShowHandler(cmd *cobra.Command, args []string) error {
Michael Yang's avatar
Michael Yang committed
577
	client, err := api.ClientFromEnvironment()
Patrick Devine's avatar
Patrick Devine committed
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
	if err != nil {
		return err
	}

	if len(args) != 1 {
		return errors.New("missing model name")
	}

	license, errLicense := cmd.Flags().GetBool("license")
	modelfile, errModelfile := cmd.Flags().GetBool("modelfile")
	parameters, errParams := cmd.Flags().GetBool("parameters")
	system, errSystem := cmd.Flags().GetBool("system")
	template, errTemplate := cmd.Flags().GetBool("template")

	for _, boolErr := range []error{errLicense, errModelfile, errParams, errSystem, errTemplate} {
		if boolErr != nil {
			return errors.New("error retrieving flags")
		}
	}

	flagsSet := 0
	showType := ""

	if license {
		flagsSet++
		showType = "license"
	}

	if modelfile {
		flagsSet++
		showType = "modelfile"
	}

	if parameters {
		flagsSet++
		showType = "parameters"
	}

	if system {
		flagsSet++
		showType = "system"
	}

	if template {
		flagsSet++
		showType = "template"
	}

	if flagsSet > 1 {
627
		return errors.New("only one of '--license', '--modelfile', '--parameters', '--system', or '--template' can be specified")
Patrick Devine's avatar
Patrick Devine committed
628
	} else if flagsSet == 0 {
629
		return errors.New("one of '--license', '--modelfile', '--parameters', '--system', or '--template' must be specified")
Patrick Devine's avatar
Patrick Devine committed
630
631
	}

632
	req := api.ShowRequest{Name: args[0]}
Michael Yang's avatar
Michael Yang committed
633
	resp, err := client.Show(cmd.Context(), &req)
Patrick Devine's avatar
Patrick Devine committed
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
	if err != nil {
		return err
	}

	switch showType {
	case "license":
		fmt.Println(resp.License)
	case "modelfile":
		fmt.Println(resp.Modelfile)
	case "parameters":
		fmt.Println(resp.Parameters)
	case "system":
		fmt.Println(resp.System)
	case "template":
		fmt.Println(resp.Template)
	}

	return nil
}

Patrick Devine's avatar
Patrick Devine committed
654
func CopyHandler(cmd *cobra.Command, args []string) error {
Michael Yang's avatar
Michael Yang committed
655
	client, err := api.ClientFromEnvironment()
656
657
658
	if err != nil {
		return err
	}
Patrick Devine's avatar
Patrick Devine committed
659
660

	req := api.CopyRequest{Source: args[0], Destination: args[1]}
Michael Yang's avatar
Michael Yang committed
661
	if err := client.Copy(cmd.Context(), &req); err != nil {
Patrick Devine's avatar
Patrick Devine committed
662
663
664
665
666
667
		return err
	}
	fmt.Printf("copied '%s' to '%s'\n", args[0], args[1])
	return nil
}

668
func PullHandler(cmd *cobra.Command, args []string) error {
669
670
671
672
673
	insecure, err := cmd.Flags().GetBool("insecure")
	if err != nil {
		return err
	}

Michael Yang's avatar
Michael Yang committed
674
	client, err := api.ClientFromEnvironment()
675
676
677
	if err != nil {
		return err
	}
678

Michael Yang's avatar
Michael Yang committed
679
680
681
682
683
	p := progress.NewProgress(os.Stderr)
	defer p.Stop()

	bars := make(map[string]*progress.Bar)

684
685
	var status string
	var spinner *progress.Spinner
Michael Yang's avatar
Michael Yang committed
686

687
	fn := func(resp api.ProgressResponse) error {
Michael Yang's avatar
Michael Yang committed
688
		if resp.Digest != "" {
689
690
691
			if spinner != nil {
				spinner.Stop()
			}
Michael Yang's avatar
Michael Yang committed
692
693
694

			bar, ok := bars[resp.Digest]
			if !ok {
695
				bar = progress.NewBar(fmt.Sprintf("pulling %s...", resp.Digest[7:19]), resp.Total, resp.Completed)
Michael Yang's avatar
Michael Yang committed
696
697
698
699
700
701
				bars[resp.Digest] = bar
				p.Add(resp.Digest, bar)
			}

			bar.Set(resp.Completed)
		} else if status != resp.Status {
702
703
704
			if spinner != nil {
				spinner.Stop()
			}
Michael Yang's avatar
Michael Yang committed
705
706
707
708
709
710

			status = resp.Status
			spinner = progress.NewSpinner(status)
			p.Add(status, spinner)
		}

711
712
		return nil
	}
713

Michael Yang's avatar
Michael Yang committed
714
	request := api.PullRequest{Name: args[0], Insecure: insecure}
Michael Yang's avatar
Michael Yang committed
715
	if err := client.Pull(cmd.Context(), &request, fn); err != nil {
Michael Yang's avatar
Michael Yang committed
716
717
718
719
		return err
	}

	return nil
Michael Yang's avatar
Michael Yang committed
720
721
}

722
723
type generateContextKey string

724
type runOptions struct {
725
726
727
728
729
730
731
732
733
734
735
	Model       string
	ParentModel string
	Prompt      string
	Messages    []api.Message
	WordWrap    bool
	Format      string
	System      string
	Template    string
	Images      []api.ImageData
	Options     map[string]interface{}
	MultiModal  bool
736
	KeepAlive   *api.Duration
737
738
}

739
740
741
742
743
744
745
746
747
type displayResponseState struct {
	lineLength int
	wordBuffer string
}

func displayResponse(content string, wordWrap bool, state *displayResponseState) {
	termWidth, _, _ := term.GetSize(int(os.Stdout.Fd()))
	if wordWrap && termWidth >= 10 {
		for _, ch := range content {
Josh Yan's avatar
Josh Yan committed
748
749
			if state.lineLength+1 > termWidth-5 {
				if runewidth.StringWidth(state.wordBuffer) > termWidth-10 {
750
751
752
753
754
755
756
					fmt.Printf("%s%c", state.wordBuffer, ch)
					state.wordBuffer = ""
					state.lineLength = 0
					continue
				}

				// backtrack the length of the last word and clear to the end of the line
757
758
				a := runewidth.StringWidth(state.wordBuffer)
				if a > 0 {
759
					fmt.Printf("\x1b[%dD", a)
760
761
				}
				fmt.Printf("\x1b[K\n")
762
				fmt.Printf("%s%c", state.wordBuffer, ch)
763
764
765
				chWidth := runewidth.RuneWidth(ch)

				state.lineLength = runewidth.StringWidth(state.wordBuffer) + chWidth
766
767
			} else {
				fmt.Print(string(ch))
768
769
770
771
				state.lineLength += runewidth.RuneWidth(ch)
				if runewidth.RuneWidth(ch) >= 2 {
					state.wordBuffer = ""
					continue
Josh Yan's avatar
Josh Yan committed
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

				switch ch {
				case ' ':
					state.wordBuffer = ""
				case '\n':
					state.lineLength = 0
				default:
					state.wordBuffer += string(ch)
				}
			}
		}
	} else {
		fmt.Printf("%s%s", state.wordBuffer, content)
		if len(state.wordBuffer) > 0 {
			state.wordBuffer = ""
		}
	}
}

func chat(cmd *cobra.Command, opts runOptions) (*api.Message, error) {
	client, err := api.ClientFromEnvironment()
	if err != nil {
		return nil, err
	}

	p := progress.NewProgress(os.Stderr)
	defer p.StopAndClear()

	spinner := progress.NewSpinner("")
	p.Add("", spinner)

	cancelCtx, cancel := context.WithCancel(cmd.Context())
	defer cancel()

	sigChan := make(chan os.Signal, 1)
	signal.Notify(sigChan, syscall.SIGINT)

	go func() {
		<-sigChan
		cancel()
	}()

	var state *displayResponseState = &displayResponseState{}
	var latest api.ChatResponse
	var fullResponse strings.Builder
	var role string

	fn := func(response api.ChatResponse) error {
		p.StopAndClear()

		latest = response

		role = response.Message.Role
		content := response.Message.Content
		fullResponse.WriteString(content)

		displayResponse(content, opts.WordWrap, state)

		return nil
	}

	req := &api.ChatRequest{
		Model:    opts.Model,
		Messages: opts.Messages,
		Format:   opts.Format,
		Options:  opts.Options,
	}

841
842
843
844
	if opts.KeepAlive != nil {
		req.KeepAlive = opts.KeepAlive
	}

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
	if err := client.Chat(cancelCtx, req, fn); err != nil {
		if errors.Is(err, context.Canceled) {
			return nil, nil
		}
		return nil, err
	}

	if len(opts.Messages) > 0 {
		fmt.Println()
		fmt.Println()
	}

	verbose, err := cmd.Flags().GetBool("verbose")
	if err != nil {
		return nil, err
	}

	if verbose {
		latest.Summary()
	}

	return &api.Message{Role: role, Content: fullResponse.String()}, nil
}

func generate(cmd *cobra.Command, opts runOptions) error {
Michael Yang's avatar
Michael Yang committed
870
	client, err := api.ClientFromEnvironment()
Patrick Devine's avatar
Patrick Devine committed
871
	if err != nil {
872
		return err
Patrick Devine's avatar
Patrick Devine committed
873
	}
Michael Yang's avatar
Michael Yang committed
874

Michael Yang's avatar
Michael Yang committed
875
	p := progress.NewProgress(os.Stderr)
876
	defer p.StopAndClear()
877

Michael Yang's avatar
Michael Yang committed
878
879
880
	spinner := progress.NewSpinner("")
	p.Add("", spinner)

881
882
883
884
885
886
887
	var latest api.GenerateResponse

	generateContext, ok := cmd.Context().Value(generateContextKey("context")).([]int)
	if !ok {
		generateContext = []int{}
	}

Michael Yang's avatar
Michael Yang committed
888
	ctx, cancel := context.WithCancel(cmd.Context())
889
890
891
892
893
894
895
896
897
898
	defer cancel()

	sigChan := make(chan os.Signal, 1)
	signal.Notify(sigChan, syscall.SIGINT)

	go func() {
		<-sigChan
		cancel()
	}()

899
	var state *displayResponseState = &displayResponseState{}
900

901
	fn := func(response api.GenerateResponse) error {
Michael Yang's avatar
Michael Yang committed
902
		p.StopAndClear()
903

Patrick Devine's avatar
Patrick Devine committed
904
		latest = response
905
		content := response.Response
906

907
		displayResponse(content, opts.WordWrap, state)
908

Patrick Devine's avatar
Patrick Devine committed
909
910
		return nil
	}
911

912
913
914
915
916
917
918
	if opts.MultiModal {
		opts.Prompt, opts.Images, err = extractFileData(opts.Prompt)
		if err != nil {
			return err
		}
	}

Michael Yang's avatar
Michael Yang committed
919
	request := api.GenerateRequest{
920
921
922
923
924
925
926
927
928
		Model:     opts.Model,
		Prompt:    opts.Prompt,
		Context:   generateContext,
		Images:    opts.Images,
		Format:    opts.Format,
		System:    opts.System,
		Template:  opts.Template,
		Options:   opts.Options,
		KeepAlive: opts.KeepAlive,
Michael Yang's avatar
Michael Yang committed
929
930
931
	}

	if err := client.Generate(ctx, &request, fn); err != nil {
932
		if errors.Is(err, context.Canceled) {
933
			return nil
934
		}
935
		return err
Patrick Devine's avatar
Patrick Devine committed
936
	}
937

938
	if opts.Prompt != "" {
Michael Yang's avatar
Michael Yang committed
939
940
		fmt.Println()
		fmt.Println()
Patrick Devine's avatar
Patrick Devine committed
941
	}
942

943
944
945
946
	if !latest.Done {
		return nil
	}

Patrick Devine's avatar
Patrick Devine committed
947
948
	verbose, err := cmd.Flags().GetBool("verbose")
	if err != nil {
949
		return err
Patrick Devine's avatar
Patrick Devine committed
950
	}
Michael Yang's avatar
Michael Yang committed
951

Patrick Devine's avatar
Patrick Devine committed
952
953
	if verbose {
		latest.Summary()
Michael Yang's avatar
Michael Yang committed
954
	}
Michael Yang's avatar
Michael Yang committed
955

Patrick Devine's avatar
Patrick Devine committed
956
957
958
	ctx = context.WithValue(cmd.Context(), generateContextKey("context"), latest.Context)
	cmd.SetContext(ctx)

959
	return nil
Michael Yang's avatar
Michael Yang committed
960
961
}

962
func RunServer(cmd *cobra.Command, _ []string) error {
Michael Yang's avatar
Michael Yang committed
963
	if err := initializeKeypair(); err != nil {
964
965
966
		return err
	}

967
	ln, err := net.Listen("tcp", net.JoinHostPort(envconfig.Host.Host, envconfig.Host.Port))
968
969
970
	if err != nil {
		return err
	}
Jeffrey Morgan's avatar
Jeffrey Morgan committed
971

972
973
974
975
976
977
	err = server.Serve(ln)
	if errors.Is(err, http.ErrServerClosed) {
		return nil
	}

	return err
Jeffrey Morgan's avatar
Jeffrey Morgan committed
978
979
}

980
981
982
983
984
985
986
987
988
989
990
991
func initializeKeypair() error {
	home, err := os.UserHomeDir()
	if err != nil {
		return err
	}

	privKeyPath := filepath.Join(home, ".ollama", "id_ed25519")
	pubKeyPath := filepath.Join(home, ".ollama", "id_ed25519.pub")

	_, err = os.Stat(privKeyPath)
	if os.IsNotExist(err) {
		fmt.Printf("Couldn't find '%s'. Generating new private key.\n", privKeyPath)
Michael Yang's avatar
Michael Yang committed
992
		cryptoPublicKey, cryptoPrivateKey, err := ed25519.GenerateKey(rand.Reader)
993
994
995
996
		if err != nil {
			return err
		}

Michael Yang's avatar
Michael Yang committed
997
		privateKeyBytes, err := ssh.MarshalPrivateKey(cryptoPrivateKey, "")
998
999
1000
1001
		if err != nil {
			return err
		}

Michael Yang's avatar
Michael Yang committed
1002
		if err := os.MkdirAll(filepath.Dir(privKeyPath), 0o755); err != nil {
1003
1004
1005
			return fmt.Errorf("could not create directory %w", err)
		}

Michael Yang's avatar
Michael Yang committed
1006
		if err := os.WriteFile(privKeyPath, pem.EncodeToMemory(privateKeyBytes), 0o600); err != nil {
1007
1008
1009
			return err
		}

Michael Yang's avatar
Michael Yang committed
1010
		sshPublicKey, err := ssh.NewPublicKey(cryptoPublicKey)
1011
1012
1013
1014
		if err != nil {
			return err
		}

Michael Yang's avatar
Michael Yang committed
1015
		publicKeyBytes := ssh.MarshalAuthorizedKey(sshPublicKey)
1016

Michael Yang's avatar
Michael Yang committed
1017
		if err := os.WriteFile(pubKeyPath, publicKeyBytes, 0o644); err != nil {
1018
1019
1020
			return err
		}

Michael Yang's avatar
Michael Yang committed
1021
		fmt.Printf("Your new public key is: \n\n%s\n", publicKeyBytes)
1022
1023
1024
1025
	}
	return nil
}

Michael Yang's avatar
Michael Yang committed
1026
func checkServerHeartbeat(cmd *cobra.Command, _ []string) error {
Michael Yang's avatar
Michael Yang committed
1027
	client, err := api.ClientFromEnvironment()
1028
1029
1030
	if err != nil {
		return err
	}
Michael Yang's avatar
Michael Yang committed
1031
	if err := client.Heartbeat(cmd.Context()); err != nil {
1032
		if !strings.Contains(err.Error(), " refused") {
Bruce MacDonald's avatar
Bruce MacDonald committed
1033
1034
			return err
		}
1035
1036
		if err := startApp(cmd.Context(), client); err != nil {
			return fmt.Errorf("could not connect to ollama app, is it running?")
1037
1038
1039
1040
1041
		}
	}
	return nil
}

Michael Yang's avatar
Michael Yang committed
1042
1043
1044
1045
1046
1047
1048
1049
func versionHandler(cmd *cobra.Command, _ []string) {
	client, err := api.ClientFromEnvironment()
	if err != nil {
		return
	}

	serverVersion, err := client.Version(cmd.Context())
	if err != nil {
Michael Yang's avatar
Michael Yang committed
1050
1051
1052
1053
1054
		fmt.Println("Warning: could not connect to a running Ollama instance")
	}

	if serverVersion != "" {
		fmt.Printf("ollama version is %s\n", serverVersion)
Michael Yang's avatar
Michael Yang committed
1055
1056
	}

1057
	if serverVersion != version.Version {
Michael Yang's avatar
Michael Yang committed
1058
		fmt.Printf("Warning: client version is %s\n", version.Version)
1059
	}
Michael Yang's avatar
Michael Yang committed
1060
1061
}

1062
func appendEnvDocs(cmd *cobra.Command, envs []envconfig.EnvVar) {
1063
1064
1065
1066
1067
	if len(envs) == 0 {
		return
	}

	envUsage := `
1068
1069
Environment Variables:
`
1070
	for _, e := range envs {
1071
		envUsage += fmt.Sprintf("      %-24s   %s\n", e.Name, e.Description)
1072
1073
1074
	}

	cmd.SetUsageTemplate(cmd.UsageTemplate() + envUsage)
1075
1076
}

Jeffrey Morgan's avatar
Jeffrey Morgan committed
1077
1078
func NewCLI() *cobra.Command {
	log.SetFlags(log.LstdFlags | log.Lshortfile)
Michael Yang's avatar
Michael Yang committed
1079
	cobra.EnableCommandSorting = false
Jeffrey Morgan's avatar
Jeffrey Morgan committed
1080

1081
	if runtime.GOOS == "windows" {
1082
		console.ConsoleFromFile(os.Stdin) //nolint:errcheck
1083
1084
	}

Jeffrey Morgan's avatar
Jeffrey Morgan committed
1085
	rootCmd := &cobra.Command{
1086
1087
1088
1089
		Use:           "ollama",
		Short:         "Large language model runner",
		SilenceUsage:  true,
		SilenceErrors: true,
Jeffrey Morgan's avatar
Jeffrey Morgan committed
1090
1091
1092
		CompletionOptions: cobra.CompletionOptions{
			DisableDefaultCmd: true,
		},
Michael Yang's avatar
Michael Yang committed
1093
1094
1095
1096
1097
1098
1099
1100
		Run: func(cmd *cobra.Command, args []string) {
			if version, _ := cmd.Flags().GetBool("version"); version {
				versionHandler(cmd, args)
				return
			}

			cmd.Print(cmd.UsageString())
		},
Jeffrey Morgan's avatar
Jeffrey Morgan committed
1101
1102
	}

Michael Yang's avatar
Michael Yang committed
1103
	rootCmd.Flags().BoolP("version", "v", false, "Show version information")
Jeffrey Morgan's avatar
Jeffrey Morgan committed
1104

1105
	createCmd := &cobra.Command{
1106
1107
		Use:     "create MODEL",
		Short:   "Create a model from a Modelfile",
Michael Yang's avatar
Michael Yang committed
1108
		Args:    cobra.ExactArgs(1),
1109
1110
		PreRunE: checkServerHeartbeat,
		RunE:    CreateHandler,
1111
1112
	}

1113
	createCmd.Flags().StringP("file", "f", "Modelfile", "Name of the Modelfile")
1114
	createCmd.Flags().StringP("quantize", "q", "", "Quantize model to this level (e.g. q4_0)")
1115

Patrick Devine's avatar
Patrick Devine committed
1116
1117
1118
	showCmd := &cobra.Command{
		Use:     "show MODEL",
		Short:   "Show information for a model",
Michael Yang's avatar
Michael Yang committed
1119
		Args:    cobra.ExactArgs(1),
Patrick Devine's avatar
Patrick Devine committed
1120
1121
1122
1123
1124
1125
1126
1127
		PreRunE: checkServerHeartbeat,
		RunE:    ShowHandler,
	}

	showCmd.Flags().Bool("license", false, "Show license of a model")
	showCmd.Flags().Bool("modelfile", false, "Show Modelfile of a model")
	showCmd.Flags().Bool("parameters", false, "Show parameters of a model")
	showCmd.Flags().Bool("template", false, "Show template of a model")
1128
	showCmd.Flags().Bool("system", false, "Show system message of a model")
Patrick Devine's avatar
Patrick Devine committed
1129

Jeffrey Morgan's avatar
Jeffrey Morgan committed
1130
	runCmd := &cobra.Command{
1131
1132
1133
1134
1135
		Use:     "run MODEL [PROMPT]",
		Short:   "Run a model",
		Args:    cobra.MinimumNArgs(1),
		PreRunE: checkServerHeartbeat,
		RunE:    RunHandler,
Jeffrey Morgan's avatar
Jeffrey Morgan committed
1136
1137
	}

1138
	runCmd.Flags().String("keepalive", "", "Duration to keep a model loaded (e.g. 5m)")
1139
	runCmd.Flags().Bool("verbose", false, "Show timings for response")
1140
	runCmd.Flags().Bool("insecure", false, "Use an insecure registry")
1141
	runCmd.Flags().Bool("nowordwrap", false, "Don't wrap words to the next line automatically")
Jeffrey Morgan's avatar
Jeffrey Morgan committed
1142
	runCmd.Flags().String("format", "", "Response format (e.g. json)")
Jeffrey Morgan's avatar
Jeffrey Morgan committed
1143
1144
1145
1146
	serveCmd := &cobra.Command{
		Use:     "serve",
		Aliases: []string{"start"},
		Short:   "Start ollama",
Michael Yang's avatar
Michael Yang committed
1147
		Args:    cobra.ExactArgs(0),
Michael Yang's avatar
Michael Yang committed
1148
		RunE:    RunServer,
Jeffrey Morgan's avatar
Jeffrey Morgan committed
1149
1150
	}

1151
	pullCmd := &cobra.Command{
1152
1153
		Use:     "pull MODEL",
		Short:   "Pull a model from a registry",
Michael Yang's avatar
Michael Yang committed
1154
		Args:    cobra.ExactArgs(1),
1155
1156
		PreRunE: checkServerHeartbeat,
		RunE:    PullHandler,
1157
1158
	}

1159
1160
	pullCmd.Flags().Bool("insecure", false, "Use an insecure registry")

1161
	pushCmd := &cobra.Command{
1162
1163
		Use:     "push MODEL",
		Short:   "Push a model to a registry",
Michael Yang's avatar
Michael Yang committed
1164
		Args:    cobra.ExactArgs(1),
1165
1166
		PreRunE: checkServerHeartbeat,
		RunE:    PushHandler,
1167
1168
	}

1169
1170
	pushCmd.Flags().Bool("insecure", false, "Use an insecure registry")

Patrick Devine's avatar
Patrick Devine committed
1171
	listCmd := &cobra.Command{
1172
		Use:     "list",
Patrick Devine's avatar
Patrick Devine committed
1173
		Aliases: []string{"ls"},
1174
		Short:   "List models",
1175
		PreRunE: checkServerHeartbeat,
1176
		RunE:    ListHandler,
1177
	}
1178
1179
1180
1181
1182
1183
1184
1185

	psCmd := &cobra.Command{
		Use:     "ps",
		Short:   "List running models",
		PreRunE: checkServerHeartbeat,
		RunE:    ListRunningHandler,
	}

Patrick Devine's avatar
Patrick Devine committed
1186
	copyCmd := &cobra.Command{
Michael Yang's avatar
Michael Yang committed
1187
		Use:     "cp SOURCE DESTINATION",
1188
		Short:   "Copy a model",
Michael Yang's avatar
Michael Yang committed
1189
		Args:    cobra.ExactArgs(2),
1190
1191
		PreRunE: checkServerHeartbeat,
		RunE:    CopyHandler,
Patrick Devine's avatar
Patrick Devine committed
1192
1193
	}

1194
	deleteCmd := &cobra.Command{
Michael Yang's avatar
Michael Yang committed
1195
		Use:     "rm MODEL [MODEL...]",
1196
1197
1198
1199
		Short:   "Remove a model",
		Args:    cobra.MinimumNArgs(1),
		PreRunE: checkServerHeartbeat,
		RunE:    DeleteHandler,
Patrick Devine's avatar
Patrick Devine committed
1200
1201
	}

1202
1203
1204
	envVars := envconfig.AsMap()

	envs := []envconfig.EnvVar{envVars["OLLAMA_HOST"]}
1205

1206
1207
1208
1209
1210
1211
1212
	for _, cmd := range []*cobra.Command{
		createCmd,
		showCmd,
		runCmd,
		pullCmd,
		pushCmd,
		listCmd,
1213
		psCmd,
1214
1215
		copyCmd,
		deleteCmd,
1216
		serveCmd,
1217
	} {
1218
1219
		switch cmd {
		case runCmd:
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
			appendEnvDocs(cmd, []envconfig.EnvVar{envVars["OLLAMA_HOST"], envVars["OLLAMA_NOHISTORY"]})
		case serveCmd:
			appendEnvDocs(cmd, []envconfig.EnvVar{
				envVars["OLLAMA_DEBUG"],
				envVars["OLLAMA_HOST"],
				envVars["OLLAMA_KEEP_ALIVE"],
				envVars["OLLAMA_MAX_LOADED_MODELS"],
				envVars["OLLAMA_MAX_QUEUE"],
				envVars["OLLAMA_MODELS"],
				envVars["OLLAMA_NUM_PARALLEL"],
				envVars["OLLAMA_NOPRUNE"],
				envVars["OLLAMA_ORIGINS"],
				envVars["OLLAMA_TMPDIR"],
1233
1234
1235
				envVars["OLLAMA_FLASH_ATTENTION"],
				envVars["OLLAMA_LLM_LIBRARY"],
				envVars["OLLAMA_MAX_VRAM"],
1236
			})
1237
1238
1239
		default:
			appendEnvDocs(cmd, envs)
		}
1240
1241
	}

Jeffrey Morgan's avatar
Jeffrey Morgan committed
1242
1243
	rootCmd.AddCommand(
		serveCmd,
1244
		createCmd,
Patrick Devine's avatar
Patrick Devine committed
1245
		showCmd,
1246
		runCmd,
1247
1248
		pullCmd,
		pushCmd,
Patrick Devine's avatar
Patrick Devine committed
1249
		listCmd,
1250
		psCmd,
Patrick Devine's avatar
Patrick Devine committed
1251
		copyCmd,
1252
		deleteCmd,
Jeffrey Morgan's avatar
Jeffrey Morgan committed
1253
1254
1255
1256
	)

	return rootCmd
}