interactive.go 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569
  1. package cmd
  2. import (
  3. "cmp"
  4. "errors"
  5. "fmt"
  6. "io"
  7. "net/http"
  8. "os"
  9. "path/filepath"
  10. "regexp"
  11. "slices"
  12. "strings"
  13. "github.com/spf13/cobra"
  14. "github.com/ollama/ollama/api"
  15. "github.com/ollama/ollama/envconfig"
  16. "github.com/ollama/ollama/readline"
  17. "github.com/ollama/ollama/types/errtypes"
  18. )
  19. type MultilineState int
  20. const (
  21. MultilineNone MultilineState = iota
  22. MultilinePrompt
  23. MultilineSystem
  24. )
  25. func generateInteractive(cmd *cobra.Command, opts runOptions) error {
  26. usage := func() {
  27. fmt.Fprintln(os.Stderr, "Available Commands:")
  28. fmt.Fprintln(os.Stderr, " /set Set session variables")
  29. fmt.Fprintln(os.Stderr, " /show Show model information")
  30. fmt.Fprintln(os.Stderr, " /load <model> Load a session or model")
  31. fmt.Fprintln(os.Stderr, " /save <model> Save your current session")
  32. fmt.Fprintln(os.Stderr, " /clear Clear session context")
  33. fmt.Fprintln(os.Stderr, " /bye Exit")
  34. fmt.Fprintln(os.Stderr, " /?, /help Help for a command")
  35. fmt.Fprintln(os.Stderr, " /? shortcuts Help for keyboard shortcuts")
  36. fmt.Fprintln(os.Stderr, "")
  37. fmt.Fprintln(os.Stderr, "Use \"\"\" to begin a multi-line message.")
  38. if opts.MultiModal {
  39. fmt.Fprintf(os.Stderr, "Use %s to include .jpg or .png images.\n", filepath.FromSlash("/path/to/file"))
  40. }
  41. fmt.Fprintln(os.Stderr, "")
  42. }
  43. usageSet := func() {
  44. fmt.Fprintln(os.Stderr, "Available Commands:")
  45. fmt.Fprintln(os.Stderr, " /set parameter ... Set a parameter")
  46. fmt.Fprintln(os.Stderr, " /set system <string> Set system message")
  47. fmt.Fprintln(os.Stderr, " /set history Enable history")
  48. fmt.Fprintln(os.Stderr, " /set nohistory Disable history")
  49. fmt.Fprintln(os.Stderr, " /set wordwrap Enable wordwrap")
  50. fmt.Fprintln(os.Stderr, " /set nowordwrap Disable wordwrap")
  51. fmt.Fprintln(os.Stderr, " /set format json Enable JSON mode")
  52. fmt.Fprintln(os.Stderr, " /set noformat Disable formatting")
  53. fmt.Fprintln(os.Stderr, " /set verbose Show LLM stats")
  54. fmt.Fprintln(os.Stderr, " /set quiet Disable LLM stats")
  55. fmt.Fprintln(os.Stderr, "")
  56. }
  57. usageShortcuts := func() {
  58. fmt.Fprintln(os.Stderr, "Available keyboard shortcuts:")
  59. fmt.Fprintln(os.Stderr, " Ctrl + a Move to the beginning of the line (Home)")
  60. fmt.Fprintln(os.Stderr, " Ctrl + e Move to the end of the line (End)")
  61. fmt.Fprintln(os.Stderr, " Alt + b Move back (left) one word")
  62. fmt.Fprintln(os.Stderr, " Alt + f Move forward (right) one word")
  63. fmt.Fprintln(os.Stderr, " Ctrl + k Delete the sentence after the cursor")
  64. fmt.Fprintln(os.Stderr, " Ctrl + u Delete the sentence before the cursor")
  65. fmt.Fprintln(os.Stderr, " Ctrl + w Delete the word before the cursor")
  66. fmt.Fprintln(os.Stderr, "")
  67. fmt.Fprintln(os.Stderr, " Ctrl + l Clear the screen")
  68. fmt.Fprintln(os.Stderr, " Ctrl + c Stop the model from responding")
  69. fmt.Fprintln(os.Stderr, " Ctrl + d Exit ollama (/bye)")
  70. fmt.Fprintln(os.Stderr, "")
  71. }
  72. usageShow := func() {
  73. fmt.Fprintln(os.Stderr, "Available Commands:")
  74. fmt.Fprintln(os.Stderr, " /show info Show details for this model")
  75. fmt.Fprintln(os.Stderr, " /show license Show model license")
  76. fmt.Fprintln(os.Stderr, " /show modelfile Show Modelfile for this model")
  77. fmt.Fprintln(os.Stderr, " /show parameters Show parameters for this model")
  78. fmt.Fprintln(os.Stderr, " /show system Show system message")
  79. fmt.Fprintln(os.Stderr, " /show template Show prompt template")
  80. fmt.Fprintln(os.Stderr, "")
  81. }
  82. // only list out the most common parameters
  83. usageParameters := func() {
  84. fmt.Fprintln(os.Stderr, "Available Parameters:")
  85. fmt.Fprintln(os.Stderr, " /set parameter seed <int> Random number seed")
  86. fmt.Fprintln(os.Stderr, " /set parameter num_predict <int> Max number of tokens to predict")
  87. fmt.Fprintln(os.Stderr, " /set parameter top_k <int> Pick from top k num of tokens")
  88. fmt.Fprintln(os.Stderr, " /set parameter top_p <float> Pick token based on sum of probabilities")
  89. fmt.Fprintln(os.Stderr, " /set parameter min_p <float> Pick token based on top token probability * min_p")
  90. fmt.Fprintln(os.Stderr, " /set parameter num_ctx <int> Set the context size")
  91. fmt.Fprintln(os.Stderr, " /set parameter temperature <float> Set creativity level")
  92. fmt.Fprintln(os.Stderr, " /set parameter repeat_penalty <float> How strongly to penalize repetitions")
  93. fmt.Fprintln(os.Stderr, " /set parameter repeat_last_n <int> Set how far back to look for repetitions")
  94. fmt.Fprintln(os.Stderr, " /set parameter num_gpu <int> The number of layers to send to the GPU")
  95. fmt.Fprintln(os.Stderr, " /set parameter stop <string> <string> ... Set the stop parameters")
  96. fmt.Fprintln(os.Stderr, "")
  97. }
  98. scanner, err := readline.New(readline.Prompt{
  99. Prompt: ">>> ",
  100. AltPrompt: "... ",
  101. Placeholder: "Send a message (/? for help)",
  102. AltPlaceholder: `Use """ to end multi-line input`,
  103. })
  104. if err != nil {
  105. return err
  106. }
  107. if envconfig.NoHistory() {
  108. scanner.HistoryDisable()
  109. }
  110. fmt.Print(readline.StartBracketedPaste)
  111. defer fmt.Printf(readline.EndBracketedPaste)
  112. var sb strings.Builder
  113. var multiline MultilineState
  114. for {
  115. line, err := scanner.Readline()
  116. switch {
  117. case errors.Is(err, io.EOF):
  118. fmt.Println()
  119. return nil
  120. case errors.Is(err, readline.ErrInterrupt):
  121. if line == "" {
  122. fmt.Println("\nUse Ctrl + d or /bye to exit.")
  123. }
  124. scanner.Prompt.UseAlt = false
  125. sb.Reset()
  126. continue
  127. case err != nil:
  128. return err
  129. }
  130. switch {
  131. case multiline != MultilineNone:
  132. // check if there's a multiline terminating string
  133. before, ok := strings.CutSuffix(line, `"""`)
  134. sb.WriteString(before)
  135. if !ok {
  136. fmt.Fprintln(&sb)
  137. continue
  138. }
  139. switch multiline {
  140. case MultilineSystem:
  141. opts.System = sb.String()
  142. opts.Messages = append(opts.Messages, api.Message{Role: "system", Content: opts.System})
  143. fmt.Println("Set system message.")
  144. sb.Reset()
  145. }
  146. multiline = MultilineNone
  147. scanner.Prompt.UseAlt = false
  148. case strings.HasPrefix(line, `"""`):
  149. line := strings.TrimPrefix(line, `"""`)
  150. line, ok := strings.CutSuffix(line, `"""`)
  151. sb.WriteString(line)
  152. if !ok {
  153. // no multiline terminating string; need more input
  154. fmt.Fprintln(&sb)
  155. multiline = MultilinePrompt
  156. scanner.Prompt.UseAlt = true
  157. }
  158. case scanner.Pasting:
  159. fmt.Fprintln(&sb, line)
  160. continue
  161. case strings.HasPrefix(line, "/list"):
  162. args := strings.Fields(line)
  163. if err := ListHandler(cmd, args[1:]); err != nil {
  164. return err
  165. }
  166. case strings.HasPrefix(line, "/load"):
  167. args := strings.Fields(line)
  168. if len(args) != 2 {
  169. fmt.Println("Usage:\n /load <modelname>")
  170. continue
  171. }
  172. opts.Model = args[1]
  173. opts.Messages = []api.Message{}
  174. fmt.Printf("Loading model '%s'\n", opts.Model)
  175. if err := loadOrUnloadModel(cmd, &opts); err != nil {
  176. return err
  177. }
  178. continue
  179. case strings.HasPrefix(line, "/save"):
  180. args := strings.Fields(line)
  181. if len(args) != 2 {
  182. fmt.Println("Usage:\n /save <modelname>")
  183. continue
  184. }
  185. client, err := api.ClientFromEnvironment()
  186. if err != nil {
  187. fmt.Println("error: couldn't connect to ollama server")
  188. return err
  189. }
  190. req := NewCreateRequest(args[1], opts)
  191. fn := func(resp api.ProgressResponse) error { return nil }
  192. err = client.Create(cmd.Context(), req, fn)
  193. if err != nil {
  194. if strings.Contains(err.Error(), errtypes.InvalidModelNameErrMsg) {
  195. fmt.Printf("error: The model name '%s' is invalid\n", args[1])
  196. continue
  197. }
  198. return err
  199. }
  200. fmt.Printf("Created new model '%s'\n", args[1])
  201. continue
  202. case strings.HasPrefix(line, "/clear"):
  203. opts.Messages = []api.Message{}
  204. if opts.System != "" {
  205. newMessage := api.Message{Role: "system", Content: opts.System}
  206. opts.Messages = append(opts.Messages, newMessage)
  207. }
  208. fmt.Println("Cleared session context")
  209. continue
  210. case strings.HasPrefix(line, "/set"):
  211. args := strings.Fields(line)
  212. if len(args) > 1 {
  213. switch args[1] {
  214. case "history":
  215. scanner.HistoryEnable()
  216. case "nohistory":
  217. scanner.HistoryDisable()
  218. case "wordwrap":
  219. opts.WordWrap = true
  220. fmt.Println("Set 'wordwrap' mode.")
  221. case "nowordwrap":
  222. opts.WordWrap = false
  223. fmt.Println("Set 'nowordwrap' mode.")
  224. case "verbose":
  225. if err := cmd.Flags().Set("verbose", "true"); err != nil {
  226. return err
  227. }
  228. fmt.Println("Set 'verbose' mode.")
  229. case "quiet":
  230. if err := cmd.Flags().Set("verbose", "false"); err != nil {
  231. return err
  232. }
  233. fmt.Println("Set 'quiet' mode.")
  234. case "format":
  235. if len(args) < 3 || args[2] != "json" {
  236. fmt.Println("Invalid or missing format. For 'json' mode use '/set format json'")
  237. } else {
  238. opts.Format = args[2]
  239. fmt.Printf("Set format to '%s' mode.\n", args[2])
  240. }
  241. case "noformat":
  242. opts.Format = ""
  243. fmt.Println("Disabled format.")
  244. case "parameter":
  245. if len(args) < 4 {
  246. usageParameters()
  247. continue
  248. }
  249. params := args[3:]
  250. fp, err := api.FormatParams(map[string][]string{args[2]: params})
  251. if err != nil {
  252. fmt.Printf("Couldn't set parameter: %q\n", err)
  253. continue
  254. }
  255. fmt.Printf("Set parameter '%s' to '%s'\n", args[2], strings.Join(params, ", "))
  256. opts.Options[args[2]] = fp[args[2]]
  257. case "system":
  258. if len(args) < 3 {
  259. usageSet()
  260. continue
  261. }
  262. multiline = MultilineSystem
  263. line := strings.Join(args[2:], " ")
  264. line, ok := strings.CutPrefix(line, `"""`)
  265. if !ok {
  266. multiline = MultilineNone
  267. } else {
  268. // only cut suffix if the line is multiline
  269. line, ok = strings.CutSuffix(line, `"""`)
  270. if ok {
  271. multiline = MultilineNone
  272. }
  273. }
  274. sb.WriteString(line)
  275. if multiline != MultilineNone {
  276. scanner.Prompt.UseAlt = true
  277. continue
  278. }
  279. opts.System = sb.String() // for display in modelfile
  280. newMessage := api.Message{Role: "system", Content: sb.String()}
  281. // Check if the slice is not empty and the last message is from 'system'
  282. if len(opts.Messages) > 0 && opts.Messages[len(opts.Messages)-1].Role == "system" {
  283. // Replace the last message
  284. opts.Messages[len(opts.Messages)-1] = newMessage
  285. } else {
  286. opts.Messages = append(opts.Messages, newMessage)
  287. }
  288. fmt.Println("Set system message.")
  289. sb.Reset()
  290. continue
  291. default:
  292. fmt.Printf("Unknown command '/set %s'. Type /? for help\n", args[1])
  293. }
  294. } else {
  295. usageSet()
  296. }
  297. case strings.HasPrefix(line, "/show"):
  298. args := strings.Fields(line)
  299. if len(args) > 1 {
  300. client, err := api.ClientFromEnvironment()
  301. if err != nil {
  302. fmt.Println("error: couldn't connect to ollama server")
  303. return err
  304. }
  305. req := &api.ShowRequest{
  306. Name: opts.Model,
  307. System: opts.System,
  308. Options: opts.Options,
  309. }
  310. resp, err := client.Show(cmd.Context(), req)
  311. if err != nil {
  312. fmt.Println("error: couldn't get model")
  313. return err
  314. }
  315. switch args[1] {
  316. case "info":
  317. _ = showInfo(resp, os.Stderr)
  318. case "license":
  319. if resp.License == "" {
  320. fmt.Println("No license was specified for this model.")
  321. } else {
  322. fmt.Println(resp.License)
  323. }
  324. case "modelfile":
  325. fmt.Println(resp.Modelfile)
  326. case "parameters":
  327. if resp.Parameters == "" {
  328. fmt.Println("No parameters were specified for this model.")
  329. } else {
  330. if len(opts.Options) > 0 {
  331. fmt.Println("User defined parameters:")
  332. for k, v := range opts.Options {
  333. fmt.Printf("%-*s %v\n", 30, k, v)
  334. }
  335. fmt.Println()
  336. }
  337. fmt.Println("Model defined parameters:")
  338. fmt.Println(resp.Parameters)
  339. }
  340. case "system":
  341. switch {
  342. case opts.System != "":
  343. fmt.Println(opts.System + "\n")
  344. case resp.System != "":
  345. fmt.Println(resp.System + "\n")
  346. default:
  347. fmt.Println("No system message was specified for this model.")
  348. }
  349. case "template":
  350. if resp.Template != "" {
  351. fmt.Println(resp.Template)
  352. } else {
  353. fmt.Println("No prompt template was specified for this model.")
  354. }
  355. default:
  356. fmt.Printf("Unknown command '/show %s'. Type /? for help\n", args[1])
  357. }
  358. } else {
  359. usageShow()
  360. }
  361. case strings.HasPrefix(line, "/help"), strings.HasPrefix(line, "/?"):
  362. args := strings.Fields(line)
  363. if len(args) > 1 {
  364. switch args[1] {
  365. case "set", "/set":
  366. usageSet()
  367. case "show", "/show":
  368. usageShow()
  369. case "shortcut", "shortcuts":
  370. usageShortcuts()
  371. }
  372. } else {
  373. usage()
  374. }
  375. case strings.HasPrefix(line, "/exit"), strings.HasPrefix(line, "/bye"):
  376. return nil
  377. case strings.HasPrefix(line, "/"):
  378. args := strings.Fields(line)
  379. isFile := false
  380. if opts.MultiModal {
  381. for _, f := range extractFileNames(line) {
  382. if strings.HasPrefix(f, args[0]) {
  383. isFile = true
  384. break
  385. }
  386. }
  387. }
  388. if !isFile {
  389. fmt.Printf("Unknown command '%s'. Type /? for help\n", args[0])
  390. continue
  391. }
  392. sb.WriteString(line)
  393. default:
  394. sb.WriteString(line)
  395. }
  396. if sb.Len() > 0 && multiline == MultilineNone {
  397. newMessage := api.Message{Role: "user", Content: sb.String()}
  398. if opts.MultiModal {
  399. msg, images, err := extractFileData(sb.String())
  400. if err != nil {
  401. return err
  402. }
  403. newMessage.Content = msg
  404. newMessage.Images = images
  405. }
  406. opts.Messages = append(opts.Messages, newMessage)
  407. assistant, err := chat(cmd, opts)
  408. if err != nil {
  409. return err
  410. }
  411. if assistant != nil {
  412. opts.Messages = append(opts.Messages, *assistant)
  413. }
  414. sb.Reset()
  415. }
  416. }
  417. }
  418. func NewCreateRequest(name string, opts runOptions) *api.CreateRequest {
  419. req := &api.CreateRequest{
  420. Name: name,
  421. From: cmp.Or(opts.ParentModel, opts.Model),
  422. }
  423. if opts.System != "" {
  424. req.System = opts.System
  425. }
  426. if len(opts.Options) > 0 {
  427. req.Parameters = opts.Options
  428. }
  429. if len(opts.Messages) > 0 {
  430. req.Messages = opts.Messages
  431. }
  432. return req
  433. }
  434. func normalizeFilePath(fp string) string {
  435. return strings.NewReplacer(
  436. "\\ ", " ", // Escaped space
  437. "\\(", "(", // Escaped left parenthesis
  438. "\\)", ")", // Escaped right parenthesis
  439. "\\[", "[", // Escaped left square bracket
  440. "\\]", "]", // Escaped right square bracket
  441. "\\{", "{", // Escaped left curly brace
  442. "\\}", "}", // Escaped right curly brace
  443. "\\$", "$", // Escaped dollar sign
  444. "\\&", "&", // Escaped ampersand
  445. "\\;", ";", // Escaped semicolon
  446. "\\'", "'", // Escaped single quote
  447. "\\\\", "\\", // Escaped backslash
  448. "\\*", "*", // Escaped asterisk
  449. "\\?", "?", // Escaped question mark
  450. ).Replace(fp)
  451. }
  452. func extractFileNames(input string) []string {
  453. // Regex to match file paths starting with optional drive letter, / ./ \ or .\ and include escaped or unescaped spaces (\ or %20)
  454. // and followed by more characters and a file extension
  455. // This will capture non filename strings, but we'll check for file existence to remove mismatches
  456. regexPattern := `(?:[a-zA-Z]:)?(?:\./|/|\\)[\S\\ ]+?\.(?i:jpg|jpeg|png)\b`
  457. re := regexp.MustCompile(regexPattern)
  458. return re.FindAllString(input, -1)
  459. }
  460. func extractFileData(input string) (string, []api.ImageData, error) {
  461. filePaths := extractFileNames(input)
  462. var imgs []api.ImageData
  463. for _, fp := range filePaths {
  464. nfp := normalizeFilePath(fp)
  465. data, err := getImageData(nfp)
  466. if errors.Is(err, os.ErrNotExist) {
  467. continue
  468. } else if err != nil {
  469. fmt.Fprintf(os.Stderr, "Couldn't process image: %q\n", err)
  470. return "", imgs, err
  471. }
  472. fmt.Fprintf(os.Stderr, "Added image '%s'\n", nfp)
  473. input = strings.ReplaceAll(input, fp, "")
  474. imgs = append(imgs, data)
  475. }
  476. return strings.TrimSpace(input), imgs, nil
  477. }
  478. func getImageData(filePath string) ([]byte, error) {
  479. file, err := os.Open(filePath)
  480. if err != nil {
  481. return nil, err
  482. }
  483. defer file.Close()
  484. buf := make([]byte, 512)
  485. _, err = file.Read(buf)
  486. if err != nil {
  487. return nil, err
  488. }
  489. contentType := http.DetectContentType(buf)
  490. allowedTypes := []string{"image/jpeg", "image/jpg", "image/png"}
  491. if !slices.Contains(allowedTypes, contentType) {
  492. return nil, fmt.Errorf("invalid image type: %s", contentType)
  493. }
  494. info, err := file.Stat()
  495. if err != nil {
  496. return nil, err
  497. }
  498. // Check if the file size exceeds 100MB
  499. var maxSize int64 = 100 * 1024 * 1024 // 100MB in bytes
  500. if info.Size() > maxSize {
  501. return nil, errors.New("file size exceeds maximum limit (100MB)")
  502. }
  503. buf = make([]byte, info.Size())
  504. _, err = file.Seek(0, 0)
  505. if err != nil {
  506. return nil, err
  507. }
  508. _, err = io.ReadFull(file, buf)
  509. if err != nil {
  510. return nil, err
  511. }
  512. return buf, nil
  513. }