123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184 |
- package cmd
- import (
- "bufio"
- "context"
- "encoding/json"
- "fmt"
- "log"
- "net"
- "os"
- "path"
- "sync"
- "github.com/gosuri/uiprogress"
- "github.com/spf13/cobra"
- "golang.org/x/term"
- "github.com/jmorganca/ollama/api"
- "github.com/jmorganca/ollama/server"
- )
- func cacheDir() string {
- home, err := os.UserHomeDir()
- if err != nil {
- panic(err)
- }
- return path.Join(home, ".ollama")
- }
- func bytesToGB(bytes int) float64 {
- return float64(bytes) / float64(1<<30)
- }
- func RunRun(cmd *cobra.Command, args []string) error {
- client, err := NewAPIClient()
- if err != nil {
- return err
- }
- pr := api.PullRequest{
- Model: 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))
- })
- }
- bar.Set(int(progress.Completed))
- mutex.Unlock()
- }
- if err := client.Pull(context.Background(), &pr, pullCallback); err != nil {
- return err
- }
- fmt.Println("Up to date.")
- 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(">>> ")
- }
- return nil
- }
- 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
- }
- 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",
- SilenceUsage: true,
- CompletionOptions: cobra.CompletionOptions{
- DisableDefaultCmd: true,
- },
- PersistentPreRunE: func(_ *cobra.Command, args []string) error {
- // create the models directory and it's parent
- return os.MkdirAll(path.Join(cacheDir(), "models"), 0o700)
- },
- }
- cobra.EnableCommandSorting = false
- runCmd := &cobra.Command{
- Use: "run MODEL [PROMPT]",
- Short: "Run a model",
- Args: cobra.MinimumNArgs(1),
- RunE: RunRun,
- }
- serveCmd := &cobra.Command{
- Use: "serve",
- Aliases: []string{"start"},
- Short: "Start ollama",
- RunE: RunServer,
- }
- rootCmd.AddCommand(
- serveCmd,
- runCmd,
- )
- return rootCmd
- }
|