Prechádzať zdrojové kódy

cmd: use environment variables for server options

Jeffrey Morgan 1 rok pred
rodič
commit
7e26a8df31
3 zmenil súbory, kde vykonal 35 pridanie a 135 odobranie
  1. 18 32
      cmd/cmd.go
  2. 0 103
      cmd/cmd_test.go
  3. 17 0
      docs/faq.md

+ 18 - 32
cmd/cmd.go

@@ -523,36 +523,21 @@ func generateBatch(cmd *cobra.Command, model string) error {
 	return nil
 	return nil
 }
 }
 
 
-// getRunServerParams takes a command and the environment variables and returns the correct params
-// given the order of precedence: command line args (highest), environment variables, defaults (lowest)
-func getRunServerParams(cmd *cobra.Command) (host, port string, extraOrigins []string, err error) {
-	host = os.Getenv("OLLAMA_HOST")
-	hostFlag := cmd.Flags().Lookup("host")
-	if hostFlag == nil {
-		return "", "", nil, errors.New("host unset")
-	}
-	if hostFlag.Changed || host == "" {
-		host = hostFlag.Value.String()
-	}
-	port = os.Getenv("OLLAMA_PORT")
-	portFlag := cmd.Flags().Lookup("port")
-	if portFlag == nil {
-		return "", "", nil, errors.New("port unset")
-	}
-	if portFlag.Changed || port == "" {
-		port = portFlag.Value.String()
-	}
-	extraOrigins, err = cmd.Flags().GetStringSlice("origins")
-	if err != nil {
-		return "", "", nil, err
+func RunServer(cmd *cobra.Command, _ []string) error {
+	var host, port = "127.0.0.1", "11434"
+
+	parts := strings.Split(os.Getenv("OLLAMA_HOST"), ":")
+	if ip := net.ParseIP(parts[0]); ip != nil {
+		host = ip.String()
 	}
 	}
-	return host, port, extraOrigins, nil
-}
 
 
-func RunServer(cmd *cobra.Command, _ []string) error {
-	host, port, origins, err := getRunServerParams(cmd)
-	if err != nil {
-		return err
+	if len(parts) > 1 {
+		port = parts[1]
+	}
+
+	// deprecated: include port in OLLAMA_HOST
+	if p := os.Getenv("OLLAMA_PORT"); p != "" {
+		port = p
 	}
 	}
 
 
 	ln, err := net.Listen("tcp", fmt.Sprintf("%s:%s", host, port))
 	ln, err := net.Listen("tcp", fmt.Sprintf("%s:%s", host, port))
@@ -560,6 +545,11 @@ func RunServer(cmd *cobra.Command, _ []string) error {
 		return err
 		return err
 	}
 	}
 
 
+	var origins []string
+	if o := os.Getenv("OLLAMA_ORIGINS"); o != "" {
+		origins = strings.Split(o, ",")
+	}
+
 	return server.Serve(ln, origins)
 	return server.Serve(ln, origins)
 }
 }
 
 
@@ -652,10 +642,6 @@ func NewCLI() *cobra.Command {
 		RunE:    RunServer,
 		RunE:    RunServer,
 	}
 	}
 
 
-	serveCmd.Flags().String("port", "11434", "Port to listen on")
-	serveCmd.Flags().String("host", "127.0.0.1", "Host to listen on")
-	serveCmd.Flags().StringSlice("origins", nil, "Additional allowed CORS origins as comma-separated list (e.g. http://192.168.1.24:3000)")
-
 	pullCmd := &cobra.Command{
 	pullCmd := &cobra.Command{
 		Use:     "pull MODEL",
 		Use:     "pull MODEL",
 		Short:   "Pull a model from a registry",
 		Short:   "Pull a model from a registry",

+ 0 - 103
cmd/cmd_test.go

@@ -1,103 +0,0 @@
-package cmd
-
-import (
-	"os"
-	"testing"
-)
-
-func TestGetRunServerParams(t *testing.T) {
-	t.Run("default values", func(t *testing.T) {
-		cmd := NewCLI()
-		serveCmd, _, err := cmd.Find([]string{"serve"})
-		if err != nil {
-			t.Errorf("expected serve command, got %s", err)
-		}
-		host, port, extraOrigins, err := getRunServerParams(serveCmd)
-		// assertions
-		if err != nil {
-			t.Errorf("unexpected error, got %s", err)
-		}
-		if host != "127.0.0.1" {
-			t.Errorf("unexpected host, got %s", host)
-		}
-		if port != "11434" {
-			t.Errorf("unexpected port, got %s", port)
-		}
-		if len(extraOrigins) != 0 {
-			t.Errorf("unexpected origins, got %s", extraOrigins)
-		}
-	})
-	t.Run("environment variables take precedence over default", func(t *testing.T) {
-		cmd := NewCLI()
-		serveCmd, _, err := cmd.Find([]string{"serve"})
-		if err != nil {
-			t.Errorf("expected serve command, got %s", err)
-		}
-		// setup environment variables
-		err = os.Setenv("OLLAMA_HOST", "0.0.0.0")
-		if err != nil {
-			t.Errorf("could not set env var")
-		}
-		err = os.Setenv("OLLAMA_PORT", "9999")
-		if err != nil {
-			t.Errorf("could not set env var")
-		}
-		defer func() {
-			os.Unsetenv("OLLAMA_HOST")
-			os.Unsetenv("OLLAMA_PORT")
-		}()
-
-		host, port, extraOrigins, err := getRunServerParams(serveCmd)
-		// assertions
-		if err != nil {
-			t.Errorf("unexpected error, got %s", err)
-		}
-		if host != "0.0.0.0" {
-			t.Errorf("unexpected host, got %s", host)
-		}
-		if port != "9999" {
-			t.Errorf("unexpected port, got %s", port)
-		}
-		if len(extraOrigins) != 0 {
-			t.Errorf("unexpected origins, got %s", extraOrigins)
-		}
-	})
-	t.Run("command line args take precedence over env vars", func(t *testing.T) {
-		cmd := NewCLI()
-		serveCmd, _, err := cmd.Find([]string{"serve"})
-		if err != nil {
-			t.Errorf("expected serve command, got %s", err)
-		}
-		// setup environment variables
-		err = os.Setenv("OLLAMA_HOST", "0.0.0.0")
-		if err != nil {
-			t.Errorf("could not set env var")
-		}
-		err = os.Setenv("OLLAMA_PORT", "9999")
-		if err != nil {
-			t.Errorf("could not set env var")
-		}
-		defer func() {
-			os.Unsetenv("OLLAMA_HOST")
-			os.Unsetenv("OLLAMA_PORT")
-		}()
-		// now set command flags
-		serveCmd.Flags().Set("host", "localhost")
-		serveCmd.Flags().Set("port", "8888")
-		serveCmd.Flags().Set("origins", "http://foo.example.com,http://192.168.1.1")
-
-		host, port, extraOrigins, err := getRunServerParams(serveCmd)
-		if err != nil {
-			t.Errorf("unexpected error, got %s", err)
-		}
-		if host != "localhost" {
-			t.Errorf("unexpected host, got %s", host)
-		}
-		if port != "8888" {
-			t.Errorf("unexpected port, got %s", port)
-		}
-		if len(extraOrigins) != 2 {
-			t.Errorf("expected two origins, got length %d", len(extraOrigins))
-		}
-	})
-}

+ 17 - 0
docs/faq.md

@@ -0,0 +1,17 @@
+# FAQ
+
+## How can I expose the Ollama server?
+
+```
+OLLAMA_HOST=0.0.0.0:11435 ollama serve
+```
+
+By default, Ollama allows cross origin requests from `127.0.0.1` and `0.0.0.0`. To support more origins, you can use the `OLLAMA_ORIGINS` environment variable:
+
+```
+OLLAMA_ORIGINS=http://192.168.1.1:*,https://example.com ollama serve
+```
+
+## Where are models stored?
+
+Raw model data is stored under `~/.ollama/models`.