cmd.go 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991
  1. package cmd
  2. import (
  3. "bufio"
  4. "context"
  5. "crypto/ed25519"
  6. "crypto/rand"
  7. "encoding/pem"
  8. "errors"
  9. "fmt"
  10. "io"
  11. "log"
  12. "net"
  13. "os"
  14. "os/exec"
  15. "os/signal"
  16. "path/filepath"
  17. "runtime"
  18. "strings"
  19. "syscall"
  20. "time"
  21. "github.com/dustin/go-humanize"
  22. "github.com/olekukonko/tablewriter"
  23. "github.com/pdevine/readline"
  24. "github.com/spf13/cobra"
  25. "golang.org/x/crypto/ssh"
  26. "golang.org/x/term"
  27. "github.com/jmorganca/ollama/api"
  28. "github.com/jmorganca/ollama/format"
  29. "github.com/jmorganca/ollama/progressbar"
  30. "github.com/jmorganca/ollama/server"
  31. "github.com/jmorganca/ollama/version"
  32. )
  33. type Painter struct {
  34. IsMultiLine bool
  35. }
  36. func (p Painter) Paint(line []rune, _ int) []rune {
  37. termType := os.Getenv("TERM")
  38. if termType == "xterm-256color" && len(line) == 0 {
  39. var prompt string
  40. if p.IsMultiLine {
  41. prompt = "Use \"\"\" to end multi-line input"
  42. } else {
  43. prompt = "Send a message (/? for help)"
  44. }
  45. return []rune(fmt.Sprintf("\033[38;5;245m%s\033[%dD\033[0m", prompt, len(prompt)))
  46. }
  47. // add a space and a backspace to prevent the cursor from walking up the screen
  48. line = append(line, []rune(" \b")...)
  49. return line
  50. }
  51. func CreateHandler(cmd *cobra.Command, args []string) error {
  52. filename, _ := cmd.Flags().GetString("file")
  53. filename, err := filepath.Abs(filename)
  54. if err != nil {
  55. return err
  56. }
  57. client, err := api.FromEnv()
  58. if err != nil {
  59. return err
  60. }
  61. var spinner *Spinner
  62. var currentDigest string
  63. var bar *progressbar.ProgressBar
  64. request := api.CreateRequest{Name: args[0], Path: filename}
  65. fn := func(resp api.ProgressResponse) error {
  66. if resp.Digest != currentDigest && resp.Digest != "" {
  67. if spinner != nil {
  68. spinner.Stop()
  69. }
  70. currentDigest = resp.Digest
  71. switch {
  72. case strings.Contains(resp.Status, "embeddings"):
  73. bar = progressbar.Default(resp.Total, resp.Status)
  74. bar.Set64(resp.Completed)
  75. default:
  76. // pulling
  77. bar = progressbar.DefaultBytes(
  78. resp.Total,
  79. resp.Status,
  80. )
  81. bar.Set64(resp.Completed)
  82. }
  83. } else if resp.Digest == currentDigest && resp.Digest != "" {
  84. bar.Set64(resp.Completed)
  85. } else {
  86. currentDigest = ""
  87. if spinner != nil {
  88. spinner.Stop()
  89. }
  90. spinner = NewSpinner(resp.Status)
  91. go spinner.Spin(100 * time.Millisecond)
  92. }
  93. return nil
  94. }
  95. if err := client.Create(context.Background(), &request, fn); err != nil {
  96. return err
  97. }
  98. if spinner != nil {
  99. spinner.Stop()
  100. if spinner.description != "success" {
  101. return errors.New("unexpected end to create model")
  102. }
  103. }
  104. return nil
  105. }
  106. func RunHandler(cmd *cobra.Command, args []string) error {
  107. client, err := api.FromEnv()
  108. if err != nil {
  109. return err
  110. }
  111. models, err := client.List(context.Background())
  112. if err != nil {
  113. return err
  114. }
  115. canonicalModelPath := server.ParseModelPath(args[0])
  116. for _, model := range models.Models {
  117. if model.Name == canonicalModelPath.GetShortTagname() {
  118. return RunGenerate(cmd, args)
  119. }
  120. }
  121. if err := PullHandler(cmd, args); err != nil {
  122. return err
  123. }
  124. return RunGenerate(cmd, args)
  125. }
  126. func PushHandler(cmd *cobra.Command, args []string) error {
  127. client, err := api.FromEnv()
  128. if err != nil {
  129. return err
  130. }
  131. insecure, err := cmd.Flags().GetBool("insecure")
  132. if err != nil {
  133. return err
  134. }
  135. var currentDigest string
  136. var bar *progressbar.ProgressBar
  137. request := api.PushRequest{Name: args[0], Insecure: insecure}
  138. fn := func(resp api.ProgressResponse) error {
  139. if resp.Digest != currentDigest && resp.Digest != "" {
  140. currentDigest = resp.Digest
  141. bar = progressbar.DefaultBytes(
  142. resp.Total,
  143. fmt.Sprintf("pushing %s...", resp.Digest[7:19]),
  144. )
  145. bar.Set64(resp.Completed)
  146. } else if resp.Digest == currentDigest && resp.Digest != "" {
  147. bar.Set64(resp.Completed)
  148. } else {
  149. currentDigest = ""
  150. fmt.Println(resp.Status)
  151. }
  152. return nil
  153. }
  154. if err := client.Push(context.Background(), &request, fn); err != nil {
  155. return err
  156. }
  157. if bar != nil && !bar.IsFinished() {
  158. return errors.New("unexpected end to push model")
  159. }
  160. return nil
  161. }
  162. func ListHandler(cmd *cobra.Command, args []string) error {
  163. client, err := api.FromEnv()
  164. if err != nil {
  165. return err
  166. }
  167. models, err := client.List(context.Background())
  168. if err != nil {
  169. return err
  170. }
  171. var data [][]string
  172. for _, m := range models.Models {
  173. if len(args) == 0 || strings.HasPrefix(m.Name, args[0]) {
  174. data = append(data, []string{m.Name, m.Digest[:12], humanize.Bytes(uint64(m.Size)), format.HumanTime(m.ModifiedAt, "Never")})
  175. }
  176. }
  177. table := tablewriter.NewWriter(os.Stdout)
  178. table.SetHeader([]string{"NAME", "ID", "SIZE", "MODIFIED"})
  179. table.SetHeaderAlignment(tablewriter.ALIGN_LEFT)
  180. table.SetAlignment(tablewriter.ALIGN_LEFT)
  181. table.SetHeaderLine(false)
  182. table.SetBorder(false)
  183. table.SetNoWhiteSpace(true)
  184. table.SetTablePadding("\t")
  185. table.AppendBulk(data)
  186. table.Render()
  187. return nil
  188. }
  189. func DeleteHandler(cmd *cobra.Command, args []string) error {
  190. client, err := api.FromEnv()
  191. if err != nil {
  192. return err
  193. }
  194. for _, name := range args {
  195. req := api.DeleteRequest{Name: name}
  196. if err := client.Delete(context.Background(), &req); err != nil {
  197. return err
  198. }
  199. fmt.Printf("deleted '%s'\n", name)
  200. }
  201. return nil
  202. }
  203. func ShowHandler(cmd *cobra.Command, args []string) error {
  204. client, err := api.FromEnv()
  205. if err != nil {
  206. return err
  207. }
  208. if len(args) != 1 {
  209. return errors.New("missing model name")
  210. }
  211. license, errLicense := cmd.Flags().GetBool("license")
  212. modelfile, errModelfile := cmd.Flags().GetBool("modelfile")
  213. parameters, errParams := cmd.Flags().GetBool("parameters")
  214. system, errSystem := cmd.Flags().GetBool("system")
  215. template, errTemplate := cmd.Flags().GetBool("template")
  216. for _, boolErr := range []error{errLicense, errModelfile, errParams, errSystem, errTemplate} {
  217. if boolErr != nil {
  218. return errors.New("error retrieving flags")
  219. }
  220. }
  221. flagsSet := 0
  222. showType := ""
  223. if license {
  224. flagsSet++
  225. showType = "license"
  226. }
  227. if modelfile {
  228. flagsSet++
  229. showType = "modelfile"
  230. }
  231. if parameters {
  232. flagsSet++
  233. showType = "parameters"
  234. }
  235. if system {
  236. flagsSet++
  237. showType = "system"
  238. }
  239. if template {
  240. flagsSet++
  241. showType = "template"
  242. }
  243. if flagsSet > 1 {
  244. return errors.New("only one of '--license', '--modelfile', '--parameters', '--system', or '--template' can be specified")
  245. } else if flagsSet == 0 {
  246. return errors.New("one of '--license', '--modelfile', '--parameters', '--system', or '--template' must be specified")
  247. }
  248. req := api.ShowRequest{Name: args[0]}
  249. resp, err := client.Show(context.Background(), &req)
  250. if err != nil {
  251. return err
  252. }
  253. switch showType {
  254. case "license":
  255. fmt.Println(resp.License)
  256. case "modelfile":
  257. fmt.Println(resp.Modelfile)
  258. case "parameters":
  259. fmt.Println(resp.Parameters)
  260. case "system":
  261. fmt.Println(resp.System)
  262. case "template":
  263. fmt.Println(resp.Template)
  264. }
  265. return nil
  266. }
  267. func CopyHandler(cmd *cobra.Command, args []string) error {
  268. client, err := api.FromEnv()
  269. if err != nil {
  270. return err
  271. }
  272. req := api.CopyRequest{Source: args[0], Destination: args[1]}
  273. if err := client.Copy(context.Background(), &req); err != nil {
  274. return err
  275. }
  276. fmt.Printf("copied '%s' to '%s'\n", args[0], args[1])
  277. return nil
  278. }
  279. func PullHandler(cmd *cobra.Command, args []string) error {
  280. insecure, err := cmd.Flags().GetBool("insecure")
  281. if err != nil {
  282. return err
  283. }
  284. return pull(args[0], insecure)
  285. }
  286. func pull(model string, insecure bool) error {
  287. client, err := api.FromEnv()
  288. if err != nil {
  289. return err
  290. }
  291. var currentDigest string
  292. var bar *progressbar.ProgressBar
  293. request := api.PullRequest{Name: model, Insecure: insecure}
  294. fn := func(resp api.ProgressResponse) error {
  295. if resp.Digest != currentDigest && resp.Digest != "" {
  296. currentDigest = resp.Digest
  297. bar = progressbar.DefaultBytes(
  298. resp.Total,
  299. fmt.Sprintf("pulling %s...", resp.Digest[7:19]),
  300. )
  301. bar.Set64(resp.Completed)
  302. } else if resp.Digest == currentDigest && resp.Digest != "" {
  303. bar.Set64(resp.Completed)
  304. } else {
  305. currentDigest = ""
  306. fmt.Println(resp.Status)
  307. }
  308. return nil
  309. }
  310. if err := client.Pull(context.Background(), &request, fn); err != nil {
  311. return err
  312. }
  313. if bar != nil && !bar.IsFinished() {
  314. return errors.New("unexpected end to pull model")
  315. }
  316. return nil
  317. }
  318. func RunGenerate(cmd *cobra.Command, args []string) error {
  319. if len(args) > 1 {
  320. // join all args into a single prompt
  321. wordWrap := false
  322. if term.IsTerminal(int(os.Stdout.Fd())) {
  323. wordWrap = true
  324. }
  325. nowrap, err := cmd.Flags().GetBool("nowordwrap")
  326. if err != nil {
  327. return err
  328. }
  329. if nowrap {
  330. wordWrap = false
  331. }
  332. return generate(cmd, args[0], strings.Join(args[1:], " "), wordWrap)
  333. }
  334. if readline.IsTerminal(int(os.Stdin.Fd())) {
  335. return generateInteractive(cmd, args[0])
  336. }
  337. return generateBatch(cmd, args[0])
  338. }
  339. type generateContextKey string
  340. func generate(cmd *cobra.Command, model, prompt string, wordWrap bool) error {
  341. client, err := api.FromEnv()
  342. if err != nil {
  343. return err
  344. }
  345. spinner := NewSpinner("")
  346. go spinner.Spin(60 * time.Millisecond)
  347. var latest api.GenerateResponse
  348. generateContext, ok := cmd.Context().Value(generateContextKey("context")).([]int)
  349. if !ok {
  350. generateContext = []int{}
  351. }
  352. termWidth, _, err := term.GetSize(int(0))
  353. if err != nil {
  354. wordWrap = false
  355. }
  356. cancelCtx, cancel := context.WithCancel(context.Background())
  357. defer cancel()
  358. sigChan := make(chan os.Signal, 1)
  359. signal.Notify(sigChan, syscall.SIGINT)
  360. var abort bool
  361. go func() {
  362. <-sigChan
  363. cancel()
  364. abort = true
  365. }()
  366. var currentLineLength int
  367. var wordBuffer string
  368. request := api.GenerateRequest{Model: model, Prompt: prompt, Context: generateContext}
  369. fn := func(response api.GenerateResponse) error {
  370. if !spinner.IsFinished() {
  371. spinner.Finish()
  372. }
  373. latest = response
  374. if wordWrap {
  375. for _, ch := range response.Response {
  376. if currentLineLength+1 > termWidth-5 {
  377. // backtrack the length of the last word and clear to the end of the line
  378. fmt.Printf("\x1b[%dD\x1b[K\n", len(wordBuffer))
  379. fmt.Printf("%s%c", wordBuffer, ch)
  380. currentLineLength = len(wordBuffer) + 1
  381. } else {
  382. fmt.Print(string(ch))
  383. currentLineLength += 1
  384. switch ch {
  385. case ' ':
  386. wordBuffer = ""
  387. case '\n':
  388. currentLineLength = 0
  389. default:
  390. wordBuffer += string(ch)
  391. }
  392. }
  393. }
  394. } else {
  395. fmt.Print(response.Response)
  396. }
  397. return nil
  398. }
  399. if err := client.Generate(cancelCtx, &request, fn); err != nil {
  400. if strings.Contains(err.Error(), "failed to load model") {
  401. // tell the user to check the server log, if it exists locally
  402. home, nestedErr := os.UserHomeDir()
  403. if nestedErr != nil {
  404. // return the original error
  405. return err
  406. }
  407. logPath := filepath.Join(home, ".ollama", "logs", "server.log")
  408. if _, nestedErr := os.Stat(logPath); nestedErr == nil {
  409. err = fmt.Errorf("%w\nFor more details, check the error logs at %s", err, logPath)
  410. }
  411. } else if strings.Contains(err.Error(), "context canceled") && abort {
  412. spinner.Finish()
  413. return nil
  414. }
  415. return err
  416. }
  417. if prompt != "" {
  418. fmt.Println()
  419. fmt.Println()
  420. }
  421. if !latest.Done {
  422. if abort {
  423. return nil
  424. }
  425. return errors.New("unexpected end of response")
  426. }
  427. verbose, err := cmd.Flags().GetBool("verbose")
  428. if err != nil {
  429. return err
  430. }
  431. if verbose {
  432. latest.Summary()
  433. }
  434. ctx := cmd.Context()
  435. ctx = context.WithValue(ctx, generateContextKey("context"), latest.Context)
  436. cmd.SetContext(ctx)
  437. return nil
  438. }
  439. func generateInteractive(cmd *cobra.Command, model string) error {
  440. home, err := os.UserHomeDir()
  441. if err != nil {
  442. return err
  443. }
  444. // load the model
  445. if err := generate(cmd, model, "", false); err != nil {
  446. return err
  447. }
  448. completer := readline.NewPrefixCompleter(
  449. readline.PcItem("/help"),
  450. readline.PcItem("/list"),
  451. readline.PcItem("/set",
  452. readline.PcItem("history"),
  453. readline.PcItem("nohistory"),
  454. readline.PcItem("wordwrap"),
  455. readline.PcItem("nowordwrap"),
  456. readline.PcItem("verbose"),
  457. readline.PcItem("quiet"),
  458. ),
  459. readline.PcItem("/show",
  460. readline.PcItem("license"),
  461. readline.PcItem("modelfile"),
  462. readline.PcItem("parameters"),
  463. readline.PcItem("system"),
  464. readline.PcItem("template"),
  465. ),
  466. readline.PcItem("/exit"),
  467. readline.PcItem("/bye"),
  468. )
  469. usage := func() {
  470. fmt.Fprintln(os.Stderr, "commands:")
  471. fmt.Fprintln(os.Stderr, completer.Tree(" "))
  472. }
  473. var painter Painter
  474. config := readline.Config{
  475. Painter: &painter,
  476. Prompt: ">>> ",
  477. HistoryFile: filepath.Join(home, ".ollama", "history"),
  478. AutoComplete: completer,
  479. }
  480. scanner, err := readline.NewEx(&config)
  481. if err != nil {
  482. return err
  483. }
  484. defer scanner.Close()
  485. var wordWrap bool
  486. termType := os.Getenv("TERM")
  487. if termType == "xterm-256color" {
  488. wordWrap = true
  489. }
  490. // override wrapping if the user turned it off
  491. nowrap, err := cmd.Flags().GetBool("nowordwrap")
  492. if err != nil {
  493. return err
  494. }
  495. if nowrap {
  496. wordWrap = false
  497. }
  498. var multiLineBuffer string
  499. var isMultiLine bool
  500. for {
  501. line, err := scanner.Readline()
  502. switch {
  503. case errors.Is(err, io.EOF):
  504. return nil
  505. case errors.Is(err, readline.ErrInterrupt):
  506. if line == "" {
  507. fmt.Println("Use Ctrl-D or /bye to exit.")
  508. }
  509. continue
  510. case err != nil:
  511. return err
  512. }
  513. line = strings.TrimSpace(line)
  514. switch {
  515. case isMultiLine:
  516. if strings.HasSuffix(line, `"""`) {
  517. isMultiLine = false
  518. painter.IsMultiLine = isMultiLine
  519. multiLineBuffer += strings.TrimSuffix(line, `"""`)
  520. line = multiLineBuffer
  521. multiLineBuffer = ""
  522. scanner.SetPrompt(">>> ")
  523. } else {
  524. multiLineBuffer += line + " "
  525. continue
  526. }
  527. case strings.HasPrefix(line, `"""`):
  528. isMultiLine = true
  529. painter.IsMultiLine = isMultiLine
  530. multiLineBuffer = strings.TrimPrefix(line, `"""`) + " "
  531. scanner.SetPrompt("... ")
  532. continue
  533. case strings.HasPrefix(line, "/list"):
  534. args := strings.Fields(line)
  535. if err := ListHandler(cmd, args[1:]); err != nil {
  536. return err
  537. }
  538. case strings.HasPrefix(line, "/set"):
  539. args := strings.Fields(line)
  540. if len(args) > 1 {
  541. switch args[1] {
  542. case "history":
  543. scanner.HistoryEnable()
  544. case "nohistory":
  545. scanner.HistoryDisable()
  546. case "wordwrap":
  547. wordWrap = true
  548. fmt.Println("Set 'wordwrap' mode.")
  549. case "nowordwrap":
  550. wordWrap = false
  551. fmt.Println("Set 'nowordwrap' mode.")
  552. case "verbose":
  553. cmd.Flags().Set("verbose", "true")
  554. fmt.Println("Set 'verbose' mode.")
  555. case "quiet":
  556. cmd.Flags().Set("verbose", "false")
  557. fmt.Println("Set 'quiet' mode.")
  558. case "mode":
  559. if len(args) > 2 {
  560. switch args[2] {
  561. case "vim":
  562. scanner.SetVimMode(true)
  563. case "emacs", "default":
  564. scanner.SetVimMode(false)
  565. default:
  566. usage()
  567. }
  568. } else {
  569. usage()
  570. }
  571. default:
  572. fmt.Printf("Unknown command '/set %s'. Type /? for help\n", args[1])
  573. }
  574. } else {
  575. usage()
  576. }
  577. case strings.HasPrefix(line, "/show"):
  578. args := strings.Fields(line)
  579. if len(args) > 1 {
  580. resp, err := server.GetModelInfo(model)
  581. if err != nil {
  582. fmt.Println("error: couldn't get model")
  583. return err
  584. }
  585. switch args[1] {
  586. case "license":
  587. fmt.Println(resp.License)
  588. case "modelfile":
  589. fmt.Println(resp.Modelfile)
  590. case "parameters":
  591. fmt.Println(resp.Parameters)
  592. case "system":
  593. fmt.Println(resp.System)
  594. case "template":
  595. fmt.Println(resp.Template)
  596. default:
  597. fmt.Printf("Unknown command '/show %s'. Type /? for help\n", args[1])
  598. }
  599. } else {
  600. usage()
  601. }
  602. case line == "/help", line == "/?":
  603. usage()
  604. case line == "/exit", line == "/bye":
  605. return nil
  606. case strings.HasPrefix(line, "/"):
  607. args := strings.Fields(line)
  608. fmt.Printf("Unknown command '%s'. Type /? for help\n", args[0])
  609. }
  610. if len(line) > 0 && line[0] != '/' {
  611. if err := generate(cmd, model, line, wordWrap); err != nil {
  612. return err
  613. }
  614. }
  615. }
  616. }
  617. func generateBatch(cmd *cobra.Command, model string) error {
  618. scanner := bufio.NewScanner(os.Stdin)
  619. for scanner.Scan() {
  620. prompt := scanner.Text()
  621. fmt.Printf(">>> %s\n", prompt)
  622. if err := generate(cmd, model, prompt, false); err != nil {
  623. return err
  624. }
  625. }
  626. return nil
  627. }
  628. func RunServer(cmd *cobra.Command, _ []string) error {
  629. host, port, err := net.SplitHostPort(os.Getenv("OLLAMA_HOST"))
  630. if err != nil {
  631. host, port = "127.0.0.1", "11434"
  632. if ip := net.ParseIP(strings.Trim(os.Getenv("OLLAMA_HOST"), "[]")); ip != nil {
  633. host = ip.String()
  634. }
  635. }
  636. if err := initializeKeypair(); err != nil {
  637. return err
  638. }
  639. ln, err := net.Listen("tcp", net.JoinHostPort(host, port))
  640. if err != nil {
  641. return err
  642. }
  643. var origins []string
  644. if o := os.Getenv("OLLAMA_ORIGINS"); o != "" {
  645. origins = strings.Split(o, ",")
  646. }
  647. if noprune := os.Getenv("OLLAMA_NOPRUNE"); noprune == "" {
  648. if err := server.PruneLayers(); err != nil {
  649. return err
  650. }
  651. manifestsPath, err := server.GetManifestPath()
  652. if err != nil {
  653. return err
  654. }
  655. if err := server.PruneDirectory(manifestsPath); err != nil {
  656. return err
  657. }
  658. }
  659. return server.Serve(ln, origins)
  660. }
  661. func initializeKeypair() error {
  662. home, err := os.UserHomeDir()
  663. if err != nil {
  664. return err
  665. }
  666. privKeyPath := filepath.Join(home, ".ollama", "id_ed25519")
  667. pubKeyPath := filepath.Join(home, ".ollama", "id_ed25519.pub")
  668. _, err = os.Stat(privKeyPath)
  669. if os.IsNotExist(err) {
  670. fmt.Printf("Couldn't find '%s'. Generating new private key.\n", privKeyPath)
  671. _, privKey, err := ed25519.GenerateKey(rand.Reader)
  672. if err != nil {
  673. return err
  674. }
  675. privKeyBytes, err := format.OpenSSHPrivateKey(privKey, "")
  676. if err != nil {
  677. return err
  678. }
  679. err = os.MkdirAll(filepath.Dir(privKeyPath), 0o755)
  680. if err != nil {
  681. return fmt.Errorf("could not create directory %w", err)
  682. }
  683. err = os.WriteFile(privKeyPath, pem.EncodeToMemory(privKeyBytes), 0o600)
  684. if err != nil {
  685. return err
  686. }
  687. sshPrivateKey, err := ssh.NewSignerFromKey(privKey)
  688. if err != nil {
  689. return err
  690. }
  691. pubKeyData := ssh.MarshalAuthorizedKey(sshPrivateKey.PublicKey())
  692. err = os.WriteFile(pubKeyPath, pubKeyData, 0o644)
  693. if err != nil {
  694. return err
  695. }
  696. fmt.Printf("Your new public key is: \n\n%s\n", string(pubKeyData))
  697. }
  698. return nil
  699. }
  700. func startMacApp(client *api.Client) error {
  701. exe, err := os.Executable()
  702. if err != nil {
  703. return err
  704. }
  705. link, err := os.Readlink(exe)
  706. if err != nil {
  707. return err
  708. }
  709. if !strings.Contains(link, "Ollama.app") {
  710. return fmt.Errorf("could not find ollama app")
  711. }
  712. path := strings.Split(link, "Ollama.app")
  713. if err := exec.Command("/usr/bin/open", "-a", path[0]+"Ollama.app").Run(); err != nil {
  714. return err
  715. }
  716. // wait for the server to start
  717. timeout := time.After(5 * time.Second)
  718. tick := time.Tick(500 * time.Millisecond)
  719. for {
  720. select {
  721. case <-timeout:
  722. return errors.New("timed out waiting for server to start")
  723. case <-tick:
  724. if err := client.Heartbeat(context.Background()); err == nil {
  725. return nil // server has started
  726. }
  727. }
  728. }
  729. }
  730. func checkServerHeartbeat(_ *cobra.Command, _ []string) error {
  731. client, err := api.FromEnv()
  732. if err != nil {
  733. return err
  734. }
  735. if err := client.Heartbeat(context.Background()); err != nil {
  736. if !strings.Contains(err.Error(), "connection refused") {
  737. return err
  738. }
  739. if runtime.GOOS == "darwin" {
  740. if err := startMacApp(client); err != nil {
  741. return fmt.Errorf("could not connect to ollama app, is it running?")
  742. }
  743. } else {
  744. return fmt.Errorf("could not connect to ollama server, run 'ollama serve' to start it")
  745. }
  746. }
  747. return nil
  748. }
  749. func NewCLI() *cobra.Command {
  750. log.SetFlags(log.LstdFlags | log.Lshortfile)
  751. rootCmd := &cobra.Command{
  752. Use: "ollama",
  753. Short: "Large language model runner",
  754. SilenceUsage: true,
  755. SilenceErrors: true,
  756. CompletionOptions: cobra.CompletionOptions{
  757. DisableDefaultCmd: true,
  758. },
  759. Version: version.Version,
  760. }
  761. cobra.EnableCommandSorting = false
  762. createCmd := &cobra.Command{
  763. Use: "create MODEL",
  764. Short: "Create a model from a Modelfile",
  765. Args: cobra.MinimumNArgs(1),
  766. PreRunE: checkServerHeartbeat,
  767. RunE: CreateHandler,
  768. }
  769. createCmd.Flags().StringP("file", "f", "Modelfile", "Name of the Modelfile (default \"Modelfile\")")
  770. showCmd := &cobra.Command{
  771. Use: "show MODEL",
  772. Short: "Show information for a model",
  773. Args: cobra.MinimumNArgs(1),
  774. PreRunE: checkServerHeartbeat,
  775. RunE: ShowHandler,
  776. }
  777. showCmd.Flags().Bool("license", false, "Show license of a model")
  778. showCmd.Flags().Bool("modelfile", false, "Show Modelfile of a model")
  779. showCmd.Flags().Bool("parameters", false, "Show parameters of a model")
  780. showCmd.Flags().Bool("template", false, "Show template of a model")
  781. showCmd.Flags().Bool("system", false, "Show system prompt of a model")
  782. runCmd := &cobra.Command{
  783. Use: "run MODEL [PROMPT]",
  784. Short: "Run a model",
  785. Args: cobra.MinimumNArgs(1),
  786. PreRunE: checkServerHeartbeat,
  787. RunE: RunHandler,
  788. }
  789. runCmd.Flags().Bool("verbose", false, "Show timings for response")
  790. runCmd.Flags().Bool("insecure", false, "Use an insecure registry")
  791. runCmd.Flags().Bool("nowordwrap", false, "Don't wrap words to the next line automatically")
  792. serveCmd := &cobra.Command{
  793. Use: "serve",
  794. Aliases: []string{"start"},
  795. Short: "Start ollama",
  796. RunE: RunServer,
  797. }
  798. pullCmd := &cobra.Command{
  799. Use: "pull MODEL",
  800. Short: "Pull a model from a registry",
  801. Args: cobra.MinimumNArgs(1),
  802. PreRunE: checkServerHeartbeat,
  803. RunE: PullHandler,
  804. }
  805. pullCmd.Flags().Bool("insecure", false, "Use an insecure registry")
  806. pushCmd := &cobra.Command{
  807. Use: "push MODEL",
  808. Short: "Push a model to a registry",
  809. Args: cobra.MinimumNArgs(1),
  810. PreRunE: checkServerHeartbeat,
  811. RunE: PushHandler,
  812. }
  813. pushCmd.Flags().Bool("insecure", false, "Use an insecure registry")
  814. listCmd := &cobra.Command{
  815. Use: "list",
  816. Aliases: []string{"ls"},
  817. Short: "List models",
  818. PreRunE: checkServerHeartbeat,
  819. RunE: ListHandler,
  820. }
  821. copyCmd := &cobra.Command{
  822. Use: "cp",
  823. Short: "Copy a model",
  824. Args: cobra.MinimumNArgs(2),
  825. PreRunE: checkServerHeartbeat,
  826. RunE: CopyHandler,
  827. }
  828. deleteCmd := &cobra.Command{
  829. Use: "rm",
  830. Short: "Remove a model",
  831. Args: cobra.MinimumNArgs(1),
  832. PreRunE: checkServerHeartbeat,
  833. RunE: DeleteHandler,
  834. }
  835. rootCmd.AddCommand(
  836. serveCmd,
  837. createCmd,
  838. showCmd,
  839. runCmd,
  840. pullCmd,
  841. pushCmd,
  842. listCmd,
  843. copyCmd,
  844. deleteCmd,
  845. )
  846. return rootCmd
  847. }