ollama/cmd/cmd.go

185 lines
3.7 KiB
Go
Raw Normal View History

package cmd
import (
2023-07-06 16:49:31 -04:00
"bufio"
"context"
2023-07-06 16:49:31 -04:00
"encoding/json"
2023-07-06 12:24:49 -04:00
"fmt"
"log"
"net"
"os"
"path"
2023-07-06 14:18:40 -04:00
"sync"
2023-07-06 14:18:40 -04:00
"github.com/gosuri/uiprogress"
2023-07-06 16:49:31 -04: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:18:40 -04:00
func bytesToGB(bytes int) float64 {
return float64(bytes) / float64(1<<30)
}
2023-07-06 16:49:31 -04:00
func RunRun(cmd *cobra.Command, args []string) error {
2023-07-06 12:24:49 -04:00
client, err := NewAPIClient()
if err != nil {
return err
}
pr := api.PullRequest{
2023-07-06 16:49:31 -04:00
Model: args[0],
2023-07-06 12:24:49 -04:00
}
2023-07-06 14:18:40 -04:00
var bar *uiprogress.Bar
mutex := &sync.Mutex{}
var progressData api.PullProgress
2023-07-06 14:57:11 -04:00
pullCallback := func(progress api.PullProgress) {
2023-07-06 14:18:40 -04:00
mutex.Lock()
progressData = progress
if bar == nil {
2023-07-06 15:00:15 -04:00
uiprogress.Start()
bar = uiprogress.AddBar(int(progress.Total))
2023-07-06 14:18:40 -04:00
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))
})
}
bar.Set(int(progress.Completed))
mutex.Unlock()
2023-07-06 12:24:49 -04:00
}
2023-07-06 14:57:11 -04:00
if err := client.Pull(context.Background(), &pr, pullCallback); err != nil {
return err
}
fmt.Println("Up to date.")
2023-07-06 16:49:31 -04:00
return RunGenerate(cmd, args)
}
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 {
client.Generate(context.Background(), &api.GenerateRequest{Model: model, Prompt: prompt}, func(bts []byte) {
var resp api.GenerateResponse
if err := json.Unmarshal(bts, &resp); err != nil {
return
}
fmt.Print(resp.Response)
})
}
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 16:49:31 -04: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 13:56:08 -04: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 13:56:08 -04:00
URL: "http://localhost:11434",
}, nil
}
func NewCLI() *cobra.Command {
log.SetFlags(log.LstdFlags | log.Lshortfile)
rootCmd := &cobra.Command{
2023-07-06 16:49:31 -04:00
Use: "ollama",
Short: "Large language model runner",
SilenceUsage: true,
CompletionOptions: cobra.CompletionOptions{
DisableDefaultCmd: true,
},
2023-07-06 16:49:31 -04: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 16:49:31 -04:00
return os.MkdirAll(path.Join(cacheDir(), "models"), 0o700)
},
}
cobra.EnableCommandSorting = false
runCmd := &cobra.Command{
2023-07-06 16:49:31 -04:00
Use: "run MODEL [PROMPT]",
2023-07-04 00:47:00 -04:00
Short: "Run a model",
2023-07-06 16:49:31 -04:00
Args: cobra.MinimumNArgs(1),
RunE: RunRun,
}
serveCmd := &cobra.Command{
Use: "serve",
Aliases: []string{"start"},
Short: "Start ollama",
2023-07-06 16:49:31 -04:00
RunE: RunServer,
}
rootCmd.AddCommand(
serveCmd,
runCmd,
)
return rootCmd
}