|
@@ -1,18 +1,22 @@
|
|
|
package cmd
|
|
|
|
|
|
import (
|
|
|
+ "bufio"
|
|
|
"context"
|
|
|
+ "errors"
|
|
|
"fmt"
|
|
|
"log"
|
|
|
"net"
|
|
|
"os"
|
|
|
"path"
|
|
|
- "sync"
|
|
|
+ "time"
|
|
|
+
|
|
|
+ "github.com/schollz/progressbar/v3"
|
|
|
+ "github.com/spf13/cobra"
|
|
|
+ "golang.org/x/term"
|
|
|
|
|
|
- "github.com/gosuri/uiprogress"
|
|
|
"github.com/jmorganca/ollama/api"
|
|
|
"github.com/jmorganca/ollama/server"
|
|
|
- "github.com/spf13/cobra"
|
|
|
)
|
|
|
|
|
|
func cacheDir() string {
|
|
@@ -24,46 +28,126 @@ func cacheDir() string {
|
|
|
return path.Join(home, ".ollama")
|
|
|
}
|
|
|
|
|
|
-func bytesToGB(bytes int) float64 {
|
|
|
- return float64(bytes) / float64(1<<30)
|
|
|
-}
|
|
|
+func RunRun(cmd *cobra.Command, args []string) error {
|
|
|
+ _, err := os.Stat(args[0])
|
|
|
+ switch {
|
|
|
+ case errors.Is(err, os.ErrNotExist):
|
|
|
+ if err := pull(args[0]); err != nil {
|
|
|
+ return err
|
|
|
+ }
|
|
|
|
|
|
-func run(model string) error {
|
|
|
- client, err := NewAPIClient()
|
|
|
- if err != nil {
|
|
|
+ fmt.Println("Up to date.")
|
|
|
+ case err != nil:
|
|
|
return err
|
|
|
}
|
|
|
- pr := api.PullRequest{
|
|
|
- Model: model,
|
|
|
+
|
|
|
+ return RunGenerate(cmd, args)
|
|
|
+}
|
|
|
+
|
|
|
+func pull(model string) error {
|
|
|
+ client := api.NewClient()
|
|
|
+
|
|
|
+ var bar *progressbar.ProgressBar
|
|
|
+ return client.Pull(
|
|
|
+ context.Background(),
|
|
|
+ &api.PullRequest{Model: model},
|
|
|
+ func(progress api.PullProgress) error {
|
|
|
+ if bar == nil {
|
|
|
+ bar = progressbar.DefaultBytes(progress.Total)
|
|
|
+ }
|
|
|
+
|
|
|
+ return bar.Set64(progress.Completed)
|
|
|
+ },
|
|
|
+ )
|
|
|
+}
|
|
|
+
|
|
|
+func RunGenerate(_ *cobra.Command, args []string) error {
|
|
|
+ if len(args) > 1 {
|
|
|
+ return generateOneshot(args[0], args[1:]...)
|
|
|
+ }
|
|
|
+
|
|
|
+ if term.IsTerminal(int(os.Stdin.Fd())) {
|
|
|
+ return generateInteractive(args[0])
|
|
|
}
|
|
|
- var bar *uiprogress.Bar
|
|
|
- mutex := &sync.Mutex{}
|
|
|
- var progressData api.PullProgress
|
|
|
-
|
|
|
- pullCallback := func(progress api.PullProgress) {
|
|
|
- mutex.Lock()
|
|
|
- progressData = progress
|
|
|
- if bar == nil {
|
|
|
- uiprogress.Start()
|
|
|
- bar = uiprogress.AddBar(int(progress.Total))
|
|
|
- bar.PrependFunc(func(b *uiprogress.Bar) string {
|
|
|
- return fmt.Sprintf("Downloading: %.2f GB / %.2f GB", bytesToGB(progressData.Completed), bytesToGB(progressData.Total))
|
|
|
- })
|
|
|
- bar.AppendFunc(func(b *uiprogress.Bar) string {
|
|
|
- return fmt.Sprintf(" %d%%", int((float64(progressData.Completed)/float64(progressData.Total))*100))
|
|
|
- })
|
|
|
+
|
|
|
+ return generateBatch(args[0])
|
|
|
+}
|
|
|
+
|
|
|
+func generate(model, prompt string) error {
|
|
|
+ client := api.NewClient()
|
|
|
+
|
|
|
+ spinner := progressbar.NewOptions(-1,
|
|
|
+ progressbar.OptionSetWriter(os.Stderr),
|
|
|
+ progressbar.OptionThrottle(60*time.Millisecond),
|
|
|
+ progressbar.OptionSpinnerType(14),
|
|
|
+ progressbar.OptionSetRenderBlankState(true),
|
|
|
+ progressbar.OptionSetElapsedTime(false),
|
|
|
+ progressbar.OptionClearOnFinish(),
|
|
|
+ )
|
|
|
+
|
|
|
+ go func() {
|
|
|
+ for range time.Tick(60 * time.Millisecond) {
|
|
|
+ if spinner.IsFinished() {
|
|
|
+ break
|
|
|
+ }
|
|
|
+
|
|
|
+ spinner.Add(1)
|
|
|
+ }
|
|
|
+ }()
|
|
|
+
|
|
|
+ client.Generate(context.Background(), &api.GenerateRequest{Model: model, Prompt: prompt}, func(resp api.GenerateResponse) error {
|
|
|
+ if !spinner.IsFinished() {
|
|
|
+ spinner.Finish()
|
|
|
+ }
|
|
|
+
|
|
|
+ fmt.Print(resp.Response)
|
|
|
+ return nil
|
|
|
+ })
|
|
|
+
|
|
|
+ fmt.Println()
|
|
|
+ fmt.Println()
|
|
|
+ return nil
|
|
|
+}
|
|
|
+
|
|
|
+func generateOneshot(model string, prompts ...string) error {
|
|
|
+ for _, prompt := range prompts {
|
|
|
+ fmt.Printf(">>> %s\n", prompt)
|
|
|
+ if err := generate(model, prompt); err != nil {
|
|
|
+ return err
|
|
|
}
|
|
|
- bar.Set(int(progress.Completed))
|
|
|
- mutex.Unlock()
|
|
|
}
|
|
|
- if err := client.Pull(context.Background(), &pr, pullCallback); err != nil {
|
|
|
- return err
|
|
|
+
|
|
|
+ return nil
|
|
|
+}
|
|
|
+
|
|
|
+func generateInteractive(model string) error {
|
|
|
+ fmt.Print(">>> ")
|
|
|
+ scanner := bufio.NewScanner(os.Stdin)
|
|
|
+ for scanner.Scan() {
|
|
|
+ if err := generate(model, scanner.Text()); err != nil {
|
|
|
+ return err
|
|
|
+ }
|
|
|
+
|
|
|
+ fmt.Print(">>> ")
|
|
|
}
|
|
|
- fmt.Println("Up to date.")
|
|
|
+
|
|
|
return nil
|
|
|
}
|
|
|
|
|
|
-func serve() error {
|
|
|
+func generateBatch(model string) error {
|
|
|
+ scanner := bufio.NewScanner(os.Stdin)
|
|
|
+ for scanner.Scan() {
|
|
|
+ prompt := scanner.Text()
|
|
|
+ fmt.Printf(">>> %s\n", prompt)
|
|
|
+ if err := generate(model, prompt); err != nil {
|
|
|
+ return err
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ return nil
|
|
|
+}
|
|
|
+
|
|
|
+func RunServer(_ *cobra.Command, _ []string) error {
|
|
|
ln, err := net.Listen("tcp", "127.0.0.1:11434")
|
|
|
if err != nil {
|
|
|
return err
|
|
@@ -72,49 +156,36 @@ func serve() error {
|
|
|
return server.Serve(ln)
|
|
|
}
|
|
|
|
|
|
-func NewAPIClient() (*api.Client, error) {
|
|
|
- return &api.Client{
|
|
|
- URL: "http://localhost:11434",
|
|
|
- }, nil
|
|
|
-}
|
|
|
-
|
|
|
func NewCLI() *cobra.Command {
|
|
|
log.SetFlags(log.LstdFlags | log.Lshortfile)
|
|
|
|
|
|
rootCmd := &cobra.Command{
|
|
|
- Use: "ollama",
|
|
|
- Short: "Large language model runner",
|
|
|
+ Use: "ollama",
|
|
|
+ Short: "Large language model runner",
|
|
|
+ SilenceUsage: true,
|
|
|
CompletionOptions: cobra.CompletionOptions{
|
|
|
DisableDefaultCmd: true,
|
|
|
},
|
|
|
- PersistentPreRun: func(cmd *cobra.Command, args []string) {
|
|
|
- // Disable usage printing on errors
|
|
|
- cmd.SilenceUsage = true
|
|
|
+ PersistentPreRunE: func(_ *cobra.Command, args []string) error {
|
|
|
// create the models directory and it's parent
|
|
|
- if err := os.MkdirAll(path.Join(cacheDir(), "models"), 0o700); err != nil {
|
|
|
- panic(err)
|
|
|
- }
|
|
|
+ return os.MkdirAll(path.Join(cacheDir(), "models"), 0o700)
|
|
|
},
|
|
|
}
|
|
|
|
|
|
cobra.EnableCommandSorting = false
|
|
|
|
|
|
runCmd := &cobra.Command{
|
|
|
- Use: "run MODEL",
|
|
|
+ Use: "run MODEL [PROMPT]",
|
|
|
Short: "Run a model",
|
|
|
- Args: cobra.ExactArgs(1),
|
|
|
- RunE: func(cmd *cobra.Command, args []string) error {
|
|
|
- return run(args[0])
|
|
|
- },
|
|
|
+ Args: cobra.MinimumNArgs(1),
|
|
|
+ RunE: RunRun,
|
|
|
}
|
|
|
|
|
|
serveCmd := &cobra.Command{
|
|
|
Use: "serve",
|
|
|
Aliases: []string{"start"},
|
|
|
Short: "Start ollama",
|
|
|
- RunE: func(cmd *cobra.Command, args []string) error {
|
|
|
- return serve()
|
|
|
- },
|
|
|
+ RunE: RunServer,
|
|
|
}
|
|
|
|
|
|
rootCmd.AddCommand(
|