cmd.go 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. package cmd
  2. import (
  3. "context"
  4. "fmt"
  5. "log"
  6. "net"
  7. "os"
  8. "path"
  9. "sync"
  10. "github.com/gosuri/uiprogress"
  11. "github.com/jmorganca/ollama/api"
  12. "github.com/jmorganca/ollama/server"
  13. "github.com/spf13/cobra"
  14. )
  15. func cacheDir() string {
  16. home, err := os.UserHomeDir()
  17. if err != nil {
  18. panic(err)
  19. }
  20. return path.Join(home, ".ollama")
  21. }
  22. func bytesToGB(bytes int) float64 {
  23. return float64(bytes) / float64(1<<30)
  24. }
  25. func run(model string) error {
  26. client, err := NewAPIClient()
  27. if err != nil {
  28. return err
  29. }
  30. pr := api.PullRequest{
  31. Model: model,
  32. }
  33. var bar *uiprogress.Bar
  34. mutex := &sync.Mutex{}
  35. var progressData api.PullProgress
  36. callback := func(progress api.PullProgress) {
  37. mutex.Lock()
  38. progressData = progress
  39. if bar == nil {
  40. uiprogress.Start() // start rendering
  41. bar = uiprogress.AddBar(int(progress.Total)) // Add a new bar
  42. // display the total file size and how much has downloaded so far
  43. bar.PrependFunc(func(b *uiprogress.Bar) string {
  44. return fmt.Sprintf("Downloading: %.2f GB / %.2f GB", bytesToGB(progressData.Completed), bytesToGB(progressData.Total))
  45. })
  46. // display completion percentage
  47. bar.AppendFunc(func(b *uiprogress.Bar) string {
  48. return fmt.Sprintf(" %d%%", int((float64(progressData.Completed)/float64(progressData.Total))*100))
  49. })
  50. }
  51. bar.Set(int(progress.Completed))
  52. mutex.Unlock()
  53. }
  54. _, err = client.Pull(context.Background(), &pr, callback)
  55. return err
  56. }
  57. func serve() error {
  58. ln, err := net.Listen("tcp", "127.0.0.1:11434")
  59. if err != nil {
  60. return err
  61. }
  62. return server.Serve(ln)
  63. }
  64. func NewAPIClient() (*api.Client, error) {
  65. return &api.Client{
  66. URL: "http://localhost:11434",
  67. }, nil
  68. }
  69. func NewCLI() *cobra.Command {
  70. log.SetFlags(log.LstdFlags | log.Lshortfile)
  71. rootCmd := &cobra.Command{
  72. Use: "ollama",
  73. Short: "Large language model runner",
  74. CompletionOptions: cobra.CompletionOptions{
  75. DisableDefaultCmd: true,
  76. },
  77. PersistentPreRun: func(cmd *cobra.Command, args []string) {
  78. // Disable usage printing on errors
  79. cmd.SilenceUsage = true
  80. // create the models directory and it's parent
  81. if err := os.MkdirAll(path.Join(cacheDir(), "models"), 0o700); err != nil {
  82. panic(err)
  83. }
  84. },
  85. }
  86. cobra.EnableCommandSorting = false
  87. runCmd := &cobra.Command{
  88. Use: "run MODEL",
  89. Short: "Run a model",
  90. Args: cobra.ExactArgs(1),
  91. RunE: func(cmd *cobra.Command, args []string) error {
  92. return run(args[0])
  93. },
  94. }
  95. serveCmd := &cobra.Command{
  96. Use: "serve",
  97. Aliases: []string{"start"},
  98. Short: "Start ollama",
  99. RunE: func(cmd *cobra.Command, args []string) error {
  100. return serve()
  101. },
  102. }
  103. rootCmd.AddCommand(
  104. serveCmd,
  105. runCmd,
  106. )
  107. return rootCmd
  108. }