fix go warnings

This commit is contained in:
Jeffrey Morgan 2023-07-16 10:53:37 -07:00 committed by Patrick Devine
parent be233da145
commit 95cc9a11db
3 changed files with 12 additions and 79 deletions

View File

@ -7,7 +7,6 @@ import (
"encoding/json" "encoding/json"
"fmt" "fmt"
"io" "io"
"io/ioutil"
"log" "log"
"net/http" "net/http"
"os" "os"
@ -102,11 +101,7 @@ func GetModel(name string) (*Model, error) {
case "application/vnd.ollama.image.model": case "application/vnd.ollama.image.model":
model.ModelPath = filename model.ModelPath = filename
case "application/vnd.ollama.image.prompt": case "application/vnd.ollama.image.prompt":
f, err := os.Open(filename) data, err := os.ReadFile(filename)
if err != nil {
return nil, err
}
data, err := ioutil.ReadAll(f)
if err != nil { if err != nil {
return nil, err return nil, err
} }
@ -156,8 +151,7 @@ func CreateModel(name string, mf io.Reader, fn func(status string)) error {
} }
var layers []*LayerWithBuffer var layers []*LayerWithBuffer
var param map[string]string param := make(map[string]string)
param = make(map[string]string)
for _, c := range commands { for _, c := range commands {
log.Printf("[%s] - %s\n", c.Name, c.Arg) 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) fp := path.Join(home, ".ollama/models/blobs", layer.Digest)
file, err := os.Open(fp) file, err := os.Open(fp)
defer file.Close()
if err != nil { if err != nil {
return nil, err return nil, fmt.Errorf("could not open blob: %w", err)
} }
defer file.Close()
newLayer, err := CreateLayer(file) newLayer, err := CreateLayer(file)
if err != nil { if err != nil {
@ -386,7 +380,7 @@ func getLayerDigests(layers []*LayerWithBuffer) ([]string, error) {
var digests []string var digests []string
for _, l := range layers { for _, l := range layers {
if l.Digest == "" { 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) 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 // Check for success: For a successful upload, the Docker registry will respond with a 201 Created
if resp.StatusCode != http.StatusCreated { 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)) 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 // Check for success: For a successful upload, the Docker registry will respond with a 201 Created
if resp.StatusCode != http.StatusOK { 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)) 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 // Check for success
if resp.StatusCode != http.StatusAccepted { 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)) return "", fmt.Errorf("registry responded with code %d: %v", resp.StatusCode, string(body))
} }
// Extract UUID location from header // Extract UUID location from header
location := resp.Header.Get("Location") location := resp.Header.Get("Location")
if location == "" { if location == "" {
return "", fmt.Errorf("Location header is missing in response") return "", fmt.Errorf("location header is missing in response")
} }
return location, nil 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 // Check for success: For a successful upload, the Docker registry will respond with a 201 Created
if resp.StatusCode != http.StatusCreated { 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)) 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 // TODO: handle range requests to make this resumable
if resp.StatusCode != http.StatusOK { 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)) return fmt.Errorf("registry responded with code %d: %v", resp.StatusCode, string(body))
} }

View File

@ -1,20 +1,15 @@
package server package server
import ( import (
"encoding/json"
"errors"
"fmt" "fmt"
"io"
"net/http"
"os" "os"
"path"
"path/filepath" "path/filepath"
"strconv" "strconv"
"github.com/jmorganca/ollama/api" "github.com/jmorganca/ollama/api"
) )
const directoryURL = "https://ollama.ai/api/models"
type Model struct { type Model struct {
Name string `json:"name"` Name string `json:"name"`
ModelPath string ModelPath string
@ -31,47 +26,6 @@ type Model struct {
License string `json:"license"` 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 { func saveModel(model *Model, fn func(total, completed int64)) error {
// this models cache directory is created by the server on startup // this models cache directory is created by the server on startup

View File

@ -1,12 +1,10 @@
package server package server
import ( import (
"embed"
"encoding/json" "encoding/json"
"fmt" "fmt"
"io" "io"
"log" "log"
"math"
"net" "net"
"net/http" "net/http"
"os" "os"
@ -16,7 +14,6 @@ import (
"time" "time"
"github.com/gin-gonic/gin" "github.com/gin-gonic/gin"
"github.com/lithammer/fuzzysearch/fuzzy"
"github.com/jmorganca/ollama/api" "github.com/jmorganca/ollama/api"
"github.com/jmorganca/ollama/llama" "github.com/jmorganca/ollama/llama"
@ -203,18 +200,6 @@ func Serve(ln net.Listener) error {
return s.Serve(ln) 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) { func streamResponse(c *gin.Context, ch chan any) {
c.Stream(func(w io.Writer) bool { c.Stream(func(w io.Writer) bool {
val, ok := <-ch val, ok := <-ch