cmd.go 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  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. pullCallback := func(progress api.PullProgress) {
  37. mutex.Lock()
  38. progressData = progress
  39. if bar == nil {
  40. uiprogress.Start()
  41. bar = uiprogress.AddBar(int(progress.Total))
  42. bar.PrependFunc(func(b *uiprogress.Bar) string {
  43. return fmt.Sprintf("Downloading: %.2f GB / %.2f GB", bytesToGB(progressData.Completed), bytesToGB(progressData.Total))
  44. })
  45. bar.AppendFunc(func(b *uiprogress.Bar) string {
  46. return fmt.Sprintf(" %d%%", int((float64(progressData.Completed)/float64(progressData.Total))*100))
  47. })
  48. }
  49. bar.Set(int(progress.Completed))
  50. mutex.Unlock()
  51. }
  52. if err := client.Pull(context.Background(), &pr, pullCallback); err != nil {
  53. return err
  54. }
  55. fmt.Println("Up to date.")
  56. return nil
  57. }
  58. func serve() error {
  59. ln, err := net.Listen("tcp", "127.0.0.1:11434")
  60. if err != nil {
  61. return err
  62. }
  63. return server.Serve(ln)
  64. }
  65. func NewAPIClient() (*api.Client, error) {
  66. return &api.Client{
  67. URL: "http://localhost:11434",
  68. }, nil
  69. }
  70. func NewCLI() *cobra.Command {
  71. log.SetFlags(log.LstdFlags | log.Lshortfile)
  72. rootCmd := &cobra.Command{
  73. Use: "ollama",
  74. Short: "Large language model runner",
  75. CompletionOptions: cobra.CompletionOptions{
  76. DisableDefaultCmd: true,
  77. },
  78. PersistentPreRun: func(cmd *cobra.Command, args []string) {
  79. // Disable usage printing on errors
  80. cmd.SilenceUsage = true
  81. // create the models directory and it's parent
  82. if err := os.MkdirAll(path.Join(cacheDir(), "models"), 0o700); err != nil {
  83. panic(err)
  84. }
  85. },
  86. }
  87. cobra.EnableCommandSorting = false
  88. runCmd := &cobra.Command{
  89. Use: "run MODEL",
  90. Short: "Run a model",
  91. Args: cobra.ExactArgs(1),
  92. RunE: func(cmd *cobra.Command, args []string) error {
  93. return run(args[0])
  94. },
  95. }
  96. serveCmd := &cobra.Command{
  97. Use: "serve",
  98. Aliases: []string{"start"},
  99. Short: "Start ollama",
  100. RunE: func(cmd *cobra.Command, args []string) error {
  101. return serve()
  102. },
  103. }
  104. rootCmd.AddCommand(
  105. serveCmd,
  106. runCmd,
  107. )
  108. return rootCmd
  109. }