local path

This commit is contained in:
Josh Yan 2024-07-03 17:01:09 -07:00
parent d14d38e940
commit 56008688a1
2 changed files with 9 additions and 29 deletions

View File

@ -319,26 +319,20 @@ func createBlob(cmd *cobra.Command, client *api.Client, path string) (string, er
return "", err return "", err
} }
fmt.Println("HI") dest := config
dest := config.ModelDir
fmt.Println("dest is ", dest)
err = createBlobLocal(path, dest) err = createBlobLocal(path, dest)
if err == nil { if err == nil {
fmt.Println("createlocalblob succeed")
return digest, nil return digest, nil
} }
fmt.Println("err is ", err)
fmt.Println("createlocalblob faileds")
} }
fmt.Println("DEFAULT")
if err = client.CreateBlob(cmd.Context(), digest, bin); err != nil { if err = client.CreateBlob(cmd.Context(), digest, bin); err != nil {
return "", err return "", err
} }
return digest, nil return digest, nil
} }
func getLocalPath(ctx context.Context, digest string) (*api.ServerConfig, error) { func getLocalPath(ctx context.Context, digest string) (string, error) {
ollamaHost := envconfig.Host ollamaHost := envconfig.Host
client := http.DefaultClient client := http.DefaultClient
@ -348,10 +342,9 @@ func getLocalPath(ctx context.Context, digest string) (*api.ServerConfig, error)
} }
var reqBody io.Reader var reqBody io.Reader
var respData api.ServerConfig
data, err := json.Marshal(digest) data, err := json.Marshal(digest)
if err != nil { if err != nil {
return nil, err return "", err
} }
reqBody = bytes.NewReader(data) reqBody = bytes.NewReader(data)
@ -359,7 +352,7 @@ func getLocalPath(ctx context.Context, digest string) (*api.ServerConfig, error)
requestURL := base.JoinPath(path) requestURL := base.JoinPath(path)
request, err := http.NewRequestWithContext(ctx, http.MethodPost, requestURL.String(), reqBody) request, err := http.NewRequestWithContext(ctx, http.MethodPost, requestURL.String(), reqBody)
if err != nil { if err != nil {
return nil, err return "", err
} }
request.Header.Set("Content-Type", "application/json") request.Header.Set("Content-Type", "application/json")
@ -367,38 +360,26 @@ func getLocalPath(ctx context.Context, digest string) (*api.ServerConfig, error)
request.Header.Set("User-Agent", fmt.Sprintf("ollama/%s (%s %s) Go/%s", version.Version, runtime.GOARCH, runtime.GOOS, runtime.Version())) request.Header.Set("User-Agent", fmt.Sprintf("ollama/%s (%s %s) Go/%s", version.Version, runtime.GOARCH, runtime.GOOS, runtime.Version()))
request.Header.Set("X-Redirect-Create", "1") request.Header.Set("X-Redirect-Create", "1")
fmt.Println("request", request)
resp, err := client.Do(request) resp, err := client.Do(request)
if err != nil { if err != nil {
return nil, err return "", err
} }
defer resp.Body.Close() defer resp.Body.Close()
fmt.Println("made it here")
fmt.Println("resp", resp)
if resp.StatusCode == http.StatusTemporaryRedirect { if resp.StatusCode == http.StatusTemporaryRedirect {
fmt.Println("redirect") dest := resp.Header.Get("LocalLocation")
if err := json.Unmarshal([]byte(resp.Header.Get("loc")), &respData); err != nil {
fmt.Println("error unmarshalling response data")
return nil, err
}
return &respData, nil return dest, nil
} }
return "", ErrBlobExists
fmt.Println("!!!!!!!!!!")
fmt.Println(respData)
return nil, ErrBlobExists
} }
func createBlobLocal(path string, dest string) error { func createBlobLocal(path string, dest string) error {
// This function should be called if the server is local // This function should be called if the server is local
// It should find the model directory, copy the blob over, and return the digest // It should find the model directory, copy the blob over, and return the digest
dirPath := filepath.Dir(dest) dirPath := filepath.Dir(dest)
fmt.Println("dirpath is ", dirPath)
if err := os.MkdirAll(dirPath, 0o755); err != nil { if err := os.MkdirAll(dirPath, 0o755); err != nil {
fmt.Println("failed to create directory")
return err return err
} }

View File

@ -942,8 +942,7 @@ func (s *Server) CreateBlobHandler(c *gin.Context) {
} }
if c.GetHeader("X-Redirect-Create") == "1" { if c.GetHeader("X-Redirect-Create") == "1" {
c.Header("Location", path) c.Header("LocalLocation", path)
fmt.Println("!!!!!!!!!", string(path))
c.Status(http.StatusTemporaryRedirect) c.Status(http.StatusTemporaryRedirect)
return return
} }