From 95cc9a11db1f977206f56337c5ec26ff0d4484e3 Mon Sep 17 00:00:00 2001 From: Jeffrey Morgan Date: Sun, 16 Jul 2023 10:53:37 -0700 Subject: [PATCH] fix go warnings --- server/images.go | 28 +++++++++++----------------- server/models.go | 48 +----------------------------------------------- server/routes.go | 15 --------------- 3 files changed, 12 insertions(+), 79 deletions(-) diff --git a/server/images.go b/server/images.go index 9066065ff..e10e95302 100644 --- a/server/images.go +++ b/server/images.go @@ -7,7 +7,6 @@ import ( "encoding/json" "fmt" "io" - "io/ioutil" "log" "net/http" "os" @@ -102,11 +101,7 @@ func GetModel(name string) (*Model, error) { case "application/vnd.ollama.image.model": model.ModelPath = filename case "application/vnd.ollama.image.prompt": - f, err := os.Open(filename) - if err != nil { - return nil, err - } - data, err := ioutil.ReadAll(f) + data, err := os.ReadFile(filename) if err != nil { return nil, err } @@ -156,8 +151,7 @@ func CreateModel(name string, mf io.Reader, fn func(status string)) error { } var layers []*LayerWithBuffer - var param map[string]string - param = make(map[string]string) + param := make(map[string]string) for _, c := range commands { log.Printf("[%s] - %s\n", c.Name, c.Arg) @@ -360,10 +354,10 @@ func GetLayerWithBufferFromLayer(layer *Layer) (*LayerWithBuffer, error) { fp := path.Join(home, ".ollama/models/blobs", layer.Digest) file, err := os.Open(fp) - defer file.Close() if err != nil { - return nil, err + return nil, fmt.Errorf("could not open blob: %w", err) } + defer file.Close() newLayer, err := CreateLayer(file) if err != nil { @@ -386,7 +380,7 @@ func getLayerDigests(layers []*LayerWithBuffer) ([]string, error) { var digests []string for _, l := range layers { if l.Digest == "" { - return nil, fmt.Errorf("layer is missing a digest!") + return nil, fmt.Errorf("layer is missing a digest") } digests = append(digests, l.Digest) } @@ -496,7 +490,7 @@ func PushModel(name, username, password string, fn func(status, digest string, T // Check for success: For a successful upload, the Docker registry will respond with a 201 Created if resp.StatusCode != http.StatusCreated { - body, _ := ioutil.ReadAll(resp.Body) + body, _ := io.ReadAll(resp.Body) return fmt.Errorf("registry responded with code %d: %v", resp.StatusCode, string(body)) } @@ -594,7 +588,7 @@ func pullModelManifest(registryURL, repoName, tag, username, password string) (* // Check for success: For a successful upload, the Docker registry will respond with a 201 Created if resp.StatusCode != http.StatusOK { - body, _ := ioutil.ReadAll(resp.Body) + body, _ := io.ReadAll(resp.Body) return nil, fmt.Errorf("registry responded with code %d: %v", resp.StatusCode, string(body)) } @@ -655,14 +649,14 @@ func startUpload(registryURL string, repositoryName string, username string, pas // Check for success if resp.StatusCode != http.StatusAccepted { - body, _ := ioutil.ReadAll(resp.Body) + body, _ := io.ReadAll(resp.Body) return "", fmt.Errorf("registry responded with code %d: %v", resp.StatusCode, string(body)) } // Extract UUID location from header location := resp.Header.Get("Location") if location == "" { - return "", fmt.Errorf("Location header is missing in response") + return "", fmt.Errorf("location header is missing in response") } return location, nil @@ -716,7 +710,7 @@ func uploadBlob(location string, layer *Layer, username string, password string) // Check for success: For a successful upload, the Docker registry will respond with a 201 Created if resp.StatusCode != http.StatusCreated { - body, _ := ioutil.ReadAll(resp.Body) + body, _ := io.ReadAll(resp.Body) return fmt.Errorf("registry responded with code %d: %v", resp.StatusCode, string(body)) } @@ -751,7 +745,7 @@ func downloadBlob(registryURL, repoName, digest, username, password string) erro // TODO: handle range requests to make this resumable if resp.StatusCode != http.StatusOK { - body, _ := ioutil.ReadAll(resp.Body) + body, _ := io.ReadAll(resp.Body) return fmt.Errorf("registry responded with code %d: %v", resp.StatusCode, string(body)) } diff --git a/server/models.go b/server/models.go index dee2adde3..a8d175f63 100644 --- a/server/models.go +++ b/server/models.go @@ -1,20 +1,15 @@ package server import ( - "encoding/json" - "errors" "fmt" - "io" - "net/http" "os" + "path" "path/filepath" "strconv" "github.com/jmorganca/ollama/api" ) -const directoryURL = "https://ollama.ai/api/models" - type Model struct { Name string `json:"name"` ModelPath string @@ -31,47 +26,6 @@ type Model struct { License string `json:"license"` } -func (m *Model) FullName() string { - home, err := os.UserHomeDir() - if err != nil { - panic(err) - } - - return filepath.Join(home, ".ollama", "models", m.Name+".bin") -} - -func (m *Model) TempFile() string { - fullName := m.FullName() - return filepath.Join( - filepath.Dir(fullName), - fmt.Sprintf(".%s.part", filepath.Base(fullName)), - ) -} - -func getRemote(model string) (*Model, error) { - // resolve the model download from our directory - resp, err := http.Get(directoryURL) - if err != nil { - return nil, fmt.Errorf("failed to get directory: %w", err) - } - defer resp.Body.Close() - body, err := io.ReadAll(resp.Body) - if err != nil { - return nil, fmt.Errorf("failed to read directory: %w", err) - } - var models []Model - err = json.Unmarshal(body, &models) - if err != nil { - return nil, fmt.Errorf("failed to parse directory: %w", err) - } - for _, m := range models { - if m.Name == model { - return &m, nil - } - } - return nil, fmt.Errorf("model not found in directory: %s", model) -} - func saveModel(model *Model, fn func(total, completed int64)) error { // this models cache directory is created by the server on startup diff --git a/server/routes.go b/server/routes.go index eed5756a6..e59b00b14 100644 --- a/server/routes.go +++ b/server/routes.go @@ -1,12 +1,10 @@ package server import ( - "embed" "encoding/json" "fmt" "io" "log" - "math" "net" "net/http" "os" @@ -16,7 +14,6 @@ import ( "time" "github.com/gin-gonic/gin" - "github.com/lithammer/fuzzysearch/fuzzy" "github.com/jmorganca/ollama/api" "github.com/jmorganca/ollama/llama" @@ -203,18 +200,6 @@ func Serve(ln net.Listener) error { return s.Serve(ln) } -func matchRankOne(source string, targets []string) (bestMatch string, bestRank int) { - bestRank = math.MaxInt - for _, target := range targets { - if rank := fuzzy.LevenshteinDistance(source, target); bestRank > rank { - bestRank = rank - bestMatch = target - } - } - - return -} - func streamResponse(c *gin.Context, ch chan any) { c.Stream(func(w io.Writer) bool { val, ok := <-ch