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

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

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

37
	"github.com/ollama/ollama/api"
38
	"github.com/ollama/ollama/envconfig"
39
	"github.com/ollama/ollama/format"
40
	"github.com/ollama/ollama/parser"
41
	"github.com/ollama/ollama/progress"
42
	"github.com/ollama/ollama/readline"
Jesse Gross's avatar
Jesse Gross committed
43
	"github.com/ollama/ollama/runner"
44
	"github.com/ollama/ollama/server"
45
	"github.com/ollama/ollama/types/model"
46
	"github.com/ollama/ollama/types/syncmap"
47
	"github.com/ollama/ollama/version"
48
	xcmd "github.com/ollama/ollama/x/cmd"
Jeffrey Morgan's avatar
Jeffrey Morgan committed
49
50
)

51
const ConnectInstructions = "To sign in, navigate to:\n    %s\n\n"
52

53
54
55
56
57
58
59
60
61
// ensureThinkingSupport emits a warning if the model does not advertise thinking support
func ensureThinkingSupport(ctx context.Context, client *api.Client, name string) {
	if name == "" {
		return
	}
	resp, err := client.Show(ctx, &api.ShowRequest{Model: name})
	if err != nil {
		return
	}
62
63
	if slices.Contains(resp.Capabilities, model.CapabilityThinking) {
		return
64
65
66
67
	}
	fmt.Fprintf(os.Stderr, "warning: model %q does not support thinking output\n", name)
}

68
var errModelfileNotFound = errors.New("specified Modelfile wasn't found")
69
70

func getModelfileName(cmd *cobra.Command) (string, error) {
71
	filename, _ := cmd.Flags().GetString("file")
72
73
74
75
76
77

	if filename == "" {
		filename = "Modelfile"
	}

	absName, err := filepath.Abs(filename)
78
	if err != nil {
79
		return "", err
80
81
	}

82
	_, err = os.Stat(absName)
83
	if err != nil {
84
		return "", err
85
	}
86

87
88
89
90
	return absName, nil
}

func CreateHandler(cmd *cobra.Command, args []string) error {
Michael Yang's avatar
Michael Yang committed
91
92
93
	p := progress.NewProgress(os.Stderr)
	defer p.Stop()

94
95
96
97
98
99
100
101
102
103
	var reader io.Reader

	filename, err := getModelfileName(cmd)
	if os.IsNotExist(err) {
		if filename == "" {
			reader = strings.NewReader("FROM .\n")
		} else {
			return errModelfileNotFound
		}
	} else if err != nil {
Michael Yang's avatar
Michael Yang committed
104
		return err
105
106
107
108
109
110
111
112
	} else {
		f, err := os.Open(filename)
		if err != nil {
			return err
		}

		reader = f
		defer f.Close()
Michael Yang's avatar
Michael Yang committed
113
114
	}

115
	modelfile, err := parser.ParseFile(reader)
Michael Yang's avatar
Michael Yang committed
116
117
118
119
	if err != nil {
		return err
	}

120
121
122
123
	status := "gathering model components"
	spinner := progress.NewSpinner(status)
	p.Add(status, spinner)

124
	req, err := modelfile.CreateRequest(filepath.Dir(filename))
Michael Yang's avatar
Michael Yang committed
125
126
127
	if err != nil {
		return err
	}
128
	spinner.Stop()
Michael Yang's avatar
Michael Yang committed
129

130
	req.Model = args[0]
131
132
133
134
	quantize, _ := cmd.Flags().GetString("quantize")
	if quantize != "" {
		req.Quantize = quantize
	}
135

136
137
138
139
140
	client, err := api.ClientFromEnvironment()
	if err != nil {
		return err
	}

141
142
143
144
145
146
	var g errgroup.Group
	g.SetLimit(max(runtime.GOMAXPROCS(0)-1, 1))

	files := syncmap.NewSyncMap[string, string]()
	for f, digest := range req.Files {
		g.Go(func() error {
147
			if _, err := createBlob(cmd, client, f, digest, p); err != nil {
Michael Yang's avatar
Michael Yang committed
148
149
				return err
			}
150
151
152
153
154
155
156

			// TODO: this is incorrect since the file might be in a subdirectory
			//       instead this should take the path relative to the model directory
			//       but the current implementation does not allow this
			files.Store(filepath.Base(f), digest)
			return nil
		})
157
	}
Michael Yang's avatar
Michael Yang committed
158

159
160
161
	adapters := syncmap.NewSyncMap[string, string]()
	for f, digest := range req.Adapters {
		g.Go(func() error {
162
			if _, err := createBlob(cmd, client, f, digest, p); err != nil {
Michael Yang's avatar
Michael Yang committed
163
164
				return err
			}
165
166
167
168
169

			// TODO: same here
			adapters.Store(filepath.Base(f), digest)
			return nil
		})
Michael Yang's avatar
Michael Yang committed
170
	}
Michael Yang's avatar
Michael Yang committed
171

172
173
174
175
176
177
178
	if err := g.Wait(); err != nil {
		return err
	}

	req.Files = files.Items()
	req.Adapters = adapters.Items()

Michael Yang's avatar
Michael Yang committed
179
	bars := make(map[string]*progress.Bar)
180
	fn := func(resp api.ProgressResponse) error {
Michael Yang's avatar
Michael Yang committed
181
182
183
		if resp.Digest != "" {
			bar, ok := bars[resp.Digest]
			if !ok {
184
185
186
187
188
				msg := resp.Status
				if msg == "" {
					msg = fmt.Sprintf("pulling %s...", resp.Digest[7:19])
				}
				bar = progress.NewBar(msg, resp.Total, resp.Completed)
Michael Yang's avatar
Michael Yang committed
189
190
191
192
193
194
195
196
197
198
199
200
201
				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)
		}

202
203
204
		return nil
	}

205
	if err := client.Create(cmd.Context(), req, fn); err != nil {
206
207
208
		if strings.Contains(err.Error(), "path or Modelfile are required") {
			return fmt.Errorf("the ollama server must be updated to use `ollama create` with this client")
		}
209
210
211
212
213
214
		return err
	}

	return nil
}

215
216
func createBlob(cmd *cobra.Command, client *api.Client, path string, digest string, p *progress.Progress) (string, error) {
	realPath, err := filepath.EvalSymlinks(path)
Michael Yang's avatar
Michael Yang committed
217
218
219
220
	if err != nil {
		return "", err
	}

221
	bin, err := os.Open(realPath)
222
223
224
225
226
	if err != nil {
		return "", err
	}
	defer bin.Close()

227
228
229
230
231
232
233
234
	// Get file info to retrieve the size
	fileInfo, err := bin.Stat()
	if err != nil {
		return "", err
	}
	fileSize := fileInfo.Size()

	var pw progressWriter
235
236
237
238
	status := fmt.Sprintf("copying file %s 0%%", digest)
	spinner := progress.NewSpinner(status)
	p.Add(status, spinner)
	defer spinner.Stop()
239
240
241
242
243
244
245
246
247
248

	done := make(chan struct{})
	defer close(done)

	go func() {
		ticker := time.NewTicker(60 * time.Millisecond)
		defer ticker.Stop()
		for {
			select {
			case <-ticker.C:
249
				spinner.SetMessage(fmt.Sprintf("copying file %s %d%%", digest, int(100*pw.n.Load()/fileSize)))
250
			case <-done:
251
				spinner.SetMessage(fmt.Sprintf("copying file %s 100%%", digest))
252
253
254
255
256
				return
			}
		}
	}()

257
	if err := client.CreateBlob(cmd.Context(), digest, io.TeeReader(bin, &pw)); err != nil {
258
259
260
261
262
		return "", err
	}
	return digest, nil
}

263
264
265
266
267
268
269
270
271
type progressWriter struct {
	n atomic.Int64
}

func (w *progressWriter) Write(p []byte) (n int, err error) {
	w.n.Add(int64(len(p)))
	return len(p), nil
}

Patrick Devine's avatar
Patrick Devine committed
272
273
274
275
276
277
278
279
280
281
282
283
func loadOrUnloadModel(cmd *cobra.Command, opts *runOptions) error {
	p := progress.NewProgress(os.Stderr)
	defer p.StopAndClear()

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

	client, err := api.ClientFromEnvironment()
	if err != nil {
		return err
	}

284
285
286
287
	if info, err := client.Show(cmd.Context(), &api.ShowRequest{Model: opts.Model}); err != nil {
		return err
	} else if info.RemoteHost != "" {
		// Cloud model, no need to load/unload
288
289
290
291
292
293
294
295
		if opts.ShowConnect {
			p.StopAndClear()
			if strings.HasPrefix(info.RemoteHost, "https://ollama.com") {
				fmt.Fprintf(os.Stderr, "Connecting to '%s' on 'ollama.com' ⚡\n", info.RemoteModel)
			} else {
				fmt.Fprintf(os.Stderr, "Connecting to '%s' on '%s'\n", info.RemoteModel, info.RemoteHost)
			}
		}
296
297
298
		return nil
	}

Patrick Devine's avatar
Patrick Devine committed
299
300
301
	req := &api.GenerateRequest{
		Model:     opts.Model,
		KeepAlive: opts.KeepAlive,
302
303
304

		// pass Think here so we fail before getting to the chat prompt if the model doesn't support it
		Think: opts.Think,
Patrick Devine's avatar
Patrick Devine committed
305
306
	}

307
308
309
	return client.Generate(cmd.Context(), req, func(r api.GenerateResponse) error {
		return nil
	})
Patrick Devine's avatar
Patrick Devine committed
310
311
312
313
314
315
316
317
318
319
320
}

func StopHandler(cmd *cobra.Command, args []string) error {
	opts := &runOptions{
		Model:     args[0],
		KeepAlive: &api.Duration{Duration: 0},
	}
	if err := loadOrUnloadModel(cmd, opts); err != nil {
		if strings.Contains(err.Error(), "not found") {
			return fmt.Errorf("couldn't find model \"%s\" to stop", args[0])
		}
321
		return err
Patrick Devine's avatar
Patrick Devine committed
322
323
324
325
	}
	return nil
}

326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
func generateEmbedding(cmd *cobra.Command, modelName, input string, keepAlive *api.Duration, truncate *bool, dimensions int) error {
	client, err := api.ClientFromEnvironment()
	if err != nil {
		return err
	}

	req := &api.EmbedRequest{
		Model: modelName,
		Input: input,
	}
	if keepAlive != nil {
		req.KeepAlive = keepAlive
	}
	if truncate != nil {
		req.Truncate = truncate
	}
	if dimensions > 0 {
		req.Dimensions = dimensions
	}

	resp, err := client.Embed(cmd.Context(), req)
	if err != nil {
		return err
	}

	if len(resp.Embeddings) == 0 {
		return errors.New("no embeddings returned")
	}

	output, err := json.Marshal(resp.Embeddings[0])
	if err != nil {
		return err
	}
	fmt.Println(string(output))

	return nil
}

364
func RunHandler(cmd *cobra.Command, args []string) error {
365
366
367
	interactive := true

	opts := runOptions{
368
369
370
371
		Model:       args[0],
		WordWrap:    os.Getenv("TERM") == "xterm-256color",
		Options:     map[string]any{},
		ShowConnect: true,
372
373
374
375
376
377
378
379
	}

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

380
381
	thinkFlag := cmd.Flags().Lookup("think")
	if thinkFlag.Changed {
Michael Yang's avatar
Michael Yang committed
382
		thinkStr, err := cmd.Flags().GetString("think")
383
384
385
		if err != nil {
			return err
		}
Michael Yang's avatar
Michael Yang committed
386
387
388
389
390
391
392
393
394
395
396
397
398

		// Handle different values for --think
		switch thinkStr {
		case "", "true":
			// --think or --think=true
			opts.Think = &api.ThinkValue{Value: true}
		case "false":
			opts.Think = &api.ThinkValue{Value: false}
		case "high", "medium", "low":
			opts.Think = &api.ThinkValue{Value: thinkStr}
		default:
			return fmt.Errorf("invalid value for --think: %q (must be true, false, high, medium, or low)", thinkStr)
		}
399
400
401
402
403
404
405
406
407
	} else {
		opts.Think = nil
	}
	hidethinking, err := cmd.Flags().GetBool("hidethinking")
	if err != nil {
		return err
	}
	opts.HideThinking = hidethinking

408
409
410
411
412
413
414
415
416
417
418
419
	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}
	}

420
421
422
423
424
425
426
427
	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
		}

428
429
430
431
432
		// Only prepend stdin content if it's not empty
		stdinContent := string(in)
		if len(stdinContent) > 0 {
			prompts = append([]string{stdinContent}, prompts...)
		}
433
		opts.ShowConnect = false
434
435
436
437
438
439
440
		opts.WordWrap = false
		interactive = false
	}
	opts.Prompt = strings.Join(prompts, " ")
	if len(prompts) > 0 {
		interactive = false
	}
441
442
443
444
	// Be quiet if we're redirecting to a pipe or file
	if !term.IsTerminal(int(os.Stdout.Fd())) {
		interactive = false
	}
445
446
447
448
449
450
451

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

452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
	// Fill out the rest of the options based on information about the
	// model.
	client, err := api.ClientFromEnvironment()
	if err != nil {
		return err
	}

	name := args[0]
	info, err := func() (*api.ShowResponse, error) {
		showReq := &api.ShowRequest{Name: name}
		info, err := client.Show(cmd.Context(), showReq)
		var se api.StatusError
		if errors.As(err, &se) && se.StatusCode == http.StatusNotFound {
			if err := PullHandler(cmd, []string{name}); err != nil {
				return nil, err
			}
			return client.Show(cmd.Context(), &api.ShowRequest{Name: name})
		}
		return info, err
	}()
	if err != nil {
		return err
474
475
	}

476
477
478
479
480
	opts.Think, err = inferThinkingOption(&info.Capabilities, &opts, thinkFlag.Changed)
	if err != nil {
		return err
	}

481
482
483
484
485
	opts.MultiModal = slices.Contains(info.Capabilities, model.CapabilityVision)

	// TODO: remove the projector info and vision info checks below,
	// these are left in for backwards compatibility with older servers
	// that don't have the capabilities field in the model info
486
487
488
489
490
491
492
493
494
495
	if len(info.ProjectorInfo) != 0 {
		opts.MultiModal = true
	}
	for k := range info.ModelInfo {
		if strings.Contains(k, ".vision.") {
			opts.MultiModal = true
			break
		}
	}

496
497
	opts.ParentModel = info.Details.ParentModel

498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
	// Check if this is an embedding model
	isEmbeddingModel := slices.Contains(info.Capabilities, model.CapabilityEmbedding)

	// If it's an embedding model, handle embedding generation
	if isEmbeddingModel {
		if opts.Prompt == "" {
			return errors.New("embedding models require input text. Usage: ollama run " + name + " \"your text here\"")
		}

		// Get embedding-specific flags
		var truncate *bool
		if truncateFlag, err := cmd.Flags().GetBool("truncate"); err == nil && cmd.Flags().Changed("truncate") {
			truncate = &truncateFlag
		}

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

		return generateEmbedding(cmd, name, opts.Prompt, opts.KeepAlive, truncate, dimensions)
	}

521
522
523
	// Check for experimental flag
	isExperimental, _ := cmd.Flags().GetBool("experimental")

524
	if interactive {
Patrick Devine's avatar
Patrick Devine committed
525
		if err := loadOrUnloadModel(cmd, &opts); err != nil {
526
527
			var sErr api.AuthorizationError
			if errors.As(err, &sErr) && sErr.StatusCode == http.StatusUnauthorized {
528
529
530
531
				fmt.Printf("You need to be signed in to Ollama to run Cloud models.\n\n")

				if sErr.SigninURL != "" {
					fmt.Printf(ConnectInstructions, sErr.SigninURL)
532
533
534
				}
				return nil
			}
Michael Yang's avatar
Michael Yang committed
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
			return err
		}

		for _, msg := range info.Messages {
			switch msg.Role {
			case "user":
				fmt.Printf(">>> %s\n", msg.Content)
			case "assistant":
				state := &displayResponseState{}
				displayResponse(msg.Content, opts.WordWrap, state)
				fmt.Println()
				fmt.Println()
			}
		}

550
551
552
553
554
		// Use experimental agent loop with
		if isExperimental {
			return xcmd.GenerateInteractive(cmd, opts.Model, opts.WordWrap, opts.Options, opts.Think, opts.HideThinking, opts.KeepAlive)
		}

555
556
557
		return generateInteractive(cmd, opts)
	}
	return generate(cmd, opts)
Bruce MacDonald's avatar
Bruce MacDonald committed
558
559
}

560
561
562
563
564
565
566
567
func SigninHandler(cmd *cobra.Command, args []string) error {
	client, err := api.ClientFromEnvironment()
	if err != nil {
		return err
	}

	user, err := client.Whoami(cmd.Context())
	if err != nil {
568
569
570
571
572
573
574
575
576
577
		var aErr api.AuthorizationError
		if errors.As(err, &aErr) && aErr.StatusCode == http.StatusUnauthorized {
			fmt.Println("You need to be signed in to Ollama to run Cloud models.")
			fmt.Println()

			if aErr.SigninURL != "" {
				fmt.Printf(ConnectInstructions, aErr.SigninURL)
			}
			return nil
		}
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
		return err
	}

	if user != nil && user.Name != "" {
		fmt.Printf("You are already signed in as user '%s'\n", user.Name)
		fmt.Println()
		return nil
	}

	return nil
}

func SignoutHandler(cmd *cobra.Command, args []string) error {
	client, err := api.ClientFromEnvironment()
	if err != nil {
		return err
	}

596
	err = client.Signout(cmd.Context())
597
	if err != nil {
598
599
600
601
602
603
604
605
		var aErr api.AuthorizationError
		if errors.As(err, &aErr) && aErr.StatusCode == http.StatusUnauthorized {
			fmt.Println("You are not signed in to ollama.com")
			fmt.Println()
			return nil
		} else {
			return err
		}
606
	}
607

608
609
610
611
612
	fmt.Println("You have signed out of ollama.com")
	fmt.Println()
	return nil
}

613
func PushHandler(cmd *cobra.Command, args []string) error {
Michael Yang's avatar
Michael Yang committed
614
	client, err := api.ClientFromEnvironment()
615
616
617
	if err != nil {
		return err
	}
618

619
620
621
622
623
	insecure, err := cmd.Flags().GetBool("insecure")
	if err != nil {
		return err
	}

624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
	n := model.ParseName(args[0])
	if strings.HasSuffix(n.Host, ".ollama.ai") || strings.HasSuffix(n.Host, ".ollama.com") {
		_, err := client.Whoami(cmd.Context())
		if err != nil {
			var aErr api.AuthorizationError
			if errors.As(err, &aErr) && aErr.StatusCode == http.StatusUnauthorized {
				fmt.Println("You need to be signed in to push models to ollama.com.")
				fmt.Println()

				if aErr.SigninURL != "" {
					fmt.Printf(ConnectInstructions, aErr.SigninURL)
				}
				return nil
			}

			return err
		}
	}

Michael Yang's avatar
Michael Yang committed
643
644
645
646
	p := progress.NewProgress(os.Stderr)
	defer p.Stop()

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

650
	fn := func(resp api.ProgressResponse) error {
Michael Yang's avatar
Michael Yang committed
651
		if resp.Digest != "" {
652
653
654
			if spinner != nil {
				spinner.Stop()
			}
Michael Yang's avatar
Michael Yang committed
655
656
657

			bar, ok := bars[resp.Digest]
			if !ok {
658
				bar = progress.NewBar(fmt.Sprintf("pushing %s...", resp.Digest[7:19]), resp.Total, resp.Completed)
Michael Yang's avatar
Michael Yang committed
659
660
661
662
663
664
				bars[resp.Digest] = bar
				p.Add(resp.Digest, bar)
			}

			bar.Set(resp.Completed)
		} else if status != resp.Status {
665
666
667
			if spinner != nil {
				spinner.Stop()
			}
Michael Yang's avatar
Michael Yang committed
668
669
670
671
672
673

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

674
675
676
		return nil
	}

Michael Yang's avatar
Michael Yang committed
677
	request := api.PushRequest{Name: args[0], Insecure: insecure}
678

Michael Yang's avatar
Michael Yang committed
679
	if err := client.Push(cmd.Context(), &request, fn); err != nil {
680
681
682
		if spinner != nil {
			spinner.Stop()
		}
683
684
		errStr := strings.ToLower(err.Error())
		if strings.Contains(errStr, "access denied") || strings.Contains(errStr, "unauthorized") {
685
686
			return errors.New("you are not authorized to push to this namespace, create the model under a namespace you own")
		}
Michael Yang's avatar
Michael Yang committed
687
688
689
		return err
	}

690
	p.Stop()
691
	spinner.Stop()
692
693
694
695
696
697
698
699

	destination := n.String()
	if strings.HasSuffix(n.Host, ".ollama.ai") || strings.HasSuffix(n.Host, ".ollama.com") {
		destination = "https://ollama.com/" + strings.TrimSuffix(n.DisplayShortest(), ":latest")
	}
	fmt.Printf("\nYou can find your model at:\n\n")
	fmt.Printf("\t%s\n", destination)

Michael Yang's avatar
Michael Yang committed
700
	return nil
701
702
}

703
func ListHandler(cmd *cobra.Command, args []string) error {
Michael Yang's avatar
Michael Yang committed
704
	client, err := api.ClientFromEnvironment()
705
706
707
	if err != nil {
		return err
	}
Patrick Devine's avatar
Patrick Devine committed
708

Michael Yang's avatar
Michael Yang committed
709
	models, err := client.List(cmd.Context())
Patrick Devine's avatar
Patrick Devine committed
710
711
712
713
714
715
716
	if err != nil {
		return err
	}

	var data [][]string

	for _, m := range models.Models {
717
		if len(args) == 0 || strings.HasPrefix(strings.ToLower(m.Name), strings.ToLower(args[0])) {
718
719
720
721
722
723
724
725
			var size string
			if m.RemoteModel != "" {
				size = "-"
			} else {
				size = format.HumanBytes(m.Size)
			}

			data = append(data, []string{m.Name, m.Digest[:12], size, format.HumanTime(m.ModifiedAt, "Never")})
Michael Yang's avatar
Michael Yang committed
726
		}
Patrick Devine's avatar
Patrick Devine committed
727
728
729
	}

	table := tablewriter.NewWriter(os.Stdout)
Patrick Devine's avatar
Patrick Devine committed
730
	table.SetHeader([]string{"NAME", "ID", "SIZE", "MODIFIED"})
Patrick Devine's avatar
Patrick Devine committed
731
732
733
734
735
	table.SetHeaderAlignment(tablewriter.ALIGN_LEFT)
	table.SetAlignment(tablewriter.ALIGN_LEFT)
	table.SetHeaderLine(false)
	table.SetBorder(false)
	table.SetNoWhiteSpace(true)
Michael Yang's avatar
Michael Yang committed
736
	table.SetTablePadding("    ")
Patrick Devine's avatar
Patrick Devine committed
737
738
739
740
741
742
	table.AppendBulk(data)
	table.Render()

	return nil
}

743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
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))
			}
Patrick Devine's avatar
Patrick Devine committed
771
772
773
774
775
776
777
778

			var until string
			delta := time.Since(m.ExpiresAt)
			if delta > 0 {
				until = "Stopping..."
			} else {
				until = format.HumanTime(m.ExpiresAt, "Never")
			}
779
780
			ctxStr := strconv.Itoa(m.ContextLength)
			data = append(data, []string{m.Name, m.Digest[:12], format.HumanBytes(m.Size), procStr, ctxStr, until})
781
782
783
784
		}
	}

	table := tablewriter.NewWriter(os.Stdout)
785
	table.SetHeader([]string{"NAME", "ID", "SIZE", "PROCESSOR", "CONTEXT", "UNTIL"})
786
787
788
789
790
	table.SetHeaderAlignment(tablewriter.ALIGN_LEFT)
	table.SetAlignment(tablewriter.ALIGN_LEFT)
	table.SetHeaderLine(false)
	table.SetBorder(false)
	table.SetNoWhiteSpace(true)
Michael Yang's avatar
Michael Yang committed
791
	table.SetTablePadding("    ")
792
793
794
795
796
797
	table.AppendBulk(data)
	table.Render()

	return nil
}

798
func DeleteHandler(cmd *cobra.Command, args []string) error {
Michael Yang's avatar
Michael Yang committed
799
	client, err := api.ClientFromEnvironment()
800
801
802
	if err != nil {
		return err
	}
803

804
805
806
807
808
809
810
811
812
	for _, arg := range args {
		// Unload the model if it's running before deletion
		if err := loadOrUnloadModel(cmd, &runOptions{
			Model:     args[0],
			KeepAlive: &api.Duration{Duration: 0},
		}); err != nil {
			if !strings.Contains(strings.ToLower(err.Error()), "not found") {
				fmt.Fprintf(os.Stderr, "Warning: unable to stop model '%s'\n", args[0])
			}
813
814
		}

815
		if err := client.Delete(cmd.Context(), &api.DeleteRequest{Name: arg}); err != nil {
816
817
			return err
		}
818
		fmt.Printf("deleted '%s'\n", arg)
819
820
821
822
	}
	return nil
}

Patrick Devine's avatar
Patrick Devine committed
823
func ShowHandler(cmd *cobra.Command, args []string) error {
Michael Yang's avatar
Michael Yang committed
824
	client, err := api.ClientFromEnvironment()
Patrick Devine's avatar
Patrick Devine committed
825
826
827
828
829
830
831
832
833
	if err != nil {
		return err
	}

	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")
834
	verbose, errVerbose := cmd.Flags().GetBool("verbose")
Patrick Devine's avatar
Patrick Devine committed
835

836
	for _, boolErr := range []error{errLicense, errModelfile, errParams, errSystem, errTemplate, errVerbose} {
Patrick Devine's avatar
Patrick Devine committed
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
		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 {
871
		return errors.New("only one of '--license', '--modelfile', '--parameters', '--system', or '--template' can be specified")
872
873
	}

874
	req := api.ShowRequest{Name: args[0], Verbose: verbose}
875
876
877
878
	resp, err := client.Show(cmd.Context(), &req)
	if err != nil {
		return err
	}
879

880
	if flagsSet == 1 {
881
882
883
884
885
886
887
888
		switch showType {
		case "license":
			fmt.Println(resp.License)
		case "modelfile":
			fmt.Println(resp.Modelfile)
		case "parameters":
			fmt.Println(resp.Parameters)
		case "system":
889
			fmt.Print(resp.System)
890
		case "template":
891
			fmt.Print(resp.Template)
892
893
894
		}

		return nil
Patrick Devine's avatar
Patrick Devine committed
895
896
	}

897
	return showInfo(resp, verbose, os.Stdout)
898
899
}

900
func showInfo(resp *api.ShowResponse, verbose bool, w io.Writer) error {
Michael Yang's avatar
Michael Yang committed
901
902
903
904
905
906
907
	tableRender := func(header string, rows func() [][]string) {
		fmt.Fprintln(w, " ", header)
		table := tablewriter.NewWriter(w)
		table.SetAlignment(tablewriter.ALIGN_LEFT)
		table.SetBorder(false)
		table.SetNoWhiteSpace(true)
		table.SetTablePadding("    ")
908

Michael Yang's avatar
Michael Yang committed
909
910
911
		switch header {
		case "Template", "System", "License":
			table.SetColWidth(100)
912
913
		}

Michael Yang's avatar
Michael Yang committed
914
915
916
		table.AppendBulk(rows())
		table.Render()
		fmt.Fprintln(w)
Patrick Devine's avatar
Patrick Devine committed
917
918
	}

Michael Yang's avatar
Michael Yang committed
919
	tableRender("Model", func() (rows [][]string) {
920
921
922
923
924
		if resp.RemoteHost != "" {
			rows = append(rows, []string{"", "Remote model", resp.RemoteModel})
			rows = append(rows, []string{"", "Remote URL", resp.RemoteHost})
		}

Michael Yang's avatar
Michael Yang committed
925
926
927
		if resp.ModelInfo != nil {
			arch := resp.ModelInfo["general.architecture"].(string)
			rows = append(rows, []string{"", "architecture", arch})
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949

			var paramStr string
			if resp.Details.ParameterSize != "" {
				paramStr = resp.Details.ParameterSize
			} else if v, ok := resp.ModelInfo["general.parameter_count"]; ok {
				if f, ok := v.(float64); ok {
					paramStr = format.HumanNumber(uint64(f))
				}
			}
			rows = append(rows, []string{"", "parameters", paramStr})

			if v, ok := resp.ModelInfo[fmt.Sprintf("%s.context_length", arch)]; ok {
				if f, ok := v.(float64); ok {
					rows = append(rows, []string{"", "context length", strconv.FormatFloat(f, 'f', -1, 64)})
				}
			}

			if v, ok := resp.ModelInfo[fmt.Sprintf("%s.embedding_length", arch)]; ok {
				if f, ok := v.(float64); ok {
					rows = append(rows, []string{"", "embedding length", strconv.FormatFloat(f, 'f', -1, 64)})
				}
			}
Michael Yang's avatar
Michael Yang committed
950
951
952
953
954
		} else {
			rows = append(rows, []string{"", "architecture", resp.Details.Family})
			rows = append(rows, []string{"", "parameters", resp.Details.ParameterSize})
		}
		rows = append(rows, []string{"", "quantization", resp.Details.QuantizationLevel})
955
956
957
		if resp.Requires != "" {
			rows = append(rows, []string{"", "requires", resp.Requires})
		}
Michael Yang's avatar
Michael Yang committed
958
959
		return
	})
960

961
962
963
964
965
966
967
968
969
	if len(resp.Capabilities) > 0 {
		tableRender("Capabilities", func() (rows [][]string) {
			for _, capability := range resp.Capabilities {
				rows = append(rows, []string{"", capability.String()})
			}
			return
		})
	}

Michael Yang's avatar
Michael Yang committed
970
971
972
973
974
975
976
977
978
	if resp.ProjectorInfo != nil {
		tableRender("Projector", func() (rows [][]string) {
			arch := resp.ProjectorInfo["general.architecture"].(string)
			rows = append(rows, []string{"", "architecture", arch})
			rows = append(rows, []string{"", "parameters", format.HumanNumber(uint64(resp.ProjectorInfo["general.parameter_count"].(float64)))})
			rows = append(rows, []string{"", "embedding length", strconv.FormatFloat(resp.ProjectorInfo[fmt.Sprintf("%s.vision.embedding_length", arch)].(float64), 'f', -1, 64)})
			rows = append(rows, []string{"", "dimensions", strconv.FormatFloat(resp.ProjectorInfo[fmt.Sprintf("%s.vision.projection_dim", arch)].(float64), 'f', -1, 64)})
			return
		})
979
980
	}

Michael Yang's avatar
Michael Yang committed
981
982
983
984
985
986
987
988
989
990
	if resp.Parameters != "" {
		tableRender("Parameters", func() (rows [][]string) {
			scanner := bufio.NewScanner(strings.NewReader(resp.Parameters))
			for scanner.Scan() {
				if text := scanner.Text(); text != "" {
					rows = append(rows, append([]string{""}, strings.Fields(text)...))
				}
			}
			return
		})
991
992
	}

993
994
995
996
997
998
999
1000
1001
1002
1003
	if resp.ModelInfo != nil && verbose {
		tableRender("Metadata", func() (rows [][]string) {
			keys := make([]string, 0, len(resp.ModelInfo))
			for k := range resp.ModelInfo {
				keys = append(keys, k)
			}
			sort.Strings(keys)

			for _, k := range keys {
				var v string
				switch vData := resp.ModelInfo[k].(type) {
1004
1005
				case bool:
					v = fmt.Sprintf("%t", vData)
1006
1007
1008
1009
1010
				case string:
					v = vData
				case float64:
					v = fmt.Sprintf("%g", vData)
				case []any:
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
					targetWidth := 10 // Small width where we are displaying the data in a column

					var itemsToShow int
					totalWidth := 1 // Start with 1 for opening bracket

					// Find how many we can fit
					for i := range vData {
						itemStr := fmt.Sprintf("%v", vData[i])
						width := runewidth.StringWidth(itemStr)

						// Add separator width (", ") for all items except the first
						if i > 0 {
							width += 2
						}

						// Check if adding this item would exceed our width limit
						if totalWidth+width > targetWidth && i > 0 {
							break
						}

						totalWidth += width
						itemsToShow++
					}

					// Format the output
					if itemsToShow < len(vData) {
						v = fmt.Sprintf("%v", vData[:itemsToShow])
						v = strings.TrimSuffix(v, "]")
						v += fmt.Sprintf(" ...+%d more]", len(vData)-itemsToShow)
					} else {
						v = fmt.Sprintf("%v", vData)
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
					}
				default:
					v = fmt.Sprintf("%T", vData)
				}
				rows = append(rows, []string{"", k, v})
			}
			return
		})
	}

	if len(resp.Tensors) > 0 && verbose {
		tableRender("Tensors", func() (rows [][]string) {
			for _, t := range resp.Tensors {
				rows = append(rows, []string{"", t.Name, t.Type, fmt.Sprint(t.Shape)})
			}
			return
		})
	}

Michael Yang's avatar
Michael Yang committed
1061
1062
	head := func(s string, n int) (rows [][]string) {
		scanner := bufio.NewScanner(strings.NewReader(s))
1063
1064
1065
1066
1067
		count := 0
		for scanner.Scan() {
			text := strings.TrimSpace(scanner.Text())
			if text == "" {
				continue
1068
			}
1069
1070
1071
1072
1073
1074
1075
			count++
			if n < 0 || count <= n {
				rows = append(rows, []string{"", text})
			}
		}
		if n >= 0 && count > n {
			rows = append(rows, []string{"", "..."})
1076
		}
Michael Yang's avatar
Michael Yang committed
1077
		return
1078
1079
	}

Michael Yang's avatar
Michael Yang committed
1080
1081
1082
1083
1084
	if resp.System != "" {
		tableRender("System", func() [][]string {
			return head(resp.System, 2)
		})
	}
1085

Michael Yang's avatar
Michael Yang committed
1086
1087
1088
1089
	if resp.License != "" {
		tableRender("License", func() [][]string {
			return head(resp.License, 2)
		})
1090
	}
Michael Yang's avatar
Michael Yang committed
1091
1092

	return nil
1093
1094
}

Patrick Devine's avatar
Patrick Devine committed
1095
func CopyHandler(cmd *cobra.Command, args []string) error {
Michael Yang's avatar
Michael Yang committed
1096
	client, err := api.ClientFromEnvironment()
1097
1098
1099
	if err != nil {
		return err
	}
Patrick Devine's avatar
Patrick Devine committed
1100
1101

	req := api.CopyRequest{Source: args[0], Destination: args[1]}
Michael Yang's avatar
Michael Yang committed
1102
	if err := client.Copy(cmd.Context(), &req); err != nil {
Patrick Devine's avatar
Patrick Devine committed
1103
1104
1105
1106
1107
1108
		return err
	}
	fmt.Printf("copied '%s' to '%s'\n", args[0], args[1])
	return nil
}

1109
func PullHandler(cmd *cobra.Command, args []string) error {
1110
1111
1112
1113
1114
	insecure, err := cmd.Flags().GetBool("insecure")
	if err != nil {
		return err
	}

Michael Yang's avatar
Michael Yang committed
1115
	client, err := api.ClientFromEnvironment()
1116
1117
1118
	if err != nil {
		return err
	}
1119

Michael Yang's avatar
Michael Yang committed
1120
1121
1122
1123
1124
	p := progress.NewProgress(os.Stderr)
	defer p.Stop()

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

1125
1126
	var status string
	var spinner *progress.Spinner
Michael Yang's avatar
Michael Yang committed
1127

1128
	fn := func(resp api.ProgressResponse) error {
Michael Yang's avatar
Michael Yang committed
1129
		if resp.Digest != "" {
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
			if resp.Completed == 0 {
				// This is the initial status update for the
				// layer, which the server sends before
				// beginning the download, for clients to
				// compute total size and prepare for
				// downloads, if needed.
				//
				// Skipping this here to avoid showing a 0%
				// progress bar, which *should* clue the user
				// into the fact that many things are being
				// downloaded and that the current active
				// download is not that last. However, in rare
				// cases it seems to be triggering to some, and
				// it isn't worth explaining, so just ignore
				// and regress to the old UI that keeps giving
				// you the "But wait, there is more!" after
				// each "100% done" bar, which is "better."
				return nil
			}

1150
1151
1152
			if spinner != nil {
				spinner.Stop()
			}
Michael Yang's avatar
Michael Yang committed
1153
1154
1155

			bar, ok := bars[resp.Digest]
			if !ok {
1156
1157
1158
1159
1160
1161
				name, isDigest := strings.CutPrefix(resp.Digest, "sha256:")
				name = strings.TrimSpace(name)
				if isDigest {
					name = name[:min(12, len(name))]
				}
				bar = progress.NewBar(fmt.Sprintf("pulling %s:", name), resp.Total, resp.Completed)
Michael Yang's avatar
Michael Yang committed
1162
1163
1164
1165
1166
1167
				bars[resp.Digest] = bar
				p.Add(resp.Digest, bar)
			}

			bar.Set(resp.Completed)
		} else if status != resp.Status {
1168
1169
1170
			if spinner != nil {
				spinner.Stop()
			}
Michael Yang's avatar
Michael Yang committed
1171
1172
1173
1174
1175
1176

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

1177
1178
		return nil
	}
1179

Michael Yang's avatar
Michael Yang committed
1180
	request := api.PullRequest{Name: args[0], Insecure: insecure}
1181
	return client.Pull(cmd.Context(), &request, fn)
Michael Yang's avatar
Michael Yang committed
1182
1183
}

1184
1185
type generateContextKey string

1186
type runOptions struct {
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
	Model        string
	ParentModel  string
	Prompt       string
	Messages     []api.Message
	WordWrap     bool
	Format       string
	System       string
	Images       []api.ImageData
	Options      map[string]any
	MultiModal   bool
	KeepAlive    *api.Duration
Michael Yang's avatar
Michael Yang committed
1198
	Think        *api.ThinkValue
1199
	HideThinking bool
1200
	ShowConnect  bool
1201
1202
}

1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
func (r runOptions) Copy() runOptions {
	var messages []api.Message
	if r.Messages != nil {
		messages = make([]api.Message, len(r.Messages))
		copy(messages, r.Messages)
	}

	var images []api.ImageData
	if r.Images != nil {
		images = make([]api.ImageData, len(r.Images))
		copy(images, r.Images)
	}

	var opts map[string]any
	if r.Options != nil {
		opts = make(map[string]any, len(r.Options))
		for k, v := range r.Options {
			opts[k] = v
		}
	}

	var think *api.ThinkValue
	if r.Think != nil {
		cThink := *r.Think
		think = &cThink
	}

	return runOptions{
		Model:        r.Model,
		ParentModel:  r.ParentModel,
		Prompt:       r.Prompt,
		Messages:     messages,
		WordWrap:     r.WordWrap,
		Format:       r.Format,
		System:       r.System,
		Images:       images,
		Options:      opts,
		MultiModal:   r.MultiModal,
		KeepAlive:    r.KeepAlive,
		Think:        think,
		HideThinking: r.HideThinking,
		ShowConnect:  r.ShowConnect,
	}
}

1248
1249
1250
1251
1252
1253
1254
1255
1256
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
1257
1258
			if state.lineLength+1 > termWidth-5 {
				if runewidth.StringWidth(state.wordBuffer) > termWidth-10 {
1259
1260
1261
1262
1263
1264
1265
					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
1266
1267
				a := runewidth.StringWidth(state.wordBuffer)
				if a > 0 {
1268
					fmt.Printf("\x1b[%dD", a)
1269
1270
				}
				fmt.Printf("\x1b[K\n")
1271
				fmt.Printf("%s%c", state.wordBuffer, ch)
1272
1273
1274
				chWidth := runewidth.RuneWidth(ch)

				state.lineLength = runewidth.StringWidth(state.wordBuffer) + chWidth
1275
1276
			} else {
				fmt.Print(string(ch))
1277
1278
1279
1280
				state.lineLength += runewidth.RuneWidth(ch)
				if runewidth.RuneWidth(ch) >= 2 {
					state.wordBuffer = ""
					continue
Josh Yan's avatar
Josh Yan committed
1281
				}
1282
1283

				switch ch {
Michael Yang's avatar
Michael Yang committed
1284
				case ' ', '\t':
1285
					state.wordBuffer = ""
Michael Yang's avatar
Michael Yang committed
1286
				case '\n', '\r':
1287
					state.lineLength = 0
Michael Yang's avatar
Michael Yang committed
1288
					state.wordBuffer = ""
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
				default:
					state.wordBuffer += string(ch)
				}
			}
		}
	} else {
		fmt.Printf("%s%s", state.wordBuffer, content)
		if len(state.wordBuffer) > 0 {
			state.wordBuffer = ""
		}
	}
}

1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
func thinkingOutputOpeningText(plainText bool) string {
	text := "Thinking...\n"

	if plainText {
		return text
	}

	return readline.ColorGrey + readline.ColorBold + text + readline.ColorDefault + readline.ColorGrey
}

func thinkingOutputClosingText(plainText bool) string {
	text := "...done thinking.\n\n"

	if plainText {
		return text
	}

	return readline.ColorGrey + readline.ColorBold + text + readline.ColorDefault
}

1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
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{}
Michael Yang's avatar
Michael Yang committed
1346
	var thinkingContent strings.Builder
1347
1348
	var latest api.ChatResponse
	var fullResponse strings.Builder
1349
1350
	var thinkTagOpened bool = false
	var thinkTagClosed bool = false
1351

1352
1353
	role := "assistant"

1354
	fn := func(response api.ChatResponse) error {
1355
1356
1357
		if response.Message.Content != "" || !opts.HideThinking {
			p.StopAndClear()
		}
1358
1359
1360
1361

		latest = response

		role = response.Message.Role
1362
1363
1364
1365
		if response.Message.Thinking != "" && !opts.HideThinking {
			if !thinkTagOpened {
				fmt.Print(thinkingOutputOpeningText(false))
				thinkTagOpened = true
Michael Yang's avatar
Michael Yang committed
1366
				thinkTagClosed = false
1367
			}
Michael Yang's avatar
Michael Yang committed
1368
			thinkingContent.WriteString(response.Message.Thinking)
1369
1370
1371
			displayResponse(response.Message.Thinking, opts.WordWrap, state)
		}

1372
		content := response.Message.Content
Michael Yang's avatar
Michael Yang committed
1373
1374
1375
1376
		if thinkTagOpened && !thinkTagClosed && (content != "" || len(response.Message.ToolCalls) > 0) {
			if !strings.HasSuffix(thinkingContent.String(), "\n") {
				fmt.Println()
			}
1377
			fmt.Print(thinkingOutputClosingText(false))
Michael Yang's avatar
Michael Yang committed
1378
			thinkTagOpened = false
1379
			thinkTagClosed = true
Michael Yang's avatar
Michael Yang committed
1380
			state = &displayResponseState{}
1381
1382
1383
1384
1385
		}
		// purposefully not putting thinking blocks in the response, which would
		// only be needed if we later added tool calling to the cli (they get
		// filtered out anyway since current models don't expect them unless you're
		// about to finish some tool calls)
1386
1387
		fullResponse.WriteString(content)

Michael Yang's avatar
Michael Yang committed
1388
1389
1390
1391
1392
1393
1394
		if response.Message.ToolCalls != nil {
			toolCalls := response.Message.ToolCalls
			if len(toolCalls) > 0 {
				fmt.Print(renderToolCalls(toolCalls, false))
			}
		}

1395
1396
1397
1398
1399
		displayResponse(content, opts.WordWrap, state)

		return nil
	}

1400
1401
1402
1403
	if opts.Format == "json" {
		opts.Format = `"` + opts.Format + `"`
	}

1404
1405
1406
	req := &api.ChatRequest{
		Model:    opts.Model,
		Messages: opts.Messages,
1407
		Format:   json.RawMessage(opts.Format),
1408
		Options:  opts.Options,
1409
		Think:    opts.Think,
1410
1411
	}

1412
1413
1414
1415
	if opts.KeepAlive != nil {
		req.KeepAlive = opts.KeepAlive
	}

1416
1417
1418
1419
	if err := client.Chat(cancelCtx, req, fn); err != nil {
		if errors.Is(err, context.Canceled) {
			return nil, nil
		}
1420
1421
1422
1423
1424
1425
1426
1427

		// this error should ideally be wrapped properly by the client
		if strings.Contains(err.Error(), "upstream error") {
			p.StopAndClear()
			fmt.Println("An error occurred while processing your message. Please try again.")
			fmt.Println()
			return nil, nil
		}
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
		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()
	}

1445
	return &api.Message{Role: role, Thinking: thinkingContent.String(), Content: fullResponse.String()}, nil
1446
1447
1448
}

func generate(cmd *cobra.Command, opts runOptions) error {
Michael Yang's avatar
Michael Yang committed
1449
	client, err := api.ClientFromEnvironment()
Patrick Devine's avatar
Patrick Devine committed
1450
	if err != nil {
1451
		return err
Patrick Devine's avatar
Patrick Devine committed
1452
	}
Michael Yang's avatar
Michael Yang committed
1453

Michael Yang's avatar
Michael Yang committed
1454
	p := progress.NewProgress(os.Stderr)
1455
	defer p.StopAndClear()
1456

Michael Yang's avatar
Michael Yang committed
1457
1458
1459
	spinner := progress.NewSpinner("")
	p.Add("", spinner)

1460
1461
1462
1463
1464
1465
1466
	var latest api.GenerateResponse

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

Michael Yang's avatar
Michael Yang committed
1467
	ctx, cancel := context.WithCancel(cmd.Context())
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
	defer cancel()

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

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

1478
	var state *displayResponseState = &displayResponseState{}
Michael Yang's avatar
Michael Yang committed
1479
	var thinkingContent strings.Builder
1480
1481
	var thinkTagOpened bool = false
	var thinkTagClosed bool = false
1482

1483
	plainText := !term.IsTerminal(int(os.Stdout.Fd()))
1484

1485
	fn := func(response api.GenerateResponse) error {
Patrick Devine's avatar
Patrick Devine committed
1486
		latest = response
1487
		content := response.Response
1488

1489
1490
1491
1492
1493
1494
1495
1496
		if response.Response != "" || !opts.HideThinking {
			p.StopAndClear()
		}

		if response.Thinking != "" && !opts.HideThinking {
			if !thinkTagOpened {
				fmt.Print(thinkingOutputOpeningText(plainText))
				thinkTagOpened = true
Michael Yang's avatar
Michael Yang committed
1497
				thinkTagClosed = false
1498
			}
Michael Yang's avatar
Michael Yang committed
1499
			thinkingContent.WriteString(response.Thinking)
1500
1501
1502
			displayResponse(response.Thinking, opts.WordWrap, state)
		}

Michael Yang's avatar
Michael Yang committed
1503
1504
1505
1506
		if thinkTagOpened && !thinkTagClosed && (content != "" || len(response.ToolCalls) > 0) {
			if !strings.HasSuffix(thinkingContent.String(), "\n") {
				fmt.Println()
			}
1507
			fmt.Print(thinkingOutputClosingText(plainText))
Michael Yang's avatar
Michael Yang committed
1508
			thinkTagOpened = false
1509
			thinkTagClosed = true
Michael Yang's avatar
Michael Yang committed
1510
			state = &displayResponseState{}
1511
1512
		}

1513
		displayResponse(content, opts.WordWrap, state)
1514

Michael Yang's avatar
Michael Yang committed
1515
1516
1517
1518
1519
1520
1521
		if response.ToolCalls != nil {
			toolCalls := response.ToolCalls
			if len(toolCalls) > 0 {
				fmt.Print(renderToolCalls(toolCalls, plainText))
			}
		}

Patrick Devine's avatar
Patrick Devine committed
1522
1523
		return nil
	}
1524

1525
1526
1527
1528
1529
1530
1531
	if opts.MultiModal {
		opts.Prompt, opts.Images, err = extractFileData(opts.Prompt)
		if err != nil {
			return err
		}
	}

1532
1533
1534
1535
	if opts.Format == "json" {
		opts.Format = `"` + opts.Format + `"`
	}

Michael Yang's avatar
Michael Yang committed
1536
	request := api.GenerateRequest{
1537
1538
1539
1540
		Model:     opts.Model,
		Prompt:    opts.Prompt,
		Context:   generateContext,
		Images:    opts.Images,
1541
		Format:    json.RawMessage(opts.Format),
1542
1543
1544
		System:    opts.System,
		Options:   opts.Options,
		KeepAlive: opts.KeepAlive,
1545
		Think:     opts.Think,
Michael Yang's avatar
Michael Yang committed
1546
1547
1548
	}

	if err := client.Generate(ctx, &request, fn); err != nil {
1549
		if errors.Is(err, context.Canceled) {
1550
			return nil
1551
		}
1552
		return err
Patrick Devine's avatar
Patrick Devine committed
1553
	}
1554

1555
	if opts.Prompt != "" {
Michael Yang's avatar
Michael Yang committed
1556
1557
		fmt.Println()
		fmt.Println()
Patrick Devine's avatar
Patrick Devine committed
1558
	}
1559

1560
1561
1562
1563
	if !latest.Done {
		return nil
	}

Patrick Devine's avatar
Patrick Devine committed
1564
1565
	verbose, err := cmd.Flags().GetBool("verbose")
	if err != nil {
1566
		return err
Patrick Devine's avatar
Patrick Devine committed
1567
	}
Michael Yang's avatar
Michael Yang committed
1568

Patrick Devine's avatar
Patrick Devine committed
1569
1570
	if verbose {
		latest.Summary()
Michael Yang's avatar
Michael Yang committed
1571
	}
Michael Yang's avatar
Michael Yang committed
1572

Patrick Devine's avatar
Patrick Devine committed
1573
1574
1575
	ctx = context.WithValue(cmd.Context(), generateContextKey("context"), latest.Context)
	cmd.SetContext(ctx)

1576
	return nil
Michael Yang's avatar
Michael Yang committed
1577
1578
}

1579
func RunServer(_ *cobra.Command, _ []string) error {
Michael Yang's avatar
Michael Yang committed
1580
	if err := initializeKeypair(); err != nil {
1581
1582
1583
		return err
	}

Michael Yang's avatar
host  
Michael Yang committed
1584
	ln, err := net.Listen("tcp", envconfig.Host().Host)
1585
1586
1587
	if err != nil {
		return err
	}
Jeffrey Morgan's avatar
Jeffrey Morgan committed
1588

1589
1590
1591
1592
1593
1594
	err = server.Serve(ln)
	if errors.Is(err, http.ErrServerClosed) {
		return nil
	}

	return err
Jeffrey Morgan's avatar
Jeffrey Morgan committed
1595
1596
}

1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
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
1609
		cryptoPublicKey, cryptoPrivateKey, err := ed25519.GenerateKey(rand.Reader)
1610
1611
1612
1613
		if err != nil {
			return err
		}

Michael Yang's avatar
Michael Yang committed
1614
		privateKeyBytes, err := ssh.MarshalPrivateKey(cryptoPrivateKey, "")
1615
1616
1617
1618
		if err != nil {
			return err
		}

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

Michael Yang's avatar
Michael Yang committed
1623
		if err := os.WriteFile(privKeyPath, pem.EncodeToMemory(privateKeyBytes), 0o600); err != nil {
1624
1625
1626
			return err
		}

Michael Yang's avatar
Michael Yang committed
1627
		sshPublicKey, err := ssh.NewPublicKey(cryptoPublicKey)
1628
1629
1630
1631
		if err != nil {
			return err
		}

Michael Yang's avatar
Michael Yang committed
1632
		publicKeyBytes := ssh.MarshalAuthorizedKey(sshPublicKey)
1633

Michael Yang's avatar
Michael Yang committed
1634
		if err := os.WriteFile(pubKeyPath, publicKeyBytes, 0o644); err != nil {
1635
1636
1637
			return err
		}

Michael Yang's avatar
Michael Yang committed
1638
		fmt.Printf("Your new public key is: \n\n%s\n", publicKeyBytes)
1639
1640
1641
1642
	}
	return nil
}

Michael Yang's avatar
Michael Yang committed
1643
func checkServerHeartbeat(cmd *cobra.Command, _ []string) error {
Michael Yang's avatar
Michael Yang committed
1644
	client, err := api.ClientFromEnvironment()
1645
1646
1647
	if err != nil {
		return err
	}
Michael Yang's avatar
Michael Yang committed
1648
	if err := client.Heartbeat(cmd.Context()); err != nil {
1649
		if !(strings.Contains(err.Error(), " refused") || strings.Contains(err.Error(), "could not connect")) {
Bruce MacDonald's avatar
Bruce MacDonald committed
1650
1651
			return err
		}
1652
		if err := startApp(cmd.Context(), client); err != nil {
1653
			return fmt.Errorf("ollama server not responding - %w", err)
1654
1655
1656
1657
1658
		}
	}
	return nil
}

Michael Yang's avatar
Michael Yang committed
1659
1660
1661
1662
1663
1664
1665
1666
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
1667
1668
1669
1670
1671
		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
1672
1673
	}

1674
	if serverVersion != version.Version {
Michael Yang's avatar
Michael Yang committed
1675
		fmt.Printf("Warning: client version is %s\n", version.Version)
1676
	}
Michael Yang's avatar
Michael Yang committed
1677
1678
}

1679
func appendEnvDocs(cmd *cobra.Command, envs []envconfig.EnvVar) {
1680
1681
1682
1683
1684
	if len(envs) == 0 {
		return
	}

	envUsage := `
1685
1686
Environment Variables:
`
1687
	for _, e := range envs {
1688
		envUsage += fmt.Sprintf("      %-24s   %s\n", e.Name, e.Description)
1689
1690
1691
	}

	cmd.SetUsageTemplate(cmd.UsageTemplate() + envUsage)
1692
1693
}

Jeffrey Morgan's avatar
Jeffrey Morgan committed
1694
1695
func NewCLI() *cobra.Command {
	log.SetFlags(log.LstdFlags | log.Lshortfile)
Michael Yang's avatar
Michael Yang committed
1696
	cobra.EnableCommandSorting = false
Jeffrey Morgan's avatar
Jeffrey Morgan committed
1697

1698
	if runtime.GOOS == "windows" && term.IsTerminal(int(os.Stdout.Fd())) {
1699
		console.ConsoleFromFile(os.Stdin) //nolint:errcheck
1700
1701
	}

Jeffrey Morgan's avatar
Jeffrey Morgan committed
1702
	rootCmd := &cobra.Command{
1703
1704
1705
1706
		Use:           "ollama",
		Short:         "Large language model runner",
		SilenceUsage:  true,
		SilenceErrors: true,
Jeffrey Morgan's avatar
Jeffrey Morgan committed
1707
1708
1709
		CompletionOptions: cobra.CompletionOptions{
			DisableDefaultCmd: true,
		},
Michael Yang's avatar
Michael Yang committed
1710
1711
1712
1713
1714
1715
1716
1717
		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
1718
1719
	}

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

1722
	createCmd := &cobra.Command{
1723
		Use:     "create MODEL",
1724
		Short:   "Create a model",
Michael Yang's avatar
Michael Yang committed
1725
		Args:    cobra.ExactArgs(1),
1726
1727
		PreRunE: checkServerHeartbeat,
		RunE:    CreateHandler,
1728
1729
	}

1730
	createCmd.Flags().StringP("file", "f", "", "Name of the Modelfile (default \"Modelfile\")")
1731
	createCmd.Flags().StringP("quantize", "q", "", "Quantize model to this level (e.g. q4_K_M)")
1732

Patrick Devine's avatar
Patrick Devine committed
1733
1734
1735
	showCmd := &cobra.Command{
		Use:     "show MODEL",
		Short:   "Show information for a model",
Michael Yang's avatar
Michael Yang committed
1736
		Args:    cobra.ExactArgs(1),
Patrick Devine's avatar
Patrick Devine committed
1737
1738
1739
1740
1741
1742
1743
1744
		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")
1745
	showCmd.Flags().Bool("system", false, "Show system message of a model")
1746
	showCmd.Flags().BoolP("verbose", "v", false, "Show detailed model information")
Patrick Devine's avatar
Patrick Devine committed
1747

Jeffrey Morgan's avatar
Jeffrey Morgan committed
1748
	runCmd := &cobra.Command{
1749
1750
1751
1752
1753
		Use:     "run MODEL [PROMPT]",
		Short:   "Run a model",
		Args:    cobra.MinimumNArgs(1),
		PreRunE: checkServerHeartbeat,
		RunE:    RunHandler,
Jeffrey Morgan's avatar
Jeffrey Morgan committed
1754
1755
	}

1756
	runCmd.Flags().String("keepalive", "", "Duration to keep a model loaded (e.g. 5m)")
1757
	runCmd.Flags().Bool("verbose", false, "Show timings for response")
1758
	runCmd.Flags().Bool("insecure", false, "Use an insecure registry")
1759
	runCmd.Flags().Bool("nowordwrap", false, "Don't wrap words to the next line automatically")
Jeffrey Morgan's avatar
Jeffrey Morgan committed
1760
	runCmd.Flags().String("format", "", "Response format (e.g. json)")
Michael Yang's avatar
Michael Yang committed
1761
1762
	runCmd.Flags().String("think", "", "Enable thinking mode: true/false or high/medium/low for supported models")
	runCmd.Flags().Lookup("think").NoOptDefVal = "true"
1763
	runCmd.Flags().Bool("hidethinking", false, "Hide thinking output (if provided)")
1764
1765
	runCmd.Flags().Bool("truncate", false, "For embedding models: truncate inputs exceeding context length (default: true). Set --truncate=false to error instead")
	runCmd.Flags().Int("dimensions", 0, "Truncate output embeddings to specified dimension (embedding models only)")
1766
	runCmd.Flags().Bool("experimental", false, "Enable experimental agent loop with tools")
Patrick Devine's avatar
Patrick Devine committed
1767
1768
1769
1770
1771
1772
1773
1774
1775

	stopCmd := &cobra.Command{
		Use:     "stop MODEL",
		Short:   "Stop a running model",
		Args:    cobra.ExactArgs(1),
		PreRunE: checkServerHeartbeat,
		RunE:    StopHandler,
	}

Jeffrey Morgan's avatar
Jeffrey Morgan committed
1776
1777
1778
1779
	serveCmd := &cobra.Command{
		Use:     "serve",
		Aliases: []string{"start"},
		Short:   "Start ollama",
Michael Yang's avatar
Michael Yang committed
1780
		Args:    cobra.ExactArgs(0),
Michael Yang's avatar
Michael Yang committed
1781
		RunE:    RunServer,
Jeffrey Morgan's avatar
Jeffrey Morgan committed
1782
1783
	}

1784
	pullCmd := &cobra.Command{
1785
1786
		Use:     "pull MODEL",
		Short:   "Pull a model from a registry",
Michael Yang's avatar
Michael Yang committed
1787
		Args:    cobra.ExactArgs(1),
1788
1789
		PreRunE: checkServerHeartbeat,
		RunE:    PullHandler,
1790
1791
	}

1792
1793
	pullCmd.Flags().Bool("insecure", false, "Use an insecure registry")

1794
	pushCmd := &cobra.Command{
1795
1796
		Use:     "push MODEL",
		Short:   "Push a model to a registry",
Michael Yang's avatar
Michael Yang committed
1797
		Args:    cobra.ExactArgs(1),
1798
1799
		PreRunE: checkServerHeartbeat,
		RunE:    PushHandler,
1800
1801
	}

1802
1803
	pushCmd.Flags().Bool("insecure", false, "Use an insecure registry")

1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
	signinCmd := &cobra.Command{
		Use:     "signin",
		Short:   "Sign in to ollama.com",
		Args:    cobra.ExactArgs(0),
		PreRunE: checkServerHeartbeat,
		RunE:    SigninHandler,
	}

	signoutCmd := &cobra.Command{
		Use:     "signout",
		Short:   "Sign out from ollama.com",
		Args:    cobra.ExactArgs(0),
		PreRunE: checkServerHeartbeat,
		RunE:    SignoutHandler,
	}

Patrick Devine's avatar
Patrick Devine committed
1820
	listCmd := &cobra.Command{
1821
		Use:     "list",
Patrick Devine's avatar
Patrick Devine committed
1822
		Aliases: []string{"ls"},
1823
		Short:   "List models",
1824
		PreRunE: checkServerHeartbeat,
1825
		RunE:    ListHandler,
1826
	}
1827
1828
1829
1830
1831
1832
1833

	psCmd := &cobra.Command{
		Use:     "ps",
		Short:   "List running models",
		PreRunE: checkServerHeartbeat,
		RunE:    ListRunningHandler,
	}
Patrick Devine's avatar
Patrick Devine committed
1834
	copyCmd := &cobra.Command{
Michael Yang's avatar
Michael Yang committed
1835
		Use:     "cp SOURCE DESTINATION",
1836
		Short:   "Copy a model",
Michael Yang's avatar
Michael Yang committed
1837
		Args:    cobra.ExactArgs(2),
1838
1839
		PreRunE: checkServerHeartbeat,
		RunE:    CopyHandler,
Patrick Devine's avatar
Patrick Devine committed
1840
1841
	}

1842
	deleteCmd := &cobra.Command{
Michael Yang's avatar
Michael Yang committed
1843
		Use:     "rm MODEL [MODEL...]",
1844
1845
1846
1847
		Short:   "Remove a model",
		Args:    cobra.MinimumNArgs(1),
		PreRunE: checkServerHeartbeat,
		RunE:    DeleteHandler,
Patrick Devine's avatar
Patrick Devine committed
1848
1849
	}

1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
	runnerCmd := &cobra.Command{
		Use:    "runner",
		Hidden: true,
		RunE: func(cmd *cobra.Command, args []string) error {
			return runner.Execute(os.Args[1:])
		},
		FParseErrWhitelist: cobra.FParseErrWhitelist{UnknownFlags: true},
	}
	runnerCmd.SetHelpFunc(func(cmd *cobra.Command, args []string) {
		_ = runner.Execute(args[1:])
	})

1862
1863
1864
	envVars := envconfig.AsMap()

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

1866
1867
1868
1869
	for _, cmd := range []*cobra.Command{
		createCmd,
		showCmd,
		runCmd,
Patrick Devine's avatar
Patrick Devine committed
1870
		stopCmd,
1871
1872
1873
		pullCmd,
		pushCmd,
		listCmd,
1874
		psCmd,
1875
1876
		copyCmd,
		deleteCmd,
1877
		serveCmd,
1878
	} {
1879
1880
		switch cmd {
		case runCmd:
1881
1882
1883
1884
1885
			appendEnvDocs(cmd, []envconfig.EnvVar{envVars["OLLAMA_HOST"], envVars["OLLAMA_NOHISTORY"]})
		case serveCmd:
			appendEnvDocs(cmd, []envconfig.EnvVar{
				envVars["OLLAMA_DEBUG"],
				envVars["OLLAMA_HOST"],
1886
				envVars["OLLAMA_CONTEXT_LENGTH"],
1887
1888
1889
1890
1891
1892
1893
				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"],
1894
				envVars["OLLAMA_SCHED_SPREAD"],
1895
				envVars["OLLAMA_FLASH_ATTENTION"],
1896
				envVars["OLLAMA_KV_CACHE_TYPE"],
1897
				envVars["OLLAMA_LLM_LIBRARY"],
1898
				envVars["OLLAMA_GPU_OVERHEAD"],
1899
				envVars["OLLAMA_LOAD_TIMEOUT"],
1900
			})
1901
1902
1903
		default:
			appendEnvDocs(cmd, envs)
		}
1904
1905
	}

Jeffrey Morgan's avatar
Jeffrey Morgan committed
1906
1907
	rootCmd.AddCommand(
		serveCmd,
1908
		createCmd,
Patrick Devine's avatar
Patrick Devine committed
1909
		showCmd,
1910
		runCmd,
Patrick Devine's avatar
Patrick Devine committed
1911
		stopCmd,
1912
1913
		pullCmd,
		pushCmd,
1914
1915
		signinCmd,
		signoutCmd,
Patrick Devine's avatar
Patrick Devine committed
1916
		listCmd,
1917
		psCmd,
Patrick Devine's avatar
Patrick Devine committed
1918
		copyCmd,
1919
		deleteCmd,
1920
		runnerCmd,
Jeffrey Morgan's avatar
Jeffrey Morgan committed
1921
1922
1923
1924
	)

	return rootCmd
}
1925
1926
1927
1928
1929
1930
1931
1932
1933

// If the user has explicitly set thinking options, either through the CLI or
// through the `/set think` or `set nothink` interactive options, then we
// respect them. Otherwise, we check model capabilities to see if the model
// supports thinking. If the model does support thinking, we enable it.
// Otherwise, we unset the thinking option (which is different than setting it
// to false).
//
// If capabilities are not provided, we fetch them from the server.
Michael Yang's avatar
Michael Yang committed
1934
func inferThinkingOption(caps *[]model.Capability, runOpts *runOptions, explicitlySetByUser bool) (*api.ThinkValue, error) {
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
	if explicitlySetByUser {
		return runOpts.Think, nil
	}

	if caps == nil {
		client, err := api.ClientFromEnvironment()
		if err != nil {
			return nil, err
		}
		ret, err := client.Show(context.Background(), &api.ShowRequest{
			Model: runOpts.Model,
		})
		if err != nil {
			return nil, err
		}
		caps = &ret.Capabilities
	}

	thinkingSupported := false
	for _, cap := range *caps {
		if cap == model.CapabilityThinking {
			thinkingSupported = true
		}
	}

	if thinkingSupported {
Michael Yang's avatar
Michael Yang committed
1961
		return &api.ThinkValue{Value: true}, nil
1962
1963
1964
1965
	}

	return nil, nil
}
Michael Yang's avatar
Michael Yang committed
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991

func renderToolCalls(toolCalls []api.ToolCall, plainText bool) string {
	out := ""
	formatExplanation := ""
	formatValues := ""
	if !plainText {
		formatExplanation = readline.ColorGrey + readline.ColorBold
		formatValues = readline.ColorDefault
		out += formatExplanation
	}
	for i, toolCall := range toolCalls {
		argsAsJSON, err := json.Marshal(toolCall.Function.Arguments)
		if err != nil {
			return ""
		}
		if i > 0 {
			out += "\n"
		}
		// all tool calls are unexpected since we don't currently support registering any in the CLI
		out += fmt.Sprintf("  Model called a non-existent function '%s()' with arguments: %s", formatValues+toolCall.Function.Name+formatExplanation, formatValues+string(argsAsJSON)+formatExplanation)
	}
	if !plainText {
		out += readline.ColorDefault
	}
	return out
}