Skip to content

Commit

Permalink
fix: failed to call cli
Browse files Browse the repository at this point in the history
  • Loading branch information
thxCode committed Aug 8, 2024
1 parent 0d2e92a commit f8cbd07
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 32 deletions.
2 changes: 1 addition & 1 deletion file_from_distro.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ func ParseGGUFFileFromOllamaModel(ctx context.Context, model *OllamaModel, opts
WithTimeout(0).
WithRetryBackoff(1*time.Second, 5*time.Second, 10).
WithRetryIf(func(resp *http.Response, err error) bool {
return OllamaRegistryAuthorizeRetry(cli, resp) || httpx.DefaultRetry(resp, err)
return httpx.DefaultRetry(resp, err) || OllamaRegistryAuthorizeRetry(resp, cli)
}).
WithTransport(
httpx.TransportOptions().
Expand Down
16 changes: 4 additions & 12 deletions file_option.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package gguf_parser

import (
"net/url"
"os"
"path/filepath"
"runtime"
"strings"
Expand Down Expand Up @@ -110,16 +109,9 @@ func SkipRangeDownloadDetection() GGUFReadOption {

// UseCache caches the remote reading result.
func UseCache() GGUFReadOption {
var cd string
{
hd, err := os.UserHomeDir()
if err != nil {
hd = filepath.Join(os.TempDir(), time.Now().Format(time.DateOnly))
}
cd = filepath.Join(hd, ".cache")
if runtime.GOOS == "windows" {
cd = osx.Getenv("APPDATA", cd)
}
cd := filepath.Join(osx.UserHomeDir(), ".cache")
if runtime.GOOS == "windows" {
cd = osx.Getenv("APPDATA", cd)
}
return func(o *_GGUFReadOptions) {
o.CachePath = filepath.Join(cd, "gguf-parser")
Expand All @@ -137,7 +129,7 @@ func SkipCache() GGUFReadOption {

// UseCachePath uses the given path to cache the remote reading result.
func UseCachePath(path string) GGUFReadOption {
path = strings.TrimSpace(filepath.Clean(path))
path = strings.TrimSpace(filepath.Clean(osx.InlineTilde(path)))
return func(o *_GGUFReadOptions) {
if path == "" {
return
Expand Down
19 changes: 8 additions & 11 deletions ollama_registry_authenticate.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (

"golang.org/x/crypto/ssh"

"github.com/gpustack/gguf-parser-go/util/funcx"
"github.com/gpustack/gguf-parser-go/util/httpx"
"github.com/gpustack/gguf-parser-go/util/osx"
"github.com/gpustack/gguf-parser-go/util/stringx"
Expand All @@ -42,7 +43,11 @@ func OllamaUserAgent() string {
//
// OllamaRegistryAuthorizeRetry leverages OllamaRegistryAuthorize to obtain an authorization token,
// and configures the request with the token.
func OllamaRegistryAuthorizeRetry(cli *http.Client, resp *http.Response) bool {
func OllamaRegistryAuthorizeRetry(resp *http.Response, cli *http.Client) bool {
if resp == nil || cli == nil {
return false
}

if resp.StatusCode != http.StatusUnauthorized && resp.Request == nil {
// Not unauthorized, return.
return false
Expand All @@ -60,11 +65,7 @@ func OllamaRegistryAuthorizeRetry(cli *http.Client, resp *http.Response) bool {
// No authentication token, return.
return false
}
authzToken, err := OllamaRegistryAuthorize(req.Context(), cli, authnToken)
if err != nil {
// Failed to authorize, return.
return false
}
authzToken := funcx.MustNoError(OllamaRegistryAuthorize(req.Context(), cli, authnToken))
req.Header.Set(httpHeaderAuthorization, tokenPrefix+authzToken)
return true
}
Expand Down Expand Up @@ -170,11 +171,7 @@ func OllamaRegistryAuthorize(ctx context.Context, cli *http.Client, authnToken s
// OllamaSingKeyLoad loads the signing key for Ollama,
// and generates a new key if not exists.
func OllamaSingKeyLoad() (ssh.Signer, error) {
hd, err := os.UserHomeDir()
if err != nil {
return nil, err
}
hd = filepath.Join(hd, ".ollama")
hd := filepath.Join(osx.UserHomeDir(), ".ollama")

priKeyPath := filepath.Join(hd, "id_ed25519")
if !osx.ExistsFile(priKeyPath) {
Expand Down
9 changes: 1 addition & 8 deletions util/osx/file_mmap.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ import (
"os"
"path/filepath"
"runtime/debug"
"strings"
"syscall"
)

Expand All @@ -35,13 +34,7 @@ func OpenMmapFile(path string) (*MmapFile, error) {

func OpenMmapFileWithSize(path string, size int) (*MmapFile, error) {
p := filepath.Clean(path)
if strings.HasPrefix(p, "~"+string(filepath.Separator)) {
hd, err := os.UserHomeDir()
if err != nil {
return nil, err
}
p = filepath.Join(hd, p[2:])
}
p = InlineTilde(p)

f, err := os.Open(p)
if err != nil {
Expand Down
17 changes: 17 additions & 0 deletions util/osx/homedir.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package osx

import (
"os"
"path/filepath"
"time"
)

// UserHomeDir is similar to os.UserHomeDir,
// but returns the temp dir if the home dir is not found.
func UserHomeDir() string {
hd, err := os.UserHomeDir()
if err != nil {
hd = filepath.Join(os.TempDir(), time.Now().Format(time.DateOnly))
}
return hd
}

0 comments on commit f8cbd07

Please sign in to comment.