interactive.go 18 KB
Newer Older
1
2
3
4
5
6
7
8
package cmd

import (
	"errors"
	"fmt"
	"io"
	"net/http"
	"os"
9
	"path/filepath"
10
	"regexp"
11
	"slices"
12
	"sort"
13
14
15
16
	"strings"

	"github.com/spf13/cobra"

17
	"github.com/ollama/ollama/api"
18
	"github.com/ollama/ollama/envconfig"
19
20
	"github.com/ollama/ollama/progress"
	"github.com/ollama/ollama/readline"
21
	"github.com/ollama/ollama/types/errtypes"
22
23
24
25
26
27
28
29
30
31
)

type MultilineState int

const (
	MultilineNone MultilineState = iota
	MultilinePrompt
	MultilineSystem
)

32
33
34
35
36
37
38
func loadModel(cmd *cobra.Command, opts *runOptions) error {
	p := progress.NewProgress(os.Stderr)
	defer p.StopAndClear()

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

39
	client, err := api.ClientFromEnvironment()
40
	if err != nil {
41
		return err
42
43
	}

44
	chatReq := &api.ChatRequest{
45
46
		Model:     opts.Model,
		KeepAlive: opts.KeepAlive,
47
	}
48

49
	return client.Chat(cmd.Context(), chatReq, func(resp api.ChatResponse) error {
50
		p.StopAndClear()
51
52
53
54
55
56
57
58
59
		for _, msg := range opts.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()
60
61
62
63
64
65
66
67
68
			}
		}
		return nil
	})
}

func generateInteractive(cmd *cobra.Command, opts runOptions) error {
	err := loadModel(cmd, &opts)
	if err != nil {
69
70
71
72
73
		return err
	}

	usage := func() {
		fmt.Fprintln(os.Stderr, "Available Commands:")
74
75
76
77
		fmt.Fprintln(os.Stderr, "  /set            Set session variables")
		fmt.Fprintln(os.Stderr, "  /show           Show model information")
		fmt.Fprintln(os.Stderr, "  /load <model>   Load a session or model")
		fmt.Fprintln(os.Stderr, "  /save <model>   Save your current session")
Bryce Reitano's avatar
Bryce Reitano committed
78
		fmt.Fprintln(os.Stderr, "  /clear          Clear session context")
79
80
81
		fmt.Fprintln(os.Stderr, "  /bye            Exit")
		fmt.Fprintln(os.Stderr, "  /?, /help       Help for a command")
		fmt.Fprintln(os.Stderr, "  /? shortcuts    Help for keyboard shortcuts")
82
83
		fmt.Fprintln(os.Stderr, "")
		fmt.Fprintln(os.Stderr, "Use \"\"\" to begin a multi-line message.")
84
85
86
87
88

		if opts.MultiModal {
			fmt.Fprintf(os.Stderr, "Use %s to include .jpg or .png images.\n", filepath.FromSlash("/path/to/file"))
		}

89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
		fmt.Fprintln(os.Stderr, "")
	}

	usageSet := func() {
		fmt.Fprintln(os.Stderr, "Available Commands:")
		fmt.Fprintln(os.Stderr, "  /set parameter ...     Set a parameter")
		fmt.Fprintln(os.Stderr, "  /set system <string>   Set system message")
		fmt.Fprintln(os.Stderr, "  /set history           Enable history")
		fmt.Fprintln(os.Stderr, "  /set nohistory         Disable history")
		fmt.Fprintln(os.Stderr, "  /set wordwrap          Enable wordwrap")
		fmt.Fprintln(os.Stderr, "  /set nowordwrap        Disable wordwrap")
		fmt.Fprintln(os.Stderr, "  /set format json       Enable JSON mode")
		fmt.Fprintln(os.Stderr, "  /set noformat          Disable formatting")
		fmt.Fprintln(os.Stderr, "  /set verbose           Show LLM stats")
		fmt.Fprintln(os.Stderr, "  /set quiet             Disable LLM stats")
		fmt.Fprintln(os.Stderr, "")
	}

	usageShortcuts := func() {
		fmt.Fprintln(os.Stderr, "Available keyboard shortcuts:")
		fmt.Fprintln(os.Stderr, "  Ctrl + a            Move to the beginning of the line (Home)")
		fmt.Fprintln(os.Stderr, "  Ctrl + e            Move to the end of the line (End)")
		fmt.Fprintln(os.Stderr, "   Alt + b            Move back (left) one word")
		fmt.Fprintln(os.Stderr, "   Alt + f            Move forward (right) one word")
		fmt.Fprintln(os.Stderr, "  Ctrl + k            Delete the sentence after the cursor")
		fmt.Fprintln(os.Stderr, "  Ctrl + u            Delete the sentence before the cursor")
Josh Yan's avatar
Josh Yan committed
115
		fmt.Fprintln(os.Stderr, "  Ctrl + w            Delete the word before the cursor")
116
117
118
119
120
121
122
123
124
		fmt.Fprintln(os.Stderr, "")
		fmt.Fprintln(os.Stderr, "  Ctrl + l            Clear the screen")
		fmt.Fprintln(os.Stderr, "  Ctrl + c            Stop the model from responding")
		fmt.Fprintln(os.Stderr, "  Ctrl + d            Exit ollama (/bye)")
		fmt.Fprintln(os.Stderr, "")
	}

	usageShow := func() {
		fmt.Fprintln(os.Stderr, "Available Commands:")
125
		fmt.Fprintln(os.Stderr, "  /show info         Show details for this model")
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
		fmt.Fprintln(os.Stderr, "  /show license      Show model license")
		fmt.Fprintln(os.Stderr, "  /show modelfile    Show Modelfile for this model")
		fmt.Fprintln(os.Stderr, "  /show parameters   Show parameters for this model")
		fmt.Fprintln(os.Stderr, "  /show system       Show system message")
		fmt.Fprintln(os.Stderr, "  /show template     Show prompt template")
		fmt.Fprintln(os.Stderr, "")
	}

	// only list out the most common parameters
	usageParameters := func() {
		fmt.Fprintln(os.Stderr, "Available Parameters:")
		fmt.Fprintln(os.Stderr, "  /set parameter seed <int>             Random number seed")
		fmt.Fprintln(os.Stderr, "  /set parameter num_predict <int>      Max number of tokens to predict")
		fmt.Fprintln(os.Stderr, "  /set parameter top_k <int>            Pick from top k num of tokens")
		fmt.Fprintln(os.Stderr, "  /set parameter top_p <float>          Pick token based on sum of probabilities")
141
		fmt.Fprintln(os.Stderr, "  /set parameter min_p <float>          Pick token based on top token probability * min_p")
142
143
144
145
146
		fmt.Fprintln(os.Stderr, "  /set parameter num_ctx <int>          Set the context size")
		fmt.Fprintln(os.Stderr, "  /set parameter temperature <float>    Set creativity level")
		fmt.Fprintln(os.Stderr, "  /set parameter repeat_penalty <float> How strongly to penalize repetitions")
		fmt.Fprintln(os.Stderr, "  /set parameter repeat_last_n <int>    Set how far back to look for repetitions")
		fmt.Fprintln(os.Stderr, "  /set parameter num_gpu <int>          The number of layers to send to the GPU")
147
		fmt.Fprintln(os.Stderr, "  /set parameter stop <string> <string> ...   Set the stop parameters")
148
149
150
151
152
153
154
155
156
157
158
159
160
		fmt.Fprintln(os.Stderr, "")
	}

	scanner, err := readline.New(readline.Prompt{
		Prompt:         ">>> ",
		AltPrompt:      "... ",
		Placeholder:    "Send a message (/? for help)",
		AltPlaceholder: `Use """ to end multi-line input`,
	})
	if err != nil {
		return err
	}

161
	if envconfig.NoHistory {
162
163
164
		scanner.HistoryDisable()
	}

165
166
167
	fmt.Print(readline.StartBracketedPaste)
	defer fmt.Printf(readline.EndBracketedPaste)

168
	var sb strings.Builder
169
170
171
172
173
174
175
176
177
178
179
180
181
182
	var multiline MultilineState

	for {
		line, err := scanner.Readline()
		switch {
		case errors.Is(err, io.EOF):
			fmt.Println()
			return nil
		case errors.Is(err, readline.ErrInterrupt):
			if line == "" {
				fmt.Println("\nUse Ctrl + d or /bye to exit.")
			}

			scanner.Prompt.UseAlt = false
183
			sb.Reset()
184
185
186
187
188
189
190

			continue
		case err != nil:
			return err
		}

		switch {
191
192
193
194
195
196
		case multiline != MultilineNone:
			// check if there's a multiline terminating string
			before, ok := strings.CutSuffix(line, `"""`)
			sb.WriteString(before)
			if !ok {
				fmt.Fprintln(&sb)
197
198
199
200
201
				continue
			}

			switch multiline {
			case MultilineSystem:
202
				opts.System = sb.String()
203
				opts.Messages = append(opts.Messages, api.Message{Role: "system", Content: opts.System})
204
				fmt.Println("Set system message.")
205
				sb.Reset()
206
			}
207

208
			multiline = MultilineNone
209
210
211
212
213
214
215
216
217
218
219
			scanner.Prompt.UseAlt = false
		case strings.HasPrefix(line, `"""`):
			line := strings.TrimPrefix(line, `"""`)
			line, ok := strings.CutSuffix(line, `"""`)
			sb.WriteString(line)
			if !ok {
				// no multiline terminating string; need more input
				fmt.Fprintln(&sb)
				multiline = MultilinePrompt
				scanner.Prompt.UseAlt = true
			}
220
		case scanner.Pasting:
221
			fmt.Fprintln(&sb, line)
222
223
224
225
226
227
			continue
		case strings.HasPrefix(line, "/list"):
			args := strings.Fields(line)
			if err := ListHandler(cmd, args[1:]); err != nil {
				return err
			}
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
		case strings.HasPrefix(line, "/load"):
			args := strings.Fields(line)
			if len(args) != 2 {
				fmt.Println("Usage:\n  /load <modelname>")
				continue
			}
			opts.Model = args[1]
			opts.Messages = []api.Message{}
			fmt.Printf("Loading model '%s'\n", opts.Model)
			if err := loadModel(cmd, &opts); err != nil {
				return err
			}
			continue
		case strings.HasPrefix(line, "/save"):
			args := strings.Fields(line)
			if len(args) != 2 {
				fmt.Println("Usage:\n  /save <modelname>")
				continue
			}

			client, err := api.ClientFromEnvironment()
			if err != nil {
				fmt.Println("error: couldn't connect to ollama server")
				return err
			}

			req := &api.CreateRequest{
				Name:      args[1],
				Modelfile: buildModelfile(opts),
			}
			fn := func(resp api.ProgressResponse) error { return nil }
			err = client.Create(cmd.Context(), req, fn)
			if err != nil {
261
262
263
264
				if strings.Contains(err.Error(), errtypes.InvalidModelNameErrMsg) {
					fmt.Printf("error: The model name '%s' is invalid\n", args[1])
					continue
				}
265
266
267
268
				return err
			}
			fmt.Printf("Created new model '%s'\n", args[1])
			continue
Bryce Reitano's avatar
Bryce Reitano committed
269
270
		case strings.HasPrefix(line, "/clear"):
			opts.Messages = []api.Message{}
Patrick Devine's avatar
Patrick Devine committed
271
272
273
274
			if opts.System != "" {
				newMessage := api.Message{Role: "system", Content: opts.System}
				opts.Messages = append(opts.Messages, newMessage)
			}
Bryce Reitano's avatar
Bryce Reitano committed
275
276
			fmt.Println("Cleared session context")
			continue
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
		case strings.HasPrefix(line, "/set"):
			args := strings.Fields(line)
			if len(args) > 1 {
				switch args[1] {
				case "history":
					scanner.HistoryEnable()
				case "nohistory":
					scanner.HistoryDisable()
				case "wordwrap":
					opts.WordWrap = true
					fmt.Println("Set 'wordwrap' mode.")
				case "nowordwrap":
					opts.WordWrap = false
					fmt.Println("Set 'nowordwrap' mode.")
				case "verbose":
292
293
294
					if err := cmd.Flags().Set("verbose", "true"); err != nil {
						return err
					}
295
296
					fmt.Println("Set 'verbose' mode.")
				case "quiet":
297
298
299
					if err := cmd.Flags().Set("verbose", "false"); err != nil {
						return err
					}
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
					fmt.Println("Set 'quiet' mode.")
				case "format":
					if len(args) < 3 || args[2] != "json" {
						fmt.Println("Invalid or missing format. For 'json' mode use '/set format json'")
					} else {
						opts.Format = args[2]
						fmt.Printf("Set format to '%s' mode.\n", args[2])
					}
				case "noformat":
					opts.Format = ""
					fmt.Println("Disabled format.")
				case "parameter":
					if len(args) < 4 {
						usageParameters()
						continue
					}
Michael Yang's avatar
Michael Yang committed
316
					params := args[3:]
317
318
					fp, err := api.FormatParams(map[string][]string{args[2]: params})
					if err != nil {
319
						fmt.Printf("Couldn't set parameter: %q\n", err)
320
321
						continue
					}
322
					fmt.Printf("Set parameter '%s' to '%s'\n", args[2], strings.Join(params, ", "))
323
					opts.Options[args[2]] = fp[args[2]]
Patrick Devine's avatar
Patrick Devine committed
324
				case "system":
325
326
327
328
					if len(args) < 3 {
						usageSet()
						continue
					}
329

Patrick Devine's avatar
Patrick Devine committed
330
					multiline = MultilineSystem
331

332
					line := strings.Join(args[2:], " ")
333
334
335
					line, ok := strings.CutPrefix(line, `"""`)
					if !ok {
						multiline = MultilineNone
336
					} else {
337
338
339
340
341
342
343
344
345
346
347
348
349
						// only cut suffix if the line is multiline
						line, ok = strings.CutSuffix(line, `"""`)
						if ok {
							multiline = MultilineNone
						}
					}

					sb.WriteString(line)
					if multiline != MultilineNone {
						scanner.Prompt.UseAlt = true
						continue
					}

Patrick Devine's avatar
Patrick Devine committed
350
351
352
353
354
355
356
357
					opts.System = sb.String() // for display in modelfile
					newMessage := api.Message{Role: "system", Content: sb.String()}
					// Check if the slice is not empty and the last message is from 'system'
					if len(opts.Messages) > 0 && opts.Messages[len(opts.Messages)-1].Role == "system" {
						// Replace the last message
						opts.Messages[len(opts.Messages)-1] = newMessage
					} else {
						opts.Messages = append(opts.Messages, newMessage)
358
					}
Patrick Devine's avatar
Patrick Devine committed
359
360
					fmt.Println("Set system message.")
					sb.Reset()
361
362
363

					sb.Reset()
					continue
364
365
366
367
368
369
370
371
372
373
374
375
376
377
				default:
					fmt.Printf("Unknown command '/set %s'. Type /? for help\n", args[1])
				}
			} else {
				usageSet()
			}
		case strings.HasPrefix(line, "/show"):
			args := strings.Fields(line)
			if len(args) > 1 {
				client, err := api.ClientFromEnvironment()
				if err != nil {
					fmt.Println("error: couldn't connect to ollama server")
					return err
				}
378
				req := &api.ShowRequest{
379
					Name:     opts.Model,
380
381
382
383
					System:   opts.System,
					Options:  opts.Options,
				}
				resp, err := client.Show(cmd.Context(), req)
384
385
386
387
388
389
				if err != nil {
					fmt.Println("error: couldn't get model")
					return err
				}

				switch args[1] {
390
				case "info":
391
					showInfo(resp)
392
393
				case "license":
					if resp.License == "" {
394
						fmt.Println("No license was specified for this model.")
395
396
397
398
399
400
401
					} else {
						fmt.Println(resp.License)
					}
				case "modelfile":
					fmt.Println(resp.Modelfile)
				case "parameters":
					if resp.Parameters == "" {
402
						fmt.Println("No parameters were specified for this model.")
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
					} else {
						if len(opts.Options) > 0 {
							fmt.Println("User defined parameters:")
							for k, v := range opts.Options {
								fmt.Printf("%-*s %v\n", 30, k, v)
							}
							fmt.Println()
						}
						fmt.Println("Model defined parameters:")
						fmt.Println(resp.Parameters)
					}
				case "system":
					switch {
					case opts.System != "":
						fmt.Println(opts.System + "\n")
					case resp.System != "":
						fmt.Println(resp.System + "\n")
					default:
421
						fmt.Println("No system message was specified for this model.")
422
423
					}
				case "template":
Patrick Devine's avatar
Patrick Devine committed
424
					if resp.Template != "" {
425
						fmt.Println(resp.Template)
Patrick Devine's avatar
Patrick Devine committed
426
					} else {
427
						fmt.Println("No prompt template was specified for this model.")
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
					}
				default:
					fmt.Printf("Unknown command '/show %s'. Type /? for help\n", args[1])
				}
			} else {
				usageShow()
			}
		case strings.HasPrefix(line, "/help"), strings.HasPrefix(line, "/?"):
			args := strings.Fields(line)
			if len(args) > 1 {
				switch args[1] {
				case "set", "/set":
					usageSet()
				case "show", "/show":
					usageShow()
				case "shortcut", "shortcuts":
					usageShortcuts()
				}
			} else {
				usage()
			}
449
		case strings.HasPrefix(line, "/exit"), strings.HasPrefix(line, "/bye"):
450
451
452
453
454
			return nil
		case strings.HasPrefix(line, "/"):
			args := strings.Fields(line)
			isFile := false

455
			if opts.MultiModal {
456
457
458
459
460
461
462
463
				for _, f := range extractFileNames(line) {
					if strings.HasPrefix(f, args[0]) {
						isFile = true
						break
					}
				}
			}

464
			if !isFile {
465
466
467
				fmt.Printf("Unknown command '%s'. Type /? for help\n", args[0])
				continue
			}
468
469

			sb.WriteString(line)
470
		default:
471
			sb.WriteString(line)
472
473
		}

474
		if sb.Len() > 0 && multiline == MultilineNone {
475
476
			newMessage := api.Message{Role: "user", Content: sb.String()}

477
			if opts.MultiModal {
478
				msg, images, err := extractFileData(sb.String())
479
480
481
				if err != nil {
					return err
				}
482
483
484
485
486
487
488
489

				// clear all previous images for better responses
				if len(images) > 0 {
					for i := range opts.Messages {
						opts.Messages[i].Images = nil
					}
				}

490
				newMessage.Content = msg
491
				newMessage.Images = images
492
			}
493

494
495
496
497
			opts.Messages = append(opts.Messages, newMessage)

			assistant, err := chat(cmd, opts)
			if err != nil {
498
499
				return err
			}
500
501
502
			if assistant != nil {
				opts.Messages = append(opts.Messages, *assistant)
			}
503

504
			sb.Reset()
505
506
507
508
		}
	}
}

509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
func buildModelfile(opts runOptions) string {
	var mf strings.Builder
	model := opts.ParentModel
	if model == "" {
		model = opts.Model
	}
	fmt.Fprintf(&mf, "FROM %s\n", model)
	if opts.System != "" {
		fmt.Fprintf(&mf, "SYSTEM \"\"\"%s\"\"\"\n", opts.System)
	}

	keys := make([]string, 0)
	for k := range opts.Options {
		keys = append(keys, k)
	}
	sort.Strings(keys)
	for _, k := range keys {
		fmt.Fprintf(&mf, "PARAMETER %s %v\n", k, opts.Options[k])
	}
	fmt.Fprintln(&mf)

	for _, msg := range opts.Messages {
		fmt.Fprintf(&mf, "MESSAGE %s \"\"\"%s\"\"\"\n", msg.Role, msg.Content)
	}

	return mf.String()
}

537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
func normalizeFilePath(fp string) string {
	// Define a map of escaped characters and their replacements
	replacements := map[string]string{
		"\\ ":  " ",  // Escaped space
		"\\(":  "(",  // Escaped left parenthesis
		"\\)":  ")",  // Escaped right parenthesis
		"\\[":  "[",  // Escaped left square bracket
		"\\]":  "]",  // Escaped right square bracket
		"\\{":  "{",  // Escaped left curly brace
		"\\}":  "}",  // Escaped right curly brace
		"\\$":  "$",  // Escaped dollar sign
		"\\&":  "&",  // Escaped ampersand
		"\\;":  ";",  // Escaped semicolon
		"\\'":  "'",  // Escaped single quote
		"\\\\": "\\", // Escaped backslash
		"\\*":  "*",  // Escaped asterisk
		"\\?":  "?",  // Escaped question mark
	}

	for escaped, actual := range replacements {
		fp = strings.ReplaceAll(fp, escaped, actual)
	}
	return fp
}

func extractFileNames(input string) []string {
563
	// Regex to match file paths starting with optional drive letter, / ./ \ or .\ and include escaped or unescaped spaces (\ or %20)
564
	// and followed by more characters and a file extension
565
566
	// This will capture non filename strings, but we'll check for file existence to remove mismatches
	regexPattern := `(?:[a-zA-Z]:)?(?:\./|/|\\)[\S\\ ]+?\.(?i:jpg|jpeg|png|svg)\b`
567
568
569
570
571
	re := regexp.MustCompile(regexPattern)

	return re.FindAllString(input, -1)
}

572
func extractFileData(input string) (string, []api.ImageData, error) {
573
	filePaths := extractFileNames(input)
574
	var imgs []api.ImageData
575
576
577
578
579
580
581
582

	for _, fp := range filePaths {
		nfp := normalizeFilePath(fp)
		data, err := getImageData(nfp)
		if err != nil {
			if os.IsNotExist(err) {
				continue
			}
583
			fmt.Fprintf(os.Stderr, "Couldn't process image: %q\n", err)
584
585
			return "", imgs, err
		}
586
		fmt.Fprintf(os.Stderr, "Added image '%s'\n", nfp)
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
		input = strings.ReplaceAll(input, fp, "")
		imgs = append(imgs, data)
	}
	return input, imgs, nil
}

func getImageData(filePath string) ([]byte, error) {
	file, err := os.Open(filePath)
	if err != nil {
		return nil, err
	}
	defer file.Close()

	buf := make([]byte, 512)
	_, err = file.Read(buf)
	if err != nil {
		return nil, err
	}

	contentType := http.DetectContentType(buf)
607
	allowedTypes := []string{"image/jpeg", "image/jpg", "image/png"}
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
	if !slices.Contains(allowedTypes, contentType) {
		return nil, fmt.Errorf("invalid image type: %s", contentType)
	}

	info, err := file.Stat()
	if err != nil {
		return nil, err
	}

	// Check if the file size exceeds 100MB
	var maxSize int64 = 100 * 1024 * 1024 // 100MB in bytes
	if info.Size() > maxSize {
		return nil, fmt.Errorf("file size exceeds maximum limit (100MB)")
	}

	buf = make([]byte, info.Size())
	_, err = file.Seek(0, 0)
	if err != nil {
		return nil, err
	}

	_, err = io.ReadFull(file, buf)
	if err != nil {
		return nil, err
	}

	return buf, nil
}