| 
									
										
										
										
											2023-07-04 03:22:44 +08:00
										 |  |  | package cmd | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | import ( | 
					
						
							| 
									
										
										
										
											2024-03-07 13:01:51 +08:00
										 |  |  | 	"archive/zip" | 
					
						
							| 
									
										
										
										
											2023-11-15 06:07:40 +08:00
										 |  |  | 	"bytes" | 
					
						
							| 
									
										
										
										
											2023-07-04 03:22:44 +08:00
										 |  |  | 	"context" | 
					
						
							| 
									
										
										
										
											2023-08-12 01:58:23 +08:00
										 |  |  | 	"crypto/ed25519" | 
					
						
							|  |  |  | 	"crypto/rand" | 
					
						
							| 
									
										
										
										
											2023-11-15 06:07:40 +08:00
										 |  |  | 	"crypto/sha256" | 
					
						
							| 
									
										
										
										
											2023-08-12 01:58:23 +08:00
										 |  |  | 	"encoding/pem" | 
					
						
							| 
									
										
										
										
											2023-07-07 06:43:04 +08:00
										 |  |  | 	"errors" | 
					
						
							| 
									
										
										
										
											2023-07-07 00:24:49 +08:00
										 |  |  | 	"fmt" | 
					
						
							| 
									
										
										
										
											2023-07-19 05:01:19 +08:00
										 |  |  | 	"io" | 
					
						
							| 
									
										
										
										
											2023-07-04 03:22:44 +08:00
										 |  |  | 	"log" | 
					
						
							| 
									
										
										
										
											2024-05-14 08:17:36 +08:00
										 |  |  | 	"math" | 
					
						
							| 
									
										
										
										
											2023-07-04 03:22:44 +08:00
										 |  |  | 	"net" | 
					
						
							| 
									
										
										
										
											2023-10-30 23:10:18 +08:00
										 |  |  | 	"net/http" | 
					
						
							| 
									
										
										
										
											2023-07-04 03:22:44 +08:00
										 |  |  | 	"os" | 
					
						
							| 
									
										
										
										
											2023-09-29 08:13:01 +08:00
										 |  |  | 	"os/signal" | 
					
						
							| 
									
										
										
										
											2023-07-19 10:34:05 +08:00
										 |  |  | 	"path/filepath" | 
					
						
							| 
									
										
										
										
											2024-04-25 05:27:12 +08:00
										 |  |  | 	"regexp" | 
					
						
							| 
									
										
										
										
											2023-08-01 04:25:57 +08:00
										 |  |  | 	"runtime" | 
					
						
							| 
									
										
										
										
											2024-05-22 12:30:52 +08:00
										 |  |  | 	"slices" | 
					
						
							| 
									
										
										
										
											2023-07-08 01:12:58 +08:00
										 |  |  | 	"strings" | 
					
						
							| 
									
										
										
										
											2023-09-29 08:13:01 +08:00
										 |  |  | 	"syscall" | 
					
						
							| 
									
										
										
										
											2023-07-07 06:43:04 +08:00
										 |  |  | 	"time" | 
					
						
							| 
									
										
										
										
											2023-07-04 03:22:44 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-11-24 14:21:32 +08:00
										 |  |  | 	"github.com/containerd/console" | 
					
						
							| 
									
										
										
										
											2024-05-16 07:29:33 +08:00
										 |  |  | 	"github.com/mattn/go-runewidth" | 
					
						
							| 
									
										
										
										
											2023-07-19 00:09:45 +08:00
										 |  |  | 	"github.com/olekukonko/tablewriter" | 
					
						
							| 
									
										
										
										
											2023-07-07 04:49:31 +08:00
										 |  |  | 	"github.com/spf13/cobra" | 
					
						
							| 
									
										
										
										
											2023-08-12 01:58:23 +08:00
										 |  |  | 	"golang.org/x/crypto/ssh" | 
					
						
							| 
									
										
										
										
											2023-09-23 04:36:08 +08:00
										 |  |  | 	"golang.org/x/term" | 
					
						
							| 
									
										
										
										
											2023-07-07 04:49:31 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-03-27 04:04:17 +08:00
										 |  |  | 	"github.com/ollama/ollama/api" | 
					
						
							| 
									
										
										
										
											2024-05-01 02:02:08 +08:00
										 |  |  | 	"github.com/ollama/ollama/auth" | 
					
						
							| 
									
										
										
										
											2024-05-25 05:57:15 +08:00
										 |  |  | 	"github.com/ollama/ollama/envconfig" | 
					
						
							| 
									
										
										
										
											2024-03-27 04:04:17 +08:00
										 |  |  | 	"github.com/ollama/ollama/format" | 
					
						
							| 
									
										
										
										
											2024-05-21 02:26:45 +08:00
										 |  |  | 	"github.com/ollama/ollama/parser" | 
					
						
							| 
									
										
										
										
											2024-03-27 04:04:17 +08:00
										 |  |  | 	"github.com/ollama/ollama/progress" | 
					
						
							|  |  |  | 	"github.com/ollama/ollama/server" | 
					
						
							| 
									
										
										
										
											2024-05-01 02:02:08 +08:00
										 |  |  | 	"github.com/ollama/ollama/types/errtypes" | 
					
						
							|  |  |  | 	"github.com/ollama/ollama/types/model" | 
					
						
							| 
									
										
										
										
											2024-03-27 04:04:17 +08:00
										 |  |  | 	"github.com/ollama/ollama/version" | 
					
						
							| 
									
										
										
										
											2023-07-04 03:22:44 +08:00
										 |  |  | ) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-07-21 07:09:23 +08:00
										 |  |  | func CreateHandler(cmd *cobra.Command, args []string) error { | 
					
						
							| 
									
										
										
										
											2023-07-17 08:02:22 +08:00
										 |  |  | 	filename, _ := cmd.Flags().GetString("file") | 
					
						
							| 
									
										
										
										
											2023-07-19 10:34:05 +08:00
										 |  |  | 	filename, err := filepath.Abs(filename) | 
					
						
							|  |  |  | 	if err != nil { | 
					
						
							|  |  |  | 		return err | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-10-10 03:18:26 +08:00
										 |  |  | 	client, err := api.ClientFromEnvironment() | 
					
						
							| 
									
										
										
										
											2023-08-16 23:03:48 +08:00
										 |  |  | 	if err != nil { | 
					
						
							|  |  |  | 		return err | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2023-07-17 08:02:22 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-11-15 08:33:24 +08:00
										 |  |  | 	p := progress.NewProgress(os.Stderr) | 
					
						
							|  |  |  | 	defer p.Stop() | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-05-01 01:55:19 +08:00
										 |  |  | 	f, err := os.Open(filename) | 
					
						
							| 
									
										
										
										
											2023-11-15 06:07:40 +08:00
										 |  |  | 	if err != nil { | 
					
						
							|  |  |  | 		return err | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2024-05-01 01:55:19 +08:00
										 |  |  | 	defer f.Close() | 
					
						
							| 
									
										
										
										
											2023-11-15 06:07:40 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-05-21 02:26:45 +08:00
										 |  |  | 	modelfile, err := parser.ParseFile(f) | 
					
						
							| 
									
										
										
										
											2023-11-15 06:07:40 +08:00
										 |  |  | 	if err != nil { | 
					
						
							|  |  |  | 		return err | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	home, err := os.UserHomeDir() | 
					
						
							|  |  |  | 	if err != nil { | 
					
						
							|  |  |  | 		return err | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-11-20 02:43:21 +08:00
										 |  |  | 	status := "transferring model data" | 
					
						
							|  |  |  | 	spinner := progress.NewSpinner(status) | 
					
						
							| 
									
										
										
										
											2023-11-16 08:59:49 +08:00
										 |  |  | 	p.Add(status, spinner) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-05-01 01:55:19 +08:00
										 |  |  | 	for i := range modelfile.Commands { | 
					
						
							|  |  |  | 		switch modelfile.Commands[i].Name { | 
					
						
							| 
									
										
										
										
											2023-11-15 06:07:40 +08:00
										 |  |  | 		case "model", "adapter": | 
					
						
							| 
									
										
										
										
											2024-05-01 01:55:19 +08:00
										 |  |  | 			path := modelfile.Commands[i].Args | 
					
						
							| 
									
										
										
										
											2023-11-15 06:07:40 +08:00
										 |  |  | 			if path == "~" { | 
					
						
							|  |  |  | 				path = home | 
					
						
							|  |  |  | 			} else if strings.HasPrefix(path, "~/") { | 
					
						
							|  |  |  | 				path = filepath.Join(home, path[2:]) | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-11-21 05:43:48 +08:00
										 |  |  | 			if !filepath.IsAbs(path) { | 
					
						
							|  |  |  | 				path = filepath.Join(filepath.Dir(filename), path) | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-03-07 13:01:51 +08:00
										 |  |  | 			fi, err := os.Stat(path) | 
					
						
							| 
									
										
										
										
											2024-05-01 01:55:19 +08:00
										 |  |  | 			if errors.Is(err, os.ErrNotExist) && modelfile.Commands[i].Name == "model" { | 
					
						
							| 
									
										
										
										
											2023-11-16 02:57:09 +08:00
										 |  |  | 				continue | 
					
						
							| 
									
										
										
										
											2023-11-15 06:07:40 +08:00
										 |  |  | 			} else if err != nil { | 
					
						
							|  |  |  | 				return err | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-03-07 13:01:51 +08:00
										 |  |  | 			if fi.IsDir() { | 
					
						
							| 
									
										
										
										
											2024-04-23 02:02:25 +08:00
										 |  |  | 				// this is likely a safetensors or pytorch directory
 | 
					
						
							|  |  |  | 				// TODO make this work w/ adapters
 | 
					
						
							|  |  |  | 				tempfile, err := tempZipFiles(path) | 
					
						
							| 
									
										
										
										
											2024-03-07 13:01:51 +08:00
										 |  |  | 				if err != nil { | 
					
						
							|  |  |  | 					return err | 
					
						
							|  |  |  | 				} | 
					
						
							| 
									
										
										
										
											2024-04-23 02:02:25 +08:00
										 |  |  | 				defer os.RemoveAll(tempfile) | 
					
						
							| 
									
										
										
										
											2024-04-16 02:26:42 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-04-23 02:02:25 +08:00
										 |  |  | 				path = tempfile | 
					
						
							| 
									
										
										
										
											2023-11-15 06:07:40 +08:00
										 |  |  | 			} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-03-07 13:01:51 +08:00
										 |  |  | 			digest, err := createBlob(cmd, client, path) | 
					
						
							|  |  |  | 			if err != nil { | 
					
						
							| 
									
										
										
										
											2023-11-15 06:07:40 +08:00
										 |  |  | 				return err | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-05-01 01:55:19 +08:00
										 |  |  | 			modelfile.Commands[i].Args = "@" + digest | 
					
						
							| 
									
										
										
										
											2023-11-15 06:07:40 +08:00
										 |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2023-07-18 05:14:41 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-04-23 02:02:25 +08:00
										 |  |  | 	bars := make(map[string]*progress.Bar) | 
					
						
							| 
									
										
										
										
											2023-07-26 02:25:13 +08:00
										 |  |  | 	fn := func(resp api.ProgressResponse) error { | 
					
						
							| 
									
										
										
										
											2023-11-15 08:33:24 +08:00
										 |  |  | 		if resp.Digest != "" { | 
					
						
							|  |  |  | 			spinner.Stop() | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 			bar, ok := bars[resp.Digest] | 
					
						
							|  |  |  | 			if !ok { | 
					
						
							| 
									
										
										
										
											2023-11-21 00:37:17 +08:00
										 |  |  | 				bar = progress.NewBar(fmt.Sprintf("pulling %s...", resp.Digest[7:19]), resp.Total, resp.Completed) | 
					
						
							| 
									
										
										
										
											2023-11-15 08:33:24 +08:00
										 |  |  | 				bars[resp.Digest] = bar | 
					
						
							|  |  |  | 				p.Add(resp.Digest, bar) | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 			bar.Set(resp.Completed) | 
					
						
							|  |  |  | 		} else if status != resp.Status { | 
					
						
							|  |  |  | 			spinner.Stop() | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 			status = resp.Status | 
					
						
							|  |  |  | 			spinner = progress.NewSpinner(status) | 
					
						
							|  |  |  | 			p.Add(status, spinner) | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-07-17 08:02:22 +08:00
										 |  |  | 		return nil | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-05-11 04:06:13 +08:00
										 |  |  | 	quantize, _ := cmd.Flags().GetString("quantize") | 
					
						
							| 
									
										
										
										
											2024-04-05 23:49:04 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-05-11 04:06:13 +08:00
										 |  |  | 	request := api.CreateRequest{Name: args[0], Modelfile: modelfile.String(), Quantize: quantize} | 
					
						
							| 
									
										
										
										
											2023-10-13 06:56:40 +08:00
										 |  |  | 	if err := client.Create(cmd.Context(), &request, fn); err != nil { | 
					
						
							| 
									
										
										
										
											2023-07-17 08:02:22 +08:00
										 |  |  | 		return err | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	return nil | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-04-23 02:02:25 +08:00
										 |  |  | func tempZipFiles(path string) (string, error) { | 
					
						
							|  |  |  | 	tempfile, err := os.CreateTemp("", "ollama-tf") | 
					
						
							|  |  |  | 	if err != nil { | 
					
						
							|  |  |  | 		return "", err | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	defer tempfile.Close() | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	zipfile := zip.NewWriter(tempfile) | 
					
						
							|  |  |  | 	defer zipfile.Close() | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-04-26 05:41:30 +08:00
										 |  |  | 	detectContentType := func(path string) (string, error) { | 
					
						
							|  |  |  | 		f, err := os.Open(path) | 
					
						
							| 
									
										
										
										
											2024-04-23 02:02:25 +08:00
										 |  |  | 		if err != nil { | 
					
						
							|  |  |  | 			return "", err | 
					
						
							|  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2024-04-26 05:41:30 +08:00
										 |  |  | 		defer f.Close() | 
					
						
							| 
									
										
										
										
											2024-04-23 02:02:25 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-04-26 05:41:30 +08:00
										 |  |  | 		var b bytes.Buffer | 
					
						
							|  |  |  | 		b.Grow(512) | 
					
						
							| 
									
										
										
										
											2024-04-23 02:02:25 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-04-26 05:41:30 +08:00
										 |  |  | 		if _, err := io.CopyN(&b, f, 512); err != nil && !errors.Is(err, io.EOF) { | 
					
						
							|  |  |  | 			return "", err | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		contentType, _, _ := strings.Cut(http.DetectContentType(b.Bytes()), ";") | 
					
						
							|  |  |  | 		return contentType, nil | 
					
						
							| 
									
										
										
										
											2024-04-23 02:02:25 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-04-26 05:41:30 +08:00
										 |  |  | 	glob := func(pattern, contentType string) ([]string, error) { | 
					
						
							|  |  |  | 		matches, err := filepath.Glob(pattern) | 
					
						
							|  |  |  | 		if err != nil { | 
					
						
							|  |  |  | 			return nil, err | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		for _, safetensor := range matches { | 
					
						
							|  |  |  | 			if ct, err := detectContentType(safetensor); err != nil { | 
					
						
							|  |  |  | 				return nil, err | 
					
						
							|  |  |  | 			} else if ct != contentType { | 
					
						
							|  |  |  | 				return nil, fmt.Errorf("invalid content type: expected %s for %s", ct, safetensor) | 
					
						
							| 
									
										
										
										
											2024-04-23 02:02:25 +08:00
										 |  |  | 			} | 
					
						
							| 
									
										
										
										
											2024-04-26 05:41:30 +08:00
										 |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		return matches, nil | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	var files []string | 
					
						
							|  |  |  | 	if st, _ := glob(filepath.Join(path, "model*.safetensors"), "application/octet-stream"); len(st) > 0 { | 
					
						
							|  |  |  | 		// safetensors files might be unresolved git lfs references; skip if they are
 | 
					
						
							|  |  |  | 		// covers model-x-of-y.safetensors, model.fp32-x-of-y.safetensors, model.safetensors
 | 
					
						
							|  |  |  | 		files = append(files, st...) | 
					
						
							|  |  |  | 	} else if pt, _ := glob(filepath.Join(path, "pytorch_model*.bin"), "application/zip"); len(pt) > 0 { | 
					
						
							|  |  |  | 		// pytorch files might also be unresolved git lfs references; skip if they are
 | 
					
						
							|  |  |  | 		// covers pytorch_model-x-of-y.bin, pytorch_model.fp32-x-of-y.bin, pytorch_model.bin
 | 
					
						
							|  |  |  | 		files = append(files, pt...) | 
					
						
							| 
									
										
										
										
											2024-05-09 07:07:46 +08:00
										 |  |  | 	} else if pt, _ := glob(filepath.Join(path, "consolidated*.pth"), "application/zip"); len(pt) > 0 { | 
					
						
							| 
									
										
										
										
											2024-04-26 05:41:30 +08:00
										 |  |  | 		// pytorch files might also be unresolved git lfs references; skip if they are
 | 
					
						
							|  |  |  | 		// covers consolidated.x.pth, consolidated.pth
 | 
					
						
							|  |  |  | 		files = append(files, pt...) | 
					
						
							|  |  |  | 	} else { | 
					
						
							|  |  |  | 		return "", errors.New("no safetensors or torch files found") | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	// add configuration files, json files are detected as text/plain
 | 
					
						
							|  |  |  | 	js, err := glob(filepath.Join(path, "*.json"), "text/plain") | 
					
						
							|  |  |  | 	if err != nil { | 
					
						
							|  |  |  | 		return "", err | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	files = append(files, js...) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if tks, _ := glob(filepath.Join(path, "tokenizer.model"), "application/octet-stream"); len(tks) > 0 { | 
					
						
							|  |  |  | 		// add tokenizer.model if it exists, tokenizer.json is automatically picked up by the previous glob
 | 
					
						
							|  |  |  | 		// tokenizer.model might be a unresolved git lfs reference; error if it is
 | 
					
						
							|  |  |  | 		files = append(files, tks...) | 
					
						
							|  |  |  | 	} else if tks, _ := glob(filepath.Join(path, "**/tokenizer.model"), "text/plain"); len(tks) > 0 { | 
					
						
							|  |  |  | 		// some times tokenizer.model is in a subdirectory (e.g. meta-llama/Meta-Llama-3-8B)
 | 
					
						
							|  |  |  | 		files = append(files, tks...) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	for _, file := range files { | 
					
						
							|  |  |  | 		f, err := os.Open(file) | 
					
						
							|  |  |  | 		if err != nil { | 
					
						
							| 
									
										
										
										
											2024-04-23 02:02:25 +08:00
										 |  |  | 			return "", err | 
					
						
							|  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2024-04-26 05:41:30 +08:00
										 |  |  | 		defer f.Close() | 
					
						
							| 
									
										
										
										
											2024-04-23 02:02:25 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 		fi, err := f.Stat() | 
					
						
							|  |  |  | 		if err != nil { | 
					
						
							|  |  |  | 			return "", err | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-04-26 05:41:30 +08:00
										 |  |  | 		zfi, err := zip.FileInfoHeader(fi) | 
					
						
							| 
									
										
										
										
											2024-04-23 02:02:25 +08:00
										 |  |  | 		if err != nil { | 
					
						
							|  |  |  | 			return "", err | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-04-26 05:41:30 +08:00
										 |  |  | 		zf, err := zipfile.CreateHeader(zfi) | 
					
						
							| 
									
										
										
										
											2024-04-23 02:02:25 +08:00
										 |  |  | 		if err != nil { | 
					
						
							|  |  |  | 			return "", err | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-04-26 05:41:30 +08:00
										 |  |  | 		if _, err := io.Copy(zf, f); err != nil { | 
					
						
							| 
									
										
										
										
											2024-04-23 02:02:25 +08:00
										 |  |  | 			return "", err | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	return tempfile.Name(), nil | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-03-07 13:01:51 +08:00
										 |  |  | func createBlob(cmd *cobra.Command, client *api.Client, path string) (string, error) { | 
					
						
							|  |  |  | 	bin, err := os.Open(path) | 
					
						
							|  |  |  | 	if err != nil { | 
					
						
							|  |  |  | 		return "", err | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	defer bin.Close() | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	hash := sha256.New() | 
					
						
							|  |  |  | 	if _, err := io.Copy(hash, bin); err != nil { | 
					
						
							|  |  |  | 		return "", err | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2024-03-29 09:54:01 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	if _, err := bin.Seek(0, io.SeekStart); err != nil { | 
					
						
							|  |  |  | 		return "", err | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2024-03-07 13:01:51 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	digest := fmt.Sprintf("sha256:%x", hash.Sum(nil)) | 
					
						
							|  |  |  | 	if err = client.CreateBlob(cmd.Context(), digest, bin); err != nil { | 
					
						
							|  |  |  | 		return "", err | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	return digest, nil | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-07-26 00:07:27 +08:00
										 |  |  | func RunHandler(cmd *cobra.Command, args []string) error { | 
					
						
							| 
									
										
										
										
											2023-10-10 03:18:26 +08:00
										 |  |  | 	client, err := api.ClientFromEnvironment() | 
					
						
							| 
									
										
										
										
											2023-08-22 12:56:56 +08:00
										 |  |  | 	if err != nil { | 
					
						
							|  |  |  | 		return err | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-10-30 23:10:18 +08:00
										 |  |  | 	name := args[0] | 
					
						
							| 
									
										
										
										
											2024-02-02 15:11:52 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-10-30 23:10:18 +08:00
										 |  |  | 	// check if the model exists on the server
 | 
					
						
							| 
									
										
										
										
											2024-02-02 13:33:06 +08:00
										 |  |  | 	show, err := client.Show(cmd.Context(), &api.ShowRequest{Name: name}) | 
					
						
							| 
									
										
										
										
											2023-11-01 11:49:48 +08:00
										 |  |  | 	var statusError api.StatusError | 
					
						
							|  |  |  | 	switch { | 
					
						
							|  |  |  | 	case errors.As(err, &statusError) && statusError.StatusCode == http.StatusNotFound: | 
					
						
							| 
									
										
										
										
											2024-01-06 04:22:32 +08:00
										 |  |  | 		if err := PullHandler(cmd, []string{name}); err != nil { | 
					
						
							| 
									
										
										
										
											2023-10-30 23:10:18 +08:00
										 |  |  | 			return err | 
					
						
							| 
									
										
										
										
											2023-07-07 06:43:04 +08:00
										 |  |  | 		} | 
					
						
							| 
									
										
										
										
											2024-02-02 15:11:52 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 		show, err = client.Show(cmd.Context(), &api.ShowRequest{Name: name}) | 
					
						
							|  |  |  | 		if err != nil { | 
					
						
							|  |  |  | 			return err | 
					
						
							|  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2023-11-01 11:49:48 +08:00
										 |  |  | 	case err != nil: | 
					
						
							|  |  |  | 		return err | 
					
						
							| 
									
										
										
										
											2024-01-06 04:22:32 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-02-02 13:33:06 +08:00
										 |  |  | 	interactive := true | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	opts := runOptions{ | 
					
						
							|  |  |  | 		Model:       args[0], | 
					
						
							|  |  |  | 		WordWrap:    os.Getenv("TERM") == "xterm-256color", | 
					
						
							|  |  |  | 		Options:     map[string]interface{}{}, | 
					
						
							|  |  |  | 		MultiModal:  slices.Contains(show.Details.Families, "clip"), | 
					
						
							|  |  |  | 		ParentModel: show.Details.ParentModel, | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	format, err := cmd.Flags().GetString("format") | 
					
						
							|  |  |  | 	if err != nil { | 
					
						
							|  |  |  | 		return err | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	opts.Format = format | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-05-14 08:17:36 +08:00
										 |  |  | 	keepAlive, err := cmd.Flags().GetString("keepalive") | 
					
						
							|  |  |  | 	if err != nil { | 
					
						
							|  |  |  | 		return err | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	if keepAlive != "" { | 
					
						
							|  |  |  | 		d, err := time.ParseDuration(keepAlive) | 
					
						
							|  |  |  | 		if err != nil { | 
					
						
							|  |  |  | 			return err | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 		opts.KeepAlive = &api.Duration{Duration: d} | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-02-02 13:33:06 +08:00
										 |  |  | 	prompts := args[1:] | 
					
						
							|  |  |  | 	// prepend stdin to the prompt if provided
 | 
					
						
							|  |  |  | 	if !term.IsTerminal(int(os.Stdin.Fd())) { | 
					
						
							|  |  |  | 		in, err := io.ReadAll(os.Stdin) | 
					
						
							|  |  |  | 		if err != nil { | 
					
						
							|  |  |  | 			return err | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		prompts = append([]string{string(in)}, prompts...) | 
					
						
							|  |  |  | 		opts.WordWrap = false | 
					
						
							|  |  |  | 		interactive = false | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	opts.Prompt = strings.Join(prompts, " ") | 
					
						
							|  |  |  | 	if len(prompts) > 0 { | 
					
						
							|  |  |  | 		interactive = false | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	nowrap, err := cmd.Flags().GetBool("nowordwrap") | 
					
						
							|  |  |  | 	if err != nil { | 
					
						
							|  |  |  | 		return err | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	opts.WordWrap = !nowrap | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if !interactive { | 
					
						
							|  |  |  | 		return generate(cmd, opts) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	return generateInteractive(cmd, opts) | 
					
						
							| 
									
										
										
										
											2023-07-07 02:18:40 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-05-01 02:02:08 +08:00
										 |  |  | func errFromUnknownKey(unknownKeyErr error) error { | 
					
						
							|  |  |  | 	// find SSH public key in the error message
 | 
					
						
							|  |  |  | 	sshKeyPattern := `ssh-\w+ [^\s"]+` | 
					
						
							|  |  |  | 	re := regexp.MustCompile(sshKeyPattern) | 
					
						
							|  |  |  | 	matches := re.FindStringSubmatch(unknownKeyErr.Error()) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if len(matches) > 0 { | 
					
						
							|  |  |  | 		serverPubKey := matches[0] | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		localPubKey, err := auth.GetPublicKey() | 
					
						
							|  |  |  | 		if err != nil { | 
					
						
							|  |  |  | 			return unknownKeyErr | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		if runtime.GOOS == "linux" && serverPubKey != localPubKey { | 
					
						
							|  |  |  | 			// try the ollama service public key
 | 
					
						
							|  |  |  | 			svcPubKey, err := os.ReadFile("/usr/share/ollama/.ollama/id_ed25519.pub") | 
					
						
							|  |  |  | 			if err != nil { | 
					
						
							|  |  |  | 				return unknownKeyErr | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 			localPubKey = strings.TrimSpace(string(svcPubKey)) | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		// check if the returned public key matches the local public key, this prevents adding a remote key to the user's account
 | 
					
						
							|  |  |  | 		if serverPubKey != localPubKey { | 
					
						
							|  |  |  | 			return unknownKeyErr | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		var msg strings.Builder | 
					
						
							|  |  |  | 		msg.WriteString(unknownKeyErr.Error()) | 
					
						
							|  |  |  | 		msg.WriteString("\n\nYour ollama key is:\n") | 
					
						
							|  |  |  | 		msg.WriteString(localPubKey) | 
					
						
							|  |  |  | 		msg.WriteString("\nAdd your key at:\n") | 
					
						
							|  |  |  | 		msg.WriteString("https://ollama.com/settings/keys") | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		return errors.New(msg.String()) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	return unknownKeyErr | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-07-21 07:09:23 +08:00
										 |  |  | func PushHandler(cmd *cobra.Command, args []string) error { | 
					
						
							| 
									
										
										
										
											2023-10-10 03:18:26 +08:00
										 |  |  | 	client, err := api.ClientFromEnvironment() | 
					
						
							| 
									
										
										
										
											2023-08-16 23:03:48 +08:00
										 |  |  | 	if err != nil { | 
					
						
							|  |  |  | 		return err | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2023-07-17 08:02:22 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-07-22 06:42:19 +08:00
										 |  |  | 	insecure, err := cmd.Flags().GetBool("insecure") | 
					
						
							|  |  |  | 	if err != nil { | 
					
						
							|  |  |  | 		return err | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-11-15 08:33:24 +08:00
										 |  |  | 	p := progress.NewProgress(os.Stderr) | 
					
						
							|  |  |  | 	defer p.Stop() | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	bars := make(map[string]*progress.Bar) | 
					
						
							| 
									
										
										
										
											2023-11-20 02:43:21 +08:00
										 |  |  | 	var status string | 
					
						
							|  |  |  | 	var spinner *progress.Spinner | 
					
						
							| 
									
										
										
										
											2023-11-15 08:33:24 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-07-19 09:51:30 +08:00
										 |  |  | 	fn := func(resp api.ProgressResponse) error { | 
					
						
							| 
									
										
										
										
											2023-11-15 08:33:24 +08:00
										 |  |  | 		if resp.Digest != "" { | 
					
						
							| 
									
										
										
										
											2023-11-20 02:43:21 +08:00
										 |  |  | 			if spinner != nil { | 
					
						
							|  |  |  | 				spinner.Stop() | 
					
						
							|  |  |  | 			} | 
					
						
							| 
									
										
										
										
											2023-11-15 08:33:24 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 			bar, ok := bars[resp.Digest] | 
					
						
							|  |  |  | 			if !ok { | 
					
						
							| 
									
										
										
										
											2023-11-21 00:37:17 +08:00
										 |  |  | 				bar = progress.NewBar(fmt.Sprintf("pushing %s...", resp.Digest[7:19]), resp.Total, resp.Completed) | 
					
						
							| 
									
										
										
										
											2023-11-15 08:33:24 +08:00
										 |  |  | 				bars[resp.Digest] = bar | 
					
						
							|  |  |  | 				p.Add(resp.Digest, bar) | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 			bar.Set(resp.Completed) | 
					
						
							|  |  |  | 		} else if status != resp.Status { | 
					
						
							| 
									
										
										
										
											2023-11-20 02:43:21 +08:00
										 |  |  | 			if spinner != nil { | 
					
						
							|  |  |  | 				spinner.Stop() | 
					
						
							|  |  |  | 			} | 
					
						
							| 
									
										
										
										
											2023-11-15 08:33:24 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 			status = resp.Status | 
					
						
							|  |  |  | 			spinner = progress.NewSpinner(status) | 
					
						
							|  |  |  | 			p.Add(status, spinner) | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-07-17 08:02:22 +08:00
										 |  |  | 		return nil | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-11-15 08:33:24 +08:00
										 |  |  | 	request := api.PushRequest{Name: args[0], Insecure: insecure} | 
					
						
							| 
									
										
										
										
											2023-10-13 06:56:40 +08:00
										 |  |  | 	if err := client.Push(cmd.Context(), &request, fn); err != nil { | 
					
						
							| 
									
										
										
										
											2024-05-01 02:02:08 +08:00
										 |  |  | 		if spinner != nil { | 
					
						
							|  |  |  | 			spinner.Stop() | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 		if strings.Contains(err.Error(), "access denied") { | 
					
						
							|  |  |  | 			return errors.New("you are not authorized to push to this namespace, create the model under a namespace you own") | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 		host := model.ParseName(args[0]).Host | 
					
						
							|  |  |  | 		isOllamaHost := strings.HasSuffix(host, ".ollama.ai") || strings.HasSuffix(host, ".ollama.com") | 
					
						
							|  |  |  | 		if strings.Contains(err.Error(), errtypes.UnknownOllamaKeyErrMsg) && isOllamaHost { | 
					
						
							|  |  |  | 			// the user has not added their ollama key to ollama.com
 | 
					
						
							|  |  |  | 			// re-throw an error with a more user-friendly message
 | 
					
						
							|  |  |  | 			return errFromUnknownKey(err) | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-11-15 08:33:24 +08:00
										 |  |  | 		return err | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-11-16 08:59:49 +08:00
										 |  |  | 	spinner.Stop() | 
					
						
							| 
									
										
										
										
											2023-11-15 08:33:24 +08:00
										 |  |  | 	return nil | 
					
						
							| 
									
										
										
										
											2023-07-17 08:02:22 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-07-21 07:09:23 +08:00
										 |  |  | func ListHandler(cmd *cobra.Command, args []string) error { | 
					
						
							| 
									
										
										
										
											2023-10-10 03:18:26 +08:00
										 |  |  | 	client, err := api.ClientFromEnvironment() | 
					
						
							| 
									
										
										
										
											2023-08-16 23:03:48 +08:00
										 |  |  | 	if err != nil { | 
					
						
							|  |  |  | 		return err | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2023-07-19 00:09:45 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-10-13 06:56:40 +08:00
										 |  |  | 	models, err := client.List(cmd.Context()) | 
					
						
							| 
									
										
										
										
											2023-07-19 00:09:45 +08:00
										 |  |  | 	if err != nil { | 
					
						
							|  |  |  | 		return err | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	var data [][]string | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	for _, m := range models.Models { | 
					
						
							| 
									
										
										
										
											2023-07-19 05:01:19 +08:00
										 |  |  | 		if len(args) == 0 || strings.HasPrefix(m.Name, args[0]) { | 
					
						
							| 
									
										
										
										
											2023-11-15 06:57:41 +08:00
										 |  |  | 			data = append(data, []string{m.Name, m.Digest[:12], format.HumanBytes(m.Size), format.HumanTime(m.ModifiedAt, "Never")}) | 
					
						
							| 
									
										
										
										
											2023-07-19 05:01:19 +08:00
										 |  |  | 		} | 
					
						
							| 
									
										
										
										
											2023-07-19 00:09:45 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	table := tablewriter.NewWriter(os.Stdout) | 
					
						
							| 
									
										
										
										
											2023-08-29 11:50:24 +08:00
										 |  |  | 	table.SetHeader([]string{"NAME", "ID", "SIZE", "MODIFIED"}) | 
					
						
							| 
									
										
										
										
											2023-07-19 00:09:45 +08:00
										 |  |  | 	table.SetHeaderAlignment(tablewriter.ALIGN_LEFT) | 
					
						
							|  |  |  | 	table.SetAlignment(tablewriter.ALIGN_LEFT) | 
					
						
							|  |  |  | 	table.SetHeaderLine(false) | 
					
						
							|  |  |  | 	table.SetBorder(false) | 
					
						
							|  |  |  | 	table.SetNoWhiteSpace(true) | 
					
						
							|  |  |  | 	table.SetTablePadding("\t") | 
					
						
							|  |  |  | 	table.AppendBulk(data) | 
					
						
							|  |  |  | 	table.Render() | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	return nil | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-05-14 08:17:36 +08:00
										 |  |  | func ListRunningHandler(cmd *cobra.Command, args []string) error { | 
					
						
							|  |  |  | 	client, err := api.ClientFromEnvironment() | 
					
						
							|  |  |  | 	if err != nil { | 
					
						
							|  |  |  | 		return err | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	models, err := client.ListRunning(cmd.Context()) | 
					
						
							|  |  |  | 	if err != nil { | 
					
						
							|  |  |  | 		return err | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	var data [][]string | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	for _, m := range models.Models { | 
					
						
							|  |  |  | 		if len(args) == 0 || strings.HasPrefix(m.Name, args[0]) { | 
					
						
							|  |  |  | 			var procStr string | 
					
						
							|  |  |  | 			switch { | 
					
						
							|  |  |  | 			case m.SizeVRAM == 0: | 
					
						
							|  |  |  | 				procStr = "100% CPU" | 
					
						
							|  |  |  | 			case m.SizeVRAM == m.Size: | 
					
						
							|  |  |  | 				procStr = "100% GPU" | 
					
						
							|  |  |  | 			case m.SizeVRAM > m.Size || m.Size == 0: | 
					
						
							|  |  |  | 				procStr = "Unknown" | 
					
						
							|  |  |  | 			default: | 
					
						
							|  |  |  | 				sizeCPU := m.Size - m.SizeVRAM | 
					
						
							|  |  |  | 				cpuPercent := math.Round(float64(sizeCPU) / float64(m.Size) * 100) | 
					
						
							|  |  |  | 				procStr = fmt.Sprintf("%d%%/%d%% CPU/GPU", int(cpuPercent), int(100-cpuPercent)) | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 			data = append(data, []string{m.Name, m.Digest[:12], format.HumanBytes(m.Size), procStr, format.HumanTime(m.ExpiresAt, "Never")}) | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	table := tablewriter.NewWriter(os.Stdout) | 
					
						
							|  |  |  | 	table.SetHeader([]string{"NAME", "ID", "SIZE", "PROCESSOR", "UNTIL"}) | 
					
						
							|  |  |  | 	table.SetHeaderAlignment(tablewriter.ALIGN_LEFT) | 
					
						
							|  |  |  | 	table.SetAlignment(tablewriter.ALIGN_LEFT) | 
					
						
							|  |  |  | 	table.SetHeaderLine(false) | 
					
						
							|  |  |  | 	table.SetBorder(false) | 
					
						
							|  |  |  | 	table.SetNoWhiteSpace(true) | 
					
						
							|  |  |  | 	table.SetTablePadding("\t") | 
					
						
							|  |  |  | 	table.AppendBulk(data) | 
					
						
							|  |  |  | 	table.Render() | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	return nil | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-07-21 07:09:23 +08:00
										 |  |  | func DeleteHandler(cmd *cobra.Command, args []string) error { | 
					
						
							| 
									
										
										
										
											2023-10-10 03:18:26 +08:00
										 |  |  | 	client, err := api.ClientFromEnvironment() | 
					
						
							| 
									
										
										
										
											2023-08-16 23:03:48 +08:00
										 |  |  | 	if err != nil { | 
					
						
							|  |  |  | 		return err | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2023-07-21 07:09:23 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-08-26 15:47:56 +08:00
										 |  |  | 	for _, name := range args { | 
					
						
							|  |  |  | 		req := api.DeleteRequest{Name: name} | 
					
						
							| 
									
										
										
										
											2023-10-13 06:56:40 +08:00
										 |  |  | 		if err := client.Delete(cmd.Context(), &req); err != nil { | 
					
						
							| 
									
										
										
										
											2023-08-26 15:47:56 +08:00
										 |  |  | 			return err | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 		fmt.Printf("deleted '%s'\n", name) | 
					
						
							| 
									
										
										
										
											2023-07-21 07:09:23 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | 	return nil | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-09-07 02:04:17 +08:00
										 |  |  | func ShowHandler(cmd *cobra.Command, args []string) error { | 
					
						
							| 
									
										
										
										
											2023-10-10 03:18:26 +08:00
										 |  |  | 	client, err := api.ClientFromEnvironment() | 
					
						
							| 
									
										
										
										
											2023-09-07 02:04:17 +08:00
										 |  |  | 	if err != nil { | 
					
						
							|  |  |  | 		return err | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	license, errLicense := cmd.Flags().GetBool("license") | 
					
						
							|  |  |  | 	modelfile, errModelfile := cmd.Flags().GetBool("modelfile") | 
					
						
							|  |  |  | 	parameters, errParams := cmd.Flags().GetBool("parameters") | 
					
						
							|  |  |  | 	system, errSystem := cmd.Flags().GetBool("system") | 
					
						
							|  |  |  | 	template, errTemplate := cmd.Flags().GetBool("template") | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	for _, boolErr := range []error{errLicense, errModelfile, errParams, errSystem, errTemplate} { | 
					
						
							|  |  |  | 		if boolErr != nil { | 
					
						
							|  |  |  | 			return errors.New("error retrieving flags") | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	flagsSet := 0 | 
					
						
							|  |  |  | 	showType := "" | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if license { | 
					
						
							|  |  |  | 		flagsSet++ | 
					
						
							|  |  |  | 		showType = "license" | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if modelfile { | 
					
						
							|  |  |  | 		flagsSet++ | 
					
						
							|  |  |  | 		showType = "modelfile" | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if parameters { | 
					
						
							|  |  |  | 		flagsSet++ | 
					
						
							|  |  |  | 		showType = "parameters" | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if system { | 
					
						
							|  |  |  | 		flagsSet++ | 
					
						
							|  |  |  | 		showType = "system" | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if template { | 
					
						
							|  |  |  | 		flagsSet++ | 
					
						
							|  |  |  | 		showType = "template" | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if flagsSet > 1 { | 
					
						
							| 
									
										
										
										
											2023-09-07 04:38:49 +08:00
										 |  |  | 		return errors.New("only one of '--license', '--modelfile', '--parameters', '--system', or '--template' can be specified") | 
					
						
							| 
									
										
										
										
											2024-06-20 05:19:02 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if flagsSet == 1 { | 
					
						
							|  |  |  | 		req := api.ShowRequest{Name: args[0]} | 
					
						
							|  |  |  | 		resp, err := client.Show(cmd.Context(), &req) | 
					
						
							|  |  |  | 		if err != nil { | 
					
						
							|  |  |  | 			return err | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		switch showType { | 
					
						
							|  |  |  | 		case "license": | 
					
						
							|  |  |  | 			fmt.Println(resp.License) | 
					
						
							|  |  |  | 		case "modelfile": | 
					
						
							|  |  |  | 			fmt.Println(resp.Modelfile) | 
					
						
							|  |  |  | 		case "parameters": | 
					
						
							|  |  |  | 			fmt.Println(resp.Parameters) | 
					
						
							|  |  |  | 		case "system": | 
					
						
							|  |  |  | 			fmt.Println(resp.System) | 
					
						
							|  |  |  | 		case "template": | 
					
						
							|  |  |  | 			fmt.Println(resp.Template) | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		return nil | 
					
						
							| 
									
										
										
										
											2023-09-07 02:04:17 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-01-06 07:06:43 +08:00
										 |  |  | 	req := api.ShowRequest{Name: args[0]} | 
					
						
							| 
									
										
										
										
											2023-10-13 06:56:40 +08:00
										 |  |  | 	resp, err := client.Show(cmd.Context(), &req) | 
					
						
							| 
									
										
										
										
											2023-09-07 02:04:17 +08:00
										 |  |  | 	if err != nil { | 
					
						
							|  |  |  | 		return err | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-06-20 05:19:02 +08:00
										 |  |  | 	arch := resp.ModelInfo["general.architecture"].(string) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	modelData := [][]string{ | 
					
						
							|  |  |  | 		{"arch", arch}, | 
					
						
							|  |  |  | 		{"parameters", resp.Details.ParameterSize}, | 
					
						
							|  |  |  | 		{"quantization", resp.Details.QuantizationLevel}, | 
					
						
							|  |  |  | 		{"context length", fmt.Sprintf("%v", resp.ModelInfo[fmt.Sprintf("%s.context_length", arch)].(float64))}, | 
					
						
							|  |  |  | 		{"embedding length", fmt.Sprintf("%v", resp.ModelInfo[fmt.Sprintf("%s.embedding_length", arch)].(float64))}, | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	mainTableData := [][]string{ | 
					
						
							|  |  |  | 		{"Model"}, | 
					
						
							|  |  |  | 		{renderSubTable(modelData, false)}, | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if resp.ProjectorInfo != nil { | 
					
						
							|  |  |  | 		projectorData := [][]string{ | 
					
						
							|  |  |  | 			{"arch", "clip"}, | 
					
						
							|  |  |  | 			{"parameters", format.HumanNumber(uint64(resp.ProjectorInfo["general.parameter_count"].(float64)))}, | 
					
						
							|  |  |  | 			{"projector type", resp.ProjectorInfo["clip.projector_type"].(string)}, | 
					
						
							|  |  |  | 			{"embedding length", fmt.Sprintf("%v", resp.ProjectorInfo["clip.vision.embedding_length"].(float64))}, | 
					
						
							|  |  |  | 			{"projection dimensionality", fmt.Sprintf("%v", resp.ProjectorInfo["clip.vision.projection_dim"].(float64))}, | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		mainTableData = append(mainTableData, | 
					
						
							|  |  |  | 			[]string{"Projector"}, | 
					
						
							|  |  |  | 			[]string{renderSubTable(projectorData, false)}, | 
					
						
							|  |  |  | 		) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if resp.Parameters != "" { | 
					
						
							|  |  |  | 		mainTableData = append(mainTableData, []string{"Parameters"}, []string{formatParams(resp.Parameters)}) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if resp.System != "" { | 
					
						
							|  |  |  | 		mainTableData = append(mainTableData, []string{"System"}, []string{renderSubTable(twoLines(resp.System), true)}) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if resp.License != "" { | 
					
						
							|  |  |  | 		mainTableData = append(mainTableData, []string{"License"}, []string{renderSubTable(twoLines(resp.License), true)}) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	table := tablewriter.NewWriter(os.Stdout) | 
					
						
							|  |  |  | 	table.SetAutoWrapText(false) | 
					
						
							|  |  |  | 	table.SetBorder(false) | 
					
						
							|  |  |  | 	table.SetAlignment(tablewriter.ALIGN_LEFT) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	for _, v := range mainTableData { | 
					
						
							|  |  |  | 		table.Append(v) | 
					
						
							| 
									
										
										
										
											2023-09-07 02:04:17 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-06-20 05:19:02 +08:00
										 |  |  | 	table.Render() | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-09-07 02:04:17 +08:00
										 |  |  | 	return nil | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-06-20 05:19:02 +08:00
										 |  |  | func renderSubTable(data [][]string, file bool) string { | 
					
						
							|  |  |  | 	var buf bytes.Buffer | 
					
						
							|  |  |  | 	table := tablewriter.NewWriter(&buf) | 
					
						
							|  |  |  | 	table.SetAutoWrapText(!file) | 
					
						
							|  |  |  | 	table.SetBorder(false) | 
					
						
							|  |  |  | 	table.SetNoWhiteSpace(true) | 
					
						
							|  |  |  | 	table.SetTablePadding("\t") | 
					
						
							|  |  |  | 	table.SetAlignment(tablewriter.ALIGN_LEFT) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	for _, v := range data { | 
					
						
							|  |  |  | 		table.Append(v) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	table.Render() | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	renderedTable := buf.String() | 
					
						
							|  |  |  | 	lines := strings.Split(renderedTable, "\n") | 
					
						
							|  |  |  | 	for i, line := range lines { | 
					
						
							|  |  |  | 		lines[i] = "\t" + line | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	return strings.Join(lines, "\n") | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | func twoLines(s string) [][]string { | 
					
						
							|  |  |  | 	lines := strings.Split(s, "\n") | 
					
						
							|  |  |  | 	res := [][]string{} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	count := 0 | 
					
						
							|  |  |  | 	for _, line := range lines { | 
					
						
							|  |  |  | 		line = strings.TrimSpace(line) | 
					
						
							|  |  |  | 		if line != "" { | 
					
						
							|  |  |  | 			count++ | 
					
						
							|  |  |  | 			res = append(res, []string{line}) | 
					
						
							|  |  |  | 			if count == 2 { | 
					
						
							|  |  |  | 				return res | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	return res | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | func formatParams(s string) string { | 
					
						
							|  |  |  | 	lines := strings.Split(s, "\n") | 
					
						
							|  |  |  | 	table := [][]string{} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	for _, line := range lines { | 
					
						
							|  |  |  | 		table = append(table, strings.Fields(line)) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	return renderSubTable(table, false) | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-07-24 23:27:28 +08:00
										 |  |  | func CopyHandler(cmd *cobra.Command, args []string) error { | 
					
						
							| 
									
										
										
										
											2023-10-10 03:18:26 +08:00
										 |  |  | 	client, err := api.ClientFromEnvironment() | 
					
						
							| 
									
										
										
										
											2023-08-16 23:03:48 +08:00
										 |  |  | 	if err != nil { | 
					
						
							|  |  |  | 		return err | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2023-07-24 23:27:28 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	req := api.CopyRequest{Source: args[0], Destination: args[1]} | 
					
						
							| 
									
										
										
										
											2023-10-13 06:56:40 +08:00
										 |  |  | 	if err := client.Copy(cmd.Context(), &req); err != nil { | 
					
						
							| 
									
										
										
										
											2023-07-24 23:27:28 +08:00
										 |  |  | 		return err | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	fmt.Printf("copied '%s' to '%s'\n", args[0], args[1]) | 
					
						
							|  |  |  | 	return nil | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-07-21 07:09:23 +08:00
										 |  |  | func PullHandler(cmd *cobra.Command, args []string) error { | 
					
						
							| 
									
										
										
										
											2023-07-22 06:42:19 +08:00
										 |  |  | 	insecure, err := cmd.Flags().GetBool("insecure") | 
					
						
							|  |  |  | 	if err != nil { | 
					
						
							|  |  |  | 		return err | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-10-10 03:18:26 +08:00
										 |  |  | 	client, err := api.ClientFromEnvironment() | 
					
						
							| 
									
										
										
										
											2023-08-16 23:03:48 +08:00
										 |  |  | 	if err != nil { | 
					
						
							|  |  |  | 		return err | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2023-07-17 08:02:22 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-11-15 08:33:24 +08:00
										 |  |  | 	p := progress.NewProgress(os.Stderr) | 
					
						
							|  |  |  | 	defer p.Stop() | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	bars := make(map[string]*progress.Bar) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-11-20 02:43:21 +08:00
										 |  |  | 	var status string | 
					
						
							|  |  |  | 	var spinner *progress.Spinner | 
					
						
							| 
									
										
										
										
											2023-11-15 08:33:24 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-07-19 09:51:30 +08:00
										 |  |  | 	fn := func(resp api.ProgressResponse) error { | 
					
						
							| 
									
										
										
										
											2023-11-15 08:33:24 +08:00
										 |  |  | 		if resp.Digest != "" { | 
					
						
							| 
									
										
										
										
											2023-11-20 02:43:21 +08:00
										 |  |  | 			if spinner != nil { | 
					
						
							|  |  |  | 				spinner.Stop() | 
					
						
							|  |  |  | 			} | 
					
						
							| 
									
										
										
										
											2023-11-15 08:33:24 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 			bar, ok := bars[resp.Digest] | 
					
						
							|  |  |  | 			if !ok { | 
					
						
							| 
									
										
										
										
											2023-11-21 00:37:17 +08:00
										 |  |  | 				bar = progress.NewBar(fmt.Sprintf("pulling %s...", resp.Digest[7:19]), resp.Total, resp.Completed) | 
					
						
							| 
									
										
										
										
											2023-11-15 08:33:24 +08:00
										 |  |  | 				bars[resp.Digest] = bar | 
					
						
							|  |  |  | 				p.Add(resp.Digest, bar) | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 			bar.Set(resp.Completed) | 
					
						
							|  |  |  | 		} else if status != resp.Status { | 
					
						
							| 
									
										
										
										
											2023-11-20 02:43:21 +08:00
										 |  |  | 			if spinner != nil { | 
					
						
							|  |  |  | 				spinner.Stop() | 
					
						
							|  |  |  | 			} | 
					
						
							| 
									
										
										
										
											2023-11-15 08:33:24 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 			status = resp.Status | 
					
						
							|  |  |  | 			spinner = progress.NewSpinner(status) | 
					
						
							|  |  |  | 			p.Add(status, spinner) | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-07-17 08:02:22 +08:00
										 |  |  | 		return nil | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2023-07-07 22:22:37 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-11-15 08:33:24 +08:00
										 |  |  | 	request := api.PullRequest{Name: args[0], Insecure: insecure} | 
					
						
							| 
									
										
										
										
											2023-10-13 06:56:40 +08:00
										 |  |  | 	if err := client.Pull(cmd.Context(), &request, fn); err != nil { | 
					
						
							| 
									
										
										
										
											2023-11-15 08:33:24 +08:00
										 |  |  | 		return err | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	return nil | 
					
						
							| 
									
										
										
										
											2023-07-07 04:49:31 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-12-05 08:35:29 +08:00
										 |  |  | type generateContextKey string | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-01-13 04:05:52 +08:00
										 |  |  | type runOptions struct { | 
					
						
							| 
									
										
										
										
											2024-01-26 04:12:36 +08:00
										 |  |  | 	Model       string | 
					
						
							|  |  |  | 	ParentModel string | 
					
						
							|  |  |  | 	Prompt      string | 
					
						
							|  |  |  | 	Messages    []api.Message | 
					
						
							|  |  |  | 	WordWrap    bool | 
					
						
							|  |  |  | 	Format      string | 
					
						
							|  |  |  | 	System      string | 
					
						
							|  |  |  | 	Template    string | 
					
						
							|  |  |  | 	Images      []api.ImageData | 
					
						
							|  |  |  | 	Options     map[string]interface{} | 
					
						
							|  |  |  | 	MultiModal  bool | 
					
						
							| 
									
										
										
										
											2024-05-14 08:17:36 +08:00
										 |  |  | 	KeepAlive   *api.Duration | 
					
						
							| 
									
										
										
										
											2023-11-30 01:56:42 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-01-13 04:05:52 +08:00
										 |  |  | type displayResponseState struct { | 
					
						
							|  |  |  | 	lineLength int | 
					
						
							|  |  |  | 	wordBuffer string | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | func displayResponse(content string, wordWrap bool, state *displayResponseState) { | 
					
						
							|  |  |  | 	termWidth, _, _ := term.GetSize(int(os.Stdout.Fd())) | 
					
						
							|  |  |  | 	if wordWrap && termWidth >= 10 { | 
					
						
							|  |  |  | 		for _, ch := range content { | 
					
						
							| 
									
										
										
										
											2024-05-16 08:24:17 +08:00
										 |  |  | 			if state.lineLength+1 > termWidth-5 { | 
					
						
							|  |  |  | 				if runewidth.StringWidth(state.wordBuffer) > termWidth-10 { | 
					
						
							| 
									
										
										
										
											2024-01-13 04:05:52 +08:00
										 |  |  | 					fmt.Printf("%s%c", state.wordBuffer, ch) | 
					
						
							|  |  |  | 					state.wordBuffer = "" | 
					
						
							|  |  |  | 					state.lineLength = 0 | 
					
						
							|  |  |  | 					continue | 
					
						
							|  |  |  | 				} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 				// backtrack the length of the last word and clear to the end of the line
 | 
					
						
							| 
									
										
										
										
											2024-05-31 01:24:21 +08:00
										 |  |  | 				a := runewidth.StringWidth(state.wordBuffer) | 
					
						
							|  |  |  | 				if a > 0 { | 
					
						
							| 
									
										
										
										
											2024-05-31 01:38:07 +08:00
										 |  |  | 					fmt.Printf("\x1b[%dD", a) | 
					
						
							| 
									
										
										
										
											2024-05-31 01:24:21 +08:00
										 |  |  | 				} | 
					
						
							|  |  |  | 				fmt.Printf("\x1b[K\n") | 
					
						
							| 
									
										
										
										
											2024-01-13 04:05:52 +08:00
										 |  |  | 				fmt.Printf("%s%c", state.wordBuffer, ch) | 
					
						
							| 
									
										
										
										
											2024-05-16 07:29:33 +08:00
										 |  |  | 				chWidth := runewidth.RuneWidth(ch) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 				state.lineLength = runewidth.StringWidth(state.wordBuffer) + chWidth | 
					
						
							| 
									
										
										
										
											2024-01-13 04:05:52 +08:00
										 |  |  | 			} else { | 
					
						
							|  |  |  | 				fmt.Print(string(ch)) | 
					
						
							| 
									
										
										
										
											2024-05-16 07:29:33 +08:00
										 |  |  | 				state.lineLength += runewidth.RuneWidth(ch) | 
					
						
							|  |  |  | 				if runewidth.RuneWidth(ch) >= 2 { | 
					
						
							|  |  |  | 					state.wordBuffer = "" | 
					
						
							|  |  |  | 					continue | 
					
						
							| 
									
										
										
										
											2024-05-16 08:24:17 +08:00
										 |  |  | 				} | 
					
						
							| 
									
										
										
										
											2024-01-13 04:05:52 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 				switch ch { | 
					
						
							|  |  |  | 				case ' ': | 
					
						
							|  |  |  | 					state.wordBuffer = "" | 
					
						
							|  |  |  | 				case '\n': | 
					
						
							|  |  |  | 					state.lineLength = 0 | 
					
						
							|  |  |  | 				default: | 
					
						
							|  |  |  | 					state.wordBuffer += string(ch) | 
					
						
							|  |  |  | 				} | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	} else { | 
					
						
							|  |  |  | 		fmt.Printf("%s%s", state.wordBuffer, content) | 
					
						
							|  |  |  | 		if len(state.wordBuffer) > 0 { | 
					
						
							|  |  |  | 			state.wordBuffer = "" | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | func chat(cmd *cobra.Command, opts runOptions) (*api.Message, error) { | 
					
						
							|  |  |  | 	client, err := api.ClientFromEnvironment() | 
					
						
							|  |  |  | 	if err != nil { | 
					
						
							|  |  |  | 		return nil, err | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	p := progress.NewProgress(os.Stderr) | 
					
						
							|  |  |  | 	defer p.StopAndClear() | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	spinner := progress.NewSpinner("") | 
					
						
							|  |  |  | 	p.Add("", spinner) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	cancelCtx, cancel := context.WithCancel(cmd.Context()) | 
					
						
							|  |  |  | 	defer cancel() | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	sigChan := make(chan os.Signal, 1) | 
					
						
							|  |  |  | 	signal.Notify(sigChan, syscall.SIGINT) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	go func() { | 
					
						
							|  |  |  | 		<-sigChan | 
					
						
							|  |  |  | 		cancel() | 
					
						
							|  |  |  | 	}() | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	var state *displayResponseState = &displayResponseState{} | 
					
						
							|  |  |  | 	var latest api.ChatResponse | 
					
						
							|  |  |  | 	var fullResponse strings.Builder | 
					
						
							|  |  |  | 	var role string | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	fn := func(response api.ChatResponse) error { | 
					
						
							|  |  |  | 		p.StopAndClear() | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		latest = response | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		role = response.Message.Role | 
					
						
							|  |  |  | 		content := response.Message.Content | 
					
						
							|  |  |  | 		fullResponse.WriteString(content) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		displayResponse(content, opts.WordWrap, state) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		return nil | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	req := &api.ChatRequest{ | 
					
						
							|  |  |  | 		Model:    opts.Model, | 
					
						
							|  |  |  | 		Messages: opts.Messages, | 
					
						
							|  |  |  | 		Format:   opts.Format, | 
					
						
							|  |  |  | 		Options:  opts.Options, | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-05-14 08:17:36 +08:00
										 |  |  | 	if opts.KeepAlive != nil { | 
					
						
							|  |  |  | 		req.KeepAlive = opts.KeepAlive | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-01-13 04:05:52 +08:00
										 |  |  | 	if err := client.Chat(cancelCtx, req, fn); err != nil { | 
					
						
							|  |  |  | 		if errors.Is(err, context.Canceled) { | 
					
						
							|  |  |  | 			return nil, nil | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 		return nil, err | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if len(opts.Messages) > 0 { | 
					
						
							|  |  |  | 		fmt.Println() | 
					
						
							|  |  |  | 		fmt.Println() | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	verbose, err := cmd.Flags().GetBool("verbose") | 
					
						
							|  |  |  | 	if err != nil { | 
					
						
							|  |  |  | 		return nil, err | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if verbose { | 
					
						
							|  |  |  | 		latest.Summary() | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	return &api.Message{Role: role, Content: fullResponse.String()}, nil | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | func generate(cmd *cobra.Command, opts runOptions) error { | 
					
						
							| 
									
										
										
										
											2023-10-10 03:18:26 +08:00
										 |  |  | 	client, err := api.ClientFromEnvironment() | 
					
						
							| 
									
										
										
										
											2023-09-19 03:26:56 +08:00
										 |  |  | 	if err != nil { | 
					
						
							| 
									
										
										
										
											2023-12-05 08:35:29 +08:00
										 |  |  | 		return err | 
					
						
							| 
									
										
										
										
											2023-09-19 03:26:56 +08:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2023-07-08 01:12:58 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-11-15 08:58:51 +08:00
										 |  |  | 	p := progress.NewProgress(os.Stderr) | 
					
						
							| 
									
										
										
										
											2023-11-20 13:49:08 +08:00
										 |  |  | 	defer p.StopAndClear() | 
					
						
							| 
									
										
										
										
											2023-12-05 08:35:29 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-11-15 08:58:51 +08:00
										 |  |  | 	spinner := progress.NewSpinner("") | 
					
						
							|  |  |  | 	p.Add("", spinner) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-12-05 08:35:29 +08:00
										 |  |  | 	var latest api.GenerateResponse | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	generateContext, ok := cmd.Context().Value(generateContextKey("context")).([]int) | 
					
						
							|  |  |  | 	if !ok { | 
					
						
							|  |  |  | 		generateContext = []int{} | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-10-13 06:56:40 +08:00
										 |  |  | 	ctx, cancel := context.WithCancel(cmd.Context()) | 
					
						
							| 
									
										
										
										
											2023-09-29 08:13:01 +08:00
										 |  |  | 	defer cancel() | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	sigChan := make(chan os.Signal, 1) | 
					
						
							|  |  |  | 	signal.Notify(sigChan, syscall.SIGINT) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	go func() { | 
					
						
							|  |  |  | 		<-sigChan | 
					
						
							|  |  |  | 		cancel() | 
					
						
							|  |  |  | 	}() | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-01-13 04:05:52 +08:00
										 |  |  | 	var state *displayResponseState = &displayResponseState{} | 
					
						
							| 
									
										
										
										
											2023-09-23 04:36:08 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-12-05 08:35:29 +08:00
										 |  |  | 	fn := func(response api.GenerateResponse) error { | 
					
						
							| 
									
										
										
										
											2023-11-15 08:58:51 +08:00
										 |  |  | 		p.StopAndClear() | 
					
						
							| 
									
										
										
										
											2023-12-05 08:35:29 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-09-19 03:26:56 +08:00
										 |  |  | 		latest = response | 
					
						
							| 
									
										
										
										
											2024-01-13 04:05:52 +08:00
										 |  |  | 		content := response.Response | 
					
						
							| 
									
										
										
										
											2023-07-13 09:18:06 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-01-13 04:05:52 +08:00
										 |  |  | 		displayResponse(content, opts.WordWrap, state) | 
					
						
							| 
									
										
										
										
											2023-09-23 04:36:08 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-09-19 03:26:56 +08:00
										 |  |  | 		return nil | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2023-07-08 05:04:43 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-02-02 13:33:06 +08:00
										 |  |  | 	if opts.MultiModal { | 
					
						
							|  |  |  | 		opts.Prompt, opts.Images, err = extractFileData(opts.Prompt) | 
					
						
							|  |  |  | 		if err != nil { | 
					
						
							|  |  |  | 			return err | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-10-13 06:56:40 +08:00
										 |  |  | 	request := api.GenerateRequest{ | 
					
						
							| 
									
										
										
										
											2024-05-15 06:17:04 +08:00
										 |  |  | 		Model:     opts.Model, | 
					
						
							|  |  |  | 		Prompt:    opts.Prompt, | 
					
						
							|  |  |  | 		Context:   generateContext, | 
					
						
							|  |  |  | 		Images:    opts.Images, | 
					
						
							|  |  |  | 		Format:    opts.Format, | 
					
						
							|  |  |  | 		System:    opts.System, | 
					
						
							|  |  |  | 		Template:  opts.Template, | 
					
						
							|  |  |  | 		Options:   opts.Options, | 
					
						
							|  |  |  | 		KeepAlive: opts.KeepAlive, | 
					
						
							| 
									
										
										
										
											2023-10-13 06:56:40 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if err := client.Generate(ctx, &request, fn); err != nil { | 
					
						
							| 
									
										
										
										
											2024-01-06 04:22:32 +08:00
										 |  |  | 		if errors.Is(err, context.Canceled) { | 
					
						
							| 
									
										
										
										
											2023-12-05 08:35:29 +08:00
										 |  |  | 			return nil | 
					
						
							| 
									
										
										
										
											2023-07-08 05:04:43 +08:00
										 |  |  | 		} | 
					
						
							| 
									
										
										
										
											2024-01-06 04:22:32 +08:00
										 |  |  | 		return err | 
					
						
							| 
									
										
										
										
											2023-09-19 03:26:56 +08:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2024-01-06 04:22:32 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-12-05 08:35:29 +08:00
										 |  |  | 	if opts.Prompt != "" { | 
					
						
							| 
									
										
										
										
											2023-07-08 01:12:58 +08:00
										 |  |  | 		fmt.Println() | 
					
						
							|  |  |  | 		fmt.Println() | 
					
						
							| 
									
										
										
										
											2023-09-19 03:26:56 +08:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2023-07-13 09:18:06 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-12-05 08:35:29 +08:00
										 |  |  | 	if !latest.Done { | 
					
						
							|  |  |  | 		return nil | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-09-19 03:26:56 +08:00
										 |  |  | 	verbose, err := cmd.Flags().GetBool("verbose") | 
					
						
							|  |  |  | 	if err != nil { | 
					
						
							| 
									
										
										
										
											2023-12-05 08:35:29 +08:00
										 |  |  | 		return err | 
					
						
							| 
									
										
										
										
											2023-09-19 03:26:56 +08:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2023-07-19 02:59:42 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-09-19 03:26:56 +08:00
										 |  |  | 	if verbose { | 
					
						
							|  |  |  | 		latest.Summary() | 
					
						
							| 
									
										
										
										
											2023-07-08 01:12:58 +08:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2023-07-07 04:49:31 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-12-12 05:56:22 +08:00
										 |  |  | 	ctx = context.WithValue(cmd.Context(), generateContextKey("context"), latest.Context) | 
					
						
							|  |  |  | 	cmd.SetContext(ctx) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-12-05 08:35:29 +08:00
										 |  |  | 	return nil | 
					
						
							| 
									
										
										
										
											2023-07-07 04:49:31 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-08-11 05:17:53 +08:00
										 |  |  | func RunServer(cmd *cobra.Command, _ []string) error { | 
					
						
							| 
									
										
										
										
											2023-09-21 08:49:48 +08:00
										 |  |  | 	if err := initializeKeypair(); err != nil { | 
					
						
							| 
									
										
										
										
											2023-08-12 01:58:23 +08:00
										 |  |  | 		return err | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-06-13 06:48:16 +08:00
										 |  |  | 	ln, err := net.Listen("tcp", net.JoinHostPort(envconfig.Host.Host, envconfig.Host.Port)) | 
					
						
							| 
									
										
										
										
											2023-08-07 11:34:37 +08:00
										 |  |  | 	if err != nil { | 
					
						
							|  |  |  | 		return err | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2023-07-04 12:47:00 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-05-07 07:01:37 +08:00
										 |  |  | 	err = server.Serve(ln) | 
					
						
							|  |  |  | 	if errors.Is(err, http.ErrServerClosed) { | 
					
						
							|  |  |  | 		return nil | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	return err | 
					
						
							| 
									
										
										
										
											2023-07-04 12:47:00 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-08-12 01:58:23 +08:00
										 |  |  | func initializeKeypair() error { | 
					
						
							|  |  |  | 	home, err := os.UserHomeDir() | 
					
						
							|  |  |  | 	if err != nil { | 
					
						
							|  |  |  | 		return err | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	privKeyPath := filepath.Join(home, ".ollama", "id_ed25519") | 
					
						
							|  |  |  | 	pubKeyPath := filepath.Join(home, ".ollama", "id_ed25519.pub") | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	_, err = os.Stat(privKeyPath) | 
					
						
							|  |  |  | 	if os.IsNotExist(err) { | 
					
						
							|  |  |  | 		fmt.Printf("Couldn't find '%s'. Generating new private key.\n", privKeyPath) | 
					
						
							| 
									
										
										
										
											2024-02-24 08:50:41 +08:00
										 |  |  | 		cryptoPublicKey, cryptoPrivateKey, err := ed25519.GenerateKey(rand.Reader) | 
					
						
							| 
									
										
										
										
											2023-08-12 01:58:23 +08:00
										 |  |  | 		if err != nil { | 
					
						
							|  |  |  | 			return err | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-02-24 08:50:41 +08:00
										 |  |  | 		privateKeyBytes, err := ssh.MarshalPrivateKey(cryptoPrivateKey, "") | 
					
						
							| 
									
										
										
										
											2023-08-12 01:58:23 +08:00
										 |  |  | 		if err != nil { | 
					
						
							|  |  |  | 			return err | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-02-24 08:50:41 +08:00
										 |  |  | 		if err := os.MkdirAll(filepath.Dir(privKeyPath), 0o755); err != nil { | 
					
						
							| 
									
										
										
										
											2023-08-12 06:35:55 +08:00
										 |  |  | 			return fmt.Errorf("could not create directory %w", err) | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-02-24 08:50:41 +08:00
										 |  |  | 		if err := os.WriteFile(privKeyPath, pem.EncodeToMemory(privateKeyBytes), 0o600); err != nil { | 
					
						
							| 
									
										
										
										
											2023-08-12 01:58:23 +08:00
										 |  |  | 			return err | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-02-24 08:50:41 +08:00
										 |  |  | 		sshPublicKey, err := ssh.NewPublicKey(cryptoPublicKey) | 
					
						
							| 
									
										
										
										
											2023-08-12 01:58:23 +08:00
										 |  |  | 		if err != nil { | 
					
						
							|  |  |  | 			return err | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-02-24 08:50:41 +08:00
										 |  |  | 		publicKeyBytes := ssh.MarshalAuthorizedKey(sshPublicKey) | 
					
						
							| 
									
										
										
										
											2023-08-12 01:58:23 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-02-24 08:50:41 +08:00
										 |  |  | 		if err := os.WriteFile(pubKeyPath, publicKeyBytes, 0o644); err != nil { | 
					
						
							| 
									
										
										
										
											2023-08-12 01:58:23 +08:00
										 |  |  | 			return err | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-02-24 08:50:41 +08:00
										 |  |  | 		fmt.Printf("Your new public key is: \n\n%s\n", publicKeyBytes) | 
					
						
							| 
									
										
										
										
											2023-08-12 01:58:23 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | 	return nil | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-10-13 06:56:40 +08:00
										 |  |  | func checkServerHeartbeat(cmd *cobra.Command, _ []string) error { | 
					
						
							| 
									
										
										
										
											2023-10-10 03:18:26 +08:00
										 |  |  | 	client, err := api.ClientFromEnvironment() | 
					
						
							| 
									
										
										
										
											2023-08-16 23:03:48 +08:00
										 |  |  | 	if err != nil { | 
					
						
							|  |  |  | 		return err | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2023-10-13 06:56:40 +08:00
										 |  |  | 	if err := client.Heartbeat(cmd.Context()); err != nil { | 
					
						
							| 
									
										
										
										
											2023-12-27 08:03:45 +08:00
										 |  |  | 		if !strings.Contains(err.Error(), " refused") { | 
					
						
							| 
									
										
										
										
											2023-08-01 05:38:10 +08:00
										 |  |  | 			return err | 
					
						
							|  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2023-12-27 08:03:45 +08:00
										 |  |  | 		if err := startApp(cmd.Context(), client); err != nil { | 
					
						
							|  |  |  | 			return fmt.Errorf("could not connect to ollama app, is it running?") | 
					
						
							| 
									
										
										
										
											2023-08-01 04:25:57 +08:00
										 |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	return nil | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-11-23 01:41:02 +08:00
										 |  |  | func versionHandler(cmd *cobra.Command, _ []string) { | 
					
						
							|  |  |  | 	client, err := api.ClientFromEnvironment() | 
					
						
							|  |  |  | 	if err != nil { | 
					
						
							|  |  |  | 		return | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	serverVersion, err := client.Version(cmd.Context()) | 
					
						
							|  |  |  | 	if err != nil { | 
					
						
							| 
									
										
										
										
											2023-12-02 04:10:27 +08:00
										 |  |  | 		fmt.Println("Warning: could not connect to a running Ollama instance") | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if serverVersion != "" { | 
					
						
							|  |  |  | 		fmt.Printf("ollama version is %s\n", serverVersion) | 
					
						
							| 
									
										
										
										
											2023-11-23 01:41:02 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-10-17 00:57:19 +08:00
										 |  |  | 	if serverVersion != version.Version { | 
					
						
							| 
									
										
										
										
											2023-12-02 04:10:27 +08:00
										 |  |  | 		fmt.Printf("Warning: client version is %s\n", version.Version) | 
					
						
							| 
									
										
										
										
											2023-10-17 00:57:19 +08:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2023-11-23 01:41:02 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-05-25 05:57:15 +08:00
										 |  |  | func appendEnvDocs(cmd *cobra.Command, envs []envconfig.EnvVar) { | 
					
						
							| 
									
										
										
										
											2024-05-19 02:51:57 +08:00
										 |  |  | 	if len(envs) == 0 { | 
					
						
							|  |  |  | 		return | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	envUsage := ` | 
					
						
							| 
									
										
										
										
											2024-03-08 05:57:07 +08:00
										 |  |  | Environment Variables: | 
					
						
							|  |  |  | ` | 
					
						
							| 
									
										
										
										
											2024-05-19 02:51:57 +08:00
										 |  |  | 	for _, e := range envs { | 
					
						
							| 
									
										
										
										
											2024-05-25 05:57:15 +08:00
										 |  |  | 		envUsage += fmt.Sprintf("      %-24s   %s\n", e.Name, e.Description) | 
					
						
							| 
									
										
										
										
											2024-05-19 02:51:57 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	cmd.SetUsageTemplate(cmd.UsageTemplate() + envUsage) | 
					
						
							| 
									
										
										
										
											2024-03-08 05:57:07 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-07-04 03:22:44 +08:00
										 |  |  | func NewCLI() *cobra.Command { | 
					
						
							|  |  |  | 	log.SetFlags(log.LstdFlags | log.Lshortfile) | 
					
						
							| 
									
										
										
										
											2023-11-23 01:41:02 +08:00
										 |  |  | 	cobra.EnableCommandSorting = false | 
					
						
							| 
									
										
										
										
											2023-07-04 03:22:44 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-11-24 14:21:32 +08:00
										 |  |  | 	if runtime.GOOS == "windows" { | 
					
						
							| 
									
										
										
										
											2024-03-12 06:21:57 +08:00
										 |  |  | 		console.ConsoleFromFile(os.Stdin) //nolint:errcheck
 | 
					
						
							| 
									
										
										
										
											2023-11-24 14:21:32 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-07-04 03:22:44 +08:00
										 |  |  | 	rootCmd := &cobra.Command{ | 
					
						
							| 
									
										
										
										
											2023-08-15 02:15:53 +08:00
										 |  |  | 		Use:           "ollama", | 
					
						
							|  |  |  | 		Short:         "Large language model runner", | 
					
						
							|  |  |  | 		SilenceUsage:  true, | 
					
						
							|  |  |  | 		SilenceErrors: true, | 
					
						
							| 
									
										
										
										
											2023-07-04 03:22:44 +08:00
										 |  |  | 		CompletionOptions: cobra.CompletionOptions{ | 
					
						
							|  |  |  | 			DisableDefaultCmd: true, | 
					
						
							|  |  |  | 		}, | 
					
						
							| 
									
										
										
										
											2023-11-23 01:41:02 +08:00
										 |  |  | 		Run: func(cmd *cobra.Command, args []string) { | 
					
						
							|  |  |  | 			if version, _ := cmd.Flags().GetBool("version"); version { | 
					
						
							|  |  |  | 				versionHandler(cmd, args) | 
					
						
							|  |  |  | 				return | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 			cmd.Print(cmd.UsageString()) | 
					
						
							|  |  |  | 		}, | 
					
						
							| 
									
										
										
										
											2023-07-04 03:22:44 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-11-23 01:41:02 +08:00
										 |  |  | 	rootCmd.Flags().BoolP("version", "v", false, "Show version information") | 
					
						
							| 
									
										
										
										
											2023-07-04 03:22:44 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-07-17 08:02:22 +08:00
										 |  |  | 	createCmd := &cobra.Command{ | 
					
						
							| 
									
										
										
										
											2023-08-01 04:25:57 +08:00
										 |  |  | 		Use:     "create MODEL", | 
					
						
							|  |  |  | 		Short:   "Create a model from a Modelfile", | 
					
						
							| 
									
										
										
										
											2023-10-19 02:57:22 +08:00
										 |  |  | 		Args:    cobra.ExactArgs(1), | 
					
						
							| 
									
										
										
										
											2023-08-01 04:25:57 +08:00
										 |  |  | 		PreRunE: checkServerHeartbeat, | 
					
						
							|  |  |  | 		RunE:    CreateHandler, | 
					
						
							| 
									
										
										
										
											2023-07-17 08:02:22 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-05-12 05:47:49 +08:00
										 |  |  | 	createCmd.Flags().StringP("file", "f", "Modelfile", "Name of the Modelfile") | 
					
						
							| 
									
										
										
										
											2024-05-11 04:06:13 +08:00
										 |  |  | 	createCmd.Flags().StringP("quantize", "q", "", "Quantize model to this level (e.g. q4_0)") | 
					
						
							| 
									
										
										
										
											2023-07-17 08:02:22 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-09-07 02:04:17 +08:00
										 |  |  | 	showCmd := &cobra.Command{ | 
					
						
							|  |  |  | 		Use:     "show MODEL", | 
					
						
							|  |  |  | 		Short:   "Show information for a model", | 
					
						
							| 
									
										
										
										
											2023-10-19 02:57:22 +08:00
										 |  |  | 		Args:    cobra.ExactArgs(1), | 
					
						
							| 
									
										
										
										
											2023-09-07 02:04:17 +08:00
										 |  |  | 		PreRunE: checkServerHeartbeat, | 
					
						
							|  |  |  | 		RunE:    ShowHandler, | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	showCmd.Flags().Bool("license", false, "Show license of a model") | 
					
						
							|  |  |  | 	showCmd.Flags().Bool("modelfile", false, "Show Modelfile of a model") | 
					
						
							|  |  |  | 	showCmd.Flags().Bool("parameters", false, "Show parameters of a model") | 
					
						
							|  |  |  | 	showCmd.Flags().Bool("template", false, "Show template of a model") | 
					
						
							| 
									
										
										
										
											2023-12-13 03:43:19 +08:00
										 |  |  | 	showCmd.Flags().Bool("system", false, "Show system message of a model") | 
					
						
							| 
									
										
										
										
											2023-09-07 02:04:17 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-07-04 03:22:44 +08:00
										 |  |  | 	runCmd := &cobra.Command{ | 
					
						
							| 
									
										
										
										
											2024-04-16 07:58:00 +08:00
										 |  |  | 		Use:     "run MODEL [PROMPT]", | 
					
						
							|  |  |  | 		Short:   "Run a model", | 
					
						
							|  |  |  | 		Args:    cobra.MinimumNArgs(1), | 
					
						
							|  |  |  | 		PreRunE: checkServerHeartbeat, | 
					
						
							|  |  |  | 		RunE:    RunHandler, | 
					
						
							| 
									
										
										
										
											2023-07-04 03:22:44 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-05-14 08:17:36 +08:00
										 |  |  | 	runCmd.Flags().String("keepalive", "", "Duration to keep a model loaded (e.g. 5m)") | 
					
						
							| 
									
										
										
										
											2023-07-13 09:18:06 +08:00
										 |  |  | 	runCmd.Flags().Bool("verbose", false, "Show timings for response") | 
					
						
							| 
									
										
										
										
											2023-08-22 12:56:56 +08:00
										 |  |  | 	runCmd.Flags().Bool("insecure", false, "Use an insecure registry") | 
					
						
							| 
									
										
										
										
											2023-09-23 04:36:08 +08:00
										 |  |  | 	runCmd.Flags().Bool("nowordwrap", false, "Don't wrap words to the next line automatically") | 
					
						
							| 
									
										
										
										
											2023-11-14 10:54:02 +08:00
										 |  |  | 	runCmd.Flags().String("format", "", "Response format (e.g. json)") | 
					
						
							| 
									
										
										
										
											2023-07-04 03:22:44 +08:00
										 |  |  | 	serveCmd := &cobra.Command{ | 
					
						
							|  |  |  | 		Use:     "serve", | 
					
						
							|  |  |  | 		Aliases: []string{"start"}, | 
					
						
							|  |  |  | 		Short:   "Start ollama", | 
					
						
							| 
									
										
										
										
											2023-10-19 02:57:22 +08:00
										 |  |  | 		Args:    cobra.ExactArgs(0), | 
					
						
							| 
									
										
										
										
											2023-07-07 04:49:31 +08:00
										 |  |  | 		RunE:    RunServer, | 
					
						
							| 
									
										
										
										
											2023-07-04 03:22:44 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-07-17 08:02:22 +08:00
										 |  |  | 	pullCmd := &cobra.Command{ | 
					
						
							| 
									
										
										
										
											2023-08-01 04:25:57 +08:00
										 |  |  | 		Use:     "pull MODEL", | 
					
						
							|  |  |  | 		Short:   "Pull a model from a registry", | 
					
						
							| 
									
										
										
										
											2023-10-19 02:57:22 +08:00
										 |  |  | 		Args:    cobra.ExactArgs(1), | 
					
						
							| 
									
										
										
										
											2023-08-01 04:25:57 +08:00
										 |  |  | 		PreRunE: checkServerHeartbeat, | 
					
						
							|  |  |  | 		RunE:    PullHandler, | 
					
						
							| 
									
										
										
										
											2023-07-17 08:02:22 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-07-22 06:42:19 +08:00
										 |  |  | 	pullCmd.Flags().Bool("insecure", false, "Use an insecure registry") | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-07-17 08:02:22 +08:00
										 |  |  | 	pushCmd := &cobra.Command{ | 
					
						
							| 
									
										
										
										
											2023-08-01 04:25:57 +08:00
										 |  |  | 		Use:     "push MODEL", | 
					
						
							|  |  |  | 		Short:   "Push a model to a registry", | 
					
						
							| 
									
										
										
										
											2023-10-19 02:57:22 +08:00
										 |  |  | 		Args:    cobra.ExactArgs(1), | 
					
						
							| 
									
										
										
										
											2023-08-01 04:25:57 +08:00
										 |  |  | 		PreRunE: checkServerHeartbeat, | 
					
						
							|  |  |  | 		RunE:    PushHandler, | 
					
						
							| 
									
										
										
										
											2023-07-17 08:02:22 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-07-22 06:42:19 +08:00
										 |  |  | 	pushCmd.Flags().Bool("insecure", false, "Use an insecure registry") | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-07-19 00:09:45 +08:00
										 |  |  | 	listCmd := &cobra.Command{ | 
					
						
							| 
									
										
										
										
											2023-07-22 06:42:19 +08:00
										 |  |  | 		Use:     "list", | 
					
						
							| 
									
										
										
										
											2023-07-21 06:28:27 +08:00
										 |  |  | 		Aliases: []string{"ls"}, | 
					
						
							| 
									
										
										
										
											2023-07-22 06:42:19 +08:00
										 |  |  | 		Short:   "List models", | 
					
						
							| 
									
										
										
										
											2023-08-01 04:25:57 +08:00
										 |  |  | 		PreRunE: checkServerHeartbeat, | 
					
						
							| 
									
										
										
										
											2023-07-22 06:42:19 +08:00
										 |  |  | 		RunE:    ListHandler, | 
					
						
							| 
									
										
										
										
											2023-07-21 07:09:23 +08:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2024-05-14 08:17:36 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	psCmd := &cobra.Command{ | 
					
						
							|  |  |  | 		Use:     "ps", | 
					
						
							|  |  |  | 		Short:   "List running models", | 
					
						
							|  |  |  | 		PreRunE: checkServerHeartbeat, | 
					
						
							|  |  |  | 		RunE:    ListRunningHandler, | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-07-24 23:27:28 +08:00
										 |  |  | 	copyCmd := &cobra.Command{ | 
					
						
							| 
									
										
										
										
											2024-05-02 03:39:05 +08:00
										 |  |  | 		Use:     "cp SOURCE DESTINATION", | 
					
						
							| 
									
										
										
										
											2023-08-01 04:25:57 +08:00
										 |  |  | 		Short:   "Copy a model", | 
					
						
							| 
									
										
										
										
											2023-10-19 02:57:22 +08:00
										 |  |  | 		Args:    cobra.ExactArgs(2), | 
					
						
							| 
									
										
										
										
											2023-08-01 04:25:57 +08:00
										 |  |  | 		PreRunE: checkServerHeartbeat, | 
					
						
							|  |  |  | 		RunE:    CopyHandler, | 
					
						
							| 
									
										
										
										
											2023-07-24 23:27:28 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-07-21 07:09:23 +08:00
										 |  |  | 	deleteCmd := &cobra.Command{ | 
					
						
							| 
									
										
										
										
											2023-10-19 02:57:22 +08:00
										 |  |  | 		Use:     "rm MODEL [MODEL...]", | 
					
						
							| 
									
										
										
										
											2023-08-01 04:25:57 +08:00
										 |  |  | 		Short:   "Remove a model", | 
					
						
							|  |  |  | 		Args:    cobra.MinimumNArgs(1), | 
					
						
							|  |  |  | 		PreRunE: checkServerHeartbeat, | 
					
						
							|  |  |  | 		RunE:    DeleteHandler, | 
					
						
							| 
									
										
										
										
											2023-07-19 00:09:45 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-05-25 05:57:15 +08:00
										 |  |  | 	envVars := envconfig.AsMap() | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	envs := []envconfig.EnvVar{envVars["OLLAMA_HOST"]} | 
					
						
							| 
									
										
										
										
											2024-05-19 02:51:57 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-03-08 05:57:07 +08:00
										 |  |  | 	for _, cmd := range []*cobra.Command{ | 
					
						
							|  |  |  | 		createCmd, | 
					
						
							|  |  |  | 		showCmd, | 
					
						
							|  |  |  | 		runCmd, | 
					
						
							|  |  |  | 		pullCmd, | 
					
						
							|  |  |  | 		pushCmd, | 
					
						
							|  |  |  | 		listCmd, | 
					
						
							| 
									
										
										
										
											2024-05-14 08:17:36 +08:00
										 |  |  | 		psCmd, | 
					
						
							| 
									
										
										
										
											2024-03-08 05:57:07 +08:00
										 |  |  | 		copyCmd, | 
					
						
							|  |  |  | 		deleteCmd, | 
					
						
							| 
									
										
										
										
											2024-05-25 05:57:15 +08:00
										 |  |  | 		serveCmd, | 
					
						
							| 
									
										
										
										
											2024-03-08 05:57:07 +08:00
										 |  |  | 	} { | 
					
						
							| 
									
										
										
										
											2024-05-19 02:51:57 +08:00
										 |  |  | 		switch cmd { | 
					
						
							|  |  |  | 		case runCmd: | 
					
						
							| 
									
										
										
										
											2024-05-25 05:57:15 +08:00
										 |  |  | 			appendEnvDocs(cmd, []envconfig.EnvVar{envVars["OLLAMA_HOST"], envVars["OLLAMA_NOHISTORY"]}) | 
					
						
							|  |  |  | 		case serveCmd: | 
					
						
							|  |  |  | 			appendEnvDocs(cmd, []envconfig.EnvVar{ | 
					
						
							|  |  |  | 				envVars["OLLAMA_DEBUG"], | 
					
						
							|  |  |  | 				envVars["OLLAMA_HOST"], | 
					
						
							|  |  |  | 				envVars["OLLAMA_KEEP_ALIVE"], | 
					
						
							|  |  |  | 				envVars["OLLAMA_MAX_LOADED_MODELS"], | 
					
						
							|  |  |  | 				envVars["OLLAMA_MAX_QUEUE"], | 
					
						
							|  |  |  | 				envVars["OLLAMA_MODELS"], | 
					
						
							|  |  |  | 				envVars["OLLAMA_NUM_PARALLEL"], | 
					
						
							|  |  |  | 				envVars["OLLAMA_NOPRUNE"], | 
					
						
							|  |  |  | 				envVars["OLLAMA_ORIGINS"], | 
					
						
							|  |  |  | 				envVars["OLLAMA_TMPDIR"], | 
					
						
							| 
									
										
										
										
											2024-05-31 00:36:51 +08:00
										 |  |  | 				envVars["OLLAMA_FLASH_ATTENTION"], | 
					
						
							|  |  |  | 				envVars["OLLAMA_LLM_LIBRARY"], | 
					
						
							|  |  |  | 				envVars["OLLAMA_MAX_VRAM"], | 
					
						
							| 
									
										
										
										
											2024-05-25 05:57:15 +08:00
										 |  |  | 			}) | 
					
						
							| 
									
										
										
										
											2024-05-19 02:51:57 +08:00
										 |  |  | 		default: | 
					
						
							|  |  |  | 			appendEnvDocs(cmd, envs) | 
					
						
							|  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2024-03-08 05:57:07 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-07-04 03:22:44 +08:00
										 |  |  | 	rootCmd.AddCommand( | 
					
						
							|  |  |  | 		serveCmd, | 
					
						
							| 
									
										
										
										
											2023-07-17 08:02:22 +08:00
										 |  |  | 		createCmd, | 
					
						
							| 
									
										
										
										
											2023-09-07 02:04:17 +08:00
										 |  |  | 		showCmd, | 
					
						
							| 
									
										
										
										
											2023-07-04 05:14:20 +08:00
										 |  |  | 		runCmd, | 
					
						
							| 
									
										
										
										
											2023-07-17 08:02:22 +08:00
										 |  |  | 		pullCmd, | 
					
						
							|  |  |  | 		pushCmd, | 
					
						
							| 
									
										
										
										
											2023-07-19 00:09:45 +08:00
										 |  |  | 		listCmd, | 
					
						
							| 
									
										
										
										
											2024-05-14 08:17:36 +08:00
										 |  |  | 		psCmd, | 
					
						
							| 
									
										
										
										
											2023-07-24 23:27:28 +08:00
										 |  |  | 		copyCmd, | 
					
						
							| 
									
										
										
										
											2023-07-21 07:09:23 +08:00
										 |  |  | 		deleteCmd, | 
					
						
							| 
									
										
										
										
											2023-07-04 03:22:44 +08:00
										 |  |  | 	) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	return rootCmd | 
					
						
							|  |  |  | } |