cmd.go 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. package cmd
  2. import (
  3. "context"
  4. "fmt"
  5. "log"
  6. "net"
  7. "net/http"
  8. "os"
  9. "path"
  10. "time"
  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 run(model string) error {
  23. client, err := NewAPIClient()
  24. if err != nil {
  25. return err
  26. }
  27. pr := api.PullRequest{
  28. Model: model,
  29. }
  30. callback := func(progress string) {
  31. fmt.Println(progress)
  32. }
  33. _, err = client.Pull(context.Background(), &pr, callback)
  34. return err
  35. }
  36. func serve() error {
  37. sp := path.Join(cacheDir(), "ollama.sock")
  38. if err := os.RemoveAll(sp); err != nil {
  39. return err
  40. }
  41. ln, err := net.Listen("unix", sp)
  42. if err != nil {
  43. return err
  44. }
  45. if err := os.Chmod(sp, 0o700); err != nil {
  46. return err
  47. }
  48. return server.Serve(ln)
  49. }
  50. func NewAPIClient() (*api.Client, error) {
  51. var err error
  52. home, err := os.UserHomeDir()
  53. if err != nil {
  54. return nil, err
  55. }
  56. socket := path.Join(home, ".ollama", "ollama.sock")
  57. dialer := &net.Dialer{
  58. Timeout: 10 * time.Second,
  59. }
  60. return &api.Client{
  61. URL: "http://localhost",
  62. HTTP: http.Client{
  63. Transport: &http.Transport{
  64. DialContext: func(ctx context.Context, network, addr string) (net.Conn, error) {
  65. return dialer.DialContext(ctx, "unix", socket)
  66. },
  67. },
  68. },
  69. }, nil
  70. }
  71. func NewCLI() *cobra.Command {
  72. log.SetFlags(log.LstdFlags | log.Lshortfile)
  73. rootCmd := &cobra.Command{
  74. Use: "ollama",
  75. Short: "Large language model runner",
  76. CompletionOptions: cobra.CompletionOptions{
  77. DisableDefaultCmd: true,
  78. },
  79. PersistentPreRun: func(cmd *cobra.Command, args []string) {
  80. // Disable usage printing on errors
  81. cmd.SilenceUsage = true
  82. // create the models directory and it's parent
  83. if err := os.MkdirAll(path.Join(cacheDir(), "models"), 0o700); err != nil {
  84. panic(err)
  85. }
  86. },
  87. }
  88. cobra.EnableCommandSorting = false
  89. runCmd := &cobra.Command{
  90. Use: "run MODEL",
  91. Short: "Run a model",
  92. Args: cobra.ExactArgs(1),
  93. RunE: func(cmd *cobra.Command, args []string) error {
  94. return run(args[0])
  95. },
  96. }
  97. serveCmd := &cobra.Command{
  98. Use: "serve",
  99. Aliases: []string{"start"},
  100. Short: "Start ollama",
  101. RunE: func(cmd *cobra.Command, args []string) error {
  102. return serve()
  103. },
  104. }
  105. rootCmd.AddCommand(
  106. serveCmd,
  107. runCmd,
  108. )
  109. return rootCmd
  110. }