cmd.go 24.9 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
15
	"log"
	"net"
16
	"net/http"
Jeffrey Morgan's avatar
Jeffrey Morgan committed
17
	"os"
18
	"os/signal"
19
	"path/filepath"
20
	"regexp"
21
	"runtime"
Michael Yang's avatar
Michael Yang committed
22
	"strings"
23
	"syscall"
Michael Yang's avatar
Michael Yang committed
24
	"time"
Jeffrey Morgan's avatar
Jeffrey Morgan committed
25

26
27
	"github.com/containerd/console"

Patrick Devine's avatar
Patrick Devine committed
28
	"github.com/olekukonko/tablewriter"
Michael Yang's avatar
Michael Yang committed
29
	"github.com/spf13/cobra"
30
	"golang.org/x/crypto/ssh"
31
	"golang.org/x/exp/slices"
32
	"golang.org/x/term"
Michael Yang's avatar
Michael Yang committed
33

34
35
36
37
38
39
	"github.com/ollama/ollama/api"
	"github.com/ollama/ollama/format"
	"github.com/ollama/ollama/parser"
	"github.com/ollama/ollama/progress"
	"github.com/ollama/ollama/server"
	"github.com/ollama/ollama/version"
Jeffrey Morgan's avatar
Jeffrey Morgan committed
40
41
)

42
func CreateHandler(cmd *cobra.Command, args []string) error {
43
	filename, _ := cmd.Flags().GetString("file")
44
45
46
47
48
	filename, err := filepath.Abs(filename)
	if err != nil {
		return err
	}

Michael Yang's avatar
Michael Yang committed
49
	client, err := api.ClientFromEnvironment()
50
51
52
	if err != nil {
		return err
	}
53

Michael Yang's avatar
Michael Yang committed
54
55
56
	p := progress.NewProgress(os.Stderr)
	defer p.Stop()

Michael Yang's avatar
Michael Yang committed
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
	modelfile, err := os.ReadFile(filename)
	if err != nil {
		return err
	}

	commands, err := parser.Parse(bytes.NewReader(modelfile))
	if err != nil {
		return err
	}

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

72
73
	status := "transferring model data"
	spinner := progress.NewSpinner(status)
74
75
	p.Add(status, spinner)

Michael Yang's avatar
Michael Yang committed
76
77
78
79
80
81
82
83
84
85
	for _, c := range commands {
		switch c.Name {
		case "model", "adapter":
			path := c.Args
			if path == "~" {
				path = home
			} else if strings.HasPrefix(path, "~/") {
				path = filepath.Join(home, path[2:])
			}

86
87
88
89
			if !filepath.IsAbs(path) {
				path = filepath.Join(filepath.Dir(filename), path)
			}

90
			fi, err := os.Stat(path)
Michael Yang's avatar
Michael Yang committed
91
			if errors.Is(err, os.ErrNotExist) && c.Name == "model" {
Michael Yang's avatar
Michael Yang committed
92
				continue
Michael Yang's avatar
Michael Yang committed
93
94
95
96
			} else if err != nil {
				return err
			}

97
			if fi.IsDir() {
Michael Yang's avatar
Michael Yang committed
98
99
100
				// this is likely a safetensors or pytorch directory
				// TODO make this work w/ adapters
				tempfile, err := tempZipFiles(path)
101
102
103
				if err != nil {
					return err
				}
Michael Yang's avatar
Michael Yang committed
104
				defer os.RemoveAll(tempfile)
105

Michael Yang's avatar
Michael Yang committed
106
				path = tempfile
Michael Yang's avatar
Michael Yang committed
107
108
			}

109
110
			digest, err := createBlob(cmd, client, path)
			if err != nil {
Michael Yang's avatar
Michael Yang committed
111
112
113
				return err
			}

114
115
116
117
118
119
120
			name := c.Name
			if c.Name == "model" {
				name = "from"
			}

			re := regexp.MustCompile(fmt.Sprintf(`(?im)^(%s)\s+%s\s*$`, name, c.Args))
			modelfile = re.ReplaceAll(modelfile, []byte("$1 @"+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
	}

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

	request := api.CreateRequest{Name: args[0], Modelfile: string(modelfile), Quantization: quantization}
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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
		}

		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...)
	} else if pt, _ := glob(filepath.Join(path, "consolidated*.pth"), "application/octet-stream"); len(pt) > 0 {
		// 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
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
	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

	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
358
359
}

360
func PushHandler(cmd *cobra.Command, args []string) error {
Michael Yang's avatar
Michael Yang committed
361
	client, err := api.ClientFromEnvironment()
362
363
364
	if err != nil {
		return err
	}
365

366
367
368
369
370
	insecure, err := cmd.Flags().GetBool("insecure")
	if err != nil {
		return err
	}

Michael Yang's avatar
Michael Yang committed
371
372
373
374
	p := progress.NewProgress(os.Stderr)
	defer p.Stop()

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

378
	fn := func(resp api.ProgressResponse) error {
Michael Yang's avatar
Michael Yang committed
379
		if resp.Digest != "" {
380
381
382
			if spinner != nil {
				spinner.Stop()
			}
Michael Yang's avatar
Michael Yang committed
383
384
385

			bar, ok := bars[resp.Digest]
			if !ok {
386
				bar = progress.NewBar(fmt.Sprintf("pushing %s...", resp.Digest[7:19]), resp.Total, resp.Completed)
Michael Yang's avatar
Michael Yang committed
387
388
389
390
391
392
				bars[resp.Digest] = bar
				p.Add(resp.Digest, bar)
			}

			bar.Set(resp.Completed)
		} else if status != resp.Status {
393
394
395
			if spinner != nil {
				spinner.Stop()
			}
Michael Yang's avatar
Michael Yang committed
396
397
398
399
400
401

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

402
403
404
		return nil
	}

Michael Yang's avatar
Michael Yang committed
405
	request := api.PushRequest{Name: args[0], Insecure: insecure}
Michael Yang's avatar
Michael Yang committed
406
	if err := client.Push(cmd.Context(), &request, fn); err != nil {
Michael Yang's avatar
Michael Yang committed
407
408
409
		return err
	}

410
	spinner.Stop()
Michael Yang's avatar
Michael Yang committed
411
	return nil
412
413
}

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

Michael Yang's avatar
Michael Yang committed
420
	models, err := client.List(cmd.Context())
Patrick Devine's avatar
Patrick Devine committed
421
422
423
424
425
426
427
	if err != nil {
		return err
	}

	var data [][]string

	for _, m := range models.Models {
Michael Yang's avatar
Michael Yang committed
428
		if len(args) == 0 || strings.HasPrefix(m.Name, args[0]) {
429
			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
430
		}
Patrick Devine's avatar
Patrick Devine committed
431
432
433
	}

	table := tablewriter.NewWriter(os.Stdout)
Patrick Devine's avatar
Patrick Devine committed
434
	table.SetHeader([]string{"NAME", "ID", "SIZE", "MODIFIED"})
Patrick Devine's avatar
Patrick Devine committed
435
436
437
438
439
440
441
442
443
444
445
446
	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
}

447
func DeleteHandler(cmd *cobra.Command, args []string) error {
Michael Yang's avatar
Michael Yang committed
448
	client, err := api.ClientFromEnvironment()
449
450
451
	if err != nil {
		return err
	}
452

453
454
	for _, name := range args {
		req := api.DeleteRequest{Name: name}
Michael Yang's avatar
Michael Yang committed
455
		if err := client.Delete(cmd.Context(), &req); err != nil {
456
457
458
			return err
		}
		fmt.Printf("deleted '%s'\n", name)
459
460
461
462
	}
	return nil
}

Patrick Devine's avatar
Patrick Devine committed
463
func ShowHandler(cmd *cobra.Command, args []string) error {
Michael Yang's avatar
Michael Yang committed
464
	client, err := api.ClientFromEnvironment()
Patrick Devine's avatar
Patrick Devine committed
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
	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 {
514
		return errors.New("only one of '--license', '--modelfile', '--parameters', '--system', or '--template' can be specified")
Patrick Devine's avatar
Patrick Devine committed
515
	} else if flagsSet == 0 {
516
		return errors.New("one of '--license', '--modelfile', '--parameters', '--system', or '--template' must be specified")
Patrick Devine's avatar
Patrick Devine committed
517
518
	}

519
	req := api.ShowRequest{Name: args[0]}
Michael Yang's avatar
Michael Yang committed
520
	resp, err := client.Show(cmd.Context(), &req)
Patrick Devine's avatar
Patrick Devine committed
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
	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
541
func CopyHandler(cmd *cobra.Command, args []string) error {
Michael Yang's avatar
Michael Yang committed
542
	client, err := api.ClientFromEnvironment()
543
544
545
	if err != nil {
		return err
	}
Patrick Devine's avatar
Patrick Devine committed
546
547

	req := api.CopyRequest{Source: args[0], Destination: args[1]}
Michael Yang's avatar
Michael Yang committed
548
	if err := client.Copy(cmd.Context(), &req); err != nil {
Patrick Devine's avatar
Patrick Devine committed
549
550
551
552
553
554
		return err
	}
	fmt.Printf("copied '%s' to '%s'\n", args[0], args[1])
	return nil
}

555
func PullHandler(cmd *cobra.Command, args []string) error {
556
557
558
559
560
	insecure, err := cmd.Flags().GetBool("insecure")
	if err != nil {
		return err
	}

Michael Yang's avatar
Michael Yang committed
561
	client, err := api.ClientFromEnvironment()
562
563
564
	if err != nil {
		return err
	}
565

Michael Yang's avatar
Michael Yang committed
566
567
568
569
570
	p := progress.NewProgress(os.Stderr)
	defer p.Stop()

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

571
572
	var status string
	var spinner *progress.Spinner
Michael Yang's avatar
Michael Yang committed
573

574
	fn := func(resp api.ProgressResponse) error {
Michael Yang's avatar
Michael Yang committed
575
		if resp.Digest != "" {
576
577
578
			if spinner != nil {
				spinner.Stop()
			}
Michael Yang's avatar
Michael Yang committed
579
580
581

			bar, ok := bars[resp.Digest]
			if !ok {
582
				bar = progress.NewBar(fmt.Sprintf("pulling %s...", resp.Digest[7:19]), resp.Total, resp.Completed)
Michael Yang's avatar
Michael Yang committed
583
584
585
586
587
588
				bars[resp.Digest] = bar
				p.Add(resp.Digest, bar)
			}

			bar.Set(resp.Completed)
		} else if status != resp.Status {
589
590
591
			if spinner != nil {
				spinner.Stop()
			}
Michael Yang's avatar
Michael Yang committed
592
593
594
595
596
597

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

598
599
		return nil
	}
600

Michael Yang's avatar
Michael Yang committed
601
	request := api.PullRequest{Name: args[0], Insecure: insecure}
Michael Yang's avatar
Michael Yang committed
602
	if err := client.Pull(cmd.Context(), &request, fn); err != nil {
Michael Yang's avatar
Michael Yang committed
603
604
605
606
		return err
	}

	return nil
Michael Yang's avatar
Michael Yang committed
607
608
}

609
610
type generateContextKey string

611
type runOptions struct {
612
613
614
615
616
617
618
619
620
621
622
	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
623
624
}

625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
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 {
			if state.lineLength+1 > termWidth-5 {
				if len(state.wordBuffer) > termWidth-10 {
					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
				fmt.Printf("\x1b[%dD\x1b[K\n", len(state.wordBuffer))
				fmt.Printf("%s%c", state.wordBuffer, ch)
				state.lineLength = len(state.wordBuffer) + 1
			} else {
				fmt.Print(string(ch))
				state.lineLength += 1

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

	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
742
	client, err := api.ClientFromEnvironment()
Patrick Devine's avatar
Patrick Devine committed
743
	if err != nil {
744
		return err
Patrick Devine's avatar
Patrick Devine committed
745
	}
Michael Yang's avatar
Michael Yang committed
746

Michael Yang's avatar
Michael Yang committed
747
	p := progress.NewProgress(os.Stderr)
748
	defer p.StopAndClear()
749

Michael Yang's avatar
Michael Yang committed
750
751
752
	spinner := progress.NewSpinner("")
	p.Add("", spinner)

753
754
755
756
757
758
759
	var latest api.GenerateResponse

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

Michael Yang's avatar
Michael Yang committed
760
	ctx, cancel := context.WithCancel(cmd.Context())
761
762
763
764
765
766
767
768
769
770
	defer cancel()

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

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

771
	var state *displayResponseState = &displayResponseState{}
772

773
	fn := func(response api.GenerateResponse) error {
Michael Yang's avatar
Michael Yang committed
774
		p.StopAndClear()
775

Patrick Devine's avatar
Patrick Devine committed
776
		latest = response
777
		content := response.Response
778

779
		displayResponse(content, opts.WordWrap, state)
780

Patrick Devine's avatar
Patrick Devine committed
781
782
		return nil
	}
783

784
785
786
787
788
789
790
	if opts.MultiModal {
		opts.Prompt, opts.Images, err = extractFileData(opts.Prompt)
		if err != nil {
			return err
		}
	}

Michael Yang's avatar
Michael Yang committed
791
792
793
794
	request := api.GenerateRequest{
		Model:    opts.Model,
		Prompt:   opts.Prompt,
		Context:  generateContext,
795
		Images:   opts.Images,
Michael Yang's avatar
Michael Yang committed
796
797
798
799
800
801
802
		Format:   opts.Format,
		System:   opts.System,
		Template: opts.Template,
		Options:  opts.Options,
	}

	if err := client.Generate(ctx, &request, fn); err != nil {
803
		if errors.Is(err, context.Canceled) {
804
			return nil
805
		}
806
		return err
Patrick Devine's avatar
Patrick Devine committed
807
	}
808

809
	if opts.Prompt != "" {
Michael Yang's avatar
Michael Yang committed
810
811
		fmt.Println()
		fmt.Println()
Patrick Devine's avatar
Patrick Devine committed
812
	}
813

814
815
816
817
	if !latest.Done {
		return nil
	}

Patrick Devine's avatar
Patrick Devine committed
818
819
	verbose, err := cmd.Flags().GetBool("verbose")
	if err != nil {
820
		return err
Patrick Devine's avatar
Patrick Devine committed
821
	}
Michael Yang's avatar
Michael Yang committed
822

Patrick Devine's avatar
Patrick Devine committed
823
824
	if verbose {
		latest.Summary()
Michael Yang's avatar
Michael Yang committed
825
	}
Michael Yang's avatar
Michael Yang committed
826

Patrick Devine's avatar
Patrick Devine committed
827
828
829
	ctx = context.WithValue(cmd.Context(), generateContextKey("context"), latest.Context)
	cmd.SetContext(ctx)

830
	return nil
Michael Yang's avatar
Michael Yang committed
831
832
}

833
func RunServer(cmd *cobra.Command, _ []string) error {
834
	host, port, err := net.SplitHostPort(strings.Trim(os.Getenv("OLLAMA_HOST"), "\"'"))
Michael Yang's avatar
Michael Yang committed
835
836
	if err != nil {
		host, port = "127.0.0.1", "11434"
Michael Yang's avatar
Michael Yang committed
837
		if ip := net.ParseIP(strings.Trim(os.Getenv("OLLAMA_HOST"), "[]")); ip != nil {
Michael Yang's avatar
Michael Yang committed
838
839
			host = ip.String()
		}
Jeffrey Morgan's avatar
Jeffrey Morgan committed
840
	}
841

Michael Yang's avatar
Michael Yang committed
842
	if err := initializeKeypair(); err != nil {
843
844
845
		return err
	}

Michael Yang's avatar
Michael Yang committed
846
	ln, err := net.Listen("tcp", net.JoinHostPort(host, port))
847
848
849
	if err != nil {
		return err
	}
Jeffrey Morgan's avatar
Jeffrey Morgan committed
850

851
	return server.Serve(ln)
Jeffrey Morgan's avatar
Jeffrey Morgan committed
852
853
}

854
855
856
857
858
859
860
861
862
863
864
865
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
866
		cryptoPublicKey, cryptoPrivateKey, err := ed25519.GenerateKey(rand.Reader)
867
868
869
870
		if err != nil {
			return err
		}

Michael Yang's avatar
Michael Yang committed
871
		privateKeyBytes, err := ssh.MarshalPrivateKey(cryptoPrivateKey, "")
872
873
874
875
		if err != nil {
			return err
		}

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

Michael Yang's avatar
Michael Yang committed
880
		if err := os.WriteFile(privKeyPath, pem.EncodeToMemory(privateKeyBytes), 0o600); err != nil {
881
882
883
			return err
		}

Michael Yang's avatar
Michael Yang committed
884
		sshPublicKey, err := ssh.NewPublicKey(cryptoPublicKey)
885
886
887
888
		if err != nil {
			return err
		}

Michael Yang's avatar
Michael Yang committed
889
		publicKeyBytes := ssh.MarshalAuthorizedKey(sshPublicKey)
890

Michael Yang's avatar
Michael Yang committed
891
		if err := os.WriteFile(pubKeyPath, publicKeyBytes, 0o644); err != nil {
892
893
894
			return err
		}

Michael Yang's avatar
Michael Yang committed
895
		fmt.Printf("Your new public key is: \n\n%s\n", publicKeyBytes)
896
897
898
899
	}
	return nil
}

900
901
//nolint:unused
func waitForServer(ctx context.Context, client *api.Client) error {
Bruce MacDonald's avatar
Bruce MacDonald committed
902
903
904
905
906
907
908
909
	// wait for the server to start
	timeout := time.After(5 * time.Second)
	tick := time.Tick(500 * time.Millisecond)
	for {
		select {
		case <-timeout:
			return errors.New("timed out waiting for server to start")
		case <-tick:
Michael Yang's avatar
Michael Yang committed
910
			if err := client.Heartbeat(ctx); err == nil {
Bruce MacDonald's avatar
Bruce MacDonald committed
911
912
913
914
				return nil // server has started
			}
		}
	}
915

Bruce MacDonald's avatar
Bruce MacDonald committed
916
917
}

Michael Yang's avatar
Michael Yang committed
918
func checkServerHeartbeat(cmd *cobra.Command, _ []string) error {
Michael Yang's avatar
Michael Yang committed
919
	client, err := api.ClientFromEnvironment()
920
921
922
	if err != nil {
		return err
	}
Michael Yang's avatar
Michael Yang committed
923
	if err := client.Heartbeat(cmd.Context()); err != nil {
924
		if !strings.Contains(err.Error(), " refused") {
Bruce MacDonald's avatar
Bruce MacDonald committed
925
926
			return err
		}
927
928
		if err := startApp(cmd.Context(), client); err != nil {
			return fmt.Errorf("could not connect to ollama app, is it running?")
929
930
931
932
933
		}
	}
	return nil
}

Michael Yang's avatar
Michael Yang committed
934
935
936
937
938
939
940
941
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
942
943
944
945
946
		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
947
948
	}

949
	if serverVersion != version.Version {
Michael Yang's avatar
Michael Yang committed
950
		fmt.Printf("Warning: client version is %s\n", version.Version)
951
	}
Michael Yang's avatar
Michael Yang committed
952
953
}

954
955
956
957
958
959
960
961
func appendHostEnvDocs(cmd *cobra.Command) {
	const hostEnvDocs = `
Environment Variables:
      OLLAMA_HOST        The host:port or base URL of the Ollama server (e.g. http://localhost:11434)
`
	cmd.SetUsageTemplate(cmd.UsageTemplate() + hostEnvDocs)
}

Jeffrey Morgan's avatar
Jeffrey Morgan committed
962
963
func NewCLI() *cobra.Command {
	log.SetFlags(log.LstdFlags | log.Lshortfile)
Michael Yang's avatar
Michael Yang committed
964
	cobra.EnableCommandSorting = false
Jeffrey Morgan's avatar
Jeffrey Morgan committed
965

966
	if runtime.GOOS == "windows" {
967
		console.ConsoleFromFile(os.Stdin) //nolint:errcheck
968
969
	}

Jeffrey Morgan's avatar
Jeffrey Morgan committed
970
	rootCmd := &cobra.Command{
971
972
973
974
		Use:           "ollama",
		Short:         "Large language model runner",
		SilenceUsage:  true,
		SilenceErrors: true,
Jeffrey Morgan's avatar
Jeffrey Morgan committed
975
976
977
		CompletionOptions: cobra.CompletionOptions{
			DisableDefaultCmd: true,
		},
Michael Yang's avatar
Michael Yang committed
978
979
980
981
982
983
984
985
		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
986
987
	}

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

990
	createCmd := &cobra.Command{
991
992
		Use:     "create MODEL",
		Short:   "Create a model from a Modelfile",
Michael Yang's avatar
Michael Yang committed
993
		Args:    cobra.ExactArgs(1),
994
995
		PreRunE: checkServerHeartbeat,
		RunE:    CreateHandler,
996
997
998
	}

	createCmd.Flags().StringP("file", "f", "Modelfile", "Name of the Modelfile (default \"Modelfile\")")
Michael Yang's avatar
Michael Yang committed
999
	createCmd.Flags().StringP("quantization", "q", "", "Quantization level.")
1000

Patrick Devine's avatar
Patrick Devine committed
1001
1002
1003
	showCmd := &cobra.Command{
		Use:     "show MODEL",
		Short:   "Show information for a model",
Michael Yang's avatar
Michael Yang committed
1004
		Args:    cobra.ExactArgs(1),
Patrick Devine's avatar
Patrick Devine committed
1005
1006
1007
1008
1009
1010
1011
1012
		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")
1013
	showCmd.Flags().Bool("system", false, "Show system message of a model")
Patrick Devine's avatar
Patrick Devine committed
1014

Jeffrey Morgan's avatar
Jeffrey Morgan committed
1015
	runCmd := &cobra.Command{
1016
1017
1018
1019
1020
		Use:     "run MODEL [PROMPT]",
		Short:   "Run a model",
		Args:    cobra.MinimumNArgs(1),
		PreRunE: checkServerHeartbeat,
		RunE:    RunHandler,
Jeffrey Morgan's avatar
Jeffrey Morgan committed
1021
1022
	}

1023
	runCmd.Flags().Bool("verbose", false, "Show timings for response")
1024
	runCmd.Flags().Bool("insecure", false, "Use an insecure registry")
1025
	runCmd.Flags().Bool("nowordwrap", false, "Don't wrap words to the next line automatically")
Jeffrey Morgan's avatar
Jeffrey Morgan committed
1026
	runCmd.Flags().String("format", "", "Response format (e.g. json)")
Jeffrey Morgan's avatar
Jeffrey Morgan committed
1027
1028
1029
1030
	serveCmd := &cobra.Command{
		Use:     "serve",
		Aliases: []string{"start"},
		Short:   "Start ollama",
Michael Yang's avatar
Michael Yang committed
1031
		Args:    cobra.ExactArgs(0),
Michael Yang's avatar
Michael Yang committed
1032
		RunE:    RunServer,
Jeffrey Morgan's avatar
Jeffrey Morgan committed
1033
	}
1034
1035
1036
	serveCmd.SetUsageTemplate(serveCmd.UsageTemplate() + `
Environment Variables:

1037
1038
1039
1040
    OLLAMA_HOST         The host:port to bind to (default "127.0.0.1:11434")
    OLLAMA_ORIGINS      A comma separated list of allowed origins.
    OLLAMA_MODELS       The path to the models directory (default is "~/.ollama/models")
    OLLAMA_KEEP_ALIVE   The duration that models stay loaded in memory (default is "5m")
1041
    OLLAMA_DEBUG        Set to 1 to enable additional debug logging
1042
`)
Jeffrey Morgan's avatar
Jeffrey Morgan committed
1043

1044
	pullCmd := &cobra.Command{
1045
1046
		Use:     "pull MODEL",
		Short:   "Pull a model from a registry",
Michael Yang's avatar
Michael Yang committed
1047
		Args:    cobra.ExactArgs(1),
1048
1049
		PreRunE: checkServerHeartbeat,
		RunE:    PullHandler,
1050
1051
	}

1052
1053
	pullCmd.Flags().Bool("insecure", false, "Use an insecure registry")

1054
	pushCmd := &cobra.Command{
1055
1056
		Use:     "push MODEL",
		Short:   "Push a model to a registry",
Michael Yang's avatar
Michael Yang committed
1057
		Args:    cobra.ExactArgs(1),
1058
1059
		PreRunE: checkServerHeartbeat,
		RunE:    PushHandler,
1060
1061
	}

1062
1063
	pushCmd.Flags().Bool("insecure", false, "Use an insecure registry")

Patrick Devine's avatar
Patrick Devine committed
1064
	listCmd := &cobra.Command{
1065
		Use:     "list",
Patrick Devine's avatar
Patrick Devine committed
1066
		Aliases: []string{"ls"},
1067
		Short:   "List models",
1068
		PreRunE: checkServerHeartbeat,
1069
		RunE:    ListHandler,
1070
	}
Patrick Devine's avatar
Patrick Devine committed
1071
	copyCmd := &cobra.Command{
Michael Yang's avatar
Michael Yang committed
1072
		Use:     "cp SOURCE TARGET",
1073
		Short:   "Copy a model",
Michael Yang's avatar
Michael Yang committed
1074
		Args:    cobra.ExactArgs(2),
1075
1076
		PreRunE: checkServerHeartbeat,
		RunE:    CopyHandler,
Patrick Devine's avatar
Patrick Devine committed
1077
1078
	}

1079
	deleteCmd := &cobra.Command{
Michael Yang's avatar
Michael Yang committed
1080
		Use:     "rm MODEL [MODEL...]",
1081
1082
1083
1084
		Short:   "Remove a model",
		Args:    cobra.MinimumNArgs(1),
		PreRunE: checkServerHeartbeat,
		RunE:    DeleteHandler,
Patrick Devine's avatar
Patrick Devine committed
1085
1086
	}

1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
	for _, cmd := range []*cobra.Command{
		createCmd,
		showCmd,
		runCmd,
		pullCmd,
		pushCmd,
		listCmd,
		copyCmd,
		deleteCmd,
	} {
		appendHostEnvDocs(cmd)
	}

Jeffrey Morgan's avatar
Jeffrey Morgan committed
1100
1101
	rootCmd.AddCommand(
		serveCmd,
1102
		createCmd,
Patrick Devine's avatar
Patrick Devine committed
1103
		showCmd,
1104
		runCmd,
1105
1106
		pullCmd,
		pushCmd,
Patrick Devine's avatar
Patrick Devine committed
1107
		listCmd,
Patrick Devine's avatar
Patrick Devine committed
1108
		copyCmd,
1109
		deleteCmd,
Jeffrey Morgan's avatar
Jeffrey Morgan committed
1110
1111
1112
1113
	)

	return rootCmd
}