瀏覽代碼

removed client isLocal()

Josh Yan 9 月之前
父節點
當前提交
8377dc48d0
共有 3 個文件被更改,包括 17 次插入135 次删除
  1. 0 20
      api/client.go
  2. 0 79
      api/client_test.go
  3. 17 36
      cmd/cmd.go

+ 0 - 20
api/client.go

@@ -381,26 +381,6 @@ func (c *Client) Version(ctx context.Context) (string, error) {
 	return version.Version, nil
 	return version.Version, nil
 }
 }
 
 
-// IsLocal checks whether the client is connecting to a local server.
-func (c *Client) IsLocal() bool {
-	// Resolve the host to an IP address and check if the IP is local
-	// Currently, only checks if it is localhost or loopback
-	host, _, err := net.SplitHostPort(c.base.Host)
-	if err != nil {
-		host = c.base.Host
-	}
-
-	if host == "" || host == "localhost" {
-		return true
-	}
-
-	if ip := net.ParseIP(host); ip != nil {
-		return ip.IsLoopback()
-	}
-
-	return false
-}
-
 func Authorization(ctx context.Context, request *http.Request) (string, error) {
 func Authorization(ctx context.Context, request *http.Request) (string, error) {
 	data := []byte(fmt.Sprintf("%s,%s,%d", request.Method, request.URL.RequestURI(), time.Now().Unix()))
 	data := []byte(fmt.Sprintf("%s,%s,%d", request.Method, request.URL.RequestURI(), time.Now().Unix()))
 
 

+ 0 - 79
api/client_test.go

@@ -1,8 +1,6 @@
 package api
 package api
 
 
 import (
 import (
-	"net/http"
-	"net/url"
 	"testing"
 	"testing"
 
 
 	"github.com/ollama/ollama/envconfig"
 	"github.com/ollama/ollama/envconfig"
@@ -48,80 +46,3 @@ func TestClientFromEnvironment(t *testing.T) {
 		})
 		})
 	}
 	}
 }
 }
-
-// Test function
-func TestIsLocal(t *testing.T) {
-	type test struct {
-		client *Client
-		want   bool
-		err    error
-	}
-
-	tests := map[string]test{
-		"localhost": {
-			client: func() *Client {
-				baseURL, _ := url.Parse("http://localhost:1234")
-				return &Client{base: baseURL, http: &http.Client{}}
-			}(),
-			want: true,
-			err:  nil,
-		},
-		"127.0.0.1": {
-			client: func() *Client {
-				baseURL, _ := url.Parse("http://127.0.0.1:1234")
-				return &Client{base: baseURL, http: &http.Client{}}
-			}(),
-			want: true,
-			err:  nil,
-		},
-		"example.com": {
-			client: func() *Client {
-				baseURL, _ := url.Parse("http://example.com:1111")
-				return &Client{base: baseURL, http: &http.Client{}}
-			}(),
-			want: false,
-			err:  nil,
-		},
-		"8.8.8.8": {
-			client: func() *Client {
-				baseURL, _ := url.Parse("http://8.8.8.8:1234")
-				return &Client{base: baseURL, http: &http.Client{}}
-			}(),
-			want: false,
-			err:  nil,
-		},
-		"empty host with port": {
-			client: func() *Client {
-				baseURL, _ := url.Parse("http://:1234")
-				return &Client{base: baseURL, http: &http.Client{}}
-			}(),
-			want: true,
-			err:  nil,
-		},
-		"empty host without port": {
-			client: func() *Client {
-				baseURL, _ := url.Parse("http://")
-				return &Client{base: baseURL, http: &http.Client{}}
-			}(),
-			want: true,
-			err:  nil,
-		},
-		"remote host without port": {
-			client: func() *Client {
-				baseURL, _ := url.Parse("http://example.com")
-				return &Client{base: baseURL, http: &http.Client{}}
-			}(),
-			want: false,
-			err:  nil,
-		},
-	}
-
-	for name, tc := range tests {
-		t.Run(name, func(t *testing.T) {
-			got := tc.client.IsLocal()
-			if got != tc.want {
-				t.Errorf("test %s failed: got %v, want %v", name, got, tc.want)
-			}
-		})
-	}
-}

+ 17 - 36
cmd/cmd.go

@@ -285,52 +285,33 @@ func createBlob(cmd *cobra.Command, client *api.Client, path string) (string, er
 
 
 	digest := fmt.Sprintf("sha256:%x", hash.Sum(nil))
 	digest := fmt.Sprintf("sha256:%x", hash.Sum(nil))
 
 
-	// Here, we want to check if the server is local
-	// If true, call, createBlobLocal
-	// This should find the model directory, copy blob over, and return the digest
-	// If this fails, just upload it
-	// If this is successful, return the digest
-
-	// Resolve server to IP
-	// Check if server is local
-	/* if client.IsLocal() {
-		digest = strings.ReplaceAll(digest, ":", "-")
-		config, err := client.HeadBlob(cmd.Context(), digest)
-		if err != nil {
-			return "", err
-		}
-
-		modelDir := config.ModelDir
+	// We check if we can find the models directory locally
+	// If we can, we return the path to the directory
+	// If we can't, we return an error
+	// If the blob exists already, we return the digest
+	dest, err := getLocalPath(cmd.Context(), digest)
 
 
-		// Get blob destination
-
-		dest := filepath.Join(modelDir, "blobs", digest)
+	if errors.Is(err, ErrBlobExists) {
+		return digest, nil
+	}
 
 
-		err = createBlobLocal(path, dest)
+	// Successfuly found the model directory
+	if err == nil {
+		// Copy blob in via OS specific copy
+		// Linux errors out to use io.copy
+		err = localCopy(path, dest)
 		if err == nil {
 		if err == nil {
 			return digest, nil
 			return digest, nil
 		}
 		}
-	} */
-	if client.IsLocal() {
-		dest, err := getLocalPath(cmd.Context(), digest)
-
-		if errors.Is(err, ErrBlobExists) {
-			return digest, nil
-		}
 
 
+		// Default copy using io.copy
+		err = defaultCopy(path, dest)
 		if err == nil {
 		if err == nil {
-			err = localCopy(path, dest)
-			if err == nil {
-				return digest, nil
-			}
-
-			err = defaultCopy(path, dest)
-			if err == nil {
-				return digest, nil
-			}
+			return digest, nil
 		}
 		}
 	}
 	}
 
 
+	// If at any point copying the blob over locally fails, we default to the copy through the server
 	if err = client.CreateBlob(cmd.Context(), digest, bin); err != nil {
 	if err = client.CreateBlob(cmd.Context(), digest, bin); err != nil {
 		return "", err
 		return "", err
 	}
 	}