ollama/cmd/cmd.go

169 lines
3.1 KiB
Go
Raw Normal View History

package cmd
import (
2023-07-06 13:49:31 -07:00
"bufio"
"context"
2023-07-06 12:24:49 -04:00
"fmt"
"log"
"net"
"os"
"path"
2023-07-06 14:05:55 -07:00
"github.com/schollz/progressbar/v3"
2023-07-06 13:49:31 -07:00
"github.com/spf13/cobra"
"golang.org/x/term"
2023-07-03 16:32:48 -04:00
"github.com/jmorganca/ollama/api"
"github.com/jmorganca/ollama/server"
)
2023-07-06 11:59:42 -04:00
func cacheDir() string {
2023-07-04 00:47:00 -04:00
home, err := os.UserHomeDir()
if err != nil {
panic(err)
}
2023-07-06 11:59:42 -04:00
return path.Join(home, ".ollama")
2023-07-04 00:47:00 -04:00
}
2023-07-06 14:05:55 -07:00
func RunRun(cmd *cobra.Command, args []string) error {
if err := pull(args[0]); err != nil {
return err
}
fmt.Println("Up to date.")
return RunGenerate(cmd, args)
2023-07-06 14:18:40 -04:00
}
2023-07-06 14:05:55 -07:00
func pull(model string) error {
2023-07-06 12:24:49 -04:00
client, err := NewAPIClient()
if err != nil {
return err
}
2023-07-06 14:05:55 -07:00
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)
},
)
2023-07-06 13:49:31 -07:00
}
func RunGenerate(_ *cobra.Command, args []string) error {
if len(args) > 1 {
return generate(args[0], args[1:]...)
}
if term.IsTerminal(int(os.Stdin.Fd())) {
return generateInteractive(args[0])
}
return generateBatch(args[0])
}
func generate(model string, prompts ...string) error {
client, err := NewAPIClient()
if err != nil {
return err
}
for _, prompt := range prompts {
2023-07-06 14:05:55 -07:00
client.Generate(context.Background(), &api.GenerateRequest{Model: model, Prompt: prompt}, func(resp api.GenerateResponse) error {
2023-07-06 13:49:31 -07:00
fmt.Print(resp.Response)
2023-07-06 14:05:55 -07:00
return nil
2023-07-06 13:49:31 -07:00
})
}
fmt.Println()
fmt.Println()
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(">>> ")
}
2023-07-06 14:57:11 -04:00
return nil
2023-07-06 12:24:49 -04:00
}
2023-07-06 13:49:31 -07:00
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 {
2023-07-06 10:56:08 -07:00
ln, err := net.Listen("tcp", "127.0.0.1:11434")
2023-07-04 00:47:00 -04:00
if err != nil {
return err
}
return server.Serve(ln)
}
func NewAPIClient() (*api.Client, error) {
return &api.Client{
2023-07-06 10:56:08 -07:00
URL: "http://localhost:11434",
}, nil
}
func NewCLI() *cobra.Command {
log.SetFlags(log.LstdFlags | log.Lshortfile)
rootCmd := &cobra.Command{
2023-07-06 13:49:31 -07:00
Use: "ollama",
Short: "Large language model runner",
SilenceUsage: true,
CompletionOptions: cobra.CompletionOptions{
DisableDefaultCmd: true,
},
2023-07-06 13:49:31 -07:00
PersistentPreRunE: func(_ *cobra.Command, args []string) error {
2023-07-06 11:59:42 -04:00
// create the models directory and it's parent
2023-07-06 13:49:31 -07:00
return os.MkdirAll(path.Join(cacheDir(), "models"), 0o700)
},
}
cobra.EnableCommandSorting = false
runCmd := &cobra.Command{
2023-07-06 13:49:31 -07:00
Use: "run MODEL [PROMPT]",
2023-07-04 00:47:00 -04:00
Short: "Run a model",
2023-07-06 13:49:31 -07:00
Args: cobra.MinimumNArgs(1),
RunE: RunRun,
}
serveCmd := &cobra.Command{
Use: "serve",
Aliases: []string{"start"},
Short: "Start ollama",
2023-07-06 13:49:31 -07:00
RunE: RunServer,
}
rootCmd.AddCommand(
serveCmd,
runCmd,
)
return rootCmd
}