"vscode:/vscode.git/clone" did not exist on "ea13d43e66fcc405fa5b6f630e1d41d8794d35a4"
cmd.go 2.65 KB
Newer Older
Jeffrey Morgan's avatar
Jeffrey Morgan committed
1
2
3
4
package cmd

import (
	"context"
Bruce MacDonald's avatar
Bruce MacDonald committed
5
	"fmt"
Jeffrey Morgan's avatar
Jeffrey Morgan committed
6
7
8
9
	"log"
	"net"
	"os"
	"path"
Bruce MacDonald's avatar
Bruce MacDonald committed
10
	"sync"
Jeffrey Morgan's avatar
Jeffrey Morgan committed
11

Bruce MacDonald's avatar
Bruce MacDonald committed
12
	"github.com/gosuri/uiprogress"
Jeffrey Morgan's avatar
Jeffrey Morgan committed
13
14
	"github.com/jmorganca/ollama/api"
	"github.com/jmorganca/ollama/server"
Jeffrey Morgan's avatar
Jeffrey Morgan committed
15
	"github.com/spf13/cobra"
Jeffrey Morgan's avatar
Jeffrey Morgan committed
16
17
)

Bruce MacDonald's avatar
Bruce MacDonald committed
18
func cacheDir() string {
Jeffrey Morgan's avatar
Jeffrey Morgan committed
19
20
21
22
23
	home, err := os.UserHomeDir()
	if err != nil {
		panic(err)
	}

Bruce MacDonald's avatar
Bruce MacDonald committed
24
	return path.Join(home, ".ollama")
Jeffrey Morgan's avatar
Jeffrey Morgan committed
25
26
}

Bruce MacDonald's avatar
Bruce MacDonald committed
27
28
29
30
func bytesToGB(bytes int) float64 {
	return float64(bytes) / float64(1<<30)
}

Bruce MacDonald's avatar
Bruce MacDonald committed
31
32
33
34
35
36
37
38
func run(model string) error {
	client, err := NewAPIClient()
	if err != nil {
		return err
	}
	pr := api.PullRequest{
		Model: model,
	}
Bruce MacDonald's avatar
Bruce MacDonald committed
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
	var bar *uiprogress.Bar
	mutex := &sync.Mutex{}
	var progressData api.PullProgress

	callback := func(progress api.PullProgress) {
		mutex.Lock()
		progressData = progress
		if bar == nil {
			uiprogress.Start()                           // start rendering
			bar = uiprogress.AddBar(int(progress.Total)) // Add a new bar

			// display the total file size and how much has downloaded so far
			bar.PrependFunc(func(b *uiprogress.Bar) string {
				return fmt.Sprintf("Downloading: %.2f GB / %.2f GB", bytesToGB(progressData.Completed), bytesToGB(progressData.Total))
			})

			// display completion percentage
			bar.AppendFunc(func(b *uiprogress.Bar) string {
				return fmt.Sprintf(" %d%%", int((float64(progressData.Completed)/float64(progressData.Total))*100))
			})
		}
		bar.Set(int(progress.Completed))
		mutex.Unlock()
Bruce MacDonald's avatar
Bruce MacDonald committed
62
63
64
65
66
	}
	_, err = client.Pull(context.Background(), &pr, callback)
	return err
}

Jeffrey Morgan's avatar
Jeffrey Morgan committed
67
func serve() error {
Michael Yang's avatar
Michael Yang committed
68
	ln, err := net.Listen("tcp", "127.0.0.1:11434")
Jeffrey Morgan's avatar
Jeffrey Morgan committed
69
70
71
72
73
74
75
76
	if err != nil {
		return err
	}

	return server.Serve(ln)
}

func NewAPIClient() (*api.Client, error) {
Jeffrey Morgan's avatar
Jeffrey Morgan committed
77
	return &api.Client{
Michael Yang's avatar
Michael Yang committed
78
		URL: "http://localhost:11434",
Jeffrey Morgan's avatar
Jeffrey Morgan committed
79
80
81
82
83
84
85
	}, nil
}

func NewCLI() *cobra.Command {
	log.SetFlags(log.LstdFlags | log.Lshortfile)

	rootCmd := &cobra.Command{
Jeffrey Morgan's avatar
Jeffrey Morgan committed
86
		Use:   "ollama",
87
		Short: "Large language model runner",
Jeffrey Morgan's avatar
Jeffrey Morgan committed
88
89
90
91
92
93
		CompletionOptions: cobra.CompletionOptions{
			DisableDefaultCmd: true,
		},
		PersistentPreRun: func(cmd *cobra.Command, args []string) {
			// Disable usage printing on errors
			cmd.SilenceUsage = true
Bruce MacDonald's avatar
Bruce MacDonald committed
94
95
96
97
			// create the models directory and it's parent
			if err := os.MkdirAll(path.Join(cacheDir(), "models"), 0o700); err != nil {
				panic(err)
			}
Jeffrey Morgan's avatar
Jeffrey Morgan committed
98
99
100
101
102
103
		},
	}

	cobra.EnableCommandSorting = false

	runCmd := &cobra.Command{
Bruce MacDonald's avatar
Bruce MacDonald committed
104
		Use:   "run MODEL",
Jeffrey Morgan's avatar
Jeffrey Morgan committed
105
		Short: "Run a model",
Bruce MacDonald's avatar
Bruce MacDonald committed
106
107
		Args:  cobra.ExactArgs(1),
		RunE: func(cmd *cobra.Command, args []string) error {
Bruce MacDonald's avatar
Bruce MacDonald committed
108
			return run(args[0])
Jeffrey Morgan's avatar
Jeffrey Morgan committed
109
110
111
112
113
114
115
116
		},
	}

	serveCmd := &cobra.Command{
		Use:     "serve",
		Aliases: []string{"start"},
		Short:   "Start ollama",
		RunE: func(cmd *cobra.Command, args []string) error {
Jeffrey Morgan's avatar
Jeffrey Morgan committed
117
			return serve()
Jeffrey Morgan's avatar
Jeffrey Morgan committed
118
119
120
121
122
		},
	}

	rootCmd.AddCommand(
		serveCmd,
123
		runCmd,
Jeffrey Morgan's avatar
Jeffrey Morgan committed
124
125
126
127
	)

	return rootCmd
}